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