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