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