Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'static))
33
34 (require 'path-util)
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'alist)
41 (require 'mime-view)
42 (require 'wid-edit)
43
44 ;; Avoid byte-compile warnings.
45 (eval-when-compile
46   (require 'mm-bodies)
47   (require 'mail-parse)
48   (require 'mm-decode)
49   (require 'mm-view)
50   (require 'mm-uu)
51   )
52
53 (autoload 'gnus-msg-mail "gnus-msg" nil t)
54 (autoload 'gnus-button-mailto "gnus-msg")
55 (autoload 'gnus-button-reply "gnus-msg" nil t)
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)The Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
120     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
121     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
122     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
123     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
124     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face"
125     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
126     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
127     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
128     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
129     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
130     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
131     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
132     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
133     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
134     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
135     "^MBOX-Line" "^Priority:" "^X400-[-A-Za-z]+:"
136     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
137     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
138     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
139     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
140     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
141     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
142     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
143     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
144     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
145     "^X-Received:" "^Content-length:" "X-precedence:"
146     "^X-Authenticated-User:" "^X-Comment" "^X-Report:" "^X-Abuse-Info:"
147     "^X-HTTP-Proxy:" "^X-Mydeja-Info:" "^X-Copyright" "^X-No-Markup:"
148     "^X-Abuse-Info:" "^X-From_:" "^X-Accept-Language:" "^Errors-To:"
149     "^X-BeenThere:" "^X-Mailman-Version:" "^List-Help:" "^List-Post:"
150     "^List-Subscribe:" "^List-Id:" "^List-Unsubscribe:" "^List-Archive:"
151      "^X-Content-length:" "^X-Posting-Agent:" "^Original-Received:"
152      "^X-Request-PGP:" "^X-Fingerprint:" "^X-WRIEnvto:" "^X-WRIEnvfrom:"
153      "^X-Virus-Scanned:" "^X-Delivery-Agent:" "^Posted-Date:" "^X-Gateway:"
154      "^X-Local-Origin:" "^X-Local-Destination:" "^X-UserInfo1:"
155      "^X-Received-Date:")
156   "*All headers that start with this regexp will be hidden.
157 This variable can also be a list of regexps of headers to be ignored.
158 If `gnus-visible-headers' is non-nil, this variable will be ignored."
159   :type '(choice :custom-show nil
160                  regexp
161                  (repeat regexp))
162   :group 'gnus-article-hiding)
163
164 (defcustom gnus-visible-headers
165   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
166   "*All headers that do not match this regexp will be hidden.
167 This variable can also be a list of regexp of headers to remain visible.
168 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
169   :type '(repeat :value-to-internal (lambda (widget value)
170                                       (custom-split-regexp-maybe value))
171                  :match (lambda (widget value)
172                           (or (stringp value)
173                               (widget-editable-list-match widget value)))
174                  regexp)
175   :group 'gnus-article-hiding)
176
177 (defcustom gnus-sorted-header-list
178   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
179     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
180   "*This variable is a list of regular expressions.
181 If it is non-nil, headers that match the regular expressions will
182 be placed first in the article buffer in the sequence specified by
183 this list."
184   :type '(repeat regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
188   "Headers that are only to be displayed if they have interesting data.
189 Possible values in this list are:
190
191   'empty       Headers with no content.
192   'newsgroups  Newsgroup identical to Gnus group.
193   'to-address  To identical to To-address.
194   'followup-to Followup-to identical to Newsgroups.
195   'reply-to    Reply-to identical to From.
196   'date        Date less than four days old.
197   'long-to     To and/or Cc longer than 1024 characters.
198   'many-to     Multiple To and/or Cc."
199   :type '(set (const :tag "Headers with no content." empty)
200               (const :tag "Newsgroups identical to Gnus group." newsgroups)
201               (const :tag "To identical to To-address." to-address)
202               (const :tag "Followup-to identical to Newsgroups." followup-to)
203               (const :tag "Reply-to identical to From." reply-to)
204               (const :tag "Date less than four days old." date)
205               (const :tag "To and/or Cc longer than 1024 characters." long-to)
206               (const :tag "Multiple To and/or Cc headers." many-to))
207   :group 'gnus-article-hiding)
208
209 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
210   "Regexp matching signature separator.
211 This can also be a list of regexps.  In that case, it will be checked
212 from head to tail looking for a separator.  Searches will be done from
213 the end of the buffer."
214   :type '(repeat string)
215   :group 'gnus-article-signature)
216
217 (defcustom gnus-signature-limit nil
218   "Provide a limit to what is considered a signature.
219 If it is a number, no signature may not be longer (in characters) than
220 that number.  If it is a floating point number, no signature may be
221 longer (in lines) than that number.  If it is a function, the function
222 will be called without any parameters, and if it returns nil, there is
223 no signature in the buffer.  If it is a string, it will be used as a
224 regexp.  If it matches, the text in question is not a signature."
225   :type '(choice (integer :value 200)
226                  (number :value 4.0)
227                  (function :value fun)
228                  (regexp :value ".*"))
229   :group 'gnus-article-signature)
230
231 (defcustom gnus-hidden-properties '(invisible t intangible t)
232   "Property list to use for hiding text."
233   :type 'sexp
234   :group 'gnus-article-hiding)
235
236 ;; Fixme: This isn't the right thing for mixed graphical and and
237 ;; non-graphical frames in a session.
238 (defcustom gnus-article-x-face-command
239   (cond
240    ((featurep 'xemacs)
241     (if (or (gnus-image-type-available-p 'xface)
242             (gnus-image-type-available-p 'pbm))
243         'gnus-display-x-face-in-from
244       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -"))
245    ((and (fboundp 'image-type-available-p)
246          (module-installed-p 'x-face-e21))
247     'x-face-decode-message-header)
248    ((gnus-image-type-available-p 'pbm)
249     'gnus-display-x-face-in-from)
250    ((and window-system
251          (module-installed-p 'x-face-mule))
252     'x-face-mule-gnus-article-display-x-face)
253    (t
254     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
255 display -"))
256   "*String or function to be executed to display an X-Face header.
257 If it is a string, the command will be executed in a sub-shell
258 asynchronously.  The compressed face will be piped to this command."
259   :type `(choice
260           ,@(let (x-face-e21 x-face-mule)
261               (if (featurep 'xemacs)
262                   nil
263                 (setq x-face-e21 (module-installed-p 'x-face-e21)
264                       x-face-mule (module-installed-p 'x-face-mule)))
265               (delq nil
266                     (list
267                      'string
268                      (if (or (gnus-image-type-available-p 'xface)
269                              (gnus-image-type-available-p 'pbm))
270                          '(function-item gnus-display-x-face-in-from))
271                      (if (and x-face-e21
272                               (fboundp 'image-type-available-p))
273                          '(function-item
274                            :tag "x-face-decode-message-header (x-face-e21)"
275                            x-face-decode-message-header))
276                      (if x-face-mule
277                          '(function-item
278                            x-face-mule-gnus-article-display-x-face))
279                      'function))))
280   ;;:version "21.1"
281   :group 'gnus-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     (when gnus-display-mime-function
2302       (funcall gnus-display-mime-function))))
2303
2304
2305 (defun article-wash-html (&optional read-charset)
2306   "Format an html article.
2307 If READ-CHARSET, ask for a coding system."
2308   (interactive "P")
2309   (save-excursion
2310     (let ((buffer-read-only nil)
2311           charset)
2312       (if (gnus-buffer-live-p gnus-original-article-buffer)
2313           (with-current-buffer gnus-original-article-buffer
2314             (let* ((ct (gnus-fetch-field "content-type"))
2315                    (ctl (and ct
2316                              (ignore-errors
2317                                (mail-header-parse-content-type ct)))))
2318               (setq charset (and ctl
2319                                  (mail-content-type-get ctl 'charset)))
2320               (if (stringp charset)
2321                   (setq charset (intern (downcase charset)))))))
2322       (if read-charset
2323           (setq charset (mm-read-coding-system "Charset: " charset)))
2324       (unless charset
2325         (setq charset gnus-newsgroup-charset))
2326       (article-goto-body)
2327       (save-window-excursion
2328         (save-restriction
2329           (narrow-to-region (point) (point-max))
2330           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2331                  (entry (assq func mm-text-html-washer-alist)))
2332             (if entry
2333                 (setq func (cdr entry)))
2334             (cond
2335              ((gnus-functionp func)
2336               (funcall func))
2337              (t
2338               (apply (car func) (cdr func))))))))))
2339
2340 (defun gnus-article-wash-html-with-w3 ()
2341   "Wash the current buffer with w3."
2342   (mm-setup-w3)
2343   (let ((w3-strict-width (window-width))
2344         (url-standalone-mode t)
2345         (url-gateway-unplugged t)
2346         (w3-honor-stylesheets nil))
2347     (condition-case ()
2348         (w3-region (point-min) (point-max))
2349       (error))))
2350
2351 (defun gnus-article-wash-html-with-w3m ()
2352   "Wash the current buffer with emacs-w3m."
2353   (mm-setup-w3m)
2354   (save-restriction
2355     (narrow-to-region (point) (point-max))
2356     (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
2357                                    nil
2358                                  "\\`cid:"))
2359           (w3m-display-inline-images mm-inline-text-html-with-images)
2360           w3m-force-redisplay)
2361       (w3m-region (point-min) (point-max)))
2362     (when mm-inline-text-html-with-w3m-keymap
2363       (add-text-properties
2364        (point-min) (point-max)
2365        (append '(mm-inline-text-html-with-w3m t)
2366                (gnus-local-map-property mm-w3m-mode-map))))))
2367
2368 (defun article-hide-list-identifiers ()
2369   "Remove list identifies from the Subject header.
2370 The `gnus-list-identifiers' variable specifies what to do."
2371   (interactive)
2372   (let ((inhibit-point-motion-hooks t)
2373         (regexp (if (consp gnus-list-identifiers)
2374                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2375                   gnus-list-identifiers))
2376         buffer-read-only)
2377     (when regexp
2378       (save-excursion
2379         (save-restriction
2380           (article-narrow-to-head)
2381           (goto-char (point-min))
2382           (while (re-search-forward
2383                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2384                   nil t)
2385             (delete-region (match-beginning 2) (match-end 0))
2386             (beginning-of-line))
2387           (when (re-search-forward
2388                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2389             (delete-region (match-beginning 1) (match-end 1))))))))
2390
2391 (defun article-hide-pgp ()
2392   "Remove any PGP headers and signatures in the current article."
2393   (interactive)
2394   (save-excursion
2395     (save-restriction
2396       (let ((inhibit-point-motion-hooks t)
2397             buffer-read-only beg end)
2398         (article-goto-body)
2399         ;; Hide the "header".
2400         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
2401           (gnus-add-wash-type 'pgp)
2402           (delete-region (match-beginning 0) (match-end 0))
2403           ;; Remove armor headers (rfc2440 6.2)
2404           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
2405                                      (point)))
2406           (setq beg (point))
2407           ;; Hide the actual signature.
2408           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
2409                (setq end (1+ (match-beginning 0)))
2410                (delete-region
2411                 end
2412                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
2413                     (match-end 0)
2414                   ;; Perhaps we shouldn't hide to the end of the buffer
2415                   ;; if there is no end to the signature?
2416                   (point-max))))
2417           ;; Hide "- " PGP quotation markers.
2418           (when (and beg end)
2419             (narrow-to-region beg end)
2420             (goto-char (point-min))
2421             (while (re-search-forward "^- " nil t)
2422               (delete-region
2423                (match-beginning 0) (match-end 0)))
2424             (widen))
2425           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
2426
2427 (defun article-hide-pem (&optional arg)
2428   "Toggle hiding of any PEM headers and signatures in the current article.
2429 If given a negative prefix, always show; if given a positive prefix,
2430 always hide."
2431   (interactive (gnus-article-hidden-arg))
2432   (unless (gnus-article-check-hidden-text 'pem arg)
2433     (save-excursion
2434       (let (buffer-read-only end)
2435         (goto-char (point-min))
2436         ;; Hide the horrendously ugly "header".
2437         (when (and (search-forward
2438                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2439                     nil t)
2440                    (setq end (1+ (match-beginning 0))))
2441           (gnus-add-wash-type 'pem)
2442           (gnus-article-hide-text-type
2443            end
2444            (if (search-forward "\n\n" nil t)
2445                (match-end 0)
2446              (point-max))
2447            'pem)
2448           ;; Hide the trailer as well
2449           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2450                                 nil t)
2451             (gnus-article-hide-text-type
2452              (match-beginning 0) (match-end 0) 'pem)))))))
2453
2454 (defun article-strip-banner ()
2455   "Strip the banner specified by the `banner' group parameter."
2456   (interactive)
2457   (save-excursion
2458     (save-restriction
2459       (let ((inhibit-point-motion-hooks t)
2460             (banner (gnus-parameter-banner gnus-newsgroup-name))
2461             (gnus-signature-limit nil)
2462             buffer-read-only beg end)
2463         (when banner
2464           (article-goto-body)
2465           (cond
2466            ((eq banner 'signature)
2467             (when (gnus-article-narrow-to-signature)
2468               (widen)
2469               (forward-line -1)
2470               (delete-region (point) (point-max))))
2471            ((symbolp banner)
2472             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2473                 (while (re-search-forward banner nil t)
2474                   (delete-region (match-beginning 0) (match-end 0)))))
2475            ((stringp banner)
2476             (while (re-search-forward banner nil t)
2477               (delete-region (match-beginning 0) (match-end 0))))))))))
2478
2479 (defun article-babel ()
2480   "Translate article using an online translation service."
2481   (interactive)
2482   (require 'babel)
2483   (save-excursion
2484     (set-buffer gnus-article-buffer)
2485     (when (article-goto-body)
2486       (let* ((buffer-read-only nil)
2487              (start (point))
2488              (end (point-max))
2489              (orig (buffer-substring start end))
2490              (trans (babel-as-string orig)))
2491         (save-restriction
2492           (narrow-to-region start end)
2493           (delete-region start end)
2494           (insert trans))))))
2495
2496 (defun article-hide-signature (&optional arg)
2497   "Hide the signature in the current article.
2498 If given a negative prefix, always show; if given a positive prefix,
2499 always hide."
2500   (interactive (gnus-article-hidden-arg))
2501   (save-excursion
2502     (save-restriction
2503       (if (interactive-p)
2504           (progn
2505             (widen)
2506             (article-goto-body))
2507         (goto-char (point-min)))
2508       (unless (gnus-article-check-hidden-text 'signature arg)
2509         (let ((buffer-read-only nil)
2510               (button (point)))
2511           (while (setq button (text-property-any button (point-max)
2512                                                  'gnus-callback
2513                                                  'gnus-signature-toggle))
2514             (setq button (text-property-not-all button (point-max)
2515                                                 'gnus-callback
2516                                                 'gnus-signature-toggle))
2517             (when (and button (not (eobp)))
2518               (gnus-article-hide-text-type
2519                (1+ button)
2520                (next-single-property-change (1+ button) 'mime-view-entity
2521                                             nil (point-max))
2522                'signature)))))))
2523   (gnus-set-mode-line 'article))
2524
2525 (defun article-strip-headers-in-body ()
2526   "Strip offensive headers from bodies."
2527   (interactive)
2528   (save-excursion
2529     (article-goto-body)
2530     (let ((case-fold-search t))
2531       (when (looking-at "x-no-archive:")
2532         (gnus-delete-line)))))
2533
2534 (defun article-strip-leading-blank-lines ()
2535   "Remove all blank lines from the beginning of the article."
2536   (interactive)
2537   (save-excursion
2538     (let ((inhibit-point-motion-hooks t)
2539           buffer-read-only)
2540       (when (article-goto-body)
2541         (while (and (not (eobp))
2542                     (looking-at "[ \t]*$"))
2543           (gnus-delete-line))))))
2544
2545 (defun article-narrow-to-head ()
2546   "Narrow the buffer to the head of the message.
2547 Point is left at the beginning of the narrowed-to region."
2548   (narrow-to-region
2549    (goto-char (point-min))
2550    (if (search-forward "\n\n" nil 1)
2551        (1- (point))
2552      (point-max)))
2553   (goto-char (point-min)))
2554
2555 (defun article-goto-body ()
2556   "Place point at the start of the body."
2557   (goto-char (point-min))
2558   (cond
2559    ;; This variable is only bound when dealing with separate
2560    ;; MIME body parts.
2561    (article-goto-body-goes-to-point-min-p
2562     t)
2563    ((search-forward "\n\n" nil t)
2564     t)
2565    (t
2566     (goto-char (point-max))
2567     nil)))
2568
2569 (defun article-strip-multiple-blank-lines ()
2570   "Replace consecutive blank lines with one empty line."
2571   (interactive)
2572   (save-excursion
2573     (let ((inhibit-point-motion-hooks t)
2574           buffer-read-only)
2575       ;; First make all blank lines empty.
2576       (article-goto-body)
2577       (while (re-search-forward "^[ \t]+$" nil t)
2578         (unless (gnus-annotation-in-region-p
2579                  (match-beginning 0) (match-end 0))
2580           (replace-match "" nil t)))
2581       ;; Then replace multiple empty lines with a single empty line.
2582       (article-goto-body)
2583       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2584         (unless (gnus-annotation-in-region-p
2585                  (match-beginning 0) (match-end 0))
2586           (delete-region (match-beginning 1) (match-end 1)))))))
2587
2588 (defun article-strip-leading-space ()
2589   "Remove all white space from the beginning of the lines in the article."
2590   (interactive)
2591   (save-excursion
2592     (let ((inhibit-point-motion-hooks t)
2593           buffer-read-only)
2594       (article-goto-body)
2595       (while (re-search-forward "^[ \t]+" nil t)
2596         (replace-match "" t t)))))
2597
2598 (defun article-strip-trailing-space ()
2599   "Remove all white space from the end of the lines in the article."
2600   (interactive)
2601   (save-excursion
2602     (let ((inhibit-point-motion-hooks t)
2603           buffer-read-only)
2604       (article-goto-body)
2605       (while (re-search-forward "[ \t]+$" nil t)
2606         (replace-match "" t t)))))
2607
2608 (defun article-strip-blank-lines ()
2609   "Strip leading, trailing and multiple blank lines."
2610   (interactive)
2611   (article-strip-leading-blank-lines)
2612   (article-remove-trailing-blank-lines)
2613   (article-strip-multiple-blank-lines))
2614
2615 (defun article-strip-all-blank-lines ()
2616   "Strip all blank lines."
2617   (interactive)
2618   (save-excursion
2619     (let ((inhibit-point-motion-hooks t)
2620           buffer-read-only)
2621       (article-goto-body)
2622       (while (re-search-forward "^[ \t]*\n" nil t)
2623         (replace-match "" t t)))))
2624
2625 (defun gnus-article-narrow-to-signature ()
2626   "Narrow to the signature; return t if a signature is found, else nil."
2627   (let ((inhibit-point-motion-hooks t))
2628     (when (gnus-article-search-signature)
2629       (forward-line 1)
2630       ;; Check whether we have some limits to what we consider
2631       ;; to be a signature.
2632       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2633                       (list gnus-signature-limit)))
2634             limit limited)
2635         (while (setq limit (pop limits))
2636           (if (or (and (integerp limit)
2637                        (< (- (point-max) (point)) limit))
2638                   (and (floatp limit)
2639                        (< (count-lines (point) (point-max)) limit))
2640                   (and (gnus-functionp limit)
2641                        (funcall limit))
2642                   (and (stringp limit)
2643                        (not (re-search-forward limit nil t))))
2644               ()                        ; This limit did not succeed.
2645             (setq limited t
2646                   limits nil)))
2647         (unless limited
2648           (narrow-to-region (point) (point-max))
2649           t)))))
2650
2651 (defun gnus-article-search-signature ()
2652   "Search the current buffer for the signature separator.
2653 Put point at the beginning of the signature separator."
2654   (let ((cur (point)))
2655     (goto-char (point-max))
2656     (if (if (stringp gnus-signature-separator)
2657             (re-search-backward gnus-signature-separator nil t)
2658           (let ((seps gnus-signature-separator))
2659             (while (and seps
2660                         (not (re-search-backward (car seps) nil t)))
2661               (pop seps))
2662             seps))
2663         t
2664       (goto-char cur)
2665       nil)))
2666
2667 (defun gnus-article-hidden-arg ()
2668   "Return the current prefix arg as a number, or 0 if no prefix."
2669   (list (if current-prefix-arg
2670             (prefix-numeric-value current-prefix-arg)
2671           0)))
2672
2673 (defun gnus-article-check-hidden-text (type arg)
2674   "Return nil if hiding is necessary.
2675 Arg can be nil or a number.  nil and positive means hide, negative
2676 means show, 0 means toggle."
2677   (save-excursion
2678     (save-restriction
2679       (let ((hide (gnus-article-hidden-text-p type)))
2680         (cond
2681          ((or (null arg)
2682               (> arg 0))
2683           nil)
2684          ((< arg 0)
2685           (gnus-article-show-hidden-text type)
2686           t)
2687          (t
2688           (if (eq hide 'hidden)
2689               (progn
2690                 (gnus-article-show-hidden-text type)
2691                 t)
2692             nil)))))))
2693
2694 (defun gnus-article-hidden-text-p (type)
2695   "Say whether the current buffer contains hidden text of type TYPE."
2696   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2697     (while (and pos
2698                 (not (get-text-property pos 'invisible))
2699                 (not (get-text-property pos 'dummy-invisible)))
2700       (setq pos
2701             (text-property-any (1+ pos) (point-max) 'article-type type)))
2702     (if pos
2703         'hidden
2704       nil)))
2705
2706 (defun gnus-article-show-hidden-text (type &optional dummy)
2707   "Show all hidden text of type TYPE.
2708 Originally it is hide instead of DUMMY."
2709   (let ((buffer-read-only nil)
2710         (inhibit-point-motion-hooks t))
2711     (gnus-remove-text-properties-when
2712      'article-type type
2713      (point-min) (point-max)
2714      (cons 'article-type (cons type
2715                                gnus-hidden-properties)))
2716     (gnus-delete-wash-type type)))
2717
2718 (defconst article-time-units
2719   `((year . ,(* 365.25 24 60 60))
2720     (week . ,(* 7 24 60 60))
2721     (day . ,(* 24 60 60))
2722     (hour . ,(* 60 60))
2723     (minute . 60)
2724     (second . 1))
2725   "Mapping from time units to seconds.")
2726
2727 (defun article-date-ut (&optional type highlight header)
2728   "Convert DATE date to universal time in the current article.
2729 If TYPE is `local', convert to local time; if it is `lapsed', output
2730 how much time has lapsed since DATE.  For `lapsed', the value of
2731 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2732 should replace the \"Date:\" one, or should be added below it."
2733   (interactive (list 'ut t))
2734   (let* ((header (or header
2735                      (and (eq 1 (point-min))
2736                           (mail-header-date (save-excursion
2737                                               (set-buffer gnus-summary-buffer)
2738                                               gnus-current-headers)))
2739                      (message-fetch-field "date")
2740                      ""))
2741          (date (if (vectorp header) (mail-header-date header)
2742                  header))
2743          (inhibit-point-motion-hooks t)
2744          bface eface date-pos)
2745     (when (and date (not (string= date "")))
2746       (save-excursion
2747         (save-restriction
2748           (article-narrow-to-head)
2749           (when (or (and (eq type 'lapsed)
2750                          gnus-article-date-lapsed-new-header
2751                          ;; Attempt to get the face of X-Sent first.
2752                          (re-search-forward "^X-Sent:[ \t]" nil t))
2753                     (re-search-forward "^Date:[ \t]" nil t)
2754                     ;; If Date is missing, try again for X-Sent.
2755                     (re-search-forward "^X-Sent:[ \t]" nil t))
2756             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2757                   date (or (get-text-property (gnus-point-at-bol)
2758                                               'original-date)
2759                            date)
2760                   eface (get-text-property (1- (gnus-point-at-eol))
2761                                            'face)))
2762           (let ((buffer-read-only nil))
2763             ;; Delete any old X-Sent headers.
2764             (when (setq date-pos
2765                         (text-property-any (point-min) (point-max)
2766                                            'article-date-lapsed t))
2767               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2768               (delete-region (match-beginning 0)
2769                              (progn (forward-line 1) (point))))
2770             (goto-char (point-min))
2771             ;; Delete any old Date headers.
2772             (while (re-search-forward "^Date:[ \t]" nil t)
2773               (unless date-pos
2774                 (setq date-pos (match-beginning 0)))
2775               (unless (and (eq type 'lapsed)
2776                            gnus-article-date-lapsed-new-header)
2777                 (delete-region (match-beginning 0)
2778                                (progn (message-next-header) (point)))))
2779             (if date-pos
2780                 (progn
2781                   (goto-char date-pos)
2782                   (unless (bolp)
2783                     ;; Possibly, Date has been deleted.
2784                     (insert "\n"))
2785                   (when (and (eq type 'lapsed)
2786                              gnus-article-date-lapsed-new-header
2787                              (looking-at "Date:"))
2788                     (forward-line 1)))
2789               (goto-char (point-min)))
2790             (insert (article-make-date-line date type))
2791             (when (eq type 'lapsed)
2792               (put-text-property (gnus-point-at-bol) (point)
2793                                  'article-date-lapsed t))
2794             (insert "\n")
2795             (forward-line -1)
2796             ;; Do highlighting.
2797             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2798               (put-text-property (match-beginning 1) (1+ (match-end 1))
2799                                  'original-date date)
2800               (put-text-property (match-beginning 1) (1+ (match-end 1))
2801                                  'face bface)
2802               (put-text-property (match-beginning 2) (match-end 2)
2803                                  'face eface))))))))
2804
2805 (defun article-make-date-line (date type)
2806   "Return a DATE line of TYPE."
2807   (unless (memq type '(local ut original user iso8601 lapsed english))
2808     (error "Unknown conversion type: %s" type))
2809   (condition-case ()
2810       (let ((time (date-to-time date)))
2811         (cond
2812          ;; Convert to the local timezone.
2813          ((eq type 'local)
2814           (let ((tz (car (current-time-zone time))))
2815             (format "Date: %s %s%02d%02d" (current-time-string time)
2816                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2817                     (/ (% (abs tz) 3600) 60))))
2818          ;; Convert to Universal Time.
2819          ((eq type 'ut)
2820           (concat "Date: "
2821                   (current-time-string
2822                    (let* ((e (parse-time-string date))
2823                           (tm (apply 'encode-time e))
2824                           (ms (car tm))
2825                           (ls (- (cadr tm) (car (current-time-zone time)))))
2826                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2827                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2828                            (t (list ms ls)))))
2829                   " UT"))
2830          ;; Get the original date from the article.
2831          ((eq type 'original)
2832           (concat "Date: " (if (string-match "\n+$" date)
2833                                (substring date 0 (match-beginning 0))
2834                              date)))
2835          ;; Let the user define the format.
2836          ((eq type 'user)
2837           (if (gnus-functionp gnus-article-time-format)
2838               (funcall gnus-article-time-format time)
2839             (concat
2840              "Date: "
2841              (format-time-string gnus-article-time-format time))))
2842          ;; ISO 8601.
2843          ((eq type 'iso8601)
2844           (let ((tz (car (current-time-zone time))))
2845             (concat
2846              "Date: "
2847              (format-time-string "%Y%m%dT%H%M%S" time)
2848              (format "%s%02d%02d"
2849                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2850                      (/ (% (abs tz) 3600) 60)))))
2851          ;; Do an X-Sent lapsed format.
2852          ((eq type 'lapsed)
2853           ;; If the date is seriously mangled, the timezone functions are
2854           ;; liable to bug out, so we ignore all errors.
2855           (let* ((now (current-time))
2856                  (real-time (subtract-time now time))
2857                  (real-sec (and real-time
2858                                 (+ (* (float (car real-time)) 65536)
2859                                    (cadr real-time))))
2860                  (sec (and real-time (abs real-sec)))
2861                  num prev)
2862             (cond
2863              ((null real-time)
2864               "X-Sent: Unknown")
2865              ((zerop sec)
2866               "X-Sent: Now")
2867              (t
2868               (concat
2869                "X-Sent: "
2870                ;; This is a bit convoluted, but basically we go
2871                ;; through the time units for years, weeks, etc,
2872                ;; and divide things to see whether that results
2873                ;; in positive answers.
2874                (mapconcat
2875                 (lambda (unit)
2876                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2877                       ;; The (remaining) seconds are too few to
2878                       ;; be divided into this time unit.
2879                       ""
2880                     ;; It's big enough, so we output it.
2881                     (setq sec (- sec (* num (cdr unit))))
2882                     (prog1
2883                         (concat (if prev ", " "") (int-to-string
2884                                                    (floor num))
2885                                 " " (symbol-name (car unit))
2886                                 (if (> num 1) "s" ""))
2887                       (setq prev t))))
2888                 article-time-units "")
2889                ;; If dates are odd, then it might appear like the
2890                ;; article was sent in the future.
2891                (if (> real-sec 0)
2892                    " ago"
2893                  " in the future"))))))
2894          ;; Display the date in proper English
2895          ((eq type 'english)
2896           (let ((dtime (decode-time time)))
2897             (concat
2898              "Date: the "
2899              (number-to-string (nth 3 dtime))
2900              (let ((digit (% (nth 3 dtime) 10)))
2901                (cond
2902                 ((memq (nth 3 dtime) '(11 12 13)) "th")
2903                 ((= digit 1) "st")
2904                 ((= digit 2) "nd")
2905                 ((= digit 3) "rd")
2906                 (t "th")))
2907              " of "
2908              (nth (1- (nth 4 dtime)) gnus-english-month-names)
2909              " "
2910              (number-to-string (nth 5 dtime))
2911              " at "
2912              (format "%02d" (nth 2 dtime))
2913              ":"
2914              (format "%02d" (nth 1 dtime)))))))
2915     (error
2916      (format "Date: %s (from Oort)" date))))
2917
2918 (defun article-date-local (&optional highlight)
2919   "Convert the current article date to the local timezone."
2920   (interactive (list t))
2921   (article-date-ut 'local highlight))
2922
2923 (defun article-date-english (&optional highlight)
2924   "Convert the current article date to something that is proper English."
2925   (interactive (list t))
2926   (article-date-ut 'english highlight))
2927
2928 (defun article-date-original (&optional highlight)
2929   "Convert the current article date to what it was originally.
2930 This is only useful if you have used some other date conversion
2931 function and want to see what the date was before converting."
2932   (interactive (list t))
2933   (article-date-ut 'original highlight))
2934
2935 (defun article-date-lapsed (&optional highlight)
2936   "Convert the current article date to time lapsed since it was sent."
2937   (interactive (list t))
2938   (article-date-ut 'lapsed highlight))
2939
2940 (defun article-update-date-lapsed ()
2941   "Function to be run from a timer to update the lapsed time line."
2942   (let (deactivate-mark)
2943     (save-excursion
2944       (ignore-errors
2945         (walk-windows
2946          (lambda (w)
2947            (set-buffer (window-buffer w))
2948            (when (eq major-mode 'gnus-article-mode)
2949              (goto-char (point-min))
2950              (when (re-search-forward "^X-Sent:" nil t)
2951                (article-date-lapsed t))))
2952          nil 'visible)))))
2953
2954 (defun gnus-start-date-timer (&optional n)
2955   "Start a timer to update the X-Sent header in the article buffers.
2956 The numerical prefix says how frequently (in seconds) the function
2957 is to run."
2958   (interactive "p")
2959   (unless n
2960     (setq n 1))
2961   (gnus-stop-date-timer)
2962   (setq article-lapsed-timer
2963         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2964
2965 (defun gnus-stop-date-timer ()
2966   "Stop the X-Sent timer."
2967   (interactive)
2968   (when article-lapsed-timer
2969     (nnheader-cancel-timer article-lapsed-timer)
2970     (setq article-lapsed-timer nil)))
2971
2972 (defun article-date-user (&optional highlight)
2973   "Convert the current article date to the user-defined format.
2974 This format is defined by the `gnus-article-time-format' variable."
2975   (interactive (list t))
2976   (article-date-ut 'user highlight))
2977
2978 (defun article-date-iso8601 (&optional highlight)
2979   "Convert the current article date to ISO8601."
2980   (interactive (list t))
2981   (article-date-ut 'iso8601 highlight))
2982
2983 ;; (defun article-show-all ()
2984 ;;   "Show all hidden text in the article buffer."
2985 ;;   (interactive)
2986 ;;   (save-excursion
2987 ;;     (widen)
2988 ;;     (let ((buffer-read-only nil))
2989 ;;       (gnus-article-unhide-text (point-min) (point-max))
2990 ;;       (gnus-remove-text-with-property 'gnus-prev)
2991 ;;       (gnus-remove-text-with-property 'gnus-next))))
2992
2993 (defun article-show-all-headers ()
2994   "Show all hidden headers in the article buffer."
2995   (interactive)
2996   (save-excursion
2997     (save-restriction
2998       (widen)
2999       (article-narrow-to-head)
3000       (let ((buffer-read-only nil))
3001         (gnus-article-unhide-text (point-min) (point-max))))))
3002
3003 (defun article-remove-leading-whitespace ()
3004   "Remove excessive whitespace from all headers."
3005   (interactive)
3006   (save-excursion
3007     (save-restriction
3008       (let ((buffer-read-only nil))
3009         (article-narrow-to-head)
3010         (goto-char (point-min))
3011         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3012           (delete-region (match-beginning 1) (match-end 1)))))))
3013
3014 (defun article-emphasize (&optional arg)
3015   "Emphasize text according to `gnus-emphasis-alist'."
3016   (interactive (gnus-article-hidden-arg))
3017   (unless (gnus-article-check-hidden-text 'emphasis arg)
3018     (save-excursion
3019       (let ((alist (or
3020                     (condition-case nil
3021                         (with-current-buffer gnus-summary-buffer
3022                           gnus-article-emphasis-alist)
3023                       (error))
3024                     gnus-emphasis-alist))
3025             (buffer-read-only nil)
3026             (props (append '(article-type emphasis)
3027                            gnus-hidden-properties))
3028             regexp elem beg invisible visible face)
3029         (article-goto-body)
3030         (setq beg (point))
3031         (while (setq elem (pop alist))
3032           (goto-char beg)
3033           (setq regexp (car elem)
3034                 invisible (nth 1 elem)
3035                 visible (nth 2 elem)
3036                 face (nth 3 elem))
3037           (while (re-search-forward regexp nil t)
3038             (when (and (match-beginning visible) (match-beginning invisible))
3039               (gnus-article-hide-text
3040                (match-beginning invisible) (match-end invisible) props)
3041               (gnus-article-unhide-text-type
3042                (match-beginning visible) (match-end visible) 'emphasis)
3043               (gnus-put-overlay-excluding-newlines
3044                (match-beginning visible) (match-end visible) 'face face)
3045               (gnus-add-wash-type 'emphasis)
3046               (goto-char (match-end invisible)))))))))
3047
3048 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3049   "Setup newsgroup emphasis alist."
3050   (unless gnus-article-emphasis-alist
3051     (let ((name (and gnus-newsgroup-name
3052                      (gnus-group-real-name gnus-newsgroup-name))))
3053       (make-local-variable 'gnus-article-emphasis-alist)
3054       (setq gnus-article-emphasis-alist
3055             (nconc
3056              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3057                (while (setq elem (pop alist))
3058                  (when (and name (string-match (car elem) name))
3059                    (setq alist nil
3060                          highlight (copy-sequence (cdr elem)))))
3061                highlight)
3062              (copy-sequence highlight-words)
3063              (if gnus-newsgroup-name
3064                  (copy-sequence (gnus-group-find-parameter
3065                                  gnus-newsgroup-name 'highlight-words t)))
3066              gnus-emphasis-alist)))))
3067
3068 (eval-when-compile
3069   (defvar gnus-summary-article-menu)
3070   (defvar gnus-summary-post-menu))
3071
3072 ;;; Saving functions.
3073
3074 (defun gnus-article-save (save-buffer file &optional num)
3075   "Save the currently selected article."
3076   (unless gnus-save-all-headers
3077     ;; Remove headers according to `gnus-saved-headers'.
3078     (let ((gnus-visible-headers
3079            (or gnus-saved-headers gnus-visible-headers))
3080           (gnus-article-buffer save-buffer))
3081       (save-excursion
3082         (set-buffer save-buffer)
3083         (article-hide-headers 1 t))))
3084   (save-window-excursion
3085     (if (not gnus-default-article-saver)
3086         (error "No default saver is defined")
3087       ;; !!! Magic!  The saving functions all save
3088       ;; `gnus-save-article-buffer' (or so they think), but we
3089       ;; bind that variable to our save-buffer.
3090       (set-buffer gnus-article-buffer)
3091       (let* ((gnus-save-article-buffer save-buffer)
3092              (filename
3093               (cond
3094                ((not gnus-prompt-before-saving) 'default)
3095                ((eq gnus-prompt-before-saving 'always) nil)
3096                (t file)))
3097              (gnus-number-of-articles-to-be-saved
3098               (when (eq gnus-prompt-before-saving t)
3099                 num)))                  ; Magic
3100         (set-buffer gnus-article-current-summary)
3101         (funcall gnus-default-article-saver filename)))))
3102
3103 (defun gnus-read-save-file-name (prompt &optional filename
3104                                         function group headers variable)
3105   (let ((default-name
3106           (funcall function group headers (symbol-value variable)))
3107         result)
3108     (setq result
3109           (expand-file-name
3110            (cond
3111             ((eq filename 'default)
3112              default-name)
3113             ((eq filename t)
3114              default-name)
3115             (filename filename)
3116             (t
3117              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3118                     (prompt
3119                      (format prompt
3120                              (if (and gnus-number-of-articles-to-be-saved
3121                                       (> gnus-number-of-articles-to-be-saved 1))
3122                                  (format "these %d articles"
3123                                          gnus-number-of-articles-to-be-saved)
3124                                "this article")))
3125                     (file
3126                      ;; Let the split methods have their say.
3127                      (cond
3128                       ;; No split name was found.
3129                       ((null split-name)
3130                        (read-file-name
3131                         (concat prompt " (default "
3132                                 (file-name-nondirectory default-name) ") ")
3133                         (file-name-directory default-name)
3134                         default-name))
3135                       ;; A single group name is returned.
3136                       ((stringp split-name)
3137                        (setq default-name
3138                              (funcall function split-name headers
3139                                       (symbol-value variable)))
3140                        (read-file-name
3141                         (concat prompt " (default "
3142                                 (file-name-nondirectory default-name) ") ")
3143                         (file-name-directory default-name)
3144                         default-name))
3145                       ;; A single split name was found
3146                       ((= 1 (length split-name))
3147                        (let* ((name (expand-file-name
3148                                      (car split-name)
3149                                      gnus-article-save-directory))
3150                               (dir (cond ((file-directory-p name)
3151                                           (file-name-as-directory name))
3152                                          ((file-exists-p name) name)
3153                                          (t gnus-article-save-directory))))
3154                          (read-file-name
3155                           (concat prompt " (default " name ") ")
3156                           dir name)))
3157                       ;; A list of splits was found.
3158                       (t
3159                        (setq split-name (nreverse split-name))
3160                        (let (result)
3161                          (let ((file-name-history
3162                                 (nconc split-name file-name-history)))
3163                            (setq result
3164                                  (expand-file-name
3165                                   (read-file-name
3166                                    (concat prompt " (`M-p' for defaults) ")
3167                                    gnus-article-save-directory
3168                                    (car split-name))
3169                                   gnus-article-save-directory)))
3170                          (car (push result file-name-history)))))))
3171                ;; Create the directory.
3172                (gnus-make-directory (file-name-directory file))
3173                ;; If we have read a directory, we append the default file name.
3174                (when (file-directory-p file)
3175                  (setq file (expand-file-name (file-name-nondirectory
3176                                                default-name)
3177                                               (file-name-as-directory file))))
3178                ;; Possibly translate some characters.
3179                (nnheader-translate-file-chars file))))))
3180     (gnus-make-directory (file-name-directory result))
3181     (set variable result)))
3182
3183 (defun gnus-article-archive-name (group)
3184   "Return the first instance of an \"Archive-name\" in the current buffer."
3185   (let ((case-fold-search t))
3186     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3187       (nnheader-concat gnus-article-save-directory
3188                        (match-string 1)))))
3189
3190 (defun gnus-article-nndoc-name (group)
3191   "If GROUP is an nndoc group, return the name of the parent group."
3192   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3193     (gnus-group-get-parameter group 'save-article-group)))
3194
3195 (defun gnus-summary-save-in-rmail (&optional filename)
3196   "Append this article to Rmail file.
3197 Optional argument FILENAME specifies file name.
3198 Directory to save to is default to `gnus-article-save-directory'."
3199   (setq filename (gnus-read-save-file-name
3200                   "Save %s in rmail file:" filename
3201                   gnus-rmail-save-name gnus-newsgroup-name
3202                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3203   (gnus-eval-in-buffer-window gnus-save-article-buffer
3204     (save-excursion
3205       (save-restriction
3206         (widen)
3207         (gnus-output-to-rmail filename))))
3208   filename)
3209
3210 (defun gnus-summary-save-in-mail (&optional filename)
3211   "Append this article to Unix mail file.
3212 Optional argument FILENAME specifies file name.
3213 Directory to save to is default to `gnus-article-save-directory'."
3214   (setq filename (gnus-read-save-file-name
3215                   "Save %s in Unix mail file:" filename
3216                   gnus-mail-save-name gnus-newsgroup-name
3217                   gnus-current-headers 'gnus-newsgroup-last-mail))
3218   (gnus-eval-in-buffer-window gnus-save-article-buffer
3219     (save-excursion
3220       (save-restriction
3221         (widen)
3222         (if (and (file-readable-p filename)
3223                  (mail-file-babyl-p filename))
3224             (rmail-output-to-rmail-file filename t)
3225           (gnus-output-to-mail filename)))))
3226   filename)
3227
3228 (defun gnus-summary-save-in-file (&optional filename overwrite)
3229   "Append this article to file.
3230 Optional argument FILENAME specifies file name.
3231 Directory to save to is default to `gnus-article-save-directory'."
3232   (setq filename (gnus-read-save-file-name
3233                   "Save %s in file:" filename
3234                   gnus-file-save-name gnus-newsgroup-name
3235                   gnus-current-headers 'gnus-newsgroup-last-file))
3236   (gnus-eval-in-buffer-window gnus-save-article-buffer
3237     (save-excursion
3238       (save-restriction
3239         (widen)
3240         (when (and overwrite
3241                    (file-exists-p filename))
3242           (delete-file filename))
3243         (gnus-output-to-file filename))))
3244   filename)
3245
3246 (defun gnus-summary-write-to-file (&optional filename)
3247   "Write this article to a file, overwriting it if the file exists.
3248 Optional argument FILENAME specifies file name.
3249 The directory to save in defaults to `gnus-article-save-directory'."
3250   (gnus-summary-save-in-file nil t))
3251
3252 (defun gnus-summary-save-body-in-file (&optional filename)
3253   "Append this article body to a file.
3254 Optional argument FILENAME specifies file name.
3255 The directory to save in defaults to `gnus-article-save-directory'."
3256   (setq filename (gnus-read-save-file-name
3257                   "Save %s body in file:" filename
3258                   gnus-file-save-name gnus-newsgroup-name
3259                   gnus-current-headers 'gnus-newsgroup-last-file))
3260   (gnus-eval-in-buffer-window gnus-save-article-buffer
3261     (save-excursion
3262       (save-restriction
3263         (widen)
3264         (when (article-goto-body)
3265           (narrow-to-region (point) (point-max)))
3266         (gnus-output-to-file filename))))
3267   filename)
3268
3269 (defun gnus-summary-save-in-pipe (&optional command)
3270   "Pipe this article to subprocess."
3271   (setq command
3272         (cond ((and (eq command 'default)
3273                     gnus-last-shell-command)
3274                gnus-last-shell-command)
3275               ((stringp command)
3276                command)
3277               (t (read-string
3278                   (format
3279                    "Shell command on %s: "
3280                    (if (and gnus-number-of-articles-to-be-saved
3281                             (> gnus-number-of-articles-to-be-saved 1))
3282                        (format "these %d articles"
3283                                gnus-number-of-articles-to-be-saved)
3284                      "this article"))
3285                   gnus-last-shell-command))))
3286   (when (string-equal command "")
3287     (if gnus-last-shell-command
3288         (setq command gnus-last-shell-command)
3289       (error "A command is required")))
3290   (gnus-eval-in-buffer-window gnus-article-buffer
3291     (save-restriction
3292       (widen)
3293       (shell-command-on-region (point-min) (point-max) command nil)))
3294   (setq gnus-last-shell-command command))
3295
3296 (defun gnus-summary-pipe-to-muttprint (&optional command)
3297   "Pipe this article to muttprint."
3298   (setq command (read-string
3299                  "Print using command: " gnus-summary-muttprint-program
3300                  nil gnus-summary-muttprint-program))
3301   (gnus-summary-save-in-pipe command))
3302
3303 ;;; Article file names when saving.
3304
3305 (defun gnus-capitalize-newsgroup (newsgroup)
3306   "Capitalize NEWSGROUP name."
3307   (when (not (zerop (length newsgroup)))
3308     (concat (char-to-string (upcase (aref newsgroup 0)))
3309             (substring newsgroup 1))))
3310
3311 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3312   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3313 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3314 Otherwise, it is like ~/News/news/group/num."
3315   (let ((default
3316           (expand-file-name
3317            (concat (if (gnus-use-long-file-name 'not-save)
3318                        (gnus-capitalize-newsgroup newsgroup)
3319                      (gnus-newsgroup-directory-form newsgroup))
3320                    "/" (int-to-string (mail-header-number headers)))
3321            gnus-article-save-directory)))
3322     (if (and last-file
3323              (string-equal (file-name-directory default)
3324                            (file-name-directory last-file))
3325              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3326         default
3327       (or last-file default))))
3328
3329 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3330   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3331 If variable `gnus-use-long-file-name' is non-nil, it is
3332 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3333   (let ((default
3334           (expand-file-name
3335            (concat (if (gnus-use-long-file-name 'not-save)
3336                        newsgroup
3337                      (gnus-newsgroup-directory-form newsgroup))
3338                    "/" (int-to-string (mail-header-number headers)))
3339            gnus-article-save-directory)))
3340     (if (and last-file
3341              (string-equal (file-name-directory default)
3342                            (file-name-directory last-file))
3343              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3344         default
3345       (or last-file default))))
3346
3347 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3348   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3349 If variable `gnus-use-long-file-name' is non-nil, it is
3350 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3351   (or last-file
3352       (expand-file-name
3353        (if (gnus-use-long-file-name 'not-save)
3354            newsgroup
3355          (file-relative-name
3356           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3357           default-directory))
3358        gnus-article-save-directory)))
3359
3360 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3361   "Generate file name from sender."
3362   (let ((from (mail-header-from headers)))
3363     (expand-file-name
3364      (if (and from (string-match "\\([^ <]+\\)@" from))
3365          (match-string 1 from)
3366        "nobody")
3367      gnus-article-save-directory)))
3368
3369 (defun article-verify-x-pgp-sig ()
3370   "Verify X-PGP-Sig."
3371   (interactive)
3372   (if (gnus-buffer-live-p gnus-original-article-buffer)
3373       (let ((sig (with-current-buffer gnus-original-article-buffer
3374                    (gnus-fetch-field "X-PGP-Sig")))
3375             items info headers)
3376         (when (and sig
3377                    mml2015-use
3378                    (mml2015-clear-verify-function))
3379           (with-temp-buffer
3380             (insert-buffer-substring gnus-original-article-buffer)
3381             (setq items (split-string sig))
3382             (message-narrow-to-head)
3383             (let ((inhibit-point-motion-hooks t)
3384                   (case-fold-search t))
3385               ;; Don't verify multiple headers.
3386               (setq headers (mapconcat (lambda (header)
3387                                          (concat header ": "
3388                                                  (mail-fetch-field header) "\n"))
3389                                        (split-string (nth 1 items) ",") "")))
3390             (delete-region (point-min) (point-max))
3391             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3392             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3393             (insert headers)
3394             (widen)
3395             (forward-line)
3396             (while (not (eobp))
3397               (if (looking-at "^-")
3398                   (insert "- "))
3399               (forward-line))
3400             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3401             (insert "Version: " (car items) "\n\n")
3402             (insert (mapconcat 'identity (cddr items) "\n"))
3403             (insert "\n-----END PGP SIGNATURE-----\n")
3404             (let ((mm-security-handle (list (format "multipart/signed"))))
3405               (mml2015-clean-buffer)
3406               (let ((coding-system-for-write (or gnus-newsgroup-charset
3407                                                  'iso-8859-1)))
3408                 (funcall (mml2015-clear-verify-function)))
3409               (setq info
3410                     (or (mm-handle-multipart-ctl-parameter
3411                          mm-security-handle 'gnus-details)
3412                         (mm-handle-multipart-ctl-parameter
3413                          mm-security-handle 'gnus-info)))))
3414           (when info
3415             (let (buffer-read-only bface eface)
3416               (save-restriction
3417                 (message-narrow-to-head)
3418                 (goto-char (point-max))
3419                 (forward-line -1)
3420                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3421                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3422                 (message-remove-header "X-Gnus-PGP-Verify")
3423                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3424                     (forward-line)
3425                   (goto-char (point-max)))
3426                 (narrow-to-region (point) (point))
3427                 (insert "X-Gnus-PGP-Verify: " info "\n")
3428                 (goto-char (point-min))
3429                 (forward-line)
3430                 (while (not (eobp))
3431                   (if (not (looking-at "^[ \t]"))
3432                       (insert " "))
3433                   (forward-line))
3434                 ;; Do highlighting.
3435                 (goto-char (point-min))
3436                 (when (looking-at "\\([^:]+\\): *")
3437                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3438                                      'face bface)
3439                   (put-text-property (match-end 0) (point-max)
3440                                      'face eface)))))))))
3441
3442 (defun article-verify-cancel-lock ()
3443   "Verify Cancel-Lock header."
3444   (interactive)
3445   (if (gnus-buffer-live-p gnus-original-article-buffer)
3446       (canlock-verify gnus-original-article-buffer)))
3447
3448 (defun article-monafy ()
3449   "Display body part with mona font."
3450   (interactive)
3451   (unless (if (featurep 'xemacs)
3452               (find-face 'gnus-mona-face)
3453             (facep 'gnus-mona-face))
3454     (require 'navi2ch-mona)
3455     (set-face-font (make-face 'gnus-mona-face) navi2ch-mona-font))
3456   (save-excursion
3457     (let ((buffer-read-only nil))
3458       (article-goto-body)
3459       (gnus-overlay-put
3460        (gnus-make-overlay (point) (point-max))
3461        'face 'gnus-mona-face))))
3462
3463 (eval-and-compile
3464   (mapcar
3465    (lambda (func)
3466      (let (afunc gfunc)
3467        (if (consp func)
3468            (setq afunc (car func)
3469                  gfunc (cdr func))
3470          (setq afunc func
3471                gfunc (intern (format "gnus-%s" func))))
3472        (defalias gfunc
3473          (when (fboundp afunc)
3474            `(lambda (&optional interactive &rest args)
3475               ,(documentation afunc t)
3476               (interactive (list t))
3477               (save-excursion
3478                 (set-buffer gnus-article-buffer)
3479                 (if interactive
3480                     (call-interactively ',afunc)
3481                   (apply ',afunc args))))))))
3482    '(article-hide-headers
3483      article-verify-x-pgp-sig
3484      article-verify-cancel-lock
3485      article-monafy
3486      article-hide-boring-headers
3487      article-toggle-headers
3488      article-treat-overstrike
3489      article-fill-long-lines
3490      article-capitalize-sentences
3491      article-remove-cr
3492      article-remove-leading-whitespace
3493      article-display-x-face
3494      article-de-quoted-unreadable
3495      article-de-base64-unreadable
3496      article-decode-HZ
3497      article-wash-html
3498      article-unsplit-urls
3499      article-hide-list-identifiers
3500      article-hide-pgp
3501      article-strip-banner
3502      article-babel
3503      article-hide-pem
3504      article-hide-signature
3505      article-strip-headers-in-body
3506      article-remove-trailing-blank-lines
3507      article-strip-leading-blank-lines
3508      article-strip-multiple-blank-lines
3509      article-strip-leading-space
3510      article-strip-trailing-space
3511      article-strip-blank-lines
3512      article-strip-all-blank-lines
3513      article-date-local
3514      article-date-english
3515      article-date-iso8601
3516      article-date-original
3517      article-date-ut
3518      article-decode-mime-words
3519      article-decode-charset
3520      article-decode-encoded-words
3521      article-date-user
3522      article-date-lapsed
3523      article-emphasize
3524      article-treat-dumbquotes
3525      article-normalize-headers
3526      (article-show-all-headers . gnus-article-show-all-headers)
3527 ;;      (article-show-all . gnus-article-show-all)
3528      )))
3529 \f
3530 ;;;
3531 ;;; Gnus article mode
3532 ;;;
3533
3534 (put 'gnus-article-mode 'mode-class 'special)
3535
3536 (gnus-define-keys gnus-article-mode-map
3537   " " gnus-article-goto-next-page
3538   "\177" gnus-article-goto-prev-page
3539   [delete] gnus-article-goto-prev-page
3540   [backspace] gnus-article-goto-prev-page
3541   "\C-c^" gnus-article-refer-article
3542   "h" gnus-article-show-summary
3543   "s" gnus-article-show-summary
3544   "\C-c\C-m" gnus-article-mail
3545   "?" gnus-article-describe-briefly
3546   "e" gnus-summary-edit-article
3547   "<" beginning-of-buffer
3548   ">" end-of-buffer
3549   "\C-c\C-i" gnus-info-find-node
3550   "\C-c\C-b" gnus-bug
3551   "R" gnus-article-reply-with-original
3552   "F" gnus-article-followup-with-original
3553   "\C-hk" gnus-article-describe-key
3554   "\C-hc" gnus-article-describe-key-briefly
3555
3556   "\C-d" gnus-article-read-summary-keys
3557   "\M-*" gnus-article-read-summary-keys
3558   "\M-#" gnus-article-read-summary-keys
3559   "\M-^" gnus-article-read-summary-keys
3560   "\M-g" gnus-article-read-summary-keys)
3561
3562 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3563 (let (keys)
3564   (let ((key 32))
3565     (while (<= key 127)
3566       (push (char-to-string key) keys)
3567       (incf key))
3568     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3569       (setq key (car elem))
3570       (when (and (> (length key) 0)
3571                  (not (eq 'menu-bar (aref key 0)))
3572                  (symbolp (lookup-key gnus-summary-mode-map key)))
3573         (push key keys))))
3574   (dolist (key keys)
3575     (unless (lookup-key gnus-article-mode-map key)
3576       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3577
3578 (defun gnus-article-make-menu-bar ()
3579   (unless (boundp 'gnus-article-commands-menu)
3580     (gnus-summary-make-menu-bar))
3581   (gnus-turn-off-edit-menu 'article)
3582   (unless (boundp 'gnus-article-article-menu)
3583     (easy-menu-define
3584      gnus-article-article-menu gnus-article-mode-map ""
3585      '("Article"
3586        ["Scroll forwards" gnus-article-goto-next-page t]
3587        ["Scroll backwards" gnus-article-goto-prev-page t]
3588        ["Show summary" gnus-article-show-summary t]
3589        ["Fetch Message-ID at point" gnus-article-refer-article t]
3590        ["Mail to address at point" gnus-article-mail t]
3591        ["Send a bug report" gnus-bug t]))
3592
3593     (easy-menu-define
3594      gnus-article-treatment-menu gnus-article-mode-map ""
3595      ;; Fixme: this should use :active (and maybe :visible).
3596      '("Treatment"
3597        ["Hide headers" gnus-article-toggle-headers t]
3598        ["Hide signature" gnus-article-hide-signature t]
3599        ["Hide citation" gnus-article-hide-citation t]
3600        ["Treat overstrike" gnus-article-treat-overstrike t]
3601        ["Remove carriage return" gnus-article-remove-cr t]
3602        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3603        ["Treat html" gnus-article-wash-html t]
3604        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3605        ["Decode HZ" gnus-article-decode-HZ t]))
3606
3607     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3608
3609     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3610
3611     (gnus-run-hooks 'gnus-article-menu-hook)))
3612
3613 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3614 ;; Summary.
3615
3616 (defun gnus-article-mode ()
3617   "Major mode for displaying an article.
3618
3619 All normal editing commands are switched off.
3620
3621 The following commands are available in addition to all summary mode
3622 commands:
3623 \\<gnus-article-mode-map>
3624 \\[gnus-article-next-page]\t Scroll the article one page forwards
3625 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3626 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3627 \\[gnus-article-show-summary]\t Display the summary buffer
3628 \\[gnus-article-mail]\t Send a reply to the address near point
3629 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3630 \\[gnus-info-find-node]\t Go to the Gnus info node"
3631   (interactive)
3632   (gnus-simplify-mode-line)
3633   (setq mode-name "Article")
3634   (setq major-mode 'gnus-article-mode)
3635   (make-local-variable 'minor-mode-alist)
3636   (unless (assq 'gnus-show-mime minor-mode-alist)
3637     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3638   (use-local-map gnus-article-mode-map)
3639   (when (gnus-visual-p 'article-menu 'menu)
3640     (gnus-article-make-menu-bar))
3641   (gnus-update-format-specifications nil 'article-mode)
3642   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3643   (make-local-variable 'gnus-page-broken)
3644   (make-local-variable 'gnus-button-marker-list)
3645   (make-local-variable 'gnus-article-current-summary)
3646   (make-local-variable 'gnus-article-mime-handles)
3647   (make-local-variable 'gnus-article-decoded-p)
3648   (make-local-variable 'gnus-article-mime-handle-alist)
3649   (make-local-variable 'gnus-article-wash-types)
3650   (make-local-variable 'gnus-article-image-alist)
3651   (make-local-variable 'gnus-article-charset)
3652   (make-local-variable 'gnus-article-ignored-charsets)
3653   (gnus-set-default-directory)
3654   (buffer-disable-undo)
3655   (setq buffer-read-only t)
3656   (set-syntax-table gnus-article-mode-syntax-table)
3657   (gnus-run-hooks 'gnus-article-mode-hook))
3658
3659 (defun gnus-article-setup-buffer ()
3660   "Initialize the article buffer."
3661   (let* ((name (if gnus-single-article-buffer "*Article*"
3662                  (concat "*Article " gnus-newsgroup-name "*")))
3663          (original
3664           (progn (string-match "\\*Article" name)
3665                  (concat " *Original Article"
3666                          (substring name (match-end 0))))))
3667     (setq gnus-article-buffer name)
3668     (setq gnus-original-article-buffer original)
3669     (setq gnus-article-mime-handle-alist nil)
3670     ;; This might be a variable local to the summary buffer.
3671     (unless gnus-single-article-buffer
3672       (save-excursion
3673         (set-buffer gnus-summary-buffer)
3674         (setq gnus-article-buffer name)
3675         (setq gnus-original-article-buffer original)
3676         (gnus-set-global-variables)))
3677     (gnus-article-setup-highlight-words)
3678     ;; Init original article buffer.
3679     (save-excursion
3680       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3681       (set-buffer-multibyte t)
3682       (setq major-mode 'gnus-original-article-mode)
3683       (make-local-variable 'gnus-original-article))
3684     (if (get-buffer name)
3685         (save-excursion
3686           (set-buffer name)
3687           (buffer-disable-undo)
3688           (setq buffer-read-only t)
3689           (unless (eq major-mode 'gnus-article-mode)
3690             (gnus-article-mode))
3691           (current-buffer))
3692       (save-excursion
3693         (set-buffer (gnus-get-buffer-create name))
3694         (gnus-article-mode)
3695         (make-local-variable 'gnus-summary-buffer)
3696         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3697         (current-buffer)))))
3698
3699 ;; Set article window start at LINE, where LINE is the number of lines
3700 ;; from the head of the article.
3701 (defun gnus-article-set-window-start (&optional line)
3702   (set-window-start
3703    (gnus-get-buffer-window gnus-article-buffer t)
3704    (save-excursion
3705      (set-buffer gnus-article-buffer)
3706      (goto-char (point-min))
3707      (if (not line)
3708          (point-min)
3709        (gnus-message 6 "Moved to bookmark")
3710        (search-forward "\n\n" nil t)
3711        (forward-line line)
3712        (point)))))
3713
3714 ;;; @@ article filters
3715 ;;;
3716
3717 (defun gnus-article-display-mime-message ()
3718   "Article display method for MIME message."
3719   ;; called from `gnus-original-article-buffer'.
3720   (let (charset all-headers)
3721     (with-current-buffer gnus-summary-buffer
3722       (setq charset default-mime-charset
3723             all-headers gnus-have-all-headers))
3724     (make-local-variable 'default-mime-charset)
3725     (setq default-mime-charset charset)
3726     (with-current-buffer (get-buffer-create gnus-article-buffer)
3727       (make-local-variable 'default-mime-charset)
3728       (setq default-mime-charset charset))
3729     (mime-display-message mime-message-structure
3730                           gnus-article-buffer nil gnus-article-mode-map)
3731     (when all-headers
3732       (gnus-article-hide-headers nil -1)))
3733   (run-hooks 'gnus-mime-article-prepare-hook))
3734
3735 (defun gnus-article-display-traditional-message ()
3736   "Article display method for traditional message."
3737   (set-buffer gnus-article-buffer)
3738   (let (buffer-read-only)
3739     (erase-buffer)
3740     (insert-buffer-substring gnus-original-article-buffer)))
3741
3742 (defun gnus-article-make-full-mail-header (&optional number charset)
3743   "Create a new mail header structure in a raw article buffer."
3744   (unless (and number charset)
3745     (save-current-buffer
3746       (set-buffer gnus-summary-buffer)
3747       (unless number
3748         (setq number (or (cdr gnus-article-current) 0)))
3749       (unless charset
3750         (setq charset (or default-mime-charset 'x-ctext)))))
3751   (goto-char (point-min))
3752   (let ((header-end (if (search-forward "\n\n" nil t)
3753                         (1- (point))
3754                       (goto-char (point-max))))
3755         (chars (- (point-max) (point)))
3756         (lines (count-lines (point) (point-max)))
3757         (default-mime-charset charset)
3758         xref)
3759     (narrow-to-region (point-min) header-end)
3760     (setq xref (std11-fetch-field "xref"))
3761     (prog1
3762         (make-full-mail-header
3763          number
3764          (std11-fetch-field "subject")
3765          (std11-fetch-field "from")
3766          (std11-fetch-field "date")
3767          (std11-fetch-field "message-id")
3768          (std11-fetch-field "references")
3769          chars
3770          lines
3771          (when xref (concat "Xref: " xref)))
3772       (widen))))
3773
3774 (defun gnus-article-prepare (article &optional all-headers header)
3775   "Prepare ARTICLE in article mode buffer.
3776 ARTICLE should either be an article number or a Message-ID.
3777 If ARTICLE is an id, HEADER should be the article headers.
3778 If ALL-HEADERS is non-nil, no headers are hidden."
3779   (save-excursion
3780     ;; Make sure we start in a summary buffer.
3781     (unless (eq major-mode 'gnus-summary-mode)
3782       (set-buffer gnus-summary-buffer))
3783     (setq gnus-summary-buffer (current-buffer))
3784     (let* ((gnus-article (if header (mail-header-number header) article))
3785            (summary-buffer (current-buffer))
3786            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3787            (group gnus-newsgroup-name)
3788            result)
3789       (save-excursion
3790         (gnus-article-setup-buffer)
3791         (set-buffer gnus-article-buffer)
3792         ;; Deactivate active regions.
3793         (when (and (boundp 'transient-mark-mode)
3794                    transient-mark-mode)
3795           (setq mark-active nil))
3796         (if (not (setq result (let ((buffer-read-only nil))
3797                                 (gnus-request-article-this-buffer
3798                                  article group))))
3799             ;; There is no such article.
3800             (save-excursion
3801               (when (and (numberp article)
3802                          (not (memq article gnus-newsgroup-sparse)))
3803                 (setq gnus-article-current
3804                       (cons gnus-newsgroup-name article))
3805                 (set-buffer gnus-summary-buffer)
3806                 (setq gnus-current-article article)
3807                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3808                     (progn
3809                       (gnus-summary-set-agent-mark article)
3810                       (message "Message marked for downloading"))
3811                   (gnus-summary-mark-article article gnus-canceled-mark)
3812                   (unless (memq article gnus-newsgroup-sparse)
3813                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3814           (if (or (eq result 'pseudo)
3815                   (eq result 'nneething))
3816               (progn
3817                 (save-excursion
3818                   (set-buffer summary-buffer)
3819                   (push article gnus-newsgroup-history)
3820                   (setq gnus-last-article gnus-current-article
3821                         gnus-current-article 0
3822                         gnus-current-headers nil
3823                         gnus-article-current nil)
3824                   (if (eq result 'nneething)
3825                       (gnus-configure-windows 'summary)
3826                     (gnus-configure-windows 'article))
3827                   (gnus-set-global-variables))
3828                 (let ((gnus-article-mime-handle-alist-1
3829                        gnus-article-mime-handle-alist))
3830                   (gnus-set-mode-line 'article)))
3831             ;; The result from the `request' was an actual article -
3832             ;; or at least some text that is now displayed in the
3833             ;; article buffer.
3834             (when (and (numberp article)
3835                        (not (eq article gnus-current-article)))
3836               ;; Seems like a new article has been selected.
3837               ;; `gnus-current-article' must be an article number.
3838               (save-excursion
3839                 (set-buffer summary-buffer)
3840                 (push article gnus-newsgroup-history)
3841                 (setq gnus-last-article gnus-current-article
3842                       gnus-current-article article
3843                       gnus-current-headers
3844                       (gnus-summary-article-header gnus-current-article)
3845                       gnus-article-current
3846                       (cons gnus-newsgroup-name gnus-current-article))
3847                 (unless (vectorp gnus-current-headers)
3848                   (setq gnus-current-headers nil))
3849                 (gnus-summary-goto-subject gnus-current-article)
3850                 (when (gnus-summary-show-thread)
3851                   ;; If the summary buffer really was folded, the
3852                   ;; previous goto may not actually have gone to
3853                   ;; the right article, but the thread root instead.
3854                   ;; So we go again.
3855                   (gnus-summary-goto-subject gnus-current-article))
3856                 (gnus-run-hooks 'gnus-mark-article-hook)
3857                 (gnus-set-mode-line 'summary)
3858                 (when (gnus-visual-p 'article-highlight 'highlight)
3859                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3860                 ;; Set the global newsgroup variables here.
3861                 (gnus-set-global-variables)
3862                 (setq gnus-have-all-headers
3863                       (or all-headers gnus-show-all-headers))))
3864             (save-excursion
3865               (gnus-configure-windows 'article))
3866             (when (or (numberp article)
3867                       (stringp article))
3868               (gnus-article-prepare-display)
3869               ;; Do page break.
3870               (goto-char (point-min))
3871               (setq gnus-page-broken
3872                     (when gnus-break-pages
3873                       (gnus-narrow-to-page)
3874                       t)))
3875             (let ((gnus-article-mime-handle-alist-1
3876                    gnus-article-mime-handle-alist))
3877               (gnus-set-mode-line 'article))
3878             (article-goto-body)
3879             (unless (bobp)
3880               (forward-line -1))
3881             (set-window-point (get-buffer-window (current-buffer)) (point))
3882             (gnus-configure-windows 'article)
3883             t))))))
3884
3885 (defun gnus-article-prepare-mime-display (&optional number)
3886   (goto-char (point-min))
3887   (when (re-search-forward "^[^\t ]+:" nil t)
3888     (goto-char (match-beginning 0)))
3889   (let ((entity (if (eq 1 (point-min))
3890                     (get-text-property 1 'mime-view-entity)
3891                   (get-text-property (point) 'mime-view-entity)))
3892         last-entity child-entity next type)
3893     (setq child-entity (mime-entity-children entity))
3894     (if child-entity
3895         (setq last-entity (nth (1- (length child-entity))
3896                                child-entity))
3897       (setq last-entity entity))
3898     (save-restriction
3899       (narrow-to-region (point)
3900                         (if (search-forward "\n\n" nil t)
3901                             (point)
3902                           (point-max)))
3903       (gnus-treat-article 'head)
3904       (put-text-property (point-min) (point-max) 'article-treated-header t)
3905       (goto-char (point-max)))
3906     (while (and (not (eobp)) entity)
3907       (setq next (set-marker
3908                   (make-marker)
3909                   (next-single-property-change (point) 'mime-view-entity
3910                                                nil (point-max))))
3911       (let ((types (mime-entity-content-type entity)))
3912         (while (eq 'multipart (mime-content-type-primary-type types))
3913           (setq entity (car (mime-entity-children entity))
3914                 types (mime-entity-content-type entity)))
3915         (when types
3916           (setq type (format "%s/%s"
3917                              (mime-content-type-primary-type types)
3918                              (mime-content-type-subtype types)))))
3919       (if (string-equal type "message/rfc822")
3920           (progn
3921             (setq next (point))
3922             (let ((children (mime-entity-children entity))
3923                   last-children)
3924               (when children
3925                 (setq last-children (nth (1- (length children)) children))
3926                 (while
3927                     (and
3928                      (not (eq last-children
3929                               (get-text-property next 'mime-view-entity)))
3930                      (setq next
3931                            (next-single-property-change next
3932                                                         'mime-view-entity
3933                                                         nil (point-max)))))))
3934             (setq next (next-single-property-change next 'mime-view-entity
3935                                                     nil (point-max)))
3936             (save-restriction
3937               (narrow-to-region (point) next)
3938               (gnus-article-prepare-mime-display)
3939               (goto-char (point-max)))
3940             (setq entity (get-text-property (point) 'mime-view-entity)))
3941         (save-restriction
3942           (narrow-to-region (point) next)
3943           ;; Kludge. We have to count true number, but for now,
3944           ;; part number is here only to achieve `last'.
3945           (gnus-treat-article nil 1
3946                               (if (eq entity last-entity)
3947                                   1 2)
3948                               type)
3949           (goto-char (point-max)))
3950         (setq entity (get-text-property next 'mime-view-entity))))))
3951
3952 ;;;###autoload
3953 (defun gnus-article-prepare-display ()
3954   "Make the current buffer look like a nice article."
3955   (let ((gnus-article-buffer (current-buffer))
3956         buffer-read-only)
3957     (unless (eq major-mode 'gnus-article-mode)
3958       (gnus-article-mode))
3959     (setq buffer-read-only nil
3960           gnus-button-marker-list nil
3961           gnus-article-wash-types nil
3962           gnus-article-image-alist nil)
3963     (save-restriction
3964       (widen)
3965       (static-if (featurep 'xemacs)
3966           (map-extents (lambda (extent maparg) (delete-extent extent)))
3967         (let ((lists (overlay-lists)))
3968           (dolist (overlay (nconc (car lists) (cdr lists)))
3969             (delete-overlay overlay)))))
3970     (gnus-run-hooks 'gnus-tmp-internal-hook))
3971   (set-buffer gnus-original-article-buffer)
3972   ;; Display message.
3973   (setq mime-message-structure gnus-current-headers)
3974   (mime-buffer-entity-set-buffer-internal mime-message-structure
3975                                           gnus-original-article-buffer)
3976   (mime-entity-set-representation-type-internal mime-message-structure
3977                                                 'mime-buffer-entity)
3978   (luna-send mime-message-structure 'initialize-instance
3979              mime-message-structure)
3980   (if gnus-show-mime
3981       (let (mime-display-header-hook mime-display-text/plain-hook)
3982         (funcall gnus-article-display-method-for-mime))
3983     (funcall gnus-article-display-method-for-traditional))
3984   ;; Call the treatment functions.
3985   (let ((inhibit-read-only t))
3986     (save-restriction
3987       (widen)
3988       (if gnus-show-mime
3989           (gnus-article-prepare-mime-display)
3990         (narrow-to-region (goto-char (point-min))
3991                           (if (search-forward "\n\n" nil t)
3992                               (point)
3993                             (point-max)))
3994         (gnus-treat-article 'head)
3995         (put-text-property (point-min) (point-max) 'article-treated-header t)
3996         (goto-char (point-max))
3997         (widen)
3998         (narrow-to-region (point) (point-max))
3999         (gnus-treat-article nil))
4000       (put-text-property (point-min) (point-max) 'read-only nil)))
4001   (gnus-run-hooks 'gnus-article-prepare-hook))
4002
4003 (defun gnus-article-decode-article-as-default-mime-charset ()
4004   "Decode an article as `default-mime-charset'.  It won't work if the
4005 value of the variable `gnus-show-mime' is non-nil."
4006   (unless gnus-show-mime
4007     (set (make-local-variable 'default-mime-charset)
4008          (with-current-buffer gnus-summary-buffer
4009            default-mime-charset))
4010     (decode-mime-charset-region (point-min) (point-max)
4011                                 default-mime-charset)))
4012
4013 ;;;
4014 ;;; Gnus MIME viewing functions
4015 ;;;
4016
4017 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4018   "Format of the MIME buttons.
4019
4020 Valid specifiers include:
4021 %t  The MIME type
4022 %T  MIME type, along with additional info
4023 %n  The `name' parameter
4024 %d  The description, if any
4025 %l  The length of the encoded part
4026 %p  The part identifier number
4027 %e  Dots if the part isn't displayed
4028
4029 General format specifiers can also be used.  See
4030 (gnus)Formatting Variables.")
4031
4032 (defvar gnus-mime-button-line-format-alist
4033   '((?t gnus-tmp-type ?s)
4034     (?T gnus-tmp-type-long ?s)
4035     (?n gnus-tmp-name ?s)
4036     (?d gnus-tmp-description ?s)
4037     (?p gnus-tmp-id ?s)
4038     (?l gnus-tmp-length ?d)
4039     (?e gnus-tmp-dots ?s)))
4040
4041 (defvar gnus-mime-button-commands
4042   '((gnus-article-press-button "\r" "Toggle Display")
4043     (gnus-mime-view-part "v" "View Interactively...")
4044     (gnus-mime-view-part-as-type "t" "View As Type...")
4045     (gnus-mime-view-part-as-charset "C" "View As charset...")
4046     (gnus-mime-save-part "o" "Save...")
4047     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4048     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4049     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4050     (gnus-mime-view-part-internally "E" "View Internally")
4051     (gnus-mime-view-part-externally "e" "View Externally")
4052     (gnus-mime-print-part "p" "Print")
4053     (gnus-mime-pipe-part "|" "Pipe To Command...")
4054     (gnus-mime-action-on-part "." "Take action on the part")))
4055
4056 (defun gnus-article-mime-part-status ()
4057   (with-current-buffer gnus-article-buffer
4058     (let ((entity (get-text-property (point-min) 'mime-view-entity))
4059           children)
4060       (if (and entity
4061                (setq children (mime-entity-children entity))
4062                (setq children (length children)))
4063           (if (eq 1 children)
4064               " (1 part)"
4065             (format " (%d parts)" children))
4066         ""))))
4067
4068 (defvar gnus-mime-button-map
4069   (let ((map (make-sparse-keymap)))
4070     (unless (>= (string-to-number emacs-version) 21)
4071       ;; XEmacs doesn't care.
4072       (set-keymap-parent map gnus-article-mode-map))
4073     (define-key map gnus-mouse-2 'gnus-article-push-button)
4074     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4075     (dolist (c gnus-mime-button-commands)
4076       (define-key map (cadr c) (car c)))
4077     map))
4078
4079 (defun gnus-mime-button-menu (event)
4080   "Construct a context-sensitive menu of MIME commands."
4081   (interactive "e")
4082   (save-window-excursion
4083     (let ((pos (event-start event)))
4084       (select-window (posn-window pos))
4085       (goto-char (posn-point pos))
4086       (gnus-article-check-buffer)
4087       (let ((response (x-popup-menu
4088                        t `("MIME Part"
4089                            ("" ,@(mapcar (lambda (c)
4090                                            (cons (caddr c) (car c)))
4091                                          gnus-mime-button-commands))))))
4092         (if response
4093             (call-interactively response))))))
4094
4095 (defun gnus-mime-view-all-parts (&optional handles)
4096   "View all the MIME parts."
4097   (interactive)
4098   (save-current-buffer
4099     (set-buffer gnus-article-buffer)
4100     (let ((handles (or handles gnus-article-mime-handles))
4101           (mail-parse-charset gnus-newsgroup-charset)
4102           (mail-parse-ignored-charsets
4103            (with-current-buffer gnus-summary-buffer
4104              gnus-newsgroup-ignored-charsets)))
4105       (when handles
4106         (mm-remove-parts handles)
4107         (goto-char (point-min))
4108         (or (search-forward "\n\n") (goto-char (point-max)))
4109         (let (buffer-read-only)
4110           (delete-region (point) (point-max))
4111           (mm-display-parts handles))))))
4112
4113 (defun gnus-mime-save-part-and-strip ()
4114   "Save the MIME part under point then replace it with an external body."
4115   (interactive)
4116   (gnus-article-check-buffer)
4117   (let* ((data (get-text-property (point) 'gnus-data))
4118          file param
4119          (handles gnus-article-mime-handles))
4120     (if (mm-multiple-handles gnus-article-mime-handles)
4121         (error "This function is not implemented"))
4122     (setq file (and data (mm-save-part data)))
4123     (when file
4124       (with-current-buffer (mm-handle-buffer data)
4125         (erase-buffer)
4126         (insert "Content-Type: " (mm-handle-media-type data))
4127         (mml-insert-parameter-string (cdr (mm-handle-type data))
4128                                      '(charset))
4129         (insert "\n")
4130         (insert "Content-ID: " (message-make-message-id) "\n")
4131         (insert "Content-Transfer-Encoding: binary\n")
4132         (insert "\n"))
4133       (setcdr data
4134               (cdr (mm-make-handle nil
4135                                    `("message/external-body"
4136                                      (access-type . "LOCAL-FILE")
4137                                      (name . ,file)))))
4138       (set-buffer gnus-summary-buffer)
4139       (gnus-article-edit-article
4140        `(lambda ()
4141           (erase-buffer)
4142           (let ((mail-parse-charset (or gnus-article-charset
4143                                         ',gnus-newsgroup-charset))
4144                 (mail-parse-ignored-charsets
4145                  (or gnus-article-ignored-charsets
4146                      ',gnus-newsgroup-ignored-charsets))
4147                 (mbl mml-buffer-list))
4148             (setq mml-buffer-list nil)
4149             (insert-buffer gnus-original-article-buffer)
4150             (mime-to-mml ',handles)
4151             (setq gnus-article-mime-handles nil)
4152             (let ((mbl1 mml-buffer-list))
4153               (setq mml-buffer-list mbl)
4154               (set (make-local-variable 'mml-buffer-list) mbl1))
4155             ;; LOCAL argument of add-hook differs between GNU Emacs
4156             ;; and XEmacs. make-local-hook makes sure they are local.
4157             (make-local-hook 'kill-buffer-hook)
4158             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4159        `(lambda (no-highlight)
4160           (let ((mail-parse-charset (or gnus-article-charset
4161                                         ',gnus-newsgroup-charset))
4162                 (message-options message-options)
4163                 (message-options-set-recipient)
4164                 (mail-parse-ignored-charsets
4165                  (or gnus-article-ignored-charsets
4166                      ',gnus-newsgroup-ignored-charsets)))
4167             (mml-to-mime)
4168             (mml-destroy-buffers)
4169             (remove-hook 'kill-buffer-hook
4170                          'mml-destroy-buffers t)
4171             (kill-local-variable 'mml-buffer-list))
4172           (gnus-summary-edit-article-done
4173            ,(or (mail-header-references gnus-current-headers) "")
4174            ,(gnus-group-read-only-p)
4175            ,gnus-summary-buffer no-highlight))))))
4176
4177 (defun gnus-mime-save-part ()
4178   "Save the MIME part under point."
4179   (interactive)
4180   (gnus-article-check-buffer)
4181   (let ((data (get-text-property (point) 'gnus-data)))
4182     (when data
4183       (mm-save-part data))))
4184
4185 (defun gnus-mime-pipe-part ()
4186   "Pipe the MIME part under point to a process."
4187   (interactive)
4188   (gnus-article-check-buffer)
4189   (let ((data (get-text-property (point) 'gnus-data)))
4190     (when data
4191       (mm-pipe-part data))))
4192
4193 (defun gnus-mime-view-part ()
4194   "Interactively choose a viewing method for the MIME part under point."
4195   (interactive)
4196   (gnus-article-check-buffer)
4197   (let ((data (get-text-property (point) 'gnus-data)))
4198     (when data
4199       (setq gnus-article-mime-handles
4200             (mm-merge-handles
4201              gnus-article-mime-handles (setq data (copy-sequence data))))
4202       (mm-interactively-view-part data))))
4203
4204 (defun gnus-mime-view-part-as-type-internal ()
4205   (gnus-article-check-buffer)
4206   (let* ((name (mail-content-type-get
4207                 (mm-handle-type (get-text-property (point) 'gnus-data))
4208                 'name))
4209          (def-type (and name (mm-default-file-encoding name))))
4210     (and def-type (cons def-type 0))))
4211
4212 (defun gnus-mime-view-part-as-type (&optional mime-type)
4213   "Choose a MIME media type, and view the part as such."
4214   (interactive)
4215   (unless mime-type
4216     (setq mime-type (completing-read
4217                      "View as MIME type: "
4218                      (mapcar #'list (mailcap-mime-types))
4219                      nil nil
4220                      (gnus-mime-view-part-as-type-internal))))
4221   (gnus-article-check-buffer)
4222   (let ((handle (get-text-property (point) 'gnus-data)))
4223     (when handle
4224       (setq handle
4225             (mm-make-handle (mm-handle-buffer handle)
4226                             (cons mime-type (cdr (mm-handle-type handle)))
4227                             (mm-handle-encoding handle)
4228                             (mm-handle-undisplayer handle)
4229                             (mm-handle-disposition handle)
4230                             (mm-handle-description handle)
4231                             nil
4232                             (mm-handle-id handle)))
4233       (setq gnus-article-mime-handles
4234             (mm-merge-handles gnus-article-mime-handles handle))
4235       (gnus-mm-display-part handle))))
4236
4237 (defun gnus-mime-copy-part (&optional handle)
4238   "Put the MIME part under point into a new buffer."
4239   (interactive)
4240   (gnus-article-check-buffer)
4241   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4242          (contents (and handle (mm-get-part handle)))
4243          (base (and handle
4244                     (file-name-nondirectory
4245                      (or
4246                       (mail-content-type-get (mm-handle-type handle) 'name)
4247                       (mail-content-type-get (mm-handle-type handle)
4248                                              'filename)
4249                       "*decoded*"))))
4250          (buffer (and base (generate-new-buffer base))))
4251     (when contents
4252       (switch-to-buffer buffer)
4253       (insert contents)
4254       ;; We do it this way to make `normal-mode' set the appropriate mode.
4255       (unwind-protect
4256           (progn
4257             (setq buffer-file-name (expand-file-name base))
4258             (normal-mode))
4259         (setq buffer-file-name nil))
4260       (goto-char (point-min)))))
4261
4262 (defun gnus-mime-print-part (&optional handle filename)
4263   "Print the MIME part under point."
4264   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4265   (gnus-article-check-buffer)
4266   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4267          (contents (and handle (mm-get-part handle)))
4268          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4269          (printer (mailcap-mime-info (mm-handle-type handle) "print")))
4270     (when contents
4271         (if printer
4272             (unwind-protect
4273                 (progn
4274                   (with-temp-file file
4275                     (insert contents))
4276                   (call-process shell-file-name nil
4277                                 (generate-new-buffer " *mm*")
4278                                 nil
4279                                 shell-command-switch
4280                                 (mm-mailcap-command
4281                                  printer file (mm-handle-type handle))))
4282               (delete-file file))
4283           (with-temp-buffer
4284             (insert contents)
4285             (gnus-print-buffer))
4286           (ps-despool filename)))))
4287
4288 (defun gnus-mime-inline-part (&optional handle arg)
4289   "Insert the MIME part under point into the current buffer."
4290   (interactive (list nil current-prefix-arg))
4291   (gnus-article-check-buffer)
4292   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4293          contents charset
4294          (b (point))
4295          buffer-read-only)
4296     (when handle
4297       (if (and (not arg) (mm-handle-undisplayer handle))
4298           (mm-remove-part handle)
4299         (setq contents (mm-get-part handle))
4300         (cond
4301          ((not arg)
4302           (setq charset (or (mail-content-type-get
4303                              (mm-handle-type handle) 'charset)
4304                             gnus-newsgroup-charset)))
4305          ((numberp arg)
4306           (if (mm-handle-undisplayer handle)
4307               (mm-remove-part handle))
4308           (setq charset
4309                 (or (cdr (assq arg
4310                                gnus-summary-show-article-charset-alist))
4311                     (mm-read-coding-system "Charset: ")))))
4312         (forward-line 2)
4313         (mm-insert-inline handle
4314                           (if (and charset
4315                                    (setq charset (mm-charset-to-coding-system
4316                                                   charset))
4317                                    (not (eq charset 'ascii)))
4318                               (mm-decode-coding-string contents charset)
4319                             contents))
4320         (goto-char b)))))
4321
4322 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4323   "Insert the MIME part under point into the current buffer using the
4324 specified charset."
4325   (interactive (list nil current-prefix-arg))
4326   (gnus-article-check-buffer)
4327   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4328          contents charset
4329          (b (point))
4330          buffer-read-only)
4331     (when handle
4332       (if (mm-handle-undisplayer handle)
4333           (mm-remove-part handle))
4334       (let ((gnus-newsgroup-charset
4335              (or (cdr (assq arg
4336                             gnus-summary-show-article-charset-alist))
4337                  (mm-read-coding-system "Charset: ")))
4338             (gnus-newsgroup-ignored-charsets 'gnus-all))
4339         (gnus-article-press-button)))))
4340
4341 (defun gnus-mime-view-part-externally (&optional handle)
4342   "View the MIME part under point with an external viewer."
4343   (interactive)
4344   (gnus-article-check-buffer)
4345   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4346          (mm-user-display-methods nil)
4347          (mm-inlined-types nil)
4348          (mail-parse-charset gnus-newsgroup-charset)
4349          (mail-parse-ignored-charsets
4350           (save-excursion (set-buffer gnus-summary-buffer)
4351                           gnus-newsgroup-ignored-charsets)))
4352     (when handle
4353       (if (mm-handle-undisplayer handle)
4354           (mm-remove-part handle)
4355         (mm-display-part handle)))))
4356
4357 (defun gnus-mime-view-part-internally (&optional handle)
4358   "View the MIME part under point with an internal viewer.
4359 If no internal viewer is available, use an external viewer."
4360   (interactive)
4361   (gnus-article-check-buffer)
4362   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4363          (mm-inlined-types '(".*"))
4364          (mm-inline-large-images t)
4365          (mail-parse-charset gnus-newsgroup-charset)
4366          (mail-parse-ignored-charsets
4367           (save-excursion (set-buffer gnus-summary-buffer)
4368                           gnus-newsgroup-ignored-charsets)))
4369     (when handle
4370       (if (mm-handle-undisplayer handle)
4371           (mm-remove-part handle)
4372         (mm-display-part handle)))))
4373
4374 (defun gnus-mime-action-on-part (&optional action)
4375   "Do something with the MIME attachment at \(point\)."
4376   (interactive
4377    (list (completing-read "Action: " gnus-mime-action-alist)))
4378   (gnus-article-check-buffer)
4379   (let ((action-pair (assoc action gnus-mime-action-alist)))
4380     (if action-pair
4381         (funcall (cdr action-pair)))))
4382
4383 (defun gnus-article-part-wrapper (n function)
4384   (save-current-buffer
4385     (set-buffer gnus-article-buffer)
4386     (when (> n (length gnus-article-mime-handle-alist))
4387       (error "No such part"))
4388     (gnus-article-goto-part n)
4389     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4390       (funcall function handle))))
4391
4392 (defun gnus-article-pipe-part (n)
4393   "Pipe MIME part N, which is the numerical prefix."
4394   (interactive "p")
4395   (gnus-article-part-wrapper n 'mm-pipe-part))
4396
4397 (defun gnus-article-save-part (n)
4398   "Save MIME part N, which is the numerical prefix."
4399   (interactive "p")
4400   (gnus-article-part-wrapper n 'mm-save-part))
4401
4402 (defun gnus-article-interactively-view-part (n)
4403   "View MIME part N interactively, which is the numerical prefix."
4404   (interactive "p")
4405   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4406
4407 (defun gnus-article-copy-part (n)
4408   "Copy MIME part N, which is the numerical prefix."
4409   (interactive "p")
4410   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4411
4412 (defun gnus-article-view-part-as-charset (n)
4413   "Copy MIME part N, which is the numerical prefix."
4414   (interactive "p")
4415   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4416
4417 (defun gnus-article-view-part-externally (n)
4418   "View MIME part N externally, which is the numerical prefix."
4419   (interactive "p")
4420   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4421
4422 (defun gnus-article-inline-part (n)
4423   "Inline MIME part N, which is the numerical prefix."
4424   (interactive "p")
4425   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4426
4427 (defun gnus-article-mime-match-handle-first (condition)
4428   (if condition
4429       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4430         (while (setq ihandle (pop alist))
4431           (if (and (cond
4432                     ((functionp condition)
4433                      (funcall condition (cdr ihandle)))
4434                     ((eq condition 'undisplayed)
4435                      (not (or (mm-handle-undisplayer (cdr ihandle))
4436                               (equal (mm-handle-media-type (cdr ihandle))
4437                                      "multipart/alternative"))))
4438                     ((eq condition 'undisplayed-alternative)
4439                      (not (mm-handle-undisplayer (cdr ihandle))))
4440                     (t t))
4441                    (gnus-article-goto-part (car ihandle))
4442                    (or (not n) (< (car ihandle) n)))
4443               (setq n (car ihandle))))
4444         (or n 1))
4445     1))
4446
4447 (defun gnus-article-view-part (&optional n)
4448   "View MIME part N, which is the numerical prefix."
4449   (interactive "P")
4450   (save-current-buffer
4451     (set-buffer gnus-article-buffer)
4452     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4453                              gnus-article-mime-match-handle-function)))
4454     (when (> n (length gnus-article-mime-handle-alist))
4455       (error "No such part"))
4456     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4457       (when (gnus-article-goto-part n)
4458         (if (equal (car handle) "multipart/alternative")
4459             (gnus-article-press-button)
4460           (when (eq (gnus-mm-display-part handle) 'internal)
4461             (gnus-set-window-start)))))))
4462
4463 (defsubst gnus-article-mime-total-parts ()
4464   (if (bufferp (car gnus-article-mime-handles))
4465       1 ;; single part
4466     (1- (length gnus-article-mime-handles))))
4467
4468 (defun gnus-mm-display-part (handle)
4469   "Display HANDLE and fix MIME button."
4470   (let ((id (get-text-property (point) 'gnus-part))
4471         (point (point))
4472         buffer-read-only)
4473     (forward-line 1)
4474     (prog1
4475         (let ((window (selected-window))
4476               (mail-parse-charset gnus-newsgroup-charset)
4477               (mail-parse-ignored-charsets
4478                (if (gnus-buffer-live-p gnus-summary-buffer)
4479                    (save-excursion
4480                      (set-buffer gnus-summary-buffer)
4481                      gnus-newsgroup-ignored-charsets)
4482                  nil)))
4483           (save-excursion
4484             (unwind-protect
4485                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4486                       (beg (point)))
4487                   (when win
4488                     (select-window win))
4489                   (goto-char point)
4490                   (forward-line)
4491                   (if (mm-handle-displayed-p handle)
4492                       ;; This will remove the part.
4493                       (mm-display-part handle)
4494                     (save-restriction
4495                       (narrow-to-region (point)
4496                                         (if (eobp) (point) (1+ (point))))
4497                       (mm-display-part handle)
4498                       ;; We narrow to the part itself and
4499                       ;; then call the treatment functions.
4500                       (goto-char (point-min))
4501                       (forward-line 1)
4502                       (narrow-to-region (point) (point-max))
4503                       (gnus-treat-article
4504                        nil id
4505                        (gnus-article-mime-total-parts)
4506                        (mm-handle-media-type handle)))))
4507               (if (window-live-p window)
4508                   (select-window window)))))
4509       (goto-char point)
4510       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4511       (gnus-insert-mime-button
4512        handle id (list (mm-handle-displayed-p handle)))
4513       (goto-char point))))
4514
4515 (defun gnus-article-goto-part (n)
4516   "Go to MIME part N."
4517   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4518     (when point
4519       (goto-char point))))
4520
4521 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4522   (let ((gnus-tmp-name
4523          (or (mail-content-type-get (mm-handle-type handle) 'name)
4524              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4525              (mail-content-type-get (mm-handle-type handle) 'url)
4526              ""))
4527         (gnus-tmp-type (mm-handle-media-type handle))
4528         (gnus-tmp-description
4529          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4530                                               "")))
4531         (gnus-tmp-dots
4532          (if (if displayed (car displayed)
4533                (mm-handle-displayed-p handle))
4534              "" "..."))
4535         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4536                            (buffer-size)))
4537         gnus-tmp-type-long b e)
4538     (when (string-match ".*/" gnus-tmp-name)
4539       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4540     (setq gnus-tmp-type-long (concat gnus-tmp-type
4541                                      (and (not (equal gnus-tmp-name ""))
4542                                           (concat "; " gnus-tmp-name))))
4543     (unless (equal gnus-tmp-description "")
4544       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4545     (unless (bolp)
4546       (insert "\n"))
4547     (setq b (point))
4548     (gnus-eval-format
4549      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4550      `(,@(gnus-local-map-property gnus-mime-button-map)
4551          gnus-callback gnus-mm-display-part
4552          gnus-part ,gnus-tmp-id
4553          article-type annotation
4554          gnus-data ,handle))
4555     (setq e (point))
4556     (widget-convert-button
4557      'link b e
4558      :mime-handle handle
4559      :action 'gnus-widget-press-button
4560      :button-keymap gnus-mime-button-map
4561      :help-echo
4562      (lambda (widget/window &optional overlay pos)
4563        ;; Needed to properly clear the message due to a bug in
4564        ;; wid-edit (XEmacs only).
4565        (if (boundp 'help-echo-owns-message)
4566            (setq help-echo-owns-message t))
4567        (format
4568         "%S: %s the MIME part; %S: more options"
4569         (aref gnus-mouse-2 0)
4570         ;; XEmacs will get a single widget arg; Emacs 21 will get
4571         ;; window, overlay, position.
4572         (if (mm-handle-displayed-p
4573              (if overlay
4574                  (with-current-buffer (gnus-overlay-buffer overlay)
4575                    (widget-get (widget-at (gnus-overlay-start overlay))
4576                                :mime-handle))
4577                (widget-get widget/window :mime-handle)))
4578             "hide" "show")
4579         (aref gnus-down-mouse-3 0))))))
4580
4581 (defun gnus-widget-press-button (elems el)
4582   (goto-char (widget-get elems :from))
4583   (gnus-article-press-button))
4584
4585 (defvar gnus-displaying-mime nil)
4586
4587 (defun gnus-display-mime (&optional ihandles)
4588   "Display the MIME parts."
4589   (save-excursion
4590     (save-selected-window
4591       (let ((window (get-buffer-window gnus-article-buffer))
4592             (point (point)))
4593         (when window
4594           (select-window window)
4595           ;; We have to do this since selecting the window
4596           ;; may change the point.  So we set the window point.
4597           (set-window-point window point)))
4598       (let* ((handles (or ihandles (mm-dissect-buffer
4599                                     nil gnus-article-loose-mime)
4600                           (mm-uu-dissect)))
4601              buffer-read-only handle name type b e display)
4602         (when (and (not ihandles)
4603                    (not gnus-displaying-mime))
4604           ;; Top-level call; we clean up.
4605           (when gnus-article-mime-handles
4606             (mm-destroy-parts gnus-article-mime-handles)
4607             (setq gnus-article-mime-handle-alist nil));; A trick.
4608           (setq gnus-article-mime-handles handles)
4609           ;; We allow users to glean info from the handles.
4610           (when gnus-article-mime-part-function
4611             (gnus-mime-part-function handles)))
4612         (if (and handles
4613                  (or (not (stringp (car handles)))
4614                      (cdr handles)))
4615             (progn
4616               (when (and (not ihandles)
4617                          (not gnus-displaying-mime))
4618                 ;; Clean up for mime parts.
4619                 (article-goto-body)
4620                 (delete-region (point) (point-max)))
4621               (let ((gnus-displaying-mime t))
4622                 (gnus-mime-display-part handles)))
4623           (save-restriction
4624             (article-goto-body)
4625             (narrow-to-region (point) (point-max))
4626             (gnus-treat-article nil 1 1)
4627             (widen)))
4628         (unless ihandles
4629           ;; Highlight the headers.
4630           (save-excursion
4631             (save-restriction
4632               (article-goto-body)
4633               (narrow-to-region (point-min) (point))
4634               (gnus-treat-article 'head))))))))
4635
4636 (defvar gnus-mime-display-multipart-as-mixed nil)
4637 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4638 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4639
4640 (defun gnus-mime-display-part (handle)
4641   (cond
4642    ;; Single part.
4643    ((not (stringp (car handle)))
4644     (gnus-mime-display-single handle))
4645    ;; User-defined multipart
4646    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4647     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4648              handle))
4649    ;; multipart/alternative
4650    ((and (equal (car handle) "multipart/alternative")
4651          (not (or gnus-mime-display-multipart-as-mixed
4652                   gnus-mime-display-multipart-alternative-as-mixed)))
4653     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4654       (push (cons id handle) gnus-article-mime-handle-alist)
4655       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4656    ;; multipart/related
4657    ((and (equal (car handle) "multipart/related")
4658          (not (or gnus-mime-display-multipart-as-mixed
4659                   gnus-mime-display-multipart-related-as-mixed)))
4660     ;;;!!!We should find the start part, but we just default
4661     ;;;!!!to the first part.
4662     ;;(gnus-mime-display-part (cadr handle))
4663     ;;;!!! Most multipart/related is an HTML message plus images.
4664     ;;;!!! Unfortunately we are unable to let W3 display those
4665     ;;;!!! included images, so we just display it as a mixed multipart.
4666     ;;(gnus-mime-display-mixed (cdr handle))
4667     ;;;!!! No, w3 can display everything just fine.
4668     (gnus-mime-display-part (cadr handle)))
4669    ((equal (car handle) "multipart/signed")
4670     (gnus-add-wash-type 'signed)
4671     (gnus-mime-display-security handle))
4672    ((equal (car handle) "multipart/encrypted")
4673     (gnus-add-wash-type 'encrypted)
4674     (gnus-mime-display-security handle))
4675    ;; Other multiparts are handled like multipart/mixed.
4676    (t
4677     (gnus-mime-display-mixed (cdr handle)))))
4678
4679 (defun gnus-mime-part-function (handles)
4680   (if (stringp (car handles))
4681       (mapcar 'gnus-mime-part-function (cdr handles))
4682     (funcall gnus-article-mime-part-function handles)))
4683
4684 (defun gnus-mime-display-mixed (handles)
4685   (mapcar 'gnus-mime-display-part handles))
4686
4687 (defun gnus-mime-display-single (handle)
4688   (let ((type (mm-handle-media-type handle))
4689         (ignored gnus-ignored-mime-types)
4690         (not-attachment t)
4691         (move nil)
4692         display text)
4693     (catch 'ignored
4694       (progn
4695         (while ignored
4696           (when (string-match (pop ignored) type)
4697             (throw 'ignored nil)))
4698         (if (and (setq not-attachment
4699                        (and (not (mm-inline-override-p handle))
4700                             (or (not (mm-handle-disposition handle))
4701                                 (equal (car (mm-handle-disposition handle))
4702                                        "inline")
4703                                 (mm-attachment-override-p handle))))
4704                  (mm-automatic-display-p handle)
4705                  (or (and
4706                       (mm-inlinable-p handle)
4707                       (mm-inlined-p handle))
4708                      (mm-automatic-external-display-p type)))
4709             (setq display t)
4710           (when (equal (mm-handle-media-supertype handle) "text")
4711             (setq text t)))
4712         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4713               beg)
4714           (push (cons id handle) gnus-article-mime-handle-alist)
4715           (when (or (not display)
4716                     (not (gnus-unbuttonized-mime-type-p type)))
4717             ;(gnus-article-insert-newline)
4718             (gnus-insert-mime-button
4719              handle id (list (or display (and not-attachment text))))
4720             (gnus-article-insert-newline)
4721             ;(gnus-article-insert-newline)
4722             ;; Remember modify the number of forward lines.
4723             (setq move t))
4724           (setq beg (point))
4725           (cond
4726            (display
4727             (when move
4728               (forward-line -1)
4729               (setq beg (point)))
4730             (let ((mail-parse-charset gnus-newsgroup-charset)
4731                   (mail-parse-ignored-charsets
4732                    (save-excursion (condition-case ()
4733                                        (set-buffer gnus-summary-buffer)
4734                                      (error))
4735                                    gnus-newsgroup-ignored-charsets)))
4736               (mm-display-part handle t))
4737             (goto-char (point-max)))
4738            ((and text not-attachment)
4739             (when move
4740               (forward-line -1)
4741               (setq beg (point)))
4742             (gnus-article-insert-newline)
4743             (mm-insert-inline handle (mm-get-part handle))
4744             (goto-char (point-max))))
4745           ;; Do highlighting.
4746           (save-excursion
4747             (save-restriction
4748               (narrow-to-region beg (point))
4749               (gnus-treat-article
4750                nil id
4751                (gnus-article-mime-total-parts)
4752                (mm-handle-media-type handle)))))))))
4753
4754 (defun gnus-unbuttonized-mime-type-p (type)
4755   "Say whether TYPE is to be unbuttonized."
4756   (unless gnus-inhibit-mime-unbuttonizing
4757     (when (catch 'found
4758             (let ((types gnus-unbuttonized-mime-types))
4759               (while types
4760                 (when (string-match (pop types) type)
4761                   (throw 'found t)))))
4762       (not (catch 'found
4763              (let ((types gnus-buttonized-mime-types))
4764                (while types
4765                  (when (string-match (pop types) type)
4766                    (throw 'found t)))))))))
4767
4768 (defun gnus-article-insert-newline ()
4769   "Insert a newline, but mark it as undeletable."
4770   (gnus-put-text-property
4771    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4772
4773 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4774   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4775          (ihandles handles)
4776          (point (point))
4777          handle buffer-read-only from props begend not-pref)
4778     (save-window-excursion
4779       (save-restriction
4780         (when ibegend
4781           (narrow-to-region (car ibegend)
4782                             (or (cdr ibegend)
4783                                 (progn
4784                                   (goto-char (car ibegend))
4785                                   (forward-line 2)
4786                                   (point))))
4787           (delete-region (point-min) (point-max))
4788           (mm-remove-parts handles))
4789         (setq begend (list (point-marker)))
4790         ;; Do the toggle.
4791         (unless (setq not-pref (cadr (member preferred ihandles)))
4792           (setq not-pref (car ihandles)))
4793         (when (or ibegend
4794                   (not preferred)
4795                   (not (gnus-unbuttonized-mime-type-p
4796                         "multipart/alternative")))
4797           (gnus-add-text-properties
4798            (setq from (point))
4799            (progn
4800              (insert (format "%d.  " id))
4801              (point))
4802            `(gnus-callback
4803              (lambda (handles)
4804                (unless ,(not ibegend)
4805                  (setq gnus-article-mime-handle-alist
4806                        ',gnus-article-mime-handle-alist))
4807                (gnus-mime-display-alternative
4808                 ',ihandles ',not-pref ',begend ,id))
4809              ,@(gnus-local-map-property gnus-mime-button-map)
4810              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4811              face ,gnus-article-button-face
4812              gnus-part ,id
4813              gnus-data ,handle))
4814           (widget-convert-button 'link from (point)
4815                                  :action 'gnus-widget-press-button
4816                                  :button-keymap gnus-widget-button-keymap)
4817           ;; Do the handles
4818           (while (setq handle (pop handles))
4819             (gnus-add-text-properties
4820              (setq from (point))
4821              (progn
4822                (insert (format "(%c) %-18s"
4823                                (if (equal handle preferred) ?* ? )
4824                                (mm-handle-media-type handle)))
4825                (point))
4826              `(gnus-callback
4827                (lambda (handles)
4828                  (unless ,(not ibegend)
4829                    (setq gnus-article-mime-handle-alist
4830                          ',gnus-article-mime-handle-alist))
4831                  (gnus-mime-display-alternative
4832                   ',ihandles ',handle ',begend ,id))
4833                ,@(gnus-local-map-property gnus-mime-button-map)
4834                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4835                face ,gnus-article-button-face
4836                gnus-part ,id
4837                gnus-data ,handle))
4838             (widget-convert-button 'link from (point)
4839                                    :action 'gnus-widget-press-button
4840                                    :button-keymap gnus-widget-button-keymap)
4841             (insert "  "))
4842           (insert "\n\n"))
4843         (when preferred
4844           (if (stringp (car preferred))
4845               (gnus-display-mime preferred)
4846             (let ((mail-parse-charset gnus-newsgroup-charset)
4847                   (mail-parse-ignored-charsets
4848                    (save-excursion (set-buffer gnus-summary-buffer)
4849                                    gnus-newsgroup-ignored-charsets)))
4850               (mm-display-part preferred)
4851               ;; Do highlighting.
4852               (save-excursion
4853                 (save-restriction
4854                   (narrow-to-region (car begend) (point-max))
4855                   (gnus-treat-article
4856                    nil (length gnus-article-mime-handle-alist)
4857                    (gnus-article-mime-total-parts)
4858                    (mm-handle-media-type handle))))))
4859           (goto-char (point-max))
4860           (setcdr begend (point-marker)))))
4861     (when ibegend
4862       (goto-char point))))
4863
4864 (defconst gnus-article-wash-status-strings
4865   (let ((alist '((cite "c" "Possible hidden citation text"
4866                        " " "All citation text visible")
4867                  (headers "h" "Hidden headers"
4868                           " " "All headers visible.")
4869                  (pgp "p" "Encrypted or signed message status hidden"
4870                       " " "No hidden encryption nor digital signature status")
4871                  (signature "s" "Signature has been hidden"
4872                             " " "Signature is visible")
4873                  (overstrike "o" "Overstrike (^H) characters applied"
4874                              " " "No overstrike characters applied")
4875                  (gnus-show-mime "m" "Mime processing is activated"
4876                                  " " "Mime processing is not activated")
4877                  (emphasis "e" "/*_Emphasis_*/ characters applied"
4878                            " " "No /*_emphasis_*/ characters applied")))
4879         result)
4880     (dolist (entry alist result)
4881       (let ((key (nth 0 entry))
4882             (on (copy-sequence (nth 1 entry)))
4883             (on-help (nth 2 entry))
4884             (off (copy-sequence (nth 3 entry)))
4885             (off-help (nth 4 entry)))
4886         (put-text-property 0 1 'help-echo on-help on)
4887         (put-text-property 0 1 'help-echo off-help off)
4888         (push (list key on off) result))))
4889   "Alist of strings describing wash status in the mode line.
4890 Each entry has the form (KEY ON OF), where the KEY is a symbol
4891 representing the particular washing function, ON is the string to use
4892 in the article mode line when the washing function is active, and OFF
4893 is the string to use when it is inactive.")
4894
4895 (defun gnus-article-wash-status-entry (key value)
4896   (let ((entry (assoc key gnus-article-wash-status-strings)))
4897     (if value (nth 1 entry) (nth 2 entry))))
4898
4899 (defun gnus-article-wash-status ()
4900   "Return a string which display status of article washing."
4901   (save-excursion
4902     (set-buffer gnus-article-buffer)
4903     (let ((cite (memq 'cite gnus-article-wash-types))
4904           (headers (memq 'headers gnus-article-wash-types))
4905           (boring (memq 'boring-headers gnus-article-wash-types))
4906           (pgp (memq 'pgp gnus-article-wash-types))
4907           (pem (memq 'pem gnus-article-wash-types))
4908           (signed (memq 'signed gnus-article-wash-types))
4909           (encrypted (memq 'encrypted gnus-article-wash-types))
4910           (signature (memq 'signature gnus-article-wash-types))
4911           (overstrike (memq 'overstrike gnus-article-wash-types))
4912           (emphasis (memq 'emphasis gnus-article-wash-types)))
4913       (concat
4914        (gnus-article-wash-status-entry 'cite cite)
4915        (gnus-article-wash-status-entry 'headers (or headers boring))
4916        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
4917        (gnus-article-wash-status-entry 'signature signature)
4918        (gnus-article-wash-status-entry 'overstrike overstrike)
4919        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
4920        (gnus-article-wash-status-entry 'emphasis emphasis)))))
4921
4922 (defun gnus-add-wash-type (type)
4923   "Add a washing of TYPE to the current status."
4924   (add-to-list 'gnus-article-wash-types type))
4925
4926 (defun gnus-delete-wash-type (type)
4927   "Add a washing of TYPE to the current status."
4928   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
4929
4930 (defun gnus-add-image (category image)
4931   "Add IMAGE of CATEGORY to the list of displayed images."
4932   (let ((entry (assq category gnus-article-image-alist)))
4933     (unless entry
4934       (setq entry (list category))
4935       (push entry gnus-article-image-alist))
4936     (nconc entry (list image))))
4937
4938 (defun gnus-delete-images (category)
4939   "Delete all images in CATEGORY."
4940   (let ((entry (assq category gnus-article-image-alist)))
4941     (dolist (image (cdr entry))
4942       (gnus-remove-image image))
4943     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
4944     (gnus-delete-wash-type category)))
4945
4946 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4947
4948 (defun gnus-article-maybe-hide-headers ()
4949   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4950 Provided for backwards compatibility."
4951   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4952                  (not (save-excursion (set-buffer gnus-summary-buffer)
4953                                       gnus-have-all-headers)))
4954              (not gnus-inhibit-hiding))
4955     (gnus-article-hide-headers)))
4956
4957 ;;; Article savers.
4958
4959 (defun gnus-output-to-file (file-name)
4960   "Append the current article to a file named FILE-NAME."
4961   (let ((artbuf (current-buffer)))
4962     (with-temp-buffer
4963       (insert-buffer-substring artbuf)
4964       ;; Append newline at end of the buffer as separator, and then
4965       ;; save it to file.
4966       (goto-char (point-max))
4967       (insert "\n")
4968       (let ((file-name-coding-system nnmail-pathname-coding-system)
4969             (pathname-coding-system nnmail-pathname-coding-system))
4970         (write-region-as-binary (point-min) (point-max) file-name 'append))
4971       t)))
4972
4973 (defun gnus-narrow-to-page (&optional arg)
4974   "Narrow the article buffer to a page.
4975 If given a numerical ARG, move forward ARG pages."
4976   (interactive "P")
4977   (setq arg (if arg (prefix-numeric-value arg) 0))
4978   (save-excursion
4979     (set-buffer gnus-article-buffer)
4980     (goto-char (point-min))
4981     (widen)
4982     ;; Remove any old next/prev buttons.
4983     (when (gnus-visual-p 'page-marker)
4984       (let ((buffer-read-only nil))
4985         (gnus-remove-text-with-property 'gnus-prev)
4986         (gnus-remove-text-with-property 'gnus-next)))
4987     (when
4988         (cond ((< arg 0)
4989                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4990               ((> arg 0)
4991                (re-search-forward page-delimiter nil 'move arg)))
4992       (goto-char (match-end 0)))
4993     (narrow-to-region
4994      (point)
4995      (if (re-search-forward page-delimiter nil 'move)
4996          (match-beginning 0)
4997        (point)))
4998     (when (and (gnus-visual-p 'page-marker)
4999                (not (= (point-min) 1)))
5000       (save-excursion
5001         (goto-char (point-min))
5002         (gnus-insert-prev-page-button)))
5003     (when (and (gnus-visual-p 'page-marker)
5004                (< (+ (point-max) 2) (buffer-size)))
5005       (save-excursion
5006         (goto-char (point-max))
5007         (gnus-insert-next-page-button)))))
5008
5009 ;; Article mode commands
5010
5011 (defun gnus-article-goto-next-page ()
5012   "Show the next page of the article."
5013   (interactive)
5014   (when (gnus-article-next-page)
5015     (goto-char (point-min))
5016     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5017
5018 (defun gnus-article-goto-prev-page ()
5019   "Show the next page of the article."
5020   (interactive)
5021   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5022     (gnus-article-prev-page nil)))
5023
5024 (defun gnus-article-next-page (&optional lines)
5025   "Show the next page of the current article.
5026 If end of article, return non-nil.  Otherwise return nil.
5027 Argument LINES specifies lines to be scrolled up."
5028   (interactive "p")
5029   (let ((start (window-start))
5030         end-of-buffer end-of-page)
5031     (save-excursion
5032       (move-to-window-line -1)
5033       (if (<= (point) start)
5034           (progn
5035             (forward-line 2)
5036             (setq start (point)))
5037         (forward-line 1)
5038         (setq start nil))
5039       (unless (or (cond ((eq (1+ (buffer-size)) (point))
5040                          (and (pos-visible-in-window-p)
5041                               (setq end-of-buffer t)))
5042                         ((eobp)
5043                          (setq end-of-page t)))
5044                   (not lines))
5045         (move-to-window-line lines)
5046         (unless (search-backward "\n\n" nil t)
5047           (setq start (point)))))
5048     (cond (end-of-buffer t)
5049           (end-of-page
5050            (gnus-narrow-to-page 1)
5051            nil)
5052           (t
5053            (if start
5054                (set-window-start (selected-window) start)
5055              (let (window-pixel-scroll-increment)
5056                (scroll-up lines)))
5057            nil))))
5058
5059 (defun gnus-article-prev-page (&optional lines)
5060   "Show previous page of current article.
5061 Argument LINES specifies lines to be scrolled down."
5062   (interactive "p")
5063   (let (beginning-of-buffer beginning-of-page)
5064     (save-excursion
5065       (move-to-window-line 0)
5066       (cond ((eq 1 (point))
5067              (setq beginning-of-buffer t))
5068             ((bobp)
5069              (setq beginning-of-page t))))
5070     (cond (beginning-of-buffer)
5071           (beginning-of-page
5072            (gnus-narrow-to-page -1))
5073           (t
5074            (condition-case nil
5075                (let (window-pixel-scroll-increment)
5076                  (scroll-down lines))
5077              (beginning-of-buffer
5078               (goto-char (point-min))))))))
5079
5080 (defun gnus-article-refer-article ()
5081   "Read article specified by message-id around point."
5082   (interactive)
5083   (let ((point (point)))
5084     (search-forward ">" nil t)          ;Move point to end of "<....>".
5085     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
5086         (let ((message-id (match-string 1)))
5087           (goto-char point)
5088           (set-buffer gnus-summary-buffer)
5089           (gnus-summary-refer-article message-id))
5090       (goto-char (point))
5091       (error "No references around point"))))
5092
5093 (defun gnus-article-show-summary ()
5094   "Reconfigure windows to show summary buffer."
5095   (interactive)
5096   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5097       (error "There is no summary buffer for this article buffer")
5098     (gnus-article-set-globals)
5099     (gnus-configure-windows 'article)
5100     (gnus-summary-goto-subject gnus-current-article)
5101     (gnus-summary-position-point)))
5102
5103 (defun gnus-article-describe-briefly ()
5104   "Describe article mode commands briefly."
5105   (interactive)
5106   (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")))
5107
5108 (defun gnus-article-summary-command ()
5109   "Execute the last keystroke in the summary buffer."
5110   (interactive)
5111   (let ((obuf (current-buffer))
5112         (owin (current-window-configuration))
5113         func)
5114     (switch-to-buffer gnus-article-current-summary 'norecord)
5115     (setq func (lookup-key (current-local-map) (this-command-keys)))
5116     (call-interactively func)
5117     (set-buffer obuf)
5118     (set-window-configuration owin)
5119     (set-window-point (get-buffer-window (current-buffer)) (point))))
5120
5121 (defun gnus-article-summary-command-nosave ()
5122   "Execute the last keystroke in the summary buffer."
5123   (interactive)
5124   (let (func)
5125     (pop-to-buffer gnus-article-current-summary 'norecord)
5126     (setq func (lookup-key (current-local-map) (this-command-keys)))
5127     (call-interactively func)))
5128
5129 (defun gnus-article-check-buffer ()
5130   "Beep if not in an article buffer."
5131   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5132     (error "Command invoked outside of a Gnus article buffer")))
5133
5134 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5135   "Read a summary buffer key sequence and execute it from the article buffer."
5136   (interactive "P")
5137   (gnus-article-check-buffer)
5138   (let ((nosaves
5139          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5140            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5141            "=" "^" "\M-^" "|"))
5142         (nosave-but-article
5143          '("A\r"))
5144         (nosave-in-article
5145          '("\C-d"))
5146         (up-to-top
5147          '("n" "Gn" "p" "Gp"))
5148         keys new-sum-point)
5149     (save-excursion
5150       (set-buffer gnus-article-current-summary)
5151       (let (gnus-pick-mode)
5152         (push (or key last-command-event) unread-command-events)
5153         (setq keys (static-if (featurep 'xemacs)
5154                        (events-to-keys (read-key-sequence nil))
5155                      (read-key-sequence nil)))))
5156     (message "")
5157
5158     (if (or (member keys nosaves)
5159             (member keys nosave-but-article)
5160             (member keys nosave-in-article))
5161         (let (func)
5162           (save-window-excursion
5163             (pop-to-buffer gnus-article-current-summary 'norecord)
5164             ;; We disable the pick minor mode commands.
5165             (let (gnus-pick-mode)
5166               (setq func (lookup-key (current-local-map) keys))))
5167           (if (or (not func)
5168                   (numberp func))
5169               (ding)
5170             (unless (member keys nosave-in-article)
5171               (set-buffer gnus-article-current-summary))
5172             (call-interactively func)
5173             (setq new-sum-point (point)))
5174           (when (member keys nosave-but-article)
5175             (pop-to-buffer gnus-article-buffer 'norecord)))
5176       ;; These commands should restore window configuration.
5177       (let ((obuf (current-buffer))
5178             (owin (current-window-configuration))
5179             (opoint (point))
5180             (summary gnus-article-current-summary)
5181             func in-buffer selected)
5182         (if not-restore-window
5183             (pop-to-buffer summary 'norecord)
5184           (switch-to-buffer summary 'norecord))
5185         (setq in-buffer (current-buffer))
5186         ;; We disable the pick minor mode commands.
5187         (if (and (setq func (let (gnus-pick-mode)
5188                               (lookup-key (current-local-map) keys)))
5189                  (functionp func))
5190             (progn
5191               (call-interactively func)
5192               (setq new-sum-point (point))
5193               (when (eq in-buffer (current-buffer))
5194                 (setq selected (gnus-summary-select-article))
5195                 (set-buffer obuf)
5196                 (unless not-restore-window
5197                   (set-window-configuration owin))
5198                 (when (eq selected 'old)
5199                   (article-goto-body)
5200                   (set-window-start (get-buffer-window (current-buffer))
5201                                     1)
5202                   (set-window-point (get-buffer-window (current-buffer))
5203                                     (point)))
5204                 (let ((win (get-buffer-window gnus-article-current-summary)))
5205                   (when win
5206                     (set-window-point win new-sum-point))))    )
5207           (switch-to-buffer gnus-article-buffer)
5208           (ding))))))
5209
5210 (defun gnus-article-describe-key (key)
5211   "Display documentation of the function invoked by KEY.  KEY is a string."
5212   (interactive "kDescribe key: ")
5213   (gnus-article-check-buffer)
5214   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5215       (save-excursion
5216         (set-buffer gnus-article-current-summary)
5217         (let (gnus-pick-mode)
5218           (if (featurep 'xemacs)
5219               (progn
5220                 (push (elt key 0) unread-command-events)
5221                 (setq key (events-to-keys
5222                            (read-key-sequence "Describe key: "))))
5223             (setq unread-command-events
5224                   (mapcar
5225                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5226                    key))
5227             (setq key (read-key-sequence "Describe key: "))))
5228         (describe-key key))
5229     (describe-key key)))
5230
5231 (defun gnus-article-describe-key-briefly (key &optional insert)
5232   "Display documentation of the function invoked by KEY.  KEY is a string."
5233   (interactive "kDescribe key: \nP")
5234   (gnus-article-check-buffer)
5235   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5236       (save-excursion
5237         (set-buffer gnus-article-current-summary)
5238         (let (gnus-pick-mode)
5239           (if (featurep 'xemacs)
5240               (progn
5241                 (push (elt key 0) unread-command-events)
5242                 (setq key (events-to-keys
5243                            (read-key-sequence "Describe key: "))))
5244             (setq unread-command-events
5245                   (mapcar
5246                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5247                    key))
5248             (setq key (read-key-sequence "Describe key: "))))
5249         (describe-key-briefly key insert))
5250     (describe-key-briefly key insert)))
5251
5252 (defun gnus-article-reply-with-original (&optional wide)
5253   "Start composing a reply mail to the current message.
5254 The text in the region will be yanked.  If the region isn't active,
5255 the entire article will be yanked."
5256   (interactive "P")
5257   (let ((article (cdr gnus-article-current)) cont)
5258     (if (not (mark t))
5259         (gnus-summary-reply (list (list article)) wide)
5260       (setq cont (buffer-substring (point) (mark t)))
5261       ;; Deactivate active regions.
5262       (when (and (boundp 'transient-mark-mode)
5263                  transient-mark-mode)
5264         (setq mark-active nil))
5265       (gnus-summary-reply
5266        (list (list article cont)) wide))))
5267
5268 (defun gnus-article-followup-with-original ()
5269   "Compose a followup to the current article.
5270 The text in the region will be yanked.  If the region isn't active,
5271 the entire article will be yanked."
5272   (interactive)
5273   (let ((article (cdr gnus-article-current))
5274         cont)
5275     (if (not (mark t))
5276         (gnus-summary-followup (list (list article)))
5277       (setq cont (buffer-substring (point) (mark t)))
5278       ;; Deactivate active regions.
5279       (when (and (boundp 'transient-mark-mode)
5280                  transient-mark-mode)
5281         (setq mark-active nil))
5282       (gnus-summary-followup
5283        (list (list article cont))))))
5284
5285 (defun gnus-article-hide (&optional arg force)
5286   "Hide all the gruft in the current article.
5287 This means that PGP stuff, signatures, cited text and (some)
5288 headers will be hidden.
5289 If given a prefix, show the hidden text instead."
5290   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5291   (gnus-article-hide-headers arg)
5292   (gnus-article-hide-list-identifiers arg)
5293   (gnus-article-hide-pgp arg)
5294   (gnus-article-hide-citation-maybe arg force)
5295   (gnus-article-hide-signature arg))
5296
5297 (defun gnus-article-maybe-highlight ()
5298   "Do some article highlighting if article highlighting is requested."
5299   (when (gnus-visual-p 'article-highlight 'highlight)
5300     (gnus-article-highlight-some)))
5301
5302 (defun gnus-check-group-server ()
5303   ;; Make sure the connection to the server is alive.
5304   (unless (gnus-server-opened
5305            (gnus-find-method-for-group gnus-newsgroup-name))
5306     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5307     (gnus-request-group gnus-newsgroup-name t)))
5308
5309 (eval-when-compile
5310   (autoload 'nneething-get-file-name "nneething"))
5311
5312 (defun gnus-request-article-this-buffer (article group)
5313   "Get an article and insert it into this buffer."
5314   (let (do-update-line sparse-header)
5315     (prog1
5316         (save-excursion
5317           (erase-buffer)
5318           (gnus-kill-all-overlays)
5319           (setq group (or group gnus-newsgroup-name))
5320
5321           ;; Using `gnus-request-article' directly will insert the article into
5322           ;; `nntp-server-buffer' - so we'll save some time by not having to
5323           ;; copy it from the server buffer into the article buffer.
5324
5325           ;; We only request an article by message-id when we do not have the
5326           ;; headers for it, so we'll have to get those.
5327           (when (stringp article)
5328             (gnus-read-header article))
5329
5330           ;; If the article number is negative, that means that this article
5331           ;; doesn't belong in this newsgroup (possibly), so we find its
5332           ;; message-id and request it by id instead of number.
5333           (when (and (numberp article)
5334                      gnus-summary-buffer
5335                      (get-buffer gnus-summary-buffer)
5336                      (gnus-buffer-exists-p gnus-summary-buffer))
5337             (save-excursion
5338               (set-buffer gnus-summary-buffer)
5339               (let ((header (gnus-summary-article-header article)))
5340                 (when (< article 0)
5341                   (cond
5342                    ((memq article gnus-newsgroup-sparse)
5343                     ;; This is a sparse gap article.
5344                     (setq do-update-line article)
5345                     (setq article (mail-header-id header))
5346                     (setq sparse-header (gnus-read-header article))
5347                     (setq gnus-newsgroup-sparse
5348                           (delq article gnus-newsgroup-sparse)))
5349                    ((vectorp header)
5350                     ;; It's a real article.
5351                     (setq article (mail-header-id header)))
5352                    (t
5353                     ;; It is an extracted pseudo-article.
5354                     (setq article 'pseudo)
5355                     (gnus-request-pseudo-article header))))
5356
5357                 (let ((method (gnus-find-method-for-group
5358                                gnus-newsgroup-name)))
5359                   (when (and (eq (car method) 'nneething)
5360                              (vectorp header))
5361                     (let ((dir (nneething-get-file-name
5362                                 (mail-header-id header))))
5363                       (when (and (stringp dir)
5364                                  (file-directory-p dir))
5365                         (setq article 'nneething)
5366                         (gnus-group-enter-directory dir))))))))
5367
5368           (cond
5369            ;; Refuse to select canceled articles.
5370            ((and (numberp article)
5371                  gnus-summary-buffer
5372                  (get-buffer gnus-summary-buffer)
5373                  (gnus-buffer-exists-p gnus-summary-buffer)
5374                  (eq (cdr (save-excursion
5375                             (set-buffer gnus-summary-buffer)
5376                             (assq article gnus-newsgroup-reads)))
5377                      gnus-canceled-mark))
5378             nil)
5379            ;; We first check `gnus-original-article-buffer'.
5380            ((and (get-buffer gnus-original-article-buffer)
5381                  (numberp article)
5382                  (save-excursion
5383                    (set-buffer gnus-original-article-buffer)
5384                    (and (equal (car gnus-original-article) group)
5385                         (eq (cdr gnus-original-article) article))))
5386             (insert-buffer-substring gnus-original-article-buffer)
5387             'article)
5388            ;; Check the backlog.
5389            ((and gnus-keep-backlog
5390                  (gnus-backlog-request-article group article (current-buffer)))
5391             'article)
5392            ;; Check asynchronous pre-fetch.
5393            ((gnus-async-request-fetched-article group article (current-buffer))
5394             (gnus-async-prefetch-next group article gnus-summary-buffer)
5395             (when (and (numberp article) gnus-keep-backlog)
5396               (gnus-backlog-enter-article group article (current-buffer)))
5397             'article)
5398            ;; Check the cache.
5399            ((and gnus-use-cache
5400                  (numberp article)
5401                  (gnus-cache-request-article article group))
5402             'article)
5403            ;; Check the agent cache.
5404            ((and gnus-agent gnus-agent-cache gnus-plugged
5405                  (numberp article)
5406                  (gnus-agent-request-article article group))
5407             'article)
5408            ;; Get the article and put into the article buffer.
5409            ((or (stringp article)
5410                 (numberp article))
5411             (let ((gnus-override-method gnus-override-method)
5412                   (methods (and (stringp article)
5413                                 gnus-refer-article-method))
5414                   (backend (car (gnus-find-method-for-group
5415                                  gnus-newsgroup-name)))
5416                   result
5417                   (buffer-read-only nil))
5418               (if (or (not (listp methods))
5419                       (and (symbolp (car methods))
5420                            (assq (car methods) nnoo-definition-alist)))
5421                   (setq methods (list methods)))
5422               (when (and (null gnus-override-method)
5423                          methods)
5424                 (setq gnus-override-method (pop methods)))
5425               (while (not result)
5426                 (when (eq gnus-override-method 'current)
5427                   (setq gnus-override-method
5428                         (with-current-buffer gnus-summary-buffer
5429                           gnus-current-select-method)))
5430                 (erase-buffer)
5431                 (gnus-kill-all-overlays)
5432                 (let ((gnus-newsgroup-name group))
5433                   (gnus-check-group-server))
5434                 (cond
5435                  ((gnus-request-article article group (current-buffer))
5436                   (when (numberp article)
5437                     (gnus-async-prefetch-next group article
5438                                               gnus-summary-buffer)
5439                     (when gnus-keep-backlog
5440                       (gnus-backlog-enter-article
5441                        group article (current-buffer))))
5442                   (setq result 'article))
5443                  (methods
5444                   (setq gnus-override-method (pop methods)))
5445                  ((not (string-match "^400 "
5446                                      (nnheader-get-report backend)))
5447                   ;; If we get 400 server disconnect, reconnect and
5448                   ;; retry; otherwise, assume the article has expired.
5449                   (setq result 'done))))
5450               (and (eq result 'article) 'article)))
5451            ;; It was a pseudo.
5452            (t article)))
5453
5454       ;; Associate this article with the current summary buffer.
5455       (setq gnus-article-current-summary gnus-summary-buffer)
5456
5457       ;; Take the article from the original article buffer
5458       ;; and place it in the buffer it's supposed to be in.
5459       (when (and (get-buffer gnus-article-buffer)
5460                  (equal (buffer-name (current-buffer))
5461                         (buffer-name (get-buffer gnus-article-buffer))))
5462         (save-excursion
5463           (if (get-buffer gnus-original-article-buffer)
5464               (set-buffer gnus-original-article-buffer)
5465             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5466             (buffer-disable-undo)
5467             (setq major-mode 'gnus-original-article-mode)
5468             (setq buffer-read-only t))
5469           (let (buffer-read-only)
5470             (erase-buffer)
5471             (insert-buffer-substring gnus-article-buffer))
5472           (setq gnus-original-article (cons group article)))
5473
5474         ;; Decode charsets.
5475         (run-hooks 'gnus-article-decode-hook)
5476         ;; Mark article as decoded or not.
5477         (setq gnus-article-decoded-p gnus-article-decode-hook))
5478
5479       ;; Update sparse articles.
5480       (when (and do-update-line
5481                  (or (numberp article)
5482                      (stringp article)))
5483         (let ((buf (current-buffer)))
5484           (set-buffer gnus-summary-buffer)
5485           (gnus-summary-update-article do-update-line sparse-header)
5486           (gnus-summary-goto-subject do-update-line nil t)
5487           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5488                             (point))
5489           (set-buffer buf))))))
5490
5491 ;;;
5492 ;;; Article editing
5493 ;;;
5494
5495 (defcustom gnus-article-edit-mode-hook nil
5496   "Hook run in article edit mode buffers."
5497   :group 'gnus-article-various
5498   :type 'hook)
5499
5500 (defcustom gnus-article-edit-article-setup-function
5501   'gnus-article-mime-edit-article-setup
5502   "Function called to setup an editing article buffer."
5503   :group 'gnus-article-various
5504   :type 'function)
5505
5506 (defvar gnus-article-edit-done-function nil)
5507
5508 (defvar gnus-article-edit-mode-map nil)
5509
5510 ;; Should we be using derived.el for this?
5511 (unless gnus-article-edit-mode-map
5512   (setq gnus-article-edit-mode-map (make-keymap))
5513   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5514
5515   (gnus-define-keys gnus-article-edit-mode-map
5516     "\C-c?"    describe-mode
5517     "\C-c\C-c" gnus-article-edit-done
5518     "\C-c\C-k" gnus-article-edit-exit
5519     "\C-c\C-f\C-t" message-goto-to
5520     "\C-c\C-f\C-o" message-goto-from
5521     "\C-c\C-f\C-b" message-goto-bcc
5522     ;;"\C-c\C-f\C-w" message-goto-fcc
5523     "\C-c\C-f\C-c" message-goto-cc
5524     "\C-c\C-f\C-s" message-goto-subject
5525     "\C-c\C-f\C-r" message-goto-reply-to
5526     "\C-c\C-f\C-n" message-goto-newsgroups
5527     "\C-c\C-f\C-d" message-goto-distribution
5528     "\C-c\C-f\C-f" message-goto-followup-to
5529     "\C-c\C-f\C-m" message-goto-mail-followup-to
5530     "\C-c\C-f\C-k" message-goto-keywords
5531     "\C-c\C-f\C-u" message-goto-summary
5532     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5533     "\C-c\C-f\C-a" message-gen-unsubscribed-mft
5534     "\C-c\C-b" message-goto-body
5535     "\C-c\C-i" message-goto-signature
5536
5537     "\C-c\C-t" message-insert-to
5538     "\C-c\C-n" message-insert-newsgroups
5539     "\C-c\C-o" message-sort-headers
5540     "\C-c\C-e" message-elide-region
5541     "\C-c\C-v" message-delete-not-region
5542     "\C-c\C-z" message-kill-to-signature
5543     "\M-\r" message-newline-and-reformat
5544     "\C-c\C-a" mml-attach-file
5545     "\C-a" message-beginning-of-line
5546     "\t" message-tab
5547     "\M-;" comment-region)
5548
5549   (gnus-define-keys (gnus-article-edit-wash-map
5550                      "\C-c\C-w" gnus-article-edit-mode-map)
5551     "f" gnus-article-edit-full-stops))
5552
5553 (easy-menu-define
5554   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5555   '("Field"
5556     ["Fetch To" message-insert-to t]
5557     ["Fetch Newsgroups" message-insert-newsgroups t]
5558     "----"
5559     ["To" message-goto-to t]
5560     ["From" message-goto-from t]
5561     ["Subject" message-goto-subject t]
5562     ["Cc" message-goto-cc t]
5563     ["Reply-To" message-goto-reply-to t]
5564     ["Summary" message-goto-summary t]
5565     ["Keywords" message-goto-keywords t]
5566     ["Newsgroups" message-goto-newsgroups t]
5567     ["Followup-To" message-goto-followup-to t]
5568     ["Mail-Followup-To" message-goto-mail-followup-to t]
5569     ["Distribution" message-goto-distribution t]
5570     ["Body" message-goto-body t]
5571     ["Signature" message-goto-signature t]))
5572
5573 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5574   "Major mode for editing articles.
5575 This is an extended text-mode.
5576
5577 \\{gnus-article-edit-mode-map}"
5578   (make-local-variable 'gnus-article-edit-done-function)
5579   (make-local-variable 'gnus-prev-winconf)
5580   (set (make-local-variable 'font-lock-defaults)
5581        '(message-font-lock-keywords t))
5582   (set (make-local-variable 'mail-header-separator) "")
5583   (easy-menu-add message-mode-field-menu message-mode-map)
5584   (setq buffer-read-only nil)
5585   (buffer-enable-undo)
5586   (widen))
5587
5588 (defun gnus-article-edit (&optional force)
5589   "Edit the current article.
5590 This will have permanent effect only in mail groups.
5591 If FORCE is non-nil, allow editing of articles even in read-only
5592 groups."
5593   (interactive "P")
5594   (when (and (not force)
5595              (gnus-group-read-only-p))
5596     (error "The current newsgroup does not support article editing"))
5597   (gnus-article-date-original)
5598   (gnus-article-edit-article
5599    'ignore
5600    `(lambda (no-highlight)
5601       'ignore
5602       (gnus-summary-edit-article-done
5603        ,(or (mail-header-references gnus-current-headers) "")
5604        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5605
5606 (defun gnus-article-edit-article (start-func exit-func)
5607   "Start editing the contents of the current article buffer."
5608   (let ((winconf (current-window-configuration)))
5609     (set-buffer gnus-article-buffer)
5610     (gnus-article-edit-mode)
5611     (funcall start-func)
5612     (set-buffer-modified-p nil)
5613     (gnus-configure-windows 'edit-article)
5614     (setq gnus-article-edit-done-function exit-func)
5615     (setq gnus-prev-winconf winconf)
5616     (when gnus-article-edit-article-setup-function
5617       (funcall gnus-article-edit-article-setup-function))
5618     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5619
5620 (defun gnus-article-edit-done (&optional arg)
5621   "Update the article edits and exit."
5622   (interactive "P")
5623   (let ((func gnus-article-edit-done-function)
5624         (buf (current-buffer))
5625         (start (window-start))
5626         (p (point))
5627         (winconf gnus-prev-winconf))
5628     (remove-hook 'gnus-article-mode-hook
5629                  'gnus-article-mime-edit-article-unwind)
5630     (widen) ;; Widen it in case that users narrowed the buffer.
5631     (funcall func arg)
5632     (set-buffer buf)
5633     ;; The cache and backlog have to be flushed somewhat.
5634     (when gnus-keep-backlog
5635       (gnus-backlog-remove-article
5636        (car gnus-article-current) (cdr gnus-article-current)))
5637     ;; Flush original article as well.
5638     (save-excursion
5639       (when (get-buffer gnus-original-article-buffer)
5640         (set-buffer gnus-original-article-buffer)
5641         (setq gnus-original-article nil)))
5642     (when gnus-use-cache
5643       (gnus-cache-update-article
5644        (car gnus-article-current) (cdr gnus-article-current)))
5645     ;; We remove all text props from the article buffer.
5646     (kill-all-local-variables)
5647     (gnus-set-text-properties (point-min) (point-max) nil)
5648     (gnus-article-mode)
5649     (set-window-configuration winconf)
5650     (set-buffer buf)
5651     (set-window-start (get-buffer-window buf) start)
5652     (set-window-point (get-buffer-window buf) (point))))
5653
5654 (defun gnus-article-edit-exit ()
5655   "Exit the article editing without updating."
5656   (interactive)
5657   (when (or (not (buffer-modified-p))
5658             (yes-or-no-p "Article modified; kill anyway? "))
5659     (let ((curbuf (current-buffer))
5660           (p (point))
5661           (window-start (window-start)))
5662       (erase-buffer)
5663       (if (gnus-buffer-live-p gnus-original-article-buffer)
5664           (insert-buffer-substring gnus-original-article-buffer))
5665       (let ((winconf gnus-prev-winconf))
5666         (gnus-article-mode)
5667         (set-window-configuration winconf)
5668         ;; Tippy-toe some to make sure that point remains where it was.
5669         (save-current-buffer
5670           (set-buffer curbuf)
5671           (set-window-start (get-buffer-window (current-buffer)) window-start)
5672           (goto-char p))))))
5673
5674 (defun gnus-article-edit-full-stops ()
5675   "Interactively repair spacing at end of sentences."
5676   (interactive)
5677   (save-excursion
5678     (goto-char (point-min))
5679     (search-forward-regexp "^$" nil t)
5680     (let ((case-fold-search nil))
5681       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5682
5683 ;;;
5684 ;;; Article editing with MIME-Edit
5685 ;;;
5686
5687 (defcustom gnus-article-mime-edit-article-setup-hook nil
5688   "Hook run after setting up a MIME editing article buffer."
5689   :group 'gnus-article-various
5690   :type 'hook)
5691
5692 (defun gnus-article-mime-edit-article-unwind ()
5693   "Unwind `gnus-article-buffer' if article editing was given up."
5694   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5695   (when (featurep 'font-lock)
5696     (setq font-lock-defaults nil)
5697     (font-lock-mode -1))
5698   (when mime-edit-mode-flag
5699     (mime-edit-exit 'nomime 'no-error)
5700     (message "")))
5701
5702 (defun gnus-article-mime-edit-article-setup ()
5703   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5704 after replacing with the original article."
5705   (setq gnus-show-mime t)
5706   (setq gnus-article-edit-done-function
5707         `(lambda (&rest args)
5708            (when mime-edit-mode-flag
5709              (let (mime-edit-insert-user-agent-field)
5710                (mime-edit-exit))
5711              (message ""))
5712            (goto-char (point-min))
5713            (let (case-fold-search)
5714              (when (re-search-forward
5715                     (format "^%s$" (regexp-quote mail-header-separator))
5716                     nil t)
5717                (replace-match "")))
5718            (apply ,gnus-article-edit-done-function args)
5719            (insert
5720             (prog1
5721                 (buffer-substring-no-properties (point-min) (point-max))
5722               (set-buffer (get-buffer-create gnus-original-article-buffer))
5723               (erase-buffer)))
5724            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5725            (set-buffer gnus-article-buffer)
5726            (gnus-article-prepare-display)))
5727   (substitute-key-definition 'gnus-article-edit-done
5728                              'gnus-article-mime-edit-done
5729                              gnus-article-edit-mode-map)
5730   (substitute-key-definition 'gnus-article-edit-exit
5731                              'gnus-article-mime-edit-exit
5732                              gnus-article-edit-mode-map)
5733   (erase-buffer)
5734   (insert-buffer-substring gnus-original-article-buffer)
5735   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5736     (fset 'mime-edit-decode-single-part-in-buffer
5737           (lambda (&rest args)
5738             (if (let ((content-type (car args)))
5739                   (and (eq 'message (mime-content-type-primary-type
5740                                      content-type))
5741                        (eq 'rfc822 (mime-content-type-subtype content-type))))
5742                 (setcar (cdr args) 'not-decode-text))
5743             (apply ofn args)))
5744     (unwind-protect
5745         (mime-edit-again)
5746       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
5747   (when (featurep 'font-lock)
5748     (set (make-local-variable 'font-lock-defaults)
5749          '(message-font-lock-keywords t))
5750     (font-lock-set-defaults)
5751     (turn-on-font-lock))
5752   (set-buffer-modified-p nil)
5753   (delete-other-windows)
5754   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5755   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5756
5757 (defun gnus-article-mime-edit-done (&optional arg)
5758   "Update the article MIME edits and exit."
5759   (interactive "P")
5760   (when (featurep 'font-lock)
5761     (setq font-lock-defaults nil)
5762     (font-lock-mode -1))
5763   (gnus-article-edit-done arg))
5764
5765 (defun gnus-article-mime-edit-exit ()
5766   "Exit the article MIME editing without updating."
5767   (interactive)
5768   (when (or (not (buffer-modified-p))
5769             (yes-or-no-p "Article modified; kill anyway? "))
5770     (when (featurep 'font-lock)
5771       (setq font-lock-defaults nil)
5772       (font-lock-mode -1))
5773     (when mime-edit-mode-flag
5774       (let (mime-edit-insert-user-agent-field)
5775         (mime-edit-exit))
5776       (message ""))
5777     (goto-char (point-min))
5778     (let (case-fold-search)
5779       (when (re-search-forward
5780              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5781         (replace-match "")))
5782     (let ((winconf gnus-prev-winconf))
5783       (insert (prog1
5784                   (buffer-substring-no-properties (point-min) (point-max))
5785                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5786                 (erase-buffer)))
5787       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5788       (set-buffer gnus-article-buffer)
5789       (gnus-article-prepare-display)
5790       (set-window-configuration winconf))))
5791
5792 ;;;
5793 ;;; Article highlights
5794 ;;;
5795
5796 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5797
5798 ;;; Internal Variables:
5799
5800 (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\\)\\)"
5801   "Regular expression that matches URLs."
5802   :group 'gnus-article-buttons
5803   :type 'regexp)
5804
5805 (defcustom gnus-button-alist
5806   `(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5807      0 t gnus-button-handle-news 3)
5808     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
5809      gnus-button-handle-news 2)
5810     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5811      1 t
5812      gnus-button-fetch-group 4)
5813     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5814     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5815      t gnus-button-message-id 3)
5816     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5817     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5818     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5819     ;; This is info
5820     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t
5821      gnus-button-handle-info 2)
5822     ;; This is how URLs _should_ be embedded in text...
5823     ("<URL: *\\([^<>]*\\)>" 1 t gnus-button-embedded-url 1)
5824     ;; Raw URLs.
5825     (gnus-button-url-regexp 0 t browse-url 0))
5826   "*Alist of regexps matching buttons in article bodies.
5827
5828 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5829 REGEXP: is the string matching text around the button,
5830 BUTTON: is the number of the regexp grouping actually matching the button,
5831 FORM: is a lisp expression which must eval to true for the button to
5832 be added,
5833 CALLBACK: is the function to call when the user push this button, and each
5834 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5835
5836 CALLBACK can also be a variable, in that case the value of that
5837 variable it the real callback function."
5838   :group 'gnus-article-buttons
5839   :type '(repeat (list (choice regexp variable)
5840                        (integer :tag "Button")
5841                        (sexp :tag "Form")
5842                        (function :tag "Callback")
5843                        (repeat :tag "Par"
5844                                :inline t
5845                                (integer :tag "Regexp group")))))
5846
5847 (defcustom gnus-header-button-alist
5848   `(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
5849      0 t gnus-button-message-id 0)
5850     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5851     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5852      0 t gnus-button-mailto 0)
5853     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5854     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5855     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5856     ("^[^:]+:" "\\bmailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5857     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5858      gnus-button-message-id 3))
5859   "*Alist of headers and regexps to match buttons in article heads.
5860
5861 This alist is very similar to `gnus-button-alist', except that each
5862 alist has an additional HEADER element first in each entry:
5863
5864 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5865
5866 HEADER is a regexp to match a header.  For a fuller explanation, see
5867 `gnus-button-alist'."
5868   :group 'gnus-article-buttons
5869   :group 'gnus-article-headers
5870   :type '(repeat (list (regexp :tag "Header")
5871                        regexp
5872                        (integer :tag "Button")
5873                        (sexp :tag "Form")
5874                        (function :tag "Callback")
5875                        (repeat :tag "Par"
5876                                :inline t
5877                                (integer :tag "Regexp group")))))
5878
5879 (defvar gnus-button-regexp nil)
5880 (defvar gnus-button-marker-list nil)
5881 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5882
5883 (defvar gnus-button-last nil)
5884 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5885
5886 ;;; Commands:
5887
5888 (defun gnus-article-push-button (event)
5889   "Check text under the mouse pointer for a callback function.
5890 If the text under the mouse pointer has a `gnus-callback' property,
5891 call it with the value of the `gnus-data' text property."
5892   (interactive "e")
5893   (set-buffer (window-buffer (posn-window (event-start event))))
5894   (let* ((pos (posn-point (event-start event)))
5895          (data (get-text-property pos 'gnus-data))
5896          (fun (get-text-property pos 'gnus-callback)))
5897     (goto-char pos)
5898     (when fun
5899       (funcall fun data))))
5900
5901 (defun gnus-article-press-button ()
5902   "Check text at point for a callback function.
5903 If the text at point has a `gnus-callback' property,
5904 call it with the value of the `gnus-data' text property."
5905   (interactive)
5906   (let* ((data (get-text-property (point) 'gnus-data))
5907          (fun (get-text-property (point) 'gnus-callback)))
5908     (when fun
5909       (funcall fun data))))
5910
5911 (defun gnus-article-prev-button (n)
5912   "Move point to N buttons backward.
5913 If N is negative, move forward instead."
5914   (interactive "p")
5915   (gnus-article-next-button (- n)))
5916
5917 (defun gnus-article-next-button (n)
5918   "Move point to N buttons forward.
5919 If N is negative, move backward instead."
5920   (interactive "p")
5921   (let ((function (if (< n 0) 'previous-single-property-change
5922                     'next-single-property-change))
5923         (inhibit-point-motion-hooks t)
5924         (backward (< n 0))
5925         (limit (if (< n 0) (point-min) (point-max))))
5926     (setq n (abs n))
5927     (while (and (not (= limit (point)))
5928                 (> n 0))
5929       ;; Skip past the current button.
5930       (when (get-text-property (point) 'gnus-callback)
5931         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5932       ;; Go to the next (or previous) button.
5933       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5934       ;; Put point at the start of the button.
5935       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5936         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5937       ;; Skip past intangible buttons.
5938       (when (get-text-property (point) 'intangible)
5939         (incf n))
5940       (decf n))
5941     (unless (zerop n)
5942       (gnus-message 5 "No more buttons"))
5943     n))
5944
5945 (defun gnus-article-highlight (&optional force)
5946   "Highlight current article.
5947 This function calls `gnus-article-highlight-headers',
5948 `gnus-article-highlight-citation',
5949 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5950 do the highlighting.  See the documentation for those functions."
5951   (interactive (list 'force))
5952   (gnus-article-highlight-headers)
5953   (gnus-article-highlight-citation force)
5954   (gnus-article-highlight-signature)
5955   (gnus-article-add-buttons force)
5956   (gnus-article-add-buttons-to-head))
5957
5958 (defun gnus-article-highlight-some (&optional force)
5959   "Highlight current article.
5960 This function calls `gnus-article-highlight-headers',
5961 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5962 do the highlighting.  See the documentation for those functions."
5963   (interactive (list 'force))
5964   (gnus-article-highlight-headers)
5965   (gnus-article-highlight-signature)
5966   (gnus-article-add-buttons))
5967
5968 (defun gnus-article-highlight-headers ()
5969   "Highlight article headers as specified by `gnus-header-face-alist'."
5970   (interactive)
5971   (save-excursion
5972     (set-buffer gnus-article-buffer)
5973     (save-restriction
5974       (let ((alist gnus-header-face-alist)
5975             (buffer-read-only nil)
5976             (case-fold-search t)
5977             (inhibit-point-motion-hooks t)
5978             entry regexp header-face field-face from hpoints fpoints)
5979         (article-narrow-to-head)
5980         (while (setq entry (pop alist))
5981           (goto-char (point-min))
5982           (setq regexp (concat "^\\("
5983                                (if (string-equal "" (nth 0 entry))
5984                                    "[^\t ]"
5985                                  (nth 0 entry))
5986                                "\\)")
5987                 header-face (nth 1 entry)
5988                 field-face (nth 2 entry))
5989           (while (and (re-search-forward regexp nil t)
5990                       (not (eobp)))
5991             (beginning-of-line)
5992             (setq from (point))
5993             (unless (search-forward ":" nil t)
5994               (forward-char 1))
5995             (when (and header-face
5996                        (not (memq (point) hpoints)))
5997               (push (point) hpoints)
5998               (gnus-put-text-property from (point) 'face header-face))
5999             (when (and field-face
6000                        (not (memq (setq from (point)) fpoints)))
6001               (push from fpoints)
6002               (if (re-search-forward "^[^ \t]" nil t)
6003                   (forward-char -2)
6004                 (goto-char (point-max)))
6005               (gnus-put-text-property from (point) 'face field-face))))))))
6006
6007 (defun gnus-article-highlight-signature ()
6008   "Highlight the signature in an article.
6009 It does this by highlighting everything after
6010 `gnus-signature-separator' using `gnus-signature-face'."
6011   (interactive)
6012   (when gnus-signature-face
6013     (save-excursion
6014       (set-buffer gnus-article-buffer)
6015       (let ((buffer-read-only nil)
6016             (inhibit-point-motion-hooks t))
6017         (save-restriction
6018           (when (gnus-article-narrow-to-signature)
6019             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6020                               'face gnus-signature-face)))))))
6021
6022 (defun gnus-article-buttonize-signature ()
6023   "Add button to the signature."
6024   (interactive)
6025   (save-excursion
6026     (set-buffer gnus-article-buffer)
6027     (let ((buffer-read-only nil)
6028           (inhibit-point-motion-hooks t))
6029       (when (gnus-article-search-signature)
6030         (gnus-article-add-button (match-beginning 0) (match-end 0)
6031                                  'gnus-signature-toggle
6032                                  (set-marker (make-marker)
6033                                              (1+ (match-end 0))))))))
6034
6035 (defun gnus-button-in-region-p (b e prop)
6036   "Say whether PROP exists in the region."
6037   (text-property-not-all b e prop nil))
6038
6039 (defun gnus-article-add-buttons (&optional force)
6040   "Find external references in the article and make buttons of them.
6041 \"External references\" are things like Message-IDs and URLs, as
6042 specified by `gnus-button-alist'."
6043   (interactive (list 'force))
6044   (save-excursion
6045     (set-buffer gnus-article-buffer)
6046     (let ((buffer-read-only nil)
6047           (inhibit-point-motion-hooks t)
6048           (case-fold-search t)
6049           (alist gnus-button-alist)
6050           beg entry regexp)
6051       ;; Remove all old markers.
6052       (let (marker entry new-list)
6053         (while (setq marker (pop gnus-button-marker-list))
6054           (if (or (< marker (point-min)) (>= marker (point-max)))
6055               (push marker new-list)
6056             (goto-char marker)
6057             (when (setq entry (gnus-button-entry))
6058               (put-text-property (match-beginning (nth 1 entry))
6059                                  (match-end (nth 1 entry))
6060                                  'gnus-callback nil))
6061             (set-marker marker nil)))
6062         (setq gnus-button-marker-list new-list))
6063       ;; We skip the headers.
6064       (article-goto-body)
6065       (setq beg (point))
6066       (while (setq entry (pop alist))
6067         (setq regexp (eval (car entry)))
6068         (goto-char beg)
6069         (while (re-search-forward regexp nil t)
6070           (let* ((start (and entry (match-beginning (nth 1 entry))))
6071                  (end (and entry (match-end (nth 1 entry))))
6072                  (from (match-beginning 0)))
6073             (when (and (or (eq t (nth 2 entry))
6074                            (eval (nth 2 entry)))
6075                        (not (gnus-button-in-region-p
6076                              start end 'gnus-callback)))
6077               ;; That optional form returned non-nil, so we add the
6078               ;; button.
6079               (gnus-article-add-button
6080                start end 'gnus-button-push
6081                (car (push (set-marker (make-marker) from)
6082                           gnus-button-marker-list))))))))))
6083
6084 ;; Add buttons to the head of an article.
6085 (defun gnus-article-add-buttons-to-head ()
6086   "Add buttons to the head of the article."
6087   (interactive)
6088   (save-excursion
6089     (set-buffer gnus-article-buffer)
6090     (save-restriction
6091       (let ((buffer-read-only nil)
6092             (inhibit-point-motion-hooks t)
6093             (case-fold-search t)
6094             (alist gnus-header-button-alist)
6095             entry beg end)
6096         (article-narrow-to-head)
6097         (while alist
6098           ;; Each alist entry.
6099           (setq entry (car alist)
6100                 alist (cdr alist))
6101           (goto-char (point-min))
6102           (while (re-search-forward (car entry) nil t)
6103             ;; Each header matching the entry.
6104             (setq beg (match-beginning 0))
6105             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6106                                (match-beginning 0))
6107                           (point-max)))
6108             (goto-char beg)
6109             (while (re-search-forward (nth 1 entry) end t)
6110               ;; Each match within a header.
6111               (let* ((entry (cdr entry))
6112                      (start (match-beginning (nth 1 entry)))
6113                      (end (match-end (nth 1 entry)))
6114                      (form (nth 2 entry)))
6115                 (goto-char (match-end 0))
6116                 (when (eval form)
6117                   (gnus-article-add-button
6118                    start end (nth 3 entry)
6119                    (buffer-substring (match-beginning (nth 4 entry))
6120                                      (match-end (nth 4 entry)))))))
6121             (goto-char end)))))))
6122
6123 ;;; External functions:
6124
6125 (defun gnus-article-add-button (from to fun &optional data)
6126   "Create a button between FROM and TO with callback FUN and data DATA."
6127   (when gnus-article-button-face
6128     (gnus-overlay-put (gnus-make-overlay from to)
6129                       'face gnus-article-button-face))
6130   (gnus-add-text-properties
6131    from to
6132    (nconc (and gnus-article-mouse-face
6133                (list gnus-mouse-face-prop gnus-article-mouse-face))
6134           (list 'gnus-callback fun)
6135           (and data (list 'gnus-data data))))
6136   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6137                          ;; Quote `:button-keymap' for Mule 2.3
6138                          ;; but it won't work.
6139                          ':button-keymap gnus-widget-button-keymap))
6140
6141 ;;; Internal functions:
6142
6143 (defun gnus-article-set-globals ()
6144   (save-excursion
6145     (set-buffer gnus-summary-buffer)
6146     (gnus-set-global-variables)))
6147
6148 (defun gnus-signature-toggle (end)
6149   (save-excursion
6150     (set-buffer gnus-article-buffer)
6151     (let ((buffer-read-only nil)
6152           (inhibit-point-motion-hooks t)
6153           (limit (next-single-property-change end 'mime-view-entity
6154                                               nil (point-max))))
6155       (if (text-property-any end limit 'article-type 'signature)
6156           (progn
6157             (gnus-delete-wash-type 'signature)
6158             (gnus-remove-text-properties-when
6159              'article-type 'signature end limit
6160              (cons 'article-type (cons 'signature
6161                                        gnus-hidden-properties))))
6162         (gnus-add-wash-type 'signature)
6163         (gnus-add-text-properties-when
6164          'article-type nil end limit
6165          (cons 'article-type (cons 'signature
6166                                    gnus-hidden-properties)))))
6167     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6168       (gnus-set-mode-line 'article))))
6169
6170 (defun gnus-button-entry ()
6171   ;; Return the first entry in `gnus-button-alist' matching this place.
6172   (let ((alist gnus-button-alist)
6173         (entry nil))
6174     (while alist
6175       (setq entry (pop alist))
6176       (if (looking-at (eval (car entry)))
6177           (setq alist nil)
6178         (setq entry nil)))
6179     entry))
6180
6181 (defun gnus-button-push (marker)
6182   ;; Push button starting at MARKER.
6183   (save-excursion
6184     (goto-char marker)
6185     (let* ((entry (gnus-button-entry))
6186            (inhibit-point-motion-hooks t)
6187            (fun (nth 3 entry))
6188            (args (mapcar (lambda (group)
6189                            (let ((string (match-string group)))
6190                              (gnus-set-text-properties
6191                               0 (length string) nil string)
6192                              string))
6193                          (nthcdr 4 entry))))
6194       (cond
6195        ((fboundp fun)
6196         (apply fun args))
6197        ((and (boundp fun)
6198              (fboundp (symbol-value fun)))
6199         (apply (symbol-value fun) args))
6200        (t
6201         (gnus-message 1 "You must define `%S' to use this button"
6202                       (cons fun args)))))))
6203
6204 (defun gnus-parse-news-url (url)
6205   (let (scheme server group message-id articles)
6206     (with-temp-buffer
6207       (insert url)
6208       (goto-char (point-min))
6209       (when (looking-at "\\([A-Za-z]+\\):")
6210         (setq scheme (match-string 1))
6211         (goto-char (match-end 0)))
6212       (when (looking-at "//\\([^/]+\\)/")
6213         (setq server (match-string 1))
6214         (goto-char (match-end 0)))
6215
6216       (cond
6217        ((looking-at "\\(.*@.*\\)")
6218         (setq message-id (match-string 1)))
6219        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6220         (setq group (match-string 1)
6221               articles (split-string (match-string 2) "-")))
6222        ((looking-at "\\([^/]+\\)/?")
6223         (setq group (match-string 1)))
6224        (t
6225         (error "Unknown news URL syntax"))))
6226     (list scheme server group message-id articles)))
6227
6228 (defun gnus-button-handle-news (url)
6229   "Fetch a news URL."
6230   (destructuring-bind (scheme server group message-id articles)
6231       (gnus-parse-news-url url)
6232     (cond
6233      (message-id
6234       (save-excursion
6235         (set-buffer gnus-summary-buffer)
6236         (if server
6237             (let ((gnus-refer-article-method (list (list 'nntp server))))
6238               (gnus-summary-refer-article message-id))
6239           (gnus-summary-refer-article message-id))))
6240      (group
6241       (gnus-button-fetch-group url)))))
6242
6243 (defun gnus-button-handle-info (url)
6244   "Fetch an info URL."
6245   (if (string-match
6246        "^\\([^:/]+\\)?/\\(.*\\)"
6247        url)
6248       (gnus-info-find-node
6249        (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
6250                        "Gnus")
6251                ")"
6252                (gnus-url-unhex-string (match-string 2 url))))
6253     (error "Can't parse %s" url)))
6254
6255 (defun gnus-button-message-id (message-id)
6256   "Fetch MESSAGE-ID."
6257   (save-excursion
6258     (set-buffer gnus-summary-buffer)
6259     (gnus-summary-refer-article message-id)))
6260
6261 (defun gnus-button-fetch-group (address)
6262   "Fetch GROUP specified by ADDRESS."
6263   (if (not (string-match "[:/]" address))
6264       ;; This is just a simple group url.
6265       (gnus-group-read-ephemeral-group address gnus-select-method)
6266     (if (not
6267          (string-match
6268           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
6269           address))
6270         (error "Can't parse %s" address)
6271       (gnus-group-read-ephemeral-group
6272        (match-string 4 address)
6273        `(nntp ,(match-string 1 address)
6274               (nntp-address ,(match-string 1 address))
6275               (nntp-port-number ,(if (match-end 3)
6276                                      (match-string 3 address)
6277                                    "nntp")))
6278        nil nil nil
6279        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
6280
6281 (defun gnus-url-parse-query-string (query &optional downcase)
6282   (let (retval pairs cur key val)
6283     (setq pairs (split-string query "&"))
6284     (while pairs
6285       (setq cur (car pairs)
6286             pairs (cdr pairs))
6287       (if (not (string-match "=" cur))
6288           nil                           ; Grace
6289         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
6290               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
6291         (if downcase
6292             (setq key (downcase key)))
6293         (setq cur (assoc key retval))
6294         (if cur
6295             (setcdr cur (cons val (cdr cur)))
6296           (setq retval (cons (list key val) retval)))))
6297     retval))
6298
6299 (defun gnus-url-mailto (url)
6300   ;; Send mail to someone
6301   (when (string-match "mailto:/*\\(.*\\)" url)
6302     (setq url (substring url (match-beginning 1) nil)))
6303   (let (to args subject func)
6304     (if (string-match (regexp-quote "?") url)
6305         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
6306               args (gnus-url-parse-query-string
6307                     (substring url (match-end 0) nil) t))
6308       (setq to (gnus-url-unhex-string url)))
6309     (setq args (cons (list "to" to) args)
6310           subject (cdr-safe (assoc "subject" args)))
6311     (gnus-msg-mail)
6312     (while args
6313       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
6314       (if (fboundp func)
6315           (funcall func)
6316         (message-position-on-field (caar args)))
6317       (insert (mapconcat 'identity (cdar args) ", "))
6318       (setq args (cdr args)))
6319     (if subject
6320         (message-goto-body)
6321       (message-goto-subject))))
6322
6323 (defun gnus-button-embedded-url (address)
6324   "Activate ADDRESS with `browse-url'."
6325   (browse-url (gnus-strip-whitespace address)))
6326
6327 ;;; Next/prev buttons in the article buffer.
6328
6329 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
6330 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
6331
6332 (defvar gnus-prev-page-map
6333   (let ((map (make-sparse-keymap)))
6334     (unless (>= emacs-major-version 21)
6335       ;; XEmacs doesn't care.
6336       (set-keymap-parent map gnus-article-mode-map))
6337     (define-key map gnus-mouse-2 'gnus-button-prev-page)
6338     (define-key map "\r" 'gnus-button-prev-page)
6339     map))
6340
6341 (defun gnus-insert-prev-page-button ()
6342   (let ((b (point))
6343         (buffer-read-only nil)
6344         (situation (get-text-property (point-min) 'mime-view-situation)))
6345     (gnus-eval-format
6346      gnus-prev-page-line-format nil
6347      `(,@(gnus-local-map-property gnus-prev-page-map)
6348          gnus-prev t
6349          gnus-callback gnus-article-button-prev-page
6350          article-type annotation
6351          mime-view-situation ,situation))
6352     (widget-convert-button
6353      'link b (point)
6354      :action 'gnus-button-prev-page
6355      :button-keymap gnus-prev-page-map)))
6356
6357 (defvar gnus-next-page-map
6358   (let ((map (make-sparse-keymap)))
6359     (unless (>= emacs-major-version 21)
6360       ;; XEmacs doesn't care.
6361       (set-keymap-parent map gnus-article-mode-map))
6362     (define-key map gnus-mouse-2 'gnus-button-next-page)
6363     (define-key map "\r" 'gnus-button-next-page)
6364     map))
6365
6366 (defun gnus-button-next-page (&optional args more-args)
6367   "Go to the next page."
6368   (interactive)
6369   (let ((win (selected-window)))
6370     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6371     (gnus-article-next-page)
6372     (select-window win)))
6373
6374 (defun gnus-button-prev-page (&optional args more-args)
6375   "Go to the prev page."
6376   (interactive)
6377   (let ((win (selected-window)))
6378     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6379     (gnus-article-prev-page)
6380     (select-window win)))
6381
6382 (defun gnus-insert-next-page-button ()
6383   (let ((b (point))
6384         (buffer-read-only nil)
6385         (situation (get-text-property (point-min) 'mime-view-situation)))
6386     (gnus-eval-format gnus-next-page-line-format nil
6387                       `(,@(gnus-local-map-property gnus-next-page-map)
6388                           gnus-next t
6389                           gnus-callback gnus-article-button-next-page
6390                           article-type annotation
6391                           mime-view-situation ,situation))
6392     (widget-convert-button
6393      'link b (point)
6394      :action 'gnus-button-next-page
6395      :button-keymap gnus-next-page-map)))
6396
6397 (defun gnus-article-button-next-page (arg)
6398   "Go to the next page."
6399   (interactive "P")
6400   (let ((win (selected-window)))
6401     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6402     (gnus-article-next-page)
6403     (select-window win)))
6404
6405 (defun gnus-article-button-prev-page (arg)
6406   "Go to the prev page."
6407   (interactive "P")
6408   (let ((win (selected-window)))
6409     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6410     (gnus-article-prev-page)
6411     (select-window win)))
6412
6413 (defvar gnus-decode-header-methods
6414   '(mail-decode-encoded-word-region)
6415   "List of methods used to decode headers.
6416
6417 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
6418 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
6419 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
6420 whose names match REGEXP.
6421
6422 For example:
6423 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
6424  mail-decode-encoded-word-region
6425  (\"chinese\" . rfc1843-decode-region))
6426 ")
6427
6428 (defvar gnus-decode-header-methods-cache nil)
6429
6430 (defun gnus-multi-decode-header (start end)
6431   "Apply the functions from `gnus-encoded-word-methods' that match."
6432   (unless (and gnus-decode-header-methods-cache
6433                (eq gnus-newsgroup-name
6434                    (car gnus-decode-header-methods-cache)))
6435     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
6436     (mapcar (lambda (x)
6437               (if (symbolp x)
6438                   (nconc gnus-decode-header-methods-cache (list x))
6439                 (if (and gnus-newsgroup-name
6440                          (string-match (car x) gnus-newsgroup-name))
6441                     (nconc gnus-decode-header-methods-cache
6442                            (list (cdr x))))))
6443             gnus-decode-header-methods))
6444   (let ((xlist gnus-decode-header-methods-cache))
6445     (pop xlist)
6446     (save-restriction
6447       (narrow-to-region start end)
6448       (while xlist
6449         (funcall (pop xlist) (point-min) (point-max))))))
6450
6451 ;;;
6452 ;;; Treatment top-level handling.
6453 ;;;
6454
6455 (defun gnus-treat-article (condition &optional part-number total-parts type)
6456   (let ((length (- (point-max) (point-min)))
6457         (alist gnus-treatment-function-alist)
6458         (article-goto-body-goes-to-point-min-p t)
6459         (treated-type
6460          (or (not type)
6461              (catch 'found
6462                (let ((list gnus-article-treat-types))
6463                  (while list
6464                    (when (string-match (pop list) type)
6465                      (throw 'found t)))))))
6466         (highlightp (gnus-visual-p 'article-highlight 'highlight))
6467         (entity (static-unless (featurep 'xemacs)
6468                   (when (eq 'head condition)
6469                     (get-text-property (point-min) 'mime-view-entity))))
6470         val elem buttonized)
6471     (gnus-run-hooks 'gnus-part-display-hook)
6472     (unless gnus-inhibit-treatment
6473       (while (setq elem (pop alist))
6474         (setq val
6475               (save-excursion
6476                 (if (gnus-buffer-live-p gnus-summary-buffer)
6477                     (set-buffer gnus-summary-buffer))
6478                 (symbol-value (car elem))))
6479         (when (and (or (consp val)
6480                        treated-type)
6481                    (gnus-treat-predicate val)
6482                    (or (not (get (car elem) 'highlight))
6483                        highlightp))
6484           (when (and (not buttonized)
6485                      (memq (car elem)
6486                            '(gnus-treat-hide-signature
6487                              gnus-treat-highlight-signature)))
6488             (gnus-article-buttonize-signature)
6489             (setq buttonized t))
6490           (save-restriction
6491             (funcall (cadr elem)))))
6492       ;; FSF Emacsen does not inherit the existing text properties
6493       ;; in the new text, so we should do it for `mime-view-entity'.
6494       (static-unless (featurep 'xemacs)
6495         (when entity
6496           (put-text-property (point-min) (point-max)
6497                              'mime-view-entity entity))))))
6498
6499 ;; Dynamic variables.
6500 (eval-when-compile
6501   (defvar part-number)
6502   (defvar total-parts)
6503   (defvar type)
6504   (defvar condition)
6505   (defvar length))
6506
6507 (defun gnus-treat-predicate (val)
6508   (cond
6509    ((null val)
6510     nil)
6511    ((and (listp val)
6512          (stringp (car val)))
6513     (apply 'gnus-or (mapcar `(lambda (s)
6514                                (string-match s ,(or gnus-newsgroup-name "")))
6515                             val)))
6516    ((listp val)
6517     (let ((pred (pop val)))
6518       (cond
6519        ((eq pred 'or)
6520         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
6521        ((eq pred 'and)
6522         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
6523        ((eq pred 'not)
6524         (not (gnus-treat-predicate (car val))))
6525        ((eq pred 'typep)
6526         (equal (car val) type))
6527        (t
6528         (error "%S is not a valid predicate" pred)))))
6529    ((eq val 'mime)
6530     gnus-show-mime)
6531    (condition
6532     (eq condition val))
6533    ((eq val t)
6534     t)
6535    ((eq val 'head)
6536     nil)
6537    ((eq val 'last)
6538     (eq part-number total-parts))
6539    ((numberp val)
6540     (< length val))
6541    (t
6542     (error "%S is not a valid value" val))))
6543
6544 (defun gnus-article-encrypt-body (protocol &optional n)
6545   "Encrypt the article body."
6546   (interactive
6547    (list
6548     (or gnus-article-encrypt-protocol
6549         (completing-read "Encrypt protocol: "
6550                          gnus-article-encrypt-protocol-alist
6551                          nil t))
6552     current-prefix-arg))
6553   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
6554     (unless func
6555       (error (format "Can't find the encrypt protocol %s" protocol)))
6556     (if (equal gnus-newsgroup-name "nndraft:drafts")
6557         (error "Can't encrypt the article in group nndraft:drafts"))
6558     (if (equal gnus-newsgroup-name "nndraft:queue")
6559         (error "Don't encrypt the article in group nndraft:queue"))
6560     (gnus-summary-iterate n
6561       (save-excursion
6562         (set-buffer gnus-summary-buffer)
6563         (let ((mail-parse-charset gnus-newsgroup-charset)
6564               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6565               (summary-buffer gnus-summary-buffer)
6566               references point)
6567           (gnus-set-global-variables)
6568           (when (gnus-group-read-only-p)
6569             (error "The current newsgroup does not support article encrypt"))
6570           (gnus-summary-show-article t)
6571           (setq references
6572                 (or (mail-header-references gnus-current-headers) ""))
6573           (set-buffer gnus-article-buffer)
6574           (let* ((buffer-read-only nil)
6575                  (headers
6576                   (mapcar (lambda (field)
6577                             (and (save-restriction
6578                                    (message-narrow-to-head)
6579                                    (goto-char (point-min))
6580                                    (search-forward field nil t))
6581                                  (prog2
6582                                      (message-narrow-to-field)
6583                                      (buffer-substring (point-min) (point-max))
6584                                    (delete-region (point-min) (point-max))
6585                                    (widen))))
6586                           '("Content-Type:" "Content-Transfer-Encoding:"
6587                             "Content-Disposition:"))))
6588             (message-narrow-to-head)
6589             (message-remove-header "MIME-Version")
6590             (goto-char (point-max))
6591             (setq point (point))
6592             (insert (apply 'concat headers))
6593             (widen)
6594             (narrow-to-region point (point-max))
6595             (let ((message-options message-options))
6596               (message-options-set 'message-sender user-mail-address)
6597               (message-options-set 'message-recipients user-mail-address)
6598               (message-options-set 'message-sign-encrypt 'not)
6599               (funcall func))
6600             (goto-char (point-min))
6601             (insert "MIME-Version: 1.0\n")
6602             (widen)
6603             (gnus-summary-edit-article-done
6604              references nil summary-buffer t))
6605           (when gnus-keep-backlog
6606             (gnus-backlog-remove-article
6607              (car gnus-article-current) (cdr gnus-article-current)))
6608           (save-excursion
6609             (when (get-buffer gnus-original-article-buffer)
6610               (set-buffer gnus-original-article-buffer)
6611               (setq gnus-original-article nil)))
6612           (when gnus-use-cache
6613             (gnus-cache-update-article
6614              (car gnus-article-current) (cdr gnus-article-current))))))))
6615
6616 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6617   "The following specs can be used:
6618 %t  The security MIME type
6619 %i  Additional info
6620 %d  Details
6621 %D  Details if button is pressed")
6622
6623 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6624   "The following specs can be used:
6625 %t  The security MIME type
6626 %i  Additional info
6627 %d  Details
6628 %D  Details if button is pressed")
6629
6630 (defvar gnus-mime-security-button-line-format-alist
6631   '((?t gnus-tmp-type ?s)
6632     (?i gnus-tmp-info ?s)
6633     (?d gnus-tmp-details ?s)
6634     (?D gnus-tmp-pressed-details ?s)))
6635
6636 (defvar gnus-mime-security-button-map
6637   (let ((map (make-sparse-keymap)))
6638     (unless (>= (string-to-number emacs-version) 21)
6639       (set-keymap-parent map gnus-article-mode-map))
6640     (define-key map gnus-mouse-2 'gnus-article-push-button)
6641     (define-key map "\r" 'gnus-article-press-button)
6642     map))
6643
6644 (defvar gnus-mime-security-details-buffer nil)
6645
6646 (defvar gnus-mime-security-button-pressed nil)
6647
6648 (defvar gnus-mime-security-show-details-inline t
6649   "If non-nil, show details in the article buffer.")
6650
6651 (defun gnus-mime-security-verify-or-decrypt (handle)
6652   (mm-remove-parts (cdr handle))
6653   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6654         point buffer-read-only)
6655     (if region
6656         (goto-char (car region)))
6657     (save-restriction
6658       (narrow-to-region (point) (point))
6659       (with-current-buffer (mm-handle-multipart-original-buffer handle)
6660         (let* ((mm-verify-option 'known)
6661                (mm-decrypt-option 'known)
6662                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6663           (unless (eq nparts (cdr handle))
6664             (mm-destroy-parts (cdr handle))
6665             (setcdr handle nparts))))
6666       (setq point (point))
6667       (gnus-mime-display-security handle)
6668       (goto-char (point-max)))
6669     (when region
6670       (delete-region (point) (cdr region))
6671       (set-marker (car region) nil)
6672       (set-marker (cdr region) nil))
6673     (goto-char point)))
6674
6675 (defun gnus-mime-security-show-details (handle)
6676   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6677     (if details
6678         (if gnus-mime-security-show-details-inline
6679             (let ((gnus-mime-security-button-pressed t)
6680                   (gnus-mime-security-button-line-format
6681                    (get-text-property (point) 'gnus-line-format))
6682                   buffer-read-only)
6683               (forward-char -1)
6684               (while (eq (get-text-property (point) 'gnus-line-format)
6685                          gnus-mime-security-button-line-format)
6686                 (forward-char -1))
6687               (forward-char)
6688               (save-restriction
6689                 (narrow-to-region (point) (point))
6690                 (gnus-insert-mime-security-button handle))
6691               (delete-region (point)
6692                              (or (text-property-not-all
6693                                   (point) (point-max)
6694                                   'gnus-line-format
6695                                   gnus-mime-security-button-line-format)
6696                                  (point-max))))
6697           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6698               (with-current-buffer gnus-mime-security-details-buffer
6699                 (erase-buffer)
6700                 t)
6701             (setq gnus-mime-security-details-buffer
6702                   (gnus-get-buffer-create "*MIME Security Details*")))
6703           (with-current-buffer gnus-mime-security-details-buffer
6704             (insert details)
6705             (goto-char (point-min)))
6706           (pop-to-buffer gnus-mime-security-details-buffer))
6707       (gnus-message 5 "No details."))))
6708
6709 (defun gnus-mime-security-press-button (handle)
6710   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6711       (gnus-mime-security-show-details handle)
6712     (gnus-mime-security-verify-or-decrypt handle)))
6713
6714 (defun gnus-insert-mime-security-button (handle &optional displayed)
6715   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6716          (gnus-tmp-type
6717           (concat
6718            (or (nth 2 (assoc protocol mm-verify-function-alist))
6719                (nth 2 (assoc protocol mm-decrypt-function-alist))
6720                "Unknown")
6721            (if (equal (car handle) "multipart/signed")
6722                " Signed" " Encrypted")
6723            " Part"))
6724          (gnus-tmp-info
6725           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6726               "Undecided"))
6727          (gnus-tmp-details
6728           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6729          gnus-tmp-pressed-details
6730          b e)
6731     (setq gnus-tmp-details
6732           (if gnus-tmp-details
6733               (concat "\n" gnus-tmp-details) ""))
6734     (setq gnus-tmp-pressed-details
6735           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6736     (unless (bolp)
6737       (insert "\n"))
6738     (setq b (point))
6739     (gnus-eval-format
6740      gnus-mime-security-button-line-format
6741      gnus-mime-security-button-line-format-alist
6742      `(,@(gnus-local-map-property gnus-mime-security-button-map)
6743          gnus-callback gnus-mime-security-press-button
6744          gnus-line-format ,gnus-mime-security-button-line-format
6745          article-type annotation
6746          gnus-data ,handle))
6747     (setq e (point))
6748     (widget-convert-button
6749      'link b e
6750      :mime-handle handle
6751      :action 'gnus-widget-press-button
6752      :button-keymap gnus-mime-security-button-map
6753      :help-echo
6754      (lambda (widget/window &optional overlay pos)
6755        ;; Needed to properly clear the message due to a bug in
6756        ;; wid-edit (XEmacs only).
6757        (if (boundp 'help-echo-owns-message)
6758            (setq help-echo-owns-message t))
6759        (format
6760         "%S: show detail"
6761         (aref gnus-mouse-2 0))))))
6762
6763 (defun gnus-mime-display-security (handle)
6764   (save-restriction
6765     (narrow-to-region (point) (point))
6766     (unless (gnus-unbuttonized-mime-type-p (car handle))
6767       (gnus-insert-mime-security-button handle))
6768     (gnus-mime-display-mixed (cdr handle))
6769     (unless (bolp)
6770       (insert "\n"))
6771     (unless (gnus-unbuttonized-mime-type-p (car handle))
6772       (let ((gnus-mime-security-button-line-format
6773              gnus-mime-security-button-end-line-format))
6774         (gnus-insert-mime-security-button handle)))
6775     (mm-set-handle-multipart-parameter
6776      handle 'gnus-region
6777      (cons (set-marker (make-marker) (point-min))
6778            (set-marker (make-marker) (point-max))))))
6779
6780
6781 ;;; @ for mime-view
6782 ;;;
6783
6784 (defun gnus-article-header-presentation-method (entity situation)
6785   (mime-insert-header entity)
6786   (article-decode-group-name))
6787
6788 (set-alist 'mime-header-presentation-method-alist
6789            'gnus-original-article-mode
6790            #'gnus-article-header-presentation-method)
6791
6792 (defun gnus-mime-preview-quitting-method ()
6793   (mime-preview-kill-buffer)
6794   (delete-other-windows)
6795   (gnus-article-show-summary)
6796   (gnus-summary-select-article gnus-show-all-headers t))
6797
6798 (set-alist 'mime-preview-quitting-method-alist
6799            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6800
6801 (set-alist 'mime-preview-following-method-alist
6802            'gnus-original-article-mode #'gnus-following-method)
6803
6804 (set-alist 'mime-preview-over-to-previous-method-alist
6805            'gnus-original-article-mode
6806            (lambda ()
6807              (if (> (point-min) 1)
6808                  (gnus-article-prev-page)
6809                (gnus-article-read-summary-keys
6810                 nil (gnus-character-to-event ?P)))))
6811
6812 (set-alist 'mime-preview-over-to-next-method-alist
6813            'gnus-original-article-mode'
6814            (lambda ()
6815              (if (< (point-max) (buffer-size))
6816                  (gnus-article-next-page)
6817                (gnus-article-read-summary-keys
6818                 nil (gnus-character-to-event ?N)))))
6819
6820
6821 ;;; @ end
6822 ;;;
6823
6824 (gnus-ems-redefine)
6825
6826 (provide 'gnus-art)
6827
6828 (run-hooks 'gnus-art-load-hook)
6829
6830 ;;; gnus-art.el ends here