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