* gnus-art.el (gnus-request-article-this-buffer): Temporally fix: erase the
[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   "Display 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     (set-buffer-multibyte nil)
3632     (insert-buffer-substring gnus-original-article-buffer)
3633     (set-buffer-multibyte t)))
3634
3635 (defun gnus-article-make-full-mail-header (&optional number charset)
3636   "Create a new mail header structure in a raw article buffer."
3637   (unless (and number charset)
3638     (save-current-buffer
3639       (set-buffer gnus-summary-buffer)
3640       (unless number
3641         (setq number (or (cdr gnus-article-current) 0)))
3642       (unless charset
3643         (setq charset (or default-mime-charset 'x-ctext)))))
3644   (goto-char (point-min))
3645   (let ((header-end (if (search-forward "\n\n" nil t)
3646                         (1- (point))
3647                       (goto-char (point-max))))
3648         (chars (- (point-max) (point)))
3649         (lines (count-lines (point) (point-max)))
3650         (default-mime-charset charset)
3651         xref)
3652     (narrow-to-region (point-min) header-end)
3653     (setq xref (std11-fetch-field "xref"))
3654     (prog1
3655         (make-full-mail-header
3656          number
3657          (std11-fetch-field "subject")
3658          (std11-fetch-field "from")
3659          (std11-fetch-field "date")
3660          (std11-fetch-field "message-id")
3661          (std11-fetch-field "references")
3662          chars
3663          lines
3664          (when xref (concat "Xref: " xref)))
3665       (widen))))
3666
3667 (defun gnus-article-prepare (article &optional all-headers header)
3668   "Prepare ARTICLE in article mode buffer.
3669 ARTICLE should either be an article number or a Message-ID.
3670 If ARTICLE is an id, HEADER should be the article headers.
3671 If ALL-HEADERS is non-nil, no headers are hidden."
3672   (save-excursion
3673     ;; Make sure we start in a summary buffer.
3674     (unless (eq major-mode 'gnus-summary-mode)
3675       (set-buffer gnus-summary-buffer))
3676     (setq gnus-summary-buffer (current-buffer))
3677     (let* ((gnus-article (if header (mail-header-number header) article))
3678            (summary-buffer (current-buffer))
3679            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3680            (group gnus-newsgroup-name)
3681            result)
3682       (save-excursion
3683         (gnus-article-setup-buffer)
3684         (set-buffer gnus-article-buffer)
3685         ;; Deactivate active regions.
3686         (when (and (boundp 'transient-mark-mode)
3687                    transient-mark-mode)
3688           (setq mark-active nil))
3689         (if (not (setq result (let ((buffer-read-only nil))
3690                                 (gnus-request-article-this-buffer
3691                                  article group))))
3692             ;; There is no such article.
3693             (save-excursion
3694               (when (and (numberp article)
3695                          (not (memq article gnus-newsgroup-sparse)))
3696                 (setq gnus-article-current
3697                       (cons gnus-newsgroup-name article))
3698                 (set-buffer gnus-summary-buffer)
3699                 (setq gnus-current-article article)
3700                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3701                     (progn
3702                       (gnus-summary-set-agent-mark article)
3703                       (message "Message marked for downloading"))
3704                   (gnus-summary-mark-article article gnus-canceled-mark)
3705                   (unless (memq article gnus-newsgroup-sparse)
3706                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3707           (if (or (eq result 'pseudo)
3708                   (eq result 'nneething))
3709               (progn
3710                 (save-excursion
3711                   (set-buffer summary-buffer)
3712                   (push article gnus-newsgroup-history)
3713                   (setq gnus-last-article gnus-current-article
3714                         gnus-current-article 0
3715                         gnus-current-headers nil
3716                         gnus-article-current nil)
3717                   (if (eq result 'nneething)
3718                       (gnus-configure-windows 'summary)
3719                     (gnus-configure-windows 'article))
3720                   (gnus-set-global-variables))
3721                 (let ((gnus-article-mime-handle-alist-1
3722                        gnus-article-mime-handle-alist))
3723                   (gnus-set-mode-line 'article)))
3724             ;; The result from the `request' was an actual article -
3725             ;; or at least some text that is now displayed in the
3726             ;; article buffer.
3727             (when (and (numberp article)
3728                        (not (eq article gnus-current-article)))
3729               ;; Seems like a new article has been selected.
3730               ;; `gnus-current-article' must be an article number.
3731               (save-excursion
3732                 (set-buffer summary-buffer)
3733                 (push article gnus-newsgroup-history)
3734                 (setq gnus-last-article gnus-current-article
3735                       gnus-current-article article
3736                       gnus-current-headers
3737                       (gnus-summary-article-header gnus-current-article)
3738                       gnus-article-current
3739                       (cons gnus-newsgroup-name gnus-current-article))
3740                 (unless (vectorp gnus-current-headers)
3741                   (setq gnus-current-headers nil))
3742                 (gnus-summary-goto-subject gnus-current-article)
3743                 (when (gnus-summary-show-thread)
3744                   ;; If the summary buffer really was folded, the
3745                   ;; previous goto may not actually have gone to
3746                   ;; the right article, but the thread root instead.
3747                   ;; So we go again.
3748                   (gnus-summary-goto-subject gnus-current-article))
3749                 (gnus-run-hooks 'gnus-mark-article-hook)
3750                 (gnus-set-mode-line 'summary)
3751                 (when (gnus-visual-p 'article-highlight 'highlight)
3752                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3753                 ;; Set the global newsgroup variables here.
3754                 (gnus-set-global-variables)
3755                 (setq gnus-have-all-headers
3756                       (or all-headers gnus-show-all-headers))))
3757             (save-excursion
3758               (gnus-configure-windows 'article))
3759             (when (or (numberp article)
3760                       (stringp article))
3761               (gnus-article-prepare-display)
3762               ;; Do page break.
3763               (goto-char (point-min))
3764               (setq gnus-page-broken
3765                     (when gnus-break-pages
3766                       (gnus-narrow-to-page)
3767                       t)))
3768             (let ((gnus-article-mime-handle-alist-1
3769                    gnus-article-mime-handle-alist))
3770               (gnus-set-mode-line 'article))
3771             (article-goto-body)
3772             (set-window-point (get-buffer-window (current-buffer)) (point))
3773             (gnus-configure-windows 'article)
3774             t))))))
3775
3776 (defun gnus-article-prepare-mime-display (&optional number)
3777   (goto-char (point-min))
3778   (when (re-search-forward "^[^\t ]+:" nil t)
3779     (goto-char (match-beginning 0)))
3780   (let ((entity (if (eq 1 (point-min))
3781                     (get-text-property 1 'mime-view-entity)
3782                   (get-text-property (point) 'mime-view-entity)))
3783         last-entity child-entity next type)
3784     (setq child-entity (mime-entity-children entity))
3785     (if child-entity
3786         (setq last-entity (nth (1- (length child-entity))
3787                                child-entity))
3788       (setq last-entity entity))
3789     (save-restriction
3790       (narrow-to-region (point)
3791                         (if (search-forward "\n\n" nil t)
3792                             (point)
3793                           (point-max)))
3794       (gnus-treat-article 'head)
3795       (put-text-property (point-min) (point-max) 'article-treated-header t)
3796       (goto-char (point-max)))
3797     (while (and (not (eobp)) entity)
3798       (setq next (set-marker
3799                   (make-marker)
3800                   (next-single-property-change (point) 'mime-view-entity
3801                                                nil (point-max))))
3802       (let ((types (mime-entity-content-type entity)))
3803         (while (eq 'multipart (mime-content-type-primary-type types))
3804           (setq entity (car (mime-entity-children entity))
3805                 types (mime-entity-content-type entity)))
3806         (when types
3807           (setq type (format "%s/%s"
3808                              (mime-content-type-primary-type types)
3809                              (mime-content-type-subtype types)))))
3810       (if (string-equal type "message/rfc822")
3811           (progn
3812             (setq next (point))
3813             (let ((children (mime-entity-children entity))
3814                   last-children)
3815               (when children
3816                 (setq last-children (nth (1- (length children)) children))
3817                 (while
3818                     (and
3819                      (not (eq last-children
3820                               (get-text-property next 'mime-view-entity)))
3821                      (setq next
3822                            (next-single-property-change next
3823                                                         'mime-view-entity
3824                                                         nil (point-max)))))))
3825             (setq next (next-single-property-change next 'mime-view-entity
3826                                                     nil (point-max)))
3827             (save-restriction
3828               (narrow-to-region (point) next)
3829               (gnus-article-prepare-mime-display)
3830               (goto-char (point-max)))
3831             (setq entity (get-text-property (point) 'mime-view-entity)))
3832         (save-restriction
3833           (narrow-to-region (point) next)
3834           ;; Kludge. We have to count true number, but for now,
3835           ;; part number is here only to achieve `last'.
3836           (gnus-treat-article nil 1
3837                               (if (eq entity last-entity)
3838                                   1 2)
3839                               type)
3840           (goto-char (point-max)))
3841         (setq entity (get-text-property next 'mime-view-entity))))))
3842
3843 ;;;###autoload
3844 (defun gnus-article-prepare-display ()
3845   "Make the current buffer look like a nice article."
3846   (let ((gnus-article-buffer (current-buffer))
3847         buffer-read-only)
3848     (unless (eq major-mode 'gnus-article-mode)
3849       (gnus-article-mode))
3850     (setq buffer-read-only nil
3851           gnus-button-marker-list nil
3852           gnus-article-wash-types nil
3853           gnus-article-image-alist nil)
3854     (save-restriction
3855       (widen)
3856       (static-if (featurep 'xemacs)
3857           (map-extents (lambda (extent maparg) (delete-extent extent)))
3858         (let ((lists (overlay-lists)))
3859           (dolist (overlay (nconc (car lists) (cdr lists)))
3860             (delete-overlay overlay)))))
3861     (gnus-run-hooks 'gnus-tmp-internal-hook))
3862   (set-buffer gnus-original-article-buffer)
3863   ;; Display message.
3864   (setq mime-message-structure gnus-current-headers)
3865   (mime-buffer-entity-set-buffer-internal mime-message-structure
3866                                           gnus-original-article-buffer)
3867   (mime-entity-set-representation-type-internal mime-message-structure
3868                                                 'mime-buffer-entity)
3869   (luna-send mime-message-structure 'initialize-instance
3870              mime-message-structure)
3871   (if gnus-show-mime
3872       (let (mime-display-header-hook mime-display-text/plain-hook)
3873         (funcall gnus-article-display-method-for-mime))
3874     (funcall gnus-article-display-method-for-traditional))
3875   ;; Call the treatment functions.
3876   (let ((inhibit-read-only t))
3877     (save-restriction
3878       (widen)
3879       (if gnus-show-mime
3880           (gnus-article-prepare-mime-display)
3881         (narrow-to-region (goto-char (point-min))
3882                           (if (search-forward "\n\n" nil t)
3883                               (point)
3884                             (point-max)))
3885         (gnus-treat-article 'head)
3886         (put-text-property (point-min) (point-max) 'article-treated-header t)
3887         (goto-char (point-max))
3888         (widen)
3889         (narrow-to-region (point) (point-max))
3890         (gnus-treat-article nil))
3891       (put-text-property (point-min) (point-max) 'read-only nil)))
3892   (gnus-run-hooks 'gnus-article-prepare-hook))
3893
3894 (defun gnus-article-decode-article-as-default-mime-charset ()
3895   "Decode an article as `default-mime-charset'.  It won't work if the
3896 value of the variable `gnus-show-mime' is non-nil."
3897   (unless gnus-show-mime
3898     (set (make-local-variable 'default-mime-charset)
3899          (with-current-buffer gnus-summary-buffer
3900            default-mime-charset))
3901     (decode-mime-charset-region (point-min) (point-max)
3902                                 default-mime-charset)))
3903
3904 ;;;
3905 ;;; Gnus MIME viewing functions
3906 ;;;
3907
3908 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3909   "Format of the MIME buttons.
3910
3911 Valid specifiers include:
3912 %t  The MIME type
3913 %T  MIME type, along with additional info
3914 %n  The `name' parameter
3915 %d  The description, if any
3916 %l  The length of the encoded part
3917 %p  The part identifier number
3918 %e  Dots if the part isn't displayed
3919
3920 General format specifiers can also be used.  See
3921 (gnus)Formatting Variables.")
3922
3923 (defvar gnus-mime-button-line-format-alist
3924   '((?t gnus-tmp-type ?s)
3925     (?T gnus-tmp-type-long ?s)
3926     (?n gnus-tmp-name ?s)
3927     (?d gnus-tmp-description ?s)
3928     (?p gnus-tmp-id ?s)
3929     (?l gnus-tmp-length ?d)
3930     (?e gnus-tmp-dots ?s)))
3931
3932 (defvar gnus-mime-button-commands
3933   '((gnus-article-press-button "\r" "Toggle Display")
3934     (gnus-mime-view-part "v" "View Interactively...")
3935     (gnus-mime-view-part-as-type "t" "View As Type...")
3936     (gnus-mime-view-part-as-charset "C" "View As charset...")
3937     (gnus-mime-save-part "o" "Save...")
3938     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3939     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3940     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3941     (gnus-mime-internalize-part "E" "View Internally")
3942     (gnus-mime-externalize-part "e" "View Externally")
3943     (gnus-mime-print-part "p" "Print")
3944     (gnus-mime-pipe-part "|" "Pipe To Command...")
3945     (gnus-mime-action-on-part "." "Take action on the part")))
3946
3947 (defun gnus-article-mime-part-status ()
3948   (with-current-buffer gnus-article-buffer
3949     (let ((entity (get-text-property (point-min) 'mime-view-entity))
3950           children)
3951       (if (and entity
3952                (setq children (mime-entity-children entity))
3953                (setq children (length children)))
3954           (if (eq 1 children)
3955               " (1 part)"
3956             (format " (%d parts)" children))
3957         ""))))
3958
3959 (defvar gnus-mime-button-map
3960   (let ((map (make-sparse-keymap)))
3961     (unless (>= (string-to-number emacs-version) 21)
3962       ;; XEmacs doesn't care.
3963       (set-keymap-parent map gnus-article-mode-map))
3964     (define-key map gnus-mouse-2 'gnus-article-push-button)
3965     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3966     (dolist (c gnus-mime-button-commands)
3967       (define-key map (cadr c) (car c)))
3968     map))
3969
3970 (defun gnus-mime-button-menu (event)
3971   "Construct a context-sensitive menu of MIME commands."
3972   (interactive "e")
3973   (save-window-excursion
3974     (let ((pos (event-start event)))
3975       (select-window (posn-window pos))
3976       (goto-char (posn-point pos))
3977       (gnus-article-check-buffer)
3978       (let ((response (x-popup-menu
3979                        t `("MIME Part"
3980                            ("" ,@(mapcar (lambda (c)
3981                                            (cons (caddr c) (car c)))
3982                                          gnus-mime-button-commands))))))
3983         (if response
3984             (call-interactively response))))))
3985
3986 (defun gnus-mime-view-all-parts (&optional handles)
3987   "View all the MIME parts."
3988   (interactive)
3989   (save-current-buffer
3990     (set-buffer gnus-article-buffer)
3991     (let ((handles (or handles gnus-article-mime-handles))
3992           (mail-parse-charset gnus-newsgroup-charset)
3993           (mail-parse-ignored-charsets
3994            (with-current-buffer gnus-summary-buffer
3995              gnus-newsgroup-ignored-charsets)))
3996       (when handles
3997         (mm-remove-parts handles)
3998         (goto-char (point-min))
3999         (or (search-forward "\n\n") (goto-char (point-max)))
4000         (let (buffer-read-only)
4001           (delete-region (point) (point-max))
4002           (mm-display-parts handles))))))
4003
4004 (defun gnus-mime-save-part-and-strip ()
4005   "Save the MIME part under point then replace it with an external body."
4006   (interactive)
4007   (gnus-article-check-buffer)
4008   (let* ((data (get-text-property (point) 'gnus-data))
4009          file param
4010          (handles gnus-article-mime-handles))
4011     (if (mm-multiple-handles gnus-article-mime-handles)
4012         (error "This function is not implemented"))
4013     (setq file (and data (mm-save-part data)))
4014     (when file
4015       (with-current-buffer (mm-handle-buffer data)
4016         (erase-buffer)
4017         (insert "Content-Type: " (mm-handle-media-type data))
4018         (mml-insert-parameter-string (cdr (mm-handle-type data))
4019                                      '(charset))
4020         (insert "\n")
4021         (insert "Content-ID: " (message-make-message-id) "\n")
4022         (insert "Content-Transfer-Encoding: binary\n")
4023         (insert "\n"))
4024       (setcdr data
4025               (cdr (mm-make-handle nil
4026                                    `("message/external-body"
4027                                      (access-type . "LOCAL-FILE")
4028                                      (name . ,file)))))
4029       (set-buffer gnus-summary-buffer)
4030       (gnus-article-edit-article
4031        `(lambda ()
4032           (erase-buffer)
4033           (let ((mail-parse-charset (or gnus-article-charset
4034                                         ',gnus-newsgroup-charset))
4035                 (mail-parse-ignored-charsets
4036                  (or gnus-article-ignored-charsets
4037                      ',gnus-newsgroup-ignored-charsets))
4038                 (mbl mml-buffer-list))
4039             (setq mml-buffer-list nil)
4040             (insert-buffer gnus-original-article-buffer)
4041             (mime-to-mml ',handles)
4042             (setq gnus-article-mime-handles nil)
4043             (let ((mbl1 mml-buffer-list))
4044               (setq mml-buffer-list mbl)
4045               (set (make-local-variable 'mml-buffer-list) mbl1))
4046             ;; LOCAL argument of add-hook differs between GNU Emacs
4047             ;; and XEmacs. make-local-hook makes sure they are local.
4048             (make-local-hook 'kill-buffer-hook)
4049             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4050        `(lambda (no-highlight)
4051           (let ((mail-parse-charset (or gnus-article-charset
4052                                         ',gnus-newsgroup-charset))
4053                 (message-options message-options)
4054                 (message-options-set-recipient)
4055                 (mail-parse-ignored-charsets
4056                  (or gnus-article-ignored-charsets
4057                      ',gnus-newsgroup-ignored-charsets)))
4058             (mml-to-mime)
4059             (mml-destroy-buffers)
4060             (remove-hook 'kill-buffer-hook
4061                          'mml-destroy-buffers t)
4062             (kill-local-variable 'mml-buffer-list))
4063           (gnus-summary-edit-article-done
4064            ,(or (mail-header-references gnus-current-headers) "")
4065            ,(gnus-group-read-only-p)
4066            ,gnus-summary-buffer no-highlight))))))
4067
4068 (defun gnus-mime-save-part ()
4069   "Save the MIME part under point."
4070   (interactive)
4071   (gnus-article-check-buffer)
4072   (let ((data (get-text-property (point) 'gnus-data)))
4073     (when data
4074       (mm-save-part data))))
4075
4076 (defun gnus-mime-pipe-part ()
4077   "Pipe the MIME part under point to a process."
4078   (interactive)
4079   (gnus-article-check-buffer)
4080   (let ((data (get-text-property (point) 'gnus-data)))
4081     (when data
4082       (mm-pipe-part data))))
4083
4084 (defun gnus-mime-view-part ()
4085   "Interactively choose a viewing method for the MIME part under point."
4086   (interactive)
4087   (gnus-article-check-buffer)
4088   (let ((data (get-text-property (point) 'gnus-data)))
4089     (when data
4090       (setq gnus-article-mime-handles
4091             (mm-merge-handles
4092              gnus-article-mime-handles (setq data (copy-sequence data))))
4093       (mm-interactively-view-part data))))
4094
4095 (defun gnus-mime-view-part-as-type-internal ()
4096   (gnus-article-check-buffer)
4097   (let* ((name (mail-content-type-get
4098                 (mm-handle-type (get-text-property (point) 'gnus-data))
4099                 'name))
4100          (def-type (and name (mm-default-file-encoding name))))
4101     (and def-type (cons def-type 0))))
4102
4103 (defun gnus-mime-view-part-as-type (&optional mime-type)
4104   "Choose a MIME media type, and view the part as such."
4105   (interactive)
4106   (unless mime-type
4107     (setq mime-type (completing-read
4108                      "View as MIME type: "
4109                      (mapcar #'list (mailcap-mime-types))
4110                      nil nil
4111                      (gnus-mime-view-part-as-type-internal))))
4112   (gnus-article-check-buffer)
4113   (let ((handle (get-text-property (point) 'gnus-data)))
4114     (when handle
4115       (setq handle
4116             (mm-make-handle (mm-handle-buffer handle)
4117                             (cons mime-type (cdr (mm-handle-type handle)))
4118                             (mm-handle-encoding handle)
4119                             (mm-handle-undisplayer handle)
4120                             (mm-handle-disposition handle)
4121                             (mm-handle-description handle)
4122                             nil
4123                             (mm-handle-id handle)))
4124       (setq gnus-article-mime-handles
4125             (mm-merge-handles gnus-article-mime-handles handle))
4126       (gnus-mm-display-part handle))))
4127
4128 (defun gnus-mime-copy-part (&optional handle)
4129   "Put the MIME part under point into a new buffer."
4130   (interactive)
4131   (gnus-article-check-buffer)
4132   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4133          (contents (and handle (mm-get-part handle)))
4134          (base (and handle
4135                     (file-name-nondirectory
4136                      (or
4137                       (mail-content-type-get (mm-handle-type handle) 'name)
4138                       (mail-content-type-get (mm-handle-type handle)
4139                                              'filename)
4140                       "*decoded*"))))
4141          (buffer (and base (generate-new-buffer base))))
4142     (when contents
4143       (switch-to-buffer buffer)
4144       (insert contents)
4145       ;; We do it this way to make `normal-mode' set the appropriate mode.
4146       (unwind-protect
4147           (progn
4148             (setq buffer-file-name (expand-file-name base))
4149             (normal-mode))
4150         (setq buffer-file-name nil))
4151       (goto-char (point-min)))))
4152
4153 (defun gnus-mime-print-part (&optional handle)
4154   "Print the MIME part under point."
4155   (interactive)
4156   (gnus-article-check-buffer)
4157   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4158          (contents (and handle (mm-get-part handle)))
4159          (file (make-temp-name (expand-file-name "mm." mm-tmp-directory)))
4160          (printer (mailcap-mime-info (mm-handle-type handle) "print")))
4161     (when contents
4162         (if printer
4163             (unwind-protect
4164                 (progn
4165                   (with-temp-file file
4166                     (insert contents))
4167                   (call-process shell-file-name nil
4168                                 (generate-new-buffer " *mm*")
4169                                 nil
4170                                 shell-command-switch
4171                                 (mm-mailcap-command
4172                                  printer file (mm-handle-type handle))))
4173               (delete-file file))
4174           (with-temp-buffer
4175             (insert contents)
4176             (gnus-print-buffer))))))
4177
4178 (defun gnus-mime-inline-part (&optional handle arg)
4179   "Insert the MIME part under point into the current buffer."
4180   (interactive (list nil current-prefix-arg))
4181   (gnus-article-check-buffer)
4182   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4183          contents charset
4184          (b (point))
4185          buffer-read-only)
4186     (when handle
4187       (if (and (not arg) (mm-handle-undisplayer handle))
4188           (mm-remove-part handle)
4189         (setq contents (mm-get-part handle))
4190         (cond
4191          ((not arg)
4192           (setq charset (or (mail-content-type-get
4193                              (mm-handle-type handle) 'charset)
4194                             gnus-newsgroup-charset)))
4195          ((numberp arg)
4196           (if (mm-handle-undisplayer handle)
4197               (mm-remove-part handle))
4198           (setq charset
4199                 (or (cdr (assq arg
4200                                gnus-summary-show-article-charset-alist))
4201                     (mm-read-coding-system "Charset: ")))))
4202         (forward-line 2)
4203         (mm-insert-inline handle
4204                           (if (and charset
4205                                    (setq charset (mm-charset-to-coding-system
4206                                                   charset))
4207                                    (not (eq charset 'ascii)))
4208                               (mm-decode-coding-string contents charset)
4209                             contents))
4210         (goto-char b)))))
4211
4212 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4213   "Insert the MIME part under point into the current buffer using the
4214 specified charset."
4215   (interactive (list nil current-prefix-arg))
4216   (gnus-article-check-buffer)
4217   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4218          contents charset
4219          (b (point))
4220          buffer-read-only)
4221     (when handle
4222       (if (mm-handle-undisplayer handle)
4223           (mm-remove-part handle))
4224       (let ((gnus-newsgroup-charset
4225              (or (cdr (assq arg
4226                             gnus-summary-show-article-charset-alist))
4227                  (mm-read-coding-system "Charset: ")))
4228             (gnus-newsgroup-ignored-charsets 'gnus-all))
4229         (gnus-article-press-button)))))
4230
4231 (defun gnus-mime-externalize-part (&optional handle)
4232   "View the MIME part under point with an external viewer."
4233   (interactive)
4234   (gnus-article-check-buffer)
4235   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4236          (mm-user-display-methods nil)
4237          (mm-inlined-types nil)
4238          (mail-parse-charset gnus-newsgroup-charset)
4239          (mail-parse-ignored-charsets
4240           (save-excursion (set-buffer gnus-summary-buffer)
4241                           gnus-newsgroup-ignored-charsets)))
4242     (when handle
4243       (if (mm-handle-undisplayer handle)
4244           (mm-remove-part handle)
4245         (mm-display-part handle)))))
4246
4247 (defun gnus-mime-internalize-part (&optional handle)
4248   "View the MIME part under point with an internal viewer.
4249 If no internal viewer is available, use an external viewer."
4250   (interactive)
4251   (gnus-article-check-buffer)
4252   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4253          (mm-inlined-types '(".*"))
4254          (mm-inline-large-images t)
4255          (mail-parse-charset gnus-newsgroup-charset)
4256          (mail-parse-ignored-charsets
4257           (save-excursion (set-buffer gnus-summary-buffer)
4258                           gnus-newsgroup-ignored-charsets)))
4259     (when handle
4260       (if (mm-handle-undisplayer handle)
4261           (mm-remove-part handle)
4262         (mm-display-part handle)))))
4263
4264 (defun gnus-mime-action-on-part (&optional action)
4265   "Do something with the MIME attachment at \(point\)."
4266   (interactive
4267    (list (completing-read "Action: " gnus-mime-action-alist)))
4268   (gnus-article-check-buffer)
4269   (let ((action-pair (assoc action gnus-mime-action-alist)))
4270     (if action-pair
4271         (funcall (cdr action-pair)))))
4272
4273 (defun gnus-article-part-wrapper (n function)
4274   (save-current-buffer
4275     (set-buffer gnus-article-buffer)
4276     (when (> n (length gnus-article-mime-handle-alist))
4277       (error "No such part"))
4278     (gnus-article-goto-part n)
4279     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4280       (funcall function handle))))
4281
4282 (defun gnus-article-pipe-part (n)
4283   "Pipe MIME part N, which is the numerical prefix."
4284   (interactive "p")
4285   (gnus-article-part-wrapper n 'mm-pipe-part))
4286
4287 (defun gnus-article-save-part (n)
4288   "Save MIME part N, which is the numerical prefix."
4289   (interactive "p")
4290   (gnus-article-part-wrapper n 'mm-save-part))
4291
4292 (defun gnus-article-interactively-view-part (n)
4293   "View MIME part N interactively, which is the numerical prefix."
4294   (interactive "p")
4295   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4296
4297 (defun gnus-article-copy-part (n)
4298   "Copy MIME part N, which is the numerical prefix."
4299   (interactive "p")
4300   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4301
4302 (defun gnus-article-view-part-as-charset (n)
4303   "Copy MIME part N, which is the numerical prefix."
4304   (interactive "p")
4305   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4306
4307 (defun gnus-article-externalize-part (n)
4308   "View MIME part N externally, which is the numerical prefix."
4309   (interactive "p")
4310   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
4311
4312 (defun gnus-article-inline-part (n)
4313   "Inline MIME part N, which is the numerical prefix."
4314   (interactive "p")
4315   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4316
4317 (defun gnus-article-mime-match-handle-first (condition)
4318   (if condition
4319       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4320         (while (setq ihandle (pop alist))
4321           (if (and (cond
4322                     ((functionp condition)
4323                      (funcall condition (cdr ihandle)))
4324                     ((eq condition 'undisplayed)
4325                      (not (or (mm-handle-undisplayer (cdr ihandle))
4326                               (equal (mm-handle-media-type (cdr ihandle))
4327                                      "multipart/alternative"))))
4328                     ((eq condition 'undisplayed-alternative)
4329                      (not (mm-handle-undisplayer (cdr ihandle))))
4330                     (t t))
4331                    (gnus-article-goto-part (car ihandle))
4332                    (or (not n) (< (car ihandle) n)))
4333               (setq n (car ihandle))))
4334         (or n 1))
4335     1))
4336
4337 (defun gnus-article-view-part (&optional n)
4338   "View MIME part N, which is the numerical prefix."
4339   (interactive "P")
4340   (save-current-buffer
4341     (set-buffer gnus-article-buffer)
4342     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4343                              gnus-article-mime-match-handle-function)))
4344     (when (> n (length gnus-article-mime-handle-alist))
4345       (error "No such part"))
4346     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4347       (when (gnus-article-goto-part n)
4348         (if (equal (car handle) "multipart/alternative")
4349             (gnus-article-press-button)
4350           (when (eq (gnus-mm-display-part handle) 'internal)
4351             (gnus-set-window-start)))))))
4352
4353 (defsubst gnus-article-mime-total-parts ()
4354   (if (bufferp (car gnus-article-mime-handles))
4355       1 ;; single part
4356     (1- (length gnus-article-mime-handles))))
4357
4358 (defun gnus-mm-display-part (handle)
4359   "Display HANDLE and fix MIME button."
4360   (let ((id (get-text-property (point) 'gnus-part))
4361         (point (point))
4362         buffer-read-only)
4363     (forward-line 1)
4364     (prog1
4365         (let ((window (selected-window))
4366               (mail-parse-charset gnus-newsgroup-charset)
4367               (mail-parse-ignored-charsets
4368                (save-excursion (set-buffer gnus-summary-buffer)
4369                                gnus-newsgroup-ignored-charsets)))
4370           (save-excursion
4371             (unwind-protect
4372                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4373                       (beg (point)))
4374                   (when win
4375                     (select-window win))
4376                   (goto-char point)
4377                   (forward-line)
4378                   (if (mm-handle-displayed-p handle)
4379                       ;; This will remove the part.
4380                       (mm-display-part handle)
4381                     (save-restriction
4382                       (narrow-to-region (point)
4383                                         (if (eobp) (point) (1+ (point))))
4384                       (mm-display-part handle)
4385                       ;; We narrow to the part itself and
4386                       ;; then call the treatment functions.
4387                       (goto-char (point-min))
4388                       (forward-line 1)
4389                       (narrow-to-region (point) (point-max))
4390                       (gnus-treat-article
4391                        nil id
4392                        (gnus-article-mime-total-parts)
4393                        (mm-handle-media-type handle)))))
4394               (if (window-live-p window)
4395                   (select-window window)))))
4396       (goto-char point)
4397       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4398       (gnus-insert-mime-button
4399        handle id (list (mm-handle-displayed-p handle)))
4400       (goto-char point))))
4401
4402 (defun gnus-article-goto-part (n)
4403   "Go to MIME part N."
4404   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4405     (when point
4406       (goto-char point))))
4407
4408 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4409   (let ((gnus-tmp-name
4410          (or (mail-content-type-get (mm-handle-type handle) 'name)
4411              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4412              (mail-content-type-get (mm-handle-type handle) 'url)
4413              ""))
4414         (gnus-tmp-type (mm-handle-media-type handle))
4415         (gnus-tmp-description
4416          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4417                                               "")))
4418         (gnus-tmp-dots
4419          (if (if displayed (car displayed)
4420                (mm-handle-displayed-p handle))
4421              "" "..."))
4422         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4423                            (buffer-size)))
4424         gnus-tmp-type-long b e)
4425     (when (string-match ".*/" gnus-tmp-name)
4426       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4427     (setq gnus-tmp-type-long (concat gnus-tmp-type
4428                                      (and (not (equal gnus-tmp-name ""))
4429                                           (concat "; " gnus-tmp-name))))
4430     (unless (equal gnus-tmp-description "")
4431       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4432     (unless (bolp)
4433       (insert "\n"))
4434     (setq b (point))
4435     (gnus-eval-format
4436      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4437      `(,@(gnus-local-map-property gnus-mime-button-map)
4438          gnus-callback gnus-mm-display-part
4439          gnus-part ,gnus-tmp-id
4440          article-type annotation
4441          gnus-data ,handle))
4442     (setq e (point))
4443     (widget-convert-button
4444      'link b e
4445      :mime-handle handle
4446      :action 'gnus-widget-press-button
4447      :button-keymap gnus-mime-button-map
4448      :help-echo
4449      (lambda (widget/window &optional overlay pos)
4450        ;; Needed to properly clear the message due to a bug in
4451        ;; wid-edit (XEmacs only).
4452        (if (boundp 'help-echo-owns-message)
4453            (setq help-echo-owns-message t))
4454        (format
4455         "%S: %s the MIME part; %S: more options"
4456         (aref gnus-mouse-2 0)
4457         ;; XEmacs will get a single widget arg; Emacs 21 will get
4458         ;; window, overlay, position.
4459         (if (mm-handle-displayed-p
4460              (if overlay
4461                  (with-current-buffer (gnus-overlay-buffer overlay)
4462                    (widget-get (widget-at (gnus-overlay-start overlay))
4463                                :mime-handle))
4464                (widget-get widget/window :mime-handle)))
4465             "hide" "show")
4466         (aref gnus-down-mouse-3 0))))))
4467
4468 (defun gnus-widget-press-button (elems el)
4469   (goto-char (widget-get elems :from))
4470   (gnus-article-press-button))
4471
4472 (defvar gnus-displaying-mime nil)
4473
4474 (defun gnus-display-mime (&optional ihandles)
4475   "Display the MIME parts."
4476   (save-excursion
4477     (save-selected-window
4478       (let ((window (get-buffer-window gnus-article-buffer))
4479             (point (point)))
4480         (when window
4481           (select-window window)
4482           ;; We have to do this since selecting the window
4483           ;; may change the point.  So we set the window point.
4484           (set-window-point window point)))
4485       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
4486              buffer-read-only handle name type b e display)
4487         (when (and (not ihandles)
4488                    (not gnus-displaying-mime))
4489           ;; Top-level call; we clean up.
4490           (when gnus-article-mime-handles
4491             (mm-destroy-parts gnus-article-mime-handles)
4492             (setq gnus-article-mime-handle-alist nil));; A trick.
4493           (setq gnus-article-mime-handles handles)
4494           ;; We allow users to glean info from the handles.
4495           (when gnus-article-mime-part-function
4496             (gnus-mime-part-function handles)))
4497         (if (and handles
4498                  (or (not (stringp (car handles)))
4499                      (cdr handles)))
4500             (progn
4501               (when (and (not ihandles)
4502                          (not gnus-displaying-mime))
4503                 ;; Clean up for mime parts.
4504                 (article-goto-body)
4505                 (delete-region (point) (point-max)))
4506               (let ((gnus-displaying-mime t))
4507                 (gnus-mime-display-part handles)))
4508           (save-restriction
4509             (article-goto-body)
4510             (narrow-to-region (point) (point-max))
4511             (gnus-treat-article nil 1 1)
4512             (widen)))
4513         (unless ihandles
4514           ;; Highlight the headers.
4515           (save-excursion
4516             (save-restriction
4517               (article-goto-body)
4518               (narrow-to-region (point-min) (point))
4519               (gnus-treat-article 'head))))))))
4520
4521 (defvar gnus-mime-display-multipart-as-mixed nil)
4522 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4523 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4524
4525 (defun gnus-mime-display-part (handle)
4526   (cond
4527    ;; Single part.
4528    ((not (stringp (car handle)))
4529     (gnus-mime-display-single handle))
4530    ;; User-defined multipart
4531    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4532     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4533              handle))
4534    ;; multipart/alternative
4535    ((and (equal (car handle) "multipart/alternative")
4536          (not (or gnus-mime-display-multipart-as-mixed
4537                   gnus-mime-display-multipart-alternative-as-mixed)))
4538     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4539       (push (cons id handle) gnus-article-mime-handle-alist)
4540       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4541    ;; multipart/related
4542    ((and (equal (car handle) "multipart/related")
4543          (not (or gnus-mime-display-multipart-as-mixed
4544                   gnus-mime-display-multipart-related-as-mixed)))
4545     ;;;!!!We should find the start part, but we just default
4546     ;;;!!!to the first part.
4547     ;;(gnus-mime-display-part (cadr handle))
4548     ;;;!!! Most multipart/related is an HTML message plus images.
4549     ;;;!!! Unfortunately we are unable to let W3 display those
4550     ;;;!!! included images, so we just display it as a mixed multipart.
4551     ;;(gnus-mime-display-mixed (cdr handle))
4552     ;;;!!! No, w3 can display everything just fine.
4553     (gnus-mime-display-part (cadr handle)))
4554    ((equal (car handle) "multipart/signed")
4555     (gnus-add-wash-type 'signed)
4556     (gnus-mime-display-security handle))
4557    ((equal (car handle) "multipart/encrypted")
4558     (gnus-add-wash-type 'encrypted)
4559     (gnus-mime-display-security handle))
4560    ;; Other multiparts are handled like multipart/mixed.
4561    (t
4562     (gnus-mime-display-mixed (cdr handle)))))
4563
4564 (defun gnus-mime-part-function (handles)
4565   (if (stringp (car handles))
4566       (mapcar 'gnus-mime-part-function (cdr handles))
4567     (funcall gnus-article-mime-part-function handles)))
4568
4569 (defun gnus-mime-display-mixed (handles)
4570   (mapcar 'gnus-mime-display-part handles))
4571
4572 (defun gnus-mime-display-single (handle)
4573   (let ((type (mm-handle-media-type handle))
4574         (ignored gnus-ignored-mime-types)
4575         (not-attachment t)
4576         (move nil)
4577         display text)
4578     (catch 'ignored
4579       (progn
4580         (while ignored
4581           (when (string-match (pop ignored) type)
4582             (throw 'ignored nil)))
4583         (if (and (setq not-attachment
4584                        (and (not (mm-inline-override-p handle))
4585                             (or (not (mm-handle-disposition handle))
4586                                 (equal (car (mm-handle-disposition handle))
4587                                        "inline")
4588                                 (mm-attachment-override-p handle))))
4589                  (mm-automatic-display-p handle)
4590                  (or (and
4591                       (mm-inlinable-p handle)
4592                       (mm-inlined-p handle))
4593                      (mm-automatic-external-display-p type)))
4594             (setq display t)
4595           (when (equal (mm-handle-media-supertype handle) "text")
4596             (setq text t)))
4597         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4598               beg)
4599           (push (cons id handle) gnus-article-mime-handle-alist)
4600           (when (or (not display)
4601                     (not (gnus-unbuttonized-mime-type-p type)))
4602             ;(gnus-article-insert-newline)
4603             (gnus-insert-mime-button
4604              handle id (list (or display (and not-attachment text))))
4605             (gnus-article-insert-newline)
4606             ;(gnus-article-insert-newline)
4607             ;; Remember modify the number of forward lines.
4608             (setq move t))
4609           (setq beg (point))
4610           (cond
4611            (display
4612             (when move
4613               (forward-line -1)
4614               (setq beg (point)))
4615             (let ((mail-parse-charset gnus-newsgroup-charset)
4616                   (mail-parse-ignored-charsets
4617                    (save-excursion (condition-case ()
4618                                        (set-buffer gnus-summary-buffer)
4619                                      (error))
4620                                    gnus-newsgroup-ignored-charsets)))
4621               (mm-display-part handle t))
4622             (goto-char (point-max)))
4623            ((and text not-attachment)
4624             (when move
4625               (forward-line -1)
4626               (setq beg (point)))
4627             (gnus-article-insert-newline)
4628             (mm-insert-inline handle (mm-get-part handle))
4629             (goto-char (point-max))))
4630           ;; Do highlighting.
4631           (save-excursion
4632             (save-restriction
4633               (narrow-to-region beg (point))
4634               (gnus-treat-article
4635                nil id
4636                (gnus-article-mime-total-parts)
4637                (mm-handle-media-type handle)))))))))
4638
4639 (defun gnus-unbuttonized-mime-type-p (type)
4640   "Say whether TYPE is to be unbuttonized."
4641   (unless gnus-inhibit-mime-unbuttonizing
4642     (when (catch 'found
4643             (let ((types gnus-unbuttonized-mime-types))
4644               (while types
4645                 (when (string-match (pop types) type)
4646                   (throw 'found t)))))
4647       (not (catch 'found
4648              (let ((types gnus-buttonized-mime-types))
4649                (while types
4650                  (when (string-match (pop types) type)
4651                    (throw 'found t)))))))))
4652
4653 (defun gnus-article-insert-newline ()
4654   "Insert a newline, but mark it as undeletable."
4655   (gnus-put-text-property
4656    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4657
4658 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4659   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4660          (ihandles handles)
4661          (point (point))
4662          handle buffer-read-only from props begend not-pref)
4663     (save-window-excursion
4664       (save-restriction
4665         (when ibegend
4666           (narrow-to-region (car ibegend)
4667                             (or (cdr ibegend)
4668                                 (progn
4669                                   (goto-char (car ibegend))
4670                                   (forward-line 2)
4671                                   (point))))
4672           (delete-region (point-min) (point-max))
4673           (mm-remove-parts handles))
4674         (setq begend (list (point-marker)))
4675         ;; Do the toggle.
4676         (unless (setq not-pref (cadr (member preferred ihandles)))
4677           (setq not-pref (car ihandles)))
4678         (when (or ibegend
4679                   (not preferred)
4680                   (not (gnus-unbuttonized-mime-type-p
4681                         "multipart/alternative")))
4682           (gnus-add-text-properties
4683            (setq from (point))
4684            (progn
4685              (insert (format "%d.  " id))
4686              (point))
4687            `(gnus-callback
4688              (lambda (handles)
4689                (unless ,(not ibegend)
4690                  (setq gnus-article-mime-handle-alist
4691                        ',gnus-article-mime-handle-alist))
4692                (gnus-mime-display-alternative
4693                 ',ihandles ',not-pref ',begend ,id))
4694              ,@(gnus-local-map-property gnus-mime-button-map)
4695              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4696              face ,gnus-article-button-face
4697              gnus-part ,id
4698              gnus-data ,handle))
4699           (widget-convert-button 'link from (point)
4700                                  :action 'gnus-widget-press-button
4701                                  :button-keymap gnus-widget-button-keymap)
4702           ;; Do the handles
4703           (while (setq handle (pop handles))
4704             (gnus-add-text-properties
4705              (setq from (point))
4706              (progn
4707                (insert (format "(%c) %-18s"
4708                                (if (equal handle preferred) ?* ? )
4709                                (mm-handle-media-type handle)))
4710                (point))
4711              `(gnus-callback
4712                (lambda (handles)
4713                  (unless ,(not ibegend)
4714                    (setq gnus-article-mime-handle-alist
4715                          ',gnus-article-mime-handle-alist))
4716                  (gnus-mime-display-alternative
4717                   ',ihandles ',handle ',begend ,id))
4718                ,@(gnus-local-map-property gnus-mime-button-map)
4719                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4720                face ,gnus-article-button-face
4721                gnus-part ,id
4722                gnus-data ,handle))
4723             (widget-convert-button 'link from (point)
4724                                    :action 'gnus-widget-press-button
4725                                    :button-keymap gnus-widget-button-keymap)
4726             (insert "  "))
4727           (insert "\n\n"))
4728         (when preferred
4729           (if (stringp (car preferred))
4730               (gnus-display-mime preferred)
4731             (let ((mail-parse-charset gnus-newsgroup-charset)
4732                   (mail-parse-ignored-charsets
4733                    (save-excursion (set-buffer gnus-summary-buffer)
4734                                    gnus-newsgroup-ignored-charsets)))
4735               (mm-display-part preferred)
4736               ;; Do highlighting.
4737               (save-excursion
4738                 (save-restriction
4739                   (narrow-to-region (car begend) (point-max))
4740                   (gnus-treat-article
4741                    nil (length gnus-article-mime-handle-alist)
4742                    (gnus-article-mime-total-parts)
4743                    (mm-handle-media-type handle))))))
4744           (goto-char (point-max))
4745           (setcdr begend (point-marker)))))
4746     (when ibegend
4747       (goto-char point))))
4748
4749 (defconst gnus-article-wash-status-strings
4750   (let ((alist '((cite "c" "Possible hidden citation text"
4751                        " " "All citation text visible")
4752                  (headers "h" "Hidden headers"
4753                           " " "All headers visible.")
4754                  (pgp "p" "Encrypted or signed message status hidden"
4755                       " " "No hidden encryption nor digital signature status")
4756                  (signature "s" "Signature has been hidden"
4757                             " " "Signature is visible")
4758                  (overstrike "o" "Overstrike (^H) characters applied"
4759                              " " "No overstrike characters applied")
4760                  (gnus-show-mime "m" "Mime processing is activated"
4761                                  " " "Mime processing is not activated")
4762                  (emphasis "e" "/*_Emphasis_*/ characters applied"
4763                            " " "No /*_emphasis_*/ characters applied")))
4764         result)
4765     (dolist (entry alist result)
4766       (let ((key (nth 0 entry))
4767             (on (copy-sequence (nth 1 entry)))
4768             (on-help (nth 2 entry))
4769             (off (copy-sequence (nth 3 entry)))
4770             (off-help (nth 4 entry)))
4771         (put-text-property 0 1 'help-echo on-help on)
4772         (put-text-property 0 1 'help-echo off-help off)
4773         (push (list key on off) result))))
4774   "Alist of strings describing wash status in the mode line.
4775 Each entry has the form (KEY ON OF), where the KEY is a symbol
4776 representing the particular washing function, ON is the string to use
4777 in the article mode line when the washing function is active, and OFF
4778 is the string to use when it is inactive.")
4779
4780 (defun gnus-article-wash-status-entry (key value)
4781   (let ((entry (assoc key gnus-article-wash-status-strings)))
4782     (if value (nth 1 entry) (nth 2 entry))))
4783
4784 (defun gnus-article-wash-status ()
4785   "Return a string which display status of article washing."
4786   (save-excursion
4787     (set-buffer gnus-article-buffer)
4788     (let ((cite (memq 'cite gnus-article-wash-types))
4789           (headers (memq 'headers gnus-article-wash-types))
4790           (boring (memq 'boring-headers gnus-article-wash-types))
4791           (pgp (memq 'pgp gnus-article-wash-types))
4792           (pem (memq 'pem gnus-article-wash-types))
4793           (signed (memq 'signed gnus-article-wash-types))
4794           (encrypted (memq 'encrypted gnus-article-wash-types))
4795           (signature (memq 'signature gnus-article-wash-types))
4796           (overstrike (memq 'overstrike gnus-article-wash-types))
4797           (emphasis (memq 'emphasis gnus-article-wash-types)))
4798       (concat
4799        (gnus-article-wash-status-entry 'cite cite)
4800        (gnus-article-wash-status-entry 'headers (or headers boring))
4801        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
4802        (gnus-article-wash-status-entry 'signature signature)
4803        (gnus-article-wash-status-entry 'overstrike overstrike)
4804        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
4805        (gnus-article-wash-status-entry 'emphasis emphasis)))))
4806
4807 (defun gnus-add-wash-type (type)
4808   "Add a washing of TYPE to the current status."
4809   (add-to-list 'gnus-article-wash-types type))
4810
4811 (defun gnus-delete-wash-type (type)
4812   "Add a washing of TYPE to the current status."
4813   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
4814
4815 (defun gnus-add-image (category image)
4816   "Add IMAGE of CATEGORY to the list of displayed images."
4817   (let ((entry (assq category gnus-article-image-alist)))
4818     (unless entry
4819       (setq entry (list category))
4820       (push entry gnus-article-image-alist))
4821     (nconc entry (list image))))
4822
4823 (defun gnus-delete-images (category)
4824   "Delete all images in CATEGORY."
4825   (let ((entry (assq category gnus-article-image-alist)))
4826     (dolist (image (cdr entry))
4827       (gnus-remove-image image))
4828     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
4829     (gnus-delete-wash-type category)))
4830
4831 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4832
4833 (defun gnus-article-maybe-hide-headers ()
4834   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4835 Provided for backwards compatibility."
4836   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4837                  (not (save-excursion (set-buffer gnus-summary-buffer)
4838                                       gnus-have-all-headers)))
4839              (not gnus-inhibit-hiding))
4840     (gnus-article-hide-headers)))
4841
4842 ;;; Article savers.
4843
4844 (defun gnus-output-to-file (file-name)
4845   "Append the current article to a file named FILE-NAME."
4846   (let ((artbuf (current-buffer)))
4847     (with-temp-buffer
4848       (insert-buffer-substring artbuf)
4849       ;; Append newline at end of the buffer as separator, and then
4850       ;; save it to file.
4851       (goto-char (point-max))
4852       (insert "\n")
4853       (let ((file-name-coding-system nnmail-pathname-coding-system)
4854             (pathname-coding-system nnmail-pathname-coding-system))
4855         (write-region-as-binary (point-min) (point-max) file-name 'append))
4856       t)))
4857
4858 (defun gnus-narrow-to-page (&optional arg)
4859   "Narrow the article buffer to a page.
4860 If given a numerical ARG, move forward ARG pages."
4861   (interactive "P")
4862   (setq arg (if arg (prefix-numeric-value arg) 0))
4863   (save-excursion
4864     (set-buffer gnus-article-buffer)
4865     (goto-char (point-min))
4866     (widen)
4867     ;; Remove any old next/prev buttons.
4868     (when (gnus-visual-p 'page-marker)
4869       (let ((buffer-read-only nil))
4870         (gnus-remove-text-with-property 'gnus-prev)
4871         (gnus-remove-text-with-property 'gnus-next)))
4872     (when
4873         (cond ((< arg 0)
4874                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4875               ((> arg 0)
4876                (re-search-forward page-delimiter nil 'move arg)))
4877       (goto-char (match-end 0)))
4878     (narrow-to-region
4879      (point)
4880      (if (re-search-forward page-delimiter nil 'move)
4881          (match-beginning 0)
4882        (point)))
4883     (when (and (gnus-visual-p 'page-marker)
4884                (not (= (point-min) 1)))
4885       (save-excursion
4886         (goto-char (point-min))
4887         (gnus-insert-prev-page-button)))
4888     (when (and (gnus-visual-p 'page-marker)
4889                (< (+ (point-max) 2) (buffer-size)))
4890       (save-excursion
4891         (goto-char (point-max))
4892         (gnus-insert-next-page-button)))))
4893
4894 ;; Article mode commands
4895
4896 (defun gnus-article-goto-next-page ()
4897   "Show the next page of the article."
4898   (interactive)
4899   (when (gnus-article-next-page)
4900     (goto-char (point-min))
4901     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4902
4903 (defun gnus-article-goto-prev-page ()
4904   "Show the next page of the article."
4905   (interactive)
4906   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4907     (gnus-article-prev-page nil)))
4908
4909 (defun gnus-article-next-page (&optional lines)
4910   "Show the next page of the current article.
4911 If end of article, return non-nil.  Otherwise return nil.
4912 Argument LINES specifies lines to be scrolled up."
4913   (interactive "p")
4914   (let ((start (window-start))
4915         end-of-buffer end-of-page)
4916     (save-excursion
4917       (move-to-window-line -1)
4918       (if (<= (point) start)
4919           (progn
4920             (forward-line 2)
4921             (setq start (point)))
4922         (forward-line 1)
4923         (setq start nil))
4924       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4925                          (and (pos-visible-in-window-p)
4926                               (setq end-of-buffer t)))
4927                         ((eobp)
4928                          (setq end-of-page t)))
4929                   (not lines))
4930         (move-to-window-line lines)
4931         (unless (search-backward "\n\n" nil t)
4932           (setq start (point)))))
4933     (cond (end-of-buffer t)
4934           (end-of-page
4935            (gnus-narrow-to-page 1)
4936            nil)
4937           (t
4938            (if start
4939                (set-window-start (selected-window) start)
4940              (let (window-pixel-scroll-increment)
4941                (scroll-up lines)))
4942            nil))))
4943
4944 (defun gnus-article-prev-page (&optional lines)
4945   "Show previous page of current article.
4946 Argument LINES specifies lines to be scrolled down."
4947   (interactive "p")
4948   (let (beginning-of-buffer beginning-of-page)
4949     (save-excursion
4950       (move-to-window-line 0)
4951       (cond ((eq 1 (point))
4952              (setq beginning-of-buffer t))
4953             ((bobp)
4954              (setq beginning-of-page t))))
4955     (cond (beginning-of-buffer)
4956           (beginning-of-page
4957            (gnus-narrow-to-page -1))
4958           (t
4959            (condition-case nil
4960                (let (window-pixel-scroll-increment)
4961                  (scroll-down lines))
4962              (beginning-of-buffer
4963               (goto-char (point-min))))))))
4964
4965 (defun gnus-article-refer-article ()
4966   "Read article specified by message-id around point."
4967   (interactive)
4968   (let ((point (point)))
4969     (search-forward ">" nil t)          ;Move point to end of "<....>".
4970     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4971         (let ((message-id (match-string 1)))
4972           (goto-char point)
4973           (set-buffer gnus-summary-buffer)
4974           (gnus-summary-refer-article message-id))
4975       (goto-char (point))
4976       (error "No references around point"))))
4977
4978 (defun gnus-article-show-summary ()
4979   "Reconfigure windows to show summary buffer."
4980   (interactive)
4981   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4982       (error "There is no summary buffer for this article buffer")
4983     (gnus-article-set-globals)
4984     (gnus-configure-windows 'article)
4985     (gnus-summary-goto-subject gnus-current-article)
4986     (gnus-summary-position-point)))
4987
4988 (defun gnus-article-describe-briefly ()
4989   "Describe article mode commands briefly."
4990   (interactive)
4991   (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")))
4992
4993 (defun gnus-article-summary-command ()
4994   "Execute the last keystroke in the summary buffer."
4995   (interactive)
4996   (let ((obuf (current-buffer))
4997         (owin (current-window-configuration))
4998         func)
4999     (switch-to-buffer gnus-article-current-summary 'norecord)
5000     (setq func (lookup-key (current-local-map) (this-command-keys)))
5001     (call-interactively func)
5002     (set-buffer obuf)
5003     (set-window-configuration owin)
5004     (set-window-point (get-buffer-window (current-buffer)) (point))))
5005
5006 (defun gnus-article-summary-command-nosave ()
5007   "Execute the last keystroke in the summary buffer."
5008   (interactive)
5009   (let (func)
5010     (pop-to-buffer gnus-article-current-summary 'norecord)
5011     (setq func (lookup-key (current-local-map) (this-command-keys)))
5012     (call-interactively func)))
5013
5014 (defun gnus-article-check-buffer ()
5015   "Beep if not in an article buffer."
5016   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5017     (error "Command invoked outside of a Gnus article buffer")))
5018
5019 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5020   "Read a summary buffer key sequence and execute it from the article buffer."
5021   (interactive "P")
5022   (gnus-article-check-buffer)
5023   (let ((nosaves
5024          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5025            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5026            "=" "^" "\M-^" "|"))
5027         (nosave-but-article
5028          '("A\r"))
5029         (nosave-in-article
5030          '("\C-d"))
5031         (up-to-top
5032          '("n" "Gn" "p" "Gp"))
5033         keys new-sum-point)
5034     (save-excursion
5035       (set-buffer gnus-article-current-summary)
5036       (let (gnus-pick-mode)
5037         (push (or key last-command-event) unread-command-events)
5038         (setq keys (static-if (featurep 'xemacs)
5039                        (events-to-keys (read-key-sequence nil))
5040                      (read-key-sequence nil)))))
5041     (message "")
5042
5043     (if (or (member keys nosaves)
5044             (member keys nosave-but-article)
5045             (member keys nosave-in-article))
5046         (let (func)
5047           (save-window-excursion
5048             (pop-to-buffer gnus-article-current-summary 'norecord)
5049             ;; We disable the pick minor mode commands.
5050             (let (gnus-pick-mode)
5051               (setq func (lookup-key (current-local-map) keys))))
5052           (if (or (not func)
5053                   (numberp func))
5054               (ding)
5055             (unless (member keys nosave-in-article)
5056               (set-buffer gnus-article-current-summary))
5057             (call-interactively func)
5058             (setq new-sum-point (point)))
5059           (when (member keys nosave-but-article)
5060             (pop-to-buffer gnus-article-buffer 'norecord)))
5061       ;; These commands should restore window configuration.
5062       (let ((obuf (current-buffer))
5063             (owin (current-window-configuration))
5064             (opoint (point))
5065             (summary gnus-article-current-summary)
5066             func in-buffer selected)
5067         (if not-restore-window
5068             (pop-to-buffer summary 'norecord)
5069           (switch-to-buffer summary 'norecord))
5070         (setq in-buffer (current-buffer))
5071         ;; We disable the pick minor mode commands.
5072         (if (and (setq func (let (gnus-pick-mode)
5073                               (lookup-key (current-local-map) keys)))
5074                  (functionp func))
5075             (progn
5076               (call-interactively func)
5077               (setq new-sum-point (point))
5078               (when (eq in-buffer (current-buffer))
5079                 (setq selected (gnus-summary-select-article))
5080                 (set-buffer obuf)
5081                 (unless not-restore-window
5082                   (set-window-configuration owin))
5083                 (when (eq selected 'old)
5084                   (article-goto-body)
5085                   (set-window-start (get-buffer-window (current-buffer))
5086                                     1)
5087                   (set-window-point (get-buffer-window (current-buffer))
5088                                     (point)))
5089                 (let ((win (get-buffer-window gnus-article-current-summary)))
5090                   (when win
5091                     (set-window-point win new-sum-point))))    )
5092           (switch-to-buffer gnus-article-buffer)
5093           (ding))))))
5094
5095 (defun gnus-article-describe-key (key)
5096   "Display documentation of the function invoked by KEY.  KEY is a string."
5097   (interactive "kDescribe key: ")
5098   (gnus-article-check-buffer)
5099   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5100       (save-excursion
5101         (set-buffer gnus-article-current-summary)
5102         (let (gnus-pick-mode)
5103           (if (featurep 'xemacs)
5104               (progn
5105                 (push (elt key 0) unread-command-events)
5106                 (setq key (events-to-keys
5107                            (read-key-sequence "Describe key: "))))
5108             (setq unread-command-events
5109                   (mapcar
5110                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5111                    key))
5112             (setq key (read-key-sequence "Describe key: "))))
5113         (describe-key key))
5114     (describe-key key)))
5115
5116 (defun gnus-article-describe-key-briefly (key &optional insert)
5117   "Display documentation of the function invoked by KEY.  KEY is a string."
5118   (interactive "kDescribe key: \nP")
5119   (gnus-article-check-buffer)
5120   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5121       (save-excursion
5122         (set-buffer gnus-article-current-summary)
5123         (let (gnus-pick-mode)
5124           (if (featurep 'xemacs)
5125               (progn
5126                 (push (elt key 0) unread-command-events)
5127                 (setq key (events-to-keys
5128                            (read-key-sequence "Describe key: "))))
5129             (setq unread-command-events
5130                   (mapcar
5131                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5132                    key))
5133             (setq key (read-key-sequence "Describe key: "))))
5134         (describe-key-briefly key insert))
5135     (describe-key-briefly key insert)))
5136
5137 (defun gnus-article-reply-with-original (&optional wide)
5138   "Start composing a reply mail to the current message.
5139 The text in the region will be yanked.  If the region isn't active,
5140 the entire article will be yanked."
5141   (interactive "P")
5142   (let ((article (cdr gnus-article-current)) cont)
5143     (if (not (mark))
5144         (gnus-summary-reply (list (list article)) wide)
5145       (setq cont (buffer-substring (point) (mark)))
5146       ;; Deactivate active regions.
5147       (when (and (boundp 'transient-mark-mode)
5148                  transient-mark-mode)
5149         (setq mark-active nil))
5150       (gnus-summary-reply
5151        (list (list article cont)) wide))))
5152
5153 (defun gnus-article-followup-with-original ()
5154   "Compose a followup to the current article.
5155 The text in the region will be yanked.  If the region isn't active,
5156 the entire article will be yanked."
5157   (interactive)
5158   (let ((article (cdr gnus-article-current))
5159         cont)
5160     (if (not (gnus-region-active-p))
5161         (gnus-summary-followup (list (list article)))
5162       (setq cont (buffer-substring (point) (mark)))
5163       ;; Deactivate active regions.
5164       (when (and (boundp 'transient-mark-mode)
5165                  transient-mark-mode)
5166         (setq mark-active nil))
5167       (gnus-summary-followup
5168        (list (list article cont))))))
5169
5170 (defun gnus-article-hide (&optional arg force)
5171   "Hide all the gruft in the current article.
5172 This means that PGP stuff, signatures, cited text and (some)
5173 headers will be hidden.
5174 If given a prefix, show the hidden text instead."
5175   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5176   (gnus-article-hide-headers arg)
5177   (gnus-article-hide-list-identifiers arg)
5178   (gnus-article-hide-pgp arg)
5179   (gnus-article-hide-citation-maybe arg force)
5180   (gnus-article-hide-signature arg))
5181
5182 (defun gnus-article-maybe-highlight ()
5183   "Do some article highlighting if article highlighting is requested."
5184   (when (gnus-visual-p 'article-highlight 'highlight)
5185     (gnus-article-highlight-some)))
5186
5187 (defun gnus-check-group-server ()
5188   ;; Make sure the connection to the server is alive.
5189   (unless (gnus-server-opened
5190            (gnus-find-method-for-group gnus-newsgroup-name))
5191     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5192     (gnus-request-group gnus-newsgroup-name t)))
5193
5194 (eval-when-compile
5195   (autoload 'nneething-get-file-name "nneething"))
5196
5197 (defun gnus-request-article-this-buffer (article group)
5198   "Get an article and insert it into this buffer.
5199 T-gnus change: Insert an article into `gnus-original-article-buffer'."
5200   (let (do-update-line sparse-header)
5201     ;; The current buffer is `gnus-article-buffer'.
5202     (prog1
5203         (save-excursion
5204           (erase-buffer)
5205           (gnus-kill-all-overlays)
5206           (setq group (or group gnus-newsgroup-name))
5207
5208           ;; Using `gnus-request-article' directly will insert the article into
5209           ;; `nntp-server-buffer' - so we'll save some time by not having to
5210           ;; copy it from the server buffer into the article buffer.
5211
5212           ;; We only request an article by message-id when we do not have the
5213           ;; headers for it, so we'll have to get those.
5214           (when (stringp article)
5215             (gnus-read-header article))
5216
5217           ;; If the article number is negative, that means that this article
5218           ;; doesn't belong in this newsgroup (possibly), so we find its
5219           ;; message-id and request it by id instead of number.
5220           (when (and (numberp article)
5221                      gnus-summary-buffer
5222                      (get-buffer gnus-summary-buffer)
5223                      (gnus-buffer-exists-p gnus-summary-buffer))
5224             (save-excursion
5225               (set-buffer gnus-summary-buffer)
5226               (let ((header (gnus-summary-article-header article)))
5227                 (when (< article 0)
5228                   (cond
5229                    ((memq article gnus-newsgroup-sparse)
5230                     ;; This is a sparse gap article.
5231                     (setq do-update-line article)
5232                     (setq article (mail-header-id header))
5233                     (setq sparse-header (gnus-read-header article))
5234                     (setq gnus-newsgroup-sparse
5235                           (delq article gnus-newsgroup-sparse)))
5236                    ((vectorp header)
5237                     ;; It's a real article.
5238                     (setq article (mail-header-id header)))
5239                    (t
5240                     ;; It is an extracted pseudo-article.
5241                     (setq article 'pseudo)
5242                     (gnus-request-pseudo-article header))))
5243
5244                 (let ((method (gnus-find-method-for-group
5245                                gnus-newsgroup-name)))
5246                   (when (and (eq (car method) 'nneething)
5247                              (vectorp header))
5248                     (let ((dir (nneething-get-file-name
5249                                 (mail-header-id header))))
5250                       (when (and (stringp dir)
5251                                  (file-directory-p dir))
5252                         (setq article 'nneething)
5253                         (gnus-group-enter-directory dir))))))))
5254           (setq gnus-original-article (cons group article))
5255
5256           ;; The current buffer is `gnus-original-article-buffer'.
5257           (if (get-buffer gnus-original-article-buffer)
5258               (set-buffer gnus-original-article-buffer)
5259             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5260             (set-buffer-multibyte nil)
5261             (buffer-disable-undo)
5262             (setq major-mode 'gnus-original-article-mode)
5263             (setq buffer-read-only nil))
5264
5265           (cond
5266            ;; Refuse to select canceled articles.
5267            ((and (numberp article)
5268                  gnus-summary-buffer
5269                  (get-buffer gnus-summary-buffer)
5270                  (gnus-buffer-exists-p gnus-summary-buffer)
5271                  (eq (cdr (save-excursion
5272                             (set-buffer gnus-summary-buffer)
5273                             (assq article gnus-newsgroup-reads)))
5274                      gnus-canceled-mark))
5275             nil)
5276            ;; Check the backlog.
5277            ((and gnus-keep-backlog
5278                  (gnus-backlog-request-article group article (current-buffer)))
5279             'article)
5280            ;; Check asynchronous pre-fetch.
5281            ((gnus-async-request-fetched-article group article (current-buffer))
5282             (gnus-async-prefetch-next group article gnus-summary-buffer)
5283             (when (and (numberp article) gnus-keep-backlog)
5284               (gnus-backlog-enter-article group article (current-buffer)))
5285             'article)
5286            ;; Check the cache.
5287            ((and gnus-use-cache
5288                  (numberp article)
5289                  (gnus-cache-request-article article group))
5290             'article)
5291            ;; Check the agent cache.
5292            ((and gnus-agent gnus-agent-cache gnus-plugged
5293                  (numberp article)
5294                  (gnus-agent-request-article article group))
5295             'article)
5296            ;; Get the article and put into the article buffer.
5297            ((or (stringp article)
5298                 (numberp article))
5299             (let ((gnus-override-method gnus-override-method)
5300                   (methods (and (stringp article)
5301                                 gnus-refer-article-method))
5302                   result
5303                   (buffer-read-only nil))
5304               (if (or (not (listp methods))
5305                       (and (symbolp (car methods))
5306                            (assq (car methods) nnoo-definition-alist)))
5307                   (setq methods (list methods)))
5308               (when (and (null gnus-override-method)
5309                          methods)
5310                 (setq gnus-override-method (pop methods)))
5311               (while (not result)
5312                 (when (eq gnus-override-method 'current)
5313                   (setq gnus-override-method
5314                         (with-current-buffer gnus-summary-buffer
5315                           gnus-current-select-method)))
5316                 (erase-buffer)
5317                 (gnus-kill-all-overlays)
5318                 (let ((gnus-newsgroup-name group))
5319                   (gnus-check-group-server))
5320                 (when (gnus-request-article article group (current-buffer))
5321                   (when (numberp article)
5322                     (gnus-async-prefetch-next group article
5323                                               gnus-summary-buffer)
5324                     (when gnus-keep-backlog
5325                       (gnus-backlog-enter-article
5326                        group article (current-buffer))))
5327                   (setq result 'article))
5328                 (if (not result)
5329                     (if methods
5330                         (setq gnus-override-method (pop methods))
5331                       (setq result 'done))))
5332               (and (eq result 'article) 'article)))
5333            ;; It was a pseudo.
5334            (t article)))
5335
5336       ;; Associate this article with the current summary buffer.
5337       (setq gnus-article-current-summary gnus-summary-buffer)
5338
5339       ;; Copy the requested article from `gnus-original-article-buffer'.
5340       (unless (equal (buffer-name (current-buffer))
5341                      (buffer-name (get-buffer gnus-original-article-buffer)))
5342         ;; There may be the same article if the current buffer is
5343         ;; `nntp-server-buffer' (e.g. a case that the command
5344         ;; `gnus-cache-enter-article' is invoked), it should be erased.
5345         (erase-buffer)
5346         (insert-buffer gnus-original-article-buffer))
5347
5348       ;; Decode charsets.
5349       (run-hooks 'gnus-article-decode-hook)
5350       ;; Mark article as decoded or not.
5351       (setq gnus-article-decoded-p gnus-article-decode-hook)
5352
5353       ;; Update sparse articles.
5354       (when (and do-update-line
5355                  (or (numberp article)
5356                      (stringp article)))
5357         (let ((buf (current-buffer)))
5358           (set-buffer gnus-summary-buffer)
5359           (gnus-summary-update-article do-update-line sparse-header)
5360           (gnus-summary-goto-subject do-update-line nil t)
5361           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5362                             (point))
5363           (set-buffer buf))))))
5364
5365 ;;;
5366 ;;; Article editing
5367 ;;;
5368
5369 (defcustom gnus-article-edit-mode-hook nil
5370   "Hook run in article edit mode buffers."
5371   :group 'gnus-article-various
5372   :type 'hook)
5373
5374 (defcustom gnus-article-edit-article-setup-function
5375   'gnus-article-mime-edit-article-setup
5376   "Function called to setup an editing article buffer."
5377   :group 'gnus-article-various
5378   :type 'function)
5379
5380 (defvar gnus-article-edit-done-function nil)
5381
5382 (defvar gnus-article-edit-mode-map nil)
5383
5384 ;; Should we be using derived.el for this?
5385 (unless gnus-article-edit-mode-map
5386   (setq gnus-article-edit-mode-map (make-sparse-keymap))
5387   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5388
5389   (gnus-define-keys gnus-article-edit-mode-map
5390     "\C-c\C-c" gnus-article-edit-done
5391     "\C-c\C-k" gnus-article-edit-exit)
5392
5393   (gnus-define-keys (gnus-article-edit-wash-map
5394                      "\C-c\C-w" gnus-article-edit-mode-map)
5395     "f" gnus-article-edit-full-stops))
5396
5397 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5398   "Major mode for editing articles.
5399 This is an extended text-mode.
5400
5401 \\{gnus-article-edit-mode-map}"
5402   (make-local-variable 'gnus-article-edit-done-function)
5403   (make-local-variable 'gnus-prev-winconf)
5404   (set (make-local-variable 'font-lock-defaults)
5405        '(message-font-lock-keywords t))
5406   (setq buffer-read-only nil)
5407   (buffer-enable-undo)
5408   (widen))
5409
5410 (defun gnus-article-edit (&optional force)
5411   "Edit the current article.
5412 This will have permanent effect only in mail groups.
5413 If FORCE is non-nil, allow editing of articles even in read-only
5414 groups."
5415   (interactive "P")
5416   (when (and (not force)
5417              (gnus-group-read-only-p))
5418     (error "The current newsgroup does not support article editing"))
5419   (gnus-article-date-original)
5420   (gnus-article-edit-article
5421    'ignore
5422    `(lambda (no-highlight)
5423       'ignore
5424       (gnus-summary-edit-article-done
5425        ,(or (mail-header-references gnus-current-headers) "")
5426        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5427
5428 (defun gnus-article-edit-article (start-func exit-func)
5429   "Start editing the contents of the current article buffer."
5430   (let ((winconf (current-window-configuration)))
5431     (set-buffer gnus-article-buffer)
5432     (gnus-article-edit-mode)
5433     (funcall start-func)
5434     (set-buffer-modified-p nil)
5435     (gnus-configure-windows 'edit-article)
5436     (setq gnus-article-edit-done-function exit-func)
5437     (setq gnus-prev-winconf winconf)
5438     (when gnus-article-edit-article-setup-function
5439       (funcall gnus-article-edit-article-setup-function))
5440     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5441
5442 (defun gnus-article-edit-done (&optional arg)
5443   "Update the article edits and exit."
5444   (interactive "P")
5445   (let ((func gnus-article-edit-done-function)
5446         (buf (current-buffer))
5447         (start (window-start)))
5448     (remove-hook 'gnus-article-mode-hook
5449                  'gnus-article-mime-edit-article-unwind)
5450     ;; We remove all text props from the article buffer.
5451     (let ((content
5452            (buffer-substring-no-properties (point-min) (point-max)))
5453           (p (point)))
5454       (erase-buffer)
5455       (insert content)
5456       (let ((winconf gnus-prev-winconf))
5457         (gnus-article-mode)
5458         (set-window-configuration winconf)
5459         ;; Tippy-toe some to make sure that point remains where it was.
5460         (save-current-buffer
5461           (set-buffer buf)
5462           (set-window-start (get-buffer-window (current-buffer)) start)
5463           (goto-char p))))
5464     (save-excursion
5465       (set-buffer buf)
5466       (let ((buffer-read-only nil))
5467         (funcall func arg))
5468       ;; The cache and backlog have to be flushed somewhat.
5469       (when gnus-keep-backlog
5470         (gnus-backlog-remove-article
5471          (car gnus-article-current) (cdr gnus-article-current)))
5472       ;; Flush original article as well.
5473       (save-excursion
5474         (when (get-buffer gnus-original-article-buffer)
5475           (set-buffer gnus-original-article-buffer)
5476           (setq gnus-original-article nil)))
5477       (when gnus-use-cache
5478         (gnus-cache-update-article
5479          (car gnus-article-current) (cdr gnus-article-current))))
5480     (set-buffer buf)
5481     (set-window-start (get-buffer-window buf) start)
5482     (set-window-point (get-buffer-window buf) (point))))
5483
5484 (defun gnus-article-edit-exit ()
5485   "Exit the article editing without updating."
5486   (interactive)
5487   (when (or (not (buffer-modified-p))
5488             (yes-or-no-p "Article modified; kill anyway? "))
5489     (let ((curbuf (current-buffer))
5490           (p (point))
5491           (window-start (window-start)))
5492       (erase-buffer)
5493       (if (gnus-buffer-live-p gnus-original-article-buffer)
5494           (insert-buffer gnus-original-article-buffer))
5495       (let ((winconf gnus-prev-winconf))
5496         (gnus-article-mode)
5497         (set-window-configuration winconf)
5498         ;; Tippy-toe some to make sure that point remains where it was.
5499         (save-current-buffer
5500           (set-buffer curbuf)
5501           (set-window-start (get-buffer-window (current-buffer)) window-start)
5502           (goto-char p))))))
5503
5504 (defun gnus-article-edit-full-stops ()
5505   "Interactively repair spacing at end of sentences."
5506   (interactive)
5507   (save-excursion
5508     (goto-char (point-min))
5509     (search-forward-regexp "^$" nil t)
5510     (let ((case-fold-search nil))
5511       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5512
5513 ;;;
5514 ;;; Article editing with MIME-Edit
5515 ;;;
5516
5517 (defcustom gnus-article-mime-edit-article-setup-hook nil
5518   "Hook run after setting up a MIME editing article buffer."
5519   :group 'gnus-article-various
5520   :type 'hook)
5521
5522 (defun gnus-article-mime-edit-article-unwind ()
5523   "Unwind `gnus-article-buffer' if article editing was given up."
5524   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5525   (when (featurep 'font-lock)
5526     (setq font-lock-defaults nil)
5527     (font-lock-mode -1))
5528   (when mime-edit-mode-flag
5529     (mime-edit-exit 'nomime 'no-error)
5530     (message "")))
5531
5532 (defun gnus-article-mime-edit-article-setup ()
5533   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5534 after replacing with the original article."
5535   (setq gnus-show-mime t)
5536   (setq gnus-article-edit-done-function
5537         `(lambda (&rest args)
5538            (when mime-edit-mode-flag
5539              (let (mime-edit-insert-user-agent-field)
5540                (mime-edit-exit))
5541              (message ""))
5542            (goto-char (point-min))
5543            (let (case-fold-search)
5544              (when (re-search-forward
5545                     (format "^%s$" (regexp-quote mail-header-separator))
5546                     nil t)
5547                (replace-match "")))
5548            (apply ,gnus-article-edit-done-function args)
5549            (insert
5550             (prog1
5551                 (buffer-substring-no-properties (point-min) (point-max))
5552               (set-buffer (get-buffer-create gnus-original-article-buffer))
5553               (erase-buffer)))
5554            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5555            (set-buffer gnus-article-buffer)
5556            (gnus-article-prepare-display)))
5557   (substitute-key-definition 'gnus-article-edit-done
5558                              'gnus-article-mime-edit-done
5559                              gnus-article-edit-mode-map)
5560   (substitute-key-definition 'gnus-article-edit-exit
5561                              'gnus-article-mime-edit-exit
5562                              gnus-article-edit-mode-map)
5563   (erase-buffer)
5564   (insert-buffer gnus-original-article-buffer)
5565   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5566     (fset 'mime-edit-decode-single-part-in-buffer
5567           (lambda (&rest args)
5568             (if (let ((content-type (car args)))
5569                   (and (eq 'message (mime-content-type-primary-type
5570                                      content-type))
5571                        (eq 'rfc822 (mime-content-type-subtype content-type))))
5572                 (setcar (cdr args) 'not-decode-text))
5573             (apply ofn args)))
5574     (unwind-protect
5575         (mime-edit-again)
5576       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
5577   (when (featurep 'font-lock)
5578     (set (make-local-variable 'font-lock-defaults)
5579          '(message-font-lock-keywords t))
5580     (font-lock-set-defaults)
5581     (turn-on-font-lock))
5582   (set-buffer-modified-p nil)
5583   (delete-other-windows)
5584   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5585   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5586
5587 (defun gnus-article-mime-edit-done (&optional arg)
5588   "Update the article MIME edits and exit."
5589   (interactive "P")
5590   (when (featurep 'font-lock)
5591     (setq font-lock-defaults nil)
5592     (font-lock-mode -1))
5593   (gnus-article-edit-done arg))
5594
5595 (defun gnus-article-mime-edit-exit ()
5596   "Exit the article MIME editing without updating."
5597   (interactive)
5598   (when (or (not (buffer-modified-p))
5599             (yes-or-no-p "Article modified; kill anyway? "))
5600     (when (featurep 'font-lock)
5601       (setq font-lock-defaults nil)
5602       (font-lock-mode -1))
5603     (when mime-edit-mode-flag
5604       (let (mime-edit-insert-user-agent-field)
5605         (mime-edit-exit))
5606       (message ""))
5607     (goto-char (point-min))
5608     (let (case-fold-search)
5609       (when (re-search-forward
5610              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5611         (replace-match "")))
5612     (let ((winconf gnus-prev-winconf))
5613       (insert (prog1
5614                   (buffer-substring-no-properties (point-min) (point-max))
5615                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5616                 (erase-buffer)))
5617       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5618       (set-buffer gnus-article-buffer)
5619       (gnus-article-prepare-display)
5620       (set-window-configuration winconf))))
5621
5622 ;;;
5623 ;;; Article highlights
5624 ;;;
5625
5626 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5627
5628 ;;; Internal Variables:
5629
5630 (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\\)\\)"
5631   "Regular expression that matches URLs."
5632   :group 'gnus-article-buttons
5633   :type 'regexp)
5634
5635 (defcustom gnus-button-alist
5636   `(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5637      0 t gnus-button-handle-news 3)
5638     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
5639      gnus-button-handle-news 2)
5640     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5641      1 t
5642      gnus-button-fetch-group 4)
5643     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5644     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5645      t gnus-button-message-id 3)
5646     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5647     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5648     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5649     ;; This is info
5650     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t
5651      gnus-button-handle-info 2)
5652     ;; This is how URLs _should_ be embedded in text...
5653     ("<URL: *\\([^<>]*\\)>" 1 t gnus-button-embedded-url 1)
5654     ;; Raw URLs.
5655     (,gnus-button-url-regexp 0 t browse-url 0))
5656   "*Alist of regexps matching buttons in article bodies.
5657
5658 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5659 REGEXP: is the string matching text around the button,
5660 BUTTON: is the number of the regexp grouping actually matching the button,
5661 FORM: is a lisp expression which must eval to true for the button to
5662 be added,
5663 CALLBACK: is the function to call when the user push this button, and each
5664 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5665
5666 CALLBACK can also be a variable, in that case the value of that
5667 variable it the real callback function."
5668   :group 'gnus-article-buttons
5669   :type '(repeat (list regexp
5670                        (integer :tag "Button")
5671                        (sexp :tag "Form")
5672                        (function :tag "Callback")
5673                        (repeat :tag "Par"
5674                                :inline t
5675                                (integer :tag "Regexp group")))))
5676
5677 (defcustom gnus-header-button-alist
5678   `(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
5679      0 t gnus-button-message-id 0)
5680     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5681     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5682      0 t gnus-button-mailto 0)
5683     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5684     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5685     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5686     ("^[^:]+:" "\\bmailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5687     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5688      gnus-button-message-id 3))
5689   "*Alist of headers and regexps to match buttons in article heads.
5690
5691 This alist is very similar to `gnus-button-alist', except that each
5692 alist has an additional HEADER element first in each entry:
5693
5694 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5695
5696 HEADER is a regexp to match a header.  For a fuller explanation, see
5697 `gnus-button-alist'."
5698   :group 'gnus-article-buttons
5699   :group 'gnus-article-headers
5700   :type '(repeat (list (regexp :tag "Header")
5701                        regexp
5702                        (integer :tag "Button")
5703                        (sexp :tag "Form")
5704                        (function :tag "Callback")
5705                        (repeat :tag "Par"
5706                                :inline t
5707                                (integer :tag "Regexp group")))))
5708
5709 (defvar gnus-button-regexp nil)
5710 (defvar gnus-button-marker-list nil)
5711 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5712
5713 (defvar gnus-button-last nil)
5714 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5715
5716 ;;; Commands:
5717
5718 (defun gnus-article-push-button (event)
5719   "Check text under the mouse pointer for a callback function.
5720 If the text under the mouse pointer has a `gnus-callback' property,
5721 call it with the value of the `gnus-data' text property."
5722   (interactive "e")
5723   (set-buffer (window-buffer (posn-window (event-start event))))
5724   (let* ((pos (posn-point (event-start event)))
5725          (data (get-text-property pos 'gnus-data))
5726          (fun (get-text-property pos 'gnus-callback)))
5727     (goto-char pos)
5728     (when fun
5729       (funcall fun data))))
5730
5731 (defun gnus-article-press-button ()
5732   "Check text at point for a callback function.
5733 If the text at point has a `gnus-callback' property,
5734 call it with the value of the `gnus-data' text property."
5735   (interactive)
5736   (let* ((data (get-text-property (point) 'gnus-data))
5737          (fun (get-text-property (point) 'gnus-callback)))
5738     (when fun
5739       (funcall fun data))))
5740
5741 (defun gnus-article-prev-button (n)
5742   "Move point to N buttons backward.
5743 If N is negative, move forward instead."
5744   (interactive "p")
5745   (gnus-article-next-button (- n)))
5746
5747 (defun gnus-article-next-button (n)
5748   "Move point to N buttons forward.
5749 If N is negative, move backward instead."
5750   (interactive "p")
5751   (let ((function (if (< n 0) 'previous-single-property-change
5752                     'next-single-property-change))
5753         (inhibit-point-motion-hooks t)
5754         (backward (< n 0))
5755         (limit (if (< n 0) (point-min) (point-max))))
5756     (setq n (abs n))
5757     (while (and (not (= limit (point)))
5758                 (> n 0))
5759       ;; Skip past the current button.
5760       (when (get-text-property (point) 'gnus-callback)
5761         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5762       ;; Go to the next (or previous) button.
5763       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5764       ;; Put point at the start of the button.
5765       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5766         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5767       ;; Skip past intangible buttons.
5768       (when (get-text-property (point) 'intangible)
5769         (incf n))
5770       (decf n))
5771     (unless (zerop n)
5772       (gnus-message 5 "No more buttons"))
5773     n))
5774
5775 (defun gnus-article-highlight (&optional force)
5776   "Highlight current article.
5777 This function calls `gnus-article-highlight-headers',
5778 `gnus-article-highlight-citation',
5779 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5780 do the highlighting.  See the documentation for those functions."
5781   (interactive (list 'force))
5782   (gnus-article-highlight-headers)
5783   (gnus-article-highlight-citation force)
5784   (gnus-article-highlight-signature)
5785   (gnus-article-add-buttons force)
5786   (gnus-article-add-buttons-to-head))
5787
5788 (defun gnus-article-highlight-some (&optional force)
5789   "Highlight current article.
5790 This function calls `gnus-article-highlight-headers',
5791 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5792 do the highlighting.  See the documentation for those functions."
5793   (interactive (list 'force))
5794   (gnus-article-highlight-headers)
5795   (gnus-article-highlight-signature)
5796   (gnus-article-add-buttons))
5797
5798 (defun gnus-article-highlight-headers ()
5799   "Highlight article headers as specified by `gnus-header-face-alist'."
5800   (interactive)
5801   (save-excursion
5802     (set-buffer gnus-article-buffer)
5803     (save-restriction
5804       (let ((alist gnus-header-face-alist)
5805             (buffer-read-only nil)
5806             (case-fold-search t)
5807             (inhibit-point-motion-hooks t)
5808             entry regexp header-face field-face from hpoints fpoints)
5809         (article-narrow-to-head)
5810         (while (setq entry (pop alist))
5811           (goto-char (point-min))
5812           (setq regexp (concat "^\\("
5813                                (if (string-equal "" (nth 0 entry))
5814                                    "[^\t ]"
5815                                  (nth 0 entry))
5816                                "\\)")
5817                 header-face (nth 1 entry)
5818                 field-face (nth 2 entry))
5819           (while (and (re-search-forward regexp nil t)
5820                       (not (eobp)))
5821             (beginning-of-line)
5822             (setq from (point))
5823             (unless (search-forward ":" nil t)
5824               (forward-char 1))
5825             (when (and header-face
5826                        (not (memq (point) hpoints)))
5827               (push (point) hpoints)
5828               (gnus-put-text-property from (point) 'face header-face))
5829             (when (and field-face
5830                        (not (memq (setq from (point)) fpoints)))
5831               (push from fpoints)
5832               (if (re-search-forward "^[^ \t]" nil t)
5833                   (forward-char -2)
5834                 (goto-char (point-max)))
5835               (gnus-put-text-property from (point) 'face field-face))))))))
5836
5837 (defun gnus-article-highlight-signature ()
5838   "Highlight the signature in an article.
5839 It does this by highlighting everything after
5840 `gnus-signature-separator' using `gnus-signature-face'."
5841   (interactive)
5842   (when gnus-signature-face
5843     (save-excursion
5844       (set-buffer gnus-article-buffer)
5845       (let ((buffer-read-only nil)
5846             (inhibit-point-motion-hooks t))
5847         (save-restriction
5848           (when (gnus-article-narrow-to-signature)
5849             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5850                               'face gnus-signature-face)))))))
5851
5852 (defun gnus-article-buttonize-signature ()
5853   "Add button to the signature."
5854   (interactive)
5855   (save-excursion
5856     (set-buffer gnus-article-buffer)
5857     (let ((buffer-read-only nil)
5858           (inhibit-point-motion-hooks t))
5859       (when (gnus-article-search-signature)
5860         (gnus-article-add-button (match-beginning 0) (match-end 0)
5861                                  'gnus-signature-toggle
5862                                  (set-marker (make-marker)
5863                                              (1+ (match-end 0))))))))
5864
5865 (defun gnus-button-in-region-p (b e prop)
5866   "Say whether PROP exists in the region."
5867   (text-property-not-all b e prop nil))
5868
5869 (defun gnus-article-add-buttons (&optional force)
5870   "Find external references in the article and make buttons of them.
5871 \"External references\" are things like Message-IDs and URLs, as
5872 specified by `gnus-button-alist'."
5873   (interactive (list 'force))
5874   (save-excursion
5875     (set-buffer gnus-article-buffer)
5876     (let ((buffer-read-only nil)
5877           (inhibit-point-motion-hooks t)
5878           (case-fold-search t)
5879           (alist gnus-button-alist)
5880           beg entry regexp)
5881       ;; Remove all old markers.
5882       (let (marker entry new-list)
5883         (while (setq marker (pop gnus-button-marker-list))
5884           (if (or (< marker (point-min)) (>= marker (point-max)))
5885               (push marker new-list)
5886             (goto-char marker)
5887             (when (setq entry (gnus-button-entry))
5888               (put-text-property (match-beginning (nth 1 entry))
5889                                  (match-end (nth 1 entry))
5890                                  'gnus-callback nil))
5891             (set-marker marker nil)))
5892         (setq gnus-button-marker-list new-list))
5893       ;; We skip the headers.
5894       (article-goto-body)
5895       (setq beg (point))
5896       (while (setq entry (pop alist))
5897         (setq regexp (car entry))
5898         (goto-char beg)
5899         (while (re-search-forward regexp nil t)
5900           (let* ((start (and entry (match-beginning (nth 1 entry))))
5901                  (end (and entry (match-end (nth 1 entry))))
5902                  (from (match-beginning 0)))
5903             (when (and (or (eq t (nth 2 entry))
5904                            (eval (nth 2 entry)))
5905                        (not (gnus-button-in-region-p
5906                              start end 'gnus-callback)))
5907               ;; That optional form returned non-nil, so we add the
5908               ;; button.
5909               (gnus-article-add-button
5910                start end 'gnus-button-push
5911                (car (push (set-marker (make-marker) from)
5912                           gnus-button-marker-list))))))))))
5913
5914 ;; Add buttons to the head of an article.
5915 (defun gnus-article-add-buttons-to-head ()
5916   "Add buttons to the head of the article."
5917   (interactive)
5918   (save-excursion
5919     (set-buffer gnus-article-buffer)
5920     (save-restriction
5921       (let ((buffer-read-only nil)
5922             (inhibit-point-motion-hooks t)
5923             (case-fold-search t)
5924             (alist gnus-header-button-alist)
5925             entry beg end)
5926         (article-narrow-to-head)
5927         (while alist
5928           ;; Each alist entry.
5929           (setq entry (car alist)
5930                 alist (cdr alist))
5931           (goto-char (point-min))
5932           (while (re-search-forward (car entry) nil t)
5933             ;; Each header matching the entry.
5934             (setq beg (match-beginning 0))
5935             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5936                                (match-beginning 0))
5937                           (point-max)))
5938             (goto-char beg)
5939             (while (re-search-forward (nth 1 entry) end t)
5940               ;; Each match within a header.
5941               (let* ((entry (cdr entry))
5942                      (start (match-beginning (nth 1 entry)))
5943                      (end (match-end (nth 1 entry)))
5944                      (form (nth 2 entry)))
5945                 (goto-char (match-end 0))
5946                 (when (eval form)
5947                   (gnus-article-add-button
5948                    start end (nth 3 entry)
5949                    (buffer-substring (match-beginning (nth 4 entry))
5950                                      (match-end (nth 4 entry)))))))
5951             (goto-char end)))))))
5952
5953 ;;; External functions:
5954
5955 (defun gnus-article-add-button (from to fun &optional data)
5956   "Create a button between FROM and TO with callback FUN and data DATA."
5957   (when gnus-article-button-face
5958     (gnus-overlay-put (gnus-make-overlay from to)
5959                       'face gnus-article-button-face))
5960   (gnus-add-text-properties
5961    from to
5962    (nconc (and gnus-article-mouse-face
5963                (list gnus-mouse-face-prop gnus-article-mouse-face))
5964           (list 'gnus-callback fun)
5965           (and data (list 'gnus-data data))))
5966   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5967                          ;; Quote `:button-keymap' for Mule 2.3
5968                          ;; but it won't work.
5969                          ':button-keymap gnus-widget-button-keymap))
5970
5971 ;;; Internal functions:
5972
5973 (defun gnus-article-set-globals ()
5974   (save-excursion
5975     (set-buffer gnus-summary-buffer)
5976     (gnus-set-global-variables)))
5977
5978 (defun gnus-signature-toggle (end)
5979   (save-excursion
5980     (set-buffer gnus-article-buffer)
5981     (let ((buffer-read-only nil)
5982           (inhibit-point-motion-hooks t)
5983           (limit (next-single-property-change end 'mime-view-entity
5984                                               nil (point-max))))
5985       (if (text-property-any end limit 'article-type 'signature)
5986           (progn
5987             (gnus-delete-wash-type 'signature)
5988             (gnus-remove-text-properties-when
5989              'article-type 'signature end limit
5990              (cons 'article-type (cons 'signature
5991                                        gnus-hidden-properties))))
5992         (gnus-add-wash-type 'signature)
5993         (gnus-add-text-properties-when
5994          'article-type nil end limit
5995          (cons 'article-type (cons 'signature
5996                                    gnus-hidden-properties)))))
5997     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
5998       (gnus-set-mode-line 'article))))
5999
6000 (defun gnus-button-entry ()
6001   ;; Return the first entry in `gnus-button-alist' matching this place.
6002   (let ((alist gnus-button-alist)
6003         (entry nil))
6004     (while alist
6005       (setq entry (pop alist))
6006       (if (looking-at (car entry))
6007           (setq alist nil)
6008         (setq entry nil)))
6009     entry))
6010
6011 (defun gnus-button-push (marker)
6012   ;; Push button starting at MARKER.
6013   (save-excursion
6014     (goto-char marker)
6015     (let* ((entry (gnus-button-entry))
6016            (inhibit-point-motion-hooks t)
6017            (fun (nth 3 entry))
6018            (args (mapcar (lambda (group)
6019                            (let ((string (match-string group)))
6020                              (gnus-set-text-properties
6021                               0 (length string) nil string)
6022                              string))
6023                          (nthcdr 4 entry))))
6024       (cond
6025        ((fboundp fun)
6026         (apply fun args))
6027        ((and (boundp fun)
6028              (fboundp (symbol-value fun)))
6029         (apply (symbol-value fun) args))
6030        (t
6031         (gnus-message 1 "You must define `%S' to use this button"
6032                       (cons fun args)))))))
6033
6034 (defun gnus-parse-news-url (url)
6035   (let (scheme server group message-id articles)
6036     (with-temp-buffer
6037       (insert url)
6038       (goto-char (point-min))
6039       (when (looking-at "\\([A-Za-z]+\\):")
6040         (setq scheme (match-string 1))
6041         (goto-char (match-end 0)))
6042       (when (looking-at "//\\([^/]+\\)/")
6043         (setq server (match-string 1))
6044         (goto-char (match-end 0)))
6045
6046       (cond
6047        ((looking-at "\\(.*@.*\\)")
6048         (setq message-id (match-string 1)))
6049        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6050         (setq group (match-string 1)
6051               articles (split-string (match-string 2) "-")))
6052        ((looking-at "\\([^/]+\\)/?")
6053         (setq group (match-string 1)))
6054        (t
6055         (error "Unknown news URL syntax"))))
6056     (list scheme server group message-id articles)))
6057
6058 (defun gnus-button-handle-news (url)
6059   "Fetch a news URL."
6060   (destructuring-bind (scheme server group message-id articles)
6061       (gnus-parse-news-url url)
6062     (cond
6063      (message-id
6064       (save-excursion
6065         (set-buffer gnus-summary-buffer)
6066         (if server
6067             (let ((gnus-refer-article-method (list (list 'nntp server))))
6068               (gnus-summary-refer-article message-id))
6069           (gnus-summary-refer-article message-id))))
6070      (group
6071       (gnus-button-fetch-group url)))))
6072
6073 (defun gnus-button-handle-info (url)
6074   "Fetch an info URL."
6075   (if (string-match
6076        "^\\([^:/]+\\)?/\\(.*\\)"
6077        url)
6078       (gnus-info-find-node
6079        (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
6080                        "Gnus")
6081                ")"
6082                (gnus-url-unhex-string (match-string 2 url))))
6083     (error "Can't parse %s" url)))
6084
6085 (defun gnus-button-message-id (message-id)
6086   "Fetch MESSAGE-ID."
6087   (save-excursion
6088     (set-buffer gnus-summary-buffer)
6089     (gnus-summary-refer-article message-id)))
6090
6091 (defun gnus-button-fetch-group (address)
6092   "Fetch GROUP specified by ADDRESS."
6093   (if (not (string-match "[:/]" address))
6094       ;; This is just a simple group url.
6095       (gnus-group-read-ephemeral-group address gnus-select-method)
6096     (if (not
6097          (string-match
6098           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
6099           address))
6100         (error "Can't parse %s" address)
6101       (gnus-group-read-ephemeral-group
6102        (match-string 4 address)
6103        `(nntp ,(match-string 1 address)
6104               (nntp-address ,(match-string 1 address))
6105               (nntp-port-number ,(if (match-end 3)
6106                                      (match-string 3 address)
6107                                    "nntp")))
6108        nil nil nil
6109        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
6110
6111 (defun gnus-url-parse-query-string (query &optional downcase)
6112   (let (retval pairs cur key val)
6113     (setq pairs (split-string query "&"))
6114     (while pairs
6115       (setq cur (car pairs)
6116             pairs (cdr pairs))
6117       (if (not (string-match "=" cur))
6118           nil                           ; Grace
6119         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
6120               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
6121         (if downcase
6122             (setq key (downcase key)))
6123         (setq cur (assoc key retval))
6124         (if cur
6125             (setcdr cur (cons val (cdr cur)))
6126           (setq retval (cons (list key val) retval)))))
6127     retval))
6128
6129 (defun gnus-url-mailto (url)
6130   ;; Send mail to someone
6131   (when (string-match "mailto:/*\\(.*\\)" url)
6132     (setq url (substring url (match-beginning 1) nil)))
6133   (let (to args subject func)
6134     (if (string-match (regexp-quote "?") url)
6135         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
6136               args (gnus-url-parse-query-string
6137                     (substring url (match-end 0) nil) t))
6138       (setq to (gnus-url-unhex-string url)))
6139     (setq args (cons (list "to" to) args)
6140           subject (cdr-safe (assoc "subject" args)))
6141     (gnus-msg-mail)
6142     (while args
6143       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
6144       (if (fboundp func)
6145           (funcall func)
6146         (message-position-on-field (caar args)))
6147       (insert (mapconcat 'identity (cdar args) ", "))
6148       (setq args (cdr args)))
6149     (if subject
6150         (message-goto-body)
6151       (message-goto-subject))))
6152
6153 (defun gnus-button-embedded-url (address)
6154   "Activate ADDRESS with `browse-url'."
6155   (browse-url (gnus-strip-whitespace address)))
6156
6157 ;;; Next/prev buttons in the article buffer.
6158
6159 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
6160 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
6161
6162 (defvar gnus-prev-page-map
6163   (let ((map (make-sparse-keymap)))
6164     (unless (>= emacs-major-version 21)
6165       ;; XEmacs doesn't care.
6166       (set-keymap-parent map gnus-article-mode-map))
6167     (define-key map gnus-mouse-2 'gnus-button-prev-page)
6168     (define-key map "\r" 'gnus-button-prev-page)
6169     map))
6170
6171 (defun gnus-insert-prev-page-button ()
6172   (let ((b (point))
6173         (buffer-read-only nil)
6174         (situation (get-text-property (point-min) 'mime-view-situation)))
6175     (gnus-eval-format
6176      gnus-prev-page-line-format nil
6177      `(,@(gnus-local-map-property gnus-prev-page-map)
6178          gnus-prev t
6179          gnus-callback gnus-article-button-prev-page
6180          article-type annotation
6181          mime-view-situation ,situation))
6182     (widget-convert-button
6183      'link b (point)
6184      :action 'gnus-button-prev-page
6185      :button-keymap gnus-prev-page-map)))
6186
6187 (defvar gnus-next-page-map
6188   (let ((map (make-sparse-keymap)))
6189     (unless (>= emacs-major-version 21)
6190       ;; XEmacs doesn't care.
6191       (set-keymap-parent map gnus-article-mode-map))
6192     (define-key map gnus-mouse-2 'gnus-button-next-page)
6193     (define-key map "\r" 'gnus-button-next-page)
6194     map))
6195
6196 (defun gnus-button-next-page (&optional args more-args)
6197   "Go to the next page."
6198   (interactive)
6199   (let ((win (selected-window)))
6200     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6201     (gnus-article-next-page)
6202     (select-window win)))
6203
6204 (defun gnus-button-prev-page (&optional args more-args)
6205   "Go to the prev page."
6206   (interactive)
6207   (let ((win (selected-window)))
6208     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6209     (gnus-article-prev-page)
6210     (select-window win)))
6211
6212 (defun gnus-insert-next-page-button ()
6213   (let ((b (point))
6214         (buffer-read-only nil)
6215         (situation (get-text-property (point-min) 'mime-view-situation)))
6216     (gnus-eval-format gnus-next-page-line-format nil
6217                       `(,@(gnus-local-map-property gnus-next-page-map)
6218                           gnus-next t
6219                           gnus-callback gnus-article-button-next-page
6220                           article-type annotation
6221                           mime-view-situation ,situation))
6222     (widget-convert-button
6223      'link b (point)
6224      :action 'gnus-button-next-page
6225      :button-keymap gnus-next-page-map)))
6226
6227 (defun gnus-article-button-next-page (arg)
6228   "Go to the next page."
6229   (interactive "P")
6230   (let ((win (selected-window)))
6231     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6232     (gnus-article-next-page)
6233     (select-window win)))
6234
6235 (defun gnus-article-button-prev-page (arg)
6236   "Go to the prev page."
6237   (interactive "P")
6238   (let ((win (selected-window)))
6239     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6240     (gnus-article-prev-page)
6241     (select-window win)))
6242
6243 (defvar gnus-decode-header-methods
6244   '(mail-decode-encoded-word-region)
6245   "List of methods used to decode headers.
6246
6247 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
6248 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
6249 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
6250 whose names match REGEXP.
6251
6252 For example:
6253 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
6254  mail-decode-encoded-word-region
6255  (\"chinese\" . rfc1843-decode-region))
6256 ")
6257
6258 (defvar gnus-decode-header-methods-cache nil)
6259
6260 (defun gnus-multi-decode-header (start end)
6261   "Apply the functions from `gnus-encoded-word-methods' that match."
6262   (unless (and gnus-decode-header-methods-cache
6263                (eq gnus-newsgroup-name
6264                    (car gnus-decode-header-methods-cache)))
6265     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
6266     (mapcar (lambda (x)
6267               (if (symbolp x)
6268                   (nconc gnus-decode-header-methods-cache (list x))
6269                 (if (and gnus-newsgroup-name
6270                          (string-match (car x) gnus-newsgroup-name))
6271                     (nconc gnus-decode-header-methods-cache
6272                            (list (cdr x))))))
6273             gnus-decode-header-methods))
6274   (let ((xlist gnus-decode-header-methods-cache))
6275     (pop xlist)
6276     (save-restriction
6277       (narrow-to-region start end)
6278       (while xlist
6279         (funcall (pop xlist) (point-min) (point-max))))))
6280
6281 ;;;
6282 ;;; Treatment top-level handling.
6283 ;;;
6284
6285 (defun gnus-treat-article (condition &optional part-number total-parts type)
6286   (let ((length (- (point-max) (point-min)))
6287         (alist gnus-treatment-function-alist)
6288         (article-goto-body-goes-to-point-min-p t)
6289         (treated-type
6290          (or (not type)
6291              (catch 'found
6292                (let ((list gnus-article-treat-types))
6293                  (while list
6294                    (when (string-match (pop list) type)
6295                      (throw 'found t)))))))
6296         (highlightp (gnus-visual-p 'article-highlight 'highlight))
6297         (entity (static-unless (featurep 'xemacs)
6298                   (when (eq 'head condition)
6299                     (get-text-property (point-min) 'mime-view-entity))))
6300         val elem buttonized)
6301     (gnus-run-hooks 'gnus-part-display-hook)
6302     (unless gnus-inhibit-treatment
6303       (while (setq elem (pop alist))
6304         (setq val
6305               (save-excursion
6306                 (if (gnus-buffer-live-p gnus-summary-buffer)
6307                     (set-buffer gnus-summary-buffer))
6308                 (symbol-value (car elem))))
6309         (when (and (or (consp val)
6310                        treated-type)
6311                    (gnus-treat-predicate val)
6312                    (or (not (get (car elem) 'highlight))
6313                        highlightp))
6314           (when (and (not buttonized)
6315                      (memq (car elem)
6316                            '(gnus-treat-hide-signature
6317                              gnus-treat-highlight-signature)))
6318             (gnus-article-buttonize-signature)
6319             (setq buttonized t))
6320           (save-restriction
6321             (funcall (cadr elem)))))
6322       ;; FSF Emacsen does not inherit the existing text properties
6323       ;; in the new text, so we should do it for `mime-view-entity'.
6324       (static-unless (featurep 'xemacs)
6325         (when entity
6326           (put-text-property (point-min) (point-max)
6327                              'mime-view-entity entity))))))
6328
6329 ;; Dynamic variables.
6330 (eval-when-compile
6331   (defvar part-number)
6332   (defvar total-parts)
6333   (defvar type)
6334   (defvar condition)
6335   (defvar length))
6336
6337 (defun gnus-treat-predicate (val)
6338   (cond
6339    ((null val)
6340     nil)
6341    ((and (listp val)
6342          (stringp (car val)))
6343     (apply 'gnus-or (mapcar `(lambda (s)
6344                                (string-match s ,(or gnus-newsgroup-name "")))
6345                             val)))
6346    ((listp val)
6347     (let ((pred (pop val)))
6348       (cond
6349        ((eq pred 'or)
6350         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
6351        ((eq pred 'and)
6352         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
6353        ((eq pred 'not)
6354         (not (gnus-treat-predicate (car val))))
6355        ((eq pred 'typep)
6356         (equal (car val) type))
6357        (t
6358         (error "%S is not a valid predicate" pred)))))
6359    ((eq val 'mime)
6360     gnus-show-mime)
6361    (condition
6362     (eq condition val))
6363    ((eq val t)
6364     t)
6365    ((eq val 'head)
6366     nil)
6367    ((eq val 'last)
6368     (eq part-number total-parts))
6369    ((numberp val)
6370     (< length val))
6371    (t
6372     (error "%S is not a valid value" val))))
6373
6374 (defun gnus-article-encrypt-body (protocol &optional n)
6375   "Encrypt the article body."
6376   (interactive
6377    (list
6378     (or gnus-article-encrypt-protocol
6379         (completing-read "Encrypt protocol: "
6380                          gnus-article-encrypt-protocol-alist
6381                          nil t))
6382     current-prefix-arg))
6383   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
6384     (unless func
6385       (error (format "Can't find the encrypt protocol %s" protocol)))
6386     (if (equal gnus-newsgroup-name "nndraft:drafts")
6387         (error "Can't encrypt the article in group nndraft:drafts"))
6388     (if (equal gnus-newsgroup-name "nndraft:queue")
6389         (error "Don't encrypt the article in group nndraft:queue"))
6390     (gnus-summary-iterate n
6391       (save-excursion
6392         (set-buffer gnus-summary-buffer)
6393         (let ((mail-parse-charset gnus-newsgroup-charset)
6394               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6395               (summary-buffer gnus-summary-buffer)
6396               references point)
6397           (gnus-set-global-variables)
6398           (when (gnus-group-read-only-p)
6399             (error "The current newsgroup does not support article encrypt"))
6400           (gnus-summary-show-article t)
6401           (setq references
6402                 (or (mail-header-references gnus-current-headers) ""))
6403           (set-buffer gnus-article-buffer)
6404           (let* ((buffer-read-only nil)
6405                  (headers
6406                   (mapcar (lambda (field)
6407                             (and (save-restriction
6408                                    (message-narrow-to-head)
6409                                    (goto-char (point-min))
6410                                    (search-forward field nil t))
6411                                  (prog2
6412                                      (message-narrow-to-field)
6413                                      (buffer-substring (point-min) (point-max))
6414                                    (delete-region (point-min) (point-max))
6415                                    (widen))))
6416                           '("Content-Type:" "Content-Transfer-Encoding:"
6417                             "Content-Disposition:"))))
6418             (message-narrow-to-head)
6419             (message-remove-header "MIME-Version")
6420             (goto-char (point-max))
6421             (setq point (point))
6422             (insert (apply 'concat headers))
6423             (widen)
6424             (narrow-to-region point (point-max))
6425             (let ((message-options message-options))
6426               (message-options-set 'message-sender user-mail-address)
6427               (message-options-set 'message-recipients user-mail-address)
6428               (message-options-set 'message-sign-encrypt 'not)
6429               (funcall func))
6430             (goto-char (point-min))
6431             (insert "MIME-Version: 1.0\n")
6432             (widen)
6433             (gnus-summary-edit-article-done
6434              references nil summary-buffer t))
6435           (when gnus-keep-backlog
6436             (gnus-backlog-remove-article
6437              (car gnus-article-current) (cdr gnus-article-current)))
6438           (save-excursion
6439             (when (get-buffer gnus-original-article-buffer)
6440               (set-buffer gnus-original-article-buffer)
6441               (setq gnus-original-article nil)))
6442           (when gnus-use-cache
6443             (gnus-cache-update-article
6444              (car gnus-article-current) (cdr gnus-article-current))))))))
6445
6446 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6447   "The following specs can be used:
6448 %t  The security MIME type
6449 %i  Additional info
6450 %d  Details
6451 %D  Details if button is pressed")
6452
6453 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6454   "The following specs can be used:
6455 %t  The security MIME type
6456 %i  Additional info
6457 %d  Details
6458 %D  Details if button is pressed")
6459
6460 (defvar gnus-mime-security-button-line-format-alist
6461   '((?t gnus-tmp-type ?s)
6462     (?i gnus-tmp-info ?s)
6463     (?d gnus-tmp-details ?s)
6464     (?D gnus-tmp-pressed-details ?s)))
6465
6466 (defvar gnus-mime-security-button-map
6467   (let ((map (make-sparse-keymap)))
6468     (unless (>= (string-to-number emacs-version) 21)
6469       (set-keymap-parent map gnus-article-mode-map))
6470     (define-key map gnus-mouse-2 'gnus-article-push-button)
6471     (define-key map "\r" 'gnus-article-press-button)
6472     map))
6473
6474 (defvar gnus-mime-security-details-buffer nil)
6475
6476 (defvar gnus-mime-security-button-pressed nil)
6477
6478 (defvar gnus-mime-security-show-details-inline t
6479   "If non-nil, show details in the article buffer.")
6480
6481 (defun gnus-mime-security-verify-or-decrypt (handle)
6482   (mm-remove-parts (cdr handle))
6483   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6484         point buffer-read-only)
6485     (if region
6486         (goto-char (car region)))
6487     (save-restriction
6488       (narrow-to-region (point) (point))
6489       (with-current-buffer (mm-handle-multipart-original-buffer handle)
6490         (let* ((mm-verify-option 'known)
6491                (mm-decrypt-option 'known)
6492                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6493           (unless (eq nparts (cdr handle))
6494             (mm-destroy-parts (cdr handle))
6495             (setcdr handle nparts))))
6496       (setq point (point))
6497       (gnus-mime-display-security handle)
6498       (goto-char (point-max)))
6499     (when region
6500       (delete-region (point) (cdr region))
6501       (set-marker (car region) nil)
6502       (set-marker (cdr region) nil))
6503     (goto-char point)))
6504
6505 (defun gnus-mime-security-show-details (handle)
6506   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6507     (if details
6508         (if gnus-mime-security-show-details-inline
6509             (let ((gnus-mime-security-button-pressed t)
6510                   (gnus-mime-security-button-line-format
6511                    (get-text-property (point) 'gnus-line-format))
6512                   buffer-read-only)
6513               (forward-char -1)
6514               (while (eq (get-text-property (point) 'gnus-line-format)
6515                          gnus-mime-security-button-line-format)
6516                 (forward-char -1))
6517               (forward-char)
6518               (save-restriction
6519                 (narrow-to-region (point) (point))
6520                 (gnus-insert-mime-security-button handle))
6521               (delete-region (point)
6522                              (or (text-property-not-all
6523                                   (point) (point-max)
6524                                   'gnus-line-format
6525                                   gnus-mime-security-button-line-format)
6526                                  (point-max))))
6527           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6528               (with-current-buffer gnus-mime-security-details-buffer
6529                 (erase-buffer)
6530                 t)
6531             (setq gnus-mime-security-details-buffer
6532                   (gnus-get-buffer-create "*MIME Security Details*")))
6533           (with-current-buffer gnus-mime-security-details-buffer
6534             (insert details)
6535             (goto-char (point-min)))
6536           (pop-to-buffer gnus-mime-security-details-buffer))
6537       (gnus-message 5 "No details."))))
6538
6539 (defun gnus-mime-security-press-button (handle)
6540   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6541       (gnus-mime-security-show-details handle)
6542     (gnus-mime-security-verify-or-decrypt handle)))
6543
6544 (defun gnus-insert-mime-security-button (handle &optional displayed)
6545   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6546          (gnus-tmp-type
6547           (concat
6548            (or (nth 2 (assoc protocol mm-verify-function-alist))
6549                (nth 2 (assoc protocol mm-decrypt-function-alist))
6550                "Unknown")
6551            (if (equal (car handle) "multipart/signed")
6552                " Signed" " Encrypted")
6553            " Part"))
6554          (gnus-tmp-info
6555           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6556               "Undecided"))
6557          (gnus-tmp-details
6558           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6559          gnus-tmp-pressed-details
6560          b e)
6561     (setq gnus-tmp-details
6562           (if gnus-tmp-details
6563               (concat "\n" gnus-tmp-details) ""))
6564     (setq gnus-tmp-pressed-details
6565           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6566     (unless (bolp)
6567       (insert "\n"))
6568     (setq b (point))
6569     (gnus-eval-format
6570      gnus-mime-security-button-line-format
6571      gnus-mime-security-button-line-format-alist
6572      `(,@(gnus-local-map-property gnus-mime-security-button-map)
6573          gnus-callback gnus-mime-security-press-button
6574          gnus-line-format ,gnus-mime-security-button-line-format
6575          article-type annotation
6576          gnus-data ,handle))
6577     (setq e (point))
6578     (widget-convert-button
6579      'link b e
6580      :mime-handle handle
6581      :action 'gnus-widget-press-button
6582      :button-keymap gnus-mime-security-button-map
6583      :help-echo
6584      (lambda (widget/window &optional overlay pos)
6585        ;; Needed to properly clear the message due to a bug in
6586        ;; wid-edit (XEmacs only).
6587        (if (boundp 'help-echo-owns-message)
6588            (setq help-echo-owns-message t))
6589        (format
6590         "%S: show detail"
6591         (aref gnus-mouse-2 0))))))
6592
6593 (defun gnus-mime-display-security (handle)
6594   (save-restriction
6595     (narrow-to-region (point) (point))
6596     (unless (gnus-unbuttonized-mime-type-p (car handle))
6597       (gnus-insert-mime-security-button handle))
6598     (gnus-mime-display-mixed (cdr handle))
6599     (unless (bolp)
6600       (insert "\n"))
6601     (unless (gnus-unbuttonized-mime-type-p (car handle))
6602       (let ((gnus-mime-security-button-line-format
6603              gnus-mime-security-button-end-line-format))
6604         (gnus-insert-mime-security-button handle)))
6605     (mm-set-handle-multipart-parameter
6606      handle 'gnus-region
6607      (cons (set-marker (make-marker) (point-min))
6608            (set-marker (make-marker) (point-max))))))
6609
6610
6611 ;;; @ for mime-view
6612 ;;;
6613
6614 (defun gnus-article-header-presentation-method (entity situation)
6615   (mime-insert-header entity)
6616   (article-decode-group-name))
6617
6618 (set-alist 'mime-header-presentation-method-alist
6619            'gnus-original-article-mode
6620            #'gnus-article-header-presentation-method)
6621
6622 (defun gnus-mime-preview-quitting-method ()
6623   (mime-preview-kill-buffer)
6624   (delete-other-windows)
6625   (gnus-article-show-summary)
6626   (gnus-summary-select-article gnus-show-all-headers t))
6627
6628 (set-alist 'mime-preview-quitting-method-alist
6629            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6630
6631 (set-alist 'mime-preview-following-method-alist
6632            'gnus-original-article-mode #'gnus-following-method)
6633
6634 (set-alist 'mime-preview-over-to-previous-method-alist
6635            'gnus-original-article-mode
6636            (lambda ()
6637              (if (> (point-min) 1)
6638                  (gnus-article-prev-page)
6639                (gnus-article-read-summary-keys
6640                 nil (gnus-character-to-event ?P)))))
6641
6642 (set-alist 'mime-preview-over-to-next-method-alist
6643            'gnus-original-article-mode'
6644            (lambda ()
6645              (if (< (point-max) (buffer-size))
6646                  (gnus-article-next-page)
6647                (gnus-article-read-summary-keys
6648                 nil (gnus-character-to-event ?N)))))
6649
6650
6651 ;;; @ end
6652 ;;;
6653
6654 (gnus-ems-redefine)
6655
6656 (provide 'gnus-art)
6657
6658 (run-hooks 'gnus-art-load-hook)
6659
6660 ;;; gnus-art.el ends here