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