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