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