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