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