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