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