* lisp/gnus.el (gnus-version-number): Update to 6.10.059.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'custom)
33 (require 'gnus)
34 (require 'gnus-sum)
35 (require 'gnus-spec)
36 (require 'gnus-int)
37 (require 'browse-url)
38 (require 'alist)
39 (require 'mime-view)
40
41 ;; Avoid byte-compile warnings.
42 (eval-when-compile
43   (defvar gnus-article-decoded-p)
44   (defvar gnus-article-mime-handles)
45   (require 'mm-bodies)
46   (require 'mail-parse)
47   (require 'mm-decode)
48   (require 'mm-view)
49   (require 'wid-edit)
50   (require 'mm-uu)
51   )
52
53 (defgroup gnus-article nil
54   "Article display."
55   :link '(custom-manual "(gnus)The Article Buffer")
56   :group 'gnus)
57
58 (defgroup gnus-article-treat nil
59   "Treating article parts."
60   :link '(custom-manual "(gnus)Article Hiding")
61   :group 'gnus-article)
62
63 (defgroup gnus-article-hiding nil
64   "Hiding article parts."
65   :link '(custom-manual "(gnus)Article Hiding")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-highlight nil
69   "Article highlighting."
70   :link '(custom-manual "(gnus)Article Highlighting")
71   :group 'gnus-article
72   :group 'gnus-visual)
73
74 (defgroup gnus-article-signature nil
75   "Article signatures."
76   :link '(custom-manual "(gnus)Article Signature")
77   :group 'gnus-article)
78
79 (defgroup gnus-article-headers nil
80   "Article headers."
81   :link '(custom-manual "(gnus)Hiding Headers")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-washing nil
85   "Special commands on articles."
86   :link '(custom-manual "(gnus)Article Washing")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-emphasis nil
90   "Fontisizing articles."
91   :link '(custom-manual "(gnus)Article Fontisizing")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-saving nil
95   "Saving articles."
96   :link '(custom-manual "(gnus)Saving Articles")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-mime nil
100   "Worshiping the MIME wonder."
101   :link '(custom-manual "(gnus)Using MIME")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-buttons nil
105   "Pushable buttons in the article buffer."
106   :link '(custom-manual "(gnus)Article Buttons")
107   :group 'gnus-article)
108
109 (defgroup gnus-article-various nil
110   "Other article options."
111   :link '(custom-manual "(gnus)Misc Article")
112   :group 'gnus-article)
113
114 (defcustom gnus-ignored-headers
115   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
116     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
117     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
118     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
119     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
120     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
121     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
122     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
123     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
124     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
125     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
126     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
127     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
128     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
129     "^Old-Received:" "^X-Pgp-Fingerprint:" "^X-Pgp-Key-Id:"
130     "^X-Pgp-Public-Key-Url:" "^X-Auth:" "^X-From-Line:"
131     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
132     "^X-Mailing-List:" "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
133     "^Status:")
134   "*All headers that start with this regexp will be hidden.
135 This variable can also be a list of regexps of headers to be ignored.
136 If `gnus-visible-headers' is non-nil, this variable will be ignored."
137   :type '(choice :custom-show nil
138                  regexp
139                  (repeat regexp))
140   :group 'gnus-article-hiding)
141
142 (defcustom gnus-visible-headers
143   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
144   "*All headers that do not match this regexp will be hidden.
145 This variable can also be a list of regexp of headers to remain visible.
146 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
147   :type '(repeat :value-to-internal (lambda (widget value)
148                                       (custom-split-regexp-maybe value))
149                  :match (lambda (widget value)
150                           (or (stringp value)
151                               (widget-editable-list-match widget value)))
152                  regexp)
153   :group 'gnus-article-hiding)
154
155 (defcustom gnus-sorted-header-list
156   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
157     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
158   "*This variable is a list of regular expressions.
159 If it is non-nil, headers that match the regular expressions will
160 be placed first in the article buffer in the sequence specified by
161 this list."
162   :type '(repeat regexp)
163   :group 'gnus-article-hiding)
164
165 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
166   "Headers that are only to be displayed if they have interesting data.
167 Possible values in this list are `empty', `newsgroups', `followup-to',
168 `reply-to', `date', `long-to', and `many-to'."
169   :type '(set (const :tag "Headers with no content." empty)
170               (const :tag "Newsgroups with only one group." newsgroups)
171               (const :tag "Followup-to identical to newsgroups." followup-to)
172               (const :tag "Reply-to identical to from." reply-to)
173               (const :tag "Date less than four days old." date)
174               (const :tag "Very long To header." long-to)
175               (const :tag "Multiple To headers." many-to))
176   :group 'gnus-article-hiding)
177
178 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
179   "Regexp matching signature separator.
180 This can also be a list of regexps.  In that case, it will be checked
181 from head to tail looking for a separator.  Searches will be done from
182 the end of the buffer."
183   :type '(repeat string)
184   :group 'gnus-article-signature)
185
186 (defcustom gnus-signature-limit nil
187    "Provide a limit to what is considered a signature.
188 If it is a number, no signature may not be longer (in characters) than
189 that number.  If it is a floating point number, no signature may be
190 longer (in lines) than that number.  If it is a function, the function
191 will be called without any parameters, and if it returns nil, there is
192 no signature in the buffer.  If it is a string, it will be used as a
193 regexp.  If it matches, the text in question is not a signature."
194   :type '(choice (integer :value 200)
195                  (number :value 4.0)
196                  (function :value fun)
197                  (regexp :value ".*"))
198   :group 'gnus-article-signature)
199
200 (defcustom gnus-hidden-properties '(invisible t intangible t)
201   "Property list to use for hiding text."
202   :type 'sexp
203   :group 'gnus-article-hiding)
204
205 (defcustom gnus-article-x-face-command
206   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
207   "*String or function to be executed to display an X-Face header.
208 If it is a string, the command will be executed in a sub-shell
209 asynchronously.  The compressed face will be piped to this command."
210   :type 'string                         ;Leave function case to Lisp.
211   :group 'gnus-article-washing)
212
213 (defcustom gnus-article-x-face-too-ugly nil
214   "Regexp matching posters whose face shouldn't be shown automatically."
215   :type '(choice regexp (const nil))
216   :group 'gnus-article-washing)
217
218 (defcustom gnus-emphasis-alist
219   (let ((format
220          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\|\\s)\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-?!.,;:\"]\\|\\s(\\|\\s)\\)")
221         (types
222          '(("_" "_" underline)
223            ("/" "/" italic)
224            ("\\*" "\\*" bold)
225            ("_/" "/_" underline-italic)
226            ("_\\*" "\\*_" underline-bold)
227            ("\\*/" "/\\*" bold-italic)
228            ("_\\*/" "/\\*_" underline-bold-italic))))
229     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
230        2 3 gnus-emphasis-underline)
231       ,@(mapcar
232          (lambda (spec)
233            (list
234             (format format (car spec) (cadr spec))
235             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
236          types)))
237   "*Alist that says how to fontify certain phrases.
238 Each item looks like this:
239
240   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
241
242 The first element is a regular expression to be matched.  The second
243 is a number that says what regular expression grouping used to find
244 the entire emphasized word.  The third is a number that says what
245 regexp grouping should be displayed and highlighted.  The fourth
246 is the face used for highlighting."
247   :type '(repeat (list :value ("" 0 0 default)
248                        regexp
249                        (integer :tag "Match group")
250                        (integer :tag "Emphasize group")
251                        face))
252   :group 'gnus-article-emphasis)
253
254 (defface gnus-emphasis-bold '((t (:bold t)))
255   "Face used for displaying strong emphasized text (*word*)."
256   :group 'gnus-article-emphasis)
257
258 (defface gnus-emphasis-italic '((t (:italic t)))
259   "Face used for displaying italic emphasized text (/word/)."
260   :group 'gnus-article-emphasis)
261
262 (defface gnus-emphasis-underline '((t (:underline t)))
263   "Face used for displaying underlined emphasized text (_word_)."
264   :group 'gnus-article-emphasis)
265
266 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
267   "Face used for displaying underlined bold emphasized text (_*word*_)."
268   :group 'gnus-article-emphasis)
269
270 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
271   "Face used for displaying underlined italic emphasized text (_*word*_)."
272   :group 'gnus-article-emphasis)
273
274 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
275   "Face used for displaying bold italic emphasized text (/*word*/)."
276   :group 'gnus-article-emphasis)
277
278 (defface gnus-emphasis-underline-bold-italic
279   '((t (:bold t :italic t :underline t)))
280   "Face used for displaying underlined bold italic emphasized text.
281 Esample: (_/*word*/_)."
282   :group 'gnus-article-emphasis)
283
284 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
285   "Format for display of Date headers in article bodies.
286 See `format-time-string' for the possible values.
287
288 The variable can also be function, which should return a complete Date
289 header.  The function is called with one argument, the time, which can
290 be fed to `format-time-string'."
291   :type '(choice string symbol)
292   :link '(custom-manual "(gnus)Article Date")
293   :group 'gnus-article-washing)
294
295 (eval-and-compile
296   (autoload 'mail-extract-address-components "mail-extr"))
297
298 (defcustom gnus-save-all-headers t
299   "*If non-nil, don't remove any headers before saving."
300   :group 'gnus-article-saving
301   :type 'boolean)
302
303 (defcustom gnus-prompt-before-saving 'always
304   "*This variable says how much prompting is to be done when saving articles.
305 If it is nil, no prompting will be done, and the articles will be
306 saved to the default files.  If this variable is `always', each and
307 every article that is saved will be preceded by a prompt, even when
308 saving large batches of articles.  If this variable is neither nil not
309 `always', there the user will be prompted once for a file name for
310 each invocation of the saving commands."
311   :group 'gnus-article-saving
312   :type '(choice (item always)
313                  (item :tag "never" nil)
314                  (sexp :tag "once" :format "%t\n" :value t)))
315
316 (defcustom gnus-saved-headers gnus-visible-headers
317   "Headers to keep if `gnus-save-all-headers' is nil.
318 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
319 If that variable is nil, however, all headers that match this regexp
320 will be kept while the rest will be deleted before saving."
321   :group 'gnus-article-saving
322   :type 'regexp)
323
324 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
325   "A function to save articles in your favourite format.
326 The function must be interactively callable (in other words, it must
327 be an Emacs command).
328
329 Gnus provides the following functions:
330
331 * gnus-summary-save-in-rmail (Rmail format)
332 * gnus-summary-save-in-mail (Unix mail format)
333 * gnus-summary-save-in-folder (MH folder)
334 * gnus-summary-save-in-file (article format)
335 * gnus-summary-save-in-vm (use VM's folder format)
336 * gnus-summary-write-to-file (article format -- overwrite)."
337   :group 'gnus-article-saving
338   :type '(radio (function-item gnus-summary-save-in-rmail)
339                 (function-item gnus-summary-save-in-mail)
340                 (function-item gnus-summary-save-in-folder)
341                 (function-item gnus-summary-save-in-file)
342                 (function-item gnus-summary-save-in-vm)
343                 (function-item gnus-summary-write-to-file)))
344
345 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
346   "A function generating a file name to save articles in Rmail format.
347 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
348   :group 'gnus-article-saving
349   :type 'function)
350
351 (defcustom gnus-mail-save-name 'gnus-plain-save-name
352   "A function generating a file name to save articles in Unix mail format.
353 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
354   :group 'gnus-article-saving
355   :type 'function)
356
357 (defcustom gnus-folder-save-name 'gnus-folder-save-name
358   "A function generating a file name to save articles in MH folder.
359 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
360   :group 'gnus-article-saving
361   :type 'function)
362
363 (defcustom gnus-file-save-name 'gnus-numeric-save-name
364   "A function generating a file name to save articles in article format.
365 The function is called with NEWSGROUP, HEADERS, and optional
366 LAST-FILE."
367   :group 'gnus-article-saving
368   :type 'function)
369
370 (defcustom gnus-split-methods
371   '((gnus-article-archive-name)
372     (gnus-article-nndoc-name))
373   "*Variable used to suggest where articles are to be saved.
374 For instance, if you would like to save articles related to Gnus in
375 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
376 you could set this variable to something like:
377
378  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
379    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
380
381 This variable is an alist where the where the key is the match and the
382 value is a list of possible files to save in if the match is non-nil.
383
384 If the match is a string, it is used as a regexp match on the
385 article.  If the match is a symbol, that symbol will be funcalled
386 from the buffer of the article to be saved with the newsgroup as the
387 parameter.  If it is a list, it will be evaled in the same buffer.
388
389 If this form or function returns a string, this string will be used as
390 a possible file name; and if it returns a non-nil list, that list will
391 be used as possible file names."
392   :group 'gnus-article-saving
393   :type '(repeat (choice (list :value (fun) function)
394                          (cons :value ("" "") regexp (repeat string))
395                          (sexp :value nil))))
396
397 (defcustom gnus-article-display-method-for-mime
398   'gnus-article-display-mime-message
399   "Function to display a MIME message.
400 The function is called from the article buffer."
401   :group 'gnus-article-mime
402   :type 'function)
403
404 (defcustom gnus-article-display-method-for-traditional
405   'gnus-article-display-traditional-message
406   "*Function to display a traditional message.
407 The function is called from the article buffer."
408   :group 'gnus-article-mime
409   :type 'function)
410
411 (defcustom gnus-page-delimiter "^\^L"
412   "*Regexp describing what to use as article page delimiters.
413 The default value is \"^\^L\", which is a form linefeed at the
414 beginning of a line."
415   :type 'regexp
416   :group 'gnus-article-various)
417
418 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
419   "*The format specification for the article mode line.
420 See `gnus-summary-mode-line-format' for a closer description.
421
422 The following additional specs are available:
423
424 %w  The article washing status.
425 %m  The number of MIME parts in the article."
426   :type 'string
427   :group 'gnus-article-various)
428
429 (defcustom gnus-article-mode-hook nil
430   "*A hook for Gnus article mode."
431   :type 'hook
432   :group 'gnus-article-various)
433
434 (defcustom gnus-article-menu-hook nil
435   "*Hook run after the creation of the article mode menu."
436   :type 'hook
437   :group 'gnus-article-various)
438
439 (defcustom gnus-article-prepare-hook nil
440   "*A hook called after an article has been prepared in the article buffer."
441   :type 'hook
442   :group 'gnus-article-various)
443
444 (defcustom gnus-article-hide-pgp-hook nil
445   "*A hook called after successfully hiding a PGP signature."
446   :type 'hook
447   :group 'gnus-article-various)
448
449 (defcustom gnus-article-button-face 'bold
450   "Face used for highlighting buttons in the article buffer.
451
452 An article button is a piece of text that you can activate by pressing
453 `RET' or `mouse-2' above it."
454   :type 'face
455   :group 'gnus-article-buttons)
456
457 (defcustom gnus-article-mouse-face 'highlight
458   "Face used for mouse highlighting in the article buffer.
459
460 Article buttons will be displayed in this face when the cursor is
461 above them."
462   :type 'face
463   :group 'gnus-article-buttons)
464
465 (defcustom gnus-signature-face 'gnus-signature-face
466   "Face used for highlighting a signature in the article buffer.
467 Obsolete; use the face `gnus-signature-face' for customizations instead."
468   :type 'face
469   :group 'gnus-article-highlight
470   :group 'gnus-article-signature)
471
472 (defface gnus-signature-face
473   '((((type x))
474      (:italic t)))
475   "Face used for highlighting a signature in the article buffer."
476   :group 'gnus-article-highlight
477   :group 'gnus-article-signature)
478
479 (defface gnus-header-from-face
480   '((((class color)
481       (background dark))
482      (:foreground "spring green"))
483     (((class color)
484       (background light))
485      (:foreground "red3"))
486     (t
487      (:italic t)))
488   "Face used for displaying from headers."
489   :group 'gnus-article-headers
490   :group 'gnus-article-highlight)
491
492 (defface gnus-header-subject-face
493   '((((class color)
494       (background dark))
495      (:foreground "SeaGreen3"))
496     (((class color)
497       (background light))
498      (:foreground "red4"))
499     (t
500      (:bold t :italic t)))
501   "Face used for displaying subject headers."
502   :group 'gnus-article-headers
503   :group 'gnus-article-highlight)
504
505 (defface gnus-header-newsgroups-face
506   '((((class color)
507       (background dark))
508      (:foreground "yellow" :italic t))
509     (((class color)
510       (background light))
511      (:foreground "MidnightBlue" :italic t))
512     (t
513      (:italic t)))
514   "Face used for displaying newsgroups headers."
515   :group 'gnus-article-headers
516   :group 'gnus-article-highlight)
517
518 (defface gnus-header-name-face
519   '((((class color)
520       (background dark))
521      (:foreground "SeaGreen"))
522     (((class color)
523       (background light))
524      (:foreground "maroon"))
525     (t
526      (:bold t)))
527   "Face used for displaying header names."
528   :group 'gnus-article-headers
529   :group 'gnus-article-highlight)
530
531 (defface gnus-header-content-face
532   '((((class color)
533       (background dark))
534      (:foreground "forest green" :italic t))
535     (((class color)
536       (background light))
537      (:foreground "indianred4" :italic t))
538     (t
539      (:italic t)))  "Face used for displaying header content."
540   :group 'gnus-article-headers
541   :group 'gnus-article-highlight)
542
543 (defcustom gnus-header-face-alist
544   '(("From" nil gnus-header-from-face)
545     ("Subject" nil gnus-header-subject-face)
546     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
547     ("" gnus-header-name-face gnus-header-content-face))
548   "*Controls highlighting of article header.
549
550 An alist of the form (HEADER NAME CONTENT).
551
552 HEADER is a regular expression which should match the name of an
553 header header and NAME and CONTENT are either face names or nil.
554
555 The name of each header field will be displayed using the face
556 specified by the first element in the list where HEADER match the
557 header name and NAME is non-nil.  Similarly, the content will be
558 displayed by the first non-nil matching CONTENT face."
559   :group 'gnus-article-headers
560   :group 'gnus-article-highlight
561   :type '(repeat (list (regexp :tag "Header")
562                        (choice :tag "Name"
563                                (item :tag "skip" nil)
564                                (face :value default))
565                        (choice :tag "Content"
566                                (item :tag "skip" nil)
567                                (face :value default)))))
568
569 (defcustom gnus-article-decode-hook nil
570   "*Hook run to decode charsets in articles."
571   :group 'gnus-article-headers
572   :type 'hook)
573
574 (defcustom gnus-display-mime-function 'gnus-display-mime
575   "Function to display MIME articles."
576   :group 'gnus-article-mime
577   :type 'function)
578
579 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
580   "Function used to decode headers.")
581
582 (defvar gnus-article-dumbquotes-map
583   '(("\202" ",")
584     ("\203" "f")
585     ("\204" ",,")
586     ("\205" "...")
587     ("\213" "<")
588     ("\214" "OE")
589     ("\221" "`")
590     ("\222" "'")
591     ("\223" "``")
592     ("\224" "''")
593     ("\225" "*")
594     ("\226" "-")
595     ("\227" "-")
596     ("\231" "(TM)")
597     ("\233" ">")
598     ("\234" "oe")
599     ("\264" "'"))
600   "Table for MS-to-Latin1 translation.")
601
602 (defcustom gnus-ignored-mime-types nil
603   "List of MIME types that should be ignored by Gnus."
604   :group 'gnus-article-mime
605   :type '(repeat regexp))
606
607 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
608   "List of MIME types that should not be given buttons when rendered."
609   :group 'gnus-article-mime
610   :type '(repeat regexp))
611
612 (defcustom gnus-article-mime-part-function nil
613   "Function called with a MIME handle as the argument.
614 This is meant for people who want to do something automatic based
615 on parts -- for instance, adding Vcard info to a database."
616   :group 'gnus-article-mime
617   :type 'function)
618
619 ;;;
620 ;;; The treatment variables
621 ;;;
622
623 (defvar gnus-part-display-hook nil
624   "Hook called on parts that are to receive treatment.")
625
626 (defvar gnus-article-treat-custom
627   '(choice (const :tag "Off" nil)
628            (const :tag "On" t)
629            (const :tag "Header" head)
630            (const :tag "Last" last)
631            (integer :tag "Less")
632            (sexp :tag "Predicate")))
633
634 (defvar gnus-article-treat-types '("text/plain")
635   "Parts to treat.")
636
637 (defvar gnus-inhibit-treatment nil
638   "Whether to inhibit treatment.")
639
640 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
641   "Highlight the signature."
642   :group 'gnus-article-treat
643   :type gnus-article-treat-custom)
644
645 (defcustom gnus-treat-buttonize t
646   "Add buttons."
647   :group 'gnus-article-treat
648   :type gnus-article-treat-custom)
649
650 (defcustom gnus-treat-buttonize-head 'head
651   "Add buttons to the head."
652   :group 'gnus-article-treat
653   :type gnus-article-treat-custom)
654
655 (defcustom gnus-treat-emphasize t
656   "Emphasize text."
657   :group 'gnus-article-treat
658   :type gnus-article-treat-custom)
659
660 (defcustom gnus-treat-strip-cr nil
661   "Remove carriage returns."
662   :group 'gnus-article-treat
663   :type gnus-article-treat-custom)
664
665 (defcustom gnus-treat-hide-headers 'head
666   "Hide headers."
667   :group 'gnus-article-treat
668   :type gnus-article-treat-custom)
669
670 (defcustom gnus-treat-hide-boring-headers nil
671   "Hide boring headers."
672   :group 'gnus-article-treat
673   :type gnus-article-treat-custom)
674
675 (defcustom gnus-treat-hide-signature nil
676   "Hide the signature."
677   :group 'gnus-article-treat
678   :type gnus-article-treat-custom)
679
680 (defcustom gnus-treat-fill-article nil
681   "Fill the article."
682   :group 'gnus-article-treat
683   :type gnus-article-treat-custom)
684
685 (defcustom gnus-treat-hide-citation nil
686   "Hide cited text."
687   :group 'gnus-article-treat
688   :type gnus-article-treat-custom)
689
690 (defcustom gnus-treat-strip-pgp t
691   "Strip PGP signatures."
692   :group 'gnus-article-treat
693   :type gnus-article-treat-custom)
694
695 (defcustom gnus-treat-strip-pem nil
696   "Strip PEM signatures."
697   :group 'gnus-article-treat
698   :type gnus-article-treat-custom)
699
700 (defcustom gnus-treat-strip-banner t
701   "Strip banners from articles.
702 The banner to be stripped is specified in the `banner' group parameter."
703   :group 'gnus-article-treat
704   :type gnus-article-treat-custom)
705
706 (defcustom gnus-treat-highlight-headers 'head
707   "Highlight the headers."
708   :group 'gnus-article-treat
709   :type gnus-article-treat-custom)
710
711 (defcustom gnus-treat-highlight-citation t
712   "Highlight cited text."
713   :group 'gnus-article-treat
714   :type gnus-article-treat-custom)
715
716 (defcustom gnus-treat-date-ut nil
717   "Display the Date in UT (GMT)."
718   :group 'gnus-article-treat
719   :type gnus-article-treat-custom)
720
721 (defcustom gnus-treat-date-local nil
722   "Display the Date in the local timezone."
723   :group 'gnus-article-treat
724   :type gnus-article-treat-custom)
725
726 (defcustom gnus-treat-date-lapsed nil
727   "Display the Date header in a way that says how much time has elapsed."
728   :group 'gnus-article-treat
729   :type gnus-article-treat-custom)
730
731 (defcustom gnus-treat-date-original nil
732   "Display the date in the original timezone."
733   :group 'gnus-article-treat
734   :type gnus-article-treat-custom)
735
736 (defcustom gnus-treat-date-iso8601 nil
737   "Display the date in the ISO8601 format."
738   :group 'gnus-article-treat
739   :type gnus-article-treat-custom)
740
741 (defcustom gnus-treat-date-user-defined nil
742   "Display the date in a user-defined format.
743 The format is defined by the `gnus-article-time-format' variable."
744   :group 'gnus-article-treat
745   :type gnus-article-treat-custom)
746
747 (defcustom gnus-treat-strip-trailing-blank-lines nil
748   "Strip trailing blank lines."
749   :group 'gnus-article-treat
750   :type gnus-article-treat-custom)
751
752 (defcustom gnus-treat-strip-leading-blank-lines nil
753   "Strip leading blank lines."
754   :group 'gnus-article-treat
755   :type gnus-article-treat-custom)
756
757 (defcustom gnus-treat-strip-multiple-blank-lines nil
758   "Strip multiple blank lines."
759   :group 'gnus-article-treat
760   :type gnus-article-treat-custom)
761
762 (defcustom gnus-treat-strip-blank-lines nil
763   "Strip all blank lines."
764   :group 'gnus-article-treat
765   :type gnus-article-treat-custom)
766
767 (defcustom gnus-treat-overstrike t
768   "Treat overstrike highlighting."
769   :group 'gnus-article-treat
770   :type gnus-article-treat-custom)
771
772 (defcustom gnus-treat-display-xface (if (and gnus-xemacs (featurep 'xface))
773                                         'head nil)
774   "Display X-Face headers."
775   :group 'gnus-article-treat
776   :type gnus-article-treat-custom)
777
778 (defcustom gnus-treat-display-smileys (if (and gnus-xemacs
779                                                (featurep 'xpm))
780                                           t nil)
781   "Display smileys."
782   :group 'gnus-article-treat
783   :type gnus-article-treat-custom)
784
785 (defcustom gnus-treat-display-picons (if gnus-xemacs 'head nil)
786   "Display picons."
787   :group 'gnus-article-treat
788   :type gnus-article-treat-custom)
789
790 (defcustom gnus-treat-capitalize-sentences nil
791   "Capitalize sentence-starting words."
792   :group 'gnus-article-treat
793   :type gnus-article-treat-custom)
794
795 (defcustom gnus-treat-fill-long-lines nil
796   "Fill long lines."
797   :group 'gnus-article-treat
798   :type gnus-article-treat-custom)
799
800 (defcustom gnus-treat-play-sounds nil
801   "Fill long lines."
802   :group 'gnus-article-treat
803   :type gnus-article-treat-custom)
804
805 ;;; Internal variables
806
807 (defvar article-goto-body-goes-to-point-min-p nil)
808 (defvar gnus-article-wash-types nil)
809
810 (defvar gnus-article-mime-handle-alist-1 nil)
811 (defvar gnus-treatment-function-alist
812   '((gnus-treat-strip-banner gnus-article-strip-banner)
813     (gnus-treat-highlight-signature gnus-article-highlight-signature)
814     (gnus-treat-buttonize gnus-article-add-buttons)
815     (gnus-treat-fill-article gnus-article-fill-cited-article)
816     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
817     (gnus-treat-strip-cr gnus-article-remove-cr)
818     (gnus-treat-hide-headers gnus-article-hide-headers)
819     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
820     (gnus-treat-hide-signature gnus-article-hide-signature)
821     (gnus-treat-hide-citation gnus-article-hide-citation)
822     (gnus-treat-strip-pgp gnus-article-hide-pgp)
823     (gnus-treat-strip-pem gnus-article-hide-pem)
824     (gnus-treat-highlight-headers gnus-article-highlight-headers)
825     (gnus-treat-highlight-citation gnus-article-highlight-citation)
826     (gnus-treat-highlight-signature gnus-article-highlight-signature)
827     (gnus-treat-emphasize gnus-article-emphasize)
828     (gnus-treat-date-ut gnus-article-date-ut)
829     (gnus-treat-date-local gnus-article-date-local)
830     (gnus-treat-date-lapsed gnus-article-date-lapsed)
831     (gnus-treat-date-original gnus-article-date-original)
832     (gnus-treat-date-user-defined gnus-article-date-user)
833     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
834     (gnus-treat-strip-trailing-blank-lines
835      gnus-article-remove-trailing-blank-lines)
836     (gnus-treat-strip-leading-blank-lines
837      gnus-article-strip-leading-blank-lines)
838     (gnus-treat-strip-multiple-blank-lines
839      gnus-article-strip-multiple-blank-lines)
840     (gnus-treat-strip-blank-lines gnus-article-strip-blank-lines)
841     (gnus-treat-overstrike gnus-article-treat-overstrike)
842     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
843     (gnus-treat-display-xface gnus-article-display-x-face)
844     (gnus-treat-display-smileys gnus-smiley-display)
845     (gnus-treat-display-picons gnus-article-display-picons)
846     (gnus-treat-play-sounds gnus-earcon-display)))
847
848 (defvar gnus-article-mime-handle-alist nil)
849 (defvar article-lapsed-timer nil)
850 (defvar gnus-article-current-summary nil)
851
852 (defvar gnus-article-mode-syntax-table
853   (let ((table (copy-syntax-table text-mode-syntax-table)))
854     (modify-syntax-entry ?- "w" table)
855     (modify-syntax-entry ?> ")" table)
856     (modify-syntax-entry ?< "(" table)
857     table)
858   "Syntax table used in article mode buffers.
859 Initialized from `text-mode-syntax-table.")
860
861 (defvar gnus-save-article-buffer nil)
862
863 (defvar gnus-article-mode-line-format-alist
864   (nconc '((?w (gnus-article-wash-status) ?s)
865            (?m (gnus-article-mime-part-status) ?s))
866          gnus-summary-mode-line-format-alist))
867
868 (defvar gnus-number-of-articles-to-be-saved nil)
869
870 (defvar gnus-inhibit-hiding nil)
871
872 (defsubst gnus-article-hide-text (b e props)
873   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
874   (add-text-properties b e props)
875   (when (memq 'intangible props)
876     (put-text-property
877      (max (1- b) (point-min))
878      b 'intangible (cddr (memq 'intangible props)))))
879
880 (defmacro gnus-with-article (article &rest forms)
881   "Select ARTICLE and perform FORMS in the original article buffer.
882 Then replace the article with the result."
883   `(progn
884      ;; We don't want the article to be marked as read.
885      (let (gnus-mark-article-hook)
886        (gnus-summary-select-article t t nil ,article))
887      (set-buffer gnus-original-article-buffer)
888      ,@forms
889      (if (not (gnus-check-backend-function
890                'request-replace-article (car gnus-article-current)))
891          (gnus-message 5 "Read-only group; not replacing")
892        (unless (gnus-request-replace-article
893                 ,article (car gnus-article-current)
894                 (current-buffer) t)
895          (error "Couldn't replace article")))
896      ;; The cache and backlog have to be flushed somewhat.
897      (when gnus-keep-backlog
898        (gnus-backlog-remove-article
899         (car gnus-article-current) (cdr gnus-article-current)))
900      (when gnus-use-cache
901        (gnus-cache-update-article
902         (car gnus-article-current) (cdr gnus-article-current)))))
903
904 (put 'gnus-with-article 'lisp-indent-function 1)
905 (put 'gnus-with-article 'edebug-form-spec '(form body))
906
907 (defsubst gnus-article-unhide-text (b e)
908   "Remove hidden text properties from region between B and E."
909   (remove-text-properties b e gnus-hidden-properties)
910   (when (memq 'intangible gnus-hidden-properties)
911     (put-text-property (max (1- b) (point-min))
912                        b 'intangible nil)))
913
914 (defun gnus-article-hide-text-type (b e type)
915   "Hide text of TYPE between B and E."
916   (push type gnus-article-wash-types)
917   (gnus-article-hide-text
918    b e (cons 'article-type (cons type gnus-hidden-properties))))
919
920 (defun gnus-article-unhide-text-type (b e type)
921   "Unhide text of TYPE between B and E."
922   (setq gnus-article-wash-types
923         (delq type gnus-article-wash-types))
924   (remove-text-properties
925    b e (cons 'article-type (cons type gnus-hidden-properties)))
926   (when (memq 'intangible gnus-hidden-properties)
927     (put-text-property (max (1- b) (point-min))
928                        b 'intangible nil)))
929
930 (defun gnus-article-hide-text-of-type (type)
931   "Hide text of TYPE in the current buffer."
932   (save-excursion
933     (let ((b (point-min))
934           (e (point-max)))
935       (while (setq b (text-property-any b e 'article-type type))
936         (add-text-properties b (incf b) gnus-hidden-properties)))))
937
938 (defun gnus-article-delete-text-of-type (type)
939   "Delete text of TYPE in the current buffer."
940   (save-excursion
941     (let ((b (point-min)))
942       (while (setq b (text-property-any b (point-max) 'article-type type))
943         (delete-region
944          b (or (text-property-not-all b (point-max) 'article-type type)
945                (point-max)))))))
946
947 (defun gnus-article-delete-invisible-text ()
948   "Delete all invisible text in the current buffer."
949   (save-excursion
950     (let ((b (point-min)))
951       (while (setq b (text-property-any b (point-max) 'invisible t))
952         (delete-region
953          b (or (text-property-not-all b (point-max) 'invisible t)
954                (point-max)))))))
955
956 (defun gnus-article-text-type-exists-p (type)
957   "Say whether any text of type TYPE exists in the buffer."
958   (text-property-any (point-min) (point-max) 'article-type type))
959
960 (defsubst gnus-article-header-rank ()
961   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
962   (let ((list gnus-sorted-header-list)
963         (i 0))
964     (while list
965       (when (looking-at (car list))
966         (setq list nil))
967       (setq list (cdr list))
968       (incf i))
969     i))
970
971 (defun article-hide-headers (&optional arg delete)
972   "Toggle whether to hide unwanted headers and possibly sort them as well.
973 If given a negative prefix, always show; if given a positive prefix,
974 always hide."
975   (interactive (gnus-article-hidden-arg))
976   (current-buffer)
977   (if (gnus-article-check-hidden-text 'headers arg)
978       ;; Show boring headers as well.
979       (progn
980         (gnus-article-show-hidden-text 'boring-headers)
981         (when (eq 1 (point-min))
982           (set-window-start (get-buffer-window (current-buffer)) 1)))
983     ;; This function might be inhibited.
984     (unless gnus-inhibit-hiding
985       (save-excursion
986         (save-restriction
987           (let ((buffer-read-only nil)
988                 (case-fold-search t)
989                 (props (nconc (list 'article-type 'headers)
990                               gnus-hidden-properties))
991                 (max (1+ (length gnus-sorted-header-list)))
992                 (ignored (when (not gnus-visible-headers)
993                            (cond ((stringp gnus-ignored-headers)
994                                   gnus-ignored-headers)
995                                  ((listp gnus-ignored-headers)
996                                   (mapconcat 'identity gnus-ignored-headers
997                                              "\\|")))))
998                 (visible
999                  (cond ((stringp gnus-visible-headers)
1000                         gnus-visible-headers)
1001                        ((and gnus-visible-headers
1002                              (listp gnus-visible-headers))
1003                         (mapconcat 'identity gnus-visible-headers "\\|"))))
1004                 (inhibit-point-motion-hooks t)
1005                 beg)
1006             ;; First we narrow to just the headers.
1007             (goto-char (point-min))
1008             ;; Hide any "From " lines at the beginning of (mail) articles.
1009             (while (looking-at "From ")
1010               (forward-line 1))
1011             (unless (bobp)
1012               (if delete
1013                   (delete-region (point-min) (point))
1014                 (gnus-article-hide-text (point-min) (point) props)))
1015             ;; Then treat the rest of the header lines.
1016             (narrow-to-region
1017              (point)
1018              (if (search-forward "\n\n" nil t) ; if there's a body
1019                  (progn (forward-line -1) (point))
1020                (point-max)))
1021             ;; Then we use the two regular expressions
1022             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1023             ;; select which header lines is to remain visible in the
1024             ;; article buffer.
1025             (goto-char (point-min))
1026             (while (re-search-forward "^[^ \t]*:" nil t)
1027               (beginning-of-line)
1028               ;; Mark the rank of the header.
1029               (put-text-property
1030                (point) (1+ (point)) 'message-rank
1031                (if (or (and visible (looking-at visible))
1032                        (and ignored
1033                             (not (looking-at ignored))))
1034                    (gnus-article-header-rank)
1035                  (+ 2 max)))
1036               (forward-line 1))
1037             (message-sort-headers-1)
1038             (when (setq beg (text-property-any
1039                              (point-min) (point-max) 'message-rank (+ 2 max)))
1040               ;; We make the unwanted headers invisible.
1041               (if delete
1042                   (delete-region beg (point-max))
1043                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
1044                 (gnus-article-hide-text-type beg (point-max) 'headers))
1045               ;; Work around XEmacs lossage.
1046               (put-text-property (point-min) beg 'invisible nil))))))))
1047
1048 (defun article-hide-boring-headers (&optional arg)
1049   "Toggle hiding of headers that aren't very interesting.
1050 If given a negative prefix, always show; if given a positive prefix,
1051 always hide."
1052   (interactive (gnus-article-hidden-arg))
1053   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1054              (not gnus-show-all-headers))
1055     (save-excursion
1056       (save-restriction
1057         (let ((buffer-read-only nil)
1058               (list gnus-boring-article-headers)
1059               (inhibit-point-motion-hooks t)
1060               elem)
1061           (article-narrow-to-head)
1062           (while list
1063             (setq elem (pop list))
1064             (goto-char (point-min))
1065             (cond
1066              ;; Hide empty headers.
1067              ((eq elem 'empty)
1068               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
1069                 (forward-line -1)
1070                 (gnus-article-hide-text-type
1071                  (progn (beginning-of-line) (point))
1072                  (progn
1073                    (end-of-line)
1074                    (if (re-search-forward "^[^ \t]" nil t)
1075                        (match-beginning 0)
1076                      (point-max)))
1077                  'boring-headers)))
1078              ;; Hide boring Newsgroups header.
1079              ((eq elem 'newsgroups)
1080               (when (equal (gnus-fetch-field "newsgroups")
1081                            (gnus-group-real-name
1082                             (if (boundp 'gnus-newsgroup-name)
1083                                 gnus-newsgroup-name
1084                               "")))
1085                 (gnus-article-hide-header "newsgroups")))
1086              ((eq elem 'followup-to)
1087               (when (equal (message-fetch-field "followup-to")
1088                            (message-fetch-field "newsgroups"))
1089                 (gnus-article-hide-header "followup-to")))
1090              ((eq elem 'reply-to)
1091               (let ((from (message-fetch-field "from"))
1092                     (reply-to (message-fetch-field "reply-to")))
1093                 (when (and
1094                        from reply-to
1095                        (ignore-errors
1096                          (equal
1097                           (nth 1 (funcall gnus-extract-address-components from))
1098                           (nth 1 (funcall gnus-extract-address-components reply-to)))))
1099                   (gnus-article-hide-header "reply-to"))))
1100              ((eq elem 'date)
1101               (let ((date (message-fetch-field "date")))
1102                 (when (and date
1103                            (< (days-between (current-time-string) date)
1104                               4))
1105                   (gnus-article-hide-header "date"))))
1106              ((eq elem 'long-to)
1107               (let ((to (message-fetch-field "to")))
1108                 (when (> (length to) 1024)
1109                   (gnus-article-hide-header "to"))))
1110              ((eq elem 'many-to)
1111               (let ((to-count 0))
1112                 (goto-char (point-min))
1113                 (while (re-search-forward "^to:" nil t)
1114                   (setq to-count (1+ to-count)))
1115                 (when (> to-count 1)
1116                   (while (> to-count 0)
1117                     (goto-char (point-min))
1118                     (save-restriction
1119                       (re-search-forward "^to:" nil nil to-count)
1120                       (forward-line -1)
1121                       (narrow-to-region (point) (point-max))
1122                       (gnus-article-hide-header "to"))
1123                     (setq to-count (1- to-count)))))))))))))
1124
1125 (defun gnus-article-hide-header (header)
1126   (save-excursion
1127     (goto-char (point-min))
1128     (when (re-search-forward (concat "^" header ":") nil t)
1129       (gnus-article-hide-text-type
1130        (progn (beginning-of-line) (point))
1131        (progn
1132          (end-of-line)
1133          (if (re-search-forward "^[^ \t]" nil t)
1134              (match-beginning 0)
1135            (point-max)))
1136        'boring-headers))))
1137
1138 (defvar gnus-article-normalized-header-length 40
1139   "Length of normalized headers.")
1140
1141 (defun article-normalize-headers ()
1142   "Make all header lines 40 characters long."
1143   (interactive)
1144   (let ((buffer-read-only nil)
1145         column)
1146     (save-excursion
1147       (save-restriction
1148         (article-narrow-to-head)
1149         (while (not (eobp))
1150           (cond
1151            ((< (setq column (- (gnus-point-at-eol) (point)))
1152                gnus-article-normalized-header-length)
1153             (end-of-line)
1154             (insert (make-string
1155                      (- gnus-article-normalized-header-length column)
1156                      ? )))
1157            ((> column gnus-article-normalized-header-length)
1158             (gnus-put-text-property
1159              (progn
1160                (forward-char gnus-article-normalized-header-length)
1161                (point))
1162              (gnus-point-at-eol)
1163              'invisible t))
1164            (t
1165             ;; Do nothing.
1166             ))
1167           (forward-line 1))))))
1168
1169 (defun article-treat-dumbquotes ()
1170   "Translate M******** sm*rtq**t*s into proper text.
1171 Note that this function guesses whether a character is a sm*rtq**t* or
1172 not, so it should only be used interactively."
1173   (interactive)
1174   (article-translate-strings gnus-article-dumbquotes-map))
1175
1176 (defun article-translate-characters (from to)
1177   "Translate all characters in the body of the article according to FROM and TO.
1178 FROM is a string of characters to translate from; to is a string of
1179 characters to translate to."
1180   (save-excursion
1181     (when (article-goto-body)
1182       (let ((buffer-read-only nil)
1183             (x (make-string 225 ?x))
1184             (i -1))
1185         (while (< (incf i) (length x))
1186           (aset x i i))
1187         (setq i 0)
1188         (while (< i (length from))
1189           (aset x (aref from i) (aref to i))
1190           (incf i))
1191         (translate-region (point) (point-max) x)))))
1192
1193 (defun article-translate-strings (map)
1194   "Translate all string in the body of the article according to MAP.
1195 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1196   (save-excursion
1197     (when (article-goto-body)
1198       (let ((buffer-read-only nil)
1199             elem)
1200         (while (setq elem (pop map))
1201           (save-excursion
1202             (while (search-forward (car elem) nil t)
1203               (replace-match (cadr elem)))))))))
1204
1205 (defun article-treat-overstrike ()
1206   "Translate overstrikes into bold text."
1207   (interactive)
1208   (save-excursion
1209     (when (article-goto-body)
1210       (let ((buffer-read-only nil))
1211         (while (search-forward "\b" nil t)
1212           (let ((next (char-after))
1213                 (previous (char-after (- (point) 2))))
1214             ;; We do the boldification/underlining by hiding the
1215             ;; overstrikes and putting the proper text property
1216             ;; on the letters.
1217             (cond
1218              ((eq next previous)
1219               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1220               (put-text-property (point) (1+ (point)) 'face 'bold))
1221              ((eq next ?_)
1222               (gnus-article-hide-text-type
1223                (1- (point)) (1+ (point)) 'overstrike)
1224               (put-text-property
1225                (- (point) 2) (1- (point)) 'face 'underline))
1226              ((eq previous ?_)
1227               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1228               (put-text-property
1229                (point) (1+ (point)) 'face 'underline)))))))))
1230
1231 (defun article-fill-long-lines ()
1232   "Fill lines that are wider than the window width."
1233   (interactive)
1234   (save-excursion
1235     (let ((buffer-read-only nil)
1236           (width (window-width (get-buffer-window (current-buffer)))))
1237       (save-restriction
1238         (article-goto-body)
1239         (let ((adaptive-fill-mode nil))
1240           (while (not (eobp))
1241             (end-of-line)
1242             (when (>= (current-column) (min fill-column width))
1243               (narrow-to-region (point) (gnus-point-at-bol))
1244               (fill-paragraph nil)
1245               (goto-char (point-max))
1246               (widen))
1247             (forward-line 1)))))))
1248
1249 (defun article-capitalize-sentences ()
1250   "Capitalize the first word in each sentence."
1251   (interactive)
1252   (save-excursion
1253     (let ((buffer-read-only nil)
1254           (paragraph-start "^[\n\^L]"))
1255       (article-goto-body)
1256       (while (not (eobp))
1257         (capitalize-word 1)
1258         (forward-sentence)))))
1259
1260 (defun article-remove-cr ()
1261   "Translate CRLF pairs into LF, and then CR into LF.."
1262   (interactive)
1263   (save-excursion
1264     (let ((buffer-read-only nil))
1265       (goto-char (point-min))
1266       (while (search-forward "\r$" nil t)
1267         (replace-match "" t t))
1268       (goto-char (point-min))
1269       (while (search-forward "\r" nil t)
1270         (replace-match "\n" t t)))))
1271
1272 (defun article-remove-trailing-blank-lines ()
1273   "Remove all trailing blank lines from the article."
1274   (interactive)
1275   (save-excursion
1276     (let ((buffer-read-only nil))
1277       (goto-char (point-max))
1278       (delete-region
1279        (point)
1280        (progn
1281          (while (and (not (bobp))
1282                      (looking-at "^[ \t]*$")
1283                      (not (gnus-annotation-in-region-p
1284                            (point) (gnus-point-at-eol))))
1285            (forward-line -1))
1286          (forward-line 1)
1287          (point))))))
1288
1289 (defun article-display-x-face (&optional force)
1290   "Look for an X-Face header and display it if present."
1291   (interactive (list 'force))
1292   (save-excursion
1293     ;; Delete the old process, if any.
1294     (when (process-status "article-x-face")
1295       (delete-process "article-x-face"))
1296     (let ((inhibit-point-motion-hooks t)
1297           (case-fold-search t)
1298           from last)
1299       (save-restriction
1300         (article-narrow-to-head)
1301         (goto-char (point-min))
1302         (setq from (message-fetch-field "from"))
1303         (goto-char (point-min))
1304         (while (and gnus-article-x-face-command
1305                     (not last)
1306                     (or force
1307                         ;; Check whether this face is censored.
1308                         (not gnus-article-x-face-too-ugly)
1309                         (and gnus-article-x-face-too-ugly from
1310                              (not (string-match gnus-article-x-face-too-ugly
1311                                                 from))))
1312                     ;; Has to be present.
1313                     (re-search-forward "^X-Face: " nil t))
1314           ;; This used to try to do multiple faces (`while' instead of
1315           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1316           ;; work (b) it can crash some versions of Emacs (c) are
1317           ;; multiple faces really something to encourage?
1318           (when (stringp gnus-article-x-face-command)
1319             (setq last t))
1320           ;; We now have the area of the buffer where the X-Face is stored.
1321           (save-excursion
1322             (let ((beg (point))
1323                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1324               ;; We display the face.
1325               (if (symbolp gnus-article-x-face-command)
1326                   ;; The command is a lisp function, so we call it.
1327                   (if (gnus-functionp gnus-article-x-face-command)
1328                       (funcall gnus-article-x-face-command beg end)
1329                     (error "%s is not a function" gnus-article-x-face-command))
1330                 ;; The command is a string, so we interpret the command
1331                 ;; as a, well, command, and fork it off.
1332                 (let ((process-connection-type nil))
1333                   (process-kill-without-query
1334                    (start-process
1335                     "article-x-face" nil shell-file-name shell-command-switch
1336                     gnus-article-x-face-command))
1337                   (process-send-region "article-x-face" beg end)
1338                   (process-send-eof "article-x-face"))))))))))
1339
1340 (defun article-decode-mime-words ()
1341   "Decode all MIME-encoded words in the article."
1342   (interactive)
1343   (save-excursion
1344     (set-buffer gnus-article-buffer)
1345     (let ((inhibit-point-motion-hooks t)
1346           buffer-read-only
1347           (mail-parse-charset gnus-newsgroup-charset))
1348       (mail-decode-encoded-word-region (point-min) (point-max)))))
1349
1350 (defun article-decode-charset (&optional prompt)
1351   "Decode charset-encoded text in the article.
1352 If PROMPT (the prefix), prompt for a coding system to use."
1353   (interactive "P")
1354   (save-excursion
1355     (save-restriction
1356       (article-narrow-to-head)
1357       (let* ((inhibit-point-motion-hooks t)
1358              (case-fold-search t)
1359              (ct (message-fetch-field "Content-Type" t))
1360              (cte (message-fetch-field "Content-Transfer-Encoding" t))
1361              (ctl (and ct (ignore-errors
1362                             (mail-header-parse-content-type ct))))
1363              (charset (cond
1364                        (prompt
1365                         (mm-read-coding-system "Charset to decode: "))
1366                        (ctl
1367                         (mail-content-type-get ctl 'charset))))
1368              (mail-parse-charset gnus-newsgroup-charset)
1369              buffer-read-only)
1370         (when (memq charset gnus-newsgroup-ignored-charsets)
1371           (setq charset nil))
1372         (goto-char (point-max))
1373         (widen)
1374         (forward-line 1)
1375         (narrow-to-region (point) (point-max))
1376         (when (and (or (not ctl)
1377                        (equal (car ctl) "text/plain"))
1378                    (not (mm-uu-test)))
1379           (mm-decode-body
1380            charset (and cte (intern (downcase
1381                                      (gnus-strip-whitespace cte))))
1382            (car ctl)))))))
1383
1384 (defun article-decode-encoded-words ()
1385   "Remove encoded-word encoding from headers."
1386   (let (buffer-read-only)
1387     (let ((charset (save-excursion
1388                      (set-buffer gnus-summary-buffer)
1389                      default-mime-charset)))
1390       (mime-decode-header-in-buffer charset)
1391       )))
1392
1393 (defun article-de-quoted-unreadable (&optional force)
1394   "Translate a quoted-printable-encoded article.
1395 If FORCE, decode the article whether it is marked as quoted-printable
1396 or not."
1397   (interactive (list 'force))
1398   (save-excursion
1399     (let ((buffer-read-only nil)
1400           (type (gnus-fetch-field "content-transfer-encoding"))
1401           (charset gnus-newsgroup-charset))
1402       (when (or force
1403                 (and type (string-match "quoted-printable" (downcase type))))
1404         (article-goto-body)
1405         (save-restriction
1406           (narrow-to-region (point) (point-max))
1407           (quoted-printable-decode-region (point-min) (point-max))
1408           (when charset
1409             (mm-decode-body charset)))))))
1410
1411 (defun article-hide-pgp ()
1412   "Remove any PGP headers and signatures in the current article."
1413   (interactive)
1414   (save-excursion
1415     (save-restriction
1416       (let ((inhibit-point-motion-hooks t)
1417             buffer-read-only beg end)
1418         (article-goto-body)
1419         ;; Hide the "header".
1420         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1421           (push 'pgp gnus-article-wash-types)
1422           (delete-region (match-beginning 0) (match-end 0))
1423           ;; PGP 5 and GNU PG add a `Hash: <>' comment, hide that too
1424           (when (looking-at "Hash:.*$")
1425             (delete-region (point) (1+ (gnus-point-at-eol))))
1426           (setq beg (point))
1427           ;; Hide the actual signature.
1428           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1429                (setq end (1+ (match-beginning 0)))
1430                (delete-region
1431                 end
1432                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1433                     (match-end 0)
1434                   ;; Perhaps we shouldn't hide to the end of the buffer
1435                   ;; if there is no end to the signature?
1436                   (point-max))))
1437           ;; Hide "- " PGP quotation markers.
1438           (when (and beg end)
1439             (narrow-to-region beg end)
1440             (goto-char (point-min))
1441             (while (re-search-forward "^- " nil t)
1442               (delete-region
1443                (match-beginning 0) (match-end 0)))
1444             (widen))
1445           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1446
1447 (defun article-hide-pem (&optional arg)
1448   "Toggle hiding of any PEM headers and signatures in the current article.
1449 If given a negative prefix, always show; if given a positive prefix,
1450 always hide."
1451   (interactive (gnus-article-hidden-arg))
1452   (unless (gnus-article-check-hidden-text 'pem arg)
1453     (save-excursion
1454       (let (buffer-read-only end)
1455         (goto-char (point-min))
1456         ;; Hide the horrendously ugly "header".
1457         (when (and (search-forward
1458                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1459                     nil t)
1460                    (setq end (1+ (match-beginning 0))))
1461           (push 'pem gnus-article-wash-types)
1462           (gnus-article-hide-text-type
1463            end
1464            (if (search-forward "\n\n" nil t)
1465                (match-end 0)
1466              (point-max))
1467            'pem)
1468           ;; Hide the trailer as well
1469           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1470                                 nil t)
1471             (gnus-article-hide-text-type
1472              (match-beginning 0) (match-end 0) 'pem)))))))
1473
1474 (defun article-strip-banner ()
1475   "Strip the banner specified by the `banner' group parameter."
1476   (interactive)
1477   (save-excursion
1478     (save-restriction
1479       (let ((inhibit-point-motion-hooks t)
1480             (banner (gnus-group-get-parameter gnus-newsgroup-name 'banner))
1481             (gnus-signature-limit nil)
1482             buffer-read-only beg end)
1483         (when banner
1484           (article-goto-body)
1485           (cond
1486            ((eq banner 'signature)
1487             (when (gnus-article-narrow-to-signature)
1488               (widen)
1489               (forward-line -1)
1490               (delete-region (point) (point-max))))
1491            ((stringp banner)
1492             (while (re-search-forward banner nil t)
1493               (delete-region (match-beginning 0) (match-end 0))))))))))
1494
1495 (defun article-hide-signature (&optional arg)
1496   "Hide the signature in the current article.
1497 If given a negative prefix, always show; if given a positive prefix,
1498 always hide."
1499   (interactive (gnus-article-hidden-arg))
1500   (unless (gnus-article-check-hidden-text 'signature arg)
1501     (save-excursion
1502       (save-restriction
1503         (let ((buffer-read-only nil))
1504           (when (gnus-article-narrow-to-signature)
1505             (gnus-article-hide-text-type
1506              (point-min) (point-max) 'signature)))))))
1507
1508 (defun article-strip-leading-blank-lines ()
1509   "Remove all blank lines from the beginning of the article."
1510   (interactive)
1511   (save-excursion
1512     (let ((inhibit-point-motion-hooks t)
1513           buffer-read-only)
1514       (when (article-goto-body)
1515         (while (and (not (eobp))
1516                     (looking-at "[ \t]*$"))
1517           (gnus-delete-line))))))
1518
1519 (defun article-narrow-to-head ()
1520   "Narrow the buffer to the head of the message.
1521 Point is left at the beginning of the narrowed-to region."
1522   (narrow-to-region
1523    (goto-char (point-min))
1524    (if (search-forward "\n\n" nil 1)
1525        (1- (point))
1526      (point-max)))
1527   (goto-char (point-min)))
1528
1529 (defun article-goto-body ()
1530   "Place point at the start of the body."
1531   (goto-char (point-min))
1532   (cond
1533    ;; This variable is only bound when dealing with separate
1534    ;; MIME body parts.
1535    (article-goto-body-goes-to-point-min-p
1536     t)
1537    ((search-forward "\n\n" nil t)
1538     t)
1539    (t
1540     (goto-char (point-max))
1541     nil)))
1542
1543 (defun article-strip-multiple-blank-lines ()
1544   "Replace consecutive blank lines with one empty line."
1545   (interactive)
1546   (save-excursion
1547     (let ((inhibit-point-motion-hooks t)
1548           buffer-read-only)
1549       ;; First make all blank lines empty.
1550       (article-goto-body)
1551       (while (re-search-forward "^[ \t]+$" nil t)
1552         (unless (gnus-annotation-in-region-p
1553                  (match-beginning 0) (match-end 0))
1554           (replace-match "" nil t)))
1555       ;; Then replace multiple empty lines with a single empty line.
1556       (article-goto-body)
1557       (while (re-search-forward "\n\n\n+" nil t)
1558         (unless (gnus-annotation-in-region-p
1559                  (match-beginning 0) (match-end 0))
1560           (replace-match "\n\n" t t))))))
1561
1562 (defun article-strip-leading-space ()
1563   "Remove all white space from the beginning of the lines in the article."
1564   (interactive)
1565   (save-excursion
1566     (let ((inhibit-point-motion-hooks t)
1567           buffer-read-only)
1568       (article-goto-body)
1569       (while (re-search-forward "^[ \t]+" nil t)
1570         (replace-match "" t t)))))
1571
1572 (defun article-strip-trailing-space ()
1573   "Remove all white space from the end of the lines in the article."
1574   (interactive)
1575   (save-excursion
1576     (let ((inhibit-point-motion-hooks t)
1577           buffer-read-only)
1578       (article-goto-body)
1579       (while (re-search-forward "[ \t]+$" nil t)
1580         (replace-match "" t t)))))
1581
1582 (defun article-strip-blank-lines ()
1583   "Strip leading, trailing and multiple blank lines."
1584   (interactive)
1585   (article-strip-leading-blank-lines)
1586   (article-remove-trailing-blank-lines)
1587   (article-strip-multiple-blank-lines))
1588
1589 (defun article-strip-all-blank-lines ()
1590   "Strip all blank lines."
1591   (interactive)
1592   (save-excursion
1593     (let ((inhibit-point-motion-hooks t)
1594           buffer-read-only)
1595       (article-goto-body)
1596       (while (re-search-forward "^[ \t]*\n" nil t)
1597         (replace-match "" t t)))))
1598
1599 (defun gnus-article-narrow-to-signature ()
1600   "Narrow to the signature; return t if a signature is found, else nil."
1601   (let ((inhibit-point-motion-hooks t))
1602     (when (gnus-article-search-signature)
1603       (forward-line 1)
1604       ;; Check whether we have some limits to what we consider
1605       ;; to be a signature.
1606       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1607                       (list gnus-signature-limit)))
1608             limit limited)
1609         (while (setq limit (pop limits))
1610           (if (or (and (integerp limit)
1611                        (< (- (point-max) (point)) limit))
1612                   (and (floatp limit)
1613                        (< (count-lines (point) (point-max)) limit))
1614                   (and (gnus-functionp limit)
1615                        (funcall limit))
1616                   (and (stringp limit)
1617                        (not (re-search-forward limit nil t))))
1618               ()                        ; This limit did not succeed.
1619             (setq limited t
1620                   limits nil)))
1621         (unless limited
1622           (narrow-to-region (point) (point-max))
1623           t)))))
1624
1625 (defun gnus-article-search-signature ()
1626   "Search the current buffer for the signature separator.
1627 Put point at the beginning of the signature separator."
1628   (let ((cur (point)))
1629     (goto-char (point-max))
1630     (if (if (stringp gnus-signature-separator)
1631             (re-search-backward gnus-signature-separator nil t)
1632           (let ((seps gnus-signature-separator))
1633             (while (and seps
1634                         (not (re-search-backward (car seps) nil t)))
1635               (pop seps))
1636             seps))
1637         t
1638       (goto-char cur)
1639       nil)))
1640
1641 (defun gnus-article-hidden-arg ()
1642   "Return the current prefix arg as a number, or 0 if no prefix."
1643   (list (if current-prefix-arg
1644             (prefix-numeric-value current-prefix-arg)
1645           0)))
1646
1647 (defun gnus-article-check-hidden-text (type arg)
1648   "Return nil if hiding is necessary.
1649 Arg can be nil or a number.  Nil and positive means hide, negative
1650 means show, 0 means toggle."
1651   (save-excursion
1652     (save-restriction
1653       (let ((hide (gnus-article-hidden-text-p type)))
1654         (cond
1655          ((or (null arg)
1656               (> arg 0))
1657           nil)
1658          ((< arg 0)
1659           (gnus-article-show-hidden-text type))
1660          (t
1661           (if (eq hide 'hidden)
1662               (gnus-article-show-hidden-text type)
1663             nil)))))))
1664
1665 (defun gnus-article-hidden-text-p (type)
1666   "Say whether the current buffer contains hidden text of type TYPE."
1667   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1668     (while (and pos
1669                 (not (get-text-property pos 'invisible)))
1670       (setq pos
1671             (text-property-any (1+ pos) (point-max) 'article-type type)))
1672     (if pos
1673         'hidden
1674       nil)))
1675
1676 (defun gnus-article-show-hidden-text (type &optional hide)
1677   "Show all hidden text of type TYPE.
1678 If HIDE, hide the text instead."
1679   (save-excursion
1680     (let ((buffer-read-only nil)
1681           (inhibit-point-motion-hooks t)
1682           (end (point-min))
1683           beg)
1684       (while (setq beg (text-property-any end (point-max) 'article-type type))
1685         (goto-char beg)
1686         (setq end (or
1687                    (text-property-not-all beg (point-max) 'article-type type)
1688                    (point-max)))
1689         (if hide
1690             (gnus-article-hide-text beg end gnus-hidden-properties)
1691           (gnus-article-unhide-text beg end))
1692         (goto-char end))
1693       t)))
1694
1695 (defconst article-time-units
1696   `((year . ,(* 365.25 24 60 60))
1697     (week . ,(* 7 24 60 60))
1698     (day . ,(* 24 60 60))
1699     (hour . ,(* 60 60))
1700     (minute . 60)
1701     (second . 1))
1702   "Mapping from time units to seconds.")
1703
1704 (defun article-date-ut (&optional type highlight header)
1705   "Convert DATE date to universal time in the current article.
1706 If TYPE is `local', convert to local time; if it is `lapsed', output
1707 how much time has lapsed since DATE."
1708   (interactive (list 'ut t))
1709   (let* ((header (or header
1710                      (mail-header-date (save-excursion
1711                                          (set-buffer gnus-summary-buffer)
1712                                          gnus-current-headers))
1713                      (message-fetch-field "date")
1714                      ""))
1715          (date (if (vectorp header) (mail-header-date header)
1716                  header))
1717          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1718          (inhibit-point-motion-hooks t)
1719          bface eface newline)
1720     (when (and date (not (string= date "")))
1721       (save-excursion
1722         (save-restriction
1723           (article-narrow-to-head)
1724           (let ((buffer-read-only nil))
1725             ;; Delete any old Date headers.
1726             (if (re-search-forward date-regexp nil t)
1727                 (progn
1728                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1729                         eface (get-text-property (1- (gnus-point-at-eol))
1730                                                  'face))
1731                   (delete-region (progn (beginning-of-line) (point))
1732                                  (progn (end-of-line) (point)))
1733                   (beginning-of-line))
1734               (goto-char (point-max))
1735               (setq newline t))
1736             (insert (article-make-date-line date type))
1737             ;; Do highlighting.
1738             (beginning-of-line)
1739             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1740               (put-text-property (match-beginning 1) (1+ (match-end 1))
1741                                  'face bface)
1742               (put-text-property (match-beginning 2) (match-end 2)
1743                                  'face eface))
1744             (when newline
1745               (end-of-line)
1746               (insert "\n"))))))))
1747
1748 (defun article-make-date-line (date type)
1749   "Return a DATE line of TYPE."
1750   (let ((time (condition-case ()
1751                   (date-to-time date)
1752                 (error '(0 0)))))
1753     (cond
1754      ;; Convert to the local timezone.  We have to slap a
1755      ;; `condition-case' round the calls to the timezone
1756      ;; functions since they aren't particularly resistant to
1757      ;; buggy dates.
1758      ((eq type 'local)
1759       (let ((tz (car (current-time-zone))))
1760         (format "Date: %s %s%04d" (current-time-string time)
1761                 (if (> tz 0) "+" "-") (abs (/ tz 36)))))
1762      ;; Convert to Universal Time.
1763      ((eq type 'ut)
1764       (concat "Date: "
1765               (current-time-string
1766                (let* ((e (parse-time-string date))
1767                      (tm (apply 'encode-time e))
1768                      (ms (car tm))
1769                      (ls (- (cadr tm) (car (current-time-zone)))))
1770                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1771                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1772                        (t (list ms ls)))))
1773               " UT"))
1774      ;; Get the original date from the article.
1775      ((eq type 'original)
1776       (concat "Date: " (if (string-match "\n+$" date)
1777                            (substring date 0 (match-beginning 0))
1778                          date)))
1779      ;; Let the user define the format.
1780      ((eq type 'user)
1781       (if (gnus-functionp gnus-article-time-format)
1782           (funcall gnus-article-time-format time)
1783         (concat
1784          "Date: "
1785          (format-time-string gnus-article-time-format time))))
1786      ;; ISO 8601.
1787      ((eq type 'iso8601)
1788       (concat
1789        "Date: "
1790        (format-time-string "%Y%m%dT%H%M%S" time)))
1791      ;; Do an X-Sent lapsed format.
1792      ((eq type 'lapsed)
1793       ;; If the date is seriously mangled, the timezone functions are
1794       ;; liable to bug out, so we ignore all errors.
1795       (let* ((now (current-time))
1796              (real-time (subtract-time now time))
1797              (real-sec (and real-time
1798                             (+ (* (float (car real-time)) 65536)
1799                                (cadr real-time))))
1800              (sec (and real-time (abs real-sec)))
1801              num prev)
1802         (cond
1803          ((null real-time)
1804           "X-Sent: Unknown")
1805          ((zerop sec)
1806           "X-Sent: Now")
1807          (t
1808           (concat
1809            "X-Sent: "
1810            ;; This is a bit convoluted, but basically we go
1811            ;; through the time units for years, weeks, etc,
1812            ;; and divide things to see whether that results
1813            ;; in positive answers.
1814            (mapconcat
1815             (lambda (unit)
1816               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1817                   ;; The (remaining) seconds are too few to
1818                   ;; be divided into this time unit.
1819                   ""
1820                 ;; It's big enough, so we output it.
1821                 (setq sec (- sec (* num (cdr unit))))
1822                 (prog1
1823                     (concat (if prev ", " "") (int-to-string
1824                                                (floor num))
1825                             " " (symbol-name (car unit))
1826                             (if (> num 1) "s" ""))
1827                   (setq prev t))))
1828             article-time-units "")
1829            ;; If dates are odd, then it might appear like the
1830            ;; article was sent in the future.
1831            (if (> real-sec 0)
1832                " ago"
1833              " in the future"))))))
1834      (t
1835       (error "Unknown conversion type: %s" type)))))
1836
1837 (defun article-date-local (&optional highlight)
1838   "Convert the current article date to the local timezone."
1839   (interactive (list t))
1840   (article-date-ut 'local highlight))
1841
1842 (defun article-date-original (&optional highlight)
1843   "Convert the current article date to what it was originally.
1844 This is only useful if you have used some other date conversion
1845 function and want to see what the date was before converting."
1846   (interactive (list t))
1847   (article-date-ut 'original highlight))
1848
1849 (defun article-date-lapsed (&optional highlight)
1850   "Convert the current article date to time lapsed since it was sent."
1851   (interactive (list t))
1852   (article-date-ut 'lapsed highlight))
1853
1854 (defun article-update-date-lapsed ()
1855   "Function to be run from a timer to update the lapsed time line."
1856   (let (deactivate-mark)
1857     (save-excursion
1858       (ignore-errors
1859         (walk-windows
1860          (lambda (w)
1861            (set-buffer (window-buffer w))
1862            (when (eq major-mode 'gnus-article-mode)
1863              (goto-char (point-min))
1864              (when (re-search-forward "^X-Sent:" nil t)
1865                (article-date-lapsed t))))
1866          nil 'visible)))))
1867
1868 (defun gnus-start-date-timer (&optional n)
1869   "Start a timer to update the X-Sent header in the article buffers.
1870 The numerical prefix says how frequently (in seconds) the function
1871 is to run."
1872   (interactive "p")
1873   (unless n
1874     (setq n 1))
1875   (gnus-stop-date-timer)
1876   (setq article-lapsed-timer
1877         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1878
1879 (defun gnus-stop-date-timer ()
1880   "Stop the X-Sent timer."
1881   (interactive)
1882   (when article-lapsed-timer
1883     (nnheader-cancel-timer article-lapsed-timer)
1884     (setq article-lapsed-timer nil)))
1885
1886 (defun article-date-user (&optional highlight)
1887   "Convert the current article date to the user-defined format.
1888 This format is defined by the `gnus-article-time-format' variable."
1889   (interactive (list t))
1890   (article-date-ut 'user highlight))
1891
1892 (defun article-date-iso8601 (&optional highlight)
1893   "Convert the current article date to ISO8601."
1894   (interactive (list t))
1895   (article-date-ut 'iso8601 highlight))
1896
1897 (defun article-show-all ()
1898   "Show all hidden text in the article buffer."
1899   (interactive)
1900   (save-excursion
1901     (let ((buffer-read-only nil))
1902       (gnus-article-unhide-text (point-min) (point-max)))))
1903
1904 (defun article-emphasize (&optional arg)
1905   "Emphasize text according to `gnus-emphasis-alist'."
1906   (interactive (gnus-article-hidden-arg))
1907   (unless (gnus-article-check-hidden-text 'emphasis arg)
1908     (save-excursion
1909       (let ((alist gnus-emphasis-alist)
1910             (buffer-read-only nil)
1911             (props (append '(article-type emphasis)
1912                            gnus-hidden-properties))
1913             regexp elem beg invisible visible face)
1914         (article-goto-body)
1915         (setq beg (point))
1916         (while (setq elem (pop alist))
1917           (goto-char beg)
1918           (setq regexp (car elem)
1919                 invisible (nth 1 elem)
1920                 visible (nth 2 elem)
1921                 face (nth 3 elem))
1922           (while (re-search-forward regexp nil t)
1923             (when (and (match-beginning visible) (match-beginning invisible))
1924               (gnus-article-hide-text
1925                (match-beginning invisible) (match-end invisible) props)
1926               (gnus-article-unhide-text-type
1927                (match-beginning visible) (match-end visible) 'emphasis)
1928               (gnus-put-text-property-excluding-newlines
1929                (match-beginning visible) (match-end visible) 'face face)
1930               (goto-char (match-end invisible)))))))))
1931
1932 (defvar gnus-summary-article-menu)
1933 (defvar gnus-summary-post-menu)
1934
1935 ;;; Saving functions.
1936
1937 (defun gnus-article-save (save-buffer file &optional num)
1938   "Save the currently selected article."
1939   (unless gnus-save-all-headers
1940     ;; Remove headers according to `gnus-saved-headers'.
1941     (let ((gnus-visible-headers
1942            (or gnus-saved-headers gnus-visible-headers))
1943           (gnus-article-buffer save-buffer))
1944       (save-excursion
1945         (set-buffer save-buffer)
1946         (article-hide-headers 1 t))))
1947   (save-window-excursion
1948     (if (not gnus-default-article-saver)
1949         (error "No default saver is defined")
1950       ;; !!! Magic!  The saving functions all save
1951       ;; `gnus-save-article-buffer' (or so they think), but we
1952       ;; bind that variable to our save-buffer.
1953       (set-buffer gnus-article-buffer)
1954       (let* ((gnus-save-article-buffer save-buffer)
1955              (filename
1956               (cond
1957                ((not gnus-prompt-before-saving) 'default)
1958                ((eq gnus-prompt-before-saving 'always) nil)
1959                (t file)))
1960              (gnus-number-of-articles-to-be-saved
1961               (when (eq gnus-prompt-before-saving t)
1962                 num)))                  ; Magic
1963         (set-buffer gnus-article-current-summary)
1964         (funcall gnus-default-article-saver filename)))))
1965
1966 (defun gnus-read-save-file-name (prompt &optional filename
1967                                         function group headers variable)
1968   (let ((default-name
1969           (funcall function group headers (symbol-value variable)))
1970         result)
1971     (setq
1972      result
1973      (cond
1974       ((eq filename 'default)
1975        default-name)
1976       ((eq filename t)
1977        default-name)
1978       (filename filename)
1979       (t
1980        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1981               (prompt
1982                (format prompt
1983                        (if (and gnus-number-of-articles-to-be-saved
1984                                 (> gnus-number-of-articles-to-be-saved 1))
1985                            (format "these %d articles"
1986                                    gnus-number-of-articles-to-be-saved)
1987                          "this article")))
1988               (file
1989                ;; Let the split methods have their say.
1990                (cond
1991                 ;; No split name was found.
1992                 ((null split-name)
1993                  (read-file-name
1994                   (concat prompt " (default "
1995                           (file-name-nondirectory default-name) ") ")
1996                   (file-name-directory default-name)
1997                   default-name))
1998                 ;; A single group name is returned.
1999                 ((stringp split-name)
2000                  (setq default-name
2001                        (funcall function split-name headers
2002                                 (symbol-value variable)))
2003                  (read-file-name
2004                   (concat prompt " (default "
2005                           (file-name-nondirectory default-name) ") ")
2006                   (file-name-directory default-name)
2007                   default-name))
2008                 ;; A single split name was found
2009                 ((= 1 (length split-name))
2010                  (let* ((name (expand-file-name
2011                                (car split-name) gnus-article-save-directory))
2012                         (dir (cond ((file-directory-p name)
2013                                     (file-name-as-directory name))
2014                                    ((file-exists-p name) name)
2015                                    (t gnus-article-save-directory))))
2016                    (read-file-name
2017                     (concat prompt " (default " name ") ")
2018                     dir name)))
2019                 ;; A list of splits was found.
2020                 (t
2021                  (setq split-name (nreverse split-name))
2022                  (let (result)
2023                    (let ((file-name-history
2024                           (nconc split-name file-name-history)))
2025                      (setq result
2026                            (expand-file-name
2027                             (read-file-name
2028                              (concat prompt " (`M-p' for defaults) ")
2029                              gnus-article-save-directory
2030                              (car split-name))
2031                             gnus-article-save-directory)))
2032                    (car (push result file-name-history)))))))
2033          ;; Create the directory.
2034          (gnus-make-directory (file-name-directory file))
2035          ;; If we have read a directory, we append the default file name.
2036          (when (file-directory-p file)
2037            (setq file (concat (file-name-as-directory file)
2038                               (file-name-nondirectory default-name))))
2039          ;; Possibly translate some characters.
2040          (nnheader-translate-file-chars file)))))
2041     (gnus-make-directory (file-name-directory result))
2042     (set variable result)))
2043
2044 (defun gnus-article-archive-name (group)
2045   "Return the first instance of an \"Archive-name\" in the current buffer."
2046   (let ((case-fold-search t))
2047     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2048       (nnheader-concat gnus-article-save-directory
2049                        (match-string 1)))))
2050
2051 (defun gnus-article-nndoc-name (group)
2052   "If GROUP is an nndoc group, return the name of the parent group."
2053   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2054     (gnus-group-get-parameter group 'save-article-group)))
2055
2056 (defun gnus-summary-save-in-rmail (&optional filename)
2057   "Append this article to Rmail file.
2058 Optional argument FILENAME specifies file name.
2059 Directory to save to is default to `gnus-article-save-directory'."
2060   (setq filename (gnus-read-save-file-name
2061                   "Save %s in rmail file:" filename
2062                   gnus-rmail-save-name gnus-newsgroup-name
2063                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2064   (gnus-eval-in-buffer-window gnus-save-article-buffer
2065     (save-excursion
2066       (save-restriction
2067         (widen)
2068         (gnus-output-to-rmail filename))))
2069   filename)
2070
2071 (defun gnus-summary-save-in-mail (&optional filename)
2072   "Append this article to Unix mail file.
2073 Optional argument FILENAME specifies file name.
2074 Directory to save to is default to `gnus-article-save-directory'."
2075   (setq filename (gnus-read-save-file-name
2076                   "Save %s in Unix mail file:" filename
2077                   gnus-mail-save-name gnus-newsgroup-name
2078                   gnus-current-headers 'gnus-newsgroup-last-mail))
2079   (gnus-eval-in-buffer-window gnus-save-article-buffer
2080     (save-excursion
2081       (save-restriction
2082         (widen)
2083         (if (and (file-readable-p filename)
2084                  (mail-file-babyl-p filename))
2085             (rmail-output-to-rmail-file filename t)
2086           (gnus-output-to-mail filename)))))
2087   filename)
2088
2089 (defun gnus-summary-save-in-file (&optional filename overwrite)
2090   "Append this article to file.
2091 Optional argument FILENAME specifies file name.
2092 Directory to save to is default to `gnus-article-save-directory'."
2093   (setq filename (gnus-read-save-file-name
2094                   "Save %s in file:" filename
2095                   gnus-file-save-name gnus-newsgroup-name
2096                   gnus-current-headers 'gnus-newsgroup-last-file))
2097   (gnus-eval-in-buffer-window gnus-save-article-buffer
2098     (save-excursion
2099       (save-restriction
2100         (widen)
2101         (when (and overwrite
2102                    (file-exists-p filename))
2103           (delete-file filename))
2104         (gnus-output-to-file filename))))
2105   filename)
2106
2107 (defun gnus-summary-write-to-file (&optional filename)
2108   "Write this article to a file.
2109 Optional argument FILENAME specifies file name.
2110 The directory to save in defaults to `gnus-article-save-directory'."
2111   (gnus-summary-save-in-file nil t))
2112
2113 (defun gnus-summary-save-body-in-file (&optional filename)
2114   "Append this article body to a file.
2115 Optional argument FILENAME specifies file name.
2116 The directory to save in defaults to `gnus-article-save-directory'."
2117   (setq filename (gnus-read-save-file-name
2118                   "Save %s body in file:" filename
2119                   gnus-file-save-name gnus-newsgroup-name
2120                   gnus-current-headers 'gnus-newsgroup-last-file))
2121   (gnus-eval-in-buffer-window gnus-save-article-buffer
2122     (save-excursion
2123       (save-restriction
2124         (widen)
2125         (when (article-goto-body)
2126           (narrow-to-region (point) (point-max)))
2127         (gnus-output-to-file filename))))
2128   filename)
2129
2130 (defun gnus-summary-save-in-pipe (&optional command)
2131   "Pipe this article to subprocess."
2132   (setq command
2133         (cond ((and (eq command 'default)
2134                     gnus-last-shell-command)
2135                gnus-last-shell-command)
2136               (command command)
2137               (t (read-string
2138                   (format
2139                    "Shell command on %s: "
2140                    (if (and gnus-number-of-articles-to-be-saved
2141                             (> gnus-number-of-articles-to-be-saved 1))
2142                        (format "these %d articles"
2143                                gnus-number-of-articles-to-be-saved)
2144                      "this article"))
2145                   gnus-last-shell-command))))
2146   (when (string-equal command "")
2147     (setq command gnus-last-shell-command))
2148   (gnus-eval-in-buffer-window gnus-article-buffer
2149     (save-restriction
2150       (widen)
2151       (shell-command-on-region (point-min) (point-max) command nil)))
2152   (setq gnus-last-shell-command command))
2153
2154 ;;; Article file names when saving.
2155
2156 (defun gnus-capitalize-newsgroup (newsgroup)
2157   "Capitalize NEWSGROUP name."
2158   (when (not (zerop (length newsgroup)))
2159     (concat (char-to-string (upcase (aref newsgroup 0)))
2160             (substring newsgroup 1))))
2161
2162 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2163   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2164 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2165 Otherwise, it is like ~/News/news/group/num."
2166   (let ((default
2167           (expand-file-name
2168            (concat (if (gnus-use-long-file-name 'not-save)
2169                        (gnus-capitalize-newsgroup newsgroup)
2170                      (gnus-newsgroup-directory-form newsgroup))
2171                    "/" (int-to-string (mail-header-number headers)))
2172            gnus-article-save-directory)))
2173     (if (and last-file
2174              (string-equal (file-name-directory default)
2175                            (file-name-directory last-file))
2176              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2177         default
2178       (or last-file default))))
2179
2180 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2181   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2182 If variable `gnus-use-long-file-name' is non-nil, it is
2183 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2184   (let ((default
2185           (expand-file-name
2186            (concat (if (gnus-use-long-file-name 'not-save)
2187                        newsgroup
2188                      (gnus-newsgroup-directory-form newsgroup))
2189                    "/" (int-to-string (mail-header-number headers)))
2190            gnus-article-save-directory)))
2191     (if (and last-file
2192              (string-equal (file-name-directory default)
2193                            (file-name-directory last-file))
2194              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2195         default
2196       (or last-file default))))
2197
2198 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2199   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2200 If variable `gnus-use-long-file-name' is non-nil, it is
2201 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2202   (or last-file
2203       (expand-file-name
2204        (if (gnus-use-long-file-name 'not-save)
2205            (gnus-capitalize-newsgroup newsgroup)
2206          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2207        gnus-article-save-directory)))
2208
2209 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2210   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2211 If variable `gnus-use-long-file-name' is non-nil, it is
2212 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2213   (or last-file
2214       (expand-file-name
2215        (if (gnus-use-long-file-name 'not-save)
2216            newsgroup
2217          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2218        gnus-article-save-directory)))
2219
2220 (eval-and-compile
2221   (mapcar
2222    (lambda (func)
2223      (let (afunc gfunc)
2224        (if (consp func)
2225            (setq afunc (car func)
2226                  gfunc (cdr func))
2227          (setq afunc func
2228                gfunc (intern (format "gnus-%s" func))))
2229        (fset gfunc
2230              (if (not (fboundp afunc))
2231                  nil
2232                `(lambda (&optional interactive &rest args)
2233                   ,(documentation afunc t)
2234                   (interactive (list t))
2235                   (save-excursion
2236                     (set-buffer gnus-article-buffer)
2237                     (if interactive
2238                         (call-interactively ',afunc)
2239                       (apply ',afunc args))))))))
2240    '(article-hide-headers
2241      article-hide-boring-headers
2242      article-treat-overstrike
2243      article-fill-long-lines
2244      article-capitalize-sentences
2245      article-remove-cr
2246      article-display-x-face
2247      article-de-quoted-unreadable
2248      article-mime-decode-quoted-printable
2249      article-hide-pgp
2250      article-strip-banner
2251      article-hide-pem
2252      article-hide-signature
2253      article-remove-trailing-blank-lines
2254      article-strip-leading-blank-lines
2255      article-strip-multiple-blank-lines
2256      article-strip-leading-space
2257      article-strip-trailing-space
2258      article-strip-blank-lines
2259      article-strip-all-blank-lines
2260      article-date-local
2261      article-date-iso8601
2262      article-date-original
2263      article-date-ut
2264      article-decode-mime-words
2265      article-decode-charset
2266      article-decode-encoded-words
2267      article-date-user
2268      article-date-lapsed
2269      article-emphasize
2270      article-treat-dumbquotes
2271      article-normalize-headers
2272      (article-show-all . gnus-article-show-all-headers))))
2273 \f
2274 ;;;
2275 ;;; Gnus article mode
2276 ;;;
2277
2278 (put 'gnus-article-mode 'mode-class 'special)
2279
2280 (gnus-define-keys gnus-article-mode-map
2281   " " gnus-article-goto-next-page
2282   "\177" gnus-article-goto-prev-page
2283   [delete] gnus-article-goto-prev-page
2284   [backspace] gnus-article-goto-prev-page
2285   "\C-c^" gnus-article-refer-article
2286   "h" gnus-article-show-summary
2287   "s" gnus-article-show-summary
2288   "\C-c\C-m" gnus-article-mail
2289   "?" gnus-article-describe-briefly
2290   gnus-mouse-2 gnus-article-push-button
2291   "\r" gnus-article-press-button
2292   "\t" gnus-article-next-button
2293   "\M-\t" gnus-article-prev-button
2294   "e" gnus-article-edit
2295   "<" beginning-of-buffer
2296   ">" end-of-buffer
2297   "\C-c\C-i" gnus-info-find-node
2298   "\C-c\C-b" gnus-bug
2299
2300   "\C-d" gnus-article-read-summary-keys
2301   "\M-*" gnus-article-read-summary-keys
2302   "\M-#" gnus-article-read-summary-keys
2303   "\M-^" gnus-article-read-summary-keys
2304   "\M-g" gnus-article-read-summary-keys)
2305
2306 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
2307 (mapcar
2308  (lambda (key)
2309    (unless (lookup-key gnus-article-mode-map key)
2310      (define-key gnus-article-mode-map key
2311        'gnus-article-read-summary-keys)))
2312  (delq nil
2313        (append
2314         (mapcar
2315          (lambda (elt)
2316            (let ((key (car elt)))
2317              (and (> (length key) 0)
2318                   (not (eq 'menu-bar (aref key 0)))
2319                   (symbolp (lookup-key gnus-summary-mode-map key))
2320                   key)))
2321          (accessible-keymaps gnus-summary-mode-map))
2322         (let ((c 127)
2323               keys)
2324           (while (>= c 32)
2325             (push (char-to-string c) keys)
2326             (decf c))
2327           keys))))
2328
2329 (defun gnus-article-make-menu-bar ()
2330   (gnus-turn-off-edit-menu 'article)
2331   (unless (boundp 'gnus-article-article-menu)
2332     (easy-menu-define
2333      gnus-article-article-menu gnus-article-mode-map ""
2334      '("Article"
2335        ["Scroll forwards" gnus-article-goto-next-page t]
2336        ["Scroll backwards" gnus-article-goto-prev-page t]
2337        ["Show summary" gnus-article-show-summary t]
2338        ["Fetch Message-ID at point" gnus-article-refer-article t]
2339        ["Mail to address at point" gnus-article-mail t]
2340        ["Send a bug report" gnus-bug t]))
2341
2342     (easy-menu-define
2343      gnus-article-treatment-menu gnus-article-mode-map ""
2344      '("Treatment"
2345        ["Hide headers" gnus-article-hide-headers t]
2346        ["Hide signature" gnus-article-hide-signature t]
2347        ["Hide citation" gnus-article-hide-citation t]
2348        ["Treat overstrike" gnus-article-treat-overstrike t]
2349        ["Remove carriage return" gnus-article-remove-cr t]))
2350
2351     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2352
2353     (when (boundp 'gnus-summary-post-menu)
2354       (define-key gnus-article-mode-map [menu-bar post]
2355         (cons "Post" gnus-summary-post-menu)))
2356
2357     (gnus-run-hooks 'gnus-article-menu-hook)))
2358
2359 (defun gnus-article-mode ()
2360   "Major mode for displaying an article.
2361
2362 All normal editing commands are switched off.
2363
2364 The following commands are available in addition to all summary mode
2365 commands:
2366 \\<gnus-article-mode-map>
2367 \\[gnus-article-next-page]\t Scroll the article one page forwards
2368 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2369 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2370 \\[gnus-article-show-summary]\t Display the summary buffer
2371 \\[gnus-article-mail]\t Send a reply to the address near point
2372 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2373 \\[gnus-info-find-node]\t Go to the Gnus info node"
2374   (interactive)
2375   (when (gnus-visual-p 'article-menu 'menu)
2376     (gnus-article-make-menu-bar))
2377   (gnus-simplify-mode-line)
2378   (setq mode-name "Article")
2379   (setq major-mode 'gnus-article-mode)
2380   (make-local-variable 'minor-mode-alist)
2381   (unless (assq 'gnus-show-mime minor-mode-alist)
2382     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
2383   (use-local-map gnus-article-mode-map)
2384   (gnus-update-format-specifications nil 'article-mode)
2385   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2386   (make-local-variable 'gnus-page-broken)
2387   (make-local-variable 'gnus-button-marker-list)
2388   (make-local-variable 'gnus-article-current-summary)
2389   (make-local-variable 'gnus-article-mime-handles)
2390   (make-local-variable 'gnus-article-decoded-p)
2391   (make-local-variable 'gnus-article-mime-handle-alist)
2392   (make-local-variable 'gnus-article-washed-types)
2393   (gnus-set-default-directory)
2394   (buffer-disable-undo)
2395   (setq buffer-read-only t)
2396   (set-syntax-table gnus-article-mode-syntax-table)
2397   (gnus-run-hooks 'gnus-article-mode-hook))
2398
2399 (defun gnus-article-setup-buffer ()
2400   "Initialize the article buffer."
2401   (let* ((name (if gnus-single-article-buffer "*Article*"
2402                  (concat "*Article " gnus-newsgroup-name "*")))
2403          (original
2404           (progn (string-match "\\*Article" name)
2405                  (concat " *Original Article"
2406                          (substring name (match-end 0))))))
2407     (setq gnus-article-buffer name)
2408     (setq gnus-original-article-buffer original)
2409     (setq gnus-article-mime-handle-alist nil)
2410     ;; This might be a variable local to the summary buffer.
2411     (unless gnus-single-article-buffer
2412       (save-excursion
2413         (set-buffer gnus-summary-buffer)
2414         (setq gnus-article-buffer name)
2415         (setq gnus-original-article-buffer original)
2416         (gnus-set-global-variables)))
2417     ;; Init original article buffer.
2418     (save-excursion
2419       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2420       (setq major-mode 'gnus-original-article-mode)
2421       (make-local-variable 'gnus-original-article))
2422     (if (get-buffer name)
2423         (save-excursion
2424           (set-buffer name)
2425           (buffer-disable-undo)
2426           (setq buffer-read-only t)
2427           (unless (eq major-mode 'gnus-article-mode)
2428             (gnus-article-mode))
2429           (current-buffer))
2430       (save-excursion
2431         (set-buffer (gnus-get-buffer-create name))
2432         (gnus-article-mode)
2433         (make-local-variable 'gnus-summary-buffer)
2434         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2435         (current-buffer)))))
2436
2437 ;; Set article window start at LINE, where LINE is the number of lines
2438 ;; from the head of the article.
2439 (defun gnus-article-set-window-start (&optional line)
2440   (set-window-start
2441    (get-buffer-window gnus-article-buffer t)
2442    (save-excursion
2443      (set-buffer gnus-article-buffer)
2444      (goto-char (point-min))
2445      (if (not line)
2446          (point-min)
2447        (gnus-message 6 "Moved to bookmark")
2448        (search-forward "\n\n" nil t)
2449        (forward-line line)
2450        (point)))))
2451
2452 ;;; @@ article filters
2453 ;;;
2454
2455 (defun gnus-article-display-mime-message ()
2456   "Article display method for MIME message."
2457   ;; called from `gnus-original-article-buffer'.
2458   (let (charset all-headers)
2459     (with-current-buffer gnus-summary-buffer
2460       (setq charset default-mime-charset
2461             all-headers gnus-have-all-headers))
2462     (make-local-variable 'default-mime-charset)
2463     (setq default-mime-charset charset)
2464     (mime-display-message mime-message-structure
2465                           gnus-article-buffer nil gnus-article-mode-map)
2466     (when all-headers
2467       (gnus-article-hide-headers nil -1))
2468     (make-local-variable 'default-mime-charset)
2469     (setq default-mime-charset charset)
2470     )
2471   ;; `mime-display-message' changes current buffer to `gnus-article-buffer'.
2472   (make-local-variable 'mime-button-mother-dispatcher)
2473   (setq mime-button-mother-dispatcher
2474         (function gnus-article-push-button))
2475   (run-hooks 'gnus-mime-article-prepare-hook))
2476
2477 (defun gnus-article-display-traditional-message ()
2478   "Article display method for traditional message."
2479   (set-buffer gnus-article-buffer)
2480   (let (buffer-read-only)
2481     (erase-buffer)
2482     (insert-buffer-substring gnus-original-article-buffer)))
2483
2484 (defun gnus-article-make-full-mail-header (&optional number charset)
2485   "Create a new mail header structure in a raw article buffer."
2486   (unless (and number charset)
2487     (save-current-buffer
2488       (set-buffer gnus-summary-buffer)
2489       (unless number
2490         (setq number (or (cdr gnus-article-current) 0)))
2491       (unless charset
2492         (setq charset (or default-mime-charset 'x-ctext)))))
2493   (goto-char (point-min))
2494   (let ((header-end (if (search-forward "\n\n" nil t)
2495                         (1- (point))
2496                       (goto-char (point-max))))
2497         (chars (- (point-max) (point)))
2498         (lines (count-lines (point) (point-max)))
2499         (default-mime-charset charset)
2500         xref)
2501     (narrow-to-region (point-min) header-end)
2502     (setq xref (std11-fetch-field "xref"))
2503     (prog1
2504         (make-full-mail-header
2505          number
2506          (std11-fetch-field "subject")
2507          (std11-fetch-field "from")
2508          (std11-fetch-field "date")
2509          (std11-fetch-field "message-id")
2510          (std11-fetch-field "references")
2511          chars
2512          lines
2513          (when xref (concat "Xref: " xref)))
2514       (widen))))
2515
2516 (defun gnus-article-prepare (article &optional all-headers header)
2517   "Prepare ARTICLE in article mode buffer.
2518 ARTICLE should either be an article number or a Message-ID.
2519 If ARTICLE is an id, HEADER should be the article headers.
2520 If ALL-HEADERS is non-nil, no headers are hidden."
2521   (save-excursion
2522     ;; Make sure we start in a summary buffer.
2523     (unless (eq major-mode 'gnus-summary-mode)
2524       (set-buffer gnus-summary-buffer))
2525     (setq gnus-summary-buffer (current-buffer))
2526     (let* ((gnus-article (if header (mail-header-number header) article))
2527            (summary-buffer (current-buffer))
2528            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2529            (group gnus-newsgroup-name)
2530            result)
2531       (save-excursion
2532         (gnus-article-setup-buffer)
2533         (set-buffer gnus-original-article-buffer)
2534         ;; Deactivate active regions.
2535         (when (and (boundp 'transient-mark-mode)
2536                    transient-mark-mode)
2537           (setq mark-active nil))
2538         (if (not (setq result (let ((buffer-read-only nil))
2539                                 (gnus-request-article-this-buffer
2540                                  article group))))
2541             ;; There is no such article.
2542             (save-excursion
2543               (when (and (numberp article)
2544                          (not (memq article gnus-newsgroup-sparse)))
2545                 (setq gnus-article-current
2546                       (cons gnus-newsgroup-name article))
2547                 (set-buffer gnus-summary-buffer)
2548                 (setq gnus-current-article article)
2549                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2550                     (progn
2551                       (gnus-summary-set-agent-mark article)
2552                       (message "Message marked for downloading"))
2553                   (gnus-summary-mark-article article gnus-canceled-mark)
2554                   (unless (memq article gnus-newsgroup-sparse)
2555                     (gnus-error 1
2556                      "No such article (may have expired or been canceled)")))))
2557           (if (or (eq result 'pseudo)
2558                   (eq result 'nneething))
2559               (progn
2560                 (save-excursion
2561                   (set-buffer summary-buffer)
2562                   (push article gnus-newsgroup-history)
2563                   (setq gnus-last-article gnus-current-article
2564                         gnus-current-article 0
2565                         gnus-current-headers nil
2566                         gnus-article-current nil)
2567                   (if (eq result 'nneething)
2568                       (gnus-configure-windows 'summary)
2569                     (gnus-configure-windows 'article))
2570                   (gnus-set-global-variables))
2571                 (let ((gnus-article-mime-handle-alist-1
2572                        gnus-article-mime-handle-alist))
2573                   (gnus-set-mode-line 'article)))
2574             ;; The result from the `request' was an actual article -
2575             ;; or at least some text that is now displayed in the
2576             ;; article buffer.
2577             (when (and (numberp article)
2578                        (not (eq article gnus-current-article)))
2579               ;; Seems like a new article has been selected.
2580               ;; `gnus-current-article' must be an article number.
2581               (save-excursion
2582                 (set-buffer summary-buffer)
2583                 (push article gnus-newsgroup-history)
2584                 (setq gnus-last-article gnus-current-article
2585                       gnus-current-article article
2586                       gnus-current-headers
2587                       (gnus-summary-article-header gnus-current-article)
2588                       gnus-article-current
2589                       (cons gnus-newsgroup-name gnus-current-article))
2590                 (unless (vectorp gnus-current-headers)
2591                   (setq gnus-current-headers nil))
2592                 (gnus-summary-goto-subject gnus-current-article)
2593                 (when (gnus-summary-show-thread)
2594                   ;; If the summary buffer really was folded, the
2595                   ;; previous goto may not actually have gone to
2596                   ;; the right article, but the thread root instead.
2597                   ;; So we go again.
2598                   (gnus-summary-goto-subject gnus-current-article))
2599                 (gnus-run-hooks 'gnus-mark-article-hook)
2600                 (gnus-set-mode-line 'summary)
2601                 (when (gnus-visual-p 'article-highlight 'highlight)
2602                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2603                 ;; Set the global newsgroup variables here.
2604                 ;; Suggested by Jim Sisolak
2605                 ;; <sisolak@trans4.neep.wisc.edu>.
2606                 (gnus-set-global-variables)
2607                 (setq gnus-have-all-headers
2608                       (or all-headers gnus-show-all-headers))))
2609             (when (or (numberp article)
2610                       (stringp article))
2611               (gnus-article-prepare-display)
2612               ;; Do page break.
2613               (goto-char (point-min))
2614               (setq gnus-page-broken
2615                     (when gnus-break-pages
2616                       (gnus-narrow-to-page)
2617                       t)))
2618             (let ((gnus-article-mime-handle-alist-1
2619                    gnus-article-mime-handle-alist))
2620               (gnus-set-mode-line 'article))
2621             (article-goto-body)
2622             (set-window-point (get-buffer-window (current-buffer)) (point))
2623             (gnus-configure-windows 'article)
2624             t))))))
2625
2626 ;;;###autoload
2627 (defun gnus-article-prepare-display ()
2628   "Make the current buffer look like a nice article."
2629   (let ((method
2630          (if gnus-show-mime
2631              (progn
2632                (setq mime-message-structure gnus-current-headers)
2633                gnus-article-display-method-for-mime)
2634            gnus-article-display-method-for-traditional)))
2635     (gnus-run-hooks 'gnus-tmp-internal-hook)
2636     (gnus-run-hooks 'gnus-article-prepare-hook)
2637     ;; Display message.
2638     (funcall method)
2639     ;; Associate this article with the current summary buffer.
2640     (setq gnus-article-current-summary gnus-summary-buffer)
2641     ;; Perform the article display hooks.
2642     (gnus-run-hooks 'gnus-article-display-hook)))
2643
2644 ;;;
2645 ;;; Gnus MIME viewing functions
2646 ;;;
2647
2648 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2649   "The following specs can be used:
2650 %t  The MIME type
2651 %T  MIME type, along with additional info
2652 %n  The `name' parameter
2653 %d  The description, if any
2654 %l  The length of the encoded part
2655 %p  The part identifier number
2656 %e  Dots if the part isn't displayed")
2657
2658 (defvar gnus-mime-button-line-format-alist
2659   '((?t gnus-tmp-type ?s)
2660     (?T gnus-tmp-type-long ?s)
2661     (?n gnus-tmp-name ?s)
2662     (?d gnus-tmp-description ?s)
2663     (?p gnus-tmp-id ?s)
2664     (?l gnus-tmp-length ?d)
2665     (?e gnus-tmp-dots ?s)))
2666
2667 (defvar gnus-mime-button-commands
2668   '((gnus-article-press-button  "\r"    "Toggle Display")
2669     (gnus-mime-view-part        "v"     "View Interactively...")
2670     (gnus-mime-save-part        "o"     "Save...")
2671     (gnus-mime-copy-part        "c"     "View As Text, In Other Buffer")
2672     (gnus-mime-inline-part      "i"     "View As Text, In This Buffer")
2673     (gnus-mime-internalize-part "E"     "View Internally")
2674     (gnus-mime-externalize-part "e"     "View Externally")
2675     (gnus-mime-pipe-part        "|"     "Pipe To Command...")))
2676
2677 (defun gnus-article-mime-part-status ()
2678   (if gnus-article-mime-handle-alist-1
2679       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2680     ""))
2681
2682 (defvar gnus-mime-button-map nil)
2683 (unless gnus-mime-button-map
2684   (setq gnus-mime-button-map (make-sparse-keymap))
2685   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2686   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2687   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
2688   (mapcar (lambda (c)
2689             (define-key gnus-mime-button-map (cadr c) (car c)))
2690           gnus-mime-button-commands))
2691
2692 (defun gnus-mime-button-menu (event)
2693   "Construct a context-sensitive menu of MIME commands."
2694   (interactive "e")
2695   (save-excursion
2696     (let ((pos (event-start event)))
2697       (set-buffer (window-buffer (posn-window pos)))
2698       (goto-char (posn-point pos))
2699       (gnus-article-check-buffer)
2700       (let ((response (x-popup-menu
2701                        t `("MIME Part"
2702                            ("" ,@(mapcar (lambda (c)
2703                                            (cons (caddr c) (car c)))
2704                                          gnus-mime-button-commands))))))
2705         (if response
2706             (funcall response))))))
2707
2708 (defun gnus-mime-view-all-parts (&optional handles)
2709   "View all the MIME parts."
2710   (interactive)
2711   (save-current-buffer
2712     (set-buffer gnus-article-buffer)
2713     (let ((handles (or handles gnus-article-mime-handles))
2714           (mail-parse-charset gnus-newsgroup-charset))
2715       (if (stringp (car handles))
2716           (gnus-mime-view-all-parts (cdr handles))
2717         (mapcar 'mm-display-part handles)))))
2718
2719 (defun gnus-mime-save-part ()
2720   "Save the MIME part under point."
2721   (interactive)
2722   (gnus-article-check-buffer)
2723   (let ((data (get-text-property (point) 'gnus-data)))
2724     (mm-save-part data)))
2725
2726 (defun gnus-mime-pipe-part ()
2727   "Pipe the MIME part under point to a process."
2728   (interactive)
2729   (gnus-article-check-buffer)
2730   (let ((data (get-text-property (point) 'gnus-data)))
2731     (mm-pipe-part data)))
2732
2733 (defun gnus-mime-view-part ()
2734   "Interactively choose a view method for the MIME part under point."
2735   (interactive)
2736   (gnus-article-check-buffer)
2737   (let ((data (get-text-property (point) 'gnus-data)))
2738     (mm-interactively-view-part data)))
2739
2740 (defun gnus-mime-copy-part (&optional handle)
2741   "Put the the MIME part under point into a new buffer."
2742   (interactive)
2743   (gnus-article-check-buffer)
2744   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2745          (contents (mm-get-part handle))|
2746          (base (file-name-nondirectory
2747                 (or
2748                  (mail-content-type-get (mm-handle-type handle) 'name)
2749                  (mail-content-type-get (mm-handle-type handle)
2750                                         'filename)
2751                  "*decoded*")))
2752          (buffer (generate-new-buffer base)))
2753     (switch-to-buffer buffer)
2754     (insert contents)
2755     ;; We do it this way to make `normal-mode' set the appropriate mode.
2756     (unwind-protect
2757         (progn
2758           (setq buffer-file-name (expand-file-name base))
2759           (normal-mode))
2760       (setq buffer-file-name nil))
2761     (goto-char (point-min))))
2762
2763 (defun gnus-mime-inline-part (&optional charset)
2764   "Insert the MIME part under point into the current buffer."
2765   (interactive "P") ; For compatibility reasons we are not using "z".
2766   (gnus-article-check-buffer)
2767   (let* ((data (get-text-property (point) 'gnus-data))
2768          contents
2769          (b (point))
2770          buffer-read-only)
2771     (if (mm-handle-undisplayer data)
2772         (mm-remove-part data)
2773       (setq contents (mm-get-part data))
2774       (forward-line 2)
2775       (when charset
2776         (unless (symbolp charset)
2777           (setq charset (mm-read-coding-system "Charset: ")))
2778         (setq contents (mm-decode-coding-string contents charset)))
2779       (mm-insert-inline data contents)
2780       (goto-char b))))
2781
2782 (defun gnus-mime-externalize-part (&optional handle)
2783   "View the MIME part under point with an external viewer."
2784   (interactive)
2785   (gnus-article-check-buffer)
2786   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2787          (mm-user-display-methods nil)
2788          (mm-all-images-fit t)
2789          (mail-parse-charset gnus-newsgroup-charset))
2790     (if (mm-handle-undisplayer handle)
2791         (mm-remove-part handle)
2792       (mm-display-part handle))))
2793
2794 (defun gnus-mime-internalize-part (&optional handle)
2795   "View the MIME part under point with an internal viewer."
2796   (interactive)
2797   (gnus-article-check-buffer)
2798   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2799          (mm-user-display-methods '((".*" . inline)))
2800          (mm-all-images-fit t)
2801          (mail-parse-charset gnus-newsgroup-charset))
2802     (if (mm-handle-undisplayer handle)
2803         (mm-remove-part handle)
2804       (mm-display-part handle))))
2805
2806 (defun gnus-article-part-wrapper (n function)
2807   (save-current-buffer
2808     (set-buffer gnus-article-buffer)
2809     (when (> n (length gnus-article-mime-handle-alist))
2810       (error "No such part"))
2811     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2812       (funcall function handle))))
2813
2814 (defun gnus-article-pipe-part (n)
2815   "Pipe MIME part N, which is the numerical prefix."
2816   (interactive "p")
2817   (gnus-article-part-wrapper n 'mm-pipe-part))
2818
2819 (defun gnus-article-save-part (n)
2820   "Save MIME part N, which is the numerical prefix."
2821   (interactive "p")
2822   (gnus-article-part-wrapper n 'mm-save-part))
2823
2824 (defun gnus-article-interactively-view-part (n)
2825   "View MIME part N interactively, which is the numerical prefix."
2826   (interactive "p")
2827   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2828
2829 (defun gnus-article-copy-part (n)
2830   "Copy MIME part N, which is the numerical prefix."
2831   (interactive "p")
2832   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2833
2834 (defun gnus-article-externalize-part (n)
2835   "View MIME part N externally, which is the numerical prefix."
2836   (interactive "p")
2837   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2838
2839 (defun gnus-article-inline-part (n)
2840   "Inline MIME part N, which is the numerical prefix."
2841   (interactive "p")
2842   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
2843
2844 (defun gnus-article-view-part (n)
2845   "View MIME part N, which is the numerical prefix."
2846   (interactive "p")
2847   (save-current-buffer
2848     (set-buffer gnus-article-buffer)
2849     (when (> n (length gnus-article-mime-handle-alist))
2850       (error "No such part"))
2851     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2852       (when (gnus-article-goto-part n)
2853         (if (equal (car handle) "multipart/alternative")
2854             (gnus-article-press-button)
2855           (when (eq (gnus-mm-display-part handle) 'internal)
2856             (gnus-set-window-start)))))))
2857
2858 (defun gnus-mm-display-part (handle)
2859   "Display HANDLE and fix MIME button."
2860   (let ((id (get-text-property (point) 'gnus-part))
2861         (point (point))
2862         buffer-read-only)
2863     (forward-line 1)
2864     (prog1
2865         (let ((window (selected-window))
2866               (mail-parse-charset gnus-newsgroup-charset))
2867           (save-excursion
2868             (unwind-protect
2869                 (let ((win (get-buffer-window (current-buffer) t))
2870                       (beg (point)))
2871                   (when win
2872                     (select-window win))
2873                   (goto-char point)
2874                   (forward-line)
2875                   (if (mm-handle-displayed-p handle)
2876                       ;; This will remove the part.
2877                       (mm-display-part handle)
2878                     (save-restriction
2879                       (narrow-to-region (point) (1+ (point)))
2880                       (mm-display-part handle)
2881                       (gnus-treat-article
2882                        nil id
2883                        (1- (length gnus-article-mime-handles))
2884                        (car (mm-handle-type handle))))))
2885               (select-window window))))
2886       (goto-char point)
2887       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
2888       (gnus-insert-mime-button
2889        handle id (list (mm-handle-displayed-p handle)))
2890       (goto-char point))))
2891
2892 (defun gnus-article-goto-part (n)
2893   "Go to MIME part N."
2894   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
2895     (when point
2896       (goto-char point))))
2897
2898 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
2899   (let ((gnus-tmp-name
2900          (or (mail-content-type-get (mm-handle-type handle)
2901                                     'name)
2902              (mail-content-type-get (mm-handle-disposition handle)
2903                                     'filename)
2904              ""))
2905         (gnus-tmp-type (car (mm-handle-type handle)))
2906         (gnus-tmp-description
2907          (mail-decode-encoded-word-string (or (mm-handle-description handle)
2908                                               "")))
2909         (gnus-tmp-dots
2910          (if (if displayed (car displayed)
2911                (mm-handle-displayed-p handle))
2912              "" "..."))
2913         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
2914                            (buffer-size)))
2915         gnus-tmp-type-long b e)
2916     (when (string-match ".*/" gnus-tmp-name)
2917       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
2918     (setq gnus-tmp-type-long (concat gnus-tmp-type
2919                                      (and (not (equal gnus-tmp-name ""))
2920                                           (concat "; " gnus-tmp-name))))
2921     (or (equal gnus-tmp-description "")
2922         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
2923     (unless (bolp)
2924       (insert "\n"))
2925     (setq b (point))
2926     (gnus-eval-format
2927      gnus-mime-button-line-format gnus-mime-button-line-format-alist
2928      `(local-map ,gnus-mime-button-map
2929                  keymap ,gnus-mime-button-map
2930                  gnus-callback gnus-mm-display-part
2931                  gnus-part ,gnus-tmp-id
2932                  article-type annotation
2933                  gnus-data ,handle))
2934     (setq e (point))
2935     (widget-convert-button 'link b e
2936                            :mime-handle handle
2937                            :action 'gnus-widget-press-button
2938                            :button-keymap gnus-mime-button-map
2939                            :help-echo
2940                            (lambda (widget)
2941                              ;; Needed to properly clear the message
2942                              ;; due to a bug in wid-edit
2943                              (setq help-echo-owns-message t)
2944                              (format
2945                               "Click to %s the MIME part; %s for more options"
2946                               (if (mm-handle-displayed-p
2947                                    (widget-get widget :mime-handle))
2948                                   "hide" "show")
2949                               (if gnus-xemacs "button3" "mouse-3"))))))
2950
2951 (defun gnus-widget-press-button (elems el)
2952   (goto-char (widget-get elems :from))
2953   (gnus-article-press-button))
2954
2955 (defvar gnus-displaying-mime nil)
2956
2957 (defun gnus-display-mime (&optional ihandles)
2958   "Display the MIME parts."
2959   (save-excursion
2960     (save-selected-window
2961       (let ((window (get-buffer-window gnus-article-buffer))
2962             (point (point)))
2963         (when window
2964           (select-window window)
2965           ;; We have to do this since selecting the window
2966           ;; may change the point.  So we set the window point.
2967           (set-window-point window point)))
2968       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
2969              buffer-read-only handle name type b e display)
2970         (when (and (not ihandles)
2971                    (not gnus-displaying-mime))
2972           ;; Top-level call; we clean up.
2973           (mm-destroy-parts gnus-article-mime-handles)
2974           (setq gnus-article-mime-handles handles
2975                 gnus-article-mime-handle-alist nil)
2976           ;; We allow users to glean info from the handles.
2977           (when gnus-article-mime-part-function
2978             (gnus-mime-part-function handles)))
2979         (if (and handles
2980                  (or (not (stringp (car handles)))
2981                      (cdr handles)))
2982             (progn
2983               (when (and (not ihandles)
2984                          (not gnus-displaying-mime))
2985                 ;; Clean up for mime parts.
2986                 (article-goto-body)
2987                 (delete-region (point) (point-max)))
2988               (let ((gnus-displaying-mime t))
2989                 (gnus-mime-display-part handles)))
2990           (save-restriction
2991             (article-goto-body)
2992             (narrow-to-region (point) (point-max))
2993             (gnus-treat-article nil 1 1)
2994             (widen)))
2995         ;; Highlight the headers.
2996         (save-excursion
2997           (save-restriction
2998             (article-goto-body)
2999             (narrow-to-region (point-min) (point))
3000             (gnus-treat-article 'head)))))))
3001
3002 (defvar gnus-mime-display-multipart-as-mixed nil)
3003
3004 (defun gnus-mime-display-part (handle)
3005   (cond
3006    ;; Single part.
3007    ((not (stringp (car handle)))
3008     (gnus-mime-display-single handle))
3009    ;; multipart/alternative
3010    ((and (equal (car handle) "multipart/alternative")
3011          (not gnus-mime-display-multipart-as-mixed))
3012     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3013       (push (cons id handle) gnus-article-mime-handle-alist)
3014       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3015    ;; multipart/related
3016    ((and (equal (car handle) "multipart/related")
3017          (not gnus-mime-display-multipart-as-mixed))
3018     ;;;!!!We should find the start part, but we just default
3019     ;;;!!!to the first part.
3020     (gnus-mime-display-part (cadr handle)))
3021    ;; Other multiparts are handled like multipart/mixed.
3022    (t
3023     (gnus-mime-display-mixed (cdr handle)))))
3024
3025 (defun gnus-mime-part-function (handles)
3026   (if (stringp (car handles))
3027       (mapcar 'gnus-mime-part-function (cdr handles))
3028     (funcall gnus-article-mime-part-function handles)))
3029
3030 (defun gnus-mime-display-mixed (handles)
3031   (mapcar 'gnus-mime-display-part handles))
3032
3033 (defun gnus-mime-display-single (handle)
3034   (let ((type (car (mm-handle-type handle)))
3035         (ignored gnus-ignored-mime-types)
3036         (not-attachment t)
3037         (move nil)
3038         display text)
3039     (catch 'ignored
3040       (progn
3041         (while ignored
3042           (when (string-match (pop ignored) type)
3043             (throw 'ignored nil)))
3044         (if (and (setq not-attachment
3045                        (or (not (mm-handle-disposition handle))
3046                            (equal (car (mm-handle-disposition handle))
3047                                   "inline")
3048                            (mm-attachment-override-p type)))
3049                  (mm-automatic-display-p type)
3050                  (or (mm-inlinable-part-p type)
3051                      (mm-automatic-external-display-p type)))
3052             (setq display t)
3053           (when (equal (car (split-string type "/"))
3054                        "text")
3055             (setq text t)))
3056         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3057           (push (cons id handle) gnus-article-mime-handle-alist)
3058           (when (or (not display)
3059                     (not (gnus-unbuttonized-mime-type-p type)))
3060             (gnus-article-insert-newline)
3061             (gnus-insert-mime-button
3062              handle id (list (or display (and not-attachment text))))
3063             (gnus-article-insert-newline)
3064             (gnus-article-insert-newline)
3065             (setq move t)))
3066         (let ((beg (point)))
3067           (cond
3068            (display
3069             (when move
3070               (forward-line -2))
3071             (let ((mail-parse-charset gnus-newsgroup-charset))
3072               (mm-display-part handle t))
3073             (goto-char (point-max)))
3074            ((and text not-attachment)
3075             (when move
3076               (forward-line -2))
3077             (gnus-article-insert-newline)
3078             (mm-insert-inline handle (mm-get-part handle))
3079             (goto-char (point-max))))
3080           ;; Do highlighting.
3081           (save-excursion
3082             (save-restriction
3083               (narrow-to-region beg (point))
3084               (gnus-treat-article
3085                nil (length gnus-article-mime-handle-alist)
3086                (1- (length gnus-article-mime-handles))
3087                (car (mm-handle-type handle))))))))))
3088
3089 (defun gnus-unbuttonized-mime-type-p (type)
3090   "Say whether TYPE is to be unbuttonized."
3091   (unless gnus-inhibit-mime-unbuttonizing
3092     (catch 'found
3093       (let ((types gnus-unbuttonized-mime-types))
3094         (while types
3095           (when (string-match (pop types) type)
3096             (throw 'found t)))))))
3097
3098 (defun gnus-article-insert-newline ()
3099   "Insert a newline, but mark it as undeletable."
3100   (gnus-put-text-property
3101    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3102
3103 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3104   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3105          (ihandles handles)
3106          (point (point))
3107          handle buffer-read-only from props begend not-pref)
3108     (save-window-excursion
3109       (save-restriction
3110         (when ibegend
3111           (narrow-to-region (car ibegend)
3112                             (or (cdr ibegend)
3113                                 (progn
3114                                   (goto-char (car ibegend))
3115                                   (forward-line 2)
3116                                   (point))))
3117           (delete-region (point-min) (point-max))
3118           (mm-remove-parts handles))
3119         (setq begend (list (point-marker)))
3120         ;; Do the toggle.
3121         (unless (setq not-pref (cadr (member preferred ihandles)))
3122           (setq not-pref (car ihandles)))
3123         (when (or ibegend
3124                   (not (gnus-unbuttonized-mime-type-p
3125                         "multipart/alternative")))
3126           (gnus-add-text-properties
3127            (setq from (point))
3128            (progn
3129              (insert (format "%d.  " id))
3130              (point))
3131            `(gnus-callback
3132              (lambda (handles)
3133                (unless ,(not ibegend)
3134                  (setq gnus-article-mime-handle-alist
3135                        ',gnus-article-mime-handle-alist))
3136                (gnus-mime-display-alternative
3137                 ',ihandles ',not-pref ',begend ,id))
3138              local-map ,gnus-mime-button-map
3139              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3140              face ,gnus-article-button-face
3141              keymap ,gnus-mime-button-map
3142              gnus-part ,id
3143              gnus-data ,handle))
3144           (widget-convert-button 'link from (point)
3145                                  :action 'gnus-widget-press-button
3146                                  :button-keymap gnus-widget-button-keymap)
3147           ;; Do the handles
3148           (while (setq handle (pop handles))
3149             (gnus-add-text-properties
3150              (setq from (point))
3151              (progn
3152                (insert (format "(%c) %-18s"
3153                                (if (equal handle preferred) ?* ? )
3154                                (if (stringp (car handle))
3155                                    (car handle)
3156                                  (car (mm-handle-type handle)))))
3157                (point))
3158              `(gnus-callback
3159                (lambda (handles)
3160                  (unless ,(not ibegend)
3161                    (setq gnus-article-mime-handle-alist
3162                          ',gnus-article-mime-handle-alist))
3163                  (gnus-mime-display-alternative
3164                   ',ihandles ',handle ',begend ,id))
3165                local-map ,gnus-mime-button-map
3166                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3167                face ,gnus-article-button-face
3168                keymap ,gnus-mime-button-map
3169                gnus-part ,id
3170                gnus-data ,handle))
3171             (widget-convert-button 'link from (point)
3172                                    :action 'gnus-widget-press-button
3173                                    :button-keymap gnus-widget-button-keymap)
3174             (insert "  "))
3175           (insert "\n\n"))
3176         (when preferred
3177           (if (stringp (car preferred))
3178               (gnus-display-mime preferred)
3179             (let ((mail-parse-charset gnus-newsgroup-charset))
3180               (mm-display-part preferred)))
3181           (goto-char (point-max))
3182           (setcdr begend (point-marker)))))
3183     (when ibegend
3184       (goto-char point))))
3185
3186 (defun gnus-article-wash-status ()
3187   "Return a string which display status of article washing."
3188   (save-excursion
3189     (set-buffer gnus-article-buffer)
3190     (let ((cite (gnus-article-hidden-text-p 'cite))
3191           (headers (gnus-article-hidden-text-p 'headers))
3192           (boring (gnus-article-hidden-text-p 'boring-headers))
3193           (pgp (gnus-article-hidden-text-p 'pgp))
3194           (pem (gnus-article-hidden-text-p 'pem))
3195           (signature (gnus-article-hidden-text-p 'signature))
3196           (overstrike (gnus-article-hidden-text-p 'overstrike))
3197           (emphasis (gnus-article-hidden-text-p 'emphasis))
3198           (mime gnus-show-mime))
3199       (format "%c%c%c%c%c%c%c"
3200               (if cite ?c ? )
3201               (if (or headers boring) ?h ? )
3202               (if (or pgp pem) ?p ? )
3203               (if signature ?s ? )
3204               (if overstrike ?o ? )
3205               (if mime ?m ? )
3206               (if emphasis ?e ? )))))
3207
3208 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3209
3210 (defun gnus-article-maybe-hide-headers ()
3211   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3212 Provided for backwards compatibility."
3213   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3214                  (not (save-excursion (set-buffer gnus-summary-buffer)
3215                                       gnus-have-all-headers)))
3216              (not gnus-inhibit-hiding))
3217     (gnus-article-hide-headers)))
3218
3219 ;;; Article savers.
3220
3221 (defun gnus-output-to-file (file-name)
3222   "Append the current article to a file named FILE-NAME."
3223   (let ((artbuf (current-buffer)))
3224     (with-temp-buffer
3225       (insert-buffer-substring artbuf)
3226       ;; Append newline at end of the buffer as separator, and then
3227       ;; save it to file.
3228       (goto-char (point-max))
3229       (insert "\n")
3230       (append-to-file (point-min) (point-max) file-name)
3231       t)))
3232
3233 (defun gnus-narrow-to-page (&optional arg)
3234   "Narrow the article buffer to a page.
3235 If given a numerical ARG, move forward ARG pages."
3236   (interactive "P")
3237   (setq arg (if arg (prefix-numeric-value arg) 0))
3238   (save-excursion
3239     (set-buffer gnus-article-buffer)
3240     (goto-char (point-min))
3241     ;; Remove any old next/prev buttons.
3242     (when (gnus-visual-p 'page-marker)
3243       (let ((buffer-read-only nil))
3244         (gnus-remove-text-with-property 'gnus-prev)
3245         (gnus-remove-text-with-property 'gnus-next)))
3246     (when
3247         (cond ((< arg 0)
3248                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3249               ((> arg 0)
3250                (re-search-forward page-delimiter nil 'move arg)))
3251       (goto-char (match-end 0)))
3252     (narrow-to-region
3253      (point)
3254      (if (re-search-forward page-delimiter nil 'move)
3255          (match-beginning 0)
3256        (point)))
3257     (when (and (gnus-visual-p 'page-marker)
3258                (not (= (point-min) 1)))
3259       (save-excursion
3260         (goto-char (point-min))
3261         (gnus-insert-prev-page-button)))
3262     (when (and (gnus-visual-p 'page-marker)
3263                (< (+ (point-max) 2) (buffer-size)))
3264       (save-excursion
3265         (goto-char (point-max))
3266         (gnus-insert-next-page-button)))))
3267
3268 ;; Article mode commands
3269
3270 (defun gnus-article-goto-next-page ()
3271   "Show the next page of the article."
3272   (interactive)
3273   (when (gnus-article-next-page)
3274     (goto-char (point-min))
3275     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3276
3277 (defun gnus-article-goto-prev-page ()
3278   "Show the next page of the article."
3279   (interactive)
3280   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3281     (gnus-article-prev-page nil)))
3282
3283 (defun gnus-article-next-page (&optional lines)
3284   "Show the next page of the current article.
3285 If end of article, return non-nil.  Otherwise return nil.
3286 Argument LINES specifies lines to be scrolled up."
3287   (interactive "p")
3288   (move-to-window-line -1)
3289   (if (save-excursion
3290         (end-of-line)
3291         (and (pos-visible-in-window-p)  ;Not continuation line.
3292              (eobp)))
3293       ;; Nothing in this page.
3294       (if (or (not gnus-page-broken)
3295               (save-excursion
3296                 (save-restriction
3297                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3298           t                             ;Nothing more.
3299         (gnus-narrow-to-page 1)         ;Go to next page.
3300         nil)
3301     ;; More in this page.
3302     (let ((scroll-in-place nil))
3303       (condition-case ()
3304           (scroll-up lines)
3305         (end-of-buffer
3306          ;; Long lines may cause an end-of-buffer error.
3307          (goto-char (point-max)))))
3308     (move-to-window-line 0)
3309     nil))
3310
3311 (defun gnus-article-prev-page (&optional lines)
3312   "Show previous page of current article.
3313 Argument LINES specifies lines to be scrolled down."
3314   (interactive "p")
3315   (move-to-window-line 0)
3316   (if (and gnus-page-broken
3317            (bobp)
3318            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3319       (progn
3320         (gnus-narrow-to-page -1)        ;Go to previous page.
3321         (goto-char (point-max))
3322         (recenter -1))
3323     (let ((scroll-in-place nil))
3324       (prog1
3325           (condition-case ()
3326               (scroll-down lines)
3327             (beginning-of-buffer
3328              (goto-char (point-min))))
3329         (move-to-window-line 0)))))
3330
3331 (defun gnus-article-refer-article ()
3332   "Read article specified by message-id around point."
3333   (interactive)
3334   (let ((point (point)))
3335     (search-forward ">" nil t)          ;Move point to end of "<....>".
3336     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3337         (let ((message-id (match-string 1)))
3338           (goto-char point)
3339           (set-buffer gnus-summary-buffer)
3340           (gnus-summary-refer-article message-id))
3341       (goto-char (point))
3342       (error "No references around point"))))
3343
3344 (defun gnus-article-show-summary ()
3345   "Reconfigure windows to show summary buffer."
3346   (interactive)
3347   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3348       (error "There is no summary buffer for this article buffer")
3349     (gnus-article-set-globals)
3350     (gnus-configure-windows 'article)
3351     (gnus-summary-goto-subject gnus-current-article)
3352     (gnus-summary-position-point)))
3353
3354 (defun gnus-article-describe-briefly ()
3355   "Describe article mode commands briefly."
3356   (interactive)
3357   (gnus-message 6 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page   \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
3358
3359 (defun gnus-article-summary-command ()
3360   "Execute the last keystroke in the summary buffer."
3361   (interactive)
3362   (let ((obuf (current-buffer))
3363         (owin (current-window-configuration))
3364         func)
3365     (switch-to-buffer gnus-article-current-summary 'norecord)
3366     (setq func (lookup-key (current-local-map) (this-command-keys)))
3367     (call-interactively func)
3368     (set-buffer obuf)
3369     (set-window-configuration owin)
3370     (set-window-point (get-buffer-window (current-buffer)) (point))))
3371
3372 (defun gnus-article-summary-command-nosave ()
3373   "Execute the last keystroke in the summary buffer."
3374   (interactive)
3375   (let (func)
3376     (pop-to-buffer gnus-article-current-summary 'norecord)
3377     (setq func (lookup-key (current-local-map) (this-command-keys)))
3378     (call-interactively func)))
3379
3380 (defun gnus-article-check-buffer ()
3381   "Beep if not in an article buffer."
3382   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
3383     (error "Command invoked outside of a Gnus article buffer")))
3384
3385 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3386   "Read a summary buffer key sequence and execute it from the article buffer."
3387   (interactive "P")
3388   (gnus-article-check-buffer)
3389   (let ((nosaves
3390          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3391            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3392            "=" "^" "\M-^" "|"))
3393         (nosave-but-article
3394          '("A\r"))
3395         (nosave-in-article
3396          '("\C-d"))
3397         (up-to-top
3398          '("n" "Gn" "p" "Gp"))
3399         keys new-sum-point)
3400     (save-excursion
3401       (set-buffer gnus-article-current-summary)
3402       (let (gnus-pick-mode)
3403         (push (or key last-command-event) unread-command-events)
3404         (setq keys (read-key-sequence nil))))
3405     (message "")
3406
3407     (if (or (member keys nosaves)
3408             (member keys nosave-but-article)
3409             (member keys nosave-in-article))
3410         (let (func)
3411           (save-window-excursion
3412             (pop-to-buffer gnus-article-current-summary 'norecord)
3413             ;; We disable the pick minor mode commands.
3414             (let (gnus-pick-mode)
3415               (setq func (lookup-key (current-local-map) keys))))
3416           (if (not func)
3417               (ding)
3418             (unless (member keys nosave-in-article)
3419               (set-buffer gnus-article-current-summary))
3420             (call-interactively func)
3421             (setq new-sum-point (point)))
3422           (when (member keys nosave-but-article)
3423             (pop-to-buffer gnus-article-buffer 'norecord)))
3424       ;; These commands should restore window configuration.
3425       (let ((obuf (current-buffer))
3426             (owin (current-window-configuration))
3427             (opoint (point))
3428             (summary gnus-article-current-summary)
3429             func in-buffer selected)
3430         (if not-restore-window
3431             (pop-to-buffer summary 'norecord)
3432           (switch-to-buffer summary 'norecord))
3433         (setq in-buffer (current-buffer))
3434         ;; We disable the pick minor mode commands.
3435         (if (setq func (let (gnus-pick-mode)
3436                          (lookup-key (current-local-map) keys)))
3437             (progn
3438               (call-interactively func)
3439               (setq new-sum-point (point)))
3440           (ding))
3441         (when (eq in-buffer (current-buffer))
3442           (setq selected (gnus-summary-select-article))
3443           (set-buffer obuf)
3444           (unless not-restore-window
3445             (set-window-configuration owin))
3446           (when (eq selected 'old)
3447             (article-goto-body)
3448             (set-window-start (get-buffer-window (current-buffer))
3449                               1)
3450             (set-window-point (get-buffer-window (current-buffer))
3451                               (point)))
3452           (let ((win (get-buffer-window gnus-article-current-summary)))
3453             (when win
3454               (set-window-point win new-sum-point))))))))
3455
3456 (defun gnus-article-hide (&optional arg force)
3457   "Hide all the gruft in the current article.
3458 This means that PGP stuff, signatures, cited text and (some)
3459 headers will be hidden.
3460 If given a prefix, show the hidden text instead."
3461   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3462   (gnus-article-hide-headers arg)
3463   (gnus-article-hide-pgp arg)
3464   (gnus-article-hide-citation-maybe arg force)
3465   (gnus-article-hide-signature arg))
3466
3467 (defun gnus-article-maybe-highlight ()
3468   "Do some article highlighting if article highlighting is requested."
3469   (when (gnus-visual-p 'article-highlight 'highlight)
3470     (gnus-article-highlight-some)))
3471
3472 (defun gnus-check-group-server ()
3473   ;; Make sure the connection to the server is alive.
3474   (unless (gnus-server-opened
3475            (gnus-find-method-for-group gnus-newsgroup-name))
3476     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3477     (gnus-request-group gnus-newsgroup-name t)))
3478
3479 (defun gnus-request-article-this-buffer (article group)
3480   "Get an article and insert it into this buffer."
3481   (let (do-update-line sparse-header)
3482     (prog1
3483         (save-excursion
3484           (erase-buffer)
3485           (gnus-kill-all-overlays)
3486           (setq group (or group gnus-newsgroup-name))
3487
3488           ;; Using `gnus-request-article' directly will insert the article into
3489           ;; `nntp-server-buffer' - so we'll save some time by not having to
3490           ;; copy it from the server buffer into the article buffer.
3491
3492           ;; We only request an article by message-id when we do not have the
3493           ;; headers for it, so we'll have to get those.
3494           (when (stringp article)
3495             (let ((gnus-override-method gnus-refer-article-method))
3496               (gnus-read-header article)))
3497
3498           ;; If the article number is negative, that means that this article
3499           ;; doesn't belong in this newsgroup (possibly), so we find its
3500           ;; message-id and request it by id instead of number.
3501           (when (and (numberp article)
3502                      gnus-summary-buffer
3503                      (get-buffer gnus-summary-buffer)
3504                      (gnus-buffer-exists-p gnus-summary-buffer))
3505             (save-excursion
3506               (set-buffer gnus-summary-buffer)
3507               (let ((header (gnus-summary-article-header article)))
3508                 (when (< article 0)
3509                   (cond
3510                    ((memq article gnus-newsgroup-sparse)
3511                     ;; This is a sparse gap article.
3512                     (setq do-update-line article)
3513                     (setq article (mail-header-id header))
3514                     (let ((gnus-override-method gnus-refer-article-method))
3515                       (setq sparse-header (gnus-read-header article)))
3516                     (setq gnus-newsgroup-sparse
3517                           (delq article gnus-newsgroup-sparse)))
3518                    ((vectorp header)
3519                     ;; It's a real article.
3520                     (setq article (mail-header-id header)))
3521                    (t
3522                     ;; It is an extracted pseudo-article.
3523                     (setq article 'pseudo)
3524                     (gnus-request-pseudo-article header))))
3525
3526                 (let ((method (gnus-find-method-for-group
3527                                gnus-newsgroup-name)))
3528                   (when (and (eq (car method) 'nneething)
3529                              (vectorp header))
3530                     (let ((dir (concat
3531                                 (file-name-as-directory
3532                                  (or (cadr (assq 'nneething-address method))
3533                                      (nth 1 method)))
3534                                 (mail-header-subject header))))
3535                       (when (file-directory-p dir)
3536                         (setq article 'nneething)
3537                         (gnus-group-enter-directory dir))))))))
3538
3539           (cond
3540            ;; Refuse to select canceled articles.
3541            ((and (numberp article)
3542                  gnus-summary-buffer
3543                  (get-buffer gnus-summary-buffer)
3544                  (gnus-buffer-exists-p gnus-summary-buffer)
3545                  (eq (cdr (save-excursion
3546                             (set-buffer gnus-summary-buffer)
3547                             (assq article gnus-newsgroup-reads)))
3548                      gnus-canceled-mark))
3549             nil)
3550            ;; Check the backlog.
3551            ((and gnus-keep-backlog
3552                  (gnus-backlog-request-article group article (current-buffer)))
3553             'article)
3554            ;; Check asynchronous pre-fetch.
3555            ((gnus-async-request-fetched-article group article (current-buffer))
3556             (gnus-async-prefetch-next group article gnus-summary-buffer)
3557             (when (and (numberp article) gnus-keep-backlog)
3558               (gnus-backlog-enter-article group article (current-buffer)))
3559             'article)
3560            ;; Check the cache.
3561            ((and gnus-use-cache
3562                  (numberp article)
3563                  (gnus-cache-request-article article group))
3564             'article)
3565            ;; Get the article and put into the article buffer.
3566            ((or (stringp article) (numberp article))
3567             (let ((gnus-override-method
3568                    (and (stringp article) gnus-refer-article-method))
3569                   (buffer-read-only nil))
3570               (erase-buffer)
3571               (gnus-kill-all-overlays)
3572               (gnus-check-group-server)
3573               (when (gnus-request-article article group (current-buffer))
3574                 (when (numberp article)
3575                   (gnus-async-prefetch-next group article gnus-summary-buffer)
3576                   (when gnus-keep-backlog
3577                     (gnus-backlog-enter-article
3578                      group article (current-buffer))))
3579                 'article)))
3580            ;; It was a pseudo.
3581            (t article)))
3582
3583       ;; Associate this article with the current summary buffer.
3584       (setq gnus-article-current-summary gnus-summary-buffer)
3585
3586       ;; Take the article from the original article buffer
3587       ;; and place it in the buffer it's supposed to be in.
3588       (when (and (get-buffer gnus-article-buffer)
3589                  (equal (buffer-name (current-buffer))
3590                         (buffer-name (get-buffer gnus-article-buffer))))
3591         (save-excursion
3592           (if (get-buffer gnus-original-article-buffer)
3593               (set-buffer gnus-original-article-buffer)
3594             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3595             (buffer-disable-undo)
3596             (setq major-mode 'gnus-original-article-mode)
3597             (setq buffer-read-only t))
3598           (let (buffer-read-only)
3599             (erase-buffer)
3600             (insert-buffer-substring gnus-article-buffer))
3601           (setq gnus-original-article (cons group article)))
3602
3603         ;; Decode charsets.
3604         (run-hooks 'gnus-article-decode-hook)
3605         ;; Mark article as decoded or not.
3606         (setq gnus-article-decoded-p gnus-article-decode-hook))
3607
3608       ;; Update sparse articles.
3609       (when (and do-update-line
3610                  (or (numberp article)
3611                      (stringp article)))
3612         (let ((buf (current-buffer)))
3613           (set-buffer gnus-summary-buffer)
3614           (gnus-summary-update-article do-update-line sparse-header)
3615           (gnus-summary-goto-subject do-update-line nil t)
3616           (set-window-point (get-buffer-window (current-buffer) t)
3617                             (point))
3618           (set-buffer buf))))))
3619
3620 ;;;
3621 ;;; Article editing
3622 ;;;
3623
3624 (defcustom gnus-article-edit-mode-hook nil
3625   "Hook run in article edit mode buffers."
3626   :group 'gnus-article-various
3627   :type 'hook)
3628
3629 (defcustom gnus-article-edit-article-setup-function
3630   'gnus-article-mime-edit-article-setup
3631   "Function called to setup an editing article buffer."
3632   :group 'gnus-article-various
3633   :type 'function)
3634
3635 (defvar gnus-article-edit-done-function nil)
3636
3637 (defvar gnus-article-edit-mode-map nil)
3638
3639 ;; Should we be using derived.el for this?
3640 (unless gnus-article-edit-mode-map
3641   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3642   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3643
3644   (gnus-define-keys gnus-article-edit-mode-map
3645     "\C-c\C-c" gnus-article-edit-done
3646     "\C-c\C-k" gnus-article-edit-exit)
3647
3648   (gnus-define-keys (gnus-article-edit-wash-map
3649                      "\C-c\C-w" gnus-article-edit-mode-map)
3650     "f" gnus-article-edit-full-stops))
3651
3652 (defun gnus-article-edit-mode ()
3653   "Major mode for editing articles.
3654 This is an extended text-mode.
3655
3656 \\{gnus-article-edit-mode-map}"
3657   (interactive)
3658   (setq major-mode 'gnus-article-edit-mode)
3659   (setq mode-name "Article Edit")
3660   (use-local-map gnus-article-edit-mode-map)
3661   (make-local-variable 'gnus-article-edit-done-function)
3662   (make-local-variable 'gnus-prev-winconf)
3663   (setq buffer-read-only nil)
3664   (buffer-enable-undo)
3665   (widen)
3666   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3667
3668 (defun gnus-article-edit (&optional force)
3669   "Edit the current article.
3670 This will have permanent effect only in mail groups.
3671 If FORCE is non-nil, allow editing of articles even in read-only
3672 groups."
3673   (interactive "P")
3674   (when (and (not force)
3675              (gnus-group-read-only-p))
3676     (error "The current newsgroup does not support article editing"))
3677   (gnus-article-date-original)
3678   (gnus-article-edit-article
3679    'ignore
3680    `(lambda (no-highlight)
3681       'ignore
3682       (gnus-summary-edit-article-done
3683        ,(or (mail-header-references gnus-current-headers) "")
3684        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3685
3686 (defun gnus-article-edit-article (start-func exit-func)
3687   "Start editing the contents of the current article buffer."
3688   (let ((winconf (current-window-configuration)))
3689     (set-buffer gnus-article-buffer)
3690     (gnus-article-edit-mode)
3691     (funcall start-func)
3692     ;;(gnus-article-delete-text-of-type 'annotation)
3693     ;;(gnus-set-text-properties (point-min) (point-max) nil)
3694     (gnus-configure-windows 'edit-article)
3695     (setq gnus-article-edit-done-function exit-func)
3696     (setq gnus-prev-winconf winconf)
3697     (when gnus-article-edit-article-setup-function
3698       (funcall gnus-article-edit-article-setup-function))
3699     (gnus-message 6 "C-c C-c to end edits")))
3700
3701 (defun gnus-article-edit-done (&optional arg)
3702   "Update the article edits and exit."
3703   (interactive "P")
3704   (save-excursion
3705     (save-restriction
3706       (widen)
3707       (when (article-goto-body)
3708         (let ((lines (count-lines (point) (point-max)))
3709               (length (- (point-max) (point)))
3710               (case-fold-search t)
3711               (body (copy-marker (point))))
3712           (goto-char (point-min))
3713           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3714             (delete-region (match-beginning 1) (match-end 1))
3715             (insert (number-to-string length)))
3716           (goto-char (point-min))
3717           (when (re-search-forward
3718                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3719             (delete-region (match-beginning 1) (match-end 1))
3720             (insert (number-to-string length)))
3721           (goto-char (point-min))
3722           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3723             (delete-region (match-beginning 1) (match-end 1))
3724             (insert (number-to-string lines)))))))
3725   (let ((func gnus-article-edit-done-function)
3726         (buf (current-buffer))
3727         (start (window-start)))
3728     (remove-hook 'gnus-article-mode-hook
3729                  'gnus-article-mime-edit-article-unwind)
3730     (gnus-article-edit-exit)
3731     (save-excursion
3732       (set-buffer buf)
3733       (let ((buffer-read-only nil))
3734         (funcall func arg))
3735       ;; The cache and backlog have to be flushed somewhat.
3736       (when gnus-keep-backlog
3737         (gnus-backlog-remove-article
3738          (car gnus-article-current) (cdr gnus-article-current)))
3739       ;; Flush original article as well.
3740       (save-excursion
3741         (when (get-buffer gnus-original-article-buffer)
3742           (set-buffer gnus-original-article-buffer)
3743           (setq gnus-original-article nil)))
3744       (when gnus-use-cache
3745         (gnus-cache-update-article
3746          (car gnus-article-current) (cdr gnus-article-current))))
3747     (set-buffer buf)
3748     (set-window-start (get-buffer-window buf) start)
3749     (set-window-point (get-buffer-window buf) (point))))
3750
3751 (defun gnus-article-edit-exit ()
3752   "Exit the article editing without updating."
3753   (interactive)
3754   ;; We remove all text props from the article buffer.
3755   (let ((buf (format "%s" (buffer-string)))
3756         (curbuf (current-buffer))
3757         (p (point))
3758         (window-start (window-start)))
3759     (erase-buffer)
3760     (insert buf)
3761     (let ((winconf gnus-prev-winconf))
3762       (gnus-article-mode)
3763       (set-window-configuration winconf)
3764       ;; Tippy-toe some to make sure that point remains where it was.
3765       (save-current-buffer
3766         (set-buffer curbuf)
3767         (set-window-start (get-buffer-window (current-buffer)) window-start)
3768         (goto-char p)))))
3769
3770 (defun gnus-article-edit-full-stops ()
3771   "Interactively repair spacing at end of sentences."
3772   (interactive)
3773   (save-excursion
3774     (goto-char (point-min))
3775     (search-forward-regexp "^$" nil t)
3776     (let ((case-fold-search nil))
3777       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3778
3779 ;;;
3780 ;;; Article editing with MIME-Edit
3781 ;;;
3782
3783 (defcustom gnus-article-mime-edit-article-setup-hook nil
3784   "Hook run after setting up a MIME editing article buffer."
3785   :group 'gnus-article-various
3786   :type 'hook)
3787
3788 (defun gnus-article-mime-edit-article-unwind ()
3789   "Unwind `gnus-article-buffer' if article editing was given up."
3790   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
3791   (when mime-edit-mode-flag
3792     (mime-edit-exit 'nomime 'no-error)
3793     (message ""))
3794   (when (featurep 'font-lock)
3795     (setq font-lock-defaults nil)
3796     (font-lock-mode 0)))
3797
3798 (defun gnus-article-mime-edit-article-setup ()
3799   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
3800 after replacing with the original article."
3801   (setq gnus-show-mime t)
3802   (setq gnus-article-edit-done-function
3803         `(lambda (&rest args)
3804            (when mime-edit-mode-flag
3805              (mime-edit-exit)
3806              (message ""))
3807            (goto-char (point-min))
3808            (let (case-fold-search)
3809              (when (re-search-forward
3810                     (format "^%s$" (regexp-quote mail-header-separator))
3811                     nil t)
3812                (replace-match "")))
3813            (when (featurep 'font-lock)
3814              (setq font-lock-defaults nil)
3815              (font-lock-mode 0))
3816            (apply ,gnus-article-edit-done-function args)
3817            (set-buffer gnus-original-article-buffer)
3818            (erase-buffer)
3819            (insert-buffer gnus-article-buffer)
3820            (setq gnus-current-headers (gnus-article-make-full-mail-header))
3821            (gnus-article-prepare-display)))
3822   (substitute-key-definition
3823    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
3824    gnus-article-edit-mode-map)
3825   (erase-buffer)
3826   (insert-buffer gnus-original-article-buffer)
3827   (mime-edit-again)
3828   (when (featurep 'font-lock)
3829     (set (make-local-variable 'font-lock-defaults)
3830          '(message-font-lock-keywords t))
3831     (font-lock-set-defaults)
3832     (turn-on-font-lock))
3833   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
3834   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
3835
3836 (defun gnus-article-mime-edit-exit ()
3837   "Exit the article MIME editing without updating."
3838   (interactive)
3839   (let ((winconf gnus-prev-winconf)
3840         buf)
3841     (when mime-edit-mode-flag
3842       (mime-edit-exit)
3843       (message ""))
3844     (goto-char (point-min))
3845     (let (case-fold-search)
3846       (when (re-search-forward
3847              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
3848         (replace-match "")))
3849     (when (featurep 'font-lock)
3850       (setq font-lock-defaults nil)
3851       (font-lock-mode 0))
3852     ;; We remove all text props from the article buffer.
3853     (setq buf (format "%s" (buffer-string)))
3854     (set-buffer (get-buffer-create gnus-original-article-buffer))
3855     (erase-buffer)
3856     (insert buf)
3857     (setq gnus-current-headers (gnus-article-make-full-mail-header))
3858     (gnus-article-prepare-display)
3859     (set-window-configuration winconf)))
3860
3861 ;;;
3862 ;;; Article highlights
3863 ;;;
3864
3865 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
3866
3867 ;;; Internal Variables:
3868
3869 (defcustom gnus-button-url-regexp "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)"
3870   "Regular expression that matches URLs."
3871   :group 'gnus-article-buttons
3872   :type 'regexp)
3873
3874 (defcustom gnus-button-alist
3875   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
3876      0 t gnus-button-message-id 2)
3877     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
3878     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
3879      1 t
3880      gnus-button-fetch-group 4)
3881     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
3882     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
3883      t gnus-button-message-id 3)
3884     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
3885     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
3886     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
3887     ;; This is how URLs _should_ be embedded in text...
3888     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
3889     ;; Raw URLs.
3890     (,gnus-button-url-regexp 0 t gnus-button-url 0))
3891   "*Alist of regexps matching buttons in article bodies.
3892
3893 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
3894 REGEXP: is the string matching text around the button,
3895 BUTTON: is the number of the regexp grouping actually matching the button,
3896 FORM: is a lisp expression which must eval to true for the button to
3897 be added,
3898 CALLBACK: is the function to call when the user push this button, and each
3899 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
3900
3901 CALLBACK can also be a variable, in that case the value of that
3902 variable it the real callback function."
3903   :group 'gnus-article-buttons
3904   :type '(repeat (list regexp
3905                        (integer :tag "Button")
3906                        (sexp :tag "Form")
3907                        (function :tag "Callback")
3908                        (repeat :tag "Par"
3909                                :inline t
3910                                (integer :tag "Regexp group")))))
3911
3912 (defcustom gnus-header-button-alist
3913   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
3914      0 t gnus-button-message-id 0)
3915     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
3916     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
3917      0 t gnus-button-mailto 0)
3918     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3919     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3920     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3921     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
3922      gnus-button-message-id 3))
3923   "*Alist of headers and regexps to match buttons in article heads.
3924
3925 This alist is very similar to `gnus-button-alist', except that each
3926 alist has an additional HEADER element first in each entry:
3927
3928 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
3929
3930 HEADER is a regexp to match a header.  For a fuller explanation, see
3931 `gnus-button-alist'."
3932   :group 'gnus-article-buttons
3933   :group 'gnus-article-headers
3934   :type '(repeat (list (regexp :tag "Header")
3935                        regexp
3936                        (integer :tag "Button")
3937                        (sexp :tag "Form")
3938                        (function :tag "Callback")
3939                        (repeat :tag "Par"
3940                                :inline t
3941                                (integer :tag "Regexp group")))))
3942
3943 (defvar gnus-button-regexp nil)
3944 (defvar gnus-button-marker-list nil)
3945 ;; Regexp matching any of the regexps from `gnus-button-alist'.
3946
3947 (defvar gnus-button-last nil)
3948 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
3949
3950 ;;; Commands:
3951
3952 (defun gnus-article-push-button (event)
3953   "Check text under the mouse pointer for a callback function.
3954 If the text under the mouse pointer has a `gnus-callback' property,
3955 call it with the value of the `gnus-data' text property."
3956   (interactive "e")
3957   (set-buffer (window-buffer (posn-window (event-start event))))
3958   (let* ((pos (posn-point (event-start event)))
3959          (data (get-text-property pos 'gnus-data))
3960          (fun (get-text-property pos 'gnus-callback)))
3961     (goto-char pos)
3962     (when fun
3963       (funcall fun data))))
3964
3965 (defun gnus-article-press-button ()
3966   "Check text at point for a callback function.
3967 If the text at point has a `gnus-callback' property,
3968 call it with the value of the `gnus-data' text property."
3969   (interactive)
3970   (let* ((data (get-text-property (point) 'gnus-data))
3971          (fun (get-text-property (point) 'gnus-callback)))
3972     (when fun
3973       (funcall fun data))))
3974
3975 (defun gnus-article-prev-button (n)
3976   "Move point to N buttons backward.
3977 If N is negative, move forward instead."
3978   (interactive "p")
3979   (gnus-article-next-button (- n)))
3980
3981 (defun gnus-article-next-button (n)
3982   "Move point to N buttons forward.
3983 If N is negative, move backward instead."
3984   (interactive "p")
3985   (let ((function (if (< n 0) 'previous-single-property-change
3986                     'next-single-property-change))
3987         (inhibit-point-motion-hooks t)
3988         (backward (< n 0))
3989         (limit (if (< n 0) (point-min) (point-max))))
3990     (setq n (abs n))
3991     (while (and (not (= limit (point)))
3992                 (> n 0))
3993       ;; Skip past the current button.
3994       (when (get-text-property (point) 'gnus-callback)
3995         (goto-char (funcall function (point) 'gnus-callback nil limit)))
3996       ;; Go to the next (or previous) button.
3997       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
3998       ;; Put point at the start of the button.
3999       (when (and backward (not (get-text-property (point) 'gnus-callback)))
4000         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4001       ;; Skip past intangible buttons.
4002       (when (get-text-property (point) 'intangible)
4003         (incf n))
4004       (decf n))
4005     (unless (zerop n)
4006       (gnus-message 5 "No more buttons"))
4007     n))
4008
4009 (defun gnus-article-highlight (&optional force)
4010   "Highlight current article.
4011 This function calls `gnus-article-highlight-headers',
4012 `gnus-article-highlight-citation',
4013 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4014 do the highlighting.  See the documentation for those functions."
4015   (interactive (list 'force))
4016   (gnus-article-highlight-headers)
4017   (gnus-article-highlight-citation force)
4018   (gnus-article-highlight-signature)
4019   (gnus-article-add-buttons force)
4020   (gnus-article-add-buttons-to-head))
4021
4022 (defun gnus-article-highlight-some (&optional force)
4023   "Highlight current article.
4024 This function calls `gnus-article-highlight-headers',
4025 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4026 do the highlighting.  See the documentation for those functions."
4027   (interactive (list 'force))
4028   (gnus-article-highlight-headers)
4029   (gnus-article-highlight-signature)
4030   (gnus-article-add-buttons))
4031
4032 (defun gnus-article-highlight-headers ()
4033   "Highlight article headers as specified by `gnus-header-face-alist'."
4034   (interactive)
4035   (save-excursion
4036     (set-buffer gnus-article-buffer)
4037     (save-restriction
4038       (let ((alist gnus-header-face-alist)
4039             (buffer-read-only nil)
4040             (case-fold-search t)
4041             (inhibit-point-motion-hooks t)
4042             entry regexp header-face field-face from hpoints fpoints)
4043         (article-narrow-to-head)
4044         (while (setq entry (pop alist))
4045           (goto-char (point-min))
4046           (setq regexp (concat "^\\("
4047                                (if (string-equal "" (nth 0 entry))
4048                                    "[^\t ]"
4049                                  (nth 0 entry))
4050                                "\\)")
4051                 header-face (nth 1 entry)
4052                 field-face (nth 2 entry))
4053           (while (and (re-search-forward regexp nil t)
4054                       (not (eobp)))
4055             (beginning-of-line)
4056             (setq from (point))
4057             (unless (search-forward ":" nil t)
4058               (forward-char 1))
4059             (when (and header-face
4060                        (not (memq (point) hpoints)))
4061               (push (point) hpoints)
4062               (gnus-put-text-property from (point) 'face header-face))
4063             (when (and field-face
4064                        (not (memq (setq from (point)) fpoints)))
4065               (push from fpoints)
4066               (if (re-search-forward "^[^ \t]" nil t)
4067                   (forward-char -2)
4068                 (goto-char (point-max)))
4069               (gnus-put-text-property from (point) 'face field-face))))))))
4070
4071 (defun gnus-article-highlight-signature ()
4072   "Highlight the signature in an article.
4073 It does this by highlighting everything after
4074 `gnus-signature-separator' using `gnus-signature-face'."
4075   (interactive)
4076   (save-excursion
4077     (set-buffer gnus-article-buffer)
4078     (let ((buffer-read-only nil)
4079           (inhibit-point-motion-hooks t))
4080       (save-restriction
4081         (when (and gnus-signature-face
4082                    (gnus-article-narrow-to-signature))
4083           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4084                             'face gnus-signature-face)
4085           (widen)
4086           (gnus-article-search-signature)
4087           (let ((start (match-beginning 0))
4088                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4089             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4090                                      end)))))))
4091
4092 (defun gnus-button-in-region-p (b e prop)
4093   "Say whether PROP exists in the region."
4094   (text-property-not-all b e prop nil))
4095
4096 (defun gnus-article-add-buttons (&optional force)
4097   "Find external references in the article and make buttons of them.
4098 \"External references\" are things like Message-IDs and URLs, as
4099 specified by `gnus-button-alist'."
4100   (interactive (list 'force))
4101   (save-excursion
4102     (set-buffer gnus-article-buffer)
4103     (let ((buffer-read-only nil)
4104           (inhibit-point-motion-hooks t)
4105           (case-fold-search t)
4106           (alist gnus-button-alist)
4107           beg entry regexp)
4108       ;; Remove all old markers.
4109       (let (marker entry)
4110         (while (setq marker (pop gnus-button-marker-list))
4111           (goto-char marker)
4112           (when (setq entry (gnus-button-entry))
4113             (put-text-property (match-beginning (nth 1 entry))
4114                                (match-end (nth 1 entry))
4115                                'gnus-callback nil))
4116           (set-marker marker nil)))
4117       ;; We skip the headers.
4118       (article-goto-body)
4119       (setq beg (point))
4120       (while (setq entry (pop alist))
4121         (setq regexp (car entry))
4122         (goto-char beg)
4123         (while (re-search-forward regexp nil t)
4124           (let* ((start (and entry (match-beginning (nth 1 entry))))
4125                  (end (and entry (match-end (nth 1 entry))))
4126                  (from (match-beginning 0)))
4127             (when (and (or (eq t (nth 2 entry))
4128                            (eval (nth 2 entry)))
4129                        (not (gnus-button-in-region-p
4130                              start end 'gnus-callback)))
4131               ;; That optional form returned non-nil, so we add the
4132               ;; button.
4133               (gnus-article-add-button
4134                start end 'gnus-button-push
4135                (car (push (set-marker (make-marker) from)
4136                           gnus-button-marker-list))))))))))
4137
4138 ;; Add buttons to the head of an article.
4139 (defun gnus-article-add-buttons-to-head ()
4140   "Add buttons to the head of the article."
4141   (interactive)
4142   (save-excursion
4143     (set-buffer gnus-article-buffer)
4144     (save-restriction
4145       (let ((buffer-read-only nil)
4146             (inhibit-point-motion-hooks t)
4147             (case-fold-search t)
4148             (alist gnus-header-button-alist)
4149             entry beg end)
4150         (article-narrow-to-head)
4151         (while alist
4152           ;; Each alist entry.
4153           (setq entry (car alist)
4154                 alist (cdr alist))
4155           (goto-char (point-min))
4156           (while (re-search-forward (car entry) nil t)
4157             ;; Each header matching the entry.
4158             (setq beg (match-beginning 0))
4159             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4160                                (match-beginning 0))
4161                           (point-max)))
4162             (goto-char beg)
4163             (while (re-search-forward (nth 1 entry) end t)
4164               ;; Each match within a header.
4165               (let* ((entry (cdr entry))
4166                      (start (match-beginning (nth 1 entry)))
4167                      (end (match-end (nth 1 entry)))
4168                      (form (nth 2 entry)))
4169                 (goto-char (match-end 0))
4170                 (when (eval form)
4171                   (gnus-article-add-button
4172                    start end (nth 3 entry)
4173                    (buffer-substring (match-beginning (nth 4 entry))
4174                                      (match-end (nth 4 entry)))))))
4175             (goto-char end)))))))
4176
4177 ;;; External functions:
4178
4179 (defun gnus-article-add-button (from to fun &optional data)
4180   "Create a button between FROM and TO with callback FUN and data DATA."
4181   (when gnus-article-button-face
4182     (gnus-overlay-put (gnus-make-overlay from to)
4183                       'face gnus-article-button-face))
4184   (gnus-add-text-properties
4185    from to
4186    (nconc (and gnus-article-mouse-face
4187                (list gnus-mouse-face-prop gnus-article-mouse-face))
4188           (list 'gnus-callback fun)
4189           (and data (list 'gnus-data data)))))
4190
4191 ;;; Internal functions:
4192
4193 (defun gnus-article-set-globals ()
4194   (save-excursion
4195     (set-buffer gnus-summary-buffer)
4196     (gnus-set-global-variables)))
4197
4198 (defun gnus-signature-toggle (end)
4199   (save-excursion
4200     (set-buffer gnus-article-buffer)
4201     (let ((buffer-read-only nil)
4202           (inhibit-point-motion-hooks t))
4203       (if (get-text-property end 'invisible)
4204           (gnus-article-unhide-text end (point-max))
4205         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4206
4207 (defun gnus-button-entry ()
4208   ;; Return the first entry in `gnus-button-alist' matching this place.
4209   (let ((alist gnus-button-alist)
4210         (entry nil))
4211     (while alist
4212       (setq entry (pop alist))
4213       (if (looking-at (car entry))
4214           (setq alist nil)
4215         (setq entry nil)))
4216     entry))
4217
4218 (defun gnus-button-push (marker)
4219   ;; Push button starting at MARKER.
4220   (save-excursion
4221     (goto-char marker)
4222     (let* ((entry (gnus-button-entry))
4223            (inhibit-point-motion-hooks t)
4224            (fun (nth 3 entry))
4225            (args (mapcar (lambda (group)
4226                            (let ((string (match-string group)))
4227                              (gnus-set-text-properties
4228                               0 (length string) nil string)
4229                              string))
4230                          (nthcdr 4 entry))))
4231       (cond
4232        ((fboundp fun)
4233         (apply fun args))
4234        ((and (boundp fun)
4235              (fboundp (symbol-value fun)))
4236         (apply (symbol-value fun) args))
4237        (t
4238         (gnus-message 1 "You must define `%S' to use this button"
4239                       (cons fun args)))))))
4240
4241 (defun gnus-button-message-id (message-id)
4242   "Fetch MESSAGE-ID."
4243   (save-excursion
4244     (set-buffer gnus-summary-buffer)
4245     (gnus-summary-refer-article message-id)))
4246
4247 (defun gnus-button-fetch-group (address)
4248   "Fetch GROUP specified by ADDRESS."
4249   (if (not (string-match "[:/]" address))
4250       ;; This is just a simple group url.
4251       (gnus-group-read-ephemeral-group address gnus-select-method)
4252     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4253                            address))
4254         (error "Can't parse %s" address)
4255       (gnus-group-read-ephemeral-group
4256        (match-string 4 address)
4257        `(nntp ,(match-string 1 address)
4258               (nntp-address ,(match-string 1 address))
4259               (nntp-port-number ,(if (match-end 3)
4260                                      (match-string 3 address)
4261                                    "nntp")))))))
4262
4263 (defun gnus-url-parse-query-string (query &optional downcase)
4264   (let (retval pairs cur key val)
4265     (setq pairs (split-string query "&"))
4266     (while pairs
4267       (setq cur (car pairs)
4268             pairs (cdr pairs))
4269       (if (not (string-match "=" cur))
4270           nil                           ; Grace
4271         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4272               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4273         (if downcase
4274             (setq key (downcase key)))
4275         (setq cur (assoc key retval))
4276         (if cur
4277             (setcdr cur (cons val (cdr cur)))
4278           (setq retval (cons (list key val) retval)))))
4279     retval))
4280
4281 (defun gnus-url-unhex (x)
4282   (if (> x ?9)
4283       (if (>= x ?a)
4284           (+ 10 (- x ?a))
4285         (+ 10 (- x ?A)))
4286     (- x ?0)))
4287
4288 (defun gnus-url-unhex-string (str &optional allow-newlines)
4289   "Remove %XXX embedded spaces, etc in a url.
4290 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4291 decoding of carriage returns and line feeds in the string, which is normally
4292 forbidden in URL encoding."
4293   (setq str (or str ""))
4294   (let ((tmp "")
4295         (case-fold-search t))
4296     (while (string-match "%[0-9a-f][0-9a-f]" str)
4297       (let* ((start (match-beginning 0))
4298              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4299              (code (+ (* 16 ch1)
4300                       (gnus-url-unhex (elt str (+ start 2))))))
4301         (setq tmp (concat
4302                    tmp (substring str 0 start)
4303                    (cond
4304                     (allow-newlines
4305                      (char-to-string code))
4306                     ((or (= code ?\n) (= code ?\r))
4307                      " ")
4308                     (t (char-to-string code))))
4309               str (substring str (match-end 0)))))
4310     (setq tmp (concat tmp str))
4311     tmp))
4312
4313 (defun gnus-url-mailto (url)
4314   ;; Send mail to someone
4315   (when (string-match "mailto:/*\\(.*\\)" url)
4316     (setq url (substring url (match-beginning 1) nil)))
4317   (let (to args subject func)
4318     (if (string-match (regexp-quote "?") url)
4319         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4320               args (gnus-url-parse-query-string
4321                     (substring url (match-end 0) nil) t))
4322       (setq to (gnus-url-unhex-string url)))
4323     (setq args (cons (list "to" to) args)
4324           subject (cdr-safe (assoc "subject" args)))
4325     (gnus-setup-message 'reply
4326       (message-mail)
4327       (while args
4328         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4329         (if (fboundp func)
4330             (funcall func)
4331           (message-position-on-field (caar args)))
4332         (insert (mapconcat 'identity (cdar args) ", "))
4333         (setq args (cdr args)))
4334       (if subject
4335           (message-goto-body)
4336         (message-goto-subject)))))
4337
4338 (defun gnus-button-mailto (address)
4339   ;; Mail to ADDRESS.
4340   (set-buffer (gnus-copy-article-buffer))
4341   (gnus-setup-message 'reply
4342     (message-reply address)))
4343
4344 (defun gnus-button-reply (address)
4345   ;; Reply to ADDRESS.
4346   (gnus-setup-message 'reply
4347     (message-reply address)))
4348
4349 (defun gnus-button-url (address)
4350   "Browse ADDRESS."
4351   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4352   (if (listp browse-url-browser-function)
4353       (browse-url address)
4354     (funcall browse-url-browser-function address)))
4355
4356 (defun gnus-button-embedded-url (address)
4357   "Browse ADDRESS."
4358   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4359   (if (listp browse-url-browser-function)
4360       (browse-url (gnus-strip-whitespace address))
4361     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
4362
4363 ;;; Next/prev buttons in the article buffer.
4364
4365 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4366 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4367
4368 (defvar gnus-prev-page-map nil)
4369 (unless gnus-prev-page-map
4370   (setq gnus-prev-page-map (make-sparse-keymap))
4371   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4372   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4373
4374 (defun gnus-insert-prev-page-button ()
4375   (let ((buffer-read-only nil))
4376     (gnus-eval-format
4377      gnus-prev-page-line-format nil
4378      `(gnus-prev t local-map ,gnus-prev-page-map
4379                  gnus-callback gnus-article-button-prev-page
4380                  article-type annotation))))
4381
4382 (defvar gnus-next-page-map nil)
4383 (unless gnus-next-page-map
4384   (setq gnus-next-page-map (make-keymap))
4385   (suppress-keymap gnus-prev-page-map)
4386   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4387   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4388
4389 (defun gnus-button-next-page ()
4390   "Go to the next page."
4391   (interactive)
4392   (let ((win (selected-window)))
4393     (select-window (get-buffer-window gnus-article-buffer t))
4394     (gnus-article-next-page)
4395     (select-window win)))
4396
4397 (defun gnus-button-prev-page ()
4398   "Go to the prev page."
4399   (interactive)
4400   (let ((win (selected-window)))
4401     (select-window (get-buffer-window gnus-article-buffer t))
4402     (gnus-article-prev-page)
4403     (select-window win)))
4404
4405 (defun gnus-insert-next-page-button ()
4406   (let ((buffer-read-only nil))
4407     (gnus-eval-format gnus-next-page-line-format nil
4408                       `(gnus-next
4409                         t local-map ,gnus-next-page-map
4410                         gnus-callback gnus-article-button-next-page
4411                         article-type annotation))))
4412
4413 (defun gnus-article-button-next-page (arg)
4414   "Go to the next page."
4415   (interactive "P")
4416   (let ((win (selected-window)))
4417     (select-window (get-buffer-window gnus-article-buffer t))
4418     (gnus-article-next-page)
4419     (select-window win)))
4420
4421 (defun gnus-article-button-prev-page (arg)
4422   "Go to the prev page."
4423   (interactive "P")
4424   (let ((win (selected-window)))
4425     (select-window (get-buffer-window gnus-article-buffer t))
4426     (gnus-article-prev-page)
4427     (select-window win)))
4428
4429 (defvar gnus-decode-header-methods
4430   '(mail-decode-encoded-word-region)
4431   "List of methods used to decode headers.
4432
4433 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
4434 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
4435 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4436 whose names match REGEXP.
4437
4438 For example:
4439 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4440  mail-decode-encoded-word-region
4441  (\"chinese\" . rfc1843-decode-region))
4442 ")
4443
4444 (defvar gnus-decode-header-methods-cache nil)
4445
4446 (defun gnus-multi-decode-header (start end)
4447   "Apply the functions from `gnus-encoded-word-methods' that match."
4448   (unless (and gnus-decode-header-methods-cache
4449                (eq gnus-newsgroup-name
4450                    (car gnus-decode-header-methods-cache)))
4451     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4452     (mapc '(lambda (x)
4453              (if (symbolp x)
4454                  (nconc gnus-decode-header-methods-cache (list x))
4455                (if (and gnus-newsgroup-name
4456                         (string-match (car x) gnus-newsgroup-name))
4457                    (nconc gnus-decode-header-methods-cache
4458                           (list (cdr x))))))
4459           gnus-decode-header-methods))
4460   (let ((xlist gnus-decode-header-methods-cache))
4461     (pop xlist)
4462     (save-restriction
4463       (narrow-to-region start end)
4464       (while xlist
4465         (funcall (pop xlist) (point-min) (point-max))))))
4466
4467 ;;;
4468 ;;; Treatment top-level handling.
4469 ;;;
4470
4471 (defun gnus-treat-article (condition &optional part-number total-parts type)
4472   (let ((length (- (point-max) (point-min)))
4473         (alist gnus-treatment-function-alist)
4474         (article-goto-body-goes-to-point-min-p t)
4475         (treated-type
4476          (or (not type)
4477              (catch 'found
4478                (let ((list gnus-article-treat-types))
4479                  (while list
4480                    (when (string-match (pop list) type)
4481                      (throw 'found t)))))))
4482         val elem)
4483     (when (gnus-visual-p 'article-highlight 'highlight)
4484       (gnus-run-hooks 'gnus-part-display-hook)
4485       (while (setq elem (pop alist))
4486         (setq val (symbol-value (car elem)))
4487         (when (and (or (consp val)
4488                        treated-type)
4489                    (gnus-treat-predicate val))
4490           (save-restriction
4491             (funcall (cadr elem))))))))
4492
4493 ;; Dynamic variables.
4494 (defvar part-number)
4495 (defvar total-parts)
4496 (defvar type)
4497 (defvar condition)
4498 (defvar length)
4499 (defun gnus-treat-predicate (val)
4500   (cond
4501    (condition
4502     (eq condition val))
4503    ((null val)
4504     nil)
4505    ((eq val t)
4506     t)
4507    ((eq val 'head)
4508     nil)
4509    ((eq val 'last)
4510     (eq part-number total-parts))
4511    ((numberp val)
4512     (< length val))
4513    ((listp val)
4514     (let ((pred (pop val)))
4515       (cond
4516        ((eq pred 'or)
4517         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4518        ((eq pred 'and)
4519         (apply 'gnus-and (mapcar 'gnus-tread-predicate val)))
4520        ((eq pred 'not)
4521         (not (gnus-treat-predicate val)))
4522        ((eq pred 'typep)
4523         (equal (cadr val) type))
4524        (t
4525         (error "%S is not a valid predicate" pred)))))
4526    (t
4527     (error "%S is not a valid value" val))))
4528
4529
4530 ;;; @ for mime-view
4531 ;;;
4532
4533 (defun gnus-article-header-presentation-method (entity situation)
4534   (mime-insert-header entity)
4535   )
4536
4537 (set-alist 'mime-header-presentation-method-alist
4538            'gnus-original-article-mode
4539            #'gnus-article-header-presentation-method)
4540
4541 (defun gnus-mime-preview-quitting-method ()
4542   (mime-preview-kill-buffer)
4543   (delete-other-windows)
4544   (gnus-article-show-summary)
4545   (gnus-summary-select-article gnus-show-all-headers t))
4546
4547 (set-alist 'mime-preview-quitting-method-alist
4548            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
4549
4550 (defun gnus-following-method (buf)
4551   (set-buffer buf)
4552   (message-followup)
4553   (message-yank-original)
4554   (kill-buffer buf)
4555   (goto-char (point-min))
4556   )
4557
4558 (set-alist 'mime-preview-following-method-alist
4559            'gnus-original-article-mode #'gnus-following-method)
4560
4561 (set-alist 'mime-preview-over-to-previous-method-alist
4562            'gnus-original-article-mode
4563            (lambda ()
4564              (gnus-article-read-summary-keys
4565               nil (gnus-character-to-event ?P))))
4566
4567 (set-alist 'mime-preview-over-to-next-method-alist
4568            'gnus-original-article-mode'
4569            (lambda ()
4570              (gnus-article-read-summary-keys
4571               nil (gnus-character-to-event ?N))))
4572
4573
4574 ;;; @ end
4575 ;;;
4576
4577 (gnus-ems-redefine)
4578
4579 (provide 'gnus-art)
4580
4581 (run-hooks 'gnus-art-load-hook)
4582
4583 ;;; gnus-art.el ends here