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