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