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