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