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