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