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