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