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