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