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
2916   "\C-d" gnus-article-read-summary-keys
2917   "\M-*" gnus-article-read-summary-keys
2918   "\M-#" gnus-article-read-summary-keys
2919   "\M-^" gnus-article-read-summary-keys
2920   "\M-g" gnus-article-read-summary-keys)
2921
2922 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
2923 (mapcar
2924  (lambda (key)
2925    (unless (lookup-key gnus-article-mode-map key)
2926      (define-key gnus-article-mode-map key
2927        'gnus-article-read-summary-keys)))
2928  (delq nil
2929        (append
2930         (mapcar
2931          (lambda (elt)
2932            (let ((key (car elt)))
2933              (and (> (length key) 0)
2934                   (not (eq 'menu-bar (aref key 0)))
2935                   (symbolp (lookup-key gnus-summary-mode-map key))
2936                   key)))
2937          (accessible-keymaps gnus-summary-mode-map))
2938         (let ((c 127)
2939               keys)
2940           (while (>= c 32)
2941             (push (char-to-string c) keys)
2942             (decf c))
2943           keys))))
2944
2945 (defun gnus-article-make-menu-bar ()
2946   (gnus-turn-off-edit-menu 'article)
2947   (unless (boundp 'gnus-article-article-menu)
2948     (easy-menu-define
2949      gnus-article-article-menu gnus-article-mode-map ""
2950      '("Article"
2951        ["Scroll forwards" gnus-article-goto-next-page t]
2952        ["Scroll backwards" gnus-article-goto-prev-page t]
2953        ["Show summary" gnus-article-show-summary t]
2954        ["Fetch Message-ID at point" gnus-article-refer-article t]
2955        ["Mail to address at point" gnus-article-mail t]
2956        ["Send a bug report" gnus-bug t]))
2957
2958     (easy-menu-define
2959      gnus-article-treatment-menu gnus-article-mode-map ""
2960      '("Treatment"
2961        ["Hide headers" gnus-article-toggle-headers t]
2962        ["Hide signature" gnus-article-hide-signature t]
2963        ["Hide citation" gnus-article-hide-citation t]
2964        ["Treat overstrike" gnus-article-treat-overstrike t]
2965        ["Remove carriage return" gnus-article-remove-cr t]
2966        ["Decode HZ" gnus-article-decode-HZ t]))
2967
2968     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2969
2970     (when (boundp 'gnus-summary-post-menu)
2971       (define-key gnus-article-mode-map [menu-bar post]
2972         (cons "Post" gnus-summary-post-menu)))
2973
2974     (gnus-run-hooks 'gnus-article-menu-hook)))
2975
2976 (defun gnus-article-mode ()
2977   "Major mode for displaying an article.
2978
2979 All normal editing commands are switched off.
2980
2981 The following commands are available in addition to all summary mode
2982 commands:
2983 \\<gnus-article-mode-map>
2984 \\[gnus-article-next-page]\t Scroll the article one page forwards
2985 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2986 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2987 \\[gnus-article-show-summary]\t Display the summary buffer
2988 \\[gnus-article-mail]\t Send a reply to the address near point
2989 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2990 \\[gnus-info-find-node]\t Go to the Gnus info node"
2991   (interactive)
2992   (when (gnus-visual-p 'article-menu 'menu)
2993     (gnus-article-make-menu-bar))
2994   (gnus-simplify-mode-line)
2995   (setq mode-name "Article")
2996   (setq major-mode 'gnus-article-mode)
2997   (make-local-variable 'minor-mode-alist)
2998   (unless (assq 'gnus-show-mime minor-mode-alist)
2999     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3000   (use-local-map gnus-article-mode-map)
3001   (gnus-update-format-specifications nil 'article-mode)
3002   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3003   (make-local-variable 'gnus-page-broken)
3004   (make-local-variable 'gnus-button-marker-list)
3005   (make-local-variable 'gnus-article-current-summary)
3006   (make-local-variable 'gnus-article-mime-handles)
3007   (make-local-variable 'gnus-article-decoded-p)
3008   (make-local-variable 'gnus-article-mime-handle-alist)
3009   (make-local-variable 'gnus-article-wash-types)
3010   (make-local-variable 'gnus-article-charset)
3011   (make-local-variable 'gnus-article-ignored-charsets)
3012   (gnus-set-default-directory)
3013   (buffer-disable-undo)
3014   (setq buffer-read-only t)
3015   (set-syntax-table gnus-article-mode-syntax-table)
3016   (gnus-run-hooks 'gnus-article-mode-hook))
3017
3018 (defun gnus-article-setup-buffer ()
3019   "Initialize the article buffer."
3020   (let* ((name (if gnus-single-article-buffer "*Article*"
3021                  (concat "*Article " gnus-newsgroup-name "*")))
3022          (original
3023           (progn (string-match "\\*Article" name)
3024                  (concat " *Original Article"
3025                          (substring name (match-end 0))))))
3026     (setq gnus-article-buffer name)
3027     (setq gnus-original-article-buffer original)
3028     (setq gnus-article-mime-handle-alist nil)
3029     ;; This might be a variable local to the summary buffer.
3030     (unless gnus-single-article-buffer
3031       (save-excursion
3032         (set-buffer gnus-summary-buffer)
3033         (setq gnus-article-buffer name)
3034         (setq gnus-original-article-buffer original)
3035         (gnus-set-global-variables)))
3036     (gnus-article-setup-highlight-words)
3037     ;; Init original article buffer.
3038     (save-excursion
3039       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3040       (set-buffer-multibyte nil)
3041       (setq major-mode 'gnus-original-article-mode)
3042       (make-local-variable 'gnus-original-article))
3043     (if (get-buffer name)
3044         (save-excursion
3045           (set-buffer name)
3046           (buffer-disable-undo)
3047           (setq buffer-read-only t)
3048           (unless (eq major-mode 'gnus-article-mode)
3049             (gnus-article-mode))
3050           (current-buffer))
3051       (save-excursion
3052         (set-buffer (gnus-get-buffer-create name))
3053         (gnus-article-mode)
3054         (make-local-variable 'gnus-summary-buffer)
3055         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3056         (current-buffer)))))
3057
3058 ;; Set article window start at LINE, where LINE is the number of lines
3059 ;; from the head of the article.
3060 (defun gnus-article-set-window-start (&optional line)
3061   (set-window-start
3062    (get-buffer-window gnus-article-buffer t)
3063    (save-excursion
3064      (set-buffer gnus-article-buffer)
3065      (goto-char (point-min))
3066      (if (not line)
3067          (point-min)
3068        (gnus-message 6 "Moved to bookmark")
3069        (search-forward "\n\n" nil t)
3070        (forward-line line)
3071        (point)))))
3072
3073 ;;; @@ article filters
3074 ;;;
3075
3076 (defun gnus-article-display-mime-message ()
3077   "Article display method for MIME message."
3078   ;; called from `gnus-original-article-buffer'.
3079   (let (charset all-headers)
3080     (with-current-buffer gnus-summary-buffer
3081       (setq charset default-mime-charset
3082             all-headers gnus-have-all-headers))
3083     (make-local-variable 'default-mime-charset)
3084     (setq default-mime-charset charset)
3085     (with-current-buffer (get-buffer-create gnus-article-buffer)
3086       (make-local-variable 'default-mime-charset)
3087       (setq default-mime-charset charset))
3088     (mime-display-message mime-message-structure
3089                           gnus-article-buffer nil gnus-article-mode-map)
3090     (when all-headers
3091       (gnus-article-hide-headers nil -1)))
3092   (run-hooks 'gnus-mime-article-prepare-hook))
3093
3094 (defun gnus-article-display-traditional-message ()
3095   "Article display method for traditional message."
3096   (set-buffer gnus-article-buffer)
3097   (let (buffer-read-only)
3098     (erase-buffer)
3099     (insert-buffer-substring gnus-original-article-buffer)))
3100
3101 (defun gnus-article-make-full-mail-header (&optional number charset)
3102   "Create a new mail header structure in a raw article buffer."
3103   (unless (and number charset)
3104     (save-current-buffer
3105       (set-buffer gnus-summary-buffer)
3106       (unless number
3107         (setq number (or (cdr gnus-article-current) 0)))
3108       (unless charset
3109         (setq charset (or default-mime-charset 'x-ctext)))))
3110   (goto-char (point-min))
3111   (let ((header-end (if (search-forward "\n\n" nil t)
3112                         (1- (point))
3113                       (goto-char (point-max))))
3114         (chars (- (point-max) (point)))
3115         (lines (count-lines (point) (point-max)))
3116         (default-mime-charset charset)
3117         xref)
3118     (narrow-to-region (point-min) header-end)
3119     (setq xref (std11-fetch-field "xref"))
3120     (prog1
3121         (make-full-mail-header
3122          number
3123          (std11-fetch-field "subject")
3124          (std11-fetch-field "from")
3125          (std11-fetch-field "date")
3126          (std11-fetch-field "message-id")
3127          (std11-fetch-field "references")
3128          chars
3129          lines
3130          (when xref (concat "Xref: " xref)))
3131       (widen))))
3132
3133 (defun gnus-article-prepare (article &optional all-headers header)
3134   "Prepare ARTICLE in article mode buffer.
3135 ARTICLE should either be an article number or a Message-ID.
3136 If ARTICLE is an id, HEADER should be the article headers.
3137 If ALL-HEADERS is non-nil, no headers are hidden."
3138   (save-excursion
3139     ;; Make sure we start in a summary buffer.
3140     (unless (eq major-mode 'gnus-summary-mode)
3141       (set-buffer gnus-summary-buffer))
3142     (setq gnus-summary-buffer (current-buffer))
3143     (let* ((gnus-article (if header (mail-header-number header) article))
3144            (summary-buffer (current-buffer))
3145            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3146            (group gnus-newsgroup-name)
3147            result)
3148       (save-excursion
3149         (gnus-article-setup-buffer)
3150         (set-buffer gnus-original-article-buffer)
3151         ;; Deactivate active regions.
3152         (when (and (boundp 'transient-mark-mode)
3153                    transient-mark-mode)
3154           (setq mark-active nil))
3155         (if (not (setq result (let ((buffer-read-only nil))
3156                                 (gnus-request-article-this-buffer
3157                                  article group))))
3158             ;; There is no such article.
3159             (save-excursion
3160               (when (and (numberp article)
3161                          (not (memq article gnus-newsgroup-sparse)))
3162                 (setq gnus-article-current
3163                       (cons gnus-newsgroup-name article))
3164                 (set-buffer gnus-summary-buffer)
3165                 (setq gnus-current-article article)
3166                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3167                     (progn
3168                       (gnus-summary-set-agent-mark article)
3169                       (message "Message marked for downloading"))
3170                   (gnus-summary-mark-article article gnus-canceled-mark)
3171                   (unless (memq article gnus-newsgroup-sparse)
3172                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3173           (if (or (eq result 'pseudo)
3174                   (eq result 'nneething))
3175               (progn
3176                 (save-excursion
3177                   (set-buffer summary-buffer)
3178                   (push article gnus-newsgroup-history)
3179                   (setq gnus-last-article gnus-current-article
3180                         gnus-current-article 0
3181                         gnus-current-headers nil
3182                         gnus-article-current nil)
3183                   (if (eq result 'nneething)
3184                       (gnus-configure-windows 'summary)
3185                     (gnus-configure-windows 'article))
3186                   (gnus-set-global-variables))
3187                 (let ((gnus-article-mime-handle-alist-1
3188                        gnus-article-mime-handle-alist))
3189                   (gnus-set-mode-line 'article)))
3190             ;; The result from the `request' was an actual article -
3191             ;; or at least some text that is now displayed in the
3192             ;; article buffer.
3193             (when (and (numberp article)
3194                        (not (eq article gnus-current-article)))
3195               ;; Seems like a new article has been selected.
3196               ;; `gnus-current-article' must be an article number.
3197               (save-excursion
3198                 (set-buffer summary-buffer)
3199                 (push article gnus-newsgroup-history)
3200                 (setq gnus-last-article gnus-current-article
3201                       gnus-current-article article
3202                       gnus-current-headers
3203                       (gnus-summary-article-header gnus-current-article)
3204                       gnus-article-current
3205                       (cons gnus-newsgroup-name gnus-current-article))
3206                 (unless (vectorp gnus-current-headers)
3207                   (setq gnus-current-headers nil))
3208                 (gnus-summary-goto-subject gnus-current-article)
3209                 (when (gnus-summary-show-thread)
3210                   ;; If the summary buffer really was folded, the
3211                   ;; previous goto may not actually have gone to
3212                   ;; the right article, but the thread root instead.
3213                   ;; So we go again.
3214                   (gnus-summary-goto-subject gnus-current-article))
3215                 (gnus-run-hooks 'gnus-mark-article-hook)
3216                 (gnus-set-mode-line 'summary)
3217                 (when (gnus-visual-p 'article-highlight 'highlight)
3218                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3219                 ;; Set the global newsgroup variables here.
3220                 (gnus-set-global-variables)
3221                 (setq gnus-have-all-headers
3222                       (or all-headers gnus-show-all-headers))))
3223             (save-excursion
3224               (gnus-configure-windows 'article))
3225             (when (or (numberp article)
3226                       (stringp article))
3227               (gnus-article-prepare-display)
3228               ;; Do page break.
3229               (goto-char (point-min))
3230               (setq gnus-page-broken
3231                     (when gnus-break-pages
3232                       (gnus-narrow-to-page)
3233                       t)))
3234             (let ((gnus-article-mime-handle-alist-1
3235                    gnus-article-mime-handle-alist))
3236               (gnus-set-mode-line 'article))
3237             (article-goto-body)
3238             (set-window-point (get-buffer-window (current-buffer)) (point))
3239             (gnus-configure-windows 'article)
3240             t))))))
3241
3242 (defun gnus-article-prepare-mime-display (&optional number)
3243   (goto-char (point-min))
3244   (when (re-search-forward "^[^\t ]+:" nil t)
3245     (goto-char (match-beginning 0)))
3246   (let ((entity (if (eq 1 (point-min))
3247                     (get-text-property 1 'mime-view-entity)
3248                   (get-text-property (point) 'mime-view-entity)))
3249         last-entity child-entity next type)
3250     (setq child-entity (mime-entity-children entity))
3251     (if child-entity
3252         (setq last-entity (nth (1- (length child-entity))
3253                                child-entity))
3254       (setq last-entity entity))
3255     (save-restriction
3256       (narrow-to-region (point)
3257                         (if (search-forward "\n\n" nil t)
3258                             (point)
3259                           (point-max)))
3260       (gnus-treat-article 'head)
3261       (put-text-property (point-min) (point-max) 'article-treated-header t)
3262       (goto-char (point-max)))
3263     (while (and (not (eobp)) entity)
3264       (setq next (set-marker
3265                   (make-marker)
3266                   (next-single-property-change (point) 'mime-view-entity
3267                                                nil (point-max))))
3268       (let ((types (mime-entity-content-type entity)))
3269         (while (eq 'multipart (mime-content-type-primary-type types))
3270           (setq entity (car (mime-entity-children entity))
3271                 types (mime-entity-content-type entity)))
3272         (when types
3273           (setq type (format "%s/%s"
3274                              (mime-content-type-primary-type types)
3275                              (mime-content-type-subtype types)))))
3276       (if (string-equal type "message/rfc822")
3277           (progn
3278             (setq next (point))
3279             (let ((children (mime-entity-children entity))
3280                   last-children)
3281               (when children
3282                 (setq last-children (nth (1- (length children)) children))
3283                 (while
3284                     (and
3285                      (not (eq last-children
3286                               (get-text-property next 'mime-view-entity)))
3287                      (setq next
3288                            (next-single-property-change next
3289                                                         'mime-view-entity
3290                                                         nil (point-max)))))))
3291             (setq next (next-single-property-change next 'mime-view-entity
3292                                                     nil (point-max)))
3293             (save-restriction
3294               (narrow-to-region (point) next)
3295               (gnus-article-prepare-mime-display)
3296               (goto-char (point-max)))
3297             (setq entity (get-text-property (point) 'mime-view-entity)))
3298         (save-restriction
3299           (narrow-to-region (point) next)
3300           ;; Kludge. We have to count true number, but for now,
3301           ;; part number is here only to achieve `last'.
3302           (gnus-treat-article nil 1
3303                               (if (eq entity last-entity)
3304                                   1 2)
3305                               type)
3306           (goto-char (point-max)))
3307         (setq entity (get-text-property next 'mime-view-entity))))))
3308
3309 ;;;###autoload
3310 (defun gnus-article-prepare-display ()
3311   "Make the current buffer look like a nice article."
3312   (setq gnus-article-wash-types nil)
3313   (gnus-run-hooks 'gnus-tmp-internal-hook)
3314   (gnus-run-hooks 'gnus-article-prepare-hook)
3315   ;; Display message.
3316   (let (mime-display-header-hook mime-display-text/plain-hook)
3317     (funcall (if gnus-show-mime
3318                  (progn
3319                    (setq mime-message-structure gnus-current-headers)
3320                    (mime-buffer-entity-set-buffer-internal
3321                     mime-message-structure
3322                     gnus-original-article-buffer)
3323                    (mime-entity-set-representation-type-internal
3324                     mime-message-structure 'mime-buffer-entity)
3325                    (luna-send mime-message-structure
3326                               'initialize-instance
3327                               mime-message-structure)
3328                    gnus-article-display-method-for-mime)
3329                gnus-article-display-method-for-traditional)))
3330   ;; Associate this article with the current summary buffer.
3331   (setq gnus-article-current-summary gnus-summary-buffer)
3332   ;; Call the treatment functions.
3333   (let ((inhibit-read-only t)
3334         buffer-read-only)
3335     (save-restriction
3336       (widen)
3337       (if gnus-show-mime
3338           (gnus-article-prepare-mime-display)
3339         (narrow-to-region (goto-char (point-min))
3340                           (if (search-forward "\n\n" nil t)
3341                               (point)
3342                             (point-max)))
3343         (gnus-treat-article 'head)
3344         (put-text-property (point-min) (point-max) 'article-treated-header t)
3345         (goto-char (point-max))
3346         (widen)
3347         (narrow-to-region (point) (point-max))
3348         (gnus-treat-article nil))
3349       (put-text-property (point-min) (point-max) 'read-only nil)))
3350   ;; Perform the article display hooks.  Incidentally, this hook is
3351   ;; an obsolete variable by now.
3352   (gnus-run-hooks 'gnus-article-display-hook))
3353
3354 (defun gnus-article-decode-article-as-default-mime-charset ()
3355   "Decode an article as `default-mime-charset'.  It won't work if the
3356 value of the variable `gnus-show-mime' is non-nil."
3357   (unless gnus-show-mime
3358     (decode-mime-charset-region (point-min) (point-max)
3359                                 (with-current-buffer gnus-summary-buffer
3360                                   default-mime-charset))))
3361
3362 ;;;
3363 ;;; Gnus MIME viewing functions
3364 ;;;
3365
3366 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3367   "The following specs can be used:
3368 %t  The MIME type
3369 %T  MIME type, along with additional info
3370 %n  The `name' parameter
3371 %d  The description, if any
3372 %l  The length of the encoded part
3373 %p  The part identifier number
3374 %e  Dots if the part isn't displayed")
3375
3376 (defvar gnus-mime-button-line-format-alist
3377   '((?t gnus-tmp-type ?s)
3378     (?T gnus-tmp-type-long ?s)
3379     (?n gnus-tmp-name ?s)
3380     (?d gnus-tmp-description ?s)
3381     (?p gnus-tmp-id ?s)
3382     (?l gnus-tmp-length ?d)
3383     (?e gnus-tmp-dots ?s)))
3384
3385 (defvar gnus-mime-button-commands
3386   '((gnus-article-press-button "\r" "Toggle Display")
3387     (gnus-mime-view-part "v" "View Interactively...")
3388     (gnus-mime-view-part-as-type "t" "View As Type...")
3389     (gnus-mime-view-part-as-charset "C" "View As charset...")
3390     (gnus-mime-save-part "o" "Save...")
3391     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3392     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3393     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3394     (gnus-mime-internalize-part "E" "View Internally")
3395     (gnus-mime-externalize-part "e" "View Externally")
3396     (gnus-mime-pipe-part "|" "Pipe To Command...")
3397     (gnus-mime-action-on-part "." "Take action on the part")))
3398
3399 (defun gnus-article-mime-part-status ()
3400   (with-current-buffer gnus-article-buffer
3401     (let ((entity (get-text-property (point-min) 'mime-view-entity)))
3402       (if (and entity (mime-entity-children entity))
3403           (format " (%d parts)" (length (mime-entity-children entity)))
3404         ""))))
3405
3406 (defvar gnus-mime-button-map
3407   (let ((map (make-sparse-keymap)))
3408     (set-keymap-parent map gnus-article-mode-map)
3409     (define-key map gnus-mouse-2 'gnus-article-push-button)
3410     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3411     (dolist (c gnus-mime-button-commands)
3412       (define-key map (cadr c) (car c)))
3413     map))
3414
3415 (defun gnus-mime-button-menu (event)
3416   "Construct a context-sensitive menu of MIME commands."
3417   (interactive "e")
3418   (save-window-excursion
3419     (let ((pos (event-start event)))
3420       (select-window (posn-window pos))
3421       (goto-char (posn-point pos))
3422       (gnus-article-check-buffer)
3423       (let ((response (x-popup-menu
3424                        t `("MIME Part"
3425                            ("" ,@(mapcar (lambda (c)
3426                                            (cons (caddr c) (car c)))
3427                                          gnus-mime-button-commands))))))
3428         (if response
3429             (call-interactively response))))))
3430
3431 (defun gnus-mime-view-all-parts (&optional handles)
3432   "View all the MIME parts."
3433   (interactive)
3434   (save-current-buffer
3435     (set-buffer gnus-article-buffer)
3436     (let ((handles (or handles gnus-article-mime-handles))
3437           (mail-parse-charset gnus-newsgroup-charset)
3438           (mail-parse-ignored-charsets 
3439            (with-current-buffer gnus-summary-buffer
3440              gnus-newsgroup-ignored-charsets)))
3441       (mm-remove-parts handles)
3442       (goto-char (point-min))
3443       (or (search-forward "\n\n") (goto-char (point-max)))
3444       (let (buffer-read-only)
3445         (delete-region (point) (point-max)))
3446       (mm-display-parts handles))))
3447
3448 (defun gnus-mime-save-part-and-strip ()
3449   "Save the MIME part under point then replace it with an external body."
3450   (interactive)
3451   (gnus-article-check-buffer)
3452   (let* ((data (get-text-property (point) 'gnus-data)) 
3453          (file (mm-save-part data))
3454          param)
3455     (when file
3456       (with-current-buffer (mm-handle-buffer data)
3457         (erase-buffer)
3458         (insert "Content-Type: " (mm-handle-media-type data))
3459         (mml-insert-parameter-string (cdr (mm-handle-type data))
3460                                      '(charset))
3461         (insert "\n")
3462         (insert "Content-ID: " (message-make-message-id) "\n")
3463         (insert "Content-Transfer-Encoding: binary\n")
3464         (insert "\n"))
3465       (setcdr data
3466               (cdr (mm-make-handle nil 
3467                                    `("message/external-body"
3468                                      (access-type . "LOCAL-FILE")
3469                                      (name . ,file)))))
3470       (set-buffer gnus-summary-buffer)
3471       (gnus-article-edit-article
3472        `(lambda () 
3473            (erase-buffer)
3474            (let ((mail-parse-charset (or gnus-article-charset 
3475                                          ',gnus-newsgroup-charset))
3476                  (mail-parse-ignored-charsets 
3477                   (or gnus-article-ignored-charsets
3478                       ',gnus-newsgroup-ignored-charsets))
3479                  (mbl mml-buffer-list))
3480              (setq mml-buffer-list nil)
3481              (insert-buffer gnus-original-article-buffer)
3482              (mime-to-mml gnus-article-mime-handles)
3483              (setq gnus-article-mime-handles nil)
3484              (make-local-hook 'kill-buffer-hook)
3485              (let ((mbl1 mml-buffer-list))
3486                (setq mml-buffer-list mbl)
3487                (set (make-local-variable 'mml-buffer-list) mbl1))
3488              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3489        `(lambda (no-highlight)
3490           (let ((mail-parse-charset (or gnus-article-charset
3491                                         ',gnus-newsgroup-charset))
3492                 (message-options message-options)
3493                 (message-options-set-recipient)
3494                 (mail-parse-ignored-charsets  
3495                  (or gnus-article-ignored-charsets
3496                      ',gnus-newsgroup-ignored-charsets)))
3497            (mml-to-mime)
3498            (mml-destroy-buffers)
3499            (remove-hook 'kill-buffer-hook 
3500                         'mml-destroy-buffers t)
3501            (kill-local-variable 'mml-buffer-list))
3502           (gnus-summary-edit-article-done
3503            ,(or (mail-header-references gnus-current-headers) "")
3504            ,(gnus-group-read-only-p) 
3505            ,gnus-summary-buffer no-highlight))))))
3506
3507 (defun gnus-mime-save-part ()
3508   "Save the MIME part under point."
3509   (interactive)
3510   (gnus-article-check-buffer)
3511   (let ((data (get-text-property (point) 'gnus-data)))
3512     (mm-save-part data)))
3513
3514 (defun gnus-mime-pipe-part ()
3515   "Pipe the MIME part under point to a process."
3516   (interactive)
3517   (gnus-article-check-buffer)
3518   (let ((data (get-text-property (point) 'gnus-data)))
3519     (mm-pipe-part data)))
3520
3521 (defun gnus-mime-view-part ()
3522   "Interactively choose a viewing method for the MIME part under point."
3523   (interactive)
3524   (gnus-article-check-buffer)
3525   (let ((data (get-text-property (point) 'gnus-data)))
3526     (mm-interactively-view-part data)))
3527
3528 (defun gnus-mime-view-part-as-type-internal ()
3529   (gnus-article-check-buffer)
3530   (let* ((name (mail-content-type-get
3531                 (mm-handle-type (get-text-property (point) 'gnus-data))
3532                 'name))
3533          (def-type (and name (mm-default-file-encoding name))))
3534     (and def-type (cons def-type 0))))
3535
3536 (defun gnus-mime-view-part-as-type (mime-type)
3537   "Choose a MIME media type, and view the part as such."
3538   (interactive
3539    (list (completing-read
3540           "View as MIME type: "
3541           (mapcar #'list (mailcap-mime-types))
3542           nil nil
3543           (gnus-mime-view-part-as-type-internal))))
3544   (gnus-article-check-buffer)
3545   (let ((handle (get-text-property (point) 'gnus-data)))
3546     (gnus-mm-display-part
3547      (mm-make-handle (mm-handle-buffer handle)
3548                      (cons mime-type (cdr (mm-handle-type handle)))
3549                      (mm-handle-encoding handle)
3550                      (mm-handle-undisplayer handle)
3551                      (mm-handle-disposition handle)
3552                      (mm-handle-description handle)
3553                      (mm-handle-cache handle)
3554                      (mm-handle-id handle)))))
3555
3556 (defun gnus-mime-copy-part (&optional handle)
3557   "Put the the MIME part under point into a new buffer."
3558   (interactive)
3559   (gnus-article-check-buffer)
3560   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3561          (contents (mm-get-part handle))|
3562          (base (file-name-nondirectory
3563                 (or
3564                  (mail-content-type-get (mm-handle-type handle) 'name)
3565                  (mail-content-type-get (mm-handle-type handle)
3566                                         'filename)
3567                  "*decoded*")))
3568          (buffer (generate-new-buffer base)))
3569     (switch-to-buffer buffer)
3570     (insert contents)
3571     ;; We do it this way to make `normal-mode' set the appropriate mode.
3572     (unwind-protect
3573         (progn
3574           (setq buffer-file-name (expand-file-name base))
3575           (normal-mode))
3576       (setq buffer-file-name nil))
3577     (goto-char (point-min))))
3578
3579 (defun gnus-mime-inline-part (&optional handle arg)
3580   "Insert the MIME part under point into the current buffer."
3581   (interactive (list nil current-prefix-arg))
3582   (gnus-article-check-buffer)
3583   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3584          contents charset
3585          (b (point))
3586          buffer-read-only)
3587     (if (and (not arg) (mm-handle-undisplayer handle))
3588         (mm-remove-part handle)
3589       (setq contents (mm-get-part handle))
3590       (cond
3591        ((not arg)
3592         (setq charset (or (mail-content-type-get
3593                            (mm-handle-type handle) 'charset)
3594                           gnus-newsgroup-charset)))
3595        ((numberp arg)
3596         (if (mm-handle-undisplayer handle)
3597             (mm-remove-part handle))
3598         (setq charset
3599               (or (cdr (assq arg 
3600                              gnus-summary-show-article-charset-alist))
3601                   (read-coding-system "Charset: ")))))
3602       (forward-line 2)
3603       (mm-insert-inline handle
3604                         (if (and charset 
3605                                  (setq charset (mm-charset-to-coding-system 
3606                                                 charset))
3607                                  (not (eq charset 'ascii)))
3608                             (mm-decode-coding-string contents charset)
3609                           contents))
3610       (goto-char b))))
3611
3612 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3613   "Insert the MIME part under point into the current buffer."
3614   (interactive (list nil current-prefix-arg))
3615   (gnus-article-check-buffer)
3616   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3617          contents charset
3618          (b (point))
3619          buffer-read-only)
3620     (if (mm-handle-undisplayer handle)
3621         (mm-remove-part handle))
3622     (let ((gnus-newsgroup-charset
3623            (or (cdr (assq arg 
3624                           gnus-summary-show-article-charset-alist))
3625                (read-coding-system "Charset: ")))
3626           (gnus-newsgroup-ignored-charsets 'gnus-all))
3627       (gnus-article-press-button))))
3628
3629 (defun gnus-mime-externalize-part (&optional handle)
3630   "View the MIME part under point with an external viewer."
3631   (interactive)
3632   (gnus-article-check-buffer)
3633   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3634          (mm-user-display-methods nil)
3635          (mm-inlined-types nil)
3636          (mail-parse-charset gnus-newsgroup-charset)
3637          (mail-parse-ignored-charsets 
3638           (save-excursion (set-buffer gnus-summary-buffer)
3639                           gnus-newsgroup-ignored-charsets)))
3640     (if (mm-handle-undisplayer handle)
3641         (mm-remove-part handle)
3642       (mm-display-part handle))))
3643
3644 (defun gnus-mime-internalize-part (&optional handle)
3645   "View the MIME part under point with an internal viewer.
3646 In no internal viewer is available, use an external viewer."
3647   (interactive)
3648   (gnus-article-check-buffer)
3649   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3650          (mm-inlined-types '(".*"))
3651          (mm-inline-large-images t)
3652          (mail-parse-charset gnus-newsgroup-charset)
3653          (mail-parse-ignored-charsets 
3654           (save-excursion (set-buffer gnus-summary-buffer)
3655                           gnus-newsgroup-ignored-charsets)))
3656     (if (mm-handle-undisplayer handle)
3657         (mm-remove-part handle)
3658       (mm-display-part handle))))
3659
3660 (defun gnus-mime-action-on-part (&optional action)
3661   "Do something with the MIME attachment at \(point\)."
3662   (interactive
3663    (list (completing-read "Action: " gnus-mime-action-alist)))
3664   (gnus-article-check-buffer)
3665   (let ((action-pair (assoc action gnus-mime-action-alist)))
3666     (if action-pair
3667         (funcall (cdr action-pair)))))
3668
3669
3670 (defun gnus-article-part-wrapper (n function)
3671   (save-current-buffer
3672     (set-buffer gnus-article-buffer)
3673     (when (> n (length gnus-article-mime-handle-alist))
3674       (error "No such part"))
3675     (gnus-article-goto-part n)
3676     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3677       (funcall function handle))))
3678
3679 (defun gnus-article-pipe-part (n)
3680   "Pipe MIME part N, which is the numerical prefix."
3681   (interactive "p")
3682   (gnus-article-part-wrapper n 'mm-pipe-part))
3683
3684 (defun gnus-article-save-part (n)
3685   "Save MIME part N, which is the numerical prefix."
3686   (interactive "p")
3687   (gnus-article-part-wrapper n 'mm-save-part))
3688
3689 (defun gnus-article-interactively-view-part (n)
3690   "View MIME part N interactively, which is the numerical prefix."
3691   (interactive "p")
3692   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3693
3694 (defun gnus-article-copy-part (n)
3695   "Copy MIME part N, which is the numerical prefix."
3696   (interactive "p")
3697   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3698
3699 (defun gnus-article-view-part-as-charset (n)
3700   "Copy MIME part N, which is the numerical prefix."
3701   (interactive "p")
3702   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3703
3704 (defun gnus-article-externalize-part (n)
3705   "View MIME part N externally, which is the numerical prefix."
3706   (interactive "p")
3707   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3708
3709 (defun gnus-article-inline-part (n)
3710   "Inline MIME part N, which is the numerical prefix."
3711   (interactive "p")
3712   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3713
3714 (defun gnus-article-mime-match-handle-first (condition)
3715   (if condition
3716       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3717         (while (setq ihandle (pop alist))
3718           (if (and (cond 
3719                     ((functionp condition)
3720                      (funcall condition (cdr ihandle)))
3721                     ((eq condition 'undisplayed) 
3722                      (not (or (mm-handle-undisplayer (cdr ihandle))
3723                               (equal (mm-handle-media-type (cdr ihandle))
3724                                      "multipart/alternative"))))
3725                     ((eq condition 'undisplayed-alternative)
3726                      (not (mm-handle-undisplayer (cdr ihandle))))
3727                     (t t))
3728                    (gnus-article-goto-part (car ihandle))
3729                    (or (not n) (< (car ihandle) n)))
3730               (setq n (car ihandle))))
3731         (or n 1))
3732     1))
3733
3734 (defun gnus-article-view-part (&optional n)
3735   "View MIME part N, which is the numerical prefix."
3736   (interactive "P")
3737   (save-current-buffer
3738     (set-buffer gnus-article-buffer)
3739     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3740                              gnus-article-mime-match-handle-function)))
3741     (when (> n (length gnus-article-mime-handle-alist))
3742       (error "No such part"))
3743     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3744       (when (gnus-article-goto-part n)
3745         (if (equal (car handle) "multipart/alternative")
3746             (gnus-article-press-button)
3747           (when (eq (gnus-mm-display-part handle) 'internal)
3748             (gnus-set-window-start)))))))
3749
3750 (defsubst gnus-article-mime-total-parts ()
3751   (if (bufferp (car gnus-article-mime-handles))
3752       1 ;; single part
3753     (1- (length gnus-article-mime-handles))))
3754
3755 (defun gnus-mm-display-part (handle)
3756   "Display HANDLE and fix MIME button."
3757   (let ((id (get-text-property (point) 'gnus-part))
3758         (point (point))
3759         buffer-read-only)
3760     (forward-line 1)
3761     (prog1
3762         (let ((window (selected-window))
3763               (mail-parse-charset gnus-newsgroup-charset)
3764               (mail-parse-ignored-charsets 
3765                (save-excursion (set-buffer gnus-summary-buffer)
3766                                gnus-newsgroup-ignored-charsets)))
3767           (save-excursion
3768             (unwind-protect
3769                 (let ((win (get-buffer-window (current-buffer) t))
3770                       (beg (point)))
3771                   (when win
3772                     (select-window win))
3773                   (goto-char point)
3774                   (forward-line)
3775                   (if (mm-handle-displayed-p handle)
3776                       ;; This will remove the part.
3777                       (mm-display-part handle)
3778                     (save-restriction
3779                       (narrow-to-region (point) (1+ (point)))
3780                       (mm-display-part handle)
3781                       ;; We narrow to the part itself and
3782                       ;; then call the treatment functions.
3783                       (goto-char (point-min))
3784                       (forward-line 1)
3785                       (narrow-to-region (point) (point-max))
3786                       (gnus-treat-article
3787                        nil id
3788                        (gnus-article-mime-total-parts)
3789                        (mm-handle-media-type handle)))))
3790               (select-window window))))
3791       (goto-char point)
3792       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3793       (gnus-insert-mime-button
3794        handle id (list (mm-handle-displayed-p handle)))
3795       (goto-char point))))
3796
3797 (defun gnus-article-goto-part (n)
3798   "Go to MIME part N."
3799   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3800     (when point
3801       (goto-char point))))
3802
3803 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3804   (let ((gnus-tmp-name
3805          (or (mail-content-type-get (mm-handle-type handle)
3806                                     'name)
3807              (mail-content-type-get (mm-handle-disposition handle)
3808                                     'filename)
3809              (mail-content-type-get (mm-handle-type handle)
3810                                     'url)
3811              ""))
3812         (gnus-tmp-type (mm-handle-media-type handle))
3813         (gnus-tmp-description
3814          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3815                                               "")))
3816         (gnus-tmp-dots
3817          (if (if displayed (car displayed)
3818                (mm-handle-displayed-p handle))
3819              "" "..."))
3820         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3821                            (buffer-size)))
3822         gnus-tmp-type-long b e)
3823     (when (string-match ".*/" gnus-tmp-name)
3824       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3825     (setq gnus-tmp-type-long (concat gnus-tmp-type
3826                                      (and (not (equal gnus-tmp-name ""))
3827                                           (concat "; " gnus-tmp-name))))
3828     (or (equal gnus-tmp-description "")
3829         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3830     (unless (bolp)
3831       (insert "\n"))
3832     (setq b (point))
3833     (gnus-eval-format
3834      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3835      `(local-map ,gnus-mime-button-map
3836                  keymap ,gnus-mime-button-map
3837                  gnus-callback gnus-mm-display-part
3838                  gnus-part ,gnus-tmp-id
3839                  article-type annotation
3840                  gnus-data ,handle))
3841     (setq e (point))
3842     (widget-convert-button
3843      'link b e
3844      :mime-handle handle
3845      :action 'gnus-widget-press-button
3846      :button-keymap gnus-mime-button-map
3847      :help-echo
3848      (lambda (widget/window &optional overlay pos)
3849        ;; Needed to properly clear the message due to a bug in
3850        ;; wid-edit (XEmacs only).
3851        (if (boundp 'help-echo-owns-message)
3852            (setq help-echo-owns-message t))
3853        (format
3854         "%S: %s the MIME part; %S: more options"
3855         (aref gnus-mouse-2 0)
3856         ;; XEmacs will get a single widget arg; Emacs 21 will get
3857         ;; window, overlay, position.
3858         (if (mm-handle-displayed-p
3859              (if overlay
3860                  (with-current-buffer (gnus-overlay-buffer overlay)
3861                    (widget-get (widget-at (gnus-overlay-start overlay))
3862                                :mime-handle))
3863                (widget-get widget/window :mime-handle)))
3864             "hide" "show")
3865         (aref gnus-down-mouse-3 0))))))
3866
3867 (defun gnus-widget-press-button (elems el)
3868   (goto-char (widget-get elems :from))
3869   (gnus-article-press-button))
3870
3871 (defvar gnus-displaying-mime nil)
3872
3873 (defun gnus-display-mime (&optional ihandles)
3874   "Display the MIME parts."
3875   (save-excursion
3876     (save-selected-window
3877       (let ((window (get-buffer-window gnus-article-buffer))
3878             (point (point)))
3879         (when window
3880           (select-window window)
3881           ;; We have to do this since selecting the window
3882           ;; may change the point.  So we set the window point.
3883           (set-window-point window point)))
3884       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3885              buffer-read-only handle name type b e display)
3886         (when (and (not ihandles)
3887                    (not gnus-displaying-mime))
3888           ;; Top-level call; we clean up.
3889           (when gnus-article-mime-handles
3890             (mm-destroy-parts gnus-article-mime-handles)
3891             (setq gnus-article-mime-handle-alist nil));; A trick.
3892           (setq gnus-article-mime-handles handles)
3893           ;; We allow users to glean info from the handles.
3894           (when gnus-article-mime-part-function
3895             (gnus-mime-part-function handles)))
3896         (if (and handles
3897                  (or (not (stringp (car handles)))
3898                      (cdr handles)))
3899             (progn
3900               (when (and (not ihandles)
3901                          (not gnus-displaying-mime))
3902                 ;; Clean up for mime parts.
3903                 (article-goto-body)
3904                 (delete-region (point) (point-max)))
3905               (let ((gnus-displaying-mime t))
3906                 (gnus-mime-display-part handles)))
3907           (save-restriction
3908             (article-goto-body)
3909             (narrow-to-region (point) (point-max))
3910             (gnus-treat-article nil 1 1)
3911             (widen)))
3912         (unless ihandles
3913           ;; Highlight the headers.
3914           (save-excursion
3915             (save-restriction
3916               (article-goto-body)
3917               (narrow-to-region (point-min) (point))
3918               (gnus-treat-article 'head))))))))
3919
3920 (defvar gnus-mime-display-multipart-as-mixed nil)
3921
3922 (defun gnus-mime-display-part (handle)
3923   (cond
3924    ;; Single part.
3925    ((not (stringp (car handle)))
3926     (gnus-mime-display-single handle))
3927    ;; User-defined multipart
3928    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3929     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3930              handle))
3931    ;; multipart/alternative
3932    ((and (equal (car handle) "multipart/alternative")
3933          (not gnus-mime-display-multipart-as-mixed))
3934     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3935       (push (cons id handle) gnus-article-mime-handle-alist)
3936       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3937    ;; multipart/related
3938    ((and (equal (car handle) "multipart/related")
3939          (not gnus-mime-display-multipart-as-mixed))
3940     ;;;!!!We should find the start part, but we just default
3941     ;;;!!!to the first part.
3942     ;;(gnus-mime-display-part (cadr handle))
3943     ;;;!!! Most multipart/related is an HTML message plus images.
3944     ;;;!!! Unfortunately we are unable to let W3 display those 
3945     ;;;!!! included images, so we just display it as a mixed multipart.
3946     (gnus-mime-display-mixed (cdr handle)))
3947    ((equal (car handle) "multipart/signed")
3948     (or (memq 'signed gnus-article-wash-types)
3949         (push 'signed gnus-article-wash-types))
3950     (gnus-mime-display-security handle))
3951    ((equal (car handle) "multipart/encrypted")
3952     (or (memq 'encrypted gnus-article-wash-types)
3953         (push 'encrypted gnus-article-wash-types))
3954     (gnus-mime-display-security handle))
3955    ;; Other multiparts are handled like multipart/mixed.
3956    (t
3957     (gnus-mime-display-mixed (cdr handle)))))
3958
3959 (defun gnus-mime-part-function (handles)
3960   (if (stringp (car handles))
3961       (mapcar 'gnus-mime-part-function (cdr handles))
3962     (funcall gnus-article-mime-part-function handles)))
3963
3964 (defun gnus-mime-display-mixed (handles)
3965   (mapcar 'gnus-mime-display-part handles))
3966
3967 (defun gnus-mime-display-single (handle)
3968   (let ((type (mm-handle-media-type handle))
3969         (ignored gnus-ignored-mime-types)
3970         (not-attachment t)
3971         (move nil)
3972         display text)
3973     (catch 'ignored
3974       (progn
3975         (while ignored
3976           (when (string-match (pop ignored) type)
3977             (throw 'ignored nil)))
3978         (if (and (setq not-attachment
3979                        (and (not (mm-inline-override-p handle))
3980                             (or (not (mm-handle-disposition handle))
3981                                 (equal (car (mm-handle-disposition handle))
3982                                        "inline")
3983                                 (mm-attachment-override-p handle))))
3984                  (mm-automatic-display-p handle)
3985                  (or (mm-inlined-p handle)
3986                      (mm-automatic-external-display-p type)))
3987             (setq display t)
3988           (when (equal (mm-handle-media-supertype handle) "text")
3989             (setq text t)))
3990         (let ((id (1+ (length gnus-article-mime-handle-alist)))
3991               beg)
3992           (push (cons id handle) gnus-article-mime-handle-alist)
3993           (when (or (not display)
3994                     (not (gnus-unbuttonized-mime-type-p type)))
3995             ;(gnus-article-insert-newline)
3996             (gnus-insert-mime-button
3997              handle id (list (or display (and not-attachment text))))
3998             (gnus-article-insert-newline) 
3999             ;(gnus-article-insert-newline) 
4000             ;; Remember modify the number of forward lines.
4001             (setq move t))
4002           (setq beg (point))
4003           (cond
4004            (display
4005             (when move
4006               (forward-line -1)
4007               (setq beg (point)))
4008             (let ((mail-parse-charset gnus-newsgroup-charset)
4009                   (mail-parse-ignored-charsets 
4010                    (save-excursion (condition-case ()
4011                                        (set-buffer gnus-summary-buffer)
4012                                      (error))
4013                                    gnus-newsgroup-ignored-charsets)))
4014               (mm-display-part handle t))
4015             (goto-char (point-max)))
4016            ((and text not-attachment)
4017             (when move
4018               (forward-line -1)
4019               (setq beg (point)))
4020             (gnus-article-insert-newline)
4021             (mm-insert-inline handle (mm-get-part handle))
4022             (goto-char (point-max))))
4023           ;; Do highlighting.
4024           (save-excursion
4025             (save-restriction
4026               (narrow-to-region beg (point))
4027               (gnus-treat-article
4028                nil id 
4029                (gnus-article-mime-total-parts)
4030                (mm-handle-media-type handle)))))))))
4031
4032 (defun gnus-unbuttonized-mime-type-p (type)
4033   "Say whether TYPE is to be unbuttonized."
4034   (unless gnus-inhibit-mime-unbuttonizing
4035     (catch 'found
4036       (let ((types gnus-unbuttonized-mime-types))
4037         (while types
4038           (when (string-match (pop types) type)
4039             (throw 'found t)))))))
4040
4041 (defun gnus-article-insert-newline ()
4042   "Insert a newline, but mark it as undeletable."
4043   (gnus-put-text-property
4044    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4045
4046 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4047   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4048          (ihandles handles)
4049          (point (point))
4050          handle buffer-read-only from props begend not-pref)
4051     (save-window-excursion
4052       (save-restriction
4053         (when ibegend
4054           (narrow-to-region (car ibegend)
4055                             (or (cdr ibegend)
4056                                 (progn
4057                                   (goto-char (car ibegend))
4058                                   (forward-line 2)
4059                                   (point))))
4060           (delete-region (point-min) (point-max))
4061           (mm-remove-parts handles))
4062         (setq begend (list (point-marker)))
4063         ;; Do the toggle.
4064         (unless (setq not-pref (cadr (member preferred ihandles)))
4065           (setq not-pref (car ihandles)))
4066         (when (or ibegend
4067                   (not preferred)
4068                   (not (gnus-unbuttonized-mime-type-p
4069                         "multipart/alternative")))
4070           (gnus-add-text-properties
4071            (setq from (point))
4072            (progn
4073              (insert (format "%d.  " id))
4074              (point))
4075            `(gnus-callback
4076              (lambda (handles)
4077                (unless ,(not ibegend)
4078                  (setq gnus-article-mime-handle-alist
4079                        ',gnus-article-mime-handle-alist))
4080                (gnus-mime-display-alternative
4081                 ',ihandles ',not-pref ',begend ,id))
4082              local-map ,gnus-mime-button-map
4083              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4084              face ,gnus-article-button-face
4085              keymap ,gnus-mime-button-map
4086              gnus-part ,id
4087              gnus-data ,handle))
4088           (widget-convert-button 'link from (point)
4089                                  :action 'gnus-widget-press-button
4090                                  :button-keymap gnus-widget-button-keymap)
4091           ;; Do the handles
4092           (while (setq handle (pop handles))
4093             (gnus-add-text-properties
4094              (setq from (point))
4095              (progn
4096                (insert (format "(%c) %-18s"
4097                                (if (equal handle preferred) ?* ? )
4098                                (mm-handle-media-type handle)))
4099                (point))
4100              `(gnus-callback
4101                (lambda (handles)
4102                  (unless ,(not ibegend)
4103                    (setq gnus-article-mime-handle-alist
4104                          ',gnus-article-mime-handle-alist))
4105                  (gnus-mime-display-alternative
4106                   ',ihandles ',handle ',begend ,id))
4107                local-map ,gnus-mime-button-map
4108                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4109                face ,gnus-article-button-face
4110                keymap ,gnus-mime-button-map
4111                gnus-part ,id
4112                gnus-data ,handle))
4113             (widget-convert-button 'link from (point)
4114                                    :action 'gnus-widget-press-button
4115                                    :button-keymap gnus-widget-button-keymap)
4116             (insert "  "))
4117           (insert "\n\n"))
4118         (when preferred
4119           (if (stringp (car preferred))
4120               (gnus-display-mime preferred)
4121             (let ((mail-parse-charset gnus-newsgroup-charset)
4122                   (mail-parse-ignored-charsets 
4123                    (save-excursion (set-buffer gnus-summary-buffer)
4124                                    gnus-newsgroup-ignored-charsets)))
4125               (mm-display-part preferred)
4126               ;; Do highlighting.
4127               (save-excursion
4128                 (save-restriction
4129                   (narrow-to-region (car begend) (point-max))
4130                   (gnus-treat-article
4131                    nil (length gnus-article-mime-handle-alist)
4132                    (gnus-article-mime-total-parts)
4133                    (mm-handle-media-type handle))))))
4134           (goto-char (point-max))
4135           (setcdr begend (point-marker)))))
4136     (when ibegend
4137       (goto-char point))))
4138
4139 (defun gnus-article-wash-status ()
4140   "Return a string which display status of article washing."
4141   (save-excursion
4142     (set-buffer gnus-article-buffer)
4143     (let ((cite (memq 'cite gnus-article-wash-types))
4144           (headers (memq 'headers gnus-article-wash-types))
4145           (boring (memq 'boring-headers gnus-article-wash-types))
4146           (pgp (memq 'pgp gnus-article-wash-types))
4147           (pem (memq 'pem gnus-article-wash-types))
4148           (signed (memq 'signed gnus-article-wash-types))
4149           (encrypted (memq 'encrypted gnus-article-wash-types))
4150           (signature (memq 'signature gnus-article-wash-types))
4151           (overstrike (memq 'overstrike gnus-article-wash-types))
4152           (emphasis (memq 'emphasis gnus-article-wash-types)))
4153       (format "%c%c%c%c%c%c%c"
4154               (if cite ?c ? )
4155               (if (or headers boring) ?h ? )
4156               (if (or pgp pem signed encrypted) ?p ? )
4157               (if signature ?s ? )
4158               (if overstrike ?o ? )
4159               (if gnus-show-mime ?m ? )
4160               (if emphasis ?e ? )))))
4161
4162 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4163
4164 (defun gnus-article-maybe-hide-headers ()
4165   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4166 Provided for backwards compatibility."
4167   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4168                  (not (save-excursion (set-buffer gnus-summary-buffer)
4169                                       gnus-have-all-headers)))
4170              (not gnus-inhibit-hiding))
4171     (gnus-article-hide-headers)))
4172
4173 ;;; Article savers.
4174
4175 (defun gnus-output-to-file (file-name)
4176   "Append the current article to a file named FILE-NAME."
4177   (let ((artbuf (current-buffer)))
4178     (with-temp-buffer
4179       (insert-buffer-substring artbuf)
4180       ;; Append newline at end of the buffer as separator, and then
4181       ;; save it to file.
4182       (goto-char (point-max))
4183       (insert "\n")
4184       (write-region-as-binary (point-min) (point-max) file-name 'append)
4185       t)))
4186
4187 (defun gnus-narrow-to-page (&optional arg)
4188   "Narrow the article buffer to a page.
4189 If given a numerical ARG, move forward ARG pages."
4190   (interactive "P")
4191   (setq arg (if arg (prefix-numeric-value arg) 0))
4192   (save-excursion
4193     (set-buffer gnus-article-buffer)
4194     (goto-char (point-min))
4195     (widen)
4196     ;; Remove any old next/prev buttons.
4197     (when (gnus-visual-p 'page-marker)
4198       (let ((buffer-read-only nil))
4199         (gnus-remove-text-with-property 'gnus-prev)
4200         (gnus-remove-text-with-property 'gnus-next)))
4201     (when
4202         (cond ((< arg 0)
4203                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4204               ((> arg 0)
4205                (re-search-forward page-delimiter nil 'move arg)))
4206       (goto-char (match-end 0)))
4207     (narrow-to-region
4208      (point)
4209      (if (re-search-forward page-delimiter nil 'move)
4210          (match-beginning 0)
4211        (point)))
4212     (when (and (gnus-visual-p 'page-marker)
4213                (not (= (point-min) 1)))
4214       (save-excursion
4215         (goto-char (point-min))
4216         (gnus-insert-prev-page-button)))
4217     (when (and (gnus-visual-p 'page-marker)
4218                (< (+ (point-max) 2) (buffer-size)))
4219       (save-excursion
4220         (goto-char (point-max))
4221         (gnus-insert-next-page-button)))))
4222
4223 ;; Article mode commands
4224
4225 (defun gnus-article-goto-next-page ()
4226   "Show the next page of the article."
4227   (interactive)
4228   (when (gnus-article-next-page)
4229     (goto-char (point-min))
4230     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4231
4232 (defun gnus-article-goto-prev-page ()
4233   "Show the next page of the article."
4234   (interactive)
4235   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4236     (gnus-article-prev-page nil)))
4237
4238 (defun gnus-article-next-page (&optional lines)
4239   "Show the next page of the current article.
4240 If end of article, return non-nil.  Otherwise return nil.
4241 Argument LINES specifies lines to be scrolled up."
4242   (interactive "p")
4243   (let ((start (window-start))
4244         end-of-buffer end-of-page)
4245     (save-excursion
4246       (move-to-window-line -1)
4247       (if (<= (point) start)
4248           (progn
4249             (forward-line 2)
4250             (setq start (point)))
4251         (forward-line 1)
4252         (setq start nil))
4253       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4254                          (and (pos-visible-in-window-p)
4255                               (setq end-of-buffer t)))
4256                         ((eobp)
4257                          (setq end-of-page t)))
4258                   (not lines))
4259         (move-to-window-line lines)
4260         (unless (search-backward "\n\n" nil t)
4261           (setq start (point)))))
4262     (cond (end-of-buffer t)
4263           (end-of-page
4264            (gnus-narrow-to-page 1)
4265            nil)
4266           (t
4267            (if start
4268                (set-window-start (selected-window) start)
4269              (let (window-pixel-scroll-increment)
4270                (scroll-up lines)))
4271            nil))))
4272
4273 (defun gnus-article-prev-page (&optional lines)
4274   "Show previous page of current article.
4275 Argument LINES specifies lines to be scrolled down."
4276   (interactive "p")
4277   (let (beginning-of-buffer beginning-of-page)
4278     (save-excursion
4279       (move-to-window-line 0)
4280       (cond ((eq 1 (point))
4281              (setq beginning-of-buffer t))
4282             ((bobp)
4283              (setq beginning-of-page t))))
4284     (cond (beginning-of-buffer)
4285           (beginning-of-page
4286            (gnus-narrow-to-page -1))
4287           (t
4288            (condition-case nil
4289                (let (window-pixel-scroll-increment)
4290                  (scroll-down lines))
4291              (beginning-of-buffer
4292               (goto-char (point-min))))))))
4293
4294 (defun gnus-article-refer-article ()
4295   "Read article specified by message-id around point."
4296   (interactive)
4297   (let ((point (point)))
4298     (search-forward ">" nil t)          ;Move point to end of "<....>".
4299     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4300         (let ((message-id (match-string 1)))
4301           (goto-char point)
4302           (set-buffer gnus-summary-buffer)
4303           (gnus-summary-refer-article message-id))
4304       (goto-char (point))
4305       (error "No references around point"))))
4306
4307 (defun gnus-article-show-summary ()
4308   "Reconfigure windows to show summary buffer."
4309   (interactive)
4310   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4311       (error "There is no summary buffer for this article buffer")
4312     (gnus-article-set-globals)
4313     (gnus-configure-windows 'article)
4314     (gnus-summary-goto-subject gnus-current-article)
4315     (gnus-summary-position-point)))
4316
4317 (defun gnus-article-describe-briefly ()
4318   "Describe article mode commands briefly."
4319   (interactive)
4320   (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")))
4321
4322 (defun gnus-article-summary-command ()
4323   "Execute the last keystroke in the summary buffer."
4324   (interactive)
4325   (let ((obuf (current-buffer))
4326         (owin (current-window-configuration))
4327         func)
4328     (switch-to-buffer gnus-article-current-summary 'norecord)
4329     (setq func (lookup-key (current-local-map) (this-command-keys)))
4330     (call-interactively func)
4331     (set-buffer obuf)
4332     (set-window-configuration owin)
4333     (set-window-point (get-buffer-window (current-buffer)) (point))))
4334
4335 (defun gnus-article-summary-command-nosave ()
4336   "Execute the last keystroke in the summary buffer."
4337   (interactive)
4338   (let (func)
4339     (pop-to-buffer gnus-article-current-summary 'norecord)
4340     (setq func (lookup-key (current-local-map) (this-command-keys)))
4341     (call-interactively func)))
4342
4343 (defun gnus-article-check-buffer ()
4344   "Beep if not in an article buffer."
4345   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4346     (error "Command invoked outside of a Gnus article buffer")))
4347
4348 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4349   "Read a summary buffer key sequence and execute it from the article buffer."
4350   (interactive "P")
4351   (gnus-article-check-buffer)
4352   (let ((nosaves
4353          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4354            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4355            "=" "^" "\M-^" "|"))
4356         (nosave-but-article
4357          '("A\r"))
4358         (nosave-in-article
4359          '("\C-d"))
4360         (up-to-top
4361          '("n" "Gn" "p" "Gp"))
4362         keys new-sum-point)
4363     (save-excursion
4364       (set-buffer gnus-article-current-summary)
4365       (let (gnus-pick-mode)
4366         (push (or key last-command-event) unread-command-events)
4367         (setq keys (static-if (featurep 'xemacs)
4368                        (events-to-keys (read-key-sequence nil))
4369                      (read-key-sequence nil)))))
4370     (message "")
4371
4372     (if (or (member keys nosaves)
4373             (member keys nosave-but-article)
4374             (member keys nosave-in-article))
4375         (let (func)
4376           (save-window-excursion
4377             (pop-to-buffer gnus-article-current-summary 'norecord)
4378             ;; We disable the pick minor mode commands.
4379             (let (gnus-pick-mode)
4380               (setq func (lookup-key (current-local-map) keys))))
4381           (if (or (not func)
4382                   (numberp func))
4383               (ding)
4384             (unless (member keys nosave-in-article)
4385               (set-buffer gnus-article-current-summary))
4386             (call-interactively func)
4387             (setq new-sum-point (point)))
4388           (when (member keys nosave-but-article)
4389             (pop-to-buffer gnus-article-buffer 'norecord)))
4390       ;; These commands should restore window configuration.
4391       (let ((obuf (current-buffer))
4392             (owin (current-window-configuration))
4393             (opoint (point))
4394             (summary gnus-article-current-summary)
4395             func in-buffer selected)
4396         (if not-restore-window
4397             (pop-to-buffer summary 'norecord)
4398           (switch-to-buffer summary 'norecord))
4399         (setq in-buffer (current-buffer))
4400         ;; We disable the pick minor mode commands.
4401         (if (and (setq func (let (gnus-pick-mode)
4402                               (lookup-key (current-local-map) keys)))
4403                  (functionp func))
4404             (progn
4405               (call-interactively func)
4406               (setq new-sum-point (point))
4407               (when (eq in-buffer (current-buffer))
4408                 (setq selected (gnus-summary-select-article))
4409                 (set-buffer obuf)
4410                 (unless not-restore-window
4411                   (set-window-configuration owin))
4412                 (when (eq selected 'old)
4413                   (article-goto-body)
4414                   (set-window-start (get-buffer-window (current-buffer))
4415                                     1)
4416                   (set-window-point (get-buffer-window (current-buffer))
4417                                     (point)))
4418                 (let ((win (get-buffer-window gnus-article-current-summary)))
4419                   (when win
4420                     (set-window-point win new-sum-point))))    )
4421           (switch-to-buffer gnus-article-buffer)
4422           (ding))))))
4423
4424 (defun gnus-article-hide (&optional arg force)
4425   "Hide all the gruft in the current article.
4426 This means that PGP stuff, signatures, cited text and (some)
4427 headers will be hidden.
4428 If given a prefix, show the hidden text instead."
4429   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4430   (gnus-article-hide-headers arg)
4431   (gnus-article-hide-list-identifiers arg)
4432   (gnus-article-hide-pgp arg)
4433   (gnus-article-hide-citation-maybe arg force)
4434   (gnus-article-hide-signature arg))
4435
4436 (defun gnus-article-maybe-highlight ()
4437   "Do some article highlighting if article highlighting is requested."
4438   (when (gnus-visual-p 'article-highlight 'highlight)
4439     (gnus-article-highlight-some)))
4440
4441 (defun gnus-check-group-server ()
4442   ;; Make sure the connection to the server is alive.
4443   (unless (gnus-server-opened
4444            (gnus-find-method-for-group gnus-newsgroup-name))
4445     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4446     (gnus-request-group gnus-newsgroup-name t)))
4447
4448 (defun gnus-request-article-this-buffer (article group)
4449   "Get an article and insert it into this buffer."
4450   (let (do-update-line sparse-header)
4451     (prog1
4452         (save-excursion
4453           (erase-buffer)
4454           (gnus-kill-all-overlays)
4455           (setq group (or group gnus-newsgroup-name))
4456
4457           ;; Using `gnus-request-article' directly will insert the article into
4458           ;; `nntp-server-buffer' - so we'll save some time by not having to
4459           ;; copy it from the server buffer into the article buffer.
4460
4461           ;; We only request an article by message-id when we do not have the
4462           ;; headers for it, so we'll have to get those.
4463           (when (stringp article)
4464             (gnus-read-header article))
4465
4466           ;; If the article number is negative, that means that this article
4467           ;; doesn't belong in this newsgroup (possibly), so we find its
4468           ;; message-id and request it by id instead of number.
4469           (when (and (numberp article)
4470                      gnus-summary-buffer
4471                      (get-buffer gnus-summary-buffer)
4472                      (gnus-buffer-exists-p gnus-summary-buffer))
4473             (save-excursion
4474               (set-buffer gnus-summary-buffer)
4475               (let ((header (gnus-summary-article-header article)))
4476                 (when (< article 0)
4477                   (cond
4478                    ((memq article gnus-newsgroup-sparse)
4479                     ;; This is a sparse gap article.
4480                     (setq do-update-line article)
4481                     (setq article (mail-header-id header))
4482                     (setq sparse-header (gnus-read-header article))
4483                     (setq gnus-newsgroup-sparse
4484                           (delq article gnus-newsgroup-sparse)))
4485                    ((vectorp header)
4486                     ;; It's a real article.
4487                     (setq article (mail-header-id header)))
4488                    (t
4489                     ;; It is an extracted pseudo-article.
4490                     (setq article 'pseudo)
4491                     (gnus-request-pseudo-article header))))
4492
4493                 (let ((method (gnus-find-method-for-group
4494                                gnus-newsgroup-name)))
4495                   (when (and (eq (car method) 'nneething)
4496                              (vectorp header))
4497                     (let ((dir (expand-file-name
4498                                 (mail-header-subject header)
4499                                 (file-name-as-directory
4500                                  (or (cadr (assq 'nneething-address method))
4501                                      (nth 1 method))))))
4502                       (when (file-directory-p dir)
4503                         (setq article 'nneething)
4504                         (gnus-group-enter-directory dir))))))))
4505
4506           (cond
4507            ;; Refuse to select canceled articles.
4508            ((and (numberp article)
4509                  gnus-summary-buffer
4510                  (get-buffer gnus-summary-buffer)
4511                  (gnus-buffer-exists-p gnus-summary-buffer)
4512                  (eq (cdr (save-excursion
4513                             (set-buffer gnus-summary-buffer)
4514                             (assq article gnus-newsgroup-reads)))
4515                      gnus-canceled-mark))
4516             nil)
4517            ;; Check the backlog.
4518            ((and gnus-keep-backlog
4519                  (gnus-backlog-request-article group article (current-buffer)))
4520             'article)
4521            ;; Check asynchronous pre-fetch.
4522            ((gnus-async-request-fetched-article group article (current-buffer))
4523             (gnus-async-prefetch-next group article gnus-summary-buffer)
4524             (when (and (numberp article) gnus-keep-backlog)
4525               (gnus-backlog-enter-article group article (current-buffer)))
4526             'article)
4527            ;; Check the cache.
4528            ((and gnus-use-cache
4529                  (numberp article)
4530                  (gnus-cache-request-article article group))
4531             'article)
4532            ;; Get the article and put into the article buffer.
4533            ((or (stringp article)
4534                 (numberp article))
4535             (let ((gnus-override-method gnus-override-method)
4536                   (methods (and (stringp article) 
4537                                 gnus-refer-article-method))
4538                   result
4539                   (buffer-read-only nil))
4540               (if (or (not (listp methods))
4541                       (and (symbolp (car methods))
4542                            (assq (car methods) nnoo-definition-alist)))
4543                   (setq methods (list methods)))
4544               (when (and (null gnus-override-method)
4545                          methods)
4546                 (setq gnus-override-method (pop methods)))
4547               (while (not result)
4548                 (when (eq gnus-override-method 'current)
4549                   (setq gnus-override-method gnus-current-select-method))
4550                 (erase-buffer)
4551                 (gnus-kill-all-overlays)
4552                 (let ((gnus-newsgroup-name group))
4553                   (gnus-check-group-server))
4554                 (when (gnus-request-article article group (current-buffer))
4555                   (when (numberp article)
4556                     (gnus-async-prefetch-next group article 
4557                                               gnus-summary-buffer)
4558                     (when gnus-keep-backlog
4559                       (gnus-backlog-enter-article
4560                        group article (current-buffer))))
4561                   (setq result 'article))
4562                 (if (not result)
4563                     (if methods
4564                         (setq gnus-override-method (pop methods))
4565                       (setq result 'done))))
4566               (and (eq result 'article) 'article)))
4567            ;; It was a pseudo.
4568            (t article)))
4569
4570       ;; Associate this article with the current summary buffer.
4571       (setq gnus-article-current-summary gnus-summary-buffer)
4572
4573       ;; Take the article from the original article buffer
4574       ;; and place it in the buffer it's supposed to be in.
4575       (when (and (get-buffer gnus-article-buffer)
4576                  (equal (buffer-name (current-buffer))
4577                         (buffer-name (get-buffer gnus-article-buffer))))
4578         (save-excursion
4579           (if (get-buffer gnus-original-article-buffer)
4580               (set-buffer gnus-original-article-buffer)
4581             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4582             (set-buffer-multibyte nil)
4583             (buffer-disable-undo)
4584             (setq major-mode 'gnus-original-article-mode)
4585             (setq buffer-read-only t))
4586           (let (buffer-read-only)
4587             (erase-buffer)
4588             (insert-buffer-substring gnus-article-buffer))
4589           (setq gnus-original-article (cons group article)))
4590
4591         ;; Decode charsets.
4592         (run-hooks 'gnus-article-decode-hook)
4593         ;; Mark article as decoded or not.
4594         (setq gnus-article-decoded-p gnus-article-decode-hook))
4595
4596       ;; Update sparse articles.
4597       (when (and do-update-line
4598                  (or (numberp article)
4599                      (stringp article)))
4600         (let ((buf (current-buffer)))
4601           (set-buffer gnus-summary-buffer)
4602           (gnus-summary-update-article do-update-line sparse-header)
4603           (gnus-summary-goto-subject do-update-line nil t)
4604           (set-window-point (get-buffer-window (current-buffer) t)
4605                             (point))
4606           (set-buffer buf))))))
4607
4608 ;;;
4609 ;;; Article editing
4610 ;;;
4611
4612 (defcustom gnus-article-edit-mode-hook nil
4613   "Hook run in article edit mode buffers."
4614   :group 'gnus-article-various
4615   :type 'hook)
4616
4617 (defcustom gnus-article-edit-article-setup-function
4618   'gnus-article-mime-edit-article-setup
4619   "Function called to setup an editing article buffer."
4620   :group 'gnus-article-various
4621   :type 'function)
4622
4623 (defvar gnus-article-edit-done-function nil)
4624
4625 (defvar gnus-article-edit-mode-map nil)
4626
4627 ;; Should we be using derived.el for this?
4628 (unless gnus-article-edit-mode-map
4629   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4630   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4631
4632   (gnus-define-keys gnus-article-edit-mode-map
4633     "\C-c\C-c" gnus-article-edit-done
4634     "\C-c\C-k" gnus-article-edit-exit)
4635
4636   (gnus-define-keys (gnus-article-edit-wash-map
4637                      "\C-c\C-w" gnus-article-edit-mode-map)
4638     "f" gnus-article-edit-full-stops))
4639
4640 (defun gnus-article-edit-mode ()
4641   "Major mode for editing articles.
4642 This is an extended text-mode.
4643
4644 \\{gnus-article-edit-mode-map}"
4645   (interactive)
4646   (setq major-mode 'gnus-article-edit-mode)
4647   (setq mode-name "Article Edit")
4648   (use-local-map gnus-article-edit-mode-map)
4649   (make-local-variable 'gnus-article-edit-done-function)
4650   (make-local-variable 'gnus-prev-winconf)
4651   (setq buffer-read-only nil)
4652   (buffer-enable-undo)
4653   (widen)
4654   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4655
4656 (defun gnus-article-edit (&optional force)
4657   "Edit the current article.
4658 This will have permanent effect only in mail groups.
4659 If FORCE is non-nil, allow editing of articles even in read-only
4660 groups."
4661   (interactive "P")
4662   (when (and (not force)
4663              (gnus-group-read-only-p))
4664     (error "The current newsgroup does not support article editing"))
4665   (gnus-article-date-original)
4666   (gnus-article-edit-article
4667    'ignore
4668    `(lambda (no-highlight)
4669       'ignore
4670       (gnus-summary-edit-article-done
4671        ,(or (mail-header-references gnus-current-headers) "")
4672        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4673
4674 (defun gnus-article-edit-article (start-func exit-func)
4675   "Start editing the contents of the current article buffer."
4676   (let ((winconf (current-window-configuration)))
4677     (set-buffer gnus-article-buffer)
4678     (gnus-article-edit-mode)
4679     (funcall start-func)
4680     (gnus-configure-windows 'edit-article)
4681     (setq gnus-article-edit-done-function exit-func)
4682     (setq gnus-prev-winconf winconf)
4683     (when gnus-article-edit-article-setup-function
4684       (funcall gnus-article-edit-article-setup-function))
4685     (gnus-message 6 "C-c C-c to end edits")))
4686
4687 (defun gnus-article-edit-done (&optional arg)
4688   "Update the article edits and exit."
4689   (interactive "P")
4690   (let ((func gnus-article-edit-done-function)
4691         (buf (current-buffer))
4692         (start (window-start)))
4693     (remove-hook 'gnus-article-mode-hook
4694                  'gnus-article-mime-edit-article-unwind)
4695     (gnus-article-edit-exit)
4696     (save-excursion
4697       (set-buffer buf)
4698       (let ((buffer-read-only nil))
4699         (funcall func arg))
4700       ;; The cache and backlog have to be flushed somewhat.
4701       (when gnus-keep-backlog
4702         (gnus-backlog-remove-article
4703          (car gnus-article-current) (cdr gnus-article-current)))
4704       ;; Flush original article as well.
4705       (save-excursion
4706         (when (get-buffer gnus-original-article-buffer)
4707           (set-buffer gnus-original-article-buffer)
4708           (setq gnus-original-article nil)))
4709       (when gnus-use-cache
4710         (gnus-cache-update-article
4711          (car gnus-article-current) (cdr gnus-article-current))))
4712     (set-buffer buf)
4713     (set-window-start (get-buffer-window buf) start)
4714     (set-window-point (get-buffer-window buf) (point))))
4715
4716 (defun gnus-article-edit-exit ()
4717   "Exit the article editing without updating."
4718   (interactive)
4719   ;; We remove all text props from the article buffer.
4720   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4721         (curbuf (current-buffer))
4722         (p (point))
4723         (window-start (window-start)))
4724     (erase-buffer)
4725     (insert buf)
4726     (let ((winconf gnus-prev-winconf))
4727       (gnus-article-mode)
4728       (set-window-configuration winconf)
4729       ;; Tippy-toe some to make sure that point remains where it was.
4730       (save-current-buffer
4731         (set-buffer curbuf)
4732         (set-window-start (get-buffer-window (current-buffer)) window-start)
4733         (goto-char p)))))
4734
4735 (defun gnus-article-edit-full-stops ()
4736   "Interactively repair spacing at end of sentences."
4737   (interactive)
4738   (save-excursion
4739     (goto-char (point-min))
4740     (search-forward-regexp "^$" nil t)
4741     (let ((case-fold-search nil))
4742       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4743
4744 ;;;
4745 ;;; Article editing with MIME-Edit
4746 ;;;
4747
4748 (defcustom gnus-article-mime-edit-article-setup-hook nil
4749   "Hook run after setting up a MIME editing article buffer."
4750   :group 'gnus-article-various
4751   :type 'hook)
4752
4753 (defun gnus-article-mime-edit-article-unwind ()
4754   "Unwind `gnus-article-buffer' if article editing was given up."
4755   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4756   (when mime-edit-mode-flag
4757     (mime-edit-exit 'nomime 'no-error)
4758     (message ""))
4759   (when (featurep 'font-lock)
4760     (setq font-lock-defaults nil)
4761     (font-lock-mode 0)))
4762
4763 (defun gnus-article-mime-edit-article-setup ()
4764   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
4765 after replacing with the original article."
4766   (setq gnus-show-mime t)
4767   (setq gnus-article-edit-done-function
4768         `(lambda (&rest args)
4769            (when mime-edit-mode-flag
4770              (mime-edit-exit)
4771              (message ""))
4772            (goto-char (point-min))
4773            (let (case-fold-search)
4774              (when (re-search-forward
4775                     (format "^%s$" (regexp-quote mail-header-separator))
4776                     nil t)
4777                (replace-match "")))
4778            (when (featurep 'font-lock)
4779              (setq font-lock-defaults nil)
4780              (font-lock-mode 0))
4781            (apply ,gnus-article-edit-done-function args)
4782            (set-buffer gnus-original-article-buffer)
4783            (erase-buffer)
4784            (insert-buffer gnus-article-buffer)
4785            (setq gnus-current-headers (gnus-article-make-full-mail-header))
4786            (gnus-article-prepare-display)))
4787   (substitute-key-definition
4788    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
4789    gnus-article-edit-mode-map)
4790   (erase-buffer)
4791   (insert-buffer gnus-original-article-buffer)
4792   (mime-edit-again)
4793   (when (featurep 'font-lock)
4794     (set (make-local-variable 'font-lock-defaults)
4795          '(message-font-lock-keywords t))
4796     (font-lock-set-defaults)
4797     (turn-on-font-lock))
4798   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4799   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
4800
4801 (defun gnus-article-mime-edit-exit ()
4802   "Exit the article MIME editing without updating."
4803   (interactive)
4804   (let ((winconf gnus-prev-winconf)
4805         buf)
4806     (when mime-edit-mode-flag
4807       (mime-edit-exit)
4808       (message ""))
4809     (goto-char (point-min))
4810     (let (case-fold-search)
4811       (when (re-search-forward
4812              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
4813         (replace-match "")))
4814     (when (featurep 'font-lock)
4815       (setq font-lock-defaults nil)
4816       (font-lock-mode 0))
4817     ;; We remove all text props from the article buffer.
4818     (setq buf (format "%s" (buffer-string)))
4819     (set-buffer (get-buffer-create gnus-original-article-buffer))
4820     (erase-buffer)
4821     (insert buf)
4822     (setq gnus-current-headers (gnus-article-make-full-mail-header))
4823     (gnus-article-prepare-display)
4824     (set-window-configuration winconf)))
4825
4826 ;;;
4827 ;;; Article highlights
4828 ;;;
4829
4830 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4831
4832 ;;; Internal Variables:
4833
4834 (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\\)\\)"
4835   "Regular expression that matches URLs."
4836   :group 'gnus-article-buttons
4837   :type 'regexp)
4838
4839 (defcustom gnus-button-alist
4840   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4841      0 t gnus-button-message-id 2)
4842     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4843     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4844      1 t
4845      gnus-button-fetch-group 4)
4846     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4847     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4848      t gnus-button-message-id 3)
4849     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4850     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4851     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4852     ;; This is how URLs _should_ be embedded in text...
4853     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4854     ;; Raw URLs.
4855     (,gnus-button-url-regexp 0 t browse-url 0))
4856   "*Alist of regexps matching buttons in article bodies.
4857
4858 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4859 REGEXP: is the string matching text around the button,
4860 BUTTON: is the number of the regexp grouping actually matching the button,
4861 FORM: is a lisp expression which must eval to true for the button to
4862 be added,
4863 CALLBACK: is the function to call when the user push this button, and each
4864 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4865
4866 CALLBACK can also be a variable, in that case the value of that
4867 variable it the real callback function."
4868   :group 'gnus-article-buttons
4869   :type '(repeat (list regexp
4870                        (integer :tag "Button")
4871                        (sexp :tag "Form")
4872                        (function :tag "Callback")
4873                        (repeat :tag "Par"
4874                                :inline t
4875                                (integer :tag "Regexp group")))))
4876
4877 (defcustom gnus-header-button-alist
4878   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4879      0 t gnus-button-message-id 0)
4880     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4881     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4882      0 t gnus-button-mailto 0)
4883     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4884     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4885     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4886     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4887      gnus-button-message-id 3))
4888   "*Alist of headers and regexps to match buttons in article heads.
4889
4890 This alist is very similar to `gnus-button-alist', except that each
4891 alist has an additional HEADER element first in each entry:
4892
4893 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4894
4895 HEADER is a regexp to match a header.  For a fuller explanation, see
4896 `gnus-button-alist'."
4897   :group 'gnus-article-buttons
4898   :group 'gnus-article-headers
4899   :type '(repeat (list (regexp :tag "Header")
4900                        regexp
4901                        (integer :tag "Button")
4902                        (sexp :tag "Form")
4903                        (function :tag "Callback")
4904                        (repeat :tag "Par"
4905                                :inline t
4906                                (integer :tag "Regexp group")))))
4907
4908 (defvar gnus-button-regexp nil)
4909 (defvar gnus-button-marker-list nil)
4910 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4911
4912 (defvar gnus-button-last nil)
4913 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4914
4915 ;;; Commands:
4916
4917 (defun gnus-article-push-button (event)
4918   "Check text under the mouse pointer for a callback function.
4919 If the text under the mouse pointer has a `gnus-callback' property,
4920 call it with the value of the `gnus-data' text property."
4921   (interactive "e")
4922   (set-buffer (window-buffer (posn-window (event-start event))))
4923   (let* ((pos (posn-point (event-start event)))
4924          (data (get-text-property pos 'gnus-data))
4925          (fun (get-text-property pos 'gnus-callback)))
4926     (goto-char pos)
4927     (when fun
4928       (funcall fun data))))
4929
4930 (defun gnus-article-press-button ()
4931   "Check text at point for a callback function.
4932 If the text at point has a `gnus-callback' property,
4933 call it with the value of the `gnus-data' text property."
4934   (interactive)
4935   (let* ((data (get-text-property (point) 'gnus-data))
4936          (fun (get-text-property (point) 'gnus-callback)))
4937     (when fun
4938       (funcall fun data))))
4939
4940 (defun gnus-article-prev-button (n)
4941   "Move point to N buttons backward.
4942 If N is negative, move forward instead."
4943   (interactive "p")
4944   (gnus-article-next-button (- n)))
4945
4946 (defun gnus-article-next-button (n)
4947   "Move point to N buttons forward.
4948 If N is negative, move backward instead."
4949   (interactive "p")
4950   (let ((function (if (< n 0) 'previous-single-property-change
4951                     'next-single-property-change))
4952         (inhibit-point-motion-hooks t)
4953         (backward (< n 0))
4954         (limit (if (< n 0) (point-min) (point-max))))
4955     (setq n (abs n))
4956     (while (and (not (= limit (point)))
4957                 (> n 0))
4958       ;; Skip past the current button.
4959       (when (get-text-property (point) 'gnus-callback)
4960         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4961       ;; Go to the next (or previous) button.
4962       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
4963       ;; Put point at the start of the button.
4964       (when (and backward (not (get-text-property (point) 'gnus-callback)))
4965         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4966       ;; Skip past intangible buttons.
4967       (when (get-text-property (point) 'intangible)
4968         (incf n))
4969       (decf n))
4970     (unless (zerop n)
4971       (gnus-message 5 "No more buttons"))
4972     n))
4973
4974 (defun gnus-article-highlight (&optional force)
4975   "Highlight current article.
4976 This function calls `gnus-article-highlight-headers',
4977 `gnus-article-highlight-citation',
4978 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4979 do the highlighting.  See the documentation for those functions."
4980   (interactive (list 'force))
4981   (gnus-article-highlight-headers)
4982   (gnus-article-highlight-citation force)
4983   (gnus-article-highlight-signature)
4984   (gnus-article-add-buttons force)
4985   (gnus-article-add-buttons-to-head))
4986
4987 (defun gnus-article-highlight-some (&optional force)
4988   "Highlight current article.
4989 This function calls `gnus-article-highlight-headers',
4990 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4991 do the highlighting.  See the documentation for those functions."
4992   (interactive (list 'force))
4993   (gnus-article-highlight-headers)
4994   (gnus-article-highlight-signature)
4995   (gnus-article-add-buttons))
4996
4997 (defun gnus-article-highlight-headers ()
4998   "Highlight article headers as specified by `gnus-header-face-alist'."
4999   (interactive)
5000   (save-excursion
5001     (set-buffer gnus-article-buffer)
5002     (save-restriction
5003       (let ((alist gnus-header-face-alist)
5004             (buffer-read-only nil)
5005             (case-fold-search t)
5006             (inhibit-point-motion-hooks t)
5007             entry regexp header-face field-face from hpoints fpoints)
5008         (article-narrow-to-head)
5009         (while (setq entry (pop alist))
5010           (goto-char (point-min))
5011           (setq regexp (concat "^\\("
5012                                (if (string-equal "" (nth 0 entry))
5013                                    "[^\t ]"
5014                                  (nth 0 entry))
5015                                "\\)")
5016                 header-face (nth 1 entry)
5017                 field-face (nth 2 entry))
5018           (while (and (re-search-forward regexp nil t)
5019                       (not (eobp)))
5020             (beginning-of-line)
5021             (setq from (point))
5022             (unless (search-forward ":" nil t)
5023               (forward-char 1))
5024             (when (and header-face
5025                        (not (memq (point) hpoints)))
5026               (push (point) hpoints)
5027               (gnus-put-text-property from (point) 'face header-face))
5028             (when (and field-face
5029                        (not (memq (setq from (point)) fpoints)))
5030               (push from fpoints)
5031               (if (re-search-forward "^[^ \t]" nil t)
5032                   (forward-char -2)
5033                 (goto-char (point-max)))
5034               (gnus-put-text-property from (point) 'face field-face))))))))
5035
5036 (defun gnus-article-highlight-signature ()
5037   "Highlight the signature in an article.
5038 It does this by highlighting everything after
5039 `gnus-signature-separator' using `gnus-signature-face'."
5040   (interactive)
5041   (when gnus-signature-face
5042     (save-excursion
5043       (set-buffer gnus-article-buffer)
5044       (let ((buffer-read-only nil)
5045             (inhibit-point-motion-hooks t))
5046         (save-restriction
5047           (when (gnus-article-narrow-to-signature)
5048             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5049                               'face gnus-signature-face)))))))
5050
5051 (defun gnus-article-buttonize-signature ()
5052   "Add button to the signature."
5053   (interactive)
5054   (save-excursion
5055     (set-buffer gnus-article-buffer)
5056     (let ((buffer-read-only nil)
5057           (inhibit-point-motion-hooks t))
5058       (when (gnus-article-search-signature)
5059         (gnus-article-add-button (match-beginning 0) (match-end 0)
5060                                  'gnus-signature-toggle
5061                                  (set-marker (make-marker)
5062                                              (1+ (match-end 0))))))))
5063
5064 (defun gnus-button-in-region-p (b e prop)
5065   "Say whether PROP exists in the region."
5066   (text-property-not-all b e prop nil))
5067
5068 (defun gnus-article-add-buttons (&optional force)
5069   "Find external references in the article and make buttons of them.
5070 \"External references\" are things like Message-IDs and URLs, as
5071 specified by `gnus-button-alist'."
5072   (interactive (list 'force))
5073   (save-excursion
5074     (set-buffer gnus-article-buffer)
5075     (let ((buffer-read-only nil)
5076           (inhibit-point-motion-hooks t)
5077           (case-fold-search t)
5078           (alist gnus-button-alist)
5079           beg entry regexp)
5080       ;; Remove all old markers.
5081       (let (marker entry new-list)
5082         (while (setq marker (pop gnus-button-marker-list))
5083           (if (or (< marker (point-min)) (>= marker (point-max)))
5084               (push marker new-list)
5085             (goto-char marker)
5086             (when (setq entry (gnus-button-entry))
5087               (put-text-property (match-beginning (nth 1 entry))
5088                                  (match-end (nth 1 entry))
5089                                  'gnus-callback nil))
5090             (set-marker marker nil)))
5091         (setq gnus-button-marker-list new-list))
5092       ;; We skip the headers.
5093       (article-goto-body)
5094       (setq beg (point))
5095       (while (setq entry (pop alist))
5096         (setq regexp (car entry))
5097         (goto-char beg)
5098         (while (re-search-forward regexp nil t)
5099           (let* ((start (and entry (match-beginning (nth 1 entry))))
5100                  (end (and entry (match-end (nth 1 entry))))
5101                  (from (match-beginning 0)))
5102             (when (and (or (eq t (nth 2 entry))
5103                            (eval (nth 2 entry)))
5104                        (not (gnus-button-in-region-p
5105                              start end 'gnus-callback)))
5106               ;; That optional form returned non-nil, so we add the
5107               ;; button.
5108               (gnus-article-add-button
5109                start end 'gnus-button-push
5110                (car (push (set-marker (make-marker) from)
5111                           gnus-button-marker-list))))))))))
5112
5113 ;; Add buttons to the head of an article.
5114 (defun gnus-article-add-buttons-to-head ()
5115   "Add buttons to the head of the article."
5116   (interactive)
5117   (save-excursion
5118     (set-buffer gnus-article-buffer)
5119     (save-restriction
5120       (let ((buffer-read-only nil)
5121             (inhibit-point-motion-hooks t)
5122             (case-fold-search t)
5123             (alist gnus-header-button-alist)
5124             entry beg end)
5125         (article-narrow-to-head)
5126         (while alist
5127           ;; Each alist entry.
5128           (setq entry (car alist)
5129                 alist (cdr alist))
5130           (goto-char (point-min))
5131           (while (re-search-forward (car entry) nil t)
5132             ;; Each header matching the entry.
5133             (setq beg (match-beginning 0))
5134             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5135                                (match-beginning 0))
5136                           (point-max)))
5137             (goto-char beg)
5138             (while (re-search-forward (nth 1 entry) end t)
5139               ;; Each match within a header.
5140               (let* ((entry (cdr entry))
5141                      (start (match-beginning (nth 1 entry)))
5142                      (end (match-end (nth 1 entry)))
5143                      (form (nth 2 entry)))
5144                 (goto-char (match-end 0))
5145                 (when (eval form)
5146                   (gnus-article-add-button
5147                    start end (nth 3 entry)
5148                    (buffer-substring (match-beginning (nth 4 entry))
5149                                      (match-end (nth 4 entry)))))))
5150             (goto-char end)))))))
5151
5152 ;;; External functions:
5153
5154 (defun gnus-article-add-button (from to fun &optional data)
5155   "Create a button between FROM and TO with callback FUN and data DATA."
5156   (when gnus-article-button-face
5157     (gnus-overlay-put (gnus-make-overlay from to)
5158                       'face gnus-article-button-face))
5159   (gnus-add-text-properties
5160    from to
5161    (nconc (and gnus-article-mouse-face
5162                (list gnus-mouse-face-prop gnus-article-mouse-face))
5163           (list 'gnus-callback fun)
5164           (and data (list 'gnus-data data))))
5165   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5166                          ;; Quote `:button-keymap' for Mule 2.3
5167                          ;; but it won't work.
5168                          ':button-keymap gnus-widget-button-keymap))
5169
5170 ;;; Internal functions:
5171
5172 (defun gnus-article-set-globals ()
5173   (save-excursion
5174     (set-buffer gnus-summary-buffer)
5175     (gnus-set-global-variables)))
5176
5177 (defun gnus-signature-toggle (end)
5178   (save-excursion
5179     (set-buffer gnus-article-buffer)
5180     (let ((buffer-read-only nil)
5181           (inhibit-point-motion-hooks t)
5182           (limit (next-single-property-change end 'mime-view-entity
5183                                               nil (point-max))))
5184       (if (get-text-property end 'invisible)
5185           (gnus-article-unhide-text end limit)
5186         (gnus-article-hide-text end limit gnus-hidden-properties)))))
5187
5188 (defun gnus-button-entry ()
5189   ;; Return the first entry in `gnus-button-alist' matching this place.
5190   (let ((alist gnus-button-alist)
5191         (entry nil))
5192     (while alist
5193       (setq entry (pop alist))
5194       (if (looking-at (car entry))
5195           (setq alist nil)
5196         (setq entry nil)))
5197     entry))
5198
5199 (defun gnus-button-push (marker)
5200   ;; Push button starting at MARKER.
5201   (save-excursion
5202     (goto-char marker)
5203     (let* ((entry (gnus-button-entry))
5204            (inhibit-point-motion-hooks t)
5205            (fun (nth 3 entry))
5206            (args (mapcar (lambda (group)
5207                            (let ((string (match-string group)))
5208                              (gnus-set-text-properties
5209                               0 (length string) nil string)
5210                              string))
5211                          (nthcdr 4 entry))))
5212       (cond
5213        ((fboundp fun)
5214         (apply fun args))
5215        ((and (boundp fun)
5216              (fboundp (symbol-value fun)))
5217         (apply (symbol-value fun) args))
5218        (t
5219         (gnus-message 1 "You must define `%S' to use this button"
5220                       (cons fun args)))))))
5221
5222 (defun gnus-button-message-id (message-id)
5223   "Fetch MESSAGE-ID."
5224   (save-excursion
5225     (set-buffer gnus-summary-buffer)
5226     (gnus-summary-refer-article message-id)))
5227
5228 (defun gnus-button-fetch-group (address)
5229   "Fetch GROUP specified by ADDRESS."
5230   (if (not (string-match "[:/]" address))
5231       ;; This is just a simple group url.
5232       (gnus-group-read-ephemeral-group address gnus-select-method)
5233     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5234                            address))
5235         (error "Can't parse %s" address)
5236       (gnus-group-read-ephemeral-group
5237        (match-string 4 address)
5238        `(nntp ,(match-string 1 address)
5239               (nntp-address ,(match-string 1 address))
5240               (nntp-port-number ,(if (match-end 3)
5241                                      (match-string 3 address)
5242                                    "nntp")))))))
5243
5244 (defun gnus-url-parse-query-string (query &optional downcase)
5245   (let (retval pairs cur key val)
5246     (setq pairs (split-string query "&"))
5247     (while pairs
5248       (setq cur (car pairs)
5249             pairs (cdr pairs))
5250       (if (not (string-match "=" cur))
5251           nil                           ; Grace
5252         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5253               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5254         (if downcase
5255             (setq key (downcase key)))
5256         (setq cur (assoc key retval))
5257         (if cur
5258             (setcdr cur (cons val (cdr cur)))
5259           (setq retval (cons (list key val) retval)))))
5260     retval))
5261
5262 (defun gnus-url-unhex (x)
5263   (if (> x ?9)
5264       (if (>= x ?a)
5265           (+ 10 (- x ?a))
5266         (+ 10 (- x ?A)))
5267     (- x ?0)))
5268
5269 (defun gnus-url-unhex-string (str &optional allow-newlines)
5270   "Remove %XXX embedded spaces, etc in a url.
5271 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5272 decoding of carriage returns and line feeds in the string, which is normally
5273 forbidden in URL encoding."
5274   (setq str (or str ""))
5275   (let ((tmp "")
5276         (case-fold-search t))
5277     (while (string-match "%[0-9a-f][0-9a-f]" str)
5278       (let* ((start (match-beginning 0))
5279              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5280              (code (+ (* 16 ch1)
5281                       (gnus-url-unhex (elt str (+ start 2))))))
5282         (setq tmp (concat
5283                    tmp (substring str 0 start)
5284                    (cond
5285                     (allow-newlines
5286                      (char-to-string code))
5287                     ((or (= code ?\n) (= code ?\r))
5288                      " ")
5289                     (t (char-to-string code))))
5290               str (substring str (match-end 0)))))
5291     (setq tmp (concat tmp str))
5292     tmp))
5293
5294 (defun gnus-url-mailto (url)
5295   ;; Send mail to someone
5296   (when (string-match "mailto:/*\\(.*\\)" url)
5297     (setq url (substring url (match-beginning 1) nil)))
5298   (let (to args subject func)
5299     (if (string-match (regexp-quote "?") url)
5300         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5301               args (gnus-url-parse-query-string
5302                     (substring url (match-end 0) nil) t))
5303       (setq to (gnus-url-unhex-string url)))
5304     (setq args (cons (list "to" to) args)
5305           subject (cdr-safe (assoc "subject" args)))
5306     (gnus-setup-message 'reply
5307       (message-mail)
5308       (while args
5309         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5310         (if (fboundp func)
5311             (funcall func)
5312           (message-position-on-field (caar args)))
5313         (insert (mapconcat 'identity (cdar args) ", "))
5314         (setq args (cdr args)))
5315       (if subject
5316           (message-goto-body)
5317         (message-goto-subject)))))
5318
5319 (defun gnus-button-mailto (address)
5320   "Mail to ADDRESS."
5321   (set-buffer (gnus-copy-article-buffer))
5322   (message-reply address))
5323
5324 (defalias 'gnus-button-reply 'message-reply)
5325
5326 (defun gnus-button-embedded-url (address)
5327   "Activate ADDRESS with `browse-url'."
5328   (browse-url (gnus-strip-whitespace address)))
5329
5330 (defun gnus-article-smiley-display ()
5331   "Display \"smileys\" as small graphical icons."
5332   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5333
5334 ;;; Next/prev buttons in the article buffer.
5335
5336 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5337 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5338
5339 (defvar gnus-prev-page-map nil)
5340 (unless gnus-prev-page-map
5341   (setq gnus-prev-page-map (make-sparse-keymap))
5342   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5343   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5344
5345 (static-if (featurep 'xemacs)
5346     (defun gnus-insert-prev-page-button ()
5347       (let ((buffer-read-only nil))
5348         (gnus-eval-format
5349          gnus-prev-page-line-format nil
5350          `(gnus-prev t local-map ,gnus-prev-page-map
5351                      gnus-callback gnus-article-button-prev-page
5352                      article-type annotation))))
5353   (defun gnus-insert-prev-page-button ()
5354     (let ((buffer-read-only nil)
5355           (situation (get-text-property (point-min) 'mime-view-situation)))
5356       (set-keymap-parent gnus-prev-page-map (current-local-map))
5357       (gnus-eval-format
5358        gnus-prev-page-line-format nil
5359        `(gnus-prev t local-map ,gnus-prev-page-map
5360                    gnus-callback gnus-article-button-prev-page
5361                    article-type annotation
5362                    mime-view-situation ,situation))))
5363   )
5364
5365 (defvar gnus-next-page-map nil)
5366 (unless gnus-next-page-map
5367   (setq gnus-next-page-map (make-sparse-keymap))
5368   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5369   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5370
5371 (defun gnus-button-next-page ()
5372   "Go to the next page."
5373   (interactive)
5374   (let ((win (selected-window)))
5375     (select-window (get-buffer-window gnus-article-buffer t))
5376     (gnus-article-next-page)
5377     (select-window win)))
5378
5379 (defun gnus-button-prev-page ()
5380   "Go to the prev page."
5381   (interactive)
5382   (let ((win (selected-window)))
5383     (select-window (get-buffer-window gnus-article-buffer t))
5384     (gnus-article-prev-page)
5385     (select-window win)))
5386
5387 (static-if (featurep 'xemacs)
5388     (defun gnus-insert-next-page-button ()
5389       (let ((buffer-read-only nil))
5390         (gnus-eval-format gnus-next-page-line-format nil
5391                           `(gnus-next
5392                             t local-map ,gnus-next-page-map
5393                             gnus-callback gnus-article-button-next-page
5394                             article-type annotation))))
5395   (defun gnus-insert-next-page-button ()
5396     (let ((buffer-read-only nil)
5397           (situation (get-text-property (point-min) 'mime-view-situation)))
5398       (set-keymap-parent gnus-next-page-map (current-local-map))
5399       (gnus-eval-format gnus-next-page-line-format nil
5400                         `(gnus-next
5401                           t local-map ,gnus-next-page-map
5402                           gnus-callback gnus-article-button-next-page
5403                           article-type annotation
5404                           mime-view-situation ,situation))))
5405   )
5406
5407 (defun gnus-article-button-next-page (arg)
5408   "Go to the next page."
5409   (interactive "P")
5410   (let ((win (selected-window)))
5411     (select-window (get-buffer-window gnus-article-buffer t))
5412     (gnus-article-next-page)
5413     (select-window win)))
5414
5415 (defun gnus-article-button-prev-page (arg)
5416   "Go to the prev page."
5417   (interactive "P")
5418   (let ((win (selected-window)))
5419     (select-window (get-buffer-window gnus-article-buffer t))
5420     (gnus-article-prev-page)
5421     (select-window win)))
5422
5423 (defvar gnus-decode-header-methods
5424   '(mail-decode-encoded-word-region)
5425   "List of methods used to decode headers.
5426
5427 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5428 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5429 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5430 whose names match REGEXP.
5431
5432 For example:
5433 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5434  mail-decode-encoded-word-region
5435  (\"chinese\" . rfc1843-decode-region))
5436 ")
5437
5438 (defvar gnus-decode-header-methods-cache nil)
5439
5440 (defun gnus-multi-decode-header (start end)
5441   "Apply the functions from `gnus-encoded-word-methods' that match."
5442   (unless (and gnus-decode-header-methods-cache
5443                (eq gnus-newsgroup-name
5444                    (car gnus-decode-header-methods-cache)))
5445     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5446     (mapcar (lambda (x)
5447               (if (symbolp x)
5448                   (nconc gnus-decode-header-methods-cache (list x))
5449                 (if (and gnus-newsgroup-name
5450                          (string-match (car x) gnus-newsgroup-name))
5451                     (nconc gnus-decode-header-methods-cache
5452                            (list (cdr x))))))
5453           gnus-decode-header-methods))
5454   (let ((xlist gnus-decode-header-methods-cache))
5455     (pop xlist)
5456     (save-restriction
5457       (narrow-to-region start end)
5458       (while xlist
5459         (funcall (pop xlist) (point-min) (point-max))))))
5460
5461 ;;;
5462 ;;; Treatment top-level handling.
5463 ;;;
5464
5465 (defun gnus-treat-article (condition &optional part-number total-parts type)
5466   (let ((length (- (point-max) (point-min)))
5467         (alist gnus-treatment-function-alist)
5468         (article-goto-body-goes-to-point-min-p t)
5469         (treated-type
5470          (or (not type)
5471              (catch 'found
5472                (let ((list gnus-article-treat-types))
5473                  (while list
5474                    (when (string-match (pop list) type)
5475                      (throw 'found t)))))))
5476         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5477         (entity (static-unless (featurep 'xemacs)
5478                   (when (eq 'head condition)
5479                     (get-text-property (point-min) 'mime-view-entity))))
5480         val elem buttonized)
5481     (gnus-run-hooks 'gnus-part-display-hook)
5482     (unless gnus-inhibit-treatment
5483       (while (setq elem (pop alist))
5484         (setq val
5485               (save-excursion
5486                 (if (gnus-buffer-live-p gnus-summary-buffer)
5487                     (set-buffer gnus-summary-buffer))
5488                 (symbol-value (car elem))))
5489         (when (and (or (consp val)
5490                        treated-type)
5491                    (gnus-treat-predicate val)
5492                    (or (not (get (car elem) 'highlight))
5493                        highlightp))
5494           (when (and (not buttonized)
5495                      (memq (car elem)
5496                            '(gnus-treat-hide-signature
5497                              gnus-treat-highlight-signature)))
5498             (gnus-article-buttonize-signature)
5499             (setq buttonized t))
5500           (save-restriction
5501             (funcall (cadr elem)))))
5502       ;; FSF Emacsen does not inherit the existing text properties
5503       ;; in the new text, so we should do it for `mime-view-entity'.
5504       (static-unless (featurep 'xemacs)
5505         (when entity
5506           (put-text-property (point-min) (point-max)
5507                              'mime-view-entity entity))))))
5508
5509 ;; Dynamic variables.
5510 (eval-when-compile
5511   (defvar part-number)
5512   (defvar total-parts)
5513   (defvar type)
5514   (defvar condition)
5515   (defvar length))
5516
5517 (defun gnus-treat-predicate (val)
5518   (cond
5519    ((null val)
5520     nil)
5521    ((and (listp val)
5522          (stringp (car val)))
5523     (apply 'gnus-or (mapcar `(lambda (s)
5524                                (string-match s ,(or gnus-newsgroup-name "")))
5525                             val)))
5526    ((listp val)
5527     (let ((pred (pop val)))
5528       (cond
5529        ((eq pred 'or)
5530         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5531        ((eq pred 'and)
5532         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5533        ((eq pred 'not)
5534         (not (gnus-treat-predicate (car val))))
5535        ((eq pred 'typep)
5536         (equal (car val) type))
5537        (t
5538         (error "%S is not a valid predicate" pred)))))
5539    ((eq val 'mime)
5540     gnus-show-mime)
5541    (condition
5542     (eq condition val))
5543    ((eq val t)
5544     t)
5545    ((eq val 'head)
5546     nil)
5547    ((eq val 'last)
5548     (eq part-number total-parts))
5549    ((numberp val)
5550     (< length val))
5551    (t
5552     (error "%S is not a valid value" val))))
5553
5554 (defun gnus-article-encrypt-body (protocol &optional n)
5555   "Encrypt the article body."
5556   (interactive 
5557    (list
5558     (or gnus-article-encrypt-protocol
5559         (completing-read "Encrypt protocol: "
5560                          gnus-article-encrypt-protocol-alist 
5561                          nil t))
5562     current-prefix-arg))
5563   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5564     (unless func
5565       (error (format "Can't find the encrypt protocol %s" protocol)))
5566     (if (equal gnus-newsgroup-name "nndraft:drafts")
5567         (error "Can't encrypt the article in group nndraft:drafts."))
5568     (if (equal gnus-newsgroup-name "nndraft:queue")
5569         (error "Don't encrypt the article in group nndraft:queue."))
5570     (gnus-summary-iterate n
5571       (save-excursion
5572         (set-buffer gnus-summary-buffer)
5573         (let ((mail-parse-charset gnus-newsgroup-charset)
5574               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5575               (summary-buffer gnus-summary-buffer)
5576               references point)
5577           (gnus-set-global-variables)
5578           (when (gnus-group-read-only-p)
5579             (error "The current newsgroup does not support article encrypt"))
5580           (gnus-summary-show-article t)
5581           (setq references
5582               (or (mail-header-references gnus-current-headers) ""))
5583           (set-buffer gnus-article-buffer)
5584           (let* ((buffer-read-only nil)
5585                  (headers
5586                   (mapcar (lambda (field)
5587                             (and (save-restriction 
5588                                    (message-narrow-to-head)
5589                                    (goto-char (point-min))
5590                                    (search-forward field nil t))
5591                                  (prog2
5592                                      (message-narrow-to-field)
5593                                      (buffer-substring (point-min) (point-max))
5594                                    (delete-region (point-min) (point-max))
5595                                    (widen))))
5596                           '("Content-Type:" "Content-Transfer-Encoding:"
5597                             "Content-Disposition:"))))
5598             (message-narrow-to-head)
5599             (message-remove-header "MIME-Version")
5600             (goto-char (point-max))
5601             (setq point (point))
5602             (insert (apply 'concat headers))
5603             (widen)
5604             (narrow-to-region point (point-max))
5605             (let ((message-options message-options))
5606               (message-options-set 'message-sender user-mail-address)
5607               (message-options-set 'message-recipients user-mail-address)
5608               (message-options-set 'message-sign-encrypt 'not)
5609               (funcall func))
5610             (goto-char (point-min))
5611             (insert "MIME-Version: 1.0\n")
5612             (widen)
5613             (gnus-summary-edit-article-done
5614              references nil summary-buffer t))
5615           (when gnus-keep-backlog
5616             (gnus-backlog-remove-article
5617              (car gnus-article-current) (cdr gnus-article-current)))
5618           (save-excursion
5619             (when (get-buffer gnus-original-article-buffer)
5620               (set-buffer gnus-original-article-buffer)
5621               (setq gnus-original-article nil)))
5622           (when gnus-use-cache
5623             (gnus-cache-update-article
5624              (car gnus-article-current) (cdr gnus-article-current))))))))
5625
5626 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]]%)%}\n"
5627   "The following specs can be used:
5628 %t  The security MIME type
5629 %i  Additional info")
5630
5631 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]]%)%}\n"
5632   "The following specs can be used:
5633 %t  The security MIME type
5634 %i  Additional info")
5635
5636 (defvar gnus-mime-security-button-line-format-alist
5637   '((?t gnus-tmp-type ?s)
5638     (?i gnus-tmp-info ?s)))
5639
5640 (defvar gnus-mime-security-button-map
5641   (let ((map (make-sparse-keymap)))
5642     (set-keymap-parent map gnus-article-mode-map)
5643     (define-key map gnus-mouse-2 'gnus-article-push-button)
5644     (define-key map "\r" 'gnus-article-press-button)
5645     map))
5646
5647 (defvar gnus-mime-security-details-buffer nil)
5648
5649 (defun gnus-mime-security-verify-or-decrypt (handle)
5650   (mm-remove-parts (cdr handle))
5651   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
5652         buffer-read-only)
5653     (when region 
5654       (delete-region (car region) (cdr region))
5655       (set-marker (car region) nil)
5656       (set-marker (cdr region) nil)))
5657   (with-current-buffer (mm-handle-multipart-original-buffer handle)
5658     (let* ((mm-verify-option 'known)
5659            (mm-decrypt-option 'known)
5660            (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
5661       (unless (eq nparts (cdr handle))
5662         (mm-destroy-parts (cdr handle))
5663         (setcdr handle nparts))))
5664   (let ((point (point))
5665         buffer-read-only)
5666     (gnus-mime-display-security handle)
5667     (goto-char point)))
5668
5669 (defun gnus-mime-security-show-details (handle)
5670   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
5671     (if details
5672         (progn
5673           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
5674               (with-current-buffer gnus-mime-security-details-buffer
5675                 (erase-buffer)
5676                 t)
5677             (setq gnus-mime-security-details-buffer
5678                   (gnus-get-buffer-create "*MIME Security Details*")))
5679           (with-current-buffer gnus-mime-security-details-buffer
5680             (insert details))
5681           (pop-to-buffer gnus-mime-security-details-buffer))
5682       (gnus-message 5 "No details."))))
5683
5684 (defun gnus-mime-security-press-button (handle)
5685   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
5686       (gnus-mime-security-show-details handle)
5687     (gnus-mime-security-verify-or-decrypt handle)))
5688
5689 (defun gnus-insert-mime-security-button (handle &optional displayed)
5690   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
5691          (gnus-tmp-type
5692           (concat 
5693            (or (nth 2 (assoc protocol mm-verify-function-alist))
5694                (nth 2 (assoc protocol mm-decrypt-function-alist))
5695                "Unknown")
5696            (if (equal (car handle) "multipart/signed")
5697                " Signed" " Encrypted")
5698            " Part"))
5699          (gnus-tmp-info
5700           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
5701               "Undecided"))
5702          b e)
5703     (unless (bolp)
5704       (insert "\n"))
5705     (setq b (point))
5706     (gnus-eval-format
5707      gnus-mime-security-button-line-format 
5708      gnus-mime-security-button-line-format-alist
5709      `(local-map ,gnus-mime-security-button-map
5710                  keymap ,gnus-mime-security-button-map
5711                  gnus-callback gnus-mime-security-press-button
5712                  article-type annotation
5713                  gnus-data ,handle))
5714     (setq e (point))
5715     (widget-convert-button
5716      'link b e
5717      :mime-handle handle
5718      :action 'gnus-widget-press-button
5719      :button-keymap gnus-mime-security-button-map
5720      :help-echo
5721      (lambda (widget/window &optional overlay pos)
5722        ;; Needed to properly clear the message due to a bug in
5723        ;; wid-edit (XEmacs only).
5724        (if (boundp 'help-echo-owns-message)
5725            (setq help-echo-owns-message t))
5726        (format
5727         "%S: show detail"
5728         (aref gnus-mouse-2 0))))))
5729
5730 (defun gnus-mime-display-security (handle)
5731   (save-restriction
5732     (narrow-to-region (point) (point))
5733     (gnus-insert-mime-security-button handle)
5734     (gnus-mime-display-mixed (cdr handle))
5735     (unless (bolp)
5736       (insert "\n"))
5737     (let ((gnus-mime-security-button-line-format 
5738            gnus-mime-security-button-end-line-format))
5739       (gnus-insert-mime-security-button handle))
5740     (mm-set-handle-multipart-parameter handle 'gnus-region 
5741                                        (cons (set-marker (make-marker)
5742                                                          (point-min))
5743                                              (set-marker (make-marker)
5744                                                          (point-max))))))
5745
5746
5747 ;;; @ for mime-view
5748 ;;;
5749
5750 (defun gnus-article-header-presentation-method (entity situation)
5751   (mime-insert-header entity)
5752   )
5753
5754 (set-alist 'mime-header-presentation-method-alist
5755            'gnus-original-article-mode
5756            #'gnus-article-header-presentation-method)
5757
5758 (defun gnus-mime-preview-quitting-method ()
5759   (mime-preview-kill-buffer)
5760   (delete-other-windows)
5761   (gnus-article-show-summary)
5762   (gnus-summary-select-article gnus-show-all-headers t))
5763
5764 (set-alist 'mime-preview-quitting-method-alist
5765            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
5766
5767 (set-alist 'mime-preview-following-method-alist
5768            'gnus-original-article-mode #'gnus-following-method)
5769
5770 (set-alist 'mime-preview-over-to-previous-method-alist
5771            'gnus-original-article-mode
5772            (lambda ()
5773              (if (> (point-min) 1)
5774                  (gnus-article-prev-page)
5775                (gnus-article-read-summary-keys
5776                 nil (gnus-character-to-event ?P)))))
5777
5778 (set-alist 'mime-preview-over-to-next-method-alist
5779            'gnus-original-article-mode'
5780            (lambda ()
5781              (if (< (point-max) (buffer-size))
5782                  (gnus-article-next-page)
5783                (gnus-article-read-summary-keys
5784                 nil (gnus-character-to-event ?N)))))
5785
5786
5787 ;;; @ end
5788 ;;;
5789
5790 (gnus-ems-redefine)
5791
5792 (provide 'gnus-art)
5793
5794 (run-hooks 'gnus-art-load-hook)
5795
5796 ;;; gnus-art.el ends here