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