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