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