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