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