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