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