2c44f0c68b5f2637ca3d95e03bb6eb87923b8705
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98 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
32 (require 'custom)
33 (require 'gnus)
34 (require 'gnus-sum)
35 (require 'gnus-spec)
36 (require 'gnus-int)
37 (require 'browse-url)
38 (require 'alist)
39 (require 'mime-view)
40
41 ;; Avoid byte-compile warnings.
42 (eval-when-compile
43   (defvar gnus-article-decoded-p)
44   (defvar gnus-article-mime-handles)
45   (require 'mm-bodies)
46   (require 'mail-parse)
47   (require 'mm-decode)
48   (require 'mm-view)
49   (require 'wid-edit)
50   (require 'mm-uu)
51   )
52
53 (defgroup gnus-article nil
54   "Article display."
55   :link '(custom-manual "(gnus)The Article Buffer")
56   :group 'gnus)
57
58 (defgroup gnus-article-hiding nil
59   "Hiding article parts."
60   :link '(custom-manual "(gnus)Article Hiding")
61   :group 'gnus-article)
62
63 (defgroup gnus-article-highlight nil
64   "Article highlighting."
65   :link '(custom-manual "(gnus)Article Highlighting")
66   :group 'gnus-article
67   :group 'gnus-visual)
68
69 (defgroup gnus-article-signature nil
70   "Article signatures."
71   :link '(custom-manual "(gnus)Article Signature")
72   :group 'gnus-article)
73
74 (defgroup gnus-article-headers nil
75   "Article headers."
76   :link '(custom-manual "(gnus)Hiding Headers")
77   :group 'gnus-article)
78
79 (defgroup gnus-article-washing nil
80   "Special commands on articles."
81   :link '(custom-manual "(gnus)Article Washing")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-emphasis nil
85   "Fontisizing articles."
86   :link '(custom-manual "(gnus)Article Fontisizing")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-saving nil
90   "Saving articles."
91   :link '(custom-manual "(gnus)Saving Articles")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-mime nil
95   "Worshiping the MIME wonder."
96   :link '(custom-manual "(gnus)Using MIME")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-buttons nil
100   "Pushable buttons in the article buffer."
101   :link '(custom-manual "(gnus)Article Buttons")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-various nil
105   "Other article options."
106   :link '(custom-manual "(gnus)Misc Article")
107   :group 'gnus-article)
108
109 (defcustom gnus-ignored-headers
110   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
111     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
112     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
113     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
114     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
115     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
116     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
117     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
118     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
119     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
120     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
121     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
122     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
123     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
124     "^Old-Received:" "^X-Pgp-Fingerprint:" "^X-Pgp-Key-Id:"
125     "^X-Pgp-Public-Key-Url:" "^X-Auth:" "^X-From-Line:"
126     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
127     "^X-Mailing-List:" "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
128     "^Status:")
129   "*All headers that start with this regexp will be hidden.
130 This variable can also be a list of regexps of headers to be ignored.
131 If `gnus-visible-headers' is non-nil, this variable will be ignored."
132   :type '(choice :custom-show nil
133                  regexp
134                  (repeat regexp))
135   :group 'gnus-article-hiding)
136
137 (defcustom gnus-visible-headers
138   "From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^\\(Mail-\\)?Followup-To:\\|^\\(Mail-\\)?Reply-To:\\|^Mail-Copies-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|X-Sent:"
139   "*All headers that do not match this regexp will be hidden.
140 This variable can also be a list of regexp of headers to remain visible.
141 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
142   :type '(repeat :value-to-internal (lambda (widget value)
143                                       (custom-split-regexp-maybe value))
144                  :match (lambda (widget value)
145                           (or (stringp value)
146                               (widget-editable-list-match widget value)))
147                  regexp)
148   :group 'gnus-article-hiding)
149
150 (defcustom gnus-sorted-header-list
151   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
152     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
153   "*This variable is a list of regular expressions.
154 If it is non-nil, headers that match the regular expressions will
155 be placed first in the article buffer in the sequence specified by
156 this list."
157   :type '(repeat regexp)
158   :group 'gnus-article-hiding)
159
160 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
161   "Headers that are only to be displayed if they have interesting data.
162 Possible values in this list are `empty', `newsgroups', `followup-to',
163 `reply-to', `date', `long-to', and `many-to'."
164   :type '(set (const :tag "Headers with no content." empty)
165               (const :tag "Newsgroups with only one group." newsgroups)
166               (const :tag "Followup-to identical to newsgroups." followup-to)
167               (const :tag "Reply-to identical to from." reply-to)
168               (const :tag "Date less than four days old." date)
169               (const :tag "Very long To header." long-to)
170               (const :tag "Multiple To headers." many-to))
171   :group 'gnus-article-hiding)
172
173 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
174   "Regexp matching signature separator.
175 This can also be a list of regexps.  In that case, it will be checked
176 from head to tail looking for a separator.  Searches will be done from
177 the end of the buffer."
178   :type '(repeat string)
179   :group 'gnus-article-signature)
180
181 (defcustom gnus-signature-limit nil
182    "Provide a limit to what is considered a signature.
183 If it is a number, no signature may not be longer (in characters) than
184 that number.  If it is a floating point number, no signature may be
185 longer (in lines) than that number.  If it is a function, the function
186 will be called without any parameters, and if it returns nil, there is
187 no signature in the buffer.  If it is a string, it will be used as a
188 regexp.  If it matches, the text in question is not a signature."
189   :type '(choice (integer :value 200)
190                  (number :value 4.0)
191                  (function :value fun)
192                  (regexp :value ".*"))
193   :group 'gnus-article-signature)
194
195 (defcustom gnus-hidden-properties '(invisible t intangible t)
196   "Property list to use for hiding text."
197   :type 'sexp
198   :group 'gnus-article-hiding)
199
200 (defcustom gnus-article-x-face-command
201   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
202   "*String or function to be executed to display an X-Face header.
203 If it is a string, the command will be executed in a sub-shell
204 asynchronously.  The compressed face will be piped to this command."
205   :type 'string                         ;Leave function case to Lisp.
206   :group 'gnus-article-washing)
207
208 (defcustom gnus-article-x-face-too-ugly nil
209   "Regexp matching posters whose face shouldn't be shown automatically."
210   :type '(choice regexp (const nil))
211   :group 'gnus-article-washing)
212
213 (defcustom gnus-emphasis-alist
214   (let ((format
215          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\|\\s)\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-?!.,;:\"]\\|\\s(\\|\\s)\\)")
216         (types
217          '(("_" "_" underline)
218            ("/" "/" italic)
219            ("\\*" "\\*" bold)
220            ("_/" "/_" underline-italic)
221            ("_\\*" "\\*_" underline-bold)
222            ("\\*/" "/\\*" bold-italic)
223            ("_\\*/" "/\\*_" underline-bold-italic))))
224     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
225        2 3 gnus-emphasis-underline)
226       ,@(mapcar
227          (lambda (spec)
228            (list
229             (format format (car spec) (cadr spec))
230             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
231          types)))
232   "*Alist that says how to fontify certain phrases.
233 Each item looks like this:
234
235   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
236
237 The first element is a regular expression to be matched.  The second
238 is a number that says what regular expression grouping used to find
239 the entire emphasized word.  The third is a number that says what
240 regexp grouping should be displayed and highlighted.  The fourth
241 is the face used for highlighting."
242   :type '(repeat (list :value ("" 0 0 default)
243                        regexp
244                        (integer :tag "Match group")
245                        (integer :tag "Emphasize group")
246                        face))
247   :group 'gnus-article-emphasis)
248
249 (defface gnus-emphasis-bold '((t (:bold t)))
250   "Face used for displaying strong emphasized text (*word*)."
251   :group 'gnus-article-emphasis)
252
253 (defface gnus-emphasis-italic '((t (:italic t)))
254   "Face used for displaying italic emphasized text (/word/)."
255   :group 'gnus-article-emphasis)
256
257 (defface gnus-emphasis-underline '((t (:underline t)))
258   "Face used for displaying underlined emphasized text (_word_)."
259   :group 'gnus-article-emphasis)
260
261 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
262   "Face used for displaying underlined bold emphasized text (_*word*_)."
263   :group 'gnus-article-emphasis)
264
265 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
266   "Face used for displaying underlined italic emphasized text (_*word*_)."
267   :group 'gnus-article-emphasis)
268
269 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
270   "Face used for displaying bold italic emphasized text (/*word*/)."
271   :group 'gnus-article-emphasis)
272
273 (defface gnus-emphasis-underline-bold-italic
274   '((t (:bold t :italic t :underline t)))
275   "Face used for displaying underlined bold italic emphasized text.
276 Esample: (_/*word*/_)."
277   :group 'gnus-article-emphasis)
278
279 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
280   "Format for display of Date headers in article bodies.
281 See `format-time-string' for the possible values.
282
283 The variable can also be function, which should return a complete Date
284 header.  The function is called with one argument, the time, which can
285 be fed to `format-time-string'."
286   :type '(choice string symbol)
287   :link '(custom-manual "(gnus)Article Date")
288   :group 'gnus-article-washing)
289
290 (eval-and-compile
291   (autoload 'mail-extract-address-components "mail-extr"))
292
293 (defcustom gnus-save-all-headers t
294   "*If non-nil, don't remove any headers before saving."
295   :group 'gnus-article-saving
296   :type 'boolean)
297
298 (defcustom gnus-prompt-before-saving 'always
299   "*This variable says how much prompting is to be done when saving articles.
300 If it is nil, no prompting will be done, and the articles will be
301 saved to the default files.  If this variable is `always', each and
302 every article that is saved will be preceded by a prompt, even when
303 saving large batches of articles.  If this variable is neither nil not
304 `always', there the user will be prompted once for a file name for
305 each invocation of the saving commands."
306   :group 'gnus-article-saving
307   :type '(choice (item always)
308                  (item :tag "never" nil)
309                  (sexp :tag "once" :format "%t\n" :value t)))
310
311 (defcustom gnus-saved-headers gnus-visible-headers
312   "Headers to keep if `gnus-save-all-headers' is nil.
313 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
314 If that variable is nil, however, all headers that match this regexp
315 will be kept while the rest will be deleted before saving."
316   :group 'gnus-article-saving
317   :type 'regexp)
318
319 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
320   "A function to save articles in your favourite format.
321 The function must be interactively callable (in other words, it must
322 be an Emacs command).
323
324 Gnus provides the following functions:
325
326 * gnus-summary-save-in-rmail (Rmail format)
327 * gnus-summary-save-in-mail (Unix mail format)
328 * gnus-summary-save-in-folder (MH folder)
329 * gnus-summary-save-in-file (article format)
330 * gnus-summary-save-in-vm (use VM's folder format)
331 * gnus-summary-write-to-file (article format -- overwrite)."
332   :group 'gnus-article-saving
333   :type '(radio (function-item gnus-summary-save-in-rmail)
334                 (function-item gnus-summary-save-in-mail)
335                 (function-item gnus-summary-save-in-folder)
336                 (function-item gnus-summary-save-in-file)
337                 (function-item gnus-summary-save-in-vm)
338                 (function-item gnus-summary-write-to-file)))
339
340 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
341   "A function generating a file name to save articles in Rmail format.
342 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
343   :group 'gnus-article-saving
344   :type 'function)
345
346 (defcustom gnus-mail-save-name 'gnus-plain-save-name
347   "A function generating a file name to save articles in Unix mail format.
348 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
349   :group 'gnus-article-saving
350   :type 'function)
351
352 (defcustom gnus-folder-save-name 'gnus-folder-save-name
353   "A function generating a file name to save articles in MH folder.
354 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
355   :group 'gnus-article-saving
356   :type 'function)
357
358 (defcustom gnus-file-save-name 'gnus-numeric-save-name
359   "A function generating a file name to save articles in article format.
360 The function is called with NEWSGROUP, HEADERS, and optional
361 LAST-FILE."
362   :group 'gnus-article-saving
363   :type 'function)
364
365 (defcustom gnus-split-methods
366   '((gnus-article-archive-name)
367     (gnus-article-nndoc-name))
368   "*Variable used to suggest where articles are to be saved.
369 For instance, if you would like to save articles related to Gnus in
370 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
371 you could set this variable to something like:
372
373  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
374    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
375
376 This variable is an alist where the where the key is the match and the
377 value is a list of possible files to save in if the match is non-nil.
378
379 If the match is a string, it is used as a regexp match on the
380 article.  If the match is a symbol, that symbol will be funcalled
381 from the buffer of the article to be saved with the newsgroup as the
382 parameter.  If it is a list, it will be evaled in the same buffer.
383
384 If this form or function returns a string, this string will be used as
385 a possible file name; and if it returns a non-nil list, that list will
386 be used as possible file names."
387   :group 'gnus-article-saving
388   :type '(repeat (choice (list :value (fun) function)
389                          (cons :value ("" "") regexp (repeat string))
390                          (sexp :value nil))))
391
392 (defcustom gnus-article-display-method-for-mime
393   'gnus-article-display-mime-message
394   "Function to display a MIME message.
395 The function is called from the article buffer."
396   :group 'gnus-article-mime
397   :type 'function)
398
399 (defcustom gnus-article-display-method-for-traditional
400   'gnus-article-display-traditional-message
401   "*Function to display a traditional message.
402 The function is called from the article buffer."
403   :group 'gnus-article-mime
404   :type 'function)
405
406 (defcustom gnus-page-delimiter "^\^L"
407   "*Regexp describing what to use as article page delimiters.
408 The default value is \"^\^L\", which is a form linefeed at the
409 beginning of a line."
410   :type 'regexp
411   :group 'gnus-article-various)
412
413 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
414   "*The format specification for the article mode line.
415 See `gnus-summary-mode-line-format' for a closer description.
416
417 The following additional specs are available:
418
419 %w  The article washing status.
420 %m  The number of MIME parts in the article."
421   :type 'string
422   :group 'gnus-article-various)
423
424 (defcustom gnus-article-mode-hook nil
425   "*A hook for Gnus article mode."
426   :type 'hook
427   :group 'gnus-article-various)
428
429 (defcustom gnus-article-menu-hook nil
430   "*Hook run after the creation of the article mode menu."
431   :type 'hook
432   :group 'gnus-article-various)
433
434 (defcustom gnus-article-prepare-hook nil
435   "*A hook called after an article has been prepared in the article buffer.
436 If you want to run a special decoding program like nkf, use this hook."
437   :type 'hook
438   :group 'gnus-article-various)
439
440 (defcustom gnus-article-hide-pgp-hook nil
441   "*A hook called after successfully hiding a PGP signature."
442   :type 'hook
443   :group 'gnus-article-various)
444
445 (defcustom gnus-article-button-face 'bold
446   "Face used for highlighting buttons in the article buffer.
447
448 An article button is a piece of text that you can activate by pressing
449 `RET' or `mouse-2' above it."
450   :type 'face
451   :group 'gnus-article-buttons)
452
453 (defcustom gnus-article-mouse-face 'highlight
454   "Face used for mouse highlighting in the article buffer.
455
456 Article buttons will be displayed in this face when the cursor is
457 above them."
458   :type 'face
459   :group 'gnus-article-buttons)
460
461 (defcustom gnus-signature-face 'gnus-signature-face
462   "Face used for highlighting a signature in the article buffer.
463 Obsolete; use the face `gnus-signature-face' for customizations instead."
464   :type 'face
465   :group 'gnus-article-highlight
466   :group 'gnus-article-signature)
467
468 (defface gnus-signature-face
469   '((((type x))
470      (:italic t)))
471   "Face used for highlighting a signature in the article buffer."
472   :group 'gnus-article-highlight
473   :group 'gnus-article-signature)
474
475 (defface gnus-header-from-face
476   '((((class color)
477       (background dark))
478      (:foreground "spring green"))
479     (((class color)
480       (background light))
481      (:foreground "red3"))
482     (t
483      (:italic t)))
484   "Face used for displaying from headers."
485   :group 'gnus-article-headers
486   :group 'gnus-article-highlight)
487
488 (defface gnus-header-subject-face
489   '((((class color)
490       (background dark))
491      (:foreground "SeaGreen3"))
492     (((class color)
493       (background light))
494      (:foreground "red4"))
495     (t
496      (:bold t :italic t)))
497   "Face used for displaying subject headers."
498   :group 'gnus-article-headers
499   :group 'gnus-article-highlight)
500
501 (defface gnus-header-newsgroups-face
502   '((((class color)
503       (background dark))
504      (:foreground "yellow" :italic t))
505     (((class color)
506       (background light))
507      (:foreground "MidnightBlue" :italic t))
508     (t
509      (:italic t)))
510   "Face used for displaying newsgroups headers."
511   :group 'gnus-article-headers
512   :group 'gnus-article-highlight)
513
514 (defface gnus-header-name-face
515   '((((class color)
516       (background dark))
517      (:foreground "SeaGreen"))
518     (((class color)
519       (background light))
520      (:foreground "maroon"))
521     (t
522      (:bold t)))
523   "Face used for displaying header names."
524   :group 'gnus-article-headers
525   :group 'gnus-article-highlight)
526
527 (defface gnus-header-content-face
528   '((((class color)
529       (background dark))
530      (:foreground "forest green" :italic t))
531     (((class color)
532       (background light))
533      (:foreground "indianred4" :italic t))
534     (t
535      (:italic t)))  "Face used for displaying header content."
536   :group 'gnus-article-headers
537   :group 'gnus-article-highlight)
538
539 (defcustom gnus-header-face-alist
540   '(("From" nil gnus-header-from-face)
541     ("Subject" nil gnus-header-subject-face)
542     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
543     ("" gnus-header-name-face gnus-header-content-face))
544   "*Controls highlighting of article header.
545
546 An alist of the form (HEADER NAME CONTENT).
547
548 HEADER is a regular expression which should match the name of an
549 header header and NAME and CONTENT are either face names or nil.
550
551 The name of each header field will be displayed using the face
552 specified by the first element in the list where HEADER match the
553 header name and NAME is non-nil.  Similarly, the content will be
554 displayed by the first non-nil matching CONTENT face."
555   :group 'gnus-article-headers
556   :group 'gnus-article-highlight
557   :type '(repeat (list (regexp :tag "Header")
558                        (choice :tag "Name"
559                                (item :tag "skip" nil)
560                                (face :value default))
561                        (choice :tag "Content"
562                                (item :tag "skip" nil)
563                                (face :value default)))))
564
565 (defcustom gnus-article-decode-hook nil
566   "*Hook run to decode charsets in articles."
567   :group 'gnus-article-headers
568   :type 'hook)
569
570 (defcustom gnus-display-mime-function 'gnus-display-mime
571   "Function to display MIME articles."
572   :group 'gnus-article-mime
573   :type 'function)
574
575 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
576   "Function used to decode headers.")
577
578 (defvar gnus-article-dumbquotes-map
579   '(("\202" ",")
580     ("\203" "f")
581     ("\204" ",,")
582     ("\205" "...")
583     ("\213" "<")
584     ("\214" "OE")
585     ("\205" "...")
586     ("\221" "`")
587     ("\222" "'")
588     ("\223" "``")
589     ("\224" "''")
590     ("\225" "*")
591     ("\226" "-")
592     ("\227" "-")
593     ("\231" "(TM)")
594     ("\233" ">")
595     ("\234" "oe")
596     ("\264" "'"))
597   "Table for MS-to-Latin1 translation.")
598
599 (defcustom gnus-ignored-mime-types nil
600   "List of MIME types that should be ignored by Gnus."
601   :group 'gnus-article-mime
602   :type '(repeat regexp))
603
604 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
605   "List of MIME types that should not be given buttons when rendered."
606   :group 'gnus-article-mime
607   :type '(repeat regexp))
608
609 (defcustom gnus-treat-body-highlight-signature t
610   "Highlight the signature."
611   :group 'gnus-article
612   :type '(choice (const :tag "Off" nil)
613                  (const :tag "On" t)
614                  (const :tag "Last" last)
615                  (integer :tag "Less")
616                  (sexp :tag "Predicate")))
617
618 (defcustom gnus-article-mime-part-function nil
619   "Function called with a MIME handle as the argument."
620   :group 'gnus-article-mime
621   :type 'function)
622
623 ;;; Internal variables
624
625 (defvar gnus-article-mime-handle-alist-1 nil)
626 (defvar gnus-treatment-function-alist
627   '((gnus-treat-body-highlight-signature gnus-article-highlight-signature nil)
628     ))
629
630 (defvar gnus-article-mime-handle-alist nil)
631 (defvar article-lapsed-timer nil)
632 (defvar gnus-article-current-summary nil)
633
634 (defvar gnus-article-mode-syntax-table
635   (let ((table (copy-syntax-table text-mode-syntax-table)))
636     (modify-syntax-entry ?- "w" table)
637     (modify-syntax-entry ?> ")" table)
638     (modify-syntax-entry ?< "(" table)
639     table)
640   "Syntax table used in article mode buffers.
641 Initialized from `text-mode-syntax-table.")
642
643 (defvar gnus-save-article-buffer nil)
644
645 (defvar gnus-article-mode-line-format-alist
646   (nconc '((?w (gnus-article-wash-status) ?s)
647            (?m (gnus-article-mime-part-status) ?s))
648          gnus-summary-mode-line-format-alist))
649
650 (defvar gnus-number-of-articles-to-be-saved nil)
651
652 (defvar gnus-inhibit-hiding nil)
653
654 (defsubst gnus-article-hide-text (b e props)
655   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
656   (add-text-properties b e props)
657   (when (memq 'intangible props)
658     (put-text-property
659      (max (1- b) (point-min))
660      b 'intangible (cddr (memq 'intangible props)))))
661
662 (defmacro gnus-with-article (article &rest forms)
663   "Select ARTICLE and perform FORMS in the original article buffer.
664 Then replace the article with the result."
665   `(progn
666      ;; We don't want the article to be marked as read.
667      (let (gnus-mark-article-hook)
668        (gnus-summary-select-article t t nil ,article))
669      (set-buffer gnus-original-article-buffer)
670      ,@forms
671      (if (not (gnus-check-backend-function
672                'request-replace-article (car gnus-article-current)))
673          (gnus-message 5 "Read-only group; not replacing")
674        (unless (gnus-request-replace-article
675                 ,article (car gnus-article-current)
676                 (current-buffer) t)
677          (error "Couldn't replace article")))
678      ;; The cache and backlog have to be flushed somewhat.
679      (when gnus-keep-backlog
680        (gnus-backlog-remove-article
681         (car gnus-article-current) (cdr gnus-article-current)))
682      (when gnus-use-cache
683        (gnus-cache-update-article
684         (car gnus-article-current) (cdr gnus-article-current)))))
685
686 (put 'gnus-with-article 'lisp-indent-function 1)
687 (put 'gnus-with-article 'edebug-form-spec '(form body))
688
689 (defsubst gnus-article-unhide-text (b e)
690   "Remove hidden text properties from region between B and E."
691   (remove-text-properties b e gnus-hidden-properties)
692   (when (memq 'intangible gnus-hidden-properties)
693     (put-text-property (max (1- b) (point-min))
694                        b 'intangible nil)))
695
696 (defun gnus-article-hide-text-type (b e type)
697   "Hide text of TYPE between B and E."
698   (gnus-article-hide-text
699    b e (cons 'article-type (cons type gnus-hidden-properties))))
700
701 (defun gnus-article-unhide-text-type (b e type)
702   "Unhide text of TYPE between B and E."
703   (remove-text-properties
704    b e (cons 'article-type (cons type gnus-hidden-properties)))
705   (when (memq 'intangible gnus-hidden-properties)
706     (put-text-property (max (1- b) (point-min))
707                        b 'intangible nil)))
708
709 (defun gnus-article-hide-text-of-type (type)
710   "Hide text of TYPE in the current buffer."
711   (save-excursion
712     (let ((b (point-min))
713           (e (point-max)))
714       (while (setq b (text-property-any b e 'article-type type))
715         (add-text-properties b (incf b) gnus-hidden-properties)))))
716
717 (defun gnus-article-delete-text-of-type (type)
718   "Delete text of TYPE in the current buffer."
719   (save-excursion
720     (let ((b (point-min)))
721       (while (setq b (text-property-any b (point-max) 'article-type type))
722         (delete-region
723          b (or (text-property-not-all b (point-max) 'article-type type)
724                (point-max)))))))
725
726 (defun gnus-article-delete-invisible-text ()
727   "Delete all invisible text in the current buffer."
728   (save-excursion
729     (let ((b (point-min)))
730       (while (setq b (text-property-any b (point-max) 'invisible t))
731         (delete-region
732          b (or (text-property-not-all b (point-max) 'invisible t)
733                (point-max)))))))
734
735 (defun gnus-article-text-type-exists-p (type)
736   "Say whether any text of type TYPE exists in the buffer."
737   (text-property-any (point-min) (point-max) 'article-type type))
738
739 (defsubst gnus-article-header-rank ()
740   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
741   (let ((list gnus-sorted-header-list)
742         (i 0))
743     (while list
744       (when (looking-at (car list))
745         (setq list nil))
746       (setq list (cdr list))
747       (incf i))
748     i))
749
750 (defun article-hide-headers (&optional arg delete)
751   "Toggle whether to hide unwanted headers and possibly sort them as well.
752 If given a negative prefix, always show; if given a positive prefix,
753 always hide."
754   (interactive (gnus-article-hidden-arg))
755   (current-buffer)
756   (if (gnus-article-check-hidden-text 'headers arg)
757       ;; Show boring headers as well.
758       (gnus-article-show-hidden-text 'boring-headers)
759     ;; This function might be inhibited.
760     (unless gnus-inhibit-hiding
761       (save-excursion
762         (save-restriction
763           (let ((buffer-read-only nil)
764                 (case-fold-search t)
765                 (props (nconc (list 'article-type 'headers)
766                               gnus-hidden-properties))
767                 (max (1+ (length gnus-sorted-header-list)))
768                 (ignored (when (not gnus-visible-headers)
769                            (cond ((stringp gnus-ignored-headers)
770                                   gnus-ignored-headers)
771                                  ((listp gnus-ignored-headers)
772                                   (mapconcat 'identity gnus-ignored-headers
773                                              "\\|")))))
774                 (visible
775                  (cond ((stringp gnus-visible-headers)
776                         gnus-visible-headers)
777                        ((and gnus-visible-headers
778                              (listp gnus-visible-headers))
779                         (mapconcat 'identity gnus-visible-headers "\\|"))))
780                 (inhibit-point-motion-hooks t)
781                 beg)
782             ;; First we narrow to just the headers.
783             (widen)
784             (goto-char (point-min))
785             ;; Hide any "From " lines at the beginning of (mail) articles.
786             (while (looking-at "From ")
787               (forward-line 1))
788             (unless (bobp)
789               (if delete
790                   (delete-region (point-min) (point))
791                 (gnus-article-hide-text (point-min) (point) props)))
792             ;; Then treat the rest of the header lines.
793             (narrow-to-region
794              (point)
795              (if (search-forward "\n\n" nil t) ; if there's a body
796                  (progn (forward-line -1) (point))
797                (point-max)))
798             ;; Then we use the two regular expressions
799             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
800             ;; select which header lines is to remain visible in the
801             ;; article buffer.
802             (goto-char (point-min))
803             (while (re-search-forward "^[^ \t]*:" nil t)
804               (beginning-of-line)
805               ;; Mark the rank of the header.
806               (put-text-property
807                (point) (1+ (point)) 'message-rank
808                (if (or (and visible (looking-at visible))
809                        (and ignored
810                             (not (looking-at ignored))))
811                    (gnus-article-header-rank)
812                  (+ 2 max)))
813               (forward-line 1))
814             (message-sort-headers-1)
815             (when (setq beg (text-property-any
816                              (point-min) (point-max) 'message-rank (+ 2 max)))
817               ;; We make the unwanted headers invisible.
818               (if delete
819                   (delete-region beg (point-max))
820                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
821                 (gnus-article-hide-text-type beg (point-max) 'headers))
822               ;; Work around XEmacs lossage.
823               (put-text-property (point-min) beg 'invisible nil))))))))
824
825 (defun article-hide-boring-headers (&optional arg)
826   "Toggle hiding of headers that aren't very interesting.
827 If given a negative prefix, always show; if given a positive prefix,
828 always hide."
829   (interactive (gnus-article-hidden-arg))
830   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
831              (not gnus-show-all-headers))
832     (save-excursion
833       (save-restriction
834         (let ((buffer-read-only nil)
835               (list gnus-boring-article-headers)
836               (inhibit-point-motion-hooks t)
837               elem)
838           (nnheader-narrow-to-headers)
839           (while list
840             (setq elem (pop list))
841             (goto-char (point-min))
842             (cond
843              ;; Hide empty headers.
844              ((eq elem 'empty)
845               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
846                 (forward-line -1)
847                 (gnus-article-hide-text-type
848                  (progn (beginning-of-line) (point))
849                  (progn
850                    (end-of-line)
851                    (if (re-search-forward "^[^ \t]" nil t)
852                        (match-beginning 0)
853                      (point-max)))
854                  'boring-headers)))
855              ;; Hide boring Newsgroups header.
856              ((eq elem 'newsgroups)
857               (when (equal (gnus-fetch-field "newsgroups")
858                            (gnus-group-real-name
859                             (if (boundp 'gnus-newsgroup-name)
860                                 gnus-newsgroup-name
861                               "")))
862                 (gnus-article-hide-header "newsgroups")))
863              ((eq elem 'followup-to)
864               (when (equal (message-fetch-field "followup-to")
865                            (message-fetch-field "newsgroups"))
866                 (gnus-article-hide-header "followup-to")))
867              ((eq elem 'reply-to)
868               (let ((from (message-fetch-field "from"))
869                     (reply-to (message-fetch-field "reply-to")))
870                 (when (and
871                        from reply-to
872                        (ignore-errors
873                          (equal
874                           (nth 1 (funcall gnus-extract-address-components from))
875                           (nth 1 (funcall gnus-extract-address-components reply-to)))))
876                   (gnus-article-hide-header "reply-to"))))
877              ((eq elem 'date)
878               (let ((date (message-fetch-field "date")))
879                 (when (and date
880                            (< (days-between (current-time-string) date)
881                               4))
882                   (gnus-article-hide-header "date"))))
883              ((eq elem 'long-to)
884               (let ((to (message-fetch-field "to")))
885                 (when (> (length to) 1024)
886                   (gnus-article-hide-header "to"))))
887              ((eq elem 'many-to)
888               (let ((to-count 0))
889                 (goto-char (point-min))
890                 (while (re-search-forward "^to:" nil t)
891                   (setq to-count (1+ to-count)))
892                 (when (> to-count 1)
893                   (while (> to-count 0)
894                     (goto-char (point-min))
895                     (save-restriction
896                       (re-search-forward "^to:" nil nil to-count)
897                       (forward-line -1)
898                       (narrow-to-region (point) (point-max))
899                       (gnus-article-hide-header "to"))
900                     (setq to-count (1- to-count)))))))))))))
901
902 (defun gnus-article-hide-header (header)
903   (save-excursion
904     (goto-char (point-min))
905     (when (re-search-forward (concat "^" header ":") nil t)
906       (gnus-article-hide-text-type
907        (progn (beginning-of-line) (point))
908        (progn
909          (end-of-line)
910          (if (re-search-forward "^[^ \t]" nil t)
911              (match-beginning 0)
912            (point-max)))
913        'boring-headers))))
914
915 (defvar gnus-article-normalized-header-length 40
916   "Length of normalized headers.")
917
918 (defun article-normalize-headers ()
919   "Make all header lines 40 characters long."
920   (interactive)
921   (let ((buffer-read-only nil)
922         column)
923     (save-excursion
924       (save-restriction
925         (message-narrow-to-head)
926         (while (not (eobp))
927           (cond
928            ((< (setq column (- (gnus-point-at-eol) (point)))
929                gnus-article-normalized-header-length)
930             (end-of-line)
931             (insert (make-string
932                      (- gnus-article-normalized-header-length column)
933                      ? )))
934            ((> column gnus-article-normalized-header-length)
935             (gnus-put-text-property
936              (progn
937                (forward-char gnus-article-normalized-header-length)
938                (point))
939              (gnus-point-at-eol)
940              'invisible t))
941            (t
942             ;; Do nothing.
943             ))
944           (forward-line 1))))))
945
946 (defun article-treat-dumbquotes ()
947   "Translate M******** sm*rtq**t*s into proper text.
948 Note that this function guesses whether a character is a sm*rtq**t* or
949 not, so it should only be used interactively."
950   (interactive)
951   (article-translate-strings gnus-article-dumbquotes-map))
952
953 (defun article-translate-characters (from to)
954   "Translate all characters in the body of the article according to FROM and TO.
955 FROM is a string of characters to translate from; to is a string of
956 characters to translate to."
957   (save-excursion
958     (when (article-goto-body)
959       (let ((buffer-read-only nil)
960             (x (make-string 225 ?x))
961             (i -1))
962         (while (< (incf i) (length x))
963           (aset x i i))
964         (setq i 0)
965         (while (< i (length from))
966           (aset x (aref from i) (aref to i))
967           (incf i))
968         (translate-region (point) (point-max) x)))))
969
970 (defun article-translate-strings (map)
971   "Translate all string in the body of the article according to MAP.
972 MAP is an alist where the elements are on the form (\"from\" \"to\")."
973   (save-excursion
974     (when (article-goto-body)
975       (let ((buffer-read-only nil)
976             elem)
977         (while (setq elem (pop map))
978           (save-excursion
979             (while (search-forward (car elem) nil t)
980               (replace-match (cadr elem)))))))))
981
982 (defun article-treat-overstrike ()
983   "Translate overstrikes into bold text."
984   (interactive)
985   (save-excursion
986     (when (article-goto-body)
987       (let ((buffer-read-only nil))
988         (while (search-forward "\b" nil t)
989           (let ((next (char-after))
990                 (previous (char-after (- (point) 2))))
991             ;; We do the boldification/underlining by hiding the
992             ;; overstrikes and putting the proper text property
993             ;; on the letters.
994             (cond
995              ((eq next previous)
996               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
997               (put-text-property (point) (1+ (point)) 'face 'bold))
998              ((eq next ?_)
999               (gnus-article-hide-text-type
1000                (1- (point)) (1+ (point)) 'overstrike)
1001               (put-text-property
1002                (- (point) 2) (1- (point)) 'face 'underline))
1003              ((eq previous ?_)
1004               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1005               (put-text-property
1006                (point) (1+ (point)) 'face 'underline)))))))))
1007
1008 (defun article-fill ()
1009   "Format too long lines."
1010   (interactive)
1011   (save-excursion
1012     (let ((buffer-read-only nil))
1013       (widen)
1014       (article-goto-body)
1015       (end-of-line 1)
1016       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
1017             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
1018             (adaptive-fill-mode t))
1019         (while (not (eobp))
1020           (and (>= (current-column) (min fill-column (window-width)))
1021                (/= (preceding-char) ?:)
1022                (fill-paragraph nil))
1023           (end-of-line 2))))))
1024
1025 (defun article-remove-cr ()
1026   "Translate CRLF pairs into LF, and then CR into LF.."
1027   (interactive)
1028   (save-excursion
1029     (let ((buffer-read-only nil))
1030       (goto-char (point-min))
1031       (while (search-forward "\r$" nil t)
1032         (replace-match "" t t))
1033       (goto-char (point-min))
1034       (while (search-forward "\r" nil t)
1035         (replace-match "\n" t t)))))
1036
1037 (defun article-remove-trailing-blank-lines ()
1038   "Remove all trailing blank lines from the article."
1039   (interactive)
1040   (save-excursion
1041     (let ((buffer-read-only nil))
1042       (goto-char (point-max))
1043       (delete-region
1044        (point)
1045        (progn
1046          (while (and (not (bobp))
1047                      (looking-at "^[ \t]*$")
1048                      (not (gnus-annotation-in-region-p
1049                            (point) (gnus-point-at-eol))))
1050            (forward-line -1))
1051          (forward-line 1)
1052          (point))))))
1053
1054 (defun article-display-x-face (&optional force)
1055   "Look for an X-Face header and display it if present."
1056   (interactive (list 'force))
1057   (save-excursion
1058     ;; Delete the old process, if any.
1059     (when (process-status "article-x-face")
1060       (delete-process "article-x-face"))
1061     (let ((inhibit-point-motion-hooks t)
1062           (case-fold-search t)
1063           from last)
1064       (save-restriction
1065         (nnheader-narrow-to-headers)
1066         (setq from (message-fetch-field "from"))
1067         (goto-char (point-min))
1068         (while (and gnus-article-x-face-command
1069                     (not last)
1070                     (or force
1071                         ;; Check whether this face is censored.
1072                         (not gnus-article-x-face-too-ugly)
1073                         (and gnus-article-x-face-too-ugly from
1074                              (not (string-match gnus-article-x-face-too-ugly
1075                                                 from))))
1076                     ;; Has to be present.
1077                     (re-search-forward "^X-Face: " nil t))
1078           ;; This used to try to do multiple faces (`while' instead of
1079           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1080           ;; work (b) it can crash some versions of Emacs (c) are
1081           ;; multiple faces really something to encourage?
1082           (when (stringp gnus-article-x-face-command)
1083             (setq last t))
1084           ;; We now have the area of the buffer where the X-Face is stored.
1085           (save-excursion
1086             (let ((beg (point))
1087                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1088               ;; We display the face.
1089               (if (symbolp gnus-article-x-face-command)
1090                   ;; The command is a lisp function, so we call it.
1091                   (if (gnus-functionp gnus-article-x-face-command)
1092                       (funcall gnus-article-x-face-command beg end)
1093                     (error "%s is not a function" gnus-article-x-face-command))
1094                 ;; The command is a string, so we interpret the command
1095                 ;; as a, well, command, and fork it off.
1096                 (let ((process-connection-type nil))
1097                   (process-kill-without-query
1098                    (start-process
1099                     "article-x-face" nil shell-file-name shell-command-switch
1100                     gnus-article-x-face-command))
1101                   (process-send-region "article-x-face" beg end)
1102                   (process-send-eof "article-x-face"))))))))))
1103
1104 (defun article-decode-mime-words ()
1105   "Decode all MIME-encoded words in the article."
1106   (interactive)
1107   (save-excursion
1108     (set-buffer gnus-article-buffer)
1109     (let ((inhibit-point-motion-hooks t)
1110           buffer-read-only
1111           (rfc2047-default-charset gnus-newsgroup-default-charset)
1112           (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
1113       (mail-decode-encoded-word-region (point-min) (point-max)))))
1114
1115 (defun article-decode-charset (&optional prompt)
1116   "Decode charset-encoded text in the article.
1117 If PROMPT (the prefix), prompt for a coding system to use."
1118   (interactive "P")
1119   (save-excursion
1120     (save-restriction
1121       (message-narrow-to-head)
1122       (let* ((inhibit-point-motion-hooks t)
1123              (case-fold-search t)
1124              (ct (message-fetch-field "Content-Type" t))
1125              (cte (message-fetch-field "Content-Transfer-Encoding" t))
1126              (ctl (and ct (condition-case ()
1127                               (mail-header-parse-content-type ct)
1128                             (error nil))))
1129              (charset (cond
1130                        (prompt
1131                         (mm-read-coding-system "Charset to decode: "))
1132                        (ctl
1133                         (mail-content-type-get ctl 'charset))))
1134              (rfc2047-default-charset gnus-newsgroup-default-charset)
1135              (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced)
1136              buffer-read-only)
1137         (goto-char (point-max))
1138         (widen)
1139         (forward-line 1)
1140         (narrow-to-region (point) (point-max))
1141         (when (and (or (not ctl)
1142                        (equal (car ctl) "text/plain"))
1143                    (not (mm-uu-test)))
1144           (mm-decode-body
1145            charset (and cte (intern (downcase
1146                                      (gnus-strip-whitespace cte))))
1147            (car ctl)))))))
1148
1149 (defun article-decode-encoded-words ()
1150   "Remove encoded-word encoding from headers."
1151   (let (buffer-read-only)
1152     (let ((charset (save-excursion
1153                      (set-buffer gnus-summary-buffer)
1154                      default-mime-charset)))
1155       (mime-decode-header-in-buffer charset)
1156       )))
1157
1158 (defun article-de-quoted-unreadable (&optional force)
1159   "Translate a quoted-printable-encoded article.
1160 If FORCE, decode the article whether it is marked as quoted-printable
1161 or not."
1162   (interactive (list 'force))
1163   (save-excursion
1164     (let ((buffer-read-only nil)
1165           (type (gnus-fetch-field "content-transfer-encoding"))
1166           (charset
1167            (or gnus-newsgroup-default-charset mm-default-coding-system))
1168           (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
1169       (when (or force
1170                 (and type (string-match "quoted-printable" (downcase type))))
1171         (article-goto-body)
1172         (save-restriction
1173           (narrow-to-region (point) (point-max))
1174           (quoted-printable-decode-region (point-min) (point-max))
1175           (when charset
1176             (mm-decode-body charset)))))))
1177
1178 (defun article-hide-pgp (&optional arg)
1179   "Toggle hiding of any PGP headers and signatures in the current article.
1180 If given a negative prefix, always show; if given a positive prefix,
1181 always hide."
1182   (interactive (gnus-article-hidden-arg))
1183   (unless (gnus-article-check-hidden-text 'pgp arg)
1184     (save-excursion
1185       (let ((inhibit-point-motion-hooks t)
1186             buffer-read-only beg end)
1187         (widen)
1188         (goto-char (point-min))
1189         ;; Hide the "header".
1190         (when (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1191           (delete-region (1+ (match-beginning 0)) (match-end 0))
1192           ;; PGP 5 and GNU PG add a `Hash: <>' comment, hide that too
1193           (when (looking-at "Hash:.*$")
1194             (delete-region (point) (1+ (gnus-point-at-eol))))
1195           (setq beg (point))
1196           ;; Hide the actual signature.
1197           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1198                (setq end (1+ (match-beginning 0)))
1199                (delete-region
1200                 end
1201                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1202                     (match-end 0)
1203                   ;; Perhaps we shouldn't hide to the end of the buffer
1204                   ;; if there is no end to the signature?
1205                   (point-max))))
1206           ;; Hide "- " PGP quotation markers.
1207           (when (and beg end)
1208             (narrow-to-region beg end)
1209             (goto-char (point-min))
1210             (while (re-search-forward "^- " nil t)
1211               (delete-region
1212                (match-beginning 0) (match-end 0)))
1213             (widen))
1214           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1215
1216 (defun article-hide-pem (&optional arg)
1217   "Toggle hiding of any PEM headers and signatures in the current article.
1218 If given a negative prefix, always show; if given a positive prefix,
1219 always hide."
1220   (interactive (gnus-article-hidden-arg))
1221   (unless (gnus-article-check-hidden-text 'pem arg)
1222     (save-excursion
1223       (let (buffer-read-only end)
1224         (widen)
1225         (goto-char (point-min))
1226         ;; hide the horrendously ugly "header".
1227         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1228                              nil
1229                              t)
1230              (setq end (1+ (match-beginning 0)))
1231              (gnus-article-hide-text-type
1232               end
1233               (if (search-forward "\n\n" nil t)
1234                   (match-end 0)
1235                 (point-max))
1236               'pem))
1237         ;; hide the trailer as well
1238         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1239                              nil
1240                              t)
1241              (gnus-article-hide-text-type
1242               (match-beginning 0) (match-end 0) 'pem))))))
1243
1244 (defun article-hide-signature (&optional arg)
1245   "Hide the signature in the current article.
1246 If given a negative prefix, always show; if given a positive prefix,
1247 always hide."
1248   (interactive (gnus-article-hidden-arg))
1249   (unless (gnus-article-check-hidden-text 'signature arg)
1250     (save-excursion
1251       (save-restriction
1252         (let ((buffer-read-only nil))
1253           (when (gnus-article-narrow-to-signature)
1254             (gnus-article-hide-text-type
1255              (point-min) (point-max) 'signature)))))))
1256
1257 (defun article-strip-leading-blank-lines ()
1258   "Remove all blank lines from the beginning of the article."
1259   (interactive)
1260   (save-excursion
1261     (let ((inhibit-point-motion-hooks t)
1262           buffer-read-only)
1263       (when (article-goto-body)
1264         (while (and (not (eobp))
1265                     (looking-at "[ \t]*$"))
1266           (gnus-delete-line))))))
1267
1268 (defun article-goto-body ()
1269   "Place point at the start of the body."  
1270   (goto-char (point-min))
1271   (if (search-forward "\n\n" nil t)
1272       t
1273     (goto-char (point-max))
1274     nil))
1275
1276 (defun article-strip-multiple-blank-lines ()
1277   "Replace consecutive blank lines with one empty line."
1278   (interactive)
1279   (save-excursion
1280     (let ((inhibit-point-motion-hooks t)
1281           buffer-read-only)
1282       ;; First make all blank lines empty.
1283       (article-goto-body)
1284       (while (re-search-forward "^[ \t]+$" nil t)
1285         (unless (gnus-annotation-in-region-p
1286                  (match-beginning 0) (match-end 0))
1287           (replace-match "" nil t)))
1288       ;; Then replace multiple empty lines with a single empty line.
1289       (article-goto-body)
1290       (while (re-search-forward "\n\n\n+" nil t)
1291         (unless (gnus-annotation-in-region-p
1292                  (match-beginning 0) (match-end 0))
1293           (replace-match "\n\n" t t))))))
1294
1295 (defun article-strip-leading-space ()
1296   "Remove all white space from the beginning of the lines in the article."
1297   (interactive)
1298   (save-excursion
1299     (let ((inhibit-point-motion-hooks t)
1300           buffer-read-only)
1301       (article-goto-body)
1302       (while (re-search-forward "^[ \t]+" nil t)
1303         (replace-match "" t t)))))
1304
1305 (defun article-strip-trailing-space ()
1306   "Remove all white space from the end of the lines in the article."
1307   (interactive)
1308   (save-excursion
1309     (let ((inhibit-point-motion-hooks t)
1310           buffer-read-only)
1311       (article-goto-body)
1312       (while (re-search-forward "[ \t]+$" nil t)
1313         (replace-match "" t t)))))
1314
1315 (defun article-strip-blank-lines ()
1316   "Strip leading, trailing and multiple blank lines."
1317   (interactive)
1318   (article-strip-leading-blank-lines)
1319   (article-remove-trailing-blank-lines)
1320   (article-strip-multiple-blank-lines))
1321
1322 (defun article-strip-all-blank-lines ()
1323   "Strip all blank lines."
1324   (interactive)
1325   (save-excursion
1326     (let ((inhibit-point-motion-hooks t)
1327           buffer-read-only)
1328       (article-goto-body)
1329       (while (re-search-forward "^[ \t]*\n" nil t)
1330         (replace-match "" t t)))))
1331
1332 (defun gnus-article-narrow-to-signature ()
1333   "Narrow to the signature; return t if a signature is found, else nil."
1334   (widen)
1335   (let ((inhibit-point-motion-hooks t))
1336     (when (gnus-article-search-signature)
1337       (forward-line 1)
1338       ;; Check whether we have some limits to what we consider
1339       ;; to be a signature.
1340       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1341                       (list gnus-signature-limit)))
1342             limit limited)
1343         (while (setq limit (pop limits))
1344           (if (or (and (integerp limit)
1345                        (< (- (point-max) (point)) limit))
1346                   (and (floatp limit)
1347                        (< (count-lines (point) (point-max)) limit))
1348                   (and (gnus-functionp limit)
1349                        (funcall limit))
1350                   (and (stringp limit)
1351                        (not (re-search-forward limit nil t))))
1352               ()                        ; This limit did not succeed.
1353             (setq limited t
1354                   limits nil)))
1355         (unless limited
1356           (narrow-to-region (point) (point-max))
1357           t)))))
1358
1359 (defun gnus-article-search-signature ()
1360   "Search the current buffer for the signature separator.
1361 Put point at the beginning of the signature separator."
1362   (let ((cur (point)))
1363     (goto-char (point-max))
1364     (if (if (stringp gnus-signature-separator)
1365             (re-search-backward gnus-signature-separator nil t)
1366           (let ((seps gnus-signature-separator))
1367             (while (and seps
1368                         (not (re-search-backward (car seps) nil t)))
1369               (pop seps))
1370             seps))
1371         t
1372       (goto-char cur)
1373       nil)))
1374
1375 (eval-and-compile
1376   (autoload 'w3-display "w3-parse")
1377   (autoload 'w3-do-setup "w3" "" t)
1378   (autoload 'w3-region "w3-display" "" t))
1379
1380 (defun gnus-article-treat-html ()
1381   "Render HTML."
1382   (interactive)
1383   (let ((cbuf (current-buffer)))
1384     (set-buffer gnus-article-buffer)
1385     (let (buf buffer-read-only b e)
1386       (w3-do-setup)
1387       (goto-char (point-min))
1388       (narrow-to-region
1389        (if (search-forward "\n\n" nil t)
1390            (setq b (point))
1391          (point-max))
1392        (setq e (point-max)))
1393       (with-temp-buffer
1394         (insert-buffer-substring gnus-article-buffer b e)
1395         (require 'url)
1396         (save-window-excursion
1397           (w3-region (point-min) (point-max))
1398           (setq buf (buffer-substring-no-properties (point-min) (point-max)))))
1399       (when buf
1400         (delete-region (point-min) (point-max))
1401         (insert buf))
1402       (widen)
1403       (goto-char (point-min))
1404       (set-window-start (get-buffer-window (current-buffer)) (point-min))
1405       (set-buffer cbuf))))
1406
1407 (defun gnus-article-hidden-arg ()
1408   "Return the current prefix arg as a number, or 0 if no prefix."
1409   (list (if current-prefix-arg
1410             (prefix-numeric-value current-prefix-arg)
1411           0)))
1412
1413 (defun gnus-article-check-hidden-text (type arg)
1414   "Return nil if hiding is necessary.
1415 Arg can be nil or a number.  Nil and positive means hide, negative
1416 means show, 0 means toggle."
1417   (save-excursion
1418     (save-restriction
1419       (widen)
1420       (let ((hide (gnus-article-hidden-text-p type)))
1421         (cond
1422          ((or (null arg)
1423               (> arg 0))
1424           nil)
1425          ((< arg 0)
1426           (gnus-article-show-hidden-text type))
1427          (t
1428           (if (eq hide 'hidden)
1429               (gnus-article-show-hidden-text type)
1430             nil)))))))
1431
1432 (defun gnus-article-hidden-text-p (type)
1433   "Say whether the current buffer contains hidden text of type TYPE."
1434   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1435     (while (and pos
1436                 (not (get-text-property pos 'invisible)))
1437       (setq pos
1438             (text-property-any (1+ pos) (point-max) 'article-type type)))
1439     (if pos
1440         'hidden
1441       nil)))
1442
1443 (defun gnus-article-show-hidden-text (type &optional hide)
1444   "Show all hidden text of type TYPE.
1445 If HIDE, hide the text instead."
1446   (save-excursion
1447     (let ((buffer-read-only nil)
1448           (inhibit-point-motion-hooks t)
1449           (end (point-min))
1450           beg)
1451       (while (setq beg (text-property-any end (point-max) 'article-type type))
1452         (goto-char beg)
1453         (setq end (or
1454                    (text-property-not-all beg (point-max) 'article-type type)
1455                    (point-max)))
1456         (if hide
1457             (gnus-article-hide-text beg end gnus-hidden-properties)
1458           (gnus-article-unhide-text beg end))
1459         (goto-char end))
1460       t)))
1461
1462 (defconst article-time-units
1463   `((year . ,(* 365.25 24 60 60))
1464     (week . ,(* 7 24 60 60))
1465     (day . ,(* 24 60 60))
1466     (hour . ,(* 60 60))
1467     (minute . 60)
1468     (second . 1))
1469   "Mapping from time units to seconds.")
1470
1471 (defun article-date-ut (&optional type highlight header)
1472   "Convert DATE date to universal time in the current article.
1473 If TYPE is `local', convert to local time; if it is `lapsed', output
1474 how much time has lapsed since DATE."
1475   (interactive (list 'ut t))
1476   (let* ((header (or header
1477                      (mail-header-date gnus-current-headers)
1478                      (message-fetch-field "date")
1479                      ""))
1480          (date (if (vectorp header) (mail-header-date header)
1481                  header))
1482          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1483          (inhibit-point-motion-hooks t)
1484          bface eface newline)
1485     (when (and date (not (string= date "")))
1486       (save-excursion
1487         (save-restriction
1488           (nnheader-narrow-to-headers)
1489           (let ((buffer-read-only nil))
1490             ;; Delete any old Date headers.
1491             (if (re-search-forward date-regexp nil t)
1492                 (progn
1493                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1494                         eface (get-text-property (1- (gnus-point-at-eol))
1495                                                  'face))
1496                   (delete-region (progn (beginning-of-line) (point))
1497                                  (progn (end-of-line) (point)))
1498                   (beginning-of-line))
1499               (goto-char (point-max))
1500               (setq newline t))
1501             (insert (article-make-date-line date type))
1502             ;; Do highlighting.
1503             (beginning-of-line)
1504             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1505               (put-text-property (match-beginning 1) (1+ (match-end 1))
1506                                  'face bface)
1507               (put-text-property (match-beginning 2) (match-end 2)
1508                                  'face eface))
1509             (when newline
1510               (end-of-line)
1511               (insert "\n"))))))))
1512
1513 (defun article-make-date-line (date type)
1514   "Return a DATE line of TYPE."
1515   (let ((time (condition-case ()
1516                   (date-to-time date)
1517                 (error '(0 0)))))
1518     (cond
1519      ;; Convert to the local timezone.  We have to slap a
1520      ;; `condition-case' round the calls to the timezone
1521      ;; functions since they aren't particularly resistant to
1522      ;; buggy dates.
1523      ((eq type 'local)
1524       (let ((tz (car (current-time-zone))))
1525         (format "Date: %s %s%04d" (current-time-string time)
1526                 (if (> tz 0) "+" "-") (abs (/ tz 36)))))
1527      ;; Convert to Universal Time.
1528      ((eq type 'ut)
1529       (concat "Date: "
1530               (current-time-string
1531                (let* ((e (parse-time-string date))
1532                      (tm (apply 'encode-time e))
1533                      (ms (car tm))
1534                      (ls (- (cadr tm) (car (current-time-zone)))))
1535                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1536                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1537                        (t (list ms ls)))))
1538               " UT"))
1539      ;; Get the original date from the article.
1540      ((eq type 'original)
1541       (concat "Date: " (if (string-match "\n+$" date)
1542                            (substring date 0 (match-beginning 0))
1543                          date)))
1544      ;; Let the user define the format.
1545      ((eq type 'user)
1546       (if (gnus-functionp gnus-article-time-format)
1547           (funcall gnus-article-time-format time)
1548         (concat
1549          "Date: "
1550          (format-time-string gnus-article-time-format time))))
1551      ;; ISO 8601.
1552      ((eq type 'iso8601)
1553       (concat
1554        "Date: "
1555        (format-time-string "%Y%M%DT%h%m%s" time)))
1556      ;; Do an X-Sent lapsed format.
1557      ((eq type 'lapsed)
1558       ;; If the date is seriously mangled, the timezone functions are
1559       ;; liable to bug out, so we ignore all errors.
1560       (let* ((now (current-time))
1561              (real-time (subtract-time now time))
1562              (real-sec (and real-time
1563                             (+ (* (float (car real-time)) 65536)
1564                                (cadr real-time))))
1565              (sec (and real-time (abs real-sec)))
1566              num prev)
1567         (cond
1568          ((null real-time)
1569           "X-Sent: Unknown")
1570          ((zerop sec)
1571           "X-Sent: Now")
1572          (t
1573           (concat
1574            "X-Sent: "
1575            ;; This is a bit convoluted, but basically we go
1576            ;; through the time units for years, weeks, etc,
1577            ;; and divide things to see whether that results
1578            ;; in positive answers.
1579            (mapconcat
1580             (lambda (unit)
1581               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1582                   ;; The (remaining) seconds are too few to
1583                   ;; be divided into this time unit.
1584                   ""
1585                 ;; It's big enough, so we output it.
1586                 (setq sec (- sec (* num (cdr unit))))
1587                 (prog1
1588                     (concat (if prev ", " "") (int-to-string
1589                                                (floor num))
1590                             " " (symbol-name (car unit))
1591                             (if (> num 1) "s" ""))
1592                   (setq prev t))))
1593             article-time-units "")
1594            ;; If dates are odd, then it might appear like the
1595            ;; article was sent in the future.
1596            (if (> real-sec 0)
1597                " ago"
1598              " in the future"))))))
1599      (t
1600       (error "Unknown conversion type: %s" type)))))
1601
1602 (defun article-date-local (&optional highlight)
1603   "Convert the current article date to the local timezone."
1604   (interactive (list t))
1605   (article-date-ut 'local highlight))
1606
1607 (defun article-date-original (&optional highlight)
1608   "Convert the current article date to what it was originally.
1609 This is only useful if you have used some other date conversion
1610 function and want to see what the date was before converting."
1611   (interactive (list t))
1612   (article-date-ut 'original highlight))
1613
1614 (defun article-date-lapsed (&optional highlight)
1615   "Convert the current article date to time lapsed since it was sent."
1616   (interactive (list t))
1617   (article-date-ut 'lapsed highlight))
1618
1619 (defun article-update-date-lapsed ()
1620   "Function to be run from a timer to update the lapsed time line."
1621   (let (deactivate-mark)
1622     (save-excursion
1623       (ignore-errors
1624         (walk-windows
1625          (lambda (w)
1626            (set-buffer (window-buffer w))
1627            (when (eq major-mode 'gnus-article-mode)
1628              (goto-char (point-min))
1629              (when (re-search-forward "^X-Sent:" nil t)
1630                (article-date-lapsed t)))))))))
1631
1632 (defun gnus-start-date-timer (&optional n)
1633   "Start a timer to update the X-Sent header in the article buffers.
1634 The numerical prefix says how frequently (in seconds) the function
1635 is to run."
1636   (interactive "p")
1637   (unless n
1638     (setq n 1))
1639   (gnus-stop-date-timer)
1640   (setq article-lapsed-timer
1641         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1642
1643 (defun gnus-stop-date-timer ()
1644   "Stop the X-Sent timer."
1645   (interactive)
1646   (when article-lapsed-timer
1647     (nnheader-cancel-timer article-lapsed-timer)
1648     (setq article-lapsed-timer nil)))
1649
1650 (defun article-date-user (&optional highlight)
1651   "Convert the current article date to the user-defined format.
1652 This format is defined by the `gnus-article-time-format' variable."
1653   (interactive (list t))
1654   (article-date-ut 'user highlight))
1655
1656 (defun article-date-iso8601 (&optional highlight)
1657   "Convert the current article date to ISO8601."
1658   (interactive (list t))
1659   (article-date-ut 'iso8601 highlight))
1660
1661 (defun article-show-all ()
1662   "Show all hidden text in the article buffer."
1663   (interactive)
1664   (save-excursion
1665     (let ((buffer-read-only nil))
1666       (gnus-article-unhide-text (point-min) (point-max)))))
1667
1668 (defun article-emphasize (&optional arg)
1669   "Emphasize text according to `gnus-emphasis-alist'."
1670   (interactive (gnus-article-hidden-arg))
1671   (unless (gnus-article-check-hidden-text 'emphasis arg)
1672     (save-excursion
1673       (let ((alist gnus-emphasis-alist)
1674             (buffer-read-only nil)
1675             (props (append '(article-type emphasis)
1676                            gnus-hidden-properties))
1677             regexp elem beg invisible visible face)
1678         (article-goto-body)
1679         (setq beg (point))
1680         (while (setq elem (pop alist))
1681           (goto-char beg)
1682           (setq regexp (car elem)
1683                 invisible (nth 1 elem)
1684                 visible (nth 2 elem)
1685                 face (nth 3 elem))
1686           (while (re-search-forward regexp nil t)
1687             (when (and (match-beginning visible) (match-beginning invisible))
1688               (gnus-article-hide-text
1689                (match-beginning invisible) (match-end invisible) props)
1690               (gnus-article-unhide-text-type
1691                (match-beginning visible) (match-end visible) 'emphasis)
1692               (gnus-put-text-property-excluding-newlines
1693                (match-beginning visible) (match-end visible) 'face face)
1694               (goto-char (match-end invisible)))))))))
1695
1696 (defvar gnus-summary-article-menu)
1697 (defvar gnus-summary-post-menu)
1698
1699 ;;; Saving functions.
1700
1701 (defun gnus-article-save (save-buffer file &optional num)
1702   "Save the currently selected article."
1703   (unless gnus-save-all-headers
1704     ;; Remove headers according to `gnus-saved-headers'.
1705     (let ((gnus-visible-headers
1706            (or gnus-saved-headers gnus-visible-headers))
1707           (gnus-article-buffer save-buffer))
1708       (save-excursion
1709         (set-buffer save-buffer)
1710         (article-hide-headers 1 t))))
1711   (save-window-excursion
1712     (if (not gnus-default-article-saver)
1713         (error "No default saver is defined")
1714       ;; !!! Magic!  The saving functions all save
1715       ;; `gnus-save-article-buffer' (or so they think), but we
1716       ;; bind that variable to our save-buffer.
1717       (set-buffer gnus-article-buffer)
1718       (let* ((gnus-save-article-buffer save-buffer)
1719              (filename
1720               (cond
1721                ((not gnus-prompt-before-saving) 'default)
1722                ((eq gnus-prompt-before-saving 'always) nil)
1723                (t file)))
1724              (gnus-number-of-articles-to-be-saved
1725               (when (eq gnus-prompt-before-saving t)
1726                 num)))                  ; Magic
1727         (set-buffer gnus-article-current-summary)
1728         (funcall gnus-default-article-saver filename)))))
1729
1730 (defun gnus-read-save-file-name (prompt &optional filename
1731                                         function group headers variable)
1732   (let ((default-name
1733           (funcall function group headers (symbol-value variable)))
1734         result)
1735     (setq
1736      result
1737      (cond
1738       ((eq filename 'default)
1739        default-name)
1740       ((eq filename t)
1741        default-name)
1742       (filename filename)
1743       (t
1744        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1745               (prompt
1746                (format prompt
1747                        (if (and gnus-number-of-articles-to-be-saved
1748                                 (> gnus-number-of-articles-to-be-saved 1))
1749                            (format "these %d articles"
1750                                    gnus-number-of-articles-to-be-saved)
1751                          "this article")))
1752               (file
1753                ;; Let the split methods have their say.
1754                (cond
1755                 ;; No split name was found.
1756                 ((null split-name)
1757                  (read-file-name
1758                   (concat prompt " (default "
1759                           (file-name-nondirectory default-name) ") ")
1760                   (file-name-directory default-name)
1761                   default-name))
1762                 ;; A single group name is returned.
1763                 ((stringp split-name)
1764                  (setq default-name
1765                        (funcall function split-name headers
1766                                 (symbol-value variable)))
1767                  (read-file-name
1768                   (concat prompt " (default "
1769                           (file-name-nondirectory default-name) ") ")
1770                   (file-name-directory default-name)
1771                   default-name))
1772                 ;; A single split name was found
1773                 ((= 1 (length split-name))
1774                  (let* ((name (expand-file-name
1775                                (car split-name) gnus-article-save-directory))
1776                         (dir (cond ((file-directory-p name)
1777                                     (file-name-as-directory name))
1778                                    ((file-exists-p name) name)
1779                                    (t gnus-article-save-directory))))
1780                    (read-file-name
1781                     (concat prompt " (default " name ") ")
1782                     dir name)))
1783                 ;; A list of splits was found.
1784                 (t
1785                  (setq split-name (nreverse split-name))
1786                  (let (result)
1787                    (let ((file-name-history
1788                           (nconc split-name file-name-history)))
1789                      (setq result
1790                            (expand-file-name
1791                             (read-file-name
1792                              (concat prompt " (`M-p' for defaults) ")
1793                              gnus-article-save-directory
1794                              (car split-name))
1795                             gnus-article-save-directory)))
1796                    (car (push result file-name-history)))))))
1797          ;; Create the directory.
1798          (gnus-make-directory (file-name-directory file))
1799          ;; If we have read a directory, we append the default file name.
1800          (when (file-directory-p file)
1801            (setq file (concat (file-name-as-directory file)
1802                               (file-name-nondirectory default-name))))
1803          ;; Possibly translate some characters.
1804          (nnheader-translate-file-chars file)))))
1805     (gnus-make-directory (file-name-directory result))
1806     (set variable result)))
1807
1808 (defun gnus-article-archive-name (group)
1809   "Return the first instance of an \"Archive-name\" in the current buffer."
1810   (let ((case-fold-search t))
1811     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1812       (nnheader-concat gnus-article-save-directory
1813                        (match-string 1)))))
1814
1815 (defun gnus-article-nndoc-name (group)
1816   "If GROUP is an nndoc group, return the name of the parent group."
1817   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1818     (gnus-group-get-parameter group 'save-article-group)))
1819
1820 (defun gnus-summary-save-in-rmail (&optional filename)
1821   "Append this article to Rmail file.
1822 Optional argument FILENAME specifies file name.
1823 Directory to save to is default to `gnus-article-save-directory'."
1824   (setq filename (gnus-read-save-file-name
1825                   "Save %s in rmail file:" filename
1826                   gnus-rmail-save-name gnus-newsgroup-name
1827                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1828   (gnus-eval-in-buffer-window gnus-save-article-buffer
1829     (save-excursion
1830       (save-restriction
1831         (widen)
1832         (gnus-output-to-rmail filename))))
1833   filename)
1834
1835 (defun gnus-summary-save-in-mail (&optional filename)
1836   "Append this article to Unix mail file.
1837 Optional argument FILENAME specifies file name.
1838 Directory to save to is default to `gnus-article-save-directory'."
1839   (setq filename (gnus-read-save-file-name
1840                   "Save %s in Unix mail file:" filename
1841                   gnus-mail-save-name gnus-newsgroup-name
1842                   gnus-current-headers 'gnus-newsgroup-last-mail))
1843   (gnus-eval-in-buffer-window gnus-save-article-buffer
1844     (save-excursion
1845       (save-restriction
1846         (widen)
1847         (if (and (file-readable-p filename)
1848                  (mail-file-babyl-p filename))
1849             (rmail-output-to-rmail-file filename t)
1850           (gnus-output-to-mail filename)))))
1851   filename)
1852
1853 (defun gnus-summary-save-in-file (&optional filename overwrite)
1854   "Append this article to file.
1855 Optional argument FILENAME specifies file name.
1856 Directory to save to is default to `gnus-article-save-directory'."
1857   (setq filename (gnus-read-save-file-name
1858                   "Save %s in file:" filename
1859                   gnus-file-save-name gnus-newsgroup-name
1860                   gnus-current-headers 'gnus-newsgroup-last-file))
1861   (gnus-eval-in-buffer-window gnus-save-article-buffer
1862     (save-excursion
1863       (save-restriction
1864         (widen)
1865         (when (and overwrite
1866                    (file-exists-p filename))
1867           (delete-file filename))
1868         (gnus-output-to-file filename))))
1869   filename)
1870
1871 (defun gnus-summary-write-to-file (&optional filename)
1872   "Write this article to a file.
1873 Optional argument FILENAME specifies file name.
1874 The directory to save in defaults to `gnus-article-save-directory'."
1875   (gnus-summary-save-in-file nil t))
1876
1877 (defun gnus-summary-save-body-in-file (&optional filename)
1878   "Append this article body to a file.
1879 Optional argument FILENAME specifies file name.
1880 The directory to save in defaults to `gnus-article-save-directory'."
1881   (setq filename (gnus-read-save-file-name
1882                   "Save %s body in file:" filename
1883                   gnus-file-save-name gnus-newsgroup-name
1884                   gnus-current-headers 'gnus-newsgroup-last-file))
1885   (gnus-eval-in-buffer-window gnus-save-article-buffer
1886     (save-excursion
1887       (save-restriction
1888         (widen)
1889         (when (article-goto-body)
1890           (narrow-to-region (point) (point-max)))
1891         (gnus-output-to-file filename))))
1892   filename)
1893
1894 (defun gnus-summary-save-in-pipe (&optional command)
1895   "Pipe this article to subprocess."
1896   (setq command
1897         (cond ((and (eq command 'default)
1898                     gnus-last-shell-command)
1899                gnus-last-shell-command)
1900               (command command)
1901               (t (read-string
1902                   (format
1903                    "Shell command on %s: "
1904                    (if (and gnus-number-of-articles-to-be-saved
1905                             (> gnus-number-of-articles-to-be-saved 1))
1906                        (format "these %d articles"
1907                                gnus-number-of-articles-to-be-saved)
1908                      "this article"))
1909                   gnus-last-shell-command))))
1910   (when (string-equal command "")
1911     (setq command gnus-last-shell-command))
1912   (gnus-eval-in-buffer-window gnus-article-buffer
1913     (save-restriction
1914       (widen)
1915       (shell-command-on-region (point-min) (point-max) command nil)))
1916   (setq gnus-last-shell-command command))
1917
1918 ;;; Article file names when saving.
1919
1920 (defun gnus-capitalize-newsgroup (newsgroup)
1921   "Capitalize NEWSGROUP name."
1922   (when (not (zerop (length newsgroup)))
1923     (concat (char-to-string (upcase (aref newsgroup 0)))
1924             (substring newsgroup 1))))
1925
1926 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1927   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1928 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1929 Otherwise, it is like ~/News/news/group/num."
1930   (let ((default
1931           (expand-file-name
1932            (concat (if (gnus-use-long-file-name 'not-save)
1933                        (gnus-capitalize-newsgroup newsgroup)
1934                      (gnus-newsgroup-directory-form newsgroup))
1935                    "/" (int-to-string (mail-header-number headers)))
1936            gnus-article-save-directory)))
1937     (if (and last-file
1938              (string-equal (file-name-directory default)
1939                            (file-name-directory last-file))
1940              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1941         default
1942       (or last-file default))))
1943
1944 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1945   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1946 If variable `gnus-use-long-file-name' is non-nil, it is
1947 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1948   (let ((default
1949           (expand-file-name
1950            (concat (if (gnus-use-long-file-name 'not-save)
1951                        newsgroup
1952                      (gnus-newsgroup-directory-form newsgroup))
1953                    "/" (int-to-string (mail-header-number headers)))
1954            gnus-article-save-directory)))
1955     (if (and last-file
1956              (string-equal (file-name-directory default)
1957                            (file-name-directory last-file))
1958              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1959         default
1960       (or last-file default))))
1961
1962 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1963   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1964 If variable `gnus-use-long-file-name' is non-nil, it is
1965 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1966   (or last-file
1967       (expand-file-name
1968        (if (gnus-use-long-file-name 'not-save)
1969            (gnus-capitalize-newsgroup newsgroup)
1970          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1971        gnus-article-save-directory)))
1972
1973 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1974   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1975 If variable `gnus-use-long-file-name' is non-nil, it is
1976 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1977   (or last-file
1978       (expand-file-name
1979        (if (gnus-use-long-file-name 'not-save)
1980            newsgroup
1981          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1982        gnus-article-save-directory)))
1983
1984 (eval-and-compile
1985   (mapcar
1986    (lambda (func)
1987      (let (afunc gfunc)
1988        (if (consp func)
1989            (setq afunc (car func)
1990                  gfunc (cdr func))
1991          (setq afunc func
1992                gfunc (intern (format "gnus-%s" func))))
1993        (fset gfunc
1994              (if (not (fboundp afunc))
1995                  nil
1996                `(lambda (&optional interactive &rest args)
1997                   ,(documentation afunc t)
1998                   (interactive (list t))
1999                   (save-excursion
2000                     (set-buffer gnus-article-buffer)
2001                     (if interactive
2002                         (call-interactively ',afunc)
2003                       (apply ',afunc args))))))))
2004    '(article-hide-headers
2005      article-hide-boring-headers
2006      article-treat-overstrike
2007      (article-fill . gnus-article-word-wrap)
2008      article-remove-cr
2009      article-display-x-face
2010      article-de-quoted-unreadable
2011      article-mime-decode-quoted-printable
2012      article-hide-pgp
2013      article-hide-pem
2014      article-hide-signature
2015      article-remove-trailing-blank-lines
2016      article-strip-leading-blank-lines
2017      article-strip-multiple-blank-lines
2018      article-strip-leading-space
2019      article-strip-trailing-space
2020      article-strip-blank-lines
2021      article-strip-all-blank-lines
2022      article-date-local
2023      article-date-iso8601
2024      article-date-original
2025      article-date-ut
2026      article-decode-mime-words
2027      article-decode-charset
2028      article-decode-encoded-words
2029      article-date-user
2030      article-date-lapsed
2031      article-emphasize
2032      article-treat-dumbquotes
2033      article-normalize-headers
2034      (article-show-all . gnus-article-show-all-headers))))
2035 \f
2036 ;;;
2037 ;;; Gnus article mode
2038 ;;;
2039
2040 (put 'gnus-article-mode 'mode-class 'special)
2041
2042 (gnus-define-keys gnus-article-mode-map
2043   " " gnus-article-goto-next-page
2044   "\177" gnus-article-goto-prev-page
2045   [delete] gnus-article-goto-prev-page
2046   "\C-c^" gnus-article-refer-article
2047   "h" gnus-article-show-summary
2048   "s" gnus-article-show-summary
2049   "\C-c\C-m" gnus-article-mail
2050   "?" gnus-article-describe-briefly
2051   gnus-mouse-2 gnus-article-push-button
2052   "\r" gnus-article-press-button
2053   "\t" gnus-article-next-button
2054   "\M-\t" gnus-article-prev-button
2055   "e" gnus-article-edit
2056   "<" beginning-of-buffer
2057   ">" end-of-buffer
2058   "\C-c\C-i" gnus-info-find-node
2059   "\C-c\C-b" gnus-bug
2060
2061   "\C-d" gnus-article-read-summary-keys
2062   "\M-*" gnus-article-read-summary-keys
2063   "\M-#" gnus-article-read-summary-keys
2064   "\M-^" gnus-article-read-summary-keys
2065   "\M-g" gnus-article-read-summary-keys)
2066
2067 (substitute-key-definition
2068  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2069
2070 (defun gnus-article-make-menu-bar ()
2071   (gnus-turn-off-edit-menu 'article)
2072   (unless (boundp 'gnus-article-article-menu)
2073     (easy-menu-define
2074      gnus-article-article-menu gnus-article-mode-map ""
2075      '("Article"
2076        ["Scroll forwards" gnus-article-goto-next-page t]
2077        ["Scroll backwards" gnus-article-goto-prev-page t]
2078        ["Show summary" gnus-article-show-summary t]
2079        ["Fetch Message-ID at point" gnus-article-refer-article t]
2080        ["Mail to address at point" gnus-article-mail t]
2081        ["Send a bug report" gnus-bug t]))
2082
2083     (easy-menu-define
2084      gnus-article-treatment-menu gnus-article-mode-map ""
2085      '("Treatment"
2086        ["Hide headers" gnus-article-hide-headers t]
2087        ["Hide signature" gnus-article-hide-signature t]
2088        ["Hide citation" gnus-article-hide-citation t]
2089        ["Treat overstrike" gnus-article-treat-overstrike t]
2090        ["Remove carriage return" gnus-article-remove-cr t]))
2091
2092     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2093
2094     (when (boundp 'gnus-summary-post-menu)
2095       (define-key gnus-article-mode-map [menu-bar post]
2096         (cons "Post" gnus-summary-post-menu)))
2097
2098     (gnus-run-hooks 'gnus-article-menu-hook)))
2099
2100 (defun gnus-article-mode ()
2101   "Major mode for displaying an article.
2102
2103 All normal editing commands are switched off.
2104
2105 The following commands are available in addition to all summary mode
2106 commands:
2107 \\<gnus-article-mode-map>
2108 \\[gnus-article-next-page]\t Scroll the article one page forwards
2109 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2110 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2111 \\[gnus-article-show-summary]\t Display the summary buffer
2112 \\[gnus-article-mail]\t Send a reply to the address near point
2113 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2114 \\[gnus-info-find-node]\t Go to the Gnus info node"
2115   (interactive)
2116   (when (gnus-visual-p 'article-menu 'menu)
2117     (gnus-article-make-menu-bar))
2118   (gnus-simplify-mode-line)
2119   (setq mode-name "Article")
2120   (setq major-mode 'gnus-article-mode)
2121   (make-local-variable 'minor-mode-alist)
2122   (unless (assq 'gnus-show-mime minor-mode-alist)
2123     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
2124   (use-local-map gnus-article-mode-map)
2125   (gnus-update-format-specifications nil 'article-mode)
2126   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2127   (make-local-variable 'gnus-page-broken)
2128   (make-local-variable 'gnus-button-marker-list)
2129   (make-local-variable 'gnus-article-current-summary)
2130   (make-local-variable 'gnus-article-mime-handles)
2131   (make-local-variable 'gnus-article-decoded-p)
2132   (make-local-variable 'gnus-article-mime-handle-alist)
2133   (gnus-set-default-directory)
2134   (buffer-disable-undo)
2135   (setq buffer-read-only t)
2136   (set-syntax-table gnus-article-mode-syntax-table)
2137   (gnus-run-hooks 'gnus-article-mode-hook))
2138
2139 (defun gnus-article-setup-buffer ()
2140   "Initialize the article buffer."
2141   (let* ((name (if gnus-single-article-buffer "*Article*"
2142                  (concat "*Article " gnus-newsgroup-name "*")))
2143          (original
2144           (progn (string-match "\\*Article" name)
2145                  (concat " *Original Article"
2146                          (substring name (match-end 0))))))
2147     (setq gnus-article-buffer name)
2148     (setq gnus-original-article-buffer original)
2149     (setq gnus-article-mime-handle-alist nil)
2150     ;; This might be a variable local to the summary buffer.
2151     (unless gnus-single-article-buffer
2152       (save-excursion
2153         (set-buffer gnus-summary-buffer)
2154         (setq gnus-article-buffer name)
2155         (setq gnus-original-article-buffer original)
2156         (gnus-set-global-variables)))
2157     ;; Init original article buffer.
2158     (save-excursion
2159       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2160       (setq major-mode 'gnus-original-article-mode)
2161       (make-local-variable 'gnus-original-article))
2162     (if (get-buffer name)
2163         (save-excursion
2164           (set-buffer name)
2165           (buffer-disable-undo)
2166           (setq buffer-read-only t)
2167           (unless (eq major-mode 'gnus-article-mode)
2168             (gnus-article-mode))
2169           (current-buffer))
2170       (save-excursion
2171         (set-buffer (gnus-get-buffer-create name))
2172         (gnus-article-mode)
2173         (make-local-variable 'gnus-summary-buffer)
2174         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2175         (current-buffer)))))
2176
2177 ;; Set article window start at LINE, where LINE is the number of lines
2178 ;; from the head of the article.
2179 (defun gnus-article-set-window-start (&optional line)
2180   (set-window-start
2181    (get-buffer-window gnus-article-buffer t)
2182    (save-excursion
2183      (set-buffer gnus-article-buffer)
2184      (goto-char (point-min))
2185      (if (not line)
2186          (point-min)
2187        (gnus-message 6 "Moved to bookmark")
2188        (search-forward "\n\n" nil t)
2189        (forward-line line)
2190        (point)))))
2191
2192 ;;; @@ article filters
2193 ;;;
2194
2195 (defun gnus-article-display-mime-message ()
2196   "Article display method for MIME message."
2197   ;; called from `gnus-original-article-buffer'.
2198   (let ((charset (with-current-buffer gnus-summary-buffer
2199                    default-mime-charset)))
2200     (make-local-variable 'default-mime-charset)
2201     (setq default-mime-charset charset)
2202     (mime-display-message mime-message-structure
2203                           gnus-article-buffer nil gnus-article-mode-map)
2204     (make-local-variable 'default-mime-charset)
2205     (setq default-mime-charset charset)
2206     )
2207   ;; `mime-display-message' changes current buffer to `gnus-article-buffer'.
2208   (make-local-variable 'mime-button-mother-dispatcher)
2209   (setq mime-button-mother-dispatcher
2210         (function gnus-article-push-button))
2211   (run-hooks 'gnus-mime-article-prepare-hook))
2212
2213 (defun gnus-article-display-traditional-message ()
2214   "Article display method for traditional message."
2215   (set-buffer gnus-article-buffer)
2216   (let (buffer-read-only)
2217     (erase-buffer)
2218     (insert-buffer-substring gnus-original-article-buffer)))
2219
2220 (defun gnus-article-make-full-mail-header (&optional number charset)
2221   "Create a new mail header structure in a raw article buffer."
2222   (unless (and number charset)
2223     (save-current-buffer
2224       (set-buffer gnus-summary-buffer)
2225       (unless number
2226         (setq number (or (cdr gnus-article-current) 0)))
2227       (unless charset
2228         (setq charset (or default-mime-charset 'x-ctext)))))
2229   (goto-char (point-min))
2230   (let ((header-end (if (search-forward "\n\n" nil t)
2231                         (1- (point))
2232                       (goto-char (point-max))))
2233         (chars (- (point-max) (point)))
2234         (lines (count-lines (point) (point-max)))
2235         (default-mime-charset charset)
2236         xref)
2237     (narrow-to-region (point-min) header-end)
2238     (setq xref (std11-fetch-field "xref"))
2239     (prog1
2240         (make-full-mail-header
2241          number
2242          (std11-fetch-field "subject")
2243          (std11-fetch-field "from")
2244          (std11-fetch-field "date")
2245          (std11-fetch-field "message-id")
2246          (std11-fetch-field "references")
2247          chars
2248          lines
2249          (when xref (concat "Xref: " xref)))
2250       (widen))))
2251
2252 (defun gnus-article-prepare (article &optional all-headers header)
2253   "Prepare ARTICLE in article mode buffer.
2254 ARTICLE should either be an article number or a Message-ID.
2255 If ARTICLE is an id, HEADER should be the article headers.
2256 If ALL-HEADERS is non-nil, no headers are hidden."
2257   (save-excursion
2258     ;; Make sure we start in a summary buffer.
2259     (unless (eq major-mode 'gnus-summary-mode)
2260       (set-buffer gnus-summary-buffer))
2261     (setq gnus-summary-buffer (current-buffer))
2262     (let* ((gnus-article (if header (mail-header-number header) article))
2263            (summary-buffer (current-buffer))
2264            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2265            (group gnus-newsgroup-name)
2266            result)
2267       (save-excursion
2268         (gnus-article-setup-buffer)
2269         (set-buffer gnus-original-article-buffer)
2270         ;; Deactivate active regions.
2271         (when (and (boundp 'transient-mark-mode)
2272                    transient-mark-mode)
2273           (setq mark-active nil))
2274         (if (not (setq result (let ((buffer-read-only nil))
2275                                 (gnus-request-article-this-buffer
2276                                  article group))))
2277             ;; There is no such article.
2278             (save-excursion
2279               (when (and (numberp article)
2280                          (not (memq article gnus-newsgroup-sparse)))
2281                 (setq gnus-article-current
2282                       (cons gnus-newsgroup-name article))
2283                 (set-buffer gnus-summary-buffer)
2284                 (setq gnus-current-article article)
2285                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2286                     (progn
2287                       (gnus-summary-set-agent-mark article)
2288                       (message "Message marked for downloading"))
2289                   (gnus-summary-mark-article article gnus-canceled-mark)
2290                   (unless (memq article gnus-newsgroup-sparse)
2291                     (gnus-error 1
2292                      "No such article (may have expired or been canceled)")))))
2293           (if (or (eq result 'pseudo)
2294                   (eq result 'nneething))
2295               (progn
2296                 (save-excursion
2297                   (set-buffer summary-buffer)
2298                   (push article gnus-newsgroup-history)
2299                   (setq gnus-last-article gnus-current-article
2300                         gnus-current-article 0
2301                         gnus-current-headers nil
2302                         gnus-article-current nil)
2303                   (if (eq result 'nneething)
2304                       (gnus-configure-windows 'summary)
2305                     (gnus-configure-windows 'article))
2306                   (gnus-set-global-variables))
2307                 (let ((gnus-article-mime-handle-alist-1
2308                        gnus-article-mime-handle-alist))
2309                   (gnus-set-mode-line 'article)))
2310             ;; The result from the `request' was an actual article -
2311             ;; or at least some text that is now displayed in the
2312             ;; article buffer.
2313             (when (and (numberp article)
2314                        (not (eq article gnus-current-article)))
2315               ;; Seems like a new article has been selected.
2316               ;; `gnus-current-article' must be an article number.
2317               (save-excursion
2318                 (set-buffer summary-buffer)
2319                 (push article gnus-newsgroup-history)
2320                 (setq gnus-last-article gnus-current-article
2321                       gnus-current-article article
2322                       gnus-current-headers
2323                       (gnus-summary-article-header gnus-current-article)
2324                       gnus-article-current
2325                       (cons gnus-newsgroup-name gnus-current-article))
2326                 (unless (vectorp gnus-current-headers)
2327                   (setq gnus-current-headers nil))
2328                 (gnus-summary-goto-subject gnus-current-article)
2329                 (when (gnus-summary-show-thread)
2330                   ;; If the summary buffer really was folded, the
2331                   ;; previous goto may not actually have gone to
2332                   ;; the right article, but the thread root instead.
2333                   ;; So we go again.
2334                   (gnus-summary-goto-subject gnus-current-article))
2335                 (gnus-run-hooks 'gnus-mark-article-hook)
2336                 (gnus-set-mode-line 'summary)
2337                 (when (gnus-visual-p 'article-highlight 'highlight)
2338                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2339                 ;; Set the global newsgroup variables here.
2340                 ;; Suggested by Jim Sisolak
2341                 ;; <sisolak@trans4.neep.wisc.edu>.
2342                 (gnus-set-global-variables)
2343                 (setq gnus-have-all-headers
2344                       (or all-headers gnus-show-all-headers))))
2345             (when (or (numberp article)
2346                       (stringp article))
2347               (gnus-article-prepare-display)
2348               ;; Do page break.
2349               (goto-char (point-min))
2350               (setq gnus-page-broken
2351                     (when gnus-break-pages
2352                       (gnus-narrow-to-page)
2353                       t)))
2354             (let ((gnus-article-mime-handle-alist-1
2355                    gnus-article-mime-handle-alist))
2356               (gnus-set-mode-line 'article))
2357             (gnus-configure-windows 'article)
2358             (article-goto-body)
2359             (set-window-point (get-buffer-window (current-buffer)) (point))
2360             t))))))
2361
2362 (defun gnus-article-prepare-display ()
2363   "Make the current buffer look like a nice article."
2364   (let ((method
2365          (if gnus-show-mime
2366              (progn
2367                (setq mime-message-structure gnus-current-headers)
2368                gnus-article-display-method-for-mime)
2369            gnus-article-display-method-for-traditional)))
2370     (gnus-run-hooks 'gnus-tmp-internal-hook)
2371     (gnus-run-hooks 'gnus-article-prepare-hook)
2372     ;; Display message.
2373     (funcall method)
2374     ;; Associate this article with the current summary buffer.
2375     (setq gnus-article-current-summary (current-buffer))
2376     ;; Perform the article display hooks.
2377     (gnus-run-hooks 'gnus-article-display-hook)))
2378
2379 ;;;
2380 ;;; Gnus MIME viewing functions
2381 ;;;
2382
2383 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2384   "The following specs can be used:
2385 %t  The MIME type
2386 %T  MIME type, along with additional info
2387 %n  The `name' parameter
2388 %d  The description, if any
2389 %l  The length of the encoded part
2390 %p  The part identifier number
2391 %e  Dots if the part isn't displayed")
2392
2393 (defvar gnus-mime-button-line-format-alist
2394   '((?t gnus-tmp-type ?s)
2395     (?T gnus-tmp-type-long ?s)
2396     (?n gnus-tmp-name ?s)
2397     (?d gnus-tmp-description ?s)
2398     (?p gnus-tmp-id ?s)
2399     (?l gnus-tmp-length ?d)
2400     (?e gnus-tmp-dots ?s)))
2401
2402 (defvar gnus-mime-button-commands
2403   '((gnus-article-press-button  "\r"    "Toggle Display")
2404     (gnus-mime-view-part        "v"     "View Interactively...")
2405     (gnus-mime-save-part        "o"     "Save...")
2406     (gnus-mime-copy-part        "c"     "View As Text, In Other Buffer")
2407     (gnus-mime-inline-part      "i"     "View As Text, In This Buffer")
2408     (gnus-mime-internalize-part "E"     "View Internally")
2409     (gnus-mime-externalize-part "e"     "View Externally")
2410     (gnus-mime-pipe-part        "|"     "Pipe To Command...")))
2411
2412 (defun gnus-article-mime-part-status ()
2413   (if gnus-article-mime-handle-alist-1
2414       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2415     ""))
2416
2417 (defvar gnus-mime-button-map nil)
2418 (unless gnus-mime-button-map
2419   (setq gnus-mime-button-map (make-sparse-keymap))
2420   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2421   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2422   (define-key gnus-mime-button-map gnus-mouse-3 'gnus-mime-button-menu)
2423   (mapcar (lambda (c)
2424             (define-key gnus-mime-button-map (cadr c) (car c)))
2425           gnus-mime-button-commands))
2426
2427 (defun gnus-mime-button-menu (event)
2428   "Construct a context-sensitive menu of MIME commands."
2429   (interactive "e")
2430   (gnus-article-check-buffer)
2431   (let ((response (x-popup-menu 
2432                   t `("MIME Part" 
2433                       ("" ,@(mapcar (lambda (c)
2434                                       (cons (caddr c) (car c)))
2435                                     gnus-mime-button-commands)))))
2436         (pos (event-start event)))
2437     (when response
2438       (set-buffer (window-buffer (posn-window pos)))
2439       (goto-char (posn-point pos))
2440       (funcall response))))
2441
2442 (defun gnus-mime-view-all-parts (&optional handles)
2443   "View all the MIME parts."
2444   (interactive)
2445   (save-current-buffer
2446     (set-buffer gnus-article-buffer)
2447     (let ((handles (or handles gnus-article-mime-handles))
2448           (rfc2047-default-charset gnus-newsgroup-default-charset)
2449           (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2450       (if (stringp (car handles))
2451           (gnus-mime-view-all-parts (cdr handles))
2452         (mapcar 'mm-display-part handles)))))
2453
2454 (defun gnus-mime-save-part ()
2455   "Save the MIME part under point."
2456   (interactive)
2457   (gnus-article-check-buffer)
2458   (let ((data (get-text-property (point) 'gnus-data)))
2459     (mm-save-part data)))
2460
2461 (defun gnus-mime-pipe-part ()
2462   "Pipe the MIME part under point to a process."
2463   (interactive)
2464   (gnus-article-check-buffer)
2465   (let ((data (get-text-property (point) 'gnus-data)))
2466     (mm-pipe-part data)))
2467
2468 (defun gnus-mime-view-part ()
2469   "Interactively choose a view method for the MIME part under point."
2470   (interactive)
2471   (gnus-article-check-buffer)
2472   (mm-setup-w3)
2473   (let ((data (get-text-property (point) 'gnus-data))
2474         (url-standalone-mode (not gnus-plugged)))
2475     (mm-interactively-view-part data)))
2476
2477 (defun gnus-mime-copy-part (&optional handle)
2478   "Put the the MIME part under point into a new buffer."
2479   (interactive)
2480   (gnus-article-check-buffer)
2481   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2482          (contents (mm-get-part handle))|
2483          (base (file-name-nondirectory
2484                 (or
2485                  (mail-content-type-get (mm-handle-type handle) 'name)
2486                  (mail-content-type-get (mm-handle-type handle)
2487                                         'filename)
2488                  "*decoded*")))
2489          (buffer (generate-new-buffer base)))
2490     (switch-to-buffer buffer)
2491     (insert contents)
2492     ;; We do it this way to make `normal-mode' set the appropriate mode.
2493     (unwind-protect
2494         (progn
2495           (setq buffer-file-name (expand-file-name base))
2496           (normal-mode))
2497       (setq buffer-file-name nil))
2498     (goto-char (point-min))))
2499
2500 (defun gnus-mime-inline-part (&optional charset)
2501   "Insert the MIME part under point into the current buffer."
2502   (interactive "P") ; For compatibility reasons we are not using "z".
2503   (gnus-article-check-buffer)
2504   (mm-setup-w3)
2505   (let* ((data (get-text-property (point) 'gnus-data))
2506          contents
2507          (url-standalone-mode (not gnus-plugged))
2508          (b (point))
2509          buffer-read-only)
2510     (if (mm-handle-undisplayer data)
2511         (mm-remove-part data)
2512       (setq contents (mm-get-part data))
2513       (forward-line 2)
2514       (when charset 
2515         (unless (symbolp charset)
2516           (setq charset (mm-read-coding-system "Charset: ")))
2517         (setq contents (mm-decode-coding-string contents charset)))
2518       (mm-insert-inline data contents)
2519       (goto-char b))))
2520
2521 (defun gnus-mime-externalize-part (&optional handle)
2522   "View the MIME part under point with an external viewer."
2523   (interactive)
2524   (gnus-article-check-buffer)
2525   (mm-setup-w3)
2526   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2527          (url-standalone-mode (not gnus-plugged))
2528          (mm-user-display-methods nil)
2529          (mm-all-images-fit t)
2530          (rfc2047-default-charset gnus-newsgroup-default-charset)
2531          (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2532     (if (mm-handle-undisplayer handle)
2533         (mm-remove-part handle)
2534       (mm-display-part handle))))
2535
2536 (defun gnus-mime-internalize-part (&optional handle)
2537   "View the MIME part under point with an internal viewer."
2538   (interactive)
2539   (gnus-article-check-buffer)
2540   (mm-setup-w3)
2541   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2542          (url-standalone-mode (not gnus-plugged))
2543          (mm-user-display-methods '((".*" . inline)))
2544          (mm-all-images-fit t)
2545          (rfc2047-default-charset gnus-newsgroup-default-charset)
2546          (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2547     (if (mm-handle-undisplayer handle)
2548         (mm-remove-part handle)
2549       (mm-display-part handle))))
2550
2551 (defun gnus-article-part-wrapper (n function)
2552   (save-current-buffer
2553     (set-buffer gnus-article-buffer)
2554     (when (> n (length gnus-article-mime-handle-alist))
2555       (error "No such part"))
2556     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2557       (funcall function handle))))
2558
2559 (defun gnus-article-pipe-part (n)
2560   "Pipe MIME part N, which is the numerical prefix."
2561   (interactive "p")
2562   (gnus-article-part-wrapper n 'mm-pipe-part))
2563   
2564 (defun gnus-article-save-part (n)
2565   "Save MIME part N, which is the numerical prefix."
2566   (interactive "p")
2567   (gnus-article-part-wrapper n 'mm-save-part))
2568   
2569 (defun gnus-article-interactively-view-part (n)
2570   "Pipe MIME part N, which is the numerical prefix."
2571   (interactive "p")
2572   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2573   
2574 (defun gnus-article-copy-part (n)
2575   "Pipe MIME part N, which is the numerical prefix."
2576   (interactive "p")
2577   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2578
2579 (defun gnus-article-externalize-part (n)
2580   "Pipe MIME part N, which is the numerical prefix."
2581   (interactive "p")
2582   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2583   
2584 (defun gnus-article-view-part (n)
2585   "View MIME part N, which is the numerical prefix."
2586   (interactive "p")
2587   (save-current-buffer
2588     (set-buffer gnus-article-buffer)
2589     (when (> n (length gnus-article-mime-handle-alist))
2590       (error "No such part"))
2591     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2592       (when (gnus-article-goto-part n)
2593         (if (equal (car handle) "multipart/alternative")
2594             (gnus-article-press-button)
2595           (when (eq (gnus-mm-display-part handle) 'internal)
2596             (gnus-set-window-start)))))))
2597
2598 (defun gnus-mm-display-part (handle)
2599   "Display HANDLE and fix MIME button."
2600   (let ((id (get-text-property (point) 'gnus-part))
2601         (point (point))
2602         buffer-read-only)
2603     (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
2604     (gnus-insert-mime-button
2605      handle id (list (not (mm-handle-displayed-p handle))))
2606     (prog1
2607         (let ((window (selected-window))
2608               (rfc2047-default-charset gnus-newsgroup-default-charset)
2609               (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2610           (save-excursion
2611             (unwind-protect
2612                 (let ((win (get-buffer-window (current-buffer) t)))
2613                   (if win
2614                       (select-window win))
2615                   (goto-char point)
2616                   (forward-line)
2617                   (mm-display-part handle))
2618               (select-window window))))
2619       (goto-char point))))
2620
2621 (defun gnus-article-goto-part (n)
2622   "Go to MIME part N."
2623   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
2624     (when point
2625       (goto-char point))))
2626
2627 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
2628   (let ((gnus-tmp-name
2629          (or (mail-content-type-get (mm-handle-type handle)
2630                                     'name)
2631              (mail-content-type-get (mm-handle-disposition handle)
2632                                     'filename)
2633              ""))
2634         (gnus-tmp-type (car (mm-handle-type handle)))
2635         (gnus-tmp-description (or (mm-handle-description handle)
2636                                   ""))
2637         (gnus-tmp-dots
2638          (if (if displayed (car displayed)
2639                (mm-handle-displayed-p handle))
2640              "" "..."))
2641         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
2642                            (buffer-size)))
2643         gnus-tmp-type-long b e)
2644     (when (string-match ".*/" gnus-tmp-name)
2645       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
2646     (setq gnus-tmp-type-long (concat gnus-tmp-type
2647                                      (and (not (equal gnus-tmp-name ""))
2648                                           (concat "; " gnus-tmp-name))))
2649     (or (equal gnus-tmp-description "")
2650         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
2651     (unless (bolp)
2652       (insert "\n"))
2653     (setq b (point))
2654     (gnus-eval-format
2655      gnus-mime-button-line-format gnus-mime-button-line-format-alist
2656      `(local-map ,gnus-mime-button-map
2657                  keymap ,gnus-mime-button-map
2658                  gnus-callback gnus-mm-display-part
2659                  gnus-part ,gnus-tmp-id
2660                  article-type annotation
2661                  gnus-data ,handle))
2662     (setq e (point))
2663     (widget-convert-button 'link b e
2664                            :mime-handle handle
2665                            :action 'gnus-widget-press-button
2666                            :button-keymap gnus-mime-button-map
2667                            :help-echo
2668                            (lambda (widget)
2669                              ;; Needed to properly clear the message
2670                              ;; due to a bug in wid-edit
2671                              (setq help-echo-owns-message t)
2672                              (format
2673                               "Click to %s the MIME part; %s for more options"
2674                               (if (mm-handle-displayed-p
2675                                    (widget-get widget :mime-handle))
2676                                   "hide" "show")
2677                               (if gnus-xemacs "button3" "mouse-3"))))))
2678
2679 (defun gnus-widget-press-button (elems el)
2680   (goto-char (widget-get elems :from))
2681   (mm-setup-w3)
2682   (let ((url-standalone-mode (not gnus-plugged)))
2683     (gnus-article-press-button)))
2684
2685 (defun gnus-display-mime (&optional ihandles)
2686   "Insert MIME buttons in the buffer."
2687   (save-excursion
2688     (save-selected-window
2689       (let ((window (get-buffer-window gnus-article-buffer))
2690             (point (point)))
2691         (when window
2692           (select-window window)
2693           ;; We have to do this since selecting the window
2694           ;; may change the point.  So we set the window point.
2695           (set-window-point window point)))
2696       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
2697              handle name type b e display)
2698         (unless ihandles
2699           ;; Top-level call; we clean up.
2700           (mm-destroy-parts gnus-article-mime-handles)
2701           (setq gnus-article-mime-handles handles
2702                 gnus-article-mime-handle-alist nil)
2703           ;; We allow users to glean info from the handles.
2704           (when gnus-article-mime-part-function
2705             (gnus-mime-part-function handles)))
2706         (when (and handles
2707                    (or (not (stringp (car handles)))
2708                        (cdr handles)))
2709           (unless ihandles
2710             ;; Clean up for mime parts.
2711             (article-goto-body)
2712             (delete-region (point) (point-max)))
2713           (gnus-mime-display-part handles))))))
2714
2715 (defun gnus-mime-display-part (handle)
2716   (cond
2717    ;; Single part.
2718    ((not (stringp (car handle)))
2719     (gnus-mime-display-single handle))
2720    ;; multipart/alternative
2721    ((equal (car handle) "multipart/alternative")
2722     (let ((id (1+ (length gnus-article-mime-handle-alist))))
2723       (push (cons id handle) gnus-article-mime-handle-alist)
2724       (gnus-mime-display-alternative (cdr handle) nil nil id)))
2725    ;; multipart/related
2726    ((equal (car handle) "multipart/related")
2727     ;;;!!!We should find the start part, but we just default
2728     ;;;!!!to the first part.
2729     (gnus-mime-display-part (cadr handle)))
2730    ;; Other multiparts are handled like multipart/mixed.
2731    (t
2732     (gnus-mime-display-mixed (cdr handle)))))
2733
2734 (defun gnus-mime-part-function (handles)
2735   (if (stringp (car handles))
2736       (mapcar 'gnus-mime-part-function (cdr handles))
2737     (funcall gnus-article-mime-part-function handles)))
2738
2739 (defun gnus-mime-display-mixed (handles)
2740   (mapcar 'gnus-mime-display-part handles))
2741
2742 (defun gnus-mime-display-single (handle)
2743   (let ((type (car (mm-handle-type handle)))
2744         (ignored gnus-ignored-mime-types)
2745         (not-attachment t)
2746         (move nil)
2747         display text)
2748     (catch 'ignored
2749       (progn
2750         (while ignored
2751           (when (string-match (pop ignored) type)
2752             (throw 'ignored nil)))
2753         (if (and (setq not-attachment
2754                        (or (not (mm-handle-disposition handle))
2755                            (equal (car (mm-handle-disposition handle))
2756                                   "inline")))
2757                  (mm-automatic-display-p type)
2758                  (or (mm-inlinable-part-p type)
2759                      (mm-automatic-external-display-p type)))
2760             (setq display t)
2761           (when (equal (car (split-string type "/"))
2762                        "text")
2763             (setq text t)))
2764         (let ((id (1+ (length gnus-article-mime-handle-alist))))
2765           (push (cons id handle) gnus-article-mime-handle-alist)
2766           (when (or (not display)
2767                     (not (gnus-unbuttonized-mime-type-p type)))
2768             (gnus-article-insert-newline)
2769             (gnus-insert-mime-button
2770              handle id (list (or display
2771                                  (and not-attachment text))))
2772             (gnus-article-insert-newline)
2773             (gnus-article-insert-newline)
2774             (setq move t)))
2775         (cond
2776          (display
2777           (when move
2778             (forward-line -2))
2779           (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
2780                 (mm-charset-iso-8859-1-forced 
2781                  gnus-newsgroup-iso-8859-1-forced))
2782             (mm-display-part handle t))
2783           (goto-char (point-max)))
2784          ((and text not-attachment)
2785           (when move
2786             (forward-line -2))
2787           (gnus-article-insert-newline)
2788           (mm-insert-inline handle (mm-get-part handle))
2789           (goto-char (point-max))))))))
2790
2791 (defun gnus-unbuttonized-mime-type-p (type)
2792   "Say whether TYPE is to be unbuttonized."
2793   (unless gnus-inhibit-mime-unbuttonizing
2794     (catch 'found
2795       (let ((types gnus-unbuttonized-mime-types))
2796         (while types
2797           (when (string-match (pop types) type)
2798             (throw 'found t)))))))
2799
2800 (defun gnus-article-insert-newline ()
2801   "Insert a newline, but mark it as undeletable."
2802   (gnus-put-text-property
2803    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
2804
2805 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
2806   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
2807          (ihandles handles)
2808          (point (point))
2809          handle buffer-read-only from props begend not-pref)
2810     (save-window-excursion
2811       (save-restriction
2812         (when ibegend
2813           (narrow-to-region (car ibegend)
2814                             (or (cdr ibegend)
2815                                 (progn
2816                                   (goto-char (car ibegend))
2817                                   (forward-line 2)
2818                                   (point))))
2819           (delete-region (point-min) (point-max))
2820           (mm-remove-parts handles))
2821         (setq begend (list (point-marker)))
2822         ;; Do the toggle.
2823         (unless (setq not-pref (cadr (member preferred ihandles)))
2824           (setq not-pref (car ihandles)))
2825         (when (or ibegend
2826                   (not (gnus-unbuttonized-mime-type-p
2827                         "multipart/alternative")))
2828           (gnus-add-text-properties
2829            (setq from (point))
2830            (progn
2831              (insert (format "%d.  " id))
2832              (point))
2833            `(gnus-callback
2834              (lambda (handles)
2835                (unless ,(not ibegend)
2836                  (setq gnus-article-mime-handle-alist
2837                        ',gnus-article-mime-handle-alist))
2838                (gnus-mime-display-alternative
2839                 ',ihandles ',not-pref ',begend ,id))
2840              local-map ,gnus-mime-button-map
2841              ,gnus-mouse-face-prop ,gnus-article-mouse-face
2842              face ,gnus-article-button-face
2843              keymap ,gnus-mime-button-map
2844              gnus-part ,id
2845              gnus-data ,handle))
2846           (widget-convert-button 'link from (point)
2847                                  :action 'gnus-widget-press-button
2848                                  :button-keymap gnus-widget-button-keymap)
2849           ;; Do the handles
2850           (while (setq handle (pop handles))
2851             (gnus-add-text-properties
2852              (setq from (point))
2853              (progn
2854                (insert (format "(%c) %-18s"
2855                                (if (equal handle preferred) ?* ? )
2856                                (if (stringp (car handle))
2857                                    (car handle)
2858                                  (car (mm-handle-type handle)))))
2859                (point))
2860              `(gnus-callback
2861                (lambda (handles)
2862                  (unless ,(not ibegend)
2863                    (setq gnus-article-mime-handle-alist
2864                          ',gnus-article-mime-handle-alist))
2865                  (gnus-mime-display-alternative
2866                   ',ihandles ',handle ',begend ,id))
2867                local-map ,gnus-mime-button-map
2868                ,gnus-mouse-face-prop ,gnus-article-mouse-face
2869                face ,gnus-article-button-face
2870                keymap ,gnus-mime-button-map
2871                gnus-part ,id
2872                gnus-data ,handle))
2873             (widget-convert-button 'link from (point)
2874                                    :action 'gnus-widget-press-button
2875                                    :button-keymap gnus-widget-button-keymap)
2876             (insert "  "))
2877           (insert "\n\n"))
2878         (when preferred
2879           (if (stringp (car preferred))
2880               (gnus-display-mime preferred)
2881             (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
2882                   (mm-charset-iso-8859-1-forced 
2883                    gnus-newsgroup-iso-8859-1-forced))
2884               (mm-display-part preferred)))
2885           (goto-char (point-max))
2886           (setcdr begend (point-marker)))))
2887     (when ibegend
2888       (goto-char point))))
2889
2890 (defun gnus-article-wash-status ()
2891   "Return a string which display status of article washing."
2892   (save-excursion
2893     (set-buffer gnus-article-buffer)
2894     (let ((cite (gnus-article-hidden-text-p 'cite))
2895           (headers (gnus-article-hidden-text-p 'headers))
2896           (boring (gnus-article-hidden-text-p 'boring-headers))
2897           (pgp (gnus-article-hidden-text-p 'pgp))
2898           (pem (gnus-article-hidden-text-p 'pem))
2899           (signature (gnus-article-hidden-text-p 'signature))
2900           (overstrike (gnus-article-hidden-text-p 'overstrike))
2901           (emphasis (gnus-article-hidden-text-p 'emphasis))
2902           (mime gnus-show-mime))
2903       (format "%c%c%c%c%c%c%c"
2904               (if cite ?c ? )
2905               (if (or headers boring) ?h ? )
2906               (if (or pgp pem) ?p ? )
2907               (if signature ?s ? )
2908               (if overstrike ?o ? )
2909               (if mime ?m ? )
2910               (if emphasis ?e ? )))))
2911
2912 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2913
2914 (defun gnus-article-maybe-hide-headers ()
2915   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2916 Provided for backwards compatibility."
2917   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2918       gnus-inhibit-hiding
2919       (gnus-article-hide-headers)))
2920
2921 ;;; Article savers.
2922
2923 (defun gnus-output-to-file (file-name)
2924   "Append the current article to a file named FILE-NAME."
2925   (let ((artbuf (current-buffer)))
2926     (with-temp-buffer
2927       (insert-buffer-substring artbuf)
2928       ;; Append newline at end of the buffer as separator, and then
2929       ;; save it to file.
2930       (goto-char (point-max))
2931       (insert "\n")
2932       (append-to-file (point-min) (point-max) file-name)
2933       t)))
2934
2935 (defun gnus-narrow-to-page (&optional arg)
2936   "Narrow the article buffer to a page.
2937 If given a numerical ARG, move forward ARG pages."
2938   (interactive "P")
2939   (setq arg (if arg (prefix-numeric-value arg) 0))
2940   (save-excursion
2941     (set-buffer gnus-article-buffer)
2942     (goto-char (point-min))
2943     (widen)
2944     ;; Remove any old next/prev buttons.
2945     (when (gnus-visual-p 'page-marker)
2946       (let ((buffer-read-only nil))
2947         (gnus-remove-text-with-property 'gnus-prev)
2948         (gnus-remove-text-with-property 'gnus-next)))
2949     (when
2950         (cond ((< arg 0)
2951                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2952               ((> arg 0)
2953                (re-search-forward page-delimiter nil 'move arg)))
2954       (goto-char (match-end 0)))
2955     (narrow-to-region
2956      (point)
2957      (if (re-search-forward page-delimiter nil 'move)
2958          (match-beginning 0)
2959        (point)))
2960     (when (and (gnus-visual-p 'page-marker)
2961                (not (= (point-min) 1)))
2962       (save-excursion
2963         (goto-char (point-min))
2964         (gnus-insert-prev-page-button)))
2965     (when (and (gnus-visual-p 'page-marker)
2966                (< (+ (point-max) 2) (buffer-size)))
2967       (save-excursion
2968         (goto-char (point-max))
2969         (gnus-insert-next-page-button)))))
2970
2971 ;; Article mode commands
2972
2973 (defun gnus-article-goto-next-page ()
2974   "Show the next page of the article."
2975   (interactive)
2976   (when (gnus-article-next-page)
2977     (goto-char (point-min))
2978     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2979
2980 (defun gnus-article-goto-prev-page ()
2981   "Show the next page of the article."
2982   (interactive)
2983   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2984     (gnus-article-prev-page nil)))
2985
2986 (defun gnus-article-next-page (&optional lines)
2987   "Show the next page of the current article.
2988 If end of article, return non-nil.  Otherwise return nil.
2989 Argument LINES specifies lines to be scrolled up."
2990   (interactive "p")
2991   (move-to-window-line -1)
2992   (if (save-excursion
2993         (end-of-line)
2994         (and (pos-visible-in-window-p)  ;Not continuation line.
2995              (eobp)))
2996       ;; Nothing in this page.
2997       (if (or (not gnus-page-broken)
2998               (save-excursion
2999                 (save-restriction
3000                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3001           t                             ;Nothing more.
3002         (gnus-narrow-to-page 1)         ;Go to next page.
3003         nil)
3004     ;; More in this page.
3005     (let ((scroll-in-place nil))
3006       (condition-case ()
3007           (scroll-up lines)
3008         (end-of-buffer
3009          ;; Long lines may cause an end-of-buffer error.
3010          (goto-char (point-max)))))
3011     (move-to-window-line 0)
3012     nil))
3013
3014 (defun gnus-article-prev-page (&optional lines)
3015   "Show previous page of current article.
3016 Argument LINES specifies lines to be scrolled down."
3017   (interactive "p")
3018   (move-to-window-line 0)
3019   (if (and gnus-page-broken
3020            (bobp)
3021            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3022       (progn
3023         (gnus-narrow-to-page -1)        ;Go to previous page.
3024         (goto-char (point-max))
3025         (recenter -1))
3026     (let ((scroll-in-place nil))
3027       (prog1
3028           (condition-case ()
3029               (scroll-down lines)
3030             (beginning-of-buffer
3031              (goto-char (point-min))))
3032         (move-to-window-line 0)))))
3033
3034 (defun gnus-article-refer-article ()
3035   "Read article specified by message-id around point."
3036   (interactive)
3037   (let ((point (point)))
3038     (search-forward ">" nil t)          ;Move point to end of "<....>".
3039     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3040         (let ((message-id (match-string 1)))
3041           (goto-char point)
3042           (set-buffer gnus-summary-buffer)
3043           (gnus-summary-refer-article message-id))
3044       (goto-char (point))
3045       (error "No references around point"))))
3046
3047 (defun gnus-article-show-summary ()
3048   "Reconfigure windows to show summary buffer."
3049   (interactive)
3050   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3051       (error "There is no summary buffer for this article buffer")
3052     (gnus-article-set-globals)
3053     (gnus-configure-windows 'article)
3054     (gnus-summary-goto-subject gnus-current-article)
3055     (gnus-summary-position-point)))
3056
3057 (defun gnus-article-describe-briefly ()
3058   "Describe article mode commands briefly."
3059   (interactive)
3060   (gnus-message 6
3061                 (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")))
3062
3063 (defun gnus-article-summary-command ()
3064   "Execute the last keystroke in the summary buffer."
3065   (interactive)
3066   (let ((obuf (current-buffer))
3067         (owin (current-window-configuration))
3068         func)
3069     (switch-to-buffer gnus-article-current-summary 'norecord)
3070     (setq func (lookup-key (current-local-map) (this-command-keys)))
3071     (call-interactively func)
3072     (set-buffer obuf)
3073     (set-window-configuration owin)
3074     (set-window-point (get-buffer-window (current-buffer)) (point))))
3075
3076 (defun gnus-article-summary-command-nosave ()
3077   "Execute the last keystroke in the summary buffer."
3078   (interactive)
3079   (let (func)
3080     (pop-to-buffer gnus-article-current-summary 'norecord)
3081     (setq func (lookup-key (current-local-map) (this-command-keys)))
3082     (call-interactively func)))
3083
3084 (defun gnus-article-check-buffer ()
3085   "Beep if not in an article buffer."
3086   (unless (equal major-mode 'gnus-article-mode)
3087     (error "Command invoked outside of a Gnus article buffer")))
3088
3089 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3090   "Read a summary buffer key sequence and execute it from the article buffer."
3091   (interactive "P")
3092   (gnus-article-check-buffer)
3093   (let ((nosaves
3094          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3095            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3096            "=" "^" "\M-^" "|"))
3097         (nosave-but-article
3098          '("A\r"))
3099         (nosave-in-article
3100          '("\C-d"))
3101         (up-to-top
3102          '("n" "Gn" "p" "Gp"))
3103         keys new-sum-point)
3104     (save-excursion
3105       (set-buffer gnus-article-current-summary)
3106       (let (gnus-pick-mode)
3107         (push (or key last-command-event) unread-command-events)
3108         (setq keys (read-key-sequence nil))))
3109     (message "")
3110
3111     (if (or (member keys nosaves)
3112             (member keys nosave-but-article)
3113             (member keys nosave-in-article))
3114         (let (func)
3115           (save-window-excursion
3116             (pop-to-buffer gnus-article-current-summary 'norecord)
3117             ;; We disable the pick minor mode commands.
3118             (let (gnus-pick-mode)
3119               (setq func (lookup-key (current-local-map) keys))))
3120           (if (not func)
3121               (ding)
3122             (unless (member keys nosave-in-article)
3123               (set-buffer gnus-article-current-summary))
3124             (call-interactively func)
3125             (setq new-sum-point (point)))
3126           (when (member keys nosave-but-article)
3127             (pop-to-buffer gnus-article-buffer 'norecord)))
3128       ;; These commands should restore window configuration.
3129       (let ((obuf (current-buffer))
3130             (owin (current-window-configuration))
3131             (opoint (point))
3132             (summary gnus-article-current-summary)
3133             func in-buffer selected)
3134         (if not-restore-window
3135             (pop-to-buffer summary 'norecord)
3136           (switch-to-buffer summary 'norecord))
3137         (setq in-buffer (current-buffer))
3138         ;; We disable the pick minor mode commands.
3139         (if (setq func (let (gnus-pick-mode)
3140                          (lookup-key (current-local-map) keys)))
3141             (progn
3142               (call-interactively func)
3143               (setq new-sum-point (point)))
3144           (ding))
3145         (when (eq in-buffer (current-buffer))
3146           (setq selected (gnus-summary-select-article))
3147           (set-buffer obuf)
3148           (unless not-restore-window
3149             (set-window-configuration owin))
3150           (unless (or (not (eq selected 'old)) (member keys up-to-top))
3151             (set-window-point (get-buffer-window (current-buffer))
3152                               opoint))
3153           (let ((win (get-buffer-window gnus-article-current-summary)))
3154             (when win
3155               (set-window-point win new-sum-point))))))))
3156
3157 (defun gnus-article-hide (&optional arg force)
3158   "Hide all the gruft in the current article.
3159 This means that PGP stuff, signatures, cited text and (some)
3160 headers will be hidden.
3161 If given a prefix, show the hidden text instead."
3162   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3163   (gnus-article-hide-headers arg)
3164   (gnus-article-hide-pgp arg)
3165   (gnus-article-hide-citation-maybe arg force)
3166   (gnus-article-hide-signature arg))
3167
3168 (defun gnus-article-maybe-highlight ()
3169   "Do some article highlighting if article highlighting is requested."
3170   (when (gnus-visual-p 'article-highlight 'highlight)
3171     (gnus-article-highlight-some)))
3172
3173 (defun gnus-check-group-server ()
3174   ;; Make sure the connection to the server is alive.
3175   (unless (gnus-server-opened
3176            (gnus-find-method-for-group gnus-newsgroup-name))
3177     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3178     (gnus-request-group gnus-newsgroup-name t)))
3179
3180 (defun gnus-request-article-this-buffer (article group)
3181   "Get an article and insert it into this buffer."
3182   (let (do-update-line sparse-header)
3183     (prog1
3184         (save-excursion
3185           (erase-buffer)
3186           (gnus-kill-all-overlays)
3187           (setq group (or group gnus-newsgroup-name))
3188
3189           ;; Using `gnus-request-article' directly will insert the article into
3190           ;; `nntp-server-buffer' - so we'll save some time by not having to
3191           ;; copy it from the server buffer into the article buffer.
3192
3193           ;; We only request an article by message-id when we do not have the
3194           ;; headers for it, so we'll have to get those.
3195           (when (stringp article)
3196             (let ((gnus-override-method gnus-refer-article-method))
3197               (gnus-read-header article)))
3198
3199           ;; If the article number is negative, that means that this article
3200           ;; doesn't belong in this newsgroup (possibly), so we find its
3201           ;; message-id and request it by id instead of number.
3202           (when (and (numberp article)
3203                      gnus-summary-buffer
3204                      (get-buffer gnus-summary-buffer)
3205                      (gnus-buffer-exists-p gnus-summary-buffer))
3206             (save-excursion
3207               (set-buffer gnus-summary-buffer)
3208               (let ((header (gnus-summary-article-header article)))
3209                 (when (< article 0)
3210                   (cond
3211                    ((memq article gnus-newsgroup-sparse)
3212                     ;; This is a sparse gap article.
3213                     (setq do-update-line article)
3214                     (setq article (mail-header-id header))
3215                     (let ((gnus-override-method gnus-refer-article-method))
3216                       (setq sparse-header (gnus-read-header article)))
3217                     (setq gnus-newsgroup-sparse
3218                           (delq article gnus-newsgroup-sparse)))
3219                    ((vectorp header)
3220                     ;; It's a real article.
3221                     (setq article (mail-header-id header)))
3222                    (t
3223                     ;; It is an extracted pseudo-article.
3224                     (setq article 'pseudo)
3225                     (gnus-request-pseudo-article header))))
3226
3227                 (let ((method (gnus-find-method-for-group
3228                                gnus-newsgroup-name)))
3229                   (when (and (eq (car method) 'nneething)
3230                              (vectorp header))
3231                     (let ((dir (concat
3232                                 (file-name-as-directory
3233                                  (or (cadr (assq 'nneething-address method))
3234                                      (nth 1 method)))
3235                                 (mail-header-subject header))))
3236                       (when (file-directory-p dir)
3237                         (setq article 'nneething)
3238                         (gnus-group-enter-directory dir))))))))
3239
3240           (cond
3241            ;; Refuse to select canceled articles.
3242            ((and (numberp article)
3243                  gnus-summary-buffer
3244                  (get-buffer gnus-summary-buffer)
3245                  (gnus-buffer-exists-p gnus-summary-buffer)
3246                  (eq (cdr (save-excursion
3247                             (set-buffer gnus-summary-buffer)
3248                             (assq article gnus-newsgroup-reads)))
3249                      gnus-canceled-mark))
3250             nil)
3251            ;; Check the backlog.
3252            ((and gnus-keep-backlog
3253                  (gnus-backlog-request-article group article (current-buffer)))
3254             'article)
3255            ;; Check asynchronous pre-fetch.
3256            ((gnus-async-request-fetched-article group article (current-buffer))
3257             (gnus-async-prefetch-next group article gnus-summary-buffer)
3258             (when (and (numberp article) gnus-keep-backlog)
3259               (gnus-backlog-enter-article group article (current-buffer)))
3260             'article)
3261            ;; Check the cache.
3262            ((and gnus-use-cache
3263                  (numberp article)
3264                  (gnus-cache-request-article article group))
3265             'article)
3266            ;; Get the article and put into the article buffer.
3267            ((or (stringp article) (numberp article))
3268             (let ((gnus-override-method
3269                    (and (stringp article) gnus-refer-article-method))
3270                   (buffer-read-only nil))
3271               (erase-buffer)
3272               (gnus-kill-all-overlays)
3273               (gnus-check-group-server)
3274               (when (gnus-request-article article group (current-buffer))
3275                 (when (numberp article)
3276                   (gnus-async-prefetch-next group article gnus-summary-buffer)
3277                   (when gnus-keep-backlog
3278                     (gnus-backlog-enter-article
3279                      group article (current-buffer))))
3280                 'article)))
3281            ;; It was a pseudo.
3282            (t article)))
3283
3284       ;; Associate this article with the current summary buffer.
3285       (setq gnus-article-current-summary gnus-summary-buffer)
3286
3287       ;; Take the article from the original article buffer
3288       ;; and place it in the buffer it's supposed to be in.
3289       (when (and (get-buffer gnus-article-buffer)
3290                  (equal (buffer-name (current-buffer))
3291                         (buffer-name (get-buffer gnus-article-buffer))))
3292         (save-excursion
3293           (if (get-buffer gnus-original-article-buffer)
3294               (set-buffer gnus-original-article-buffer)
3295             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3296             (buffer-disable-undo)
3297             (setq major-mode 'gnus-original-article-mode)
3298             (setq buffer-read-only t))
3299           (let (buffer-read-only)
3300             (erase-buffer)
3301             (insert-buffer-substring gnus-article-buffer))
3302           (setq gnus-original-article (cons group article)))
3303
3304         ;; Decode charsets.
3305         (run-hooks 'gnus-article-decode-hook)
3306         ;; Mark article as decoded or not.
3307         (setq gnus-article-decoded-p gnus-article-decode-hook))
3308
3309       ;; Update sparse articles.
3310       (when (and do-update-line
3311                  (or (numberp article)
3312                      (stringp article)))
3313         (let ((buf (current-buffer)))
3314           (set-buffer gnus-summary-buffer)
3315           (gnus-summary-update-article do-update-line sparse-header)
3316           (gnus-summary-goto-subject do-update-line nil t)
3317           (set-window-point (get-buffer-window (current-buffer) t)
3318                             (point))
3319           (set-buffer buf))))))
3320
3321 ;;;
3322 ;;; Article editing
3323 ;;;
3324
3325 (defcustom gnus-article-edit-mode-hook nil
3326   "Hook run in article edit mode buffers."
3327   :group 'gnus-article-various
3328   :type 'hook)
3329
3330 (defcustom gnus-article-edit-article-setup-function
3331   'gnus-article-mime-edit-article-setup
3332   "Function called to setup an editing article buffer."
3333   :group 'gnus-article-various
3334   :type 'function)
3335
3336 (defvar gnus-article-edit-done-function nil)
3337
3338 (defvar gnus-article-edit-mode-map nil)
3339
3340 ;; Should we be using derived.el for this?
3341 (unless gnus-article-edit-mode-map
3342   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3343   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3344
3345   (gnus-define-keys gnus-article-edit-mode-map
3346     "\C-c\C-c" gnus-article-edit-done
3347     "\C-c\C-k" gnus-article-edit-exit)
3348
3349   (gnus-define-keys (gnus-article-edit-wash-map
3350                      "\C-c\C-w" gnus-article-edit-mode-map)
3351     "f" gnus-article-edit-full-stops))
3352
3353 (defun gnus-article-edit-mode ()
3354   "Major mode for editing articles.
3355 This is an extended text-mode.
3356
3357 \\{gnus-article-edit-mode-map}"
3358   (interactive)
3359   (setq major-mode 'gnus-article-edit-mode)
3360   (setq mode-name "Article Edit")
3361   (use-local-map gnus-article-edit-mode-map)
3362   (make-local-variable 'gnus-article-edit-done-function)
3363   (make-local-variable 'gnus-prev-winconf)
3364   (setq buffer-read-only nil)
3365   (buffer-enable-undo)
3366   (widen)
3367   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3368
3369 (defun gnus-article-edit (&optional force)
3370   "Edit the current article.
3371 This will have permanent effect only in mail groups.
3372 If FORCE is non-nil, allow editing of articles even in read-only
3373 groups."
3374   (interactive "P")
3375   (when (and (not force)
3376              (gnus-group-read-only-p))
3377     (error "The current newsgroup does not support article editing"))
3378   (gnus-article-date-original)
3379   (gnus-article-edit-article
3380    `(lambda (no-highlight)
3381       (gnus-summary-edit-article-done
3382        ,(or (mail-header-references gnus-current-headers) "")
3383        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3384
3385 (defun gnus-article-edit-article (exit-func)
3386   "Start editing the contents of the current article buffer."
3387   (let ((winconf (current-window-configuration)))
3388     (set-buffer gnus-article-buffer)
3389     (gnus-article-edit-mode)
3390     (gnus-article-delete-text-of-type 'annotation)
3391     (gnus-set-text-properties (point-min) (point-max) nil)
3392     (gnus-configure-windows 'edit-article)
3393     (setq gnus-article-edit-done-function exit-func)
3394     (setq gnus-prev-winconf winconf)
3395     (when gnus-article-edit-article-setup-function
3396       (funcall gnus-article-edit-article-setup-function))
3397     (gnus-message 6 "C-c C-c to end edits")))
3398
3399 (defun gnus-article-edit-done (&optional arg)
3400   "Update the article edits and exit."
3401   (interactive "P")
3402   (save-excursion
3403     (save-restriction
3404       (widen)
3405       (when (article-goto-body)
3406         (let ((lines (count-lines (point) (point-max)))
3407               (length (- (point-max) (point)))
3408               (case-fold-search t)
3409               (body (copy-marker (point))))
3410           (goto-char (point-min))
3411           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3412             (delete-region (match-beginning 1) (match-end 1))
3413             (insert (number-to-string length)))
3414           (goto-char (point-min))
3415           (when (re-search-forward
3416                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3417             (delete-region (match-beginning 1) (match-end 1))
3418             (insert (number-to-string length)))
3419           (goto-char (point-min))
3420           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3421             (delete-region (match-beginning 1) (match-end 1))
3422             (insert (number-to-string lines)))))))
3423   (let ((func gnus-article-edit-done-function)
3424         (buf (current-buffer))
3425         (start (window-start)))
3426     (remove-hook 'gnus-article-mode-hook
3427                  'gnus-article-mime-edit-article-unwind)
3428     (gnus-article-edit-exit)
3429     (save-excursion
3430       (set-buffer buf)
3431       (let ((buffer-read-only nil))
3432         (funcall func arg))
3433       ;; The cache and backlog have to be flushed somewhat.
3434       (when gnus-keep-backlog
3435         (gnus-backlog-remove-article
3436          (car gnus-article-current) (cdr gnus-article-current)))
3437       ;; Flush original article as well.
3438       (save-excursion
3439         (when (get-buffer gnus-original-article-buffer)
3440           (set-buffer gnus-original-article-buffer)
3441           (setq gnus-original-article nil)))
3442       (when gnus-use-cache
3443         (gnus-cache-update-article
3444          (car gnus-article-current) (cdr gnus-article-current))))
3445     (set-buffer buf)
3446     (set-window-start (get-buffer-window buf) start)
3447     (set-window-point (get-buffer-window buf) (point))))
3448
3449 (defun gnus-article-edit-exit ()
3450   "Exit the article editing without updating."
3451   (interactive)
3452   ;; We remove all text props from the article buffer.
3453   (let ((buf (format "%s" (buffer-string)))
3454         (curbuf (current-buffer))
3455         (p (point))
3456         (window-start (window-start)))
3457     (erase-buffer)
3458     (insert buf)
3459     (let ((winconf gnus-prev-winconf))
3460       (gnus-article-mode)
3461       (set-window-configuration winconf)
3462       ;; Tippy-toe some to make sure that point remains where it was.
3463       (save-current-buffer
3464         (set-buffer curbuf)
3465         (set-window-start (get-buffer-window (current-buffer)) window-start)
3466         (goto-char p)))))
3467
3468 (defun gnus-article-edit-full-stops ()
3469   "Interactively repair spacing at end of sentences."
3470   (interactive)
3471   (save-excursion
3472     (goto-char (point-min))
3473     (search-forward-regexp "^$" nil t)
3474     (let ((case-fold-search nil))
3475       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3476
3477 ;;;
3478 ;;; Article editing with MIME-Edit
3479 ;;;
3480
3481 (defcustom gnus-article-mime-edit-article-setup-hook nil
3482   "Hook run after setting up a MIME editing article buffer."
3483   :group 'gnus-article-various
3484   :type 'hook)
3485
3486 (defun gnus-article-mime-edit-article-unwind ()
3487   "Unwind `gnus-article-buffer' if article editing was given up."
3488   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
3489   (when mime-edit-mode-flag
3490     (mime-edit-exit 'nomime 'no-error)
3491     (message ""))
3492   (when (featurep 'font-lock)
3493     (setq font-lock-defaults nil)
3494     (font-lock-mode 0)))
3495
3496 (defun gnus-article-mime-edit-article-setup ()
3497   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
3498 after replacing with the original article."
3499   (setq gnus-show-mime t)
3500   (setq gnus-article-edit-done-function
3501         `(lambda (&rest args)
3502            (when mime-edit-mode-flag
3503              (mime-edit-exit)
3504              (message ""))
3505            (goto-char (point-min))
3506            (let (case-fold-search)
3507              (when (re-search-forward
3508                     (format "^%s$" (regexp-quote mail-header-separator))
3509                     nil t)
3510                (replace-match "")))
3511            (when (featurep 'font-lock)
3512              (setq font-lock-defaults nil)
3513              (font-lock-mode 0))
3514            (apply ,gnus-article-edit-done-function args)
3515            (set-buffer gnus-original-article-buffer)
3516            (erase-buffer)
3517            (insert-buffer gnus-article-buffer)
3518            (setq gnus-current-headers (gnus-article-make-full-mail-header))
3519            (gnus-article-prepare-display)))
3520   (substitute-key-definition
3521    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
3522    gnus-article-edit-mode-map)
3523   (erase-buffer)
3524   (insert-buffer gnus-original-article-buffer)
3525   (mime-edit-again)
3526   (when (featurep 'font-lock)
3527     (set (make-local-variable 'font-lock-defaults)
3528          '(message-font-lock-keywords t))
3529     (font-lock-set-defaults)
3530     (turn-on-font-lock))
3531   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
3532   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
3533
3534 (defun gnus-article-mime-edit-exit ()
3535   "Exit the article MIME editing without updating."
3536   (interactive)
3537   (let ((winconf gnus-prev-winconf)
3538         buf)
3539     (when mime-edit-mode-flag
3540       (mime-edit-exit)
3541       (message ""))
3542     (goto-char (point-min))
3543     (let (case-fold-search)
3544       (when (re-search-forward
3545              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
3546         (replace-match "")))
3547     (when (featurep 'font-lock)
3548       (setq font-lock-defaults nil)
3549       (font-lock-mode 0))
3550     ;; We remove all text props from the article buffer.
3551     (setq buf (format "%s" (buffer-string)))
3552     (set-buffer (get-buffer-create gnus-original-article-buffer))
3553     (erase-buffer)
3554     (insert buf)
3555     (setq gnus-current-headers (gnus-article-make-full-mail-header))
3556     (gnus-article-prepare-display)
3557     (set-window-configuration winconf)))
3558
3559 ;;;
3560 ;;; Article highlights
3561 ;;;
3562
3563 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
3564
3565 ;;; Internal Variables:
3566
3567 (defcustom gnus-button-url-regexp "\\b\\(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\\)"
3568   "Regular expression that matches URLs."
3569   :group 'gnus-article-buttons
3570   :type 'regexp)
3571
3572 (defcustom gnus-button-alist
3573   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
3574      0 t gnus-button-message-id 2)
3575     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
3576     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
3577      1 t
3578      gnus-button-fetch-group 4)
3579     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
3580     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
3581      t gnus-button-message-id 3)
3582     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
3583     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
3584     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
3585     ;; This is how URLs _should_ be embedded in text...
3586     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
3587     ;; Raw URLs.
3588     (,gnus-button-url-regexp 0 t gnus-button-url 0))
3589   "*Alist of regexps matching buttons in article bodies.
3590
3591 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
3592 REGEXP: is the string matching text around the button,
3593 BUTTON: is the number of the regexp grouping actually matching the button,
3594 FORM: is a lisp expression which must eval to true for the button to
3595 be added,
3596 CALLBACK: is the function to call when the user push this button, and each
3597 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
3598
3599 CALLBACK can also be a variable, in that case the value of that
3600 variable it the real callback function."
3601   :group 'gnus-article-buttons
3602   :type '(repeat (list regexp
3603                        (integer :tag "Button")
3604                        (sexp :tag "Form")
3605                        (function :tag "Callback")
3606                        (repeat :tag "Par"
3607                                :inline t
3608                                (integer :tag "Regexp group")))))
3609
3610 (defcustom gnus-header-button-alist
3611   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
3612      0 t gnus-button-message-id 0)
3613     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
3614     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
3615      0 t gnus-button-mailto 0)
3616     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3617     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3618     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3619     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
3620      gnus-button-message-id 3))
3621   "*Alist of headers and regexps to match buttons in article heads.
3622
3623 This alist is very similar to `gnus-button-alist', except that each
3624 alist has an additional HEADER element first in each entry:
3625
3626 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
3627
3628 HEADER is a regexp to match a header.  For a fuller explanation, see
3629 `gnus-button-alist'."
3630   :group 'gnus-article-buttons
3631   :group 'gnus-article-headers
3632   :type '(repeat (list (regexp :tag "Header")
3633                        regexp
3634                        (integer :tag "Button")
3635                        (sexp :tag "Form")
3636                        (function :tag "Callback")
3637                        (repeat :tag "Par"
3638                                :inline t
3639                                (integer :tag "Regexp group")))))
3640
3641 (defvar gnus-button-regexp nil)
3642 (defvar gnus-button-marker-list nil)
3643 ;; Regexp matching any of the regexps from `gnus-button-alist'.
3644
3645 (defvar gnus-button-last nil)
3646 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
3647
3648 ;;; Commands:
3649
3650 (defun gnus-article-push-button (event)
3651   "Check text under the mouse pointer for a callback function.
3652 If the text under the mouse pointer has a `gnus-callback' property,
3653 call it with the value of the `gnus-data' text property."
3654   (interactive "e")
3655   (set-buffer (window-buffer (posn-window (event-start event))))
3656   (let* ((pos (posn-point (event-start event)))
3657          (data (get-text-property pos 'gnus-data))
3658          (fun (get-text-property pos 'gnus-callback)))
3659     (goto-char pos)
3660     (when fun
3661       (funcall fun data))))
3662
3663 (defun gnus-article-press-button ()
3664   "Check text at point for a callback function.
3665 If the text at point has a `gnus-callback' property,
3666 call it with the value of the `gnus-data' text property."
3667   (interactive)
3668   (let* ((data (get-text-property (point) 'gnus-data))
3669          (fun (get-text-property (point) 'gnus-callback)))
3670     (when fun
3671       (funcall fun data))))
3672
3673 (defun gnus-article-prev-button (n)
3674   "Move point to N buttons backward.
3675 If N is negative, move forward instead."
3676   (interactive "p")
3677   (gnus-article-next-button (- n)))
3678
3679 (defun gnus-article-next-button (n)
3680   "Move point to N buttons forward.
3681 If N is negative, move backward instead."
3682   (interactive "p")
3683   (let ((function (if (< n 0) 'previous-single-property-change
3684                     'next-single-property-change))
3685         (inhibit-point-motion-hooks t)
3686         (backward (< n 0))
3687         (limit (if (< n 0) (point-min) (point-max))))
3688     (setq n (abs n))
3689     (while (and (not (= limit (point)))
3690                 (> n 0))
3691       ;; Skip past the current button.
3692       (when (get-text-property (point) 'gnus-callback)
3693         (goto-char (funcall function (point) 'gnus-callback nil limit)))
3694       ;; Go to the next (or previous) button.
3695       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
3696       ;; Put point at the start of the button.
3697       (when (and backward (not (get-text-property (point) 'gnus-callback)))
3698         (goto-char (funcall function (point) 'gnus-callback nil limit)))
3699       ;; Skip past intangible buttons.
3700       (when (get-text-property (point) 'intangible)
3701         (incf n))
3702       (decf n))
3703     (unless (zerop n)
3704       (gnus-message 5 "No more buttons"))
3705     n))
3706
3707 (defun gnus-article-highlight (&optional force)
3708   "Highlight current article.
3709 This function calls `gnus-article-highlight-headers',
3710 `gnus-article-highlight-citation',
3711 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3712 do the highlighting.  See the documentation for those functions."
3713   (interactive (list 'force))
3714   (gnus-article-highlight-headers)
3715   (gnus-article-highlight-citation force)
3716   (gnus-article-highlight-signature)
3717   (gnus-article-add-buttons force)
3718   (gnus-article-add-buttons-to-head))
3719
3720 (defun gnus-article-highlight-some (&optional force)
3721   "Highlight current article.
3722 This function calls `gnus-article-highlight-headers',
3723 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3724 do the highlighting.  See the documentation for those functions."
3725   (interactive (list 'force))
3726   (gnus-article-highlight-headers)
3727   (gnus-article-highlight-signature)
3728   (gnus-article-add-buttons))
3729
3730 (defun gnus-article-highlight-headers ()
3731   "Highlight article headers as specified by `gnus-header-face-alist'."
3732   (interactive)
3733   (save-excursion
3734     (set-buffer gnus-article-buffer)
3735     (save-restriction
3736       (let ((alist gnus-header-face-alist)
3737             (buffer-read-only nil)
3738             (case-fold-search t)
3739             (inhibit-point-motion-hooks t)
3740             entry regexp header-face field-face from hpoints fpoints)
3741         (message-narrow-to-head)
3742         (while (setq entry (pop alist))
3743           (goto-char (point-min))
3744           (setq regexp (concat "^\\("
3745                                (if (string-equal "" (nth 0 entry))
3746                                    "[^\t ]"
3747                                  (nth 0 entry))
3748                                "\\)")
3749                 header-face (nth 1 entry)
3750                 field-face (nth 2 entry))
3751           (while (and (re-search-forward regexp nil t)
3752                       (not (eobp)))
3753             (beginning-of-line)
3754             (setq from (point))
3755             (unless (search-forward ":" nil t)
3756               (forward-char 1))
3757             (when (and header-face
3758                        (not (memq (point) hpoints)))
3759               (push (point) hpoints)
3760               (gnus-put-text-property from (point) 'face header-face))
3761             (when (and field-face
3762                        (not (memq (setq from (point)) fpoints)))
3763               (push from fpoints)
3764               (if (re-search-forward "^[^ \t]" nil t)
3765                   (forward-char -2)
3766                 (goto-char (point-max)))
3767               (gnus-put-text-property from (point) 'face field-face))))))))
3768
3769 (defun gnus-article-highlight-signature ()
3770   "Highlight the signature in an article.
3771 It does this by highlighting everything after
3772 `gnus-signature-separator' using `gnus-signature-face'."
3773   (interactive)
3774   (save-excursion
3775     (set-buffer gnus-article-buffer)
3776     (let ((buffer-read-only nil)
3777           (inhibit-point-motion-hooks t))
3778       (save-restriction
3779         (when (and gnus-signature-face
3780                    (gnus-article-narrow-to-signature))
3781           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
3782                             'face gnus-signature-face)
3783           (widen)
3784           (gnus-article-search-signature)
3785           (let ((start (match-beginning 0))
3786                 (end (set-marker (make-marker) (1+ (match-end 0)))))
3787             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
3788                                      end)))))))
3789
3790 (defun gnus-button-in-region-p (b e prop)
3791   "Say whether PROP exists in the region."
3792   (text-property-not-all b e prop nil))
3793
3794 (defun gnus-article-add-buttons (&optional force)
3795   "Find external references in the article and make buttons of them.
3796 \"External references\" are things like Message-IDs and URLs, as
3797 specified by `gnus-button-alist'."
3798   (interactive (list 'force))
3799   (save-excursion
3800     (set-buffer gnus-article-buffer)
3801     (let ((buffer-read-only nil)
3802           (inhibit-point-motion-hooks t)
3803           (case-fold-search t)
3804           (alist gnus-button-alist)
3805           beg entry regexp)
3806       ;; Remove all old markers.
3807       (let (marker entry)
3808         (while (setq marker (pop gnus-button-marker-list))
3809           (goto-char marker)
3810           (when (setq entry (gnus-button-entry))
3811             (put-text-property (match-beginning (nth 1 entry))
3812                                (match-end (nth 1 entry))
3813                                'gnus-callback nil))
3814           (set-marker marker nil)))
3815       ;; We skip the headers.
3816       (article-goto-body)
3817       (setq beg (point))
3818       (while (setq entry (pop alist))
3819         (setq regexp (car entry))
3820         (goto-char beg)
3821         (while (re-search-forward regexp nil t)
3822           (let* ((start (and entry (match-beginning (nth 1 entry))))
3823                  (end (and entry (match-end (nth 1 entry))))
3824                  (from (match-beginning 0)))
3825             (when (and (or (eq t (nth 2 entry))
3826                            (eval (nth 2 entry)))
3827                        (not (gnus-button-in-region-p
3828                              start end 'gnus-callback)))
3829               ;; That optional form returned non-nil, so we add the
3830               ;; button.
3831               (gnus-article-add-button
3832                start end 'gnus-button-push
3833                (car (push (set-marker (make-marker) from)
3834                           gnus-button-marker-list))))))))))
3835
3836 ;; Add buttons to the head of an article.
3837 (defun gnus-article-add-buttons-to-head ()
3838   "Add buttons to the head of the article."
3839   (interactive)
3840   (save-excursion
3841     (set-buffer gnus-article-buffer)
3842     (let ((buffer-read-only nil)
3843           (inhibit-point-motion-hooks t)
3844           (case-fold-search t)
3845           (alist gnus-header-button-alist)
3846           entry beg end)
3847       (nnheader-narrow-to-headers)
3848       (while alist
3849         ;; Each alist entry.
3850         (setq entry (car alist)
3851               alist (cdr alist))
3852         (goto-char (point-min))
3853         (while (re-search-forward (car entry) nil t)
3854           ;; Each header matching the entry.
3855           (setq beg (match-beginning 0))
3856           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
3857                              (match-beginning 0))
3858                         (point-max)))
3859           (goto-char beg)
3860           (while (re-search-forward (nth 1 entry) end t)
3861             ;; Each match within a header.
3862             (let* ((entry (cdr entry))
3863                    (start (match-beginning (nth 1 entry)))
3864                    (end (match-end (nth 1 entry)))
3865                    (form (nth 2 entry)))
3866               (goto-char (match-end 0))
3867               (when (eval form)
3868                 (gnus-article-add-button
3869                  start end (nth 3 entry)
3870                  (buffer-substring (match-beginning (nth 4 entry))
3871                                    (match-end (nth 4 entry)))))))
3872           (goto-char end))))
3873     (widen)))
3874
3875 ;;; External functions:
3876
3877 (defun gnus-article-add-button (from to fun &optional data)
3878   "Create a button between FROM and TO with callback FUN and data DATA."
3879   (when gnus-article-button-face
3880     (gnus-overlay-put (gnus-make-overlay from to)
3881                       'face gnus-article-button-face))
3882   (gnus-add-text-properties
3883    from to
3884    (nconc (and gnus-article-mouse-face
3885                (list gnus-mouse-face-prop gnus-article-mouse-face))
3886           (list 'gnus-callback fun)
3887           (and data (list 'gnus-data data)))))
3888
3889 ;;; Internal functions:
3890
3891 (defun gnus-article-set-globals ()
3892   (save-excursion
3893     (set-buffer gnus-summary-buffer)
3894     (gnus-set-global-variables)))
3895
3896 (defun gnus-signature-toggle (end)
3897   (save-excursion
3898     (set-buffer gnus-article-buffer)
3899     (let ((buffer-read-only nil)
3900           (inhibit-point-motion-hooks t))
3901       (if (get-text-property end 'invisible)
3902           (gnus-article-unhide-text end (point-max))
3903         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3904
3905 (defun gnus-button-entry ()
3906   ;; Return the first entry in `gnus-button-alist' matching this place.
3907   (let ((alist gnus-button-alist)
3908         (entry nil))
3909     (while alist
3910       (setq entry (pop alist))
3911       (if (looking-at (car entry))
3912           (setq alist nil)
3913         (setq entry nil)))
3914     entry))
3915
3916 (defun gnus-button-push (marker)
3917   ;; Push button starting at MARKER.
3918   (save-excursion
3919     (goto-char marker)
3920     (let* ((entry (gnus-button-entry))
3921            (inhibit-point-motion-hooks t)
3922            (fun (nth 3 entry))
3923            (args (mapcar (lambda (group)
3924                            (let ((string (match-string group)))
3925                              (gnus-set-text-properties
3926                               0 (length string) nil string)
3927                              string))
3928                          (nthcdr 4 entry))))
3929       (cond
3930        ((fboundp fun)
3931         (apply fun args))
3932        ((and (boundp fun)
3933              (fboundp (symbol-value fun)))
3934         (apply (symbol-value fun) args))
3935        (t
3936         (gnus-message 1 "You must define `%S' to use this button"
3937                       (cons fun args)))))))
3938
3939 (defun gnus-button-message-id (message-id)
3940   "Fetch MESSAGE-ID."
3941   (save-excursion
3942     (set-buffer gnus-summary-buffer)
3943     (gnus-summary-refer-article message-id)))
3944
3945 (defun gnus-button-fetch-group (address)
3946   "Fetch GROUP specified by ADDRESS."
3947   (if (not (string-match "[:/]" address))
3948       ;; This is just a simple group url.
3949       (gnus-group-read-ephemeral-group address gnus-select-method)
3950     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3951                            address))
3952         (error "Can't parse %s" address)
3953       (gnus-group-read-ephemeral-group
3954        (match-string 4 address)
3955        `(nntp ,(match-string 1 address)
3956               (nntp-address ,(match-string 1 address))
3957               (nntp-port-number ,(if (match-end 3)
3958                                      (match-string 3 address)
3959                                    "nntp")))))))
3960
3961 (defun gnus-url-parse-query-string (query &optional downcase)
3962   (let (retval pairs cur key val)
3963     (setq pairs (split-string query "&"))
3964     (while pairs
3965       (setq cur (car pairs)
3966             pairs (cdr pairs))
3967       (if (not (string-match "=" cur))
3968           nil                           ; Grace
3969         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3970               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3971         (if downcase
3972             (setq key (downcase key)))
3973         (setq cur (assoc key retval))
3974         (if cur
3975             (setcdr cur (cons val (cdr cur)))
3976           (setq retval (cons (list key val) retval)))))
3977     retval))
3978
3979 (defun gnus-url-unhex (x)
3980   (if (> x ?9)
3981       (if (>= x ?a)
3982           (+ 10 (- x ?a))
3983         (+ 10 (- x ?A)))
3984     (- x ?0)))
3985
3986 (defun gnus-url-unhex-string (str &optional allow-newlines)
3987   "Remove %XXX embedded spaces, etc in a url.
3988 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3989 decoding of carriage returns and line feeds in the string, which is normally
3990 forbidden in URL encoding."
3991   (setq str (or str ""))
3992   (let ((tmp "")
3993         (case-fold-search t))
3994     (while (string-match "%[0-9a-f][0-9a-f]" str)
3995       (let* ((start (match-beginning 0))
3996              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3997              (code (+ (* 16 ch1)
3998                       (gnus-url-unhex (elt str (+ start 2))))))
3999         (setq tmp (concat
4000                    tmp (substring str 0 start)
4001                    (cond
4002                     (allow-newlines
4003                      (char-to-string code))
4004                     ((or (= code ?\n) (= code ?\r))
4005                      " ")
4006                     (t (char-to-string code))))
4007               str (substring str (match-end 0)))))
4008     (setq tmp (concat tmp str))
4009     tmp))
4010
4011 (defun gnus-url-mailto (url)
4012   ;; Send mail to someone
4013   (when (string-match "mailto:/*\\(.*\\)" url)
4014     (setq url (substring url (match-beginning 1) nil)))
4015   (let (to args subject func)
4016     (if (string-match (regexp-quote "?") url)
4017         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4018               args (gnus-url-parse-query-string
4019                     (substring url (match-end 0) nil) t))
4020       (setq to (gnus-url-unhex-string url)))
4021     (setq args (cons (list "to" to) args)
4022           subject (cdr-safe (assoc "subject" args)))
4023     (gnus-setup-message 'reply
4024       (message-mail)
4025       (while args
4026         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4027         (if (fboundp func)
4028             (funcall func)
4029           (message-position-on-field (caar args)))
4030         (insert (mapconcat 'identity (cdar args) ", "))
4031         (setq args (cdr args)))
4032       (if subject
4033           (message-goto-body)
4034         (message-goto-subject)))))
4035
4036 (defun gnus-button-mailto (address)
4037   ;; Mail to ADDRESS.
4038   (set-buffer (gnus-copy-article-buffer))
4039   (gnus-setup-message 'reply
4040     (message-reply address)))
4041
4042 (defun gnus-button-reply (address)
4043   ;; Reply to ADDRESS.
4044   (gnus-setup-message 'reply
4045     (message-reply address)))
4046
4047 (defun gnus-button-url (address)
4048   "Browse ADDRESS."
4049   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4050   (if (listp browse-url-browser-function)
4051       (browse-url address)
4052     (funcall browse-url-browser-function address)))
4053
4054 (defun gnus-button-embedded-url (address)
4055   "Browse ADDRESS."
4056   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4057   (if (listp browse-url-browser-function)
4058       (browse-url (gnus-strip-whitespace address))
4059     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
4060
4061 ;;; Next/prev buttons in the article buffer.
4062
4063 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4064 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4065
4066 (defvar gnus-prev-page-map nil)
4067 (unless gnus-prev-page-map
4068   (setq gnus-prev-page-map (make-sparse-keymap))
4069   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4070   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4071
4072 (defun gnus-insert-prev-page-button ()
4073   (let ((buffer-read-only nil))
4074     (gnus-eval-format
4075      gnus-prev-page-line-format nil
4076      `(gnus-prev t local-map ,gnus-prev-page-map
4077                  gnus-callback gnus-article-button-prev-page
4078                  article-type annotation))))
4079
4080 (defvar gnus-next-page-map nil)
4081 (unless gnus-next-page-map
4082   (setq gnus-next-page-map (make-keymap))
4083   (suppress-keymap gnus-prev-page-map)
4084   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4085   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4086
4087 (defun gnus-button-next-page ()
4088   "Go to the next page."
4089   (interactive)
4090   (let ((win (selected-window)))
4091     (select-window (get-buffer-window gnus-article-buffer t))
4092     (gnus-article-next-page)
4093     (select-window win)))
4094
4095 (defun gnus-button-prev-page ()
4096   "Go to the prev page."
4097   (interactive)
4098   (let ((win (selected-window)))
4099     (select-window (get-buffer-window gnus-article-buffer t))
4100     (gnus-article-prev-page)
4101     (select-window win)))
4102
4103 (defun gnus-insert-next-page-button ()
4104   (let ((buffer-read-only nil))
4105     (gnus-eval-format gnus-next-page-line-format nil
4106                       `(gnus-next
4107                         t local-map ,gnus-next-page-map
4108                         gnus-callback gnus-article-button-next-page
4109                         article-type annotation))))
4110
4111 (defun gnus-article-button-next-page (arg)
4112   "Go to the next page."
4113   (interactive "P")
4114   (let ((win (selected-window)))
4115     (select-window (get-buffer-window gnus-article-buffer t))
4116     (gnus-article-next-page)
4117     (select-window win)))
4118
4119 (defun gnus-article-button-prev-page (arg)
4120   "Go to the prev page."
4121   (interactive "P")
4122   (let ((win (selected-window)))
4123     (select-window (get-buffer-window gnus-article-buffer t))
4124     (gnus-article-prev-page)
4125     (select-window win)))
4126
4127 (defvar gnus-decode-header-methods
4128   '(gnus-decode-with-mail-decode-encoded-word-region)
4129   "List of methods used to decode headers.
4130
4131 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
4132 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
4133 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4134 whose names match REGEXP.
4135
4136 For example:
4137 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4138  mail-decode-encoded-word-region
4139  (\"chinese\" . rfc1843-decode-region))
4140 ")
4141
4142 (defvar gnus-decode-header-methods-cache nil)
4143
4144 (defun gnus-decode-with-mail-decode-encoded-word-region (start end)
4145   (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
4146         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
4147     (mail-decode-encoded-word-region start end)))
4148
4149 (defun gnus-multi-decode-header (start end)
4150   "Apply the functions from `gnus-encoded-word-methods' that match."
4151   (unless (and gnus-decode-header-methods-cache
4152                (eq gnus-newsgroup-name
4153                    (car gnus-decode-header-methods-cache)))
4154     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4155     (mapc '(lambda (x)
4156              (if (symbolp x)
4157                  (nconc gnus-decode-header-methods-cache (list x))
4158                (if (and gnus-newsgroup-name
4159                         (string-match (car x) gnus-newsgroup-name))
4160                    (nconc gnus-decode-header-methods-cache
4161                           (list (cdr x))))))
4162           gnus-decode-header-methods))
4163   (let ((xlist gnus-decode-header-methods-cache))
4164     (pop xlist)
4165     (save-restriction
4166       (narrow-to-region start end)
4167       (while xlist
4168         (funcall (pop xlist) (point-min) (point-max))))))
4169
4170
4171 ;;; @ for mime-view
4172 ;;;
4173
4174 (defun gnus-article-header-presentation-method (entity situation)
4175   (mime-insert-header entity)
4176   )
4177
4178 (set-alist 'mime-header-presentation-method-alist
4179            'gnus-original-article-mode
4180            #'gnus-article-header-presentation-method)
4181
4182 (defun gnus-mime-preview-quitting-method ()
4183   (if gnus-show-mime
4184       (gnus-article-show-summary)
4185     (mime-preview-kill-buffer)
4186     (delete-other-windows)
4187     (gnus-article-show-summary)
4188     (gnus-summary-select-article nil t)
4189     ))
4190
4191 (set-alist 'mime-preview-quitting-method-alist
4192            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
4193
4194 (defun gnus-following-method (buf)
4195   (set-buffer buf)
4196   (message-followup)
4197   (message-yank-original)
4198   (kill-buffer buf)
4199   (goto-char (point-min))
4200   )
4201
4202 (set-alist 'mime-preview-following-method-alist
4203            'gnus-original-article-mode #'gnus-following-method)
4204
4205
4206 ;;; @ end
4207 ;;;
4208
4209 (gnus-ems-redefine)
4210
4211 (provide 'gnus-art)
4212
4213 (run-hooks 'gnus-art-load-hook)
4214
4215 ;;; gnus-art.el ends here