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