Feeding back from `t-gnus-6_14' into `pgnus-ichikawa'.
[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 (let (keys)
2843   (let ((key 32))
2844     (while (<= key 127)
2845       (push (char-to-string key) keys)
2846       (incf key))
2847     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
2848       (setq key (car elem))
2849       (when (and (> (length key) 0)
2850                  (not (eq 'menu-bar (aref key 0)))
2851                  (symbolp (lookup-key gnus-summary-mode-map key)))
2852         (push key keys))))
2853   (dolist (key keys)
2854     (unless (lookup-key gnus-article-mode-map key)
2855       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
2856
2857 (eval-when-compile 
2858   (defvar gnus-article-commands-menu))
2859
2860 (defun gnus-article-make-menu-bar ()
2861   (gnus-turn-off-edit-menu 'article)
2862   (unless (boundp 'gnus-article-article-menu)
2863     (easy-menu-define
2864      gnus-article-article-menu gnus-article-mode-map ""
2865      '("Article"
2866        ["Scroll forwards" gnus-article-goto-next-page t]
2867        ["Scroll backwards" gnus-article-goto-prev-page t]
2868        ["Show summary" gnus-article-show-summary t]
2869        ["Fetch Message-ID at point" gnus-article-refer-article t]
2870        ["Mail to address at point" gnus-article-mail t]
2871        ["Send a bug report" gnus-bug t]))
2872
2873     (easy-menu-define
2874      gnus-article-treatment-menu gnus-article-mode-map ""
2875      '("Treatment"
2876        ["Hide headers" gnus-article-toggle-headers t]
2877        ["Hide signature" gnus-article-hide-signature t]
2878        ["Hide citation" gnus-article-hide-citation t]
2879        ["Treat overstrike" gnus-article-treat-overstrike t]
2880        ["Remove carriage return" gnus-article-remove-cr t]
2881        ["Decode HZ" gnus-article-decode-HZ t]))
2882
2883     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2884
2885     (when (boundp 'gnus-summary-post-menu)
2886       (define-key gnus-article-mode-map [menu-bar post]
2887         (cons "Post" gnus-summary-post-menu)))
2888
2889     (gnus-run-hooks 'gnus-article-menu-hook))
2890   ;; Add the menu.
2891   (when (boundp 'gnus-article-commands-menu)
2892     (easy-menu-add gnus-article-commands-menu gnus-article-mode-map))
2893   (when (boundp 'gnus-summary-post-menu)
2894     (easy-menu-add gnus-summary-post-menu gnus-article-mode-map)))
2895
2896 (defun gnus-article-mode ()
2897   "Major mode for displaying an article.
2898
2899 All normal editing commands are switched off.
2900
2901 The following commands are available in addition to all summary mode
2902 commands:
2903 \\<gnus-article-mode-map>
2904 \\[gnus-article-next-page]\t Scroll the article one page forwards
2905 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2906 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2907 \\[gnus-article-show-summary]\t Display the summary buffer
2908 \\[gnus-article-mail]\t Send a reply to the address near point
2909 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2910 \\[gnus-info-find-node]\t Go to the Gnus info node"
2911   (interactive)
2912   (when (gnus-visual-p 'article-menu 'menu)
2913     (gnus-article-make-menu-bar))
2914   (gnus-simplify-mode-line)
2915   (setq mode-name "Article")
2916   (setq major-mode 'gnus-article-mode)
2917   (make-local-variable 'minor-mode-alist)
2918   (unless (assq 'gnus-show-mime minor-mode-alist)
2919     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
2920   (use-local-map gnus-article-mode-map)
2921   (gnus-update-format-specifications nil 'article-mode)
2922   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2923   (make-local-variable 'gnus-page-broken)
2924   (make-local-variable 'gnus-button-marker-list)
2925   (make-local-variable 'gnus-article-current-summary)
2926   (make-local-variable 'gnus-article-mime-handles)
2927   (make-local-variable 'gnus-article-decoded-p)
2928   (make-local-variable 'gnus-article-mime-handle-alist)
2929   (make-local-variable 'gnus-article-wash-types)
2930   (gnus-set-default-directory)
2931   (buffer-disable-undo)
2932   (setq buffer-read-only t)
2933   (set-syntax-table gnus-article-mode-syntax-table)
2934   (gnus-run-hooks 'gnus-article-mode-hook))
2935
2936 (defun gnus-article-setup-buffer ()
2937   "Initialize the article buffer."
2938   (let* ((name (if gnus-single-article-buffer "*Article*"
2939                  (concat "*Article " gnus-newsgroup-name "*")))
2940          (original
2941           (progn (string-match "\\*Article" name)
2942                  (concat " *Original Article"
2943                          (substring name (match-end 0))))))
2944     (setq gnus-article-buffer name)
2945     (setq gnus-original-article-buffer original)
2946     (setq gnus-article-mime-handle-alist nil)
2947     ;; This might be a variable local to the summary buffer.
2948     (unless gnus-single-article-buffer
2949       (save-excursion
2950         (set-buffer gnus-summary-buffer)
2951         (setq gnus-article-buffer name)
2952         (setq gnus-original-article-buffer original)
2953         (gnus-set-global-variables)))
2954     (gnus-article-setup-highlight-words)
2955     ;; Init original article buffer.
2956     (save-excursion
2957       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2958       (set-buffer-multibyte nil)
2959       (setq major-mode 'gnus-original-article-mode)
2960       (make-local-variable 'gnus-original-article))
2961     (if (get-buffer name)
2962         (save-excursion
2963           (set-buffer name)
2964           (buffer-disable-undo)
2965           (setq buffer-read-only t)
2966           (unless (eq major-mode 'gnus-article-mode)
2967             (gnus-article-mode))
2968           (current-buffer))
2969       (save-excursion
2970         (set-buffer (gnus-get-buffer-create name))
2971         (gnus-article-mode)
2972         (make-local-variable 'gnus-summary-buffer)
2973         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2974         (current-buffer)))))
2975
2976 ;; Set article window start at LINE, where LINE is the number of lines
2977 ;; from the head of the article.
2978 (defun gnus-article-set-window-start (&optional line)
2979   (set-window-start
2980    (get-buffer-window gnus-article-buffer t)
2981    (save-excursion
2982      (set-buffer gnus-article-buffer)
2983      (goto-char (point-min))
2984      (if (not line)
2985          (point-min)
2986        (gnus-message 6 "Moved to bookmark")
2987        (search-forward "\n\n" nil t)
2988        (forward-line line)
2989        (point)))))
2990
2991 ;;; @@ article filters
2992 ;;;
2993
2994 (defun gnus-article-display-mime-message ()
2995   "Article display method for MIME message."
2996   ;; called from `gnus-original-article-buffer'.
2997   (let (charset all-headers)
2998     (with-current-buffer gnus-summary-buffer
2999       (setq charset default-mime-charset
3000             all-headers gnus-have-all-headers))
3001     (make-local-variable 'default-mime-charset)
3002     (setq default-mime-charset charset)
3003     (with-current-buffer (get-buffer-create gnus-article-buffer)
3004       (make-local-variable 'default-mime-charset)
3005       (setq default-mime-charset charset))
3006     (mime-display-message mime-message-structure
3007                           gnus-article-buffer nil gnus-article-mode-map)
3008     (when all-headers
3009       (gnus-article-hide-headers nil -1)))
3010   (run-hooks 'gnus-mime-article-prepare-hook))
3011
3012 (defun gnus-article-display-traditional-message ()
3013   "Article display method for traditional message."
3014   (set-buffer gnus-article-buffer)
3015   (let (buffer-read-only)
3016     (erase-buffer)
3017     (insert-buffer-substring gnus-original-article-buffer)))
3018
3019 (defun gnus-article-make-full-mail-header (&optional number charset)
3020   "Create a new mail header structure in a raw article buffer."
3021   (unless (and number charset)
3022     (save-current-buffer
3023       (set-buffer gnus-summary-buffer)
3024       (unless number
3025         (setq number (or (cdr gnus-article-current) 0)))
3026       (unless charset
3027         (setq charset (or default-mime-charset 'x-ctext)))))
3028   (goto-char (point-min))
3029   (let ((header-end (if (search-forward "\n\n" nil t)
3030                         (1- (point))
3031                       (goto-char (point-max))))
3032         (chars (- (point-max) (point)))
3033         (lines (count-lines (point) (point-max)))
3034         (default-mime-charset charset)
3035         xref)
3036     (narrow-to-region (point-min) header-end)
3037     (setq xref (std11-fetch-field "xref"))
3038     (prog1
3039         (make-full-mail-header
3040          number
3041          (std11-fetch-field "subject")
3042          (std11-fetch-field "from")
3043          (std11-fetch-field "date")
3044          (std11-fetch-field "message-id")
3045          (std11-fetch-field "references")
3046          chars
3047          lines
3048          (when xref (concat "Xref: " xref)))
3049       (widen))))
3050
3051 (defun gnus-article-prepare (article &optional all-headers header)
3052   "Prepare ARTICLE in article mode buffer.
3053 ARTICLE should either be an article number or a Message-ID.
3054 If ARTICLE is an id, HEADER should be the article headers.
3055 If ALL-HEADERS is non-nil, no headers are hidden."
3056   (save-excursion
3057     ;; Make sure we start in a summary buffer.
3058     (unless (eq major-mode 'gnus-summary-mode)
3059       (set-buffer gnus-summary-buffer))
3060     (setq gnus-summary-buffer (current-buffer))
3061     (let* ((gnus-article (if header (mail-header-number header) article))
3062            (summary-buffer (current-buffer))
3063            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3064            (group gnus-newsgroup-name)
3065            result)
3066       (save-excursion
3067         (gnus-article-setup-buffer)
3068         (set-buffer gnus-original-article-buffer)
3069         ;; Deactivate active regions.
3070         (when (and (boundp 'transient-mark-mode)
3071                    transient-mark-mode)
3072           (setq mark-active nil))
3073         (if (not (setq result (let ((buffer-read-only nil))
3074                                 (gnus-request-article-this-buffer
3075                                  article group))))
3076             ;; There is no such article.
3077             (save-excursion
3078               (when (and (numberp article)
3079                          (not (memq article gnus-newsgroup-sparse)))
3080                 (setq gnus-article-current
3081                       (cons gnus-newsgroup-name article))
3082                 (set-buffer gnus-summary-buffer)
3083                 (setq gnus-current-article article)
3084                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3085                     (progn
3086                       (gnus-summary-set-agent-mark article)
3087                       (message "Message marked for downloading"))
3088                   (gnus-summary-mark-article article gnus-canceled-mark)
3089                   (unless (memq article gnus-newsgroup-sparse)
3090                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3091           (if (or (eq result 'pseudo)
3092                   (eq result 'nneething))
3093               (progn
3094                 (save-excursion
3095                   (set-buffer summary-buffer)
3096                   (push article gnus-newsgroup-history)
3097                   (setq gnus-last-article gnus-current-article
3098                         gnus-current-article 0
3099                         gnus-current-headers nil
3100                         gnus-article-current nil)
3101                   (if (eq result 'nneething)
3102                       (gnus-configure-windows 'summary)
3103                     (gnus-configure-windows 'article))
3104                   (gnus-set-global-variables))
3105                 (let ((gnus-article-mime-handle-alist-1
3106                        gnus-article-mime-handle-alist))
3107                   (gnus-set-mode-line 'article)))
3108             ;; The result from the `request' was an actual article -
3109             ;; or at least some text that is now displayed in the
3110             ;; article buffer.
3111             (when (and (numberp article)
3112                        (not (eq article gnus-current-article)))
3113               ;; Seems like a new article has been selected.
3114               ;; `gnus-current-article' must be an article number.
3115               (save-excursion
3116                 (set-buffer summary-buffer)
3117                 (push article gnus-newsgroup-history)
3118                 (setq gnus-last-article gnus-current-article
3119                       gnus-current-article article
3120                       gnus-current-headers
3121                       (gnus-summary-article-header gnus-current-article)
3122                       gnus-article-current
3123                       (cons gnus-newsgroup-name gnus-current-article))
3124                 (unless (vectorp gnus-current-headers)
3125                   (setq gnus-current-headers nil))
3126                 (gnus-summary-goto-subject gnus-current-article)
3127                 (when (gnus-summary-show-thread)
3128                   ;; If the summary buffer really was folded, the
3129                   ;; previous goto may not actually have gone to
3130                   ;; the right article, but the thread root instead.
3131                   ;; So we go again.
3132                   (gnus-summary-goto-subject gnus-current-article))
3133                 (gnus-run-hooks 'gnus-mark-article-hook)
3134                 (gnus-set-mode-line 'summary)
3135                 (when (gnus-visual-p 'article-highlight 'highlight)
3136                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3137                 ;; Set the global newsgroup variables here.
3138                 (gnus-set-global-variables)
3139                 (setq gnus-have-all-headers
3140                       (or all-headers gnus-show-all-headers))))
3141             (save-excursion
3142               (gnus-configure-windows 'article))
3143             (when (or (numberp article)
3144                       (stringp article))
3145               (gnus-article-prepare-display)
3146               ;; Do page break.
3147               (goto-char (point-min))
3148               (setq gnus-page-broken
3149                     (when gnus-break-pages
3150                       (gnus-narrow-to-page)
3151                       t)))
3152             (let ((gnus-article-mime-handle-alist-1
3153                    gnus-article-mime-handle-alist))
3154               (gnus-set-mode-line 'article))
3155             (article-goto-body)
3156             (set-window-point (get-buffer-window (current-buffer)) (point))
3157             (gnus-configure-windows 'article)
3158             t))))))
3159
3160 (defun gnus-article-prepare-mime-display (&optional number)
3161   (goto-char (point-min))
3162   (when (re-search-forward "^[^\t ]+:" nil t)
3163     (goto-char (match-beginning 0)))
3164   (let ((entity (if (eq 1 (point-min))
3165                     (get-text-property 1 'mime-view-entity)
3166                   (get-text-property (point) 'mime-view-entity)))
3167         last-entity child-entity next type)
3168     (setq child-entity (mime-entity-children entity))
3169     (if child-entity
3170         (setq last-entity (nth (1- (length child-entity))
3171                                child-entity))
3172       (setq last-entity entity))
3173     (save-restriction
3174       (narrow-to-region (point)
3175                         (if (search-forward "\n\n" nil t)
3176                             (point)
3177                           (point-max)))
3178       (gnus-treat-article 'head)
3179       (put-text-property (point-min) (point-max) 'article-treated-header t)
3180       (goto-char (point-max)))
3181     (while (and (not (eobp)) entity)
3182       (setq next (set-marker
3183                   (make-marker)
3184                   (next-single-property-change (point) 'mime-view-entity
3185                                                nil (point-max))))
3186       (let ((types (mime-entity-content-type entity)))
3187         (while (eq 'multipart (mime-content-type-primary-type types))
3188           (setq entity (car (mime-entity-children entity))
3189                 types (mime-entity-content-type entity)))
3190         (when types
3191           (setq type (format "%s/%s"
3192                              (mime-content-type-primary-type types)
3193                              (mime-content-type-subtype types)))))
3194       (if (string-equal type "message/rfc822")
3195           (progn
3196             (setq next (point))
3197             (let ((children (mime-entity-children entity))
3198                   last-children)
3199               (when children
3200                 (setq last-children (nth (1- (length children)) children))
3201                 (while
3202                     (and
3203                      (not (eq last-children
3204                               (get-text-property next 'mime-view-entity)))
3205                      (setq next
3206                            (next-single-property-change next
3207                                                         'mime-view-entity
3208                                                         nil (point-max)))))))
3209             (setq next (next-single-property-change next 'mime-view-entity
3210                                                     nil (point-max)))
3211             (save-restriction
3212               (narrow-to-region (point) next)
3213               (gnus-article-prepare-mime-display)
3214               (goto-char (point-max)))
3215             (setq entity (get-text-property (point) 'mime-view-entity)))
3216         (save-restriction
3217           (narrow-to-region (point) next)
3218           ;; Kludge. We have to count true number, but for now,
3219           ;; part number is here only to achieve `last'.
3220           (gnus-treat-article nil 1
3221                               (if (eq entity last-entity)
3222                                   1 2)
3223                               type)
3224           (goto-char (point-max)))
3225         (setq entity (get-text-property next 'mime-view-entity))))))
3226
3227 ;;;###autoload
3228 (defun gnus-article-prepare-display ()
3229   "Make the current buffer look like a nice article."
3230   (setq gnus-article-wash-types nil)
3231   (gnus-run-hooks 'gnus-tmp-internal-hook)
3232   ;; Display message.
3233   (setq mime-message-structure gnus-current-headers)
3234   (mime-buffer-entity-set-buffer-internal mime-message-structure
3235                                           gnus-original-article-buffer)
3236   (mime-entity-set-representation-type-internal mime-message-structure
3237                                                 'mime-buffer-entity)
3238   (luna-send mime-message-structure 'initialize-instance
3239              mime-message-structure)
3240   (if gnus-show-mime
3241       (let (mime-display-header-hook mime-display-text/plain-hook)
3242         (funcall gnus-article-display-method-for-mime))
3243     (funcall gnus-article-display-method-for-traditional))
3244   ;; Associate this article with the current summary buffer.
3245   (setq gnus-article-current-summary gnus-summary-buffer)
3246   ;; Call the treatment functions.
3247   (let ((inhibit-read-only t)
3248         buffer-read-only)
3249     (save-restriction
3250       (widen)
3251       (if gnus-show-mime
3252           (gnus-article-prepare-mime-display)
3253         (narrow-to-region (goto-char (point-min))
3254                           (if (search-forward "\n\n" nil t)
3255                               (point)
3256                             (point-max)))
3257         (gnus-treat-article 'head)
3258         (put-text-property (point-min) (point-max) 'article-treated-header t)
3259         (goto-char (point-max))
3260         (widen)
3261         (narrow-to-region (point) (point-max))
3262         (gnus-treat-article nil))
3263       (put-text-property (point-min) (point-max) 'read-only nil)))
3264   (gnus-run-hooks 'gnus-article-prepare-hook))
3265
3266 (defun gnus-article-decode-article-as-default-mime-charset ()
3267   "Decode an article as `default-mime-charset'.  It won't work if the
3268 value of the variable `gnus-show-mime' is non-nil."
3269   (unless gnus-show-mime
3270     (set (make-local-variable 'default-mime-charset)
3271          (with-current-buffer gnus-summary-buffer
3272            default-mime-charset))
3273     (decode-mime-charset-region (point-min) (point-max)
3274                                 default-mime-charset)))
3275
3276 ;;;
3277 ;;; Gnus MIME viewing functions
3278 ;;;
3279
3280 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3281   "The following specs can be used:
3282 %t  The MIME type
3283 %T  MIME type, along with additional info
3284 %n  The `name' parameter
3285 %d  The description, if any
3286 %l  The length of the encoded part
3287 %p  The part identifier number
3288 %e  Dots if the part isn't displayed")
3289
3290 (defvar gnus-mime-button-line-format-alist
3291   '((?t gnus-tmp-type ?s)
3292     (?T gnus-tmp-type-long ?s)
3293     (?n gnus-tmp-name ?s)
3294     (?d gnus-tmp-description ?s)
3295     (?p gnus-tmp-id ?s)
3296     (?l gnus-tmp-length ?d)
3297     (?e gnus-tmp-dots ?s)))
3298
3299 (defvar gnus-mime-button-commands
3300   '((gnus-article-press-button "\r" "Toggle Display")
3301     (gnus-mime-view-part "v" "View Interactively...")
3302     (gnus-mime-view-part-as-type "t" "View As Type...")
3303     (gnus-mime-save-part "o" "Save...")
3304     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3305     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3306     (gnus-mime-internalize-part "E" "View Internally")
3307     (gnus-mime-externalize-part "e" "View Externally")
3308     (gnus-mime-pipe-part "|" "Pipe To Command...")
3309     (gnus-mime-action-on-part "." "Take action on the part")))
3310
3311 (defun gnus-article-mime-part-status ()
3312   (if gnus-article-mime-handle-alist-1
3313       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
3314     ""))
3315
3316 (defvar gnus-mime-button-map
3317   (let ((map (make-sparse-keymap)))
3318     (set-keymap-parent map gnus-article-mode-map)
3319     (define-key map gnus-mouse-2 'gnus-article-push-button)
3320     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3321     (dolist (c gnus-mime-button-commands)
3322       (define-key map (cadr c) (car c)))
3323     map))
3324
3325 (defun gnus-mime-button-menu (event)
3326   "Construct a context-sensitive menu of MIME commands."
3327   (interactive "e")
3328   (save-window-excursion
3329     (let ((pos (event-start event)))
3330       (select-window (posn-window pos))
3331       (goto-char (posn-point pos))
3332       (gnus-article-check-buffer)
3333       (let ((response (x-popup-menu
3334                        t `("MIME Part"
3335                            ("" ,@(mapcar (lambda (c)
3336                                            (cons (caddr c) (car c)))
3337                                          gnus-mime-button-commands))))))
3338         (if response
3339             (call-interactively response))))))
3340
3341 (defun gnus-mime-view-all-parts (&optional handles)
3342   "View all the MIME parts."
3343   (interactive)
3344   (save-current-buffer
3345     (set-buffer gnus-article-buffer)
3346     (let ((handles (or handles gnus-article-mime-handles))
3347           (mail-parse-charset gnus-newsgroup-charset)
3348           (mail-parse-ignored-charsets 
3349            (save-excursion (set-buffer gnus-summary-buffer)
3350                            gnus-newsgroup-ignored-charsets)))
3351       (if (stringp (car handles))
3352           (gnus-mime-view-all-parts (cdr handles))
3353         (mapcar 'mm-display-part handles)))))
3354
3355 (defun gnus-mime-save-part ()
3356   "Save the MIME part under point."
3357   (interactive)
3358   (gnus-article-check-buffer)
3359   (let ((data (get-text-property (point) 'gnus-data)))
3360     (when data
3361       (mm-save-part data))))
3362
3363 (defun gnus-mime-pipe-part ()
3364   "Pipe the MIME part under point to a process."
3365   (interactive)
3366   (gnus-article-check-buffer)
3367   (let ((data (get-text-property (point) 'gnus-data)))
3368     (when data
3369       (mm-pipe-part data))))
3370
3371 (defun gnus-mime-view-part ()
3372   "Interactively choose a viewing method for the MIME part under point."
3373   (interactive)
3374   (gnus-article-check-buffer)
3375   (let ((data (get-text-property (point) 'gnus-data)))
3376     (when data
3377       (mm-interactively-view-part data))))
3378
3379 (defun gnus-mime-view-part-as-type-internal ()
3380   (gnus-article-check-buffer)
3381   (let* ((name (mail-content-type-get
3382                 (mm-handle-type (get-text-property (point) 'gnus-data))
3383                 'name))
3384          (def-type (and name (mm-default-file-encoding name))))
3385     (and def-type (cons def-type 0))))
3386
3387 (defun gnus-mime-view-part-as-type (mime-type)
3388   "Choose a MIME media type, and view the part as such."
3389   (interactive
3390    (list (completing-read
3391           "View as MIME type: "
3392           (mapcar #'list (mailcap-mime-types))
3393           nil nil
3394           (gnus-mime-view-part-as-type-internal))))
3395   (gnus-article-check-buffer)
3396   (let ((handle (get-text-property (point) 'gnus-data)))
3397     (when handle
3398       (gnus-mm-display-part
3399        (mm-make-handle (mm-handle-buffer handle)
3400                        (cons mime-type (cdr (mm-handle-type handle)))
3401                        (mm-handle-encoding handle)
3402                        (mm-handle-undisplayer handle)
3403                        (mm-handle-disposition handle)
3404                        (mm-handle-description handle)
3405                        (mm-handle-cache handle)
3406                        (mm-handle-id handle))))))
3407
3408 (defun gnus-mime-copy-part (&optional handle)
3409   "Put the the MIME part under point into a new buffer."
3410   (interactive)
3411   (gnus-article-check-buffer)
3412   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3413          (contents (and handle (mm-get-part handle)))
3414          (base (and handle 
3415                     (file-name-nondirectory
3416                      (or
3417                       (mail-content-type-get (mm-handle-type handle) 'name)
3418                       (mail-content-type-get (mm-handle-type handle)
3419                                              'filename)
3420                       "*decoded*"))))
3421          (buffer (and base (generate-new-buffer base))))
3422     (when contents
3423       (switch-to-buffer buffer)
3424       (insert contents)
3425       ;; We do it this way to make `normal-mode' set the appropriate mode.
3426       (unwind-protect
3427           (progn
3428             (setq buffer-file-name (expand-file-name base))
3429             (normal-mode))
3430         (setq buffer-file-name nil))
3431       (goto-char (point-min)))))
3432
3433 (defun gnus-mime-inline-part (&optional handle arg)
3434   "Insert the MIME part under point into the current buffer."
3435   (interactive (list nil current-prefix-arg))
3436   (gnus-article-check-buffer)
3437   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3438          contents charset
3439          (b (point))
3440          buffer-read-only)
3441     (when handle
3442       (if (and (not arg) (mm-handle-undisplayer handle))
3443           (mm-remove-part handle)
3444         (setq contents (mm-get-part handle))
3445         (cond
3446          ((not arg)
3447           (setq charset (or (mail-content-type-get
3448                              (mm-handle-type handle) 'charset)
3449                             gnus-newsgroup-charset)))
3450          ((numberp arg)
3451           (if (mm-handle-undisplayer handle)
3452               (mm-remove-part handle))
3453           (setq charset
3454                 (or (cdr (assq arg 
3455                                gnus-summary-show-article-charset-alist))
3456                     (read-coding-system "Charset: ")))))
3457         (forward-line 2)
3458         (mm-insert-inline handle
3459                           (if (and charset 
3460                                    (setq charset (mm-charset-to-coding-system 
3461                                                   charset))
3462                                    (not (eq charset 'ascii)))
3463                               (mm-decode-coding-string contents charset)
3464                             contents))
3465         (goto-char b)))))
3466
3467 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3468   "Insert the MIME part under point into the current buffer."
3469   (interactive (list nil current-prefix-arg))
3470   (gnus-article-check-buffer)
3471   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3472          contents charset
3473          (b (point))
3474          buffer-read-only)
3475     (when handle
3476       (if (mm-handle-undisplayer handle)
3477           (mm-remove-part handle))
3478       (let ((gnus-newsgroup-charset
3479              (or (cdr (assq arg 
3480                             gnus-summary-show-article-charset-alist))
3481                  (read-coding-system "Charset: ")))
3482           (gnus-newsgroup-ignored-charsets 'gnus-all))
3483         (gnus-article-press-button)))))
3484
3485 (defun gnus-mime-externalize-part (&optional handle)
3486   "View the MIME part under point with an external viewer."
3487   (interactive)
3488   (gnus-article-check-buffer)
3489   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3490          (mm-user-display-methods nil)
3491          (mm-inlined-types nil)
3492          (mail-parse-charset gnus-newsgroup-charset)
3493          (mail-parse-ignored-charsets 
3494           (save-excursion (set-buffer gnus-summary-buffer)
3495                           gnus-newsgroup-ignored-charsets)))
3496     (when handle
3497       (if (mm-handle-undisplayer handle)
3498           (mm-remove-part handle)
3499         (mm-display-part handle)))))
3500
3501 (defun gnus-mime-internalize-part (&optional handle)
3502   "View the MIME part under point with an internal viewer.
3503 In no internal viewer is available, use an external viewer."
3504   (interactive)
3505   (gnus-article-check-buffer)
3506   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3507          (mm-inlined-types '(".*"))
3508          (mm-inline-large-images t)
3509          (mail-parse-charset gnus-newsgroup-charset)
3510          (mail-parse-ignored-charsets 
3511           (save-excursion (set-buffer gnus-summary-buffer)
3512                           gnus-newsgroup-ignored-charsets)))
3513     (when handle
3514       (if (mm-handle-undisplayer handle)
3515           (mm-remove-part handle)
3516         (mm-display-part handle)))))
3517
3518 (defun gnus-mime-action-on-part (&optional action)
3519   "Do something with the MIME attachment at \(point\)."
3520   (interactive
3521    (list (completing-read "Action: " gnus-mime-action-alist)))
3522   (gnus-article-check-buffer)
3523   (let ((action-pair (assoc action gnus-mime-action-alist)))
3524     (if action-pair
3525         (funcall (cdr action-pair)))))
3526
3527 (defun gnus-article-part-wrapper (n function)
3528   (save-current-buffer
3529     (set-buffer gnus-article-buffer)
3530     (when (> n (length gnus-article-mime-handle-alist))
3531       (error "No such part"))
3532     (gnus-article-goto-part n)
3533     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3534       (funcall function handle))))
3535
3536 (defun gnus-article-pipe-part (n)
3537   "Pipe MIME part N, which is the numerical prefix."
3538   (interactive "p")
3539   (gnus-article-part-wrapper n 'mm-pipe-part))
3540
3541 (defun gnus-article-save-part (n)
3542   "Save MIME part N, which is the numerical prefix."
3543   (interactive "p")
3544   (gnus-article-part-wrapper n 'mm-save-part))
3545
3546 (defun gnus-article-interactively-view-part (n)
3547   "View MIME part N interactively, which is the numerical prefix."
3548   (interactive "p")
3549   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3550
3551 (defun gnus-article-copy-part (n)
3552   "Copy MIME part N, which is the numerical prefix."
3553   (interactive "p")
3554   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3555
3556 (defun gnus-article-externalize-part (n)
3557   "View MIME part N externally, which is the numerical prefix."
3558   (interactive "p")
3559   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3560
3561 (defun gnus-article-inline-part (n)
3562   "Inline MIME part N, which is the numerical prefix."
3563   (interactive "p")
3564   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3565
3566 (defun gnus-article-mime-match-handle-first (condition)
3567   (if condition
3568       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3569         (while (setq ihandle (pop alist))
3570           (if (and (cond 
3571                     ((functionp condition)
3572                      (funcall condition (cdr ihandle)))
3573                     ((eq condition 'undisplayed) 
3574                      (not (or (mm-handle-undisplayer (cdr ihandle))
3575                               (equal (mm-handle-media-type (cdr ihandle))
3576                                      "multipart/alternative"))))
3577                     ((eq condition 'undisplayed-alternative)
3578                      (not (mm-handle-undisplayer (cdr ihandle))))
3579                     (t t))
3580                    (gnus-article-goto-part (car ihandle))
3581                    (or (not n) (< (car ihandle) n)))
3582               (setq n (car ihandle))))
3583         (or n 1))
3584     1))
3585
3586 (defun gnus-article-view-part (&optional n)
3587   "View MIME part N, which is the numerical prefix."
3588   (interactive "P")
3589   (save-current-buffer
3590     (set-buffer gnus-article-buffer)
3591     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3592                              gnus-article-mime-match-handle-function)))
3593     (when (> n (length gnus-article-mime-handle-alist))
3594       (error "No such part"))
3595     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3596       (when (gnus-article-goto-part n)
3597         (if (equal (car handle) "multipart/alternative")
3598             (gnus-article-press-button)
3599           (when (eq (gnus-mm-display-part handle) 'internal)
3600             (gnus-set-window-start)))))))
3601
3602 (defsubst gnus-article-mime-total-parts ()
3603   (if (bufferp (car gnus-article-mime-handles))
3604       1 ;; single part
3605     (1- (length gnus-article-mime-handles))))
3606
3607 (defun gnus-mm-display-part (handle)
3608   "Display HANDLE and fix MIME button."
3609   (let ((id (get-text-property (point) 'gnus-part))
3610         (point (point))
3611         buffer-read-only)
3612     (forward-line 1)
3613     (prog1
3614         (let ((window (selected-window))
3615               (mail-parse-charset gnus-newsgroup-charset)
3616               (mail-parse-ignored-charsets 
3617                (save-excursion (set-buffer gnus-summary-buffer)
3618                                gnus-newsgroup-ignored-charsets)))
3619           (save-excursion
3620             (unwind-protect
3621                 (let ((win (get-buffer-window (current-buffer) t))
3622                       (beg (point)))
3623                   (when win
3624                     (select-window win))
3625                   (goto-char point)
3626                   (forward-line)
3627                   (if (mm-handle-displayed-p handle)
3628                       ;; This will remove the part.
3629                       (mm-display-part handle)
3630                     (save-restriction
3631                       (narrow-to-region (point) (1+ (point)))
3632                       (mm-display-part handle)
3633                       ;; We narrow to the part itself and
3634                       ;; then call the treatment functions.
3635                       (goto-char (point-min))
3636                       (forward-line 1)
3637                       (narrow-to-region (point) (point-max))
3638                       (gnus-treat-article
3639                        nil id
3640                        (gnus-article-mime-total-parts)
3641                        (mm-handle-media-type handle)))))
3642               (select-window window))))
3643       (goto-char point)
3644       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3645       (gnus-insert-mime-button
3646        handle id (list (mm-handle-displayed-p handle)))
3647       (goto-char point))))
3648
3649 (defun gnus-article-goto-part (n)
3650   "Go to MIME part N."
3651   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3652     (when point
3653       (goto-char point))))
3654
3655 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3656   (let ((gnus-tmp-name
3657          (or (mail-content-type-get (mm-handle-type handle)
3658                                     'name)
3659              (mail-content-type-get (mm-handle-disposition handle)
3660                                     'filename)
3661              ""))
3662         (gnus-tmp-type (mm-handle-media-type handle))
3663         (gnus-tmp-description
3664          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3665                                               "")))
3666         (gnus-tmp-dots
3667          (if (if displayed (car displayed)
3668                (mm-handle-displayed-p handle))
3669              "" "..."))
3670         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3671                            (buffer-size)))
3672         gnus-tmp-type-long b e)
3673     (when (string-match ".*/" gnus-tmp-name)
3674       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3675     (setq gnus-tmp-type-long (concat gnus-tmp-type
3676                                      (and (not (equal gnus-tmp-name ""))
3677                                           (concat "; " gnus-tmp-name))))
3678     (or (equal gnus-tmp-description "")
3679         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3680     (unless (bolp)
3681       (insert "\n"))
3682     (setq b (point))
3683     (gnus-eval-format
3684      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3685      `(local-map ,gnus-mime-button-map
3686                  keymap ,gnus-mime-button-map
3687                  gnus-callback gnus-mm-display-part
3688                  gnus-part ,gnus-tmp-id
3689                  article-type annotation
3690                  gnus-data ,handle))
3691     (setq e (point))
3692     (widget-convert-button
3693      'link b e
3694      :mime-handle handle
3695      :action 'gnus-widget-press-button
3696      :button-keymap gnus-mime-button-map
3697      :help-echo
3698      (lambda (widget/window &optional overlay pos)
3699        ;; Needed to properly clear the message due to a bug in
3700        ;; wid-edit (XEmacs only).
3701        (if (boundp 'help-echo-owns-message)
3702            (setq help-echo-owns-message t))
3703        (format
3704         "%S: %s the MIME part; %S: more options"
3705         (aref gnus-mouse-2 0)
3706         ;; XEmacs will get a single widget arg; Emacs 21 will get
3707         ;; window, overlay, position.
3708         (if (mm-handle-displayed-p
3709              (if overlay
3710                  (with-current-buffer (gnus-overlay-buffer overlay)
3711                    (widget-get (widget-at (gnus-overlay-start overlay))
3712                                :mime-handle))
3713                (widget-get widget/window :mime-handle)))
3714             "hide" "show")
3715         (aref gnus-down-mouse-3 0))))))
3716
3717 (defun gnus-widget-press-button (elems el)
3718   (goto-char (widget-get elems :from))
3719   (gnus-article-press-button))
3720
3721 (defvar gnus-displaying-mime nil)
3722
3723 (defun gnus-display-mime (&optional ihandles)
3724   "Display the MIME parts."
3725   (save-excursion
3726     (save-selected-window
3727       (let ((window (get-buffer-window gnus-article-buffer))
3728             (point (point)))
3729         (when window
3730           (select-window window)
3731           ;; We have to do this since selecting the window
3732           ;; may change the point.  So we set the window point.
3733           (set-window-point window point)))
3734       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3735              buffer-read-only handle name type b e display)
3736         (when (and (not ihandles)
3737                    (not gnus-displaying-mime))
3738           ;; Top-level call; we clean up.
3739           (when gnus-article-mime-handles
3740             (mm-destroy-parts gnus-article-mime-handles)
3741             (setq gnus-article-mime-handle-alist nil));; A trick.
3742           (setq gnus-article-mime-handles handles)
3743           ;; We allow users to glean info from the handles.
3744           (when gnus-article-mime-part-function
3745             (gnus-mime-part-function handles)))
3746         (if (and handles
3747                  (or (not (stringp (car handles)))
3748                      (cdr handles)))
3749             (progn
3750               (when (and (not ihandles)
3751                          (not gnus-displaying-mime))
3752                 ;; Clean up for mime parts.
3753                 (article-goto-body)
3754                 (delete-region (point) (point-max)))
3755               (let ((gnus-displaying-mime t))
3756                 (gnus-mime-display-part handles)))
3757           (save-restriction
3758             (article-goto-body)
3759             (narrow-to-region (point) (point-max))
3760             (gnus-treat-article nil 1 1)
3761             (widen)))
3762         (unless ihandles
3763           ;; Highlight the headers.
3764           (save-excursion
3765             (save-restriction
3766               (article-goto-body)
3767               (narrow-to-region (point-min) (point))
3768               (gnus-treat-article 'head))))))))
3769
3770 (defvar gnus-mime-display-multipart-as-mixed nil)
3771
3772 (defun gnus-mime-display-part (handle)
3773   (cond
3774    ;; Single part.
3775    ((not (stringp (car handle)))
3776     (gnus-mime-display-single handle))
3777    ;; User-defined multipart
3778    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3779     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3780              handle))
3781    ;; multipart/alternative
3782    ((and (equal (car handle) "multipart/alternative")
3783          (not gnus-mime-display-multipart-as-mixed))
3784     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3785       (push (cons id handle) gnus-article-mime-handle-alist)
3786       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3787    ;; multipart/related
3788    ((and (equal (car handle) "multipart/related")
3789          (not gnus-mime-display-multipart-as-mixed))
3790     ;;;!!!We should find the start part, but we just default
3791     ;;;!!!to the first part.
3792     (gnus-mime-display-part (cadr handle)))
3793    ;; Other multiparts are handled like multipart/mixed.
3794    (t
3795     (gnus-mime-display-mixed (cdr handle)))))
3796
3797 (defun gnus-mime-part-function (handles)
3798   (if (stringp (car handles))
3799       (mapcar 'gnus-mime-part-function (cdr handles))
3800     (funcall gnus-article-mime-part-function handles)))
3801
3802 (defun gnus-mime-display-mixed (handles)
3803   (mapcar 'gnus-mime-display-part handles))
3804
3805 (defun gnus-mime-display-single (handle)
3806   (let ((type (mm-handle-media-type handle))
3807         (ignored gnus-ignored-mime-types)
3808         (not-attachment t)
3809         (move nil)
3810         display text)
3811     (catch 'ignored
3812       (progn
3813         (while ignored
3814           (when (string-match (pop ignored) type)
3815             (throw 'ignored nil)))
3816         (if (and (setq not-attachment
3817                        (and (not (mm-inline-override-p handle))
3818                             (or (not (mm-handle-disposition handle))
3819                                 (equal (car (mm-handle-disposition handle))
3820                                        "inline")
3821                                 (mm-attachment-override-p handle))))
3822                  (mm-automatic-display-p handle)
3823                  (or (mm-inlined-p handle)
3824                      (mm-automatic-external-display-p type)))
3825             (setq display t)
3826           (when (equal (mm-handle-media-supertype handle) "text")
3827             (setq text t)))
3828         (let ((id (1+ (length gnus-article-mime-handle-alist)))
3829               beg)
3830           (push (cons id handle) gnus-article-mime-handle-alist)
3831           (when (or (not display)
3832                     (not (gnus-unbuttonized-mime-type-p type)))
3833             ;(gnus-article-insert-newline)
3834             (gnus-insert-mime-button
3835              handle id (list (or display (and not-attachment text))))
3836             (gnus-article-insert-newline) 
3837             ;(gnus-article-insert-newline) 
3838             ;; Remember modify the number of forward lines.
3839             (setq move t))
3840           (setq beg (point))
3841           (cond
3842            (display
3843             (when move
3844               (forward-line -1)
3845               (setq beg (point)))
3846             (let ((mail-parse-charset gnus-newsgroup-charset)
3847                   (mail-parse-ignored-charsets 
3848                    (save-excursion (condition-case ()
3849                                        (set-buffer gnus-summary-buffer)
3850                                      (error))
3851                                    gnus-newsgroup-ignored-charsets)))
3852               (mm-display-part handle t))
3853             (goto-char (point-max)))
3854            ((and text not-attachment)
3855             (when move
3856               (forward-line -1)
3857               (setq beg (point)))
3858             (gnus-article-insert-newline)
3859             (mm-insert-inline handle (mm-get-part handle))
3860             (goto-char (point-max))))
3861           ;; Do highlighting.
3862           (save-excursion
3863             (save-restriction
3864               (narrow-to-region beg (point))
3865               (gnus-treat-article
3866                nil id 
3867                (gnus-article-mime-total-parts)
3868                (mm-handle-media-type handle)))))))))
3869
3870 (defun gnus-unbuttonized-mime-type-p (type)
3871   "Say whether TYPE is to be unbuttonized."
3872   (unless gnus-inhibit-mime-unbuttonizing
3873     (catch 'found
3874       (let ((types gnus-unbuttonized-mime-types))
3875         (while types
3876           (when (string-match (pop types) type)
3877             (throw 'found t)))))))
3878
3879 (defun gnus-article-insert-newline ()
3880   "Insert a newline, but mark it as undeletable."
3881   (gnus-put-text-property
3882    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3883
3884 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3885   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3886          (ihandles handles)
3887          (point (point))
3888          handle buffer-read-only from props begend not-pref)
3889     (save-window-excursion
3890       (save-restriction
3891         (when ibegend
3892           (narrow-to-region (car ibegend)
3893                             (or (cdr ibegend)
3894                                 (progn
3895                                   (goto-char (car ibegend))
3896                                   (forward-line 2)
3897                                   (point))))
3898           (delete-region (point-min) (point-max))
3899           (mm-remove-parts handles))
3900         (setq begend (list (point-marker)))
3901         ;; Do the toggle.
3902         (unless (setq not-pref (cadr (member preferred ihandles)))
3903           (setq not-pref (car ihandles)))
3904         (when (or ibegend
3905                   (not preferred)
3906                   (not (gnus-unbuttonized-mime-type-p
3907                         "multipart/alternative")))
3908           (gnus-add-text-properties
3909            (setq from (point))
3910            (progn
3911              (insert (format "%d.  " id))
3912              (point))
3913            `(gnus-callback
3914              (lambda (handles)
3915                (unless ,(not ibegend)
3916                  (setq gnus-article-mime-handle-alist
3917                        ',gnus-article-mime-handle-alist))
3918                (gnus-mime-display-alternative
3919                 ',ihandles ',not-pref ',begend ,id))
3920              local-map ,gnus-mime-button-map
3921              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3922              face ,gnus-article-button-face
3923              keymap ,gnus-mime-button-map
3924              gnus-part ,id
3925              gnus-data ,handle))
3926           (widget-convert-button 'link from (point)
3927                                  :action 'gnus-widget-press-button
3928                                  :button-keymap gnus-widget-button-keymap)
3929           ;; Do the handles
3930           (while (setq handle (pop handles))
3931             (gnus-add-text-properties
3932              (setq from (point))
3933              (progn
3934                (insert (format "(%c) %-18s"
3935                                (if (equal handle preferred) ?* ? )
3936                                (mm-handle-media-type handle)))
3937                (point))
3938              `(gnus-callback
3939                (lambda (handles)
3940                  (unless ,(not ibegend)
3941                    (setq gnus-article-mime-handle-alist
3942                          ',gnus-article-mime-handle-alist))
3943                  (gnus-mime-display-alternative
3944                   ',ihandles ',handle ',begend ,id))
3945                local-map ,gnus-mime-button-map
3946                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3947                face ,gnus-article-button-face
3948                keymap ,gnus-mime-button-map
3949                gnus-part ,id
3950                gnus-data ,handle))
3951             (widget-convert-button 'link from (point)
3952                                    :action 'gnus-widget-press-button
3953                                    :button-keymap gnus-widget-button-keymap)
3954             (insert "  "))
3955           (insert "\n\n"))
3956         (when preferred
3957           (if (stringp (car preferred))
3958               (gnus-display-mime preferred)
3959             (let ((mail-parse-charset gnus-newsgroup-charset)
3960                   (mail-parse-ignored-charsets 
3961                    (save-excursion (set-buffer gnus-summary-buffer)
3962                                    gnus-newsgroup-ignored-charsets)))
3963               (mm-display-part preferred)
3964               ;; Do highlighting.
3965               (save-excursion
3966                 (save-restriction
3967                   (narrow-to-region (car begend) (point-max))
3968                   (gnus-treat-article
3969                    nil (length gnus-article-mime-handle-alist)
3970                    (gnus-article-mime-total-parts)
3971                    (mm-handle-media-type handle))))))
3972           (goto-char (point-max))
3973           (setcdr begend (point-marker)))))
3974     (when ibegend
3975       (goto-char point))))
3976
3977 (defun gnus-article-wash-status ()
3978   "Return a string which display status of article washing."
3979   (save-excursion
3980     (set-buffer gnus-article-buffer)
3981     (let ((cite (memq 'cite gnus-article-wash-types))
3982           (headers (memq 'headers gnus-article-wash-types))
3983           (boring (memq 'boring-headers gnus-article-wash-types))
3984           (pgp (memq 'pgp gnus-article-wash-types))
3985           (pem (memq 'pem gnus-article-wash-types))
3986           (signature (memq 'signature gnus-article-wash-types))
3987           (overstrike (memq 'overstrike gnus-article-wash-types))
3988           (emphasis (memq 'emphasis gnus-article-wash-types)))
3989       (format "%c%c%c%c%c%c%c"
3990               (if cite ?c ? )
3991               (if (or headers boring) ?h ? )
3992               (if (or pgp pem) ?p ? )
3993               (if signature ?s ? )
3994               (if overstrike ?o ? )
3995               (if gnus-show-mime ?m ? )
3996               (if emphasis ?e ? )))))
3997
3998 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3999
4000 (defun gnus-article-maybe-hide-headers ()
4001   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4002 Provided for backwards compatibility."
4003   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4004                  (not (save-excursion (set-buffer gnus-summary-buffer)
4005                                       gnus-have-all-headers)))
4006              (not gnus-inhibit-hiding))
4007     (gnus-article-hide-headers)))
4008
4009 ;;; Article savers.
4010
4011 (defun gnus-output-to-file (file-name)
4012   "Append the current article to a file named FILE-NAME."
4013   (let ((artbuf (current-buffer)))
4014     (with-temp-buffer
4015       (insert-buffer-substring artbuf)
4016       ;; Append newline at end of the buffer as separator, and then
4017       ;; save it to file.
4018       (goto-char (point-max))
4019       (insert "\n")
4020       (write-region-as-binary (point-min) (point-max) file-name 'append)
4021       t)))
4022
4023 (defun gnus-narrow-to-page (&optional arg)
4024   "Narrow the article buffer to a page.
4025 If given a numerical ARG, move forward ARG pages."
4026   (interactive "P")
4027   (setq arg (if arg (prefix-numeric-value arg) 0))
4028   (save-excursion
4029     (set-buffer gnus-article-buffer)
4030     (goto-char (point-min))
4031     (widen)
4032     ;; Remove any old next/prev buttons.
4033     (when (gnus-visual-p 'page-marker)
4034       (let ((buffer-read-only nil))
4035         (gnus-remove-text-with-property 'gnus-prev)
4036         (gnus-remove-text-with-property 'gnus-next)))
4037     (when
4038         (cond ((< arg 0)
4039                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4040               ((> arg 0)
4041                (re-search-forward page-delimiter nil 'move arg)))
4042       (goto-char (match-end 0)))
4043     (narrow-to-region
4044      (point)
4045      (if (re-search-forward page-delimiter nil 'move)
4046          (match-beginning 0)
4047        (point)))
4048     (when (and (gnus-visual-p 'page-marker)
4049                (not (= (point-min) 1)))
4050       (save-excursion
4051         (goto-char (point-min))
4052         (gnus-insert-prev-page-button)))
4053     (when (and (gnus-visual-p 'page-marker)
4054                (< (+ (point-max) 2) (buffer-size)))
4055       (save-excursion
4056         (goto-char (point-max))
4057         (gnus-insert-next-page-button)))))
4058
4059 ;; Article mode commands
4060
4061 (defun gnus-article-goto-next-page ()
4062   "Show the next page of the article."
4063   (interactive)
4064   (when (gnus-article-next-page)
4065     (goto-char (point-min))
4066     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4067
4068 (defun gnus-article-goto-prev-page ()
4069   "Show the next page of the article."
4070   (interactive)
4071   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4072     (gnus-article-prev-page nil)))
4073
4074 (defun gnus-article-next-page (&optional lines)
4075   "Show the next page of the current article.
4076 If end of article, return non-nil.  Otherwise return nil.
4077 Argument LINES specifies lines to be scrolled up."
4078   (interactive "p")
4079   (let ((start (window-start))
4080         end-of-buffer end-of-page)
4081     (save-excursion
4082       (move-to-window-line -1)
4083       (if (<= (point) start)
4084           (progn
4085             (forward-line 2)
4086             (setq start (point)))
4087         (forward-line 1)
4088         (setq start nil))
4089       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4090                          (and (pos-visible-in-window-p)
4091                               (setq end-of-buffer t)))
4092                         ((eobp)
4093                          (setq end-of-page t)))
4094                   (not lines))
4095         (move-to-window-line lines)
4096         (unless (search-backward "\n\n" nil t)
4097           (setq start (point)))))
4098     (cond (end-of-buffer t)
4099           (end-of-page
4100            (gnus-narrow-to-page 1)
4101            nil)
4102           (t
4103            (if start
4104                (set-window-start (selected-window) start)
4105              (let (window-pixel-scroll-increment)
4106                (scroll-up lines)))
4107            nil))))
4108
4109 (defun gnus-article-prev-page (&optional lines)
4110   "Show previous page of current article.
4111 Argument LINES specifies lines to be scrolled down."
4112   (interactive "p")
4113   (let (beginning-of-buffer beginning-of-page)
4114     (save-excursion
4115       (move-to-window-line 0)
4116       (cond ((eq 1 (point))
4117              (setq beginning-of-buffer t))
4118             ((bobp)
4119              (setq beginning-of-page t))))
4120     (cond (beginning-of-buffer)
4121           (beginning-of-page
4122            (gnus-narrow-to-page -1))
4123           (t
4124            (condition-case nil
4125                (let (window-pixel-scroll-increment)
4126                  (scroll-down lines))
4127              (beginning-of-buffer
4128               (goto-char (point-min))))))))
4129
4130 (defun gnus-article-refer-article ()
4131   "Read article specified by message-id around point."
4132   (interactive)
4133   (let ((point (point)))
4134     (search-forward ">" nil t)          ;Move point to end of "<....>".
4135     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4136         (let ((message-id (match-string 1)))
4137           (goto-char point)
4138           (set-buffer gnus-summary-buffer)
4139           (gnus-summary-refer-article message-id))
4140       (goto-char (point))
4141       (error "No references around point"))))
4142
4143 (defun gnus-article-show-summary ()
4144   "Reconfigure windows to show summary buffer."
4145   (interactive)
4146   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4147       (error "There is no summary buffer for this article buffer")
4148     (gnus-article-set-globals)
4149     (gnus-configure-windows 'article)
4150     (gnus-summary-goto-subject gnus-current-article)
4151     (gnus-summary-position-point)))
4152
4153 (defun gnus-article-describe-briefly ()
4154   "Describe article mode commands briefly."
4155   (interactive)
4156   (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")))
4157
4158 (defun gnus-article-summary-command ()
4159   "Execute the last keystroke in the summary buffer."
4160   (interactive)
4161   (let ((obuf (current-buffer))
4162         (owin (current-window-configuration))
4163         func)
4164     (switch-to-buffer gnus-article-current-summary 'norecord)
4165     (setq func (lookup-key (current-local-map) (this-command-keys)))
4166     (call-interactively func)
4167     (set-buffer obuf)
4168     (set-window-configuration owin)
4169     (set-window-point (get-buffer-window (current-buffer)) (point))))
4170
4171 (defun gnus-article-summary-command-nosave ()
4172   "Execute the last keystroke in the summary buffer."
4173   (interactive)
4174   (let (func)
4175     (pop-to-buffer gnus-article-current-summary 'norecord)
4176     (setq func (lookup-key (current-local-map) (this-command-keys)))
4177     (call-interactively func)))
4178
4179 (defun gnus-article-check-buffer ()
4180   "Beep if not in an article buffer."
4181   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4182     (error "Command invoked outside of a Gnus article buffer")))
4183
4184 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4185   "Read a summary buffer key sequence and execute it from the article buffer."
4186   (interactive "P")
4187   (gnus-article-check-buffer)
4188   (let ((nosaves
4189          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4190            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4191            "=" "^" "\M-^" "|"))
4192         (nosave-but-article
4193          '("A\r"))
4194         (nosave-in-article
4195          '("\C-d"))
4196         (up-to-top
4197          '("n" "Gn" "p" "Gp"))
4198         keys new-sum-point)
4199     (save-excursion
4200       (set-buffer gnus-article-current-summary)
4201       (let (gnus-pick-mode)
4202         (push (or key last-command-event) unread-command-events)
4203         (setq keys (static-if (featurep 'xemacs)
4204                        (events-to-keys (read-key-sequence nil))
4205                      (read-key-sequence nil)))))
4206     (message "")
4207
4208     (if (or (member keys nosaves)
4209             (member keys nosave-but-article)
4210             (member keys nosave-in-article))
4211         (let (func)
4212           (save-window-excursion
4213             (pop-to-buffer gnus-article-current-summary 'norecord)
4214             ;; We disable the pick minor mode commands.
4215             (let (gnus-pick-mode)
4216               (setq func (lookup-key (current-local-map) keys))))
4217           (if (or (not func)
4218                   (numberp func))
4219               (ding)
4220             (unless (member keys nosave-in-article)
4221               (set-buffer gnus-article-current-summary))
4222             (call-interactively func)
4223             (setq new-sum-point (point)))
4224           (when (member keys nosave-but-article)
4225             (pop-to-buffer gnus-article-buffer 'norecord)))
4226       ;; These commands should restore window configuration.
4227       (let ((obuf (current-buffer))
4228             (owin (current-window-configuration))
4229             (opoint (point))
4230             (summary gnus-article-current-summary)
4231             func in-buffer selected)
4232         (if not-restore-window
4233             (pop-to-buffer summary 'norecord)
4234           (switch-to-buffer summary 'norecord))
4235         (setq in-buffer (current-buffer))
4236         ;; We disable the pick minor mode commands.
4237         (if (and (setq func (let (gnus-pick-mode)
4238                               (lookup-key (current-local-map) keys)))
4239                  (functionp func))
4240             (progn
4241               (call-interactively func)
4242               (setq new-sum-point (point))
4243               (when (eq in-buffer (current-buffer))
4244                 (setq selected (gnus-summary-select-article))
4245                 (set-buffer obuf)
4246                 (unless not-restore-window
4247                   (set-window-configuration owin))
4248                 (when (eq selected 'old)
4249                   (article-goto-body)
4250                   (set-window-start (get-buffer-window (current-buffer))
4251                                     1)
4252                   (set-window-point (get-buffer-window (current-buffer))
4253                                     (point)))
4254                 (let ((win (get-buffer-window gnus-article-current-summary)))
4255                   (when win
4256                     (set-window-point win new-sum-point))))    )
4257           (switch-to-buffer gnus-article-buffer)
4258           (ding))))))
4259
4260 (defun gnus-article-describe-key (key)
4261   "Display documentation of the function invoked by KEY.  KEY is a string."
4262   (interactive "kDescribe key: ")
4263   (gnus-article-check-buffer)
4264   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4265       (save-excursion
4266         (set-buffer gnus-article-current-summary)
4267         (let (gnus-pick-mode)
4268           (push (elt key 0) unread-command-events)
4269           (setq key (if (featurep 'xemacs)
4270                         (events-to-keys (read-key-sequence "Describe key: "))
4271                       (read-key-sequence "Describe key: "))))
4272         (describe-key key))
4273     (describe-key key)))
4274
4275 (defun gnus-article-describe-key-briefly (key &optional insert)
4276   "Display documentation of the function invoked by KEY.  KEY is a string."
4277   (interactive "kDescribe key: \nP")
4278   (gnus-article-check-buffer)
4279   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4280       (save-excursion
4281         (set-buffer gnus-article-current-summary)
4282         (let (gnus-pick-mode)
4283           (push (elt key 0) unread-command-events)
4284           (setq key (if (featurep 'xemacs)
4285                         (events-to-keys (read-key-sequence "Describe key: "))
4286                       (read-key-sequence "Describe key: "))))
4287         (describe-key-briefly key insert))
4288     (describe-key-briefly key insert)))
4289
4290 (defun gnus-article-hide (&optional arg force)
4291   "Hide all the gruft in the current article.
4292 This means that PGP stuff, signatures, cited text and (some)
4293 headers will be hidden.
4294 If given a prefix, show the hidden text instead."
4295   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4296   (gnus-article-hide-headers arg)
4297   (gnus-article-hide-list-identifiers arg)
4298   (gnus-article-hide-pgp arg)
4299   (gnus-article-hide-citation-maybe arg force)
4300   (gnus-article-hide-signature arg))
4301
4302 (defun gnus-article-maybe-highlight ()
4303   "Do some article highlighting if article highlighting is requested."
4304   (when (gnus-visual-p 'article-highlight 'highlight)
4305     (gnus-article-highlight-some)))
4306
4307 (defun gnus-check-group-server ()
4308   ;; Make sure the connection to the server is alive.
4309   (unless (gnus-server-opened
4310            (gnus-find-method-for-group gnus-newsgroup-name))
4311     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4312     (gnus-request-group gnus-newsgroup-name t)))
4313
4314 (defun gnus-request-article-this-buffer (article group)
4315   "Get an article and insert it into this buffer."
4316   (let (do-update-line sparse-header)
4317     (prog1
4318         (save-excursion
4319           (erase-buffer)
4320           (gnus-kill-all-overlays)
4321           (setq group (or group gnus-newsgroup-name))
4322
4323           ;; Using `gnus-request-article' directly will insert the article into
4324           ;; `nntp-server-buffer' - so we'll save some time by not having to
4325           ;; copy it from the server buffer into the article buffer.
4326
4327           ;; We only request an article by message-id when we do not have the
4328           ;; headers for it, so we'll have to get those.
4329           (when (stringp article)
4330             (gnus-read-header article))
4331
4332           ;; If the article number is negative, that means that this article
4333           ;; doesn't belong in this newsgroup (possibly), so we find its
4334           ;; message-id and request it by id instead of number.
4335           (when (and (numberp article)
4336                      gnus-summary-buffer
4337                      (get-buffer gnus-summary-buffer)
4338                      (gnus-buffer-exists-p gnus-summary-buffer))
4339             (save-excursion
4340               (set-buffer gnus-summary-buffer)
4341               (let ((header (gnus-summary-article-header article)))
4342                 (when (< article 0)
4343                   (cond
4344                    ((memq article gnus-newsgroup-sparse)
4345                     ;; This is a sparse gap article.
4346                     (setq do-update-line article)
4347                     (setq article (mail-header-id header))
4348                     (setq sparse-header (gnus-read-header article))
4349                     (setq gnus-newsgroup-sparse
4350                           (delq article gnus-newsgroup-sparse)))
4351                    ((vectorp header)
4352                     ;; It's a real article.
4353                     (setq article (mail-header-id header)))
4354                    (t
4355                     ;; It is an extracted pseudo-article.
4356                     (setq article 'pseudo)
4357                     (gnus-request-pseudo-article header))))
4358
4359                 (let ((method (gnus-find-method-for-group
4360                                gnus-newsgroup-name)))
4361                   (when (and (eq (car method) 'nneething)
4362                              (vectorp header))
4363                     (let ((dir (expand-file-name
4364                                 (mail-header-subject header)
4365                                 (file-name-as-directory
4366                                  (or (cadr (assq 'nneething-address method))
4367                                      (nth 1 method))))))
4368                       (when (file-directory-p dir)
4369                         (setq article 'nneething)
4370                         (gnus-group-enter-directory dir))))))))
4371
4372           (cond
4373            ;; Refuse to select canceled articles.
4374            ((and (numberp article)
4375                  gnus-summary-buffer
4376                  (get-buffer gnus-summary-buffer)
4377                  (gnus-buffer-exists-p gnus-summary-buffer)
4378                  (eq (cdr (save-excursion
4379                             (set-buffer gnus-summary-buffer)
4380                             (assq article gnus-newsgroup-reads)))
4381                      gnus-canceled-mark))
4382             nil)
4383            ;; Check the backlog.
4384            ((and gnus-keep-backlog
4385                  (gnus-backlog-request-article group article (current-buffer)))
4386             'article)
4387            ;; Check asynchronous pre-fetch.
4388            ((gnus-async-request-fetched-article group article (current-buffer))
4389             (gnus-async-prefetch-next group article gnus-summary-buffer)
4390             (when (and (numberp article) gnus-keep-backlog)
4391               (gnus-backlog-enter-article group article (current-buffer)))
4392             'article)
4393            ;; Check the cache.
4394            ((and gnus-use-cache
4395                  (numberp article)
4396                  (gnus-cache-request-article article group))
4397             'article)
4398            ;; Get the article and put into the article buffer.
4399            ((or (stringp article)
4400                 (numberp article))
4401             (let ((gnus-override-method gnus-override-method)
4402                   (methods (and (stringp article) 
4403                                 gnus-refer-article-method))
4404                   result
4405                   (buffer-read-only nil))
4406               (if (or (not (listp methods))
4407                       (and (symbolp (car methods))
4408                            (assq (car methods) nnoo-definition-alist)))
4409                   (setq methods (list methods)))
4410               (when (and (null gnus-override-method)
4411                          methods)
4412                 (setq gnus-override-method (pop methods)))
4413               (while (not result)
4414                 (when (eq gnus-override-method 'current)
4415                   (setq gnus-override-method gnus-current-select-method))
4416                 (erase-buffer)
4417                 (gnus-kill-all-overlays)
4418                 (let ((gnus-newsgroup-name group))
4419                   (gnus-check-group-server))
4420                 (when (gnus-request-article article group (current-buffer))
4421                   (when (numberp article)
4422                     (gnus-async-prefetch-next group article 
4423                                               gnus-summary-buffer)
4424                     (when gnus-keep-backlog
4425                       (gnus-backlog-enter-article
4426                        group article (current-buffer))))
4427                   (setq result 'article))
4428                 (if (not result)
4429                     (if methods
4430                         (setq gnus-override-method (pop methods))
4431                       (setq result 'done))))
4432               (and (eq result 'article) 'article)))
4433            ;; It was a pseudo.
4434            (t article)))
4435
4436       ;; Associate this article with the current summary buffer.
4437       (setq gnus-article-current-summary gnus-summary-buffer)
4438
4439       ;; Take the article from the original article buffer
4440       ;; and place it in the buffer it's supposed to be in.
4441       (when (and (get-buffer gnus-article-buffer)
4442                  (equal (buffer-name (current-buffer))
4443                         (buffer-name (get-buffer gnus-article-buffer))))
4444         (save-excursion
4445           (if (get-buffer gnus-original-article-buffer)
4446               (set-buffer gnus-original-article-buffer)
4447             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4448             (set-buffer-multibyte nil)
4449             (buffer-disable-undo)
4450             (setq major-mode 'gnus-original-article-mode)
4451             (setq buffer-read-only t))
4452           (let (buffer-read-only)
4453             (erase-buffer)
4454             (insert-buffer-substring gnus-article-buffer))
4455           (setq gnus-original-article (cons group article)))
4456
4457         ;; Decode charsets.
4458         (run-hooks 'gnus-article-decode-hook)
4459         ;; Mark article as decoded or not.
4460         (setq gnus-article-decoded-p gnus-article-decode-hook))
4461
4462       ;; Update sparse articles.
4463       (when (and do-update-line
4464                  (or (numberp article)
4465                      (stringp article)))
4466         (let ((buf (current-buffer)))
4467           (set-buffer gnus-summary-buffer)
4468           (gnus-summary-update-article do-update-line sparse-header)
4469           (gnus-summary-goto-subject do-update-line nil t)
4470           (set-window-point (get-buffer-window (current-buffer) t)
4471                             (point))
4472           (set-buffer buf))))))
4473
4474 ;;;
4475 ;;; Article editing
4476 ;;;
4477
4478 (defcustom gnus-article-edit-mode-hook nil
4479   "Hook run in article edit mode buffers."
4480   :group 'gnus-article-various
4481   :type 'hook)
4482
4483 (defcustom gnus-article-edit-article-setup-function
4484   'gnus-article-mime-edit-article-setup
4485   "Function called to setup an editing article buffer."
4486   :group 'gnus-article-various
4487   :type 'function)
4488
4489 (defvar gnus-article-edit-done-function nil)
4490
4491 (defvar gnus-article-edit-mode-map nil)
4492
4493 ;; Should we be using derived.el for this?
4494 (unless gnus-article-edit-mode-map
4495   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4496   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4497
4498   (gnus-define-keys gnus-article-edit-mode-map
4499     "\C-c\C-c" gnus-article-edit-done
4500     "\C-c\C-k" gnus-article-edit-exit)
4501
4502   (gnus-define-keys (gnus-article-edit-wash-map
4503                      "\C-c\C-w" gnus-article-edit-mode-map)
4504     "f" gnus-article-edit-full-stops))
4505
4506 (defun gnus-article-edit-mode ()
4507   "Major mode for editing articles.
4508 This is an extended text-mode.
4509
4510 \\{gnus-article-edit-mode-map}"
4511   (interactive)
4512   (setq major-mode 'gnus-article-edit-mode)
4513   (setq mode-name "Article Edit")
4514   (use-local-map gnus-article-edit-mode-map)
4515   (make-local-variable 'gnus-article-edit-done-function)
4516   (make-local-variable 'gnus-prev-winconf)
4517   (setq buffer-read-only nil)
4518   (buffer-enable-undo)
4519   (widen)
4520   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4521
4522 (defun gnus-article-edit (&optional force)
4523   "Edit the current article.
4524 This will have permanent effect only in mail groups.
4525 If FORCE is non-nil, allow editing of articles even in read-only
4526 groups."
4527   (interactive "P")
4528   (when (and (not force)
4529              (gnus-group-read-only-p))
4530     (error "The current newsgroup does not support article editing"))
4531   (gnus-article-date-original)
4532   (gnus-article-edit-article
4533    'ignore
4534    `(lambda (no-highlight)
4535       'ignore
4536       (gnus-summary-edit-article-done
4537        ,(or (mail-header-references gnus-current-headers) "")
4538        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4539
4540 (defun gnus-article-edit-article (start-func exit-func)
4541   "Start editing the contents of the current article buffer."
4542   (let ((winconf (current-window-configuration)))
4543     (set-buffer gnus-article-buffer)
4544     (gnus-article-edit-mode)
4545     (funcall start-func)
4546     (gnus-configure-windows 'edit-article)
4547     (setq gnus-article-edit-done-function exit-func)
4548     (setq gnus-prev-winconf winconf)
4549     (when gnus-article-edit-article-setup-function
4550       (funcall gnus-article-edit-article-setup-function))
4551     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
4552
4553 (defun gnus-article-edit-done (&optional arg)
4554   "Update the article edits and exit."
4555   (interactive "P")
4556   (let ((func gnus-article-edit-done-function)
4557         (buf (current-buffer))
4558         (start (window-start)))
4559     (remove-hook 'gnus-article-mode-hook
4560                  'gnus-article-mime-edit-article-unwind)
4561     (gnus-article-edit-exit)
4562     (save-excursion
4563       (set-buffer buf)
4564       (let ((buffer-read-only nil))
4565         (funcall func arg))
4566       ;; The cache and backlog have to be flushed somewhat.
4567       (when gnus-keep-backlog
4568         (gnus-backlog-remove-article
4569          (car gnus-article-current) (cdr gnus-article-current)))
4570       ;; Flush original article as well.
4571       (save-excursion
4572         (when (get-buffer gnus-original-article-buffer)
4573           (set-buffer gnus-original-article-buffer)
4574           (setq gnus-original-article nil)))
4575       (when gnus-use-cache
4576         (gnus-cache-update-article
4577          (car gnus-article-current) (cdr gnus-article-current))))
4578     (set-buffer buf)
4579     (set-window-start (get-buffer-window buf) start)
4580     (set-window-point (get-buffer-window buf) (point))))
4581
4582 (defun gnus-article-edit-exit ()
4583   "Exit the article editing without updating."
4584   (interactive)
4585   ;; We remove all text props from the article buffer.
4586   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4587         (curbuf (current-buffer))
4588         (p (point))
4589         (window-start (window-start)))
4590     (erase-buffer)
4591     (insert buf)
4592     (let ((winconf gnus-prev-winconf))
4593       (gnus-article-mode)
4594       (set-window-configuration winconf)
4595       ;; Tippy-toe some to make sure that point remains where it was.
4596       (save-current-buffer
4597         (set-buffer curbuf)
4598         (set-window-start (get-buffer-window (current-buffer)) window-start)
4599         (goto-char p)))))
4600
4601 (defun gnus-article-edit-full-stops ()
4602   "Interactively repair spacing at end of sentences."
4603   (interactive)
4604   (save-excursion
4605     (goto-char (point-min))
4606     (search-forward-regexp "^$" nil t)
4607     (let ((case-fold-search nil))
4608       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4609
4610 ;;;
4611 ;;; Article editing with MIME-Edit
4612 ;;;
4613
4614 (defcustom gnus-article-mime-edit-article-setup-hook nil
4615   "Hook run after setting up a MIME editing article buffer."
4616   :group 'gnus-article-various
4617   :type 'hook)
4618
4619 (defun gnus-article-mime-edit-article-unwind ()
4620   "Unwind `gnus-article-buffer' if article editing was given up."
4621   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4622   (when (featurep 'font-lock)
4623     (setq font-lock-defaults nil)
4624     (font-lock-mode -1))
4625   (when mime-edit-mode-flag
4626     (mime-edit-exit 'nomime 'no-error)
4627     (message "")))
4628
4629 (defun gnus-article-mime-edit-article-setup ()
4630   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
4631 after replacing with the original article."
4632   (setq gnus-show-mime t)
4633   (setq gnus-article-edit-done-function
4634         `(lambda (&rest args)
4635            (when mime-edit-mode-flag
4636              (let (mime-edit-insert-user-agent-field)
4637                (mime-edit-exit))
4638              (message ""))
4639            (goto-char (point-min))
4640            (let (case-fold-search)
4641              (when (re-search-forward
4642                     (format "^%s$" (regexp-quote mail-header-separator))
4643                     nil t)
4644                (replace-match "")))
4645            (apply ,gnus-article-edit-done-function args)
4646            (set-buffer (get-buffer-create gnus-original-article-buffer))
4647            (erase-buffer)
4648            (insert-buffer gnus-article-buffer)
4649            (setq gnus-current-headers (gnus-article-make-full-mail-header))
4650            (gnus-article-prepare-display)))
4651   (substitute-key-definition 'gnus-article-edit-done
4652                              'gnus-article-mime-edit-done
4653                              gnus-article-edit-mode-map)
4654   (substitute-key-definition 'gnus-article-edit-exit
4655                              'gnus-article-mime-edit-exit
4656                              gnus-article-edit-mode-map)
4657   (erase-buffer)
4658   (insert-buffer gnus-original-article-buffer)
4659   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
4660     (fset 'mime-edit-decode-single-part-in-buffer
4661           (lambda (&rest args)
4662             (if (let ((content-type (car args)))
4663                   (and (eq 'message (mime-content-type-primary-type
4664                                      content-type))
4665                        (eq 'rfc822 (mime-content-type-subtype content-type))))
4666                 (setcar (cdr args) 'not-decode-text))
4667             (apply ofn args)))
4668     (unwind-protect
4669         (mime-edit-again)
4670       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
4671   (when (featurep 'font-lock)
4672     (set (make-local-variable 'font-lock-defaults)
4673          '(message-font-lock-keywords t))
4674     (font-lock-set-defaults)
4675     (turn-on-font-lock))
4676   (set-buffer-modified-p nil)
4677   (delete-other-windows)
4678   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4679   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
4680
4681 (defun gnus-article-mime-edit-done (&optional arg)
4682   "Update the article MIME edits and exit."
4683   (interactive "P")
4684   (when (featurep 'font-lock)
4685     (setq font-lock-defaults nil)
4686     (font-lock-mode -1))
4687   (gnus-article-edit-done arg))
4688
4689 (defun gnus-article-mime-edit-exit ()
4690   "Exit the article MIME editing without updating."
4691   (interactive)
4692   (when (or (not (buffer-modified-p))
4693             (yes-or-no-p "Article modified; kill anyway? "))
4694     (when (featurep 'font-lock)
4695       (setq font-lock-defaults nil)
4696       (font-lock-mode -1))
4697     (when mime-edit-mode-flag
4698       (let (mime-edit-insert-user-agent-field)
4699         (mime-edit-exit))
4700       (message ""))
4701     (goto-char (point-min))
4702     (let (case-fold-search)
4703       (when (re-search-forward
4704              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
4705         (replace-match "")))
4706     (let ((winconf gnus-prev-winconf))
4707       (insert (prog1
4708                   (buffer-substring-no-properties (point-min) (point-max))
4709                 (set-buffer (get-buffer-create gnus-original-article-buffer))
4710                 (erase-buffer)))
4711       (setq gnus-current-headers (gnus-article-make-full-mail-header))
4712       (gnus-article-prepare-display)
4713       (set-window-configuration winconf))))
4714
4715 ;;;
4716 ;;; Article highlights
4717 ;;;
4718
4719 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4720
4721 ;;; Internal Variables:
4722
4723 (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\\)\\)"
4724   "Regular expression that matches URLs."
4725   :group 'gnus-article-buttons
4726   :type 'regexp)
4727
4728 (defcustom gnus-button-alist
4729   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4730      0 t gnus-button-message-id 2)
4731     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4732     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4733      1 t
4734      gnus-button-fetch-group 4)
4735     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4736     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4737      t gnus-button-message-id 3)
4738     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4739     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4740     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4741     ;; This is how URLs _should_ be embedded in text...
4742     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4743     ;; Raw URLs.
4744     (,gnus-button-url-regexp 0 t browse-url 0))
4745   "*Alist of regexps matching buttons in article bodies.
4746
4747 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4748 REGEXP: is the string matching text around the button,
4749 BUTTON: is the number of the regexp grouping actually matching the button,
4750 FORM: is a lisp expression which must eval to true for the button to
4751 be added,
4752 CALLBACK: is the function to call when the user push this button, and each
4753 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4754
4755 CALLBACK can also be a variable, in that case the value of that
4756 variable it the real callback function."
4757   :group 'gnus-article-buttons
4758   :type '(repeat (list regexp
4759                        (integer :tag "Button")
4760                        (sexp :tag "Form")
4761                        (function :tag "Callback")
4762                        (repeat :tag "Par"
4763                                :inline t
4764                                (integer :tag "Regexp group")))))
4765
4766 (defcustom gnus-header-button-alist
4767   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4768      0 t gnus-button-message-id 0)
4769     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4770     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4771      0 t gnus-button-mailto 0)
4772     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4773     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4774     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4775     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4776      gnus-button-message-id 3))
4777   "*Alist of headers and regexps to match buttons in article heads.
4778
4779 This alist is very similar to `gnus-button-alist', except that each
4780 alist has an additional HEADER element first in each entry:
4781
4782 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4783
4784 HEADER is a regexp to match a header.  For a fuller explanation, see
4785 `gnus-button-alist'."
4786   :group 'gnus-article-buttons
4787   :group 'gnus-article-headers
4788   :type '(repeat (list (regexp :tag "Header")
4789                        regexp
4790                        (integer :tag "Button")
4791                        (sexp :tag "Form")
4792                        (function :tag "Callback")
4793                        (repeat :tag "Par"
4794                                :inline t
4795                                (integer :tag "Regexp group")))))
4796
4797 (defvar gnus-button-regexp nil)
4798 (defvar gnus-button-marker-list nil)
4799 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4800
4801 (defvar gnus-button-last nil)
4802 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4803
4804 ;;; Commands:
4805
4806 (defun gnus-article-push-button (event)
4807   "Check text under the mouse pointer for a callback function.
4808 If the text under the mouse pointer has a `gnus-callback' property,
4809 call it with the value of the `gnus-data' text property."
4810   (interactive "e")
4811   (set-buffer (window-buffer (posn-window (event-start event))))
4812   (let* ((pos (posn-point (event-start event)))
4813          (data (get-text-property pos 'gnus-data))
4814          (fun (get-text-property pos 'gnus-callback)))
4815     (goto-char pos)
4816     (when fun
4817       (funcall fun data))))
4818
4819 (defun gnus-article-press-button ()
4820   "Check text at point for a callback function.
4821 If the text at point has a `gnus-callback' property,
4822 call it with the value of the `gnus-data' text property."
4823   (interactive)
4824   (let* ((data (get-text-property (point) 'gnus-data))
4825          (fun (get-text-property (point) 'gnus-callback)))
4826     (when fun
4827       (funcall fun data))))
4828
4829 (defun gnus-article-prev-button (n)
4830   "Move point to N buttons backward.
4831 If N is negative, move forward instead."
4832   (interactive "p")
4833   (gnus-article-next-button (- n)))
4834
4835 (defun gnus-article-next-button (n)
4836   "Move point to N buttons forward.
4837 If N is negative, move backward instead."
4838   (interactive "p")
4839   (let ((function (if (< n 0) 'previous-single-property-change
4840                     'next-single-property-change))
4841         (inhibit-point-motion-hooks t)
4842         (backward (< n 0))
4843         (limit (if (< n 0) (point-min) (point-max))))
4844     (setq n (abs n))
4845     (while (and (not (= limit (point)))
4846                 (> n 0))
4847       ;; Skip past the current button.
4848       (when (get-text-property (point) 'gnus-callback)
4849         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4850       ;; Go to the next (or previous) button.
4851       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
4852       ;; Put point at the start of the button.
4853       (when (and backward (not (get-text-property (point) 'gnus-callback)))
4854         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4855       ;; Skip past intangible buttons.
4856       (when (get-text-property (point) 'intangible)
4857         (incf n))
4858       (decf n))
4859     (unless (zerop n)
4860       (gnus-message 5 "No more buttons"))
4861     n))
4862
4863 (defun gnus-article-highlight (&optional force)
4864   "Highlight current article.
4865 This function calls `gnus-article-highlight-headers',
4866 `gnus-article-highlight-citation',
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-citation force)
4872   (gnus-article-highlight-signature)
4873   (gnus-article-add-buttons force)
4874   (gnus-article-add-buttons-to-head))
4875
4876 (defun gnus-article-highlight-some (&optional force)
4877   "Highlight current article.
4878 This function calls `gnus-article-highlight-headers',
4879 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4880 do the highlighting.  See the documentation for those functions."
4881   (interactive (list 'force))
4882   (gnus-article-highlight-headers)
4883   (gnus-article-highlight-signature)
4884   (gnus-article-add-buttons))
4885
4886 (defun gnus-article-highlight-headers ()
4887   "Highlight article headers as specified by `gnus-header-face-alist'."
4888   (interactive)
4889   (save-excursion
4890     (set-buffer gnus-article-buffer)
4891     (save-restriction
4892       (let ((alist gnus-header-face-alist)
4893             (buffer-read-only nil)
4894             (case-fold-search t)
4895             (inhibit-point-motion-hooks t)
4896             entry regexp header-face field-face from hpoints fpoints)
4897         (article-narrow-to-head)
4898         (while (setq entry (pop alist))
4899           (goto-char (point-min))
4900           (setq regexp (concat "^\\("
4901                                (if (string-equal "" (nth 0 entry))
4902                                    "[^\t ]"
4903                                  (nth 0 entry))
4904                                "\\)")
4905                 header-face (nth 1 entry)
4906                 field-face (nth 2 entry))
4907           (while (and (re-search-forward regexp nil t)
4908                       (not (eobp)))
4909             (beginning-of-line)
4910             (setq from (point))
4911             (unless (search-forward ":" nil t)
4912               (forward-char 1))
4913             (when (and header-face
4914                        (not (memq (point) hpoints)))
4915               (push (point) hpoints)
4916               (gnus-put-text-property from (point) 'face header-face))
4917             (when (and field-face
4918                        (not (memq (setq from (point)) fpoints)))
4919               (push from fpoints)
4920               (if (re-search-forward "^[^ \t]" nil t)
4921                   (forward-char -2)
4922                 (goto-char (point-max)))
4923               (gnus-put-text-property from (point) 'face field-face))))))))
4924
4925 (defun gnus-article-highlight-signature ()
4926   "Highlight the signature in an article.
4927 It does this by highlighting everything after
4928 `gnus-signature-separator' using `gnus-signature-face'."
4929   (interactive)
4930   (when gnus-signature-face
4931     (save-excursion
4932       (set-buffer gnus-article-buffer)
4933       (let ((buffer-read-only nil)
4934             (inhibit-point-motion-hooks t))
4935         (save-restriction
4936           (when (gnus-article-narrow-to-signature)
4937             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4938                               'face gnus-signature-face)))))))
4939
4940 (defun gnus-article-buttonize-signature ()
4941   "Add button to the signature."
4942   (interactive)
4943   (save-excursion
4944     (set-buffer gnus-article-buffer)
4945     (let ((buffer-read-only nil)
4946           (inhibit-point-motion-hooks t))
4947       (when (gnus-article-search-signature)
4948         (gnus-article-add-button (match-beginning 0) (match-end 0)
4949                                  'gnus-signature-toggle
4950                                  (set-marker (make-marker)
4951                                              (1+ (match-end 0))))))))
4952
4953 (defun gnus-button-in-region-p (b e prop)
4954   "Say whether PROP exists in the region."
4955   (text-property-not-all b e prop nil))
4956
4957 (defun gnus-article-add-buttons (&optional force)
4958   "Find external references in the article and make buttons of them.
4959 \"External references\" are things like Message-IDs and URLs, as
4960 specified by `gnus-button-alist'."
4961   (interactive (list 'force))
4962   (save-excursion
4963     (set-buffer gnus-article-buffer)
4964     (let ((buffer-read-only nil)
4965           (inhibit-point-motion-hooks t)
4966           (case-fold-search t)
4967           (alist gnus-button-alist)
4968           beg entry regexp)
4969       ;; Remove all old markers.
4970       (let (marker entry new-list)
4971         (while (setq marker (pop gnus-button-marker-list))
4972           (if (or (< marker (point-min)) (>= marker (point-max)))
4973               (push marker new-list)
4974             (goto-char marker)
4975             (when (setq entry (gnus-button-entry))
4976               (put-text-property (match-beginning (nth 1 entry))
4977                                  (match-end (nth 1 entry))
4978                                  'gnus-callback nil))
4979             (set-marker marker nil)))
4980         (setq gnus-button-marker-list new-list))
4981       ;; We skip the headers.
4982       (article-goto-body)
4983       (setq beg (point))
4984       (while (setq entry (pop alist))
4985         (setq regexp (car entry))
4986         (goto-char beg)
4987         (while (re-search-forward regexp nil t)
4988           (let* ((start (and entry (match-beginning (nth 1 entry))))
4989                  (end (and entry (match-end (nth 1 entry))))
4990                  (from (match-beginning 0)))
4991             (when (and (or (eq t (nth 2 entry))
4992                            (eval (nth 2 entry)))
4993                        (not (gnus-button-in-region-p
4994                              start end 'gnus-callback)))
4995               ;; That optional form returned non-nil, so we add the
4996               ;; button.
4997               (gnus-article-add-button
4998                start end 'gnus-button-push
4999                (car (push (set-marker (make-marker) from)
5000                           gnus-button-marker-list))))))))))
5001
5002 ;; Add buttons to the head of an article.
5003 (defun gnus-article-add-buttons-to-head ()
5004   "Add buttons to the head of the article."
5005   (interactive)
5006   (save-excursion
5007     (set-buffer gnus-article-buffer)
5008     (save-restriction
5009       (let ((buffer-read-only nil)
5010             (inhibit-point-motion-hooks t)
5011             (case-fold-search t)
5012             (alist gnus-header-button-alist)
5013             entry beg end)
5014         (article-narrow-to-head)
5015         (while alist
5016           ;; Each alist entry.
5017           (setq entry (car alist)
5018                 alist (cdr alist))
5019           (goto-char (point-min))
5020           (while (re-search-forward (car entry) nil t)
5021             ;; Each header matching the entry.
5022             (setq beg (match-beginning 0))
5023             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5024                                (match-beginning 0))
5025                           (point-max)))
5026             (goto-char beg)
5027             (while (re-search-forward (nth 1 entry) end t)
5028               ;; Each match within a header.
5029               (let* ((entry (cdr entry))
5030                      (start (match-beginning (nth 1 entry)))
5031                      (end (match-end (nth 1 entry)))
5032                      (form (nth 2 entry)))
5033                 (goto-char (match-end 0))
5034                 (when (eval form)
5035                   (gnus-article-add-button
5036                    start end (nth 3 entry)
5037                    (buffer-substring (match-beginning (nth 4 entry))
5038                                      (match-end (nth 4 entry)))))))
5039             (goto-char end)))))))
5040
5041 ;;; External functions:
5042
5043 (defun gnus-article-add-button (from to fun &optional data)
5044   "Create a button between FROM and TO with callback FUN and data DATA."
5045   (when gnus-article-button-face
5046     (gnus-overlay-put (gnus-make-overlay from to)
5047                       'face gnus-article-button-face))
5048   (gnus-add-text-properties
5049    from to
5050    (nconc (and gnus-article-mouse-face
5051                (list gnus-mouse-face-prop gnus-article-mouse-face))
5052           (list 'gnus-callback fun)
5053           (and data (list 'gnus-data data))))
5054   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5055                          ;; Quote `:button-keymap' for Mule 2.3
5056                          ;; but it won't work.
5057                          ':button-keymap gnus-widget-button-keymap))
5058
5059 ;;; Internal functions:
5060
5061 (defun gnus-article-set-globals ()
5062   (save-excursion
5063     (set-buffer gnus-summary-buffer)
5064     (gnus-set-global-variables)))
5065
5066 (defun gnus-signature-toggle (end)
5067   (save-excursion
5068     (set-buffer gnus-article-buffer)
5069     (let ((buffer-read-only nil)
5070           (inhibit-point-motion-hooks t)
5071           (limit (next-single-property-change end 'mime-view-entity
5072                                               nil (point-max))))
5073       (if (text-property-any end limit 'article-type 'signature)
5074           (gnus-remove-text-properties-when
5075            'article-type 'signature end limit
5076            (cons 'article-type (cons 'signature
5077                                      gnus-hidden-properties)))
5078         (gnus-add-text-properties-when
5079          'article-type nil end limit
5080          (cons 'article-type (cons 'signature
5081                                    gnus-hidden-properties)))))))
5082
5083 (defun gnus-button-entry ()
5084   ;; Return the first entry in `gnus-button-alist' matching this place.
5085   (let ((alist gnus-button-alist)
5086         (entry nil))
5087     (while alist
5088       (setq entry (pop alist))
5089       (if (looking-at (car entry))
5090           (setq alist nil)
5091         (setq entry nil)))
5092     entry))
5093
5094 (defun gnus-button-push (marker)
5095   ;; Push button starting at MARKER.
5096   (save-excursion
5097     (goto-char marker)
5098     (let* ((entry (gnus-button-entry))
5099            (inhibit-point-motion-hooks t)
5100            (fun (nth 3 entry))
5101            (args (mapcar (lambda (group)
5102                            (let ((string (match-string group)))
5103                              (gnus-set-text-properties
5104                               0 (length string) nil string)
5105                              string))
5106                          (nthcdr 4 entry))))
5107       (cond
5108        ((fboundp fun)
5109         (apply fun args))
5110        ((and (boundp fun)
5111              (fboundp (symbol-value fun)))
5112         (apply (symbol-value fun) args))
5113        (t
5114         (gnus-message 1 "You must define `%S' to use this button"
5115                       (cons fun args)))))))
5116
5117 (defun gnus-button-message-id (message-id)
5118   "Fetch MESSAGE-ID."
5119   (save-excursion
5120     (set-buffer gnus-summary-buffer)
5121     (gnus-summary-refer-article message-id)))
5122
5123 (defun gnus-button-fetch-group (address)
5124   "Fetch GROUP specified by ADDRESS."
5125   (if (not (string-match "[:/]" address))
5126       ;; This is just a simple group url.
5127       (gnus-group-read-ephemeral-group address gnus-select-method)
5128     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5129                            address))
5130         (error "Can't parse %s" address)
5131       (gnus-group-read-ephemeral-group
5132        (match-string 4 address)
5133        `(nntp ,(match-string 1 address)
5134               (nntp-address ,(match-string 1 address))
5135               (nntp-port-number ,(if (match-end 3)
5136                                      (match-string 3 address)
5137                                    "nntp")))))))
5138
5139 (defun gnus-url-parse-query-string (query &optional downcase)
5140   (let (retval pairs cur key val)
5141     (setq pairs (split-string query "&"))
5142     (while pairs
5143       (setq cur (car pairs)
5144             pairs (cdr pairs))
5145       (if (not (string-match "=" cur))
5146           nil                           ; Grace
5147         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5148               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5149         (if downcase
5150             (setq key (downcase key)))
5151         (setq cur (assoc key retval))
5152         (if cur
5153             (setcdr cur (cons val (cdr cur)))
5154           (setq retval (cons (list key val) retval)))))
5155     retval))
5156
5157 (defun gnus-url-unhex (x)
5158   (if (> x ?9)
5159       (if (>= x ?a)
5160           (+ 10 (- x ?a))
5161         (+ 10 (- x ?A)))
5162     (- x ?0)))
5163
5164 (defun gnus-url-unhex-string (str &optional allow-newlines)
5165   "Remove %XXX embedded spaces, etc in a url.
5166 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5167 decoding of carriage returns and line feeds in the string, which is normally
5168 forbidden in URL encoding."
5169   (setq str (or str ""))
5170   (let ((tmp "")
5171         (case-fold-search t))
5172     (while (string-match "%[0-9a-f][0-9a-f]" str)
5173       (let* ((start (match-beginning 0))
5174              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5175              (code (+ (* 16 ch1)
5176                       (gnus-url-unhex (elt str (+ start 2))))))
5177         (setq tmp (concat
5178                    tmp (substring str 0 start)
5179                    (cond
5180                     (allow-newlines
5181                      (char-to-string code))
5182                     ((or (= code ?\n) (= code ?\r))
5183                      " ")
5184                     (t (char-to-string code))))
5185               str (substring str (match-end 0)))))
5186     (setq tmp (concat tmp str))
5187     tmp))
5188
5189 (defun gnus-url-mailto (url)
5190   ;; Send mail to someone
5191   (when (string-match "mailto:/*\\(.*\\)" url)
5192     (setq url (substring url (match-beginning 1) nil)))
5193   (let (to args subject func)
5194     (if (string-match (regexp-quote "?") url)
5195         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5196               args (gnus-url-parse-query-string
5197                     (substring url (match-end 0) nil) t))
5198       (setq to (gnus-url-unhex-string url)))
5199     (setq args (cons (list "to" to) args)
5200           subject (cdr-safe (assoc "subject" args)))
5201     (gnus-setup-message 'reply
5202       (message-mail)
5203       (while args
5204         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5205         (if (fboundp func)
5206             (funcall func)
5207           (message-position-on-field (caar args)))
5208         (insert (mapconcat 'identity (cdar args) ", "))
5209         (setq args (cdr args)))
5210       (if subject
5211           (message-goto-body)
5212         (message-goto-subject)))))
5213
5214 (defun gnus-button-mailto (address)
5215   "Mail to ADDRESS."
5216   (set-buffer (gnus-copy-article-buffer))
5217   (message-reply address))
5218
5219 (defalias 'gnus-button-reply 'message-reply)
5220
5221 (defun gnus-button-embedded-url (address)
5222   "Activate ADDRESS with `browse-url'."
5223   (browse-url (gnus-strip-whitespace address)))
5224
5225 (eval-when-compile
5226   ;; Silence the byte-compiler.
5227   (autoload 'smiley-toggle-buffer "gnus-bitmap"))
5228 (defun gnus-article-smiley-display ()
5229   "Display \"smileys\" as small graphical icons."
5230   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5231
5232 ;;; Next/prev buttons in the article buffer.
5233
5234 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5235 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5236
5237 (defvar gnus-prev-page-map nil)
5238 (unless gnus-prev-page-map
5239   (setq gnus-prev-page-map (make-sparse-keymap))
5240   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5241   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5242
5243 (static-if (featurep 'xemacs)
5244     (defun gnus-insert-prev-page-button ()
5245       (let ((buffer-read-only nil))
5246         (gnus-eval-format
5247          gnus-prev-page-line-format nil
5248          `(gnus-prev t local-map ,gnus-prev-page-map
5249                      gnus-callback gnus-article-button-prev-page
5250                      article-type annotation))))
5251   (defun gnus-insert-prev-page-button ()
5252     (let ((buffer-read-only nil)
5253           (situation (get-text-property (point-min) 'mime-view-situation)))
5254       (set-keymap-parent gnus-prev-page-map (current-local-map))
5255       (gnus-eval-format
5256        gnus-prev-page-line-format nil
5257        `(gnus-prev t local-map ,gnus-prev-page-map
5258                    gnus-callback gnus-article-button-prev-page
5259                    article-type annotation
5260                    mime-view-situation ,situation))))
5261   )
5262
5263 (defvar gnus-next-page-map nil)
5264 (unless gnus-next-page-map
5265   (setq gnus-next-page-map (make-sparse-keymap))
5266   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5267   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5268
5269 (defun gnus-button-next-page ()
5270   "Go to the next page."
5271   (interactive)
5272   (let ((win (selected-window)))
5273     (select-window (get-buffer-window gnus-article-buffer t))
5274     (gnus-article-next-page)
5275     (select-window win)))
5276
5277 (defun gnus-button-prev-page ()
5278   "Go to the prev page."
5279   (interactive)
5280   (let ((win (selected-window)))
5281     (select-window (get-buffer-window gnus-article-buffer t))
5282     (gnus-article-prev-page)
5283     (select-window win)))
5284
5285 (static-if (featurep 'xemacs)
5286     (defun gnus-insert-next-page-button ()
5287       (let ((buffer-read-only nil))
5288         (gnus-eval-format gnus-next-page-line-format nil
5289                           `(gnus-next
5290                             t local-map ,gnus-next-page-map
5291                             gnus-callback gnus-article-button-next-page
5292                             article-type annotation))))
5293   (defun gnus-insert-next-page-button ()
5294     (let ((buffer-read-only nil)
5295           (situation (get-text-property (point-min) 'mime-view-situation)))
5296       (set-keymap-parent gnus-next-page-map (current-local-map))
5297       (gnus-eval-format gnus-next-page-line-format nil
5298                         `(gnus-next
5299                           t local-map ,gnus-next-page-map
5300                           gnus-callback gnus-article-button-next-page
5301                           article-type annotation
5302                           mime-view-situation ,situation))))
5303   )
5304
5305 (defun gnus-article-button-next-page (arg)
5306   "Go to the next page."
5307   (interactive "P")
5308   (let ((win (selected-window)))
5309     (select-window (get-buffer-window gnus-article-buffer t))
5310     (gnus-article-next-page)
5311     (select-window win)))
5312
5313 (defun gnus-article-button-prev-page (arg)
5314   "Go to the prev page."
5315   (interactive "P")
5316   (let ((win (selected-window)))
5317     (select-window (get-buffer-window gnus-article-buffer t))
5318     (gnus-article-prev-page)
5319     (select-window win)))
5320
5321 (defvar gnus-decode-header-methods
5322   '(mail-decode-encoded-word-region)
5323   "List of methods used to decode headers.
5324
5325 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5326 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5327 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5328 whose names match REGEXP.
5329
5330 For example:
5331 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5332  mail-decode-encoded-word-region
5333  (\"chinese\" . rfc1843-decode-region))
5334 ")
5335
5336 (defvar gnus-decode-header-methods-cache nil)
5337
5338 (defun gnus-multi-decode-header (start end)
5339   "Apply the functions from `gnus-encoded-word-methods' that match."
5340   (unless (and gnus-decode-header-methods-cache
5341                (eq gnus-newsgroup-name
5342                    (car gnus-decode-header-methods-cache)))
5343     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5344     (mapcar (lambda (x)
5345               (if (symbolp x)
5346                   (nconc gnus-decode-header-methods-cache (list x))
5347                 (if (and gnus-newsgroup-name
5348                          (string-match (car x) gnus-newsgroup-name))
5349                     (nconc gnus-decode-header-methods-cache
5350                            (list (cdr x))))))
5351           gnus-decode-header-methods))
5352   (let ((xlist gnus-decode-header-methods-cache))
5353     (pop xlist)
5354     (save-restriction
5355       (narrow-to-region start end)
5356       (while xlist
5357         (funcall (pop xlist) (point-min) (point-max))))))
5358
5359 ;;;
5360 ;;; Treatment top-level handling.
5361 ;;;
5362
5363 (defun gnus-treat-article (condition &optional part-number total-parts type)
5364   (let ((length (- (point-max) (point-min)))
5365         (alist gnus-treatment-function-alist)
5366         (article-goto-body-goes-to-point-min-p t)
5367         (treated-type
5368          (or (not type)
5369              (catch 'found
5370                (let ((list gnus-article-treat-types))
5371                  (while list
5372                    (when (string-match (pop list) type)
5373                      (throw 'found t)))))))
5374         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5375         (entity (static-unless (featurep 'xemacs)
5376                   (when (eq 'head condition)
5377                     (get-text-property (point-min) 'mime-view-entity))))
5378         val elem buttonized)
5379     (gnus-run-hooks 'gnus-part-display-hook)
5380     (unless gnus-inhibit-treatment
5381       (while (setq elem (pop alist))
5382         (setq val
5383               (save-excursion
5384                 (if (gnus-buffer-live-p gnus-summary-buffer)
5385                     (set-buffer gnus-summary-buffer))
5386                 (symbol-value (car elem))))
5387         (when (and (or (consp val)
5388                        treated-type)
5389                    (gnus-treat-predicate val)
5390                    (or (not (get (car elem) 'highlight))
5391                        highlightp))
5392           (when (and (not buttonized)
5393                      (memq (car elem)
5394                            '(gnus-treat-hide-signature
5395                              gnus-treat-highlight-signature)))
5396             (gnus-article-buttonize-signature)
5397             (setq buttonized t))
5398           (save-restriction
5399             (funcall (cadr elem)))))
5400       ;; FSF Emacsen does not inherit the existing text properties
5401       ;; in the new text, so we should do it for `mime-view-entity'.
5402       (static-unless (featurep 'xemacs)
5403         (when entity
5404           (put-text-property (point-min) (point-max)
5405                              'mime-view-entity entity))))))
5406
5407 ;; Dynamic variables.
5408 (eval-when-compile
5409   (defvar part-number)
5410   (defvar total-parts)
5411   (defvar type)
5412   (defvar condition)
5413   (defvar length))
5414
5415 (defun gnus-treat-predicate (val)
5416   (cond
5417    ((null val)
5418     nil)
5419    ((and (listp val)
5420          (stringp (car val)))
5421     (apply 'gnus-or (mapcar `(lambda (s)
5422                                (string-match s ,(or gnus-newsgroup-name "")))
5423                             val)))
5424    ((listp val)
5425     (let ((pred (pop val)))
5426       (cond
5427        ((eq pred 'or)
5428         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5429        ((eq pred 'and)
5430         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5431        ((eq pred 'not)
5432         (not (gnus-treat-predicate (car val))))
5433        ((eq pred 'typep)
5434         (equal (car val) type))
5435        (t
5436         (error "%S is not a valid predicate" pred)))))
5437    ((eq val 'mime)
5438     gnus-show-mime)
5439    (condition
5440     (eq condition val))
5441    ((eq val t)
5442     t)
5443    ((eq val 'head)
5444     nil)
5445    ((eq val 'last)
5446     (eq part-number total-parts))
5447    ((numberp val)
5448     (< length val))
5449    (t
5450     (error "%S is not a valid value" val))))
5451
5452 ;;; @ for mime-view
5453 ;;;
5454
5455 (defun gnus-article-header-presentation-method (entity situation)
5456   (mime-insert-header entity)
5457   )
5458
5459 (set-alist 'mime-header-presentation-method-alist
5460            'gnus-original-article-mode
5461            #'gnus-article-header-presentation-method)
5462
5463 (defun gnus-mime-preview-quitting-method ()
5464   (mime-preview-kill-buffer)
5465   (delete-other-windows)
5466   (gnus-article-show-summary)
5467   (gnus-summary-select-article gnus-show-all-headers t))
5468
5469 (set-alist 'mime-preview-quitting-method-alist
5470            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
5471
5472 (set-alist 'mime-preview-following-method-alist
5473            'gnus-original-article-mode #'gnus-following-method)
5474
5475 (set-alist 'mime-preview-over-to-previous-method-alist
5476            'gnus-original-article-mode
5477            (lambda ()
5478              (if (> (point-min) 1)
5479                  (gnus-article-prev-page)
5480                (gnus-article-read-summary-keys
5481                 nil (gnus-character-to-event ?P)))))
5482
5483 (set-alist 'mime-preview-over-to-next-method-alist
5484            'gnus-original-article-mode'
5485            (lambda ()
5486              (if (< (point-max) (buffer-size))
5487                  (gnus-article-next-page)
5488                (gnus-article-read-summary-keys
5489                 nil (gnus-character-to-event ?N)))))
5490
5491
5492 ;;; @ end
5493 ;;;
5494
5495 (gnus-ems-redefine)
5496
5497 (provide 'gnus-art)
5498
5499 (run-hooks 'gnus-art-load-hook)
5500
5501 ;;; gnus-art.el ends here