cbfd17cbee488b7eb9319aa435c60279afd46b2a
[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 (defun gnus-article-browse-delete-temp-files (&optional how)
2626   "Delete temp-files created by `gnus-article-browse-html-parts'."
2627   (unless how
2628     (setq how gnus-article-browse-delete-temp))
2629   (when (and gnus-article-browse-html-temp-list how)
2630     (when (and (eq how 'ask)
2631                (y-or-n-p (format
2632                           "Delete all %s temporary HTML file(s)? "
2633                           (length gnus-article-browse-html-temp-list)))
2634                (setq how t)))
2635     (dolist (file gnus-article-browse-html-temp-list)
2636       (when (and (file-exists-p file)
2637                  (or (eq how t)
2638                      ;; `how' is neither `nil', `ask' nor `t' (i.e. `file'):
2639                      (gnus-y-or-n-p
2640                       (format "Delete temporary HTML file `%s'? " file))))
2641         (delete-file file))
2642       ;; Also remove file from the list when not deleted or if file doesn't
2643       ;; exist anymore.
2644       (setq gnus-article-browse-html-temp-list
2645             (delete file gnus-article-browse-html-temp-list))))
2646   gnus-article-browse-html-temp-list)
2647
2648 (defun gnus-article-browse-html-parts (list)
2649   "View all \"text/html\" parts from LIST.
2650 Recurse into multiparts."
2651   ;; Internal function used by `gnus-article-browse-html-article'.
2652   (let ((showed))
2653     ;; Find and show the html-parts.
2654     (dolist (handle list)
2655       ;; If HTML, show it:
2656       (when (listp handle)
2657         (cond ((and (bufferp (car handle))
2658                     (string-match "text/html" (car (mm-handle-type handle))))
2659                (let ((tmp-file (mm-make-temp-file
2660                                 ;; Do we need to care for 8.3 filenames?
2661                                 "mm-" nil ".html")))
2662                  (mm-save-part-to-file handle tmp-file)
2663                  (add-to-list 'gnus-article-browse-html-temp-list tmp-file)
2664                  (add-hook 'gnus-summary-prepare-exit-hook
2665                            'gnus-article-browse-delete-temp-files)
2666                  (add-hook 'gnus-exit-gnus-hook
2667                            (lambda  ()
2668                              (gnus-article-browse-delete-temp-files t)))
2669                  (browse-url tmp-file)
2670                  (setq showed t)))
2671               ;; If multipart, recurse
2672               ((and (stringp (car handle))
2673                     (string-match "^multipart/" (car handle))
2674                     (setq showed
2675                           (or showed
2676                               (gnus-article-browse-html-parts handle))))))))
2677     showed))
2678
2679 ;; TODO: Key binding
2680 (defun gnus-article-browse-html-article ()
2681   "View \"text/html\" parts of the current article with a WWW browser."
2682   (interactive)
2683   (save-window-excursion
2684     ;; Open raw article and select the buffer
2685     (gnus-summary-show-article t)
2686     (gnus-summary-select-article-buffer)
2687     (let ((parts (mm-dissect-buffer t t)))
2688       ;; If singlepart, enforce a list.
2689       (when (and (bufferp (car parts))
2690                  (stringp (car (mm-handle-type parts))))
2691         (setq parts (list parts)))
2692       ;; Process the list
2693       (unless (gnus-article-browse-html-parts parts)
2694         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
2695       (gnus-summary-show-article))))
2696
2697 (defun article-hide-list-identifiers ()
2698   "Remove list identifies from the Subject header.
2699 The `gnus-list-identifiers' variable specifies what to do."
2700   (interactive)
2701   (let ((inhibit-point-motion-hooks t)
2702         (regexp (if (consp gnus-list-identifiers)
2703                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2704                   gnus-list-identifiers))
2705         (inhibit-read-only t))
2706     (when regexp
2707       (save-excursion
2708         (save-restriction
2709           (article-narrow-to-head)
2710           (goto-char (point-min))
2711           (while (re-search-forward
2712                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2713                   nil t)
2714             (delete-region (match-beginning 2) (match-end 0))
2715             (beginning-of-line))
2716           (when (re-search-forward
2717                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2718             (delete-region (match-beginning 1) (match-end 1))))))))
2719
2720 (defun article-hide-pem (&optional arg)
2721   "Toggle hiding of any PEM headers and signatures in the current article.
2722 If given a negative prefix, always show; if given a positive prefix,
2723 always hide."
2724   (interactive (gnus-article-hidden-arg))
2725   (unless (gnus-article-check-hidden-text 'pem arg)
2726     (save-excursion
2727       (let ((inhibit-read-only t) end)
2728         (goto-char (point-min))
2729         ;; Hide the horrendously ugly "header".
2730         (when (and (search-forward
2731                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2732                     nil t)
2733                    (setq end (1+ (match-beginning 0))))
2734           (gnus-add-wash-type 'pem)
2735           (gnus-article-hide-text-type
2736            end
2737            (if (search-forward "\n\n" nil t)
2738                (match-end 0)
2739              (point-max))
2740            'pem)
2741           ;; Hide the trailer as well
2742           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2743                                 nil t)
2744             (gnus-article-hide-text-type
2745              (match-beginning 0) (match-end 0) 'pem)))))))
2746
2747 (defun article-strip-banner ()
2748   "Strip the banners specified by the `banner' group parameter and by
2749 `gnus-article-address-banner-alist'."
2750   (interactive)
2751   (save-excursion
2752     (save-restriction
2753       (let ((inhibit-point-motion-hooks t))
2754         (when (gnus-parameter-banner gnus-newsgroup-name)
2755           (article-really-strip-banner
2756            (gnus-parameter-banner gnus-newsgroup-name)))
2757         (when gnus-article-address-banner-alist
2758           ;; Note that the From header is decoded here, so it is
2759           ;; required that the *-extract-address-components function
2760           ;; supports non-ASCII text.
2761           (let ((from (save-restriction
2762                         (widen)
2763                         (article-narrow-to-head)
2764                         (mail-fetch-field "from"))))
2765             (when (and from
2766                        (setq from
2767                              (cadr (funcall gnus-extract-address-components
2768                                             from))))
2769               (catch 'found
2770                 (dolist (pair gnus-article-address-banner-alist)
2771                   (when (string-match (car pair) from)
2772                     (throw 'found
2773                            (article-really-strip-banner (cdr pair)))))))))))))
2774
2775 (defun article-really-strip-banner (banner)
2776   "Strip the banner specified by the argument."
2777   (save-excursion
2778     (save-restriction
2779       (let ((inhibit-point-motion-hooks t)
2780             (gnus-signature-limit nil)
2781             (inhibit-read-only t))
2782         (article-goto-body)
2783         (cond
2784          ((eq banner 'signature)
2785           (when (gnus-article-narrow-to-signature)
2786             (widen)
2787             (forward-line -1)
2788             (delete-region (point) (point-max))))
2789          ((symbolp banner)
2790           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2791               (while (re-search-forward banner nil t)
2792                 (delete-region (match-beginning 0) (match-end 0)))))
2793          ((stringp banner)
2794           (while (re-search-forward banner nil t)
2795             (delete-region (match-beginning 0) (match-end 0)))))))))
2796
2797 (defun article-babel ()
2798   "Translate article using an online translation service."
2799   (interactive)
2800   (require 'babel)
2801   (gnus-with-article-buffer
2802     (when (article-goto-body)
2803       (let* ((start (point))
2804              (end (point-max))
2805              (orig (buffer-substring start end))
2806              (trans (babel-as-string orig)))
2807         (save-restriction
2808           (narrow-to-region start end)
2809           (delete-region start end)
2810           (insert trans))))))
2811
2812 (defun article-hide-signature (&optional arg)
2813   "Hide the signature in the current article.
2814 If given a negative prefix, always show; if given a positive prefix,
2815 always hide."
2816   (interactive (gnus-article-hidden-arg))
2817   (unless (gnus-article-check-hidden-text 'signature arg)
2818     (save-excursion
2819       (save-restriction
2820         (let ((inhibit-read-only t))
2821           (when (gnus-article-narrow-to-signature)
2822             (gnus-article-hide-text-type
2823              (point-min) (point-max) 'signature))))))
2824   (gnus-set-mode-line 'article))
2825
2826 (defun article-strip-headers-in-body ()
2827   "Strip offensive headers from bodies."
2828   (interactive)
2829   (save-excursion
2830     (article-goto-body)
2831     (let ((case-fold-search t))
2832       (when (looking-at "x-no-archive:")
2833         (gnus-delete-line)))))
2834
2835 (defun article-strip-leading-blank-lines ()
2836   "Remove all blank lines from the beginning of the article."
2837   (interactive)
2838   (save-excursion
2839     (let ((inhibit-point-motion-hooks t)
2840           (inhibit-read-only t))
2841       (when (article-goto-body)
2842         (while (and (not (eobp))
2843                     (looking-at "[ \t]*$"))
2844           (gnus-delete-line))))))
2845
2846 (defun article-narrow-to-head ()
2847   "Narrow the buffer to the head of the message.
2848 Point is left at the beginning of the narrowed-to region."
2849   (narrow-to-region
2850    (goto-char (point-min))
2851    (if (search-forward "\n\n" nil 1)
2852        (1- (point))
2853      (point-max)))
2854   (goto-char (point-min)))
2855
2856 (defun article-goto-body ()
2857   "Place point at the start of the body."
2858   (goto-char (point-min))
2859   (cond
2860    ;; This variable is only bound when dealing with separate
2861    ;; MIME body parts.
2862    (article-goto-body-goes-to-point-min-p
2863     t)
2864    ((search-forward "\n\n" nil t)
2865     t)
2866    (t
2867     (goto-char (point-max))
2868     nil)))
2869
2870 (defun article-strip-multiple-blank-lines ()
2871   "Replace consecutive blank lines with one empty line."
2872   (interactive)
2873   (save-excursion
2874     (let ((inhibit-point-motion-hooks t)
2875           (inhibit-read-only t))
2876       ;; First make all blank lines empty.
2877       (article-goto-body)
2878       (while (re-search-forward "^[ \t]+$" nil t)
2879         (unless (gnus-annotation-in-region-p
2880                  (match-beginning 0) (match-end 0))
2881           (replace-match "" nil t)))
2882       ;; Then replace multiple empty lines with a single empty line.
2883       (article-goto-body)
2884       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2885         (unless (gnus-annotation-in-region-p
2886                  (match-beginning 0) (match-end 0))
2887           (delete-region (match-beginning 1) (match-end 1)))))))
2888
2889 (defun article-strip-leading-space ()
2890   "Remove all white space from the beginning of the lines in the article."
2891   (interactive)
2892   (save-excursion
2893     (let ((inhibit-point-motion-hooks t)
2894           (inhibit-read-only t))
2895       (article-goto-body)
2896       (while (re-search-forward "^[ \t]+" nil t)
2897         (replace-match "" t t)))))
2898
2899 (defun article-strip-trailing-space ()
2900   "Remove all white space from the end of the lines in the article."
2901   (interactive)
2902   (save-excursion
2903     (let ((inhibit-point-motion-hooks t)
2904           (inhibit-read-only t))
2905       (article-goto-body)
2906       (while (re-search-forward "[ \t]+$" nil t)
2907         (replace-match "" t t)))))
2908
2909 (defun article-strip-blank-lines ()
2910   "Strip leading, trailing and multiple blank lines."
2911   (interactive)
2912   (article-strip-leading-blank-lines)
2913   (article-remove-trailing-blank-lines)
2914   (article-strip-multiple-blank-lines))
2915
2916 (defun article-strip-all-blank-lines ()
2917   "Strip all blank lines."
2918   (interactive)
2919   (save-excursion
2920     (let ((inhibit-point-motion-hooks t)
2921           (inhibit-read-only t))
2922       (article-goto-body)
2923       (while (re-search-forward "^[ \t]*\n" nil t)
2924         (replace-match "" t t)))))
2925
2926 (defun gnus-article-narrow-to-signature ()
2927   "Narrow to the signature; return t if a signature is found, else nil."
2928   (let ((inhibit-point-motion-hooks t))
2929     (when (gnus-article-search-signature)
2930       (forward-line 1)
2931       ;; Check whether we have some limits to what we consider
2932       ;; to be a signature.
2933       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2934                       (list gnus-signature-limit)))
2935             limit limited)
2936         (while (setq limit (pop limits))
2937           (if (or (and (integerp limit)
2938                        (< (- (point-max) (point)) limit))
2939                   (and (floatp limit)
2940                        (< (count-lines (point) (point-max)) limit))
2941                   (and (functionp limit)
2942                        (funcall limit))
2943                   (and (stringp limit)
2944                        (not (re-search-forward limit nil t))))
2945               ()                        ; This limit did not succeed.
2946             (setq limited t
2947                   limits nil)))
2948         (unless limited
2949           (narrow-to-region (point) (point-max))
2950           t)))))
2951
2952 (defun gnus-article-search-signature ()
2953   "Search the current buffer for the signature separator.
2954 Put point at the beginning of the signature separator."
2955   (let ((cur (point)))
2956     (goto-char (point-max))
2957     (if (if (stringp gnus-signature-separator)
2958             (re-search-backward gnus-signature-separator nil t)
2959           (let ((seps gnus-signature-separator))
2960             (while (and seps
2961                         (not (re-search-backward (car seps) nil t)))
2962               (pop seps))
2963             seps))
2964         t
2965       (goto-char cur)
2966       nil)))
2967
2968 (defun gnus-article-hidden-arg ()
2969   "Return the current prefix arg as a number, or 0 if no prefix."
2970   (list (if current-prefix-arg
2971             (prefix-numeric-value current-prefix-arg)
2972           0)))
2973
2974 (defun gnus-article-check-hidden-text (type arg)
2975   "Return nil if hiding is necessary.
2976 Arg can be nil or a number.  nil and positive means hide, negative
2977 means show, 0 means toggle."
2978   (save-excursion
2979     (save-restriction
2980       (let ((hide (gnus-article-hidden-text-p type)))
2981         (cond
2982          ((or (null arg)
2983               (> arg 0))
2984           nil)
2985          ((< arg 0)
2986           (gnus-article-show-hidden-text type)
2987           t)
2988          (t
2989           (if (eq hide 'hidden)
2990               (progn
2991                 (gnus-article-show-hidden-text type)
2992                 t)
2993             nil)))))))
2994
2995 (defun gnus-article-hidden-text-p (type)
2996   "Say whether the current buffer contains hidden text of type TYPE."
2997   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2998     (while (and pos
2999                 (not (get-text-property pos 'invisible))
3000                 (not (get-text-property pos 'dummy-invisible)))
3001       (setq pos
3002             (text-property-any (1+ pos) (point-max) 'article-type type)))
3003     (if pos
3004         'hidden
3005       nil)))
3006
3007 (defun gnus-article-show-hidden-text (type &optional dummy)
3008   "Show all hidden text of type TYPE.
3009 Originally it is hide instead of DUMMY."
3010   (let ((inhibit-read-only t)
3011         (inhibit-point-motion-hooks t))
3012     (gnus-remove-text-properties-when
3013      'article-type type
3014      (point-min) (point-max)
3015      (cons 'article-type (cons type
3016                                gnus-hidden-properties)))
3017     (gnus-delete-wash-type type)))
3018
3019 (defconst article-time-units
3020   `((year . ,(* 365.25 24 60 60))
3021     (week . ,(* 7 24 60 60))
3022     (day . ,(* 24 60 60))
3023     (hour . ,(* 60 60))
3024     (minute . 60)
3025     (second . 1))
3026   "Mapping from time units to seconds.")
3027
3028 (defun gnus-article-forward-header ()
3029   "Move point to the start of the next header.
3030 If the current header is a continuation header, this can be several
3031 lines forward."
3032   (let ((ended nil))
3033     (while (not ended)
3034       (forward-line 1)
3035       (if (looking-at "[ \t]+[^ \t]")
3036           (forward-line 1)
3037         (setq ended t)))))
3038
3039 (defun article-date-ut (&optional type highlight)
3040   "Convert DATE date to universal time in the current article.
3041 If TYPE is `local', convert to local time; if it is `lapsed', output
3042 how much time has lapsed since DATE.  For `lapsed', the value of
3043 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3044 should replace the \"Date:\" one, or should be added below it."
3045   (interactive (list 'ut t))
3046   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3047          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3048                              tdate-regexp)
3049                             ((eq type 'lapsed)
3050                              "^X-Sent:[ \t]")
3051                             (article-lapsed-timer
3052                              "^Date:[ \t]")
3053                             (t
3054                              tdate-regexp)))
3055          (case-fold-search t)
3056          (inhibit-read-only t)
3057          (inhibit-point-motion-hooks t)
3058          pos date bface eface)
3059     (save-excursion
3060       (save-restriction
3061         (widen)
3062         (goto-char (point-min))
3063         (while (or (setq date (get-text-property (setq pos (point))
3064                                                  'original-date))
3065                    (when (setq pos (next-single-property-change
3066                                     (point) 'original-date))
3067                      (setq date (get-text-property pos 'original-date))
3068                      t))
3069           (narrow-to-region pos (or (text-property-any pos (point-max)
3070                                                        'original-date nil)
3071                                     (point-max)))
3072           (goto-char (point-min))
3073           (when (re-search-forward tdate-regexp nil t)
3074             (setq bface (get-text-property (point-at-bol) 'face)
3075                   eface (get-text-property (1- (point-at-eol)) 'face)))
3076           (goto-char (point-min))
3077           (setq pos nil)
3078           ;; Delete any old Date headers.
3079           (while (re-search-forward date-regexp nil t)
3080             (if pos
3081                 (delete-region (point-at-bol) (progn
3082                                                 (gnus-article-forward-header)
3083                                                 (point)))
3084               (delete-region (point-at-bol) (progn
3085                                               (gnus-article-forward-header)
3086                                               (forward-char -1)
3087                                               (point)))
3088               (setq pos (point))))
3089           (when (and (not pos)
3090                      (re-search-forward tdate-regexp nil t))
3091             (forward-line 1))
3092           (gnus-goto-char pos)
3093           (insert (article-make-date-line date (or type 'ut)))
3094           (unless pos
3095             (insert "\n")
3096             (forward-line -1))
3097           ;; Do highlighting.
3098           (beginning-of-line)
3099           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3100             (put-text-property (match-beginning 1) (1+ (match-end 1))
3101                                'face bface)
3102             (put-text-property (match-beginning 2) (match-end 2)
3103                                'face eface))
3104           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3105           (goto-char (point-max))
3106           (widen))))))
3107
3108 (defun article-make-date-line (date type)
3109   "Return a DATE line of TYPE."
3110   (unless (memq type '(local ut original user iso8601 lapsed english))
3111     (error "Unknown conversion type: %s" type))
3112   (condition-case ()
3113       (let ((time (date-to-time date)))
3114         (cond
3115          ;; Convert to the local timezone.
3116          ((eq type 'local)
3117           (concat "Date: " (message-make-date time)))
3118          ;; Convert to Universal Time.
3119          ((eq type 'ut)
3120           (concat "Date: "
3121                   (substring
3122                    (message-make-date
3123                     (let* ((e (parse-time-string date))
3124                            (tm (apply 'encode-time e))
3125                            (ms (car tm))
3126                            (ls (- (cadr tm) (car (current-time-zone time)))))
3127                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3128                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3129                             (t (list ms ls)))))
3130                    0 -5)
3131                   "UT"))
3132          ;; Get the original date from the article.
3133          ((eq type 'original)
3134           (concat "Date: " (if (string-match "\n+$" date)
3135                                (substring date 0 (match-beginning 0))
3136                              date)))
3137          ;; Let the user define the format.
3138          ((eq type 'user)
3139           (let ((format (or (condition-case nil
3140                                 (with-current-buffer gnus-summary-buffer
3141                                   gnus-article-time-format)
3142                               (error nil))
3143                             gnus-article-time-format)))
3144             (if (functionp format)
3145                 (funcall format time)
3146               (concat "Date: " (format-time-string format time)))))
3147          ;; ISO 8601.
3148          ((eq type 'iso8601)
3149           (let ((tz (car (current-time-zone time))))
3150             (concat
3151              "Date: "
3152              (format-time-string "%Y%m%dT%H%M%S" time)
3153              (format "%s%02d%02d"
3154                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3155                      (/ (% (abs tz) 3600) 60)))))
3156          ;; Do an X-Sent lapsed format.
3157          ((eq type 'lapsed)
3158           ;; If the date is seriously mangled, the timezone functions are
3159           ;; liable to bug out, so we ignore all errors.
3160           (let* ((now (current-time))
3161                  (real-time (subtract-time now time))
3162                  (real-sec (and real-time
3163                                 (+ (* (float (car real-time)) 65536)
3164                                    (cadr real-time))))
3165                  (sec (and real-time (abs real-sec)))
3166                  num prev)
3167             (cond
3168              ((null real-time)
3169               "X-Sent: Unknown")
3170              ((zerop sec)
3171               "X-Sent: Now")
3172              (t
3173               (concat
3174                "X-Sent: "
3175                ;; This is a bit convoluted, but basically we go
3176                ;; through the time units for years, weeks, etc,
3177                ;; and divide things to see whether that results
3178                ;; in positive answers.
3179                (mapconcat
3180                 (lambda (unit)
3181                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3182                       ;; The (remaining) seconds are too few to
3183                       ;; be divided into this time unit.
3184                       ""
3185                     ;; It's big enough, so we output it.
3186                     (setq sec (- sec (* num (cdr unit))))
3187                     (prog1
3188                         (concat (if prev ", " "") (int-to-string
3189                                                    (floor num))
3190                                 " " (symbol-name (car unit))
3191                                 (if (> num 1) "s" ""))
3192                       (setq prev t))))
3193                 article-time-units "")
3194                ;; If dates are odd, then it might appear like the
3195                ;; article was sent in the future.
3196                (if (> real-sec 0)
3197                    " ago"
3198                  " in the future"))))))
3199          ;; Display the date in proper English
3200          ((eq type 'english)
3201           (let ((dtime (decode-time time)))
3202             (concat
3203              "Date: the "
3204              (number-to-string (nth 3 dtime))
3205              (let ((digit (% (nth 3 dtime) 10)))
3206                (cond
3207                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3208                 ((= digit 1) "st")
3209                 ((= digit 2) "nd")
3210                 ((= digit 3) "rd")
3211                 (t "th")))
3212              " of "
3213              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3214              " "
3215              (number-to-string (nth 5 dtime))
3216              " at "
3217              (format "%02d" (nth 2 dtime))
3218              ":"
3219              (format "%02d" (nth 1 dtime)))))))
3220     (error
3221      (format "Date: %s (from Gnus)" date))))
3222
3223 (defun article-date-local (&optional highlight)
3224   "Convert the current article date to the local timezone."
3225   (interactive (list t))
3226   (article-date-ut 'local highlight))
3227
3228 (defun article-date-english (&optional highlight)
3229   "Convert the current article date to something that is proper English."
3230   (interactive (list t))
3231   (article-date-ut 'english highlight))
3232
3233 (defun article-date-original (&optional highlight)
3234   "Convert the current article date to what it was originally.
3235 This is only useful if you have used some other date conversion
3236 function and want to see what the date was before converting."
3237   (interactive (list t))
3238   (article-date-ut 'original highlight))
3239
3240 (defun article-date-lapsed (&optional highlight)
3241   "Convert the current article date to time lapsed since it was sent."
3242   (interactive (list t))
3243   (article-date-ut 'lapsed highlight))
3244
3245 (defun article-update-date-lapsed ()
3246   "Function to be run from a timer to update the lapsed time line."
3247   (save-match-data
3248     (let (deactivate-mark)
3249       (save-excursion
3250         (ignore-errors
3251          (walk-windows
3252           (lambda (w)
3253             (set-buffer (window-buffer w))
3254             (when (eq major-mode 'gnus-article-mode)
3255               (let ((mark (point-marker)))
3256                 (goto-char (point-min))
3257                 (when (re-search-forward "^X-Sent:" nil t)
3258                   (article-date-lapsed t))
3259                 (goto-char (marker-position mark))
3260                 (move-marker mark nil))))
3261           nil 'visible))))))
3262
3263 (defun gnus-start-date-timer (&optional n)
3264   "Start a timer to update the X-Sent header in the article buffers.
3265 The numerical prefix says how frequently (in seconds) the function
3266 is to run."
3267   (interactive "p")
3268   (unless n
3269     (setq n 1))
3270   (gnus-stop-date-timer)
3271   (setq article-lapsed-timer
3272         (run-at-time 1 n 'article-update-date-lapsed)))
3273
3274 (defun gnus-stop-date-timer ()
3275   "Stop the X-Sent timer."
3276   (interactive)
3277   (when article-lapsed-timer
3278     (nnheader-cancel-timer article-lapsed-timer)
3279     (setq article-lapsed-timer nil)))
3280
3281 (defun article-date-user (&optional highlight)
3282   "Convert the current article date to the user-defined format.
3283 This format is defined by the `gnus-article-time-format' variable."
3284   (interactive (list t))
3285   (article-date-ut 'user highlight))
3286
3287 (defun article-date-iso8601 (&optional highlight)
3288   "Convert the current article date to ISO8601."
3289   (interactive (list t))
3290   (article-date-ut 'iso8601 highlight))
3291
3292 (defmacro gnus-article-save-original-date (&rest forms)
3293   "Save the original date as a text property and evaluate FORMS."
3294   `(let* ((case-fold-search t)
3295           (start (progn
3296                    (goto-char (point-min))
3297                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3298                               (not (bolp)))
3299                      (match-end 0))))
3300           (date (when (and start
3301                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3302                                               nil t))
3303                   (buffer-substring-no-properties start
3304                                                   (match-beginning 0)))))
3305      (goto-char (point-max))
3306      (skip-chars-backward "\n")
3307      (put-text-property (point-min) (point) 'original-date date)
3308      ,@forms
3309      (goto-char (point-max))
3310      (skip-chars-backward "\n")
3311      (put-text-property (point-min) (point) 'original-date date)))
3312
3313 ;; (defun article-show-all ()
3314 ;;   "Show all hidden text in the article buffer."
3315 ;;   (interactive)
3316 ;;   (save-excursion
3317 ;;     (let ((inhibit-read-only t))
3318 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3319
3320 (defun article-remove-leading-whitespace ()
3321   "Remove excessive whitespace from all headers."
3322   (interactive)
3323   (save-excursion
3324     (save-restriction
3325       (let ((inhibit-read-only t))
3326         (article-narrow-to-head)
3327         (goto-char (point-min))
3328         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3329           (delete-region (match-beginning 1) (match-end 1)))))))
3330
3331 (defun article-emphasize (&optional arg)
3332   "Emphasize text according to `gnus-emphasis-alist'."
3333   (interactive (gnus-article-hidden-arg))
3334   (unless (gnus-article-check-hidden-text 'emphasis arg)
3335     (save-excursion
3336       (let ((alist (or
3337                     (condition-case nil
3338                         (with-current-buffer gnus-summary-buffer
3339                           gnus-article-emphasis-alist)
3340                       (error))
3341                     gnus-emphasis-alist))
3342             (inhibit-read-only t)
3343             (props (append '(article-type emphasis)
3344                            gnus-hidden-properties))
3345             regexp elem beg invisible visible face)
3346         (article-goto-body)
3347         (setq beg (point))
3348         (while (setq elem (pop alist))
3349           (goto-char beg)
3350           (setq regexp (car elem)
3351                 invisible (nth 1 elem)
3352                 visible (nth 2 elem)
3353                 face (nth 3 elem))
3354           (while (re-search-forward regexp nil t)
3355             (when (and (match-beginning visible) (match-beginning invisible))
3356               (gnus-article-hide-text
3357                (match-beginning invisible) (match-end invisible) props)
3358               (gnus-article-unhide-text-type
3359                (match-beginning visible) (match-end visible) 'emphasis)
3360               (gnus-put-overlay-excluding-newlines
3361                (match-beginning visible) (match-end visible) 'face face)
3362               (gnus-add-wash-type 'emphasis)
3363               (goto-char (match-end invisible)))))))))
3364
3365 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3366   "Setup newsgroup emphasis alist."
3367   (unless gnus-article-emphasis-alist
3368     (let ((name (and gnus-newsgroup-name
3369                      (gnus-group-real-name gnus-newsgroup-name))))
3370       (make-local-variable 'gnus-article-emphasis-alist)
3371       (setq gnus-article-emphasis-alist
3372             (nconc
3373              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3374                (while (setq elem (pop alist))
3375                  (when (and name (string-match (car elem) name))
3376                    (setq alist nil
3377                          highlight (copy-sequence (cdr elem)))))
3378                highlight)
3379              (copy-sequence highlight-words)
3380              (if gnus-newsgroup-name
3381                  (copy-sequence (gnus-group-find-parameter
3382                                  gnus-newsgroup-name 'highlight-words t)))
3383              gnus-emphasis-alist)))))
3384
3385 (eval-when-compile
3386   (defvar gnus-summary-article-menu)
3387   (defvar gnus-summary-post-menu))
3388
3389 ;;; Saving functions.
3390
3391 (defun gnus-article-save (save-buffer file &optional num)
3392   "Save the currently selected article."
3393   (unless gnus-save-all-headers
3394     ;; Remove headers according to `gnus-saved-headers'.
3395     (let ((gnus-visible-headers
3396            (or gnus-saved-headers gnus-visible-headers))
3397           (gnus-article-buffer save-buffer))
3398       (save-excursion
3399         (set-buffer save-buffer)
3400         (article-hide-headers 1 t))))
3401   (save-window-excursion
3402     (if (not gnus-default-article-saver)
3403         (error "No default saver is defined")
3404       ;; !!! Magic!  The saving functions all save
3405       ;; `gnus-save-article-buffer' (or so they think), but we
3406       ;; bind that variable to our save-buffer.
3407       (set-buffer gnus-article-buffer)
3408       (let* ((gnus-save-article-buffer save-buffer)
3409              (filename
3410               (cond
3411                ((not gnus-prompt-before-saving) 'default)
3412                ((eq gnus-prompt-before-saving 'always) nil)
3413                (t file)))
3414              (gnus-number-of-articles-to-be-saved
3415               (when (eq gnus-prompt-before-saving t)
3416                 num)))                  ; Magic
3417         (set-buffer gnus-article-current-summary)
3418         (funcall gnus-default-article-saver filename)))))
3419
3420 (defun gnus-read-save-file-name (prompt &optional filename
3421                                         function group headers variable)
3422   (let ((default-name
3423           (funcall function group headers (symbol-value variable)))
3424         result)
3425     (setq result
3426           (expand-file-name
3427            (cond
3428             ((eq filename 'default)
3429              default-name)
3430             ((eq filename t)
3431              default-name)
3432             (filename filename)
3433             (t
3434              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3435                     (prompt
3436                      (format prompt
3437                              (if (and gnus-number-of-articles-to-be-saved
3438                                       (> gnus-number-of-articles-to-be-saved 1))
3439                                  (format "these %d articles"
3440                                          gnus-number-of-articles-to-be-saved)
3441                                "this article")))
3442                     (file
3443                      ;; Let the split methods have their say.
3444                      (cond
3445                       ;; No split name was found.
3446                       ((null split-name)
3447                        (read-file-name
3448                         (concat prompt " (default "
3449                                 (file-name-nondirectory default-name) "): ")
3450                         (file-name-directory default-name)
3451                         default-name))
3452                       ;; A single group name is returned.
3453                       ((stringp split-name)
3454                        (setq default-name
3455                              (funcall function split-name headers
3456                                       (symbol-value variable)))
3457                        (read-file-name
3458                         (concat prompt " (default "
3459                                 (file-name-nondirectory default-name) "): ")
3460                         (file-name-directory default-name)
3461                         default-name))
3462                       ;; A single split name was found
3463                       ((= 1 (length split-name))
3464                        (let* ((name (expand-file-name
3465                                      (car split-name)
3466                                      gnus-article-save-directory))
3467                               (dir (cond ((file-directory-p name)
3468                                           (file-name-as-directory name))
3469                                          ((file-exists-p name) name)
3470                                          (t gnus-article-save-directory))))
3471                          (read-file-name
3472                           (concat prompt " (default " name "): ")
3473                           dir name)))
3474                       ;; A list of splits was found.
3475                       (t
3476                        (setq split-name (nreverse split-name))
3477                        (let (result)
3478                          (let ((file-name-history
3479                                 (nconc split-name file-name-history)))
3480                            (setq result
3481                                  (expand-file-name
3482                                   (read-file-name
3483                                    (concat prompt " (`M-p' for defaults): ")
3484                                    gnus-article-save-directory
3485                                    (car split-name))
3486                                   gnus-article-save-directory)))
3487                          (car (push result file-name-history)))))))
3488                ;; Create the directory.
3489                (gnus-make-directory (file-name-directory file))
3490                ;; If we have read a directory, we append the default file name.
3491                (when (file-directory-p file)
3492                  (setq file (expand-file-name (file-name-nondirectory
3493                                                default-name)
3494                                               (file-name-as-directory file))))
3495                ;; Possibly translate some characters.
3496                (nnheader-translate-file-chars file))))))
3497     (gnus-make-directory (file-name-directory result))
3498     (set variable result)))
3499
3500 (defun gnus-article-archive-name (group)
3501   "Return the first instance of an \"Archive-name\" in the current buffer."
3502   (let ((case-fold-search t))
3503     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3504       (nnheader-concat gnus-article-save-directory
3505                        (match-string 1)))))
3506
3507 (defun gnus-article-nndoc-name (group)
3508   "If GROUP is an nndoc group, return the name of the parent group."
3509   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3510     (gnus-group-get-parameter group 'save-article-group)))
3511
3512 (defun gnus-summary-save-in-rmail (&optional filename)
3513   "Append this article to Rmail file.
3514 Optional argument FILENAME specifies file name.
3515 Directory to save to is default to `gnus-article-save-directory'."
3516   (setq filename (gnus-read-save-file-name
3517                   "Save %s in rmail file" filename
3518                   gnus-rmail-save-name gnus-newsgroup-name
3519                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3520   (gnus-eval-in-buffer-window gnus-save-article-buffer
3521     (save-excursion
3522       (save-restriction
3523         (widen)
3524         (gnus-output-to-rmail filename))))
3525   filename)
3526
3527 (defun gnus-summary-save-in-mail (&optional filename)
3528   "Append this article to Unix mail file.
3529 Optional argument FILENAME specifies file name.
3530 Directory to save to is default to `gnus-article-save-directory'."
3531   (setq filename (gnus-read-save-file-name
3532                   "Save %s in Unix mail file" filename
3533                   gnus-mail-save-name gnus-newsgroup-name
3534                   gnus-current-headers 'gnus-newsgroup-last-mail))
3535   (gnus-eval-in-buffer-window gnus-save-article-buffer
3536     (save-excursion
3537       (save-restriction
3538         (widen)
3539         (if (and (file-readable-p filename)
3540                  (file-regular-p filename)
3541                  (mail-file-babyl-p filename))
3542             (rmail-output-to-rmail-file filename t)
3543           (gnus-output-to-mail filename)))))
3544   filename)
3545
3546 (defun gnus-summary-save-in-file (&optional filename overwrite)
3547   "Append this article to file.
3548 Optional argument FILENAME specifies file name.
3549 Directory to save to is default to `gnus-article-save-directory'."
3550   (setq filename (gnus-read-save-file-name
3551                   "Save %s in file" filename
3552                   gnus-file-save-name gnus-newsgroup-name
3553                   gnus-current-headers 'gnus-newsgroup-last-file))
3554   (gnus-eval-in-buffer-window gnus-save-article-buffer
3555     (save-excursion
3556       (save-restriction
3557         (widen)
3558         (when (and overwrite
3559                    (file-exists-p filename))
3560           (delete-file filename))
3561         (gnus-output-to-file filename))))
3562   filename)
3563
3564 (defun gnus-summary-write-to-file (&optional filename)
3565   "Write this article to a file, overwriting it if the file exists.
3566 Optional argument FILENAME specifies file name.
3567 The directory to save in defaults to `gnus-article-save-directory'."
3568   (gnus-summary-save-in-file nil t))
3569
3570 (defun gnus-summary-save-body-in-file (&optional filename)
3571   "Append this article body to a file.
3572 Optional argument FILENAME specifies file name.
3573 The directory to save in defaults to `gnus-article-save-directory'."
3574   (setq filename (gnus-read-save-file-name
3575                   "Save %s body in file" filename
3576                   gnus-file-save-name gnus-newsgroup-name
3577                   gnus-current-headers 'gnus-newsgroup-last-file))
3578   (gnus-eval-in-buffer-window gnus-save-article-buffer
3579     (save-excursion
3580       (save-restriction
3581         (widen)
3582         (when (article-goto-body)
3583           (narrow-to-region (point) (point-max)))
3584         (gnus-output-to-file filename))))
3585   filename)
3586
3587 (defun gnus-summary-save-in-pipe (&optional command)
3588   "Pipe this article to subprocess."
3589   (setq command
3590         (cond ((and (eq command 'default)
3591                     gnus-last-shell-command)
3592                gnus-last-shell-command)
3593               ((stringp command)
3594                command)
3595               (t (read-string
3596                   (format
3597                    "Shell command on %s: "
3598                    (if (and gnus-number-of-articles-to-be-saved
3599                             (> gnus-number-of-articles-to-be-saved 1))
3600                        (format "these %d articles"
3601                                gnus-number-of-articles-to-be-saved)
3602                      "this article"))
3603                   gnus-last-shell-command))))
3604   (when (string-equal command "")
3605     (if gnus-last-shell-command
3606         (setq command gnus-last-shell-command)
3607       (error "A command is required")))
3608   (gnus-eval-in-buffer-window gnus-article-buffer
3609     (save-restriction
3610       (widen)
3611       (shell-command-on-region (point-min) (point-max) command nil)))
3612   (setq gnus-last-shell-command command))
3613
3614 (defun gnus-summary-pipe-to-muttprint (&optional command)
3615   "Pipe this article to muttprint."
3616   (setq command (read-string
3617                  "Print using command: " gnus-summary-muttprint-program
3618                  nil gnus-summary-muttprint-program))
3619   (gnus-summary-save-in-pipe command))
3620
3621 ;;; Article file names when saving.
3622
3623 (defun gnus-capitalize-newsgroup (newsgroup)
3624   "Capitalize NEWSGROUP name."
3625   (when (not (zerop (length newsgroup)))
3626     (concat (char-to-string (upcase (aref newsgroup 0)))
3627             (substring newsgroup 1))))
3628
3629 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3630   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3631 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3632 Otherwise, it is like ~/News/news/group/num."
3633   (let ((default
3634           (expand-file-name
3635            (concat (if (gnus-use-long-file-name 'not-save)
3636                        (gnus-capitalize-newsgroup newsgroup)
3637                      (gnus-newsgroup-directory-form newsgroup))
3638                    "/" (int-to-string (mail-header-number headers)))
3639            gnus-article-save-directory)))
3640     (if (and last-file
3641              (string-equal (file-name-directory default)
3642                            (file-name-directory last-file))
3643              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3644         default
3645       (or last-file default))))
3646
3647 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3648   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3649 If variable `gnus-use-long-file-name' is non-nil, it is
3650 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3651   (let ((default
3652           (expand-file-name
3653            (concat (if (gnus-use-long-file-name 'not-save)
3654                        newsgroup
3655                      (gnus-newsgroup-directory-form newsgroup))
3656                    "/" (int-to-string (mail-header-number headers)))
3657            gnus-article-save-directory)))
3658     (if (and last-file
3659              (string-equal (file-name-directory default)
3660                            (file-name-directory last-file))
3661              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3662         default
3663       (or last-file default))))
3664
3665 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3666   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3667 If variable `gnus-use-long-file-name' is non-nil, it is
3668 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3669   (or last-file
3670       (expand-file-name
3671        (if (gnus-use-long-file-name 'not-save)
3672            newsgroup
3673          (file-relative-name
3674           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3675           default-directory))
3676        gnus-article-save-directory)))
3677
3678 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3679   "Generate file name from sender."
3680   (let ((from (mail-header-from headers)))
3681     (expand-file-name
3682      (if (and from (string-match "\\([^ <]+\\)@" from))
3683          (match-string 1 from)
3684        "nobody")
3685      gnus-article-save-directory)))
3686
3687 (defun article-verify-x-pgp-sig ()
3688   "Verify X-PGP-Sig."
3689   (interactive)
3690   (if (gnus-buffer-live-p gnus-original-article-buffer)
3691       (let ((sig (with-current-buffer gnus-original-article-buffer
3692                    (gnus-fetch-field "X-PGP-Sig")))
3693             items info headers)
3694         (when (and sig
3695                    mml2015-use
3696                    (mml2015-clear-verify-function))
3697           (with-temp-buffer
3698             (insert-buffer-substring gnus-original-article-buffer)
3699             (setq items (split-string sig))
3700             (message-narrow-to-head)
3701             (let ((inhibit-point-motion-hooks t)
3702                   (case-fold-search t))
3703               ;; Don't verify multiple headers.
3704               (setq headers (mapconcat (lambda (header)
3705                                          (concat header ": "
3706                                                  (mail-fetch-field header)
3707                                                  "\n"))
3708                                        (split-string (nth 1 items) ",") "")))
3709             (delete-region (point-min) (point-max))
3710             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3711             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3712             (insert headers)
3713             (widen)
3714             (forward-line)
3715             (while (not (eobp))
3716               (if (looking-at "^-")
3717                   (insert "- "))
3718               (forward-line))
3719             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3720             (insert "Version: " (car items) "\n\n")
3721             (insert (mapconcat 'identity (cddr items) "\n"))
3722             (insert "\n-----END PGP SIGNATURE-----\n")
3723             (let ((mm-security-handle (list (format "multipart/signed"))))
3724               (mml2015-clean-buffer)
3725               (let ((coding-system-for-write (or gnus-newsgroup-charset
3726                                                  'iso-8859-1)))
3727                 (funcall (mml2015-clear-verify-function)))
3728               (setq info
3729                     (or (mm-handle-multipart-ctl-parameter
3730                          mm-security-handle 'gnus-details)
3731                         (mm-handle-multipart-ctl-parameter
3732                          mm-security-handle 'gnus-info)))))
3733           (when info
3734             (let ((inhibit-read-only t) bface eface)
3735               (save-restriction
3736                 (message-narrow-to-head)
3737                 (goto-char (point-max))
3738                 (forward-line -1)
3739                 (setq bface (get-text-property (point-at-bol) 'face)
3740                       eface (get-text-property (1- (point-at-eol)) 'face))
3741                 (message-remove-header "X-Gnus-PGP-Verify")
3742                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3743                     (forward-line)
3744                   (goto-char (point-max)))
3745                 (narrow-to-region (point) (point))
3746                 (insert "X-Gnus-PGP-Verify: " info "\n")
3747                 (goto-char (point-min))
3748                 (forward-line)
3749                 (while (not (eobp))
3750                   (if (not (looking-at "^[ \t]"))
3751                       (insert " "))
3752                   (forward-line))
3753                 ;; Do highlighting.
3754                 (goto-char (point-min))
3755                 (when (looking-at "\\([^:]+\\): *")
3756                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3757                                      'face bface)
3758                   (put-text-property (match-end 0) (point-max)
3759                                      'face eface)))))))))
3760
3761 (defun article-verify-cancel-lock ()
3762   "Verify Cancel-Lock header."
3763   (interactive)
3764   (if (gnus-buffer-live-p gnus-original-article-buffer)
3765       (canlock-verify gnus-original-article-buffer)))
3766
3767 (eval-and-compile
3768   (mapcar
3769    (lambda (func)
3770      (let (afunc gfunc)
3771        (if (consp func)
3772            (setq afunc (car func)
3773                  gfunc (cdr func))
3774          (setq afunc func
3775                gfunc (intern (format "gnus-%s" func))))
3776        (defalias gfunc
3777          (when (fboundp afunc)
3778            `(lambda (&optional interactive &rest args)
3779               ,(documentation afunc t)
3780               (interactive (list t))
3781               (save-excursion
3782                 (set-buffer gnus-article-buffer)
3783                 (if interactive
3784                     (call-interactively ',afunc)
3785                   (apply ',afunc args))))))))
3786    '(article-hide-headers
3787      article-verify-x-pgp-sig
3788      article-verify-cancel-lock
3789      article-hide-boring-headers
3790      article-treat-overstrike
3791      article-treat-ansi-sequences
3792      article-fill-long-lines
3793      article-capitalize-sentences
3794      article-remove-cr
3795      article-remove-leading-whitespace
3796      article-display-x-face
3797      article-display-face
3798      article-de-quoted-unreadable
3799      article-de-base64-unreadable
3800      article-decode-HZ
3801      article-wash-html
3802      article-unsplit-urls
3803      article-hide-list-identifiers
3804      article-strip-banner
3805      article-babel
3806      article-hide-pem
3807      article-hide-signature
3808      article-strip-headers-in-body
3809      article-remove-trailing-blank-lines
3810      article-strip-leading-blank-lines
3811      article-strip-multiple-blank-lines
3812      article-strip-leading-space
3813      article-strip-trailing-space
3814      article-strip-blank-lines
3815      article-strip-all-blank-lines
3816      article-date-local
3817      article-date-english
3818      article-date-iso8601
3819      article-date-original
3820      article-date-ut
3821      article-decode-mime-words
3822      article-decode-charset
3823      article-decode-encoded-words
3824      article-date-user
3825      article-date-lapsed
3826      article-emphasize
3827      article-treat-dumbquotes
3828      article-normalize-headers
3829 ;;     (article-show-all . gnus-article-show-all-headers)
3830      )))
3831 \f
3832 ;;;
3833 ;;; Gnus article mode
3834 ;;;
3835
3836 (put 'gnus-article-mode 'mode-class 'special)
3837
3838 (set-keymap-parent gnus-article-mode-map widget-keymap)
3839
3840 (gnus-define-keys gnus-article-mode-map
3841   " " gnus-article-goto-next-page
3842   "\177" gnus-article-goto-prev-page
3843   [delete] gnus-article-goto-prev-page
3844   [backspace] gnus-article-goto-prev-page
3845   "\C-c^" gnus-article-refer-article
3846   "h" gnus-article-show-summary
3847   "s" gnus-article-show-summary
3848   "\C-c\C-m" gnus-article-mail
3849   "?" gnus-article-describe-briefly
3850   "e" gnus-summary-edit-article
3851   "<" beginning-of-buffer
3852   ">" end-of-buffer
3853   "\C-c\C-i" gnus-info-find-node
3854   "\C-c\C-b" gnus-bug
3855   "R" gnus-article-reply-with-original
3856   "F" gnus-article-followup-with-original
3857   "\C-hk" gnus-article-describe-key
3858   "\C-hc" gnus-article-describe-key-briefly
3859
3860   "\C-d" gnus-article-read-summary-keys
3861   "\M-*" gnus-article-read-summary-keys
3862   "\M-#" gnus-article-read-summary-keys
3863   "\M-^" gnus-article-read-summary-keys
3864   "\M-g" gnus-article-read-summary-keys)
3865
3866 (substitute-key-definition
3867  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
3868
3869 (defun gnus-article-make-menu-bar ()
3870   (unless (boundp 'gnus-article-commands-menu)
3871     (gnus-summary-make-menu-bar))
3872   (gnus-turn-off-edit-menu 'article)
3873   (unless (boundp 'gnus-article-article-menu)
3874     (easy-menu-define
3875      gnus-article-article-menu gnus-article-mode-map ""
3876      '("Article"
3877        ["Scroll forwards" gnus-article-goto-next-page t]
3878        ["Scroll backwards" gnus-article-goto-prev-page t]
3879        ["Show summary" gnus-article-show-summary t]
3880        ["Fetch Message-ID at point" gnus-article-refer-article t]
3881        ["Mail to address at point" gnus-article-mail t]
3882        ["Send a bug report" gnus-bug t]))
3883
3884     (easy-menu-define
3885      gnus-article-treatment-menu gnus-article-mode-map ""
3886      ;; Fixme: this should use :active (and maybe :visible).
3887      '("Treatment"
3888        ["Hide headers" gnus-article-hide-headers t]
3889        ["Hide signature" gnus-article-hide-signature t]
3890        ["Hide citation" gnus-article-hide-citation t]
3891        ["Treat overstrike" gnus-article-treat-overstrike t]
3892        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3893        ["Remove carriage return" gnus-article-remove-cr t]
3894        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3895        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
3896        ["Remove base64" gnus-article-de-base64-unreadable t]
3897        ["Treat html" gnus-article-wash-html t]
3898        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3899        ["Decode HZ" gnus-article-decode-HZ t]))
3900
3901     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3902
3903     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3904
3905     (gnus-run-hooks 'gnus-article-menu-hook)))
3906
3907 (defun gnus-article-mode ()
3908   "Major mode for displaying an article.
3909
3910 All normal editing commands are switched off.
3911
3912 The following commands are available in addition to all summary mode
3913 commands:
3914 \\<gnus-article-mode-map>
3915 \\[gnus-article-next-page]\t Scroll the article one page forwards
3916 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3917 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3918 \\[gnus-article-show-summary]\t Display the summary buffer
3919 \\[gnus-article-mail]\t Send a reply to the address near point
3920 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3921 \\[gnus-info-find-node]\t Go to the Gnus info node"
3922   (interactive)
3923   (kill-all-local-variables)
3924   (gnus-simplify-mode-line)
3925   (setq mode-name "Article")
3926   (setq major-mode 'gnus-article-mode)
3927   (make-local-variable 'minor-mode-alist)
3928   (use-local-map gnus-article-mode-map)
3929   (when (gnus-visual-p 'article-menu 'menu)
3930     (gnus-article-make-menu-bar)
3931     (when gnus-summary-tool-bar-map
3932       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3933   (gnus-update-format-specifications nil 'article-mode)
3934   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3935   (set (make-local-variable 'gnus-page-broken) nil)
3936   (make-local-variable 'gnus-button-marker-list)
3937   (make-local-variable 'gnus-article-current-summary)
3938   (make-local-variable 'gnus-article-mime-handles)
3939   (make-local-variable 'gnus-article-decoded-p)
3940   (make-local-variable 'gnus-article-mime-handle-alist)
3941   (make-local-variable 'gnus-article-wash-types)
3942   (make-local-variable 'gnus-article-image-alist)
3943   (make-local-variable 'gnus-article-charset)
3944   (make-local-variable 'gnus-article-ignored-charsets)
3945   ;; Prevent recent Emacsen from displaying non-break space as "\ ".
3946   (set (make-local-variable 'nobreak-char-display) nil)
3947   (gnus-set-default-directory)
3948   (buffer-disable-undo)
3949   (setq buffer-read-only t
3950         show-trailing-whitespace nil)
3951   (set-syntax-table gnus-article-mode-syntax-table)
3952   (mm-enable-multibyte)
3953   (gnus-run-mode-hooks 'gnus-article-mode-hook))
3954
3955 (defun gnus-article-setup-buffer ()
3956   "Initialize the article buffer."
3957   (let* ((name (if gnus-single-article-buffer "*Article*"
3958                  (concat "*Article " gnus-newsgroup-name "*")))
3959          (original
3960           (progn (string-match "\\*Article" name)
3961                  (concat " *Original Article"
3962                          (substring name (match-end 0))))))
3963     (setq gnus-article-buffer name)
3964     (setq gnus-original-article-buffer original)
3965     (setq gnus-article-mime-handle-alist nil)
3966     ;; This might be a variable local to the summary buffer.
3967     (unless gnus-single-article-buffer
3968       (save-excursion
3969         (set-buffer gnus-summary-buffer)
3970         (setq gnus-article-buffer name)
3971         (setq gnus-original-article-buffer original)
3972         (gnus-set-global-variables)))
3973     (gnus-article-setup-highlight-words)
3974     ;; Init original article buffer.
3975     (save-excursion
3976       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3977       (mm-enable-multibyte)
3978       (setq major-mode 'gnus-original-article-mode)
3979       (make-local-variable 'gnus-original-article))
3980     (if (and (get-buffer name)
3981              (with-current-buffer name
3982                (if gnus-article-edit-mode
3983                    (if (y-or-n-p "Article mode edit in progress; discard? ")
3984                        (progn
3985                          (set-buffer-modified-p nil)
3986                          (gnus-kill-buffer name)
3987                          (message "")
3988                          nil)
3989                      (error "Action aborted"))
3990                  t)))
3991         (save-excursion
3992           (set-buffer name)
3993           (set (make-local-variable 'gnus-article-edit-mode) nil)
3994           (when gnus-article-mime-handles
3995             (mm-destroy-parts gnus-article-mime-handles)
3996             (setq gnus-article-mime-handles nil))
3997           ;; Set it to nil in article-buffer!
3998           (setq gnus-article-mime-handle-alist nil)
3999           (buffer-disable-undo)
4000           (setq buffer-read-only t)
4001           ;; This list just keeps growing if we don't reset it.
4002           (setq gnus-button-marker-list nil)
4003           (unless (eq major-mode 'gnus-article-mode)
4004             (gnus-article-mode))
4005           (current-buffer))
4006       (save-excursion
4007         (set-buffer (gnus-get-buffer-create name))
4008         (gnus-article-mode)
4009         (make-local-variable 'gnus-summary-buffer)
4010         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4011         (current-buffer)))))
4012
4013 ;; Set article window start at LINE, where LINE is the number of lines
4014 ;; from the head of the article.
4015 (defun gnus-article-set-window-start (&optional line)
4016   (set-window-start
4017    (gnus-get-buffer-window gnus-article-buffer t)
4018    (save-excursion
4019      (set-buffer gnus-article-buffer)
4020      (goto-char (point-min))
4021      (if (not line)
4022          (point-min)
4023        (gnus-message 6 "Moved to bookmark")
4024        (search-forward "\n\n" nil t)
4025        (forward-line line)
4026        (point)))))
4027
4028 (defun gnus-article-prepare (article &optional all-headers header)
4029   "Prepare ARTICLE in article mode buffer.
4030 ARTICLE should either be an article number or a Message-ID.
4031 If ARTICLE is an id, HEADER should be the article headers.
4032 If ALL-HEADERS is non-nil, no headers are hidden."
4033   (save-excursion
4034     ;; Make sure we start in a summary buffer.
4035     (unless (eq major-mode 'gnus-summary-mode)
4036       (set-buffer gnus-summary-buffer))
4037     (setq gnus-summary-buffer (current-buffer))
4038     (let* ((gnus-article (if header (mail-header-number header) article))
4039            (summary-buffer (current-buffer))
4040            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4041            (group gnus-newsgroup-name)
4042            result)
4043       (save-excursion
4044         (gnus-article-setup-buffer)
4045         (set-buffer gnus-article-buffer)
4046         ;; Deactivate active regions.
4047         (when (and (boundp 'transient-mark-mode)
4048                    transient-mark-mode)
4049           (setq mark-active nil))
4050         (if (not (setq result (let ((inhibit-read-only t))
4051                                 (gnus-request-article-this-buffer
4052                                  article group))))
4053             ;; There is no such article.
4054             (save-excursion
4055               (when (and (numberp article)
4056                          (not (memq article gnus-newsgroup-sparse)))
4057                 (setq gnus-article-current
4058                       (cons gnus-newsgroup-name article))
4059                 (set-buffer gnus-summary-buffer)
4060                 (setq gnus-current-article article)
4061                 (if (and (memq article gnus-newsgroup-undownloaded)
4062                          (not (gnus-online (gnus-find-method-for-group
4063                                             gnus-newsgroup-name))))
4064                     (progn
4065                       (gnus-summary-set-agent-mark article)
4066                       (message "Message marked for downloading"))
4067                   (gnus-summary-mark-article article gnus-canceled-mark)
4068                   (unless (memq article gnus-newsgroup-sparse)
4069                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4070           (if (or (eq result 'pseudo)
4071                   (eq result 'nneething))
4072               (progn
4073                 (save-excursion
4074                   (set-buffer summary-buffer)
4075                   (push article gnus-newsgroup-history)
4076                   (setq gnus-last-article gnus-current-article
4077                         gnus-current-article 0
4078                         gnus-current-headers nil
4079                         gnus-article-current nil)
4080                   (if (eq result 'nneething)
4081                       (gnus-configure-windows 'summary)
4082                     (gnus-configure-windows 'article))
4083                   (gnus-set-global-variables))
4084                 (let ((gnus-article-mime-handle-alist-1
4085                        gnus-article-mime-handle-alist))
4086                   (gnus-set-mode-line 'article)))
4087             ;; The result from the `request' was an actual article -
4088             ;; or at least some text that is now displayed in the
4089             ;; article buffer.
4090             (when (and (numberp article)
4091                        (not (eq article gnus-current-article)))
4092               ;; Seems like a new article has been selected.
4093               ;; `gnus-current-article' must be an article number.
4094               (save-excursion
4095                 (set-buffer summary-buffer)
4096                 (push article gnus-newsgroup-history)
4097                 (setq gnus-last-article gnus-current-article
4098                       gnus-current-article article
4099                       gnus-current-headers
4100                       (gnus-summary-article-header gnus-current-article)
4101                       gnus-article-current
4102                       (cons gnus-newsgroup-name gnus-current-article))
4103                 (unless (vectorp gnus-current-headers)
4104                   (setq gnus-current-headers nil))
4105                 (gnus-summary-goto-subject gnus-current-article)
4106                 (when (gnus-summary-show-thread)
4107                   ;; If the summary buffer really was folded, the
4108                   ;; previous goto may not actually have gone to
4109                   ;; the right article, but the thread root instead.
4110                   ;; So we go again.
4111                   (gnus-summary-goto-subject gnus-current-article))
4112                 (gnus-run-hooks 'gnus-mark-article-hook)
4113                 (gnus-set-mode-line 'summary)
4114                 (when (gnus-visual-p 'article-highlight 'highlight)
4115                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4116                 ;; Set the global newsgroup variables here.
4117                 (gnus-set-global-variables)
4118                 (setq gnus-have-all-headers
4119                       (or all-headers gnus-show-all-headers))))
4120             (save-excursion
4121               (gnus-configure-windows 'article))
4122             (when (or (numberp article)
4123                       (stringp article))
4124               (gnus-article-prepare-display)
4125               ;; Do page break.
4126               (goto-char (point-min))
4127               (when gnus-break-pages
4128                 (gnus-narrow-to-page)))
4129             (let ((gnus-article-mime-handle-alist-1
4130                    gnus-article-mime-handle-alist))
4131               (gnus-set-mode-line 'article))
4132             (article-goto-body)
4133             (unless (bobp)
4134               (forward-line -1))
4135             (set-window-point (get-buffer-window (current-buffer)) (point))
4136             (gnus-configure-windows 'article)
4137             t))))))
4138
4139 ;;;###autoload
4140 (defun gnus-article-prepare-display ()
4141   "Make the current buffer look like a nice article."
4142   ;; Hooks for getting information from the article.
4143   ;; This hook must be called before being narrowed.
4144   (let ((gnus-article-buffer (current-buffer))
4145         buffer-read-only
4146         (inhibit-read-only t))
4147     (unless (eq major-mode 'gnus-article-mode)
4148       (gnus-article-mode))
4149     (setq buffer-read-only nil
4150           gnus-article-wash-types nil
4151           gnus-article-image-alist nil)
4152     (gnus-run-hooks 'gnus-tmp-internal-hook)
4153     (when gnus-display-mime-function
4154       (funcall gnus-display-mime-function))
4155     (gnus-run-hooks 'gnus-article-prepare-hook)))
4156
4157 ;;;
4158 ;;; Gnus MIME viewing functions
4159 ;;;
4160
4161 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4162   "Format of the MIME buttons.
4163
4164 Valid specifiers include:
4165 %t  The MIME type
4166 %T  MIME type, along with additional info
4167 %n  The `name' parameter
4168 %d  The description, if any
4169 %l  The length of the encoded part
4170 %p  The part identifier number
4171 %e  Dots if the part isn't displayed
4172
4173 General format specifiers can also be used.  See Info node
4174 `(gnus)Formatting Variables'.")
4175
4176 (defvar gnus-mime-button-line-format-alist
4177   '((?t gnus-tmp-type ?s)
4178     (?T gnus-tmp-type-long ?s)
4179     (?n gnus-tmp-name ?s)
4180     (?d gnus-tmp-description ?s)
4181     (?p gnus-tmp-id ?s)
4182     (?l gnus-tmp-length ?d)
4183     (?e gnus-tmp-dots ?s)))
4184
4185 (defvar gnus-mime-button-commands
4186   '((gnus-article-press-button "\r" "Toggle Display")
4187     (gnus-mime-view-part "v" "View Interactively...")
4188     (gnus-mime-view-part-as-type "t" "View As Type...")
4189     (gnus-mime-view-part-as-charset "C" "View As charset...")
4190     (gnus-mime-save-part "o" "Save...")
4191     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4192     (gnus-mime-replace-part "r" "Replace part")
4193     (gnus-mime-delete-part "d" "Delete part")
4194     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4195     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4196     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4197     (gnus-mime-view-part-externally "e" "View Externally")
4198     (gnus-mime-print-part "p" "Print")
4199     (gnus-mime-pipe-part "|" "Pipe To Command...")
4200     (gnus-mime-action-on-part "." "Take action on the part...")))
4201
4202 (defun gnus-article-mime-part-status ()
4203   (if gnus-article-mime-handle-alist-1
4204       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4205           " (1 part)"
4206         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4207     ""))
4208
4209 (defvar gnus-mime-button-map
4210   (let ((map (make-sparse-keymap)))
4211     (define-key map gnus-mouse-2 'gnus-article-push-button)
4212     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4213     (dolist (c gnus-mime-button-commands)
4214       (define-key map (cadr c) (car c)))
4215     map))
4216
4217 (easy-menu-define
4218   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4219   `("MIME Part"
4220     ,@(mapcar (lambda (c)
4221                 (vector (caddr c) (car c) :enable t))
4222               gnus-mime-button-commands)))
4223
4224 (defun gnus-mime-button-menu (event prefix)
4225  "Construct a context-sensitive menu of MIME commands."
4226  (interactive "e\nP")
4227  (save-window-excursion
4228    (let ((pos (event-start event)))
4229      (select-window (posn-window pos))
4230      (goto-char (posn-point pos))
4231      (gnus-article-check-buffer)
4232      (popup-menu gnus-mime-button-menu nil prefix))))
4233
4234 (defun gnus-mime-view-all-parts (&optional handles)
4235   "View all the MIME parts."
4236   (interactive)
4237   (with-current-buffer gnus-article-buffer
4238     (let ((handles (or handles gnus-article-mime-handles))
4239           (mail-parse-charset gnus-newsgroup-charset)
4240           (mail-parse-ignored-charsets
4241            (with-current-buffer gnus-summary-buffer
4242              gnus-newsgroup-ignored-charsets)))
4243       (when handles
4244         (mm-remove-parts handles)
4245         (goto-char (point-min))
4246         (or (search-forward "\n\n") (goto-char (point-max)))
4247         (let ((inhibit-read-only t))
4248           (delete-region (point) (point-max))
4249           (mm-display-parts handles))))))
4250
4251 (defun gnus-article-jump-to-part (n)
4252   "Jump to MIME part N."
4253   (interactive "P")
4254   (pop-to-buffer gnus-article-buffer)
4255   ;; FIXME: why is it necessary?
4256   (sit-for 0)
4257   (let ((parts (length gnus-article-mime-handle-alist)))
4258     (or n (setq n
4259                 (string-to-number
4260                  (read-string ;; Emacs 21 doesn't have `read-number'.
4261                   (format "Jump to part (2..%s): " parts)))))
4262     (unless (and (integerp n) (<= n parts) (>= n 1))
4263       (setq n
4264             (progn
4265               (gnus-message 7 "Invalid part `%s', using %s instead."
4266                             n parts)
4267               parts)))
4268     (gnus-message 9 "Jumping to part %s." n)
4269     (cond ((>= gnus-auto-select-part 1)
4270            (while (and (<= n parts)
4271                        (not (gnus-article-goto-part n)))
4272              (setq n (1+ n))))
4273           ((< gnus-auto-select-part 0)
4274            (while (and (>= n 1)
4275                        (not (gnus-article-goto-part n)))
4276              (setq n (1- n))))
4277           (t
4278            (gnus-article-goto-part n)))))
4279
4280 (eval-when-compile
4281   (defsubst gnus-article-edit-part (handles &optional current-id)
4282     "Edit an article in order to delete a mime part.
4283 This function is exclusively used by `gnus-mime-save-part-and-strip'
4284 and `gnus-mime-delete-part', and not provided at run-time normally."
4285     (gnus-article-edit-article
4286      `(lambda ()
4287         (buffer-disable-undo)
4288         (erase-buffer)
4289         (let ((mail-parse-charset (or gnus-article-charset
4290                                       ',gnus-newsgroup-charset))
4291               (mail-parse-ignored-charsets
4292                (or gnus-article-ignored-charsets
4293                    ',gnus-newsgroup-ignored-charsets))
4294               (mbl mml-buffer-list))
4295           (setq mml-buffer-list nil)
4296           (insert-buffer-substring gnus-original-article-buffer)
4297           (mime-to-mml ',handles)
4298           (setq gnus-article-mime-handles nil)
4299           (let ((mbl1 mml-buffer-list))
4300             (setq mml-buffer-list mbl)
4301             (set (make-local-variable 'mml-buffer-list) mbl1))
4302           (gnus-make-local-hook 'kill-buffer-hook)
4303           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4304      `(lambda (no-highlight)
4305         (let ((mail-parse-charset (or gnus-article-charset
4306                                       ',gnus-newsgroup-charset))
4307               (message-options message-options)
4308               (message-options-set-recipient)
4309               (mail-parse-ignored-charsets
4310                (or gnus-article-ignored-charsets
4311                    ',gnus-newsgroup-ignored-charsets)))
4312           (mml-to-mime)
4313           (mml-destroy-buffers)
4314           (remove-hook 'kill-buffer-hook
4315                        'mml-destroy-buffers t)
4316           (kill-local-variable 'mml-buffer-list))
4317         (gnus-summary-edit-article-done
4318          ,(or (mail-header-references gnus-current-headers) "")
4319          ,(gnus-group-read-only-p)
4320          ,gnus-summary-buffer no-highlight))
4321      t)
4322     (gnus-article-edit-done)
4323     (gnus-summary-expand-window)
4324     (gnus-summary-show-article)
4325     (when (and current-id (integerp gnus-auto-select-part))
4326       (gnus-article-jump-to-part
4327        (+ current-id gnus-auto-select-part)))))
4328
4329 (defun gnus-mime-replace-part (file)
4330   "Replace MIME part under point with an external body."
4331   ;; Useful if file has already been saved to disk
4332   (interactive
4333    (list
4334     (mm-with-multibyte
4335       (read-file-name "Replace MIME part with file: "
4336                       (or mm-default-directory default-directory)
4337                       nil nil))))
4338   (gnus-mime-save-part-and-strip file))
4339
4340 (defun gnus-mime-save-part-and-strip (&optional file)
4341   "Save the MIME part under point then replace it with an external body.
4342 If FILE is given, use it for the external part."
4343   (interactive)
4344   (gnus-article-check-buffer)
4345   (when (gnus-group-read-only-p)
4346     (error "The current group does not support deleting of parts"))
4347   (when (mm-complicated-handles gnus-article-mime-handles)
4348     (error "\
4349 The current article has a complicated MIME structure, giving up..."))
4350   (let* ((data (get-text-property (point) 'gnus-data))
4351          (id (get-text-property (point) 'gnus-part))
4352          param
4353          (handles gnus-article-mime-handles))
4354     (unless file
4355       (setq file
4356             (and data (mm-save-part data "Delete MIME part and save to: "))))
4357     (when file
4358       (with-current-buffer (mm-handle-buffer data)
4359         (erase-buffer)
4360         (insert "Content-Type: " (mm-handle-media-type data))
4361         (mml-insert-parameter-string (cdr (mm-handle-type data))
4362                                      '(charset))
4363         (insert "\n")
4364         (insert "Content-ID: " (message-make-message-id) "\n")
4365         (insert "Content-Transfer-Encoding: binary\n")
4366         (insert "\n"))
4367       (setcdr data
4368               (cdr (mm-make-handle nil
4369                                    `("message/external-body"
4370                                      (access-type . "LOCAL-FILE")
4371                                      (name . ,file)))))
4372       ;; (set-buffer gnus-summary-buffer)
4373       (gnus-article-edit-part handles id))))
4374
4375 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4376 ;; parts...>') but with stripping would be nice.
4377
4378 (defun gnus-mime-delete-part ()
4379   "Delete the MIME part under point.
4380 Replace it with some information about the removed part."
4381   (interactive)
4382   (gnus-article-check-buffer)
4383   (when (gnus-group-read-only-p)
4384     (error "The current group does not support deleting of parts"))
4385   (when (mm-complicated-handles gnus-article-mime-handles)
4386     (error "\
4387 The current article has a complicated MIME structure, giving up..."))
4388   (when (or gnus-expert-user
4389             (gnus-yes-or-no-p "\
4390 Deleting parts may malfunction or destroy the article; continue? "))
4391     (let* ((data (get-text-property (point) 'gnus-data))
4392            (id (get-text-property (point) 'gnus-part))
4393            (handles gnus-article-mime-handles)
4394            (none "(none)")
4395            (description
4396             (or
4397              (mail-decode-encoded-word-string (or (mm-handle-description data)
4398                                                   none))))
4399            (filename
4400             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4401                 none))
4402            (type (mm-handle-media-type data)))
4403       (unless data
4404         (error "No MIME part under point"))
4405       (with-current-buffer (mm-handle-buffer data)
4406         (let ((bsize (format "%s" (buffer-size))))
4407           (erase-buffer)
4408           (insert
4409            (concat
4410             ",----\n"
4411             "| The following attachment has been deleted:\n"
4412             "|\n"
4413             "| Type:           " type "\n"
4414             "| Filename:       " filename "\n"
4415             "| Size (encoded): " bsize " Byte\n"
4416             "| Description:    " description "\n"
4417             "`----\n"))
4418           (setcdr data
4419                   (cdr (mm-make-handle
4420                         nil `("text/plain") nil nil
4421                         (list "attachment")
4422                         (format "Deleted attachment (%s bytes)" bsize))))))
4423       ;; (set-buffer gnus-summary-buffer)
4424       (gnus-article-edit-part handles id))))
4425
4426 (defun gnus-mime-save-part ()
4427   "Save the MIME part under point."
4428   (interactive)
4429   (gnus-article-check-buffer)
4430   (let ((data (get-text-property (point) 'gnus-data)))
4431     (when data
4432       (mm-save-part data))))
4433
4434 (defun gnus-mime-pipe-part ()
4435   "Pipe the MIME part under point to a process."
4436   (interactive)
4437   (gnus-article-check-buffer)
4438   (let ((data (get-text-property (point) 'gnus-data)))
4439     (when data
4440       (mm-pipe-part data))))
4441
4442 (defun gnus-mime-view-part ()
4443   "Interactively choose a viewing method for the MIME part under point."
4444   (interactive)
4445   (gnus-article-check-buffer)
4446   (let ((data (get-text-property (point) 'gnus-data)))
4447     (when data
4448       (setq gnus-article-mime-handles
4449             (mm-merge-handles
4450              gnus-article-mime-handles (setq data (copy-sequence data))))
4451       (mm-interactively-view-part data))))
4452
4453 (defun gnus-mime-view-part-as-type-internal ()
4454   (gnus-article-check-buffer)
4455   (let* ((handle (get-text-property (point) 'gnus-data))
4456          (name (or
4457                 ;; Content-Type: foo/bar; name=...
4458                 (mail-content-type-get (mm-handle-type handle) 'name)
4459                 ;; Content-Disposition: attachment; filename=...
4460                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4461          (def-type (and name (mm-default-file-encoding name))))
4462     (and def-type (cons def-type 0))))
4463
4464 (defun gnus-mime-view-part-as-type (&optional mime-type)
4465   "Choose a MIME media type, and view the part as such."
4466   (interactive)
4467   (unless mime-type
4468     (setq mime-type
4469           (let ((default (gnus-mime-view-part-as-type-internal)))
4470             (completing-read
4471              (format "View as MIME type (default %s): "
4472                      (car default))
4473              (mapcar #'list (mailcap-mime-types))
4474              nil nil nil nil
4475              (car default)))))
4476   (gnus-article-check-buffer)
4477   (let ((handle (get-text-property (point) 'gnus-data)))
4478     (when handle
4479       (setq handle
4480             (mm-make-handle (mm-handle-buffer handle)
4481                             (cons mime-type (cdr (mm-handle-type handle)))
4482                             (mm-handle-encoding handle)
4483                             (mm-handle-undisplayer handle)
4484                             (mm-handle-disposition handle)
4485                             (mm-handle-description handle)
4486                             nil
4487                             (mm-handle-id handle)))
4488       (setq gnus-article-mime-handles
4489             (mm-merge-handles gnus-article-mime-handles handle))
4490       (gnus-mm-display-part handle))))
4491
4492 (defun gnus-mime-copy-part (&optional handle arg)
4493   "Put the MIME part under point into a new buffer.
4494 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4495 are decompressed."
4496   (interactive (list nil current-prefix-arg))
4497   (gnus-article-check-buffer)
4498   (unless handle
4499     (setq handle (get-text-property (point) 'gnus-data)))
4500   (when handle
4501     (let ((filename (or (mail-content-type-get (mm-handle-disposition handle)
4502                                                'name)
4503                         (mail-content-type-get (mm-handle-disposition handle)
4504                                                'filename)))
4505           contents dont-decode charset coding-system)
4506       (mm-with-unibyte-buffer
4507         (mm-insert-part handle)
4508         (setq contents (or (condition-case nil
4509                                (mm-decompress-buffer filename nil 'sig)
4510                              (error
4511                               (setq dont-decode t)
4512                               nil))
4513                            (buffer-string))))
4514       (setq filename (cond (filename (file-name-nondirectory filename))
4515                            (dont-decode "*raw data*")
4516                            (t "*decoded*")))
4517       (cond
4518        (dont-decode)
4519        ((not arg)
4520         (unless (setq charset (mail-content-type-get
4521                                (mm-handle-type handle) 'charset))
4522           (unless (setq coding-system (mm-with-unibyte-buffer
4523                                         (insert contents)
4524                                         (mm-find-buffer-file-coding-system)))
4525             (setq charset gnus-newsgroup-charset))))
4526        ((numberp arg)
4527         (setq charset (or (cdr (assq arg
4528                                      gnus-summary-show-article-charset-alist))
4529                           (mm-read-coding-system "Charset: ")))))
4530       (switch-to-buffer (generate-new-buffer filename))
4531       (if (or coding-system
4532               (and charset
4533                    (setq coding-system (mm-charset-to-coding-system charset))
4534                    (not (eq charset 'ascii))))
4535           (progn
4536             (mm-enable-multibyte)
4537             (insert (mm-decode-coding-string contents coding-system))
4538             (setq buffer-file-coding-system
4539                   (if (boundp 'last-coding-system-used)
4540                       (symbol-value 'last-coding-system-used)
4541                     coding-system)))
4542         (mm-disable-multibyte)
4543         (insert contents)
4544         (setq buffer-file-coding-system mm-binary-coding-system))
4545       ;; We do it this way to make `normal-mode' set the appropriate mode.
4546       (unwind-protect
4547           (progn
4548             (setq buffer-file-name (expand-file-name filename))
4549             (normal-mode))
4550         (setq buffer-file-name nil))
4551       (goto-char (point-min)))))
4552
4553 (defun gnus-mime-print-part (&optional handle filename)
4554   "Print the MIME part under point."
4555   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4556   (gnus-article-check-buffer)
4557   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4558          (contents (and handle (mm-get-part handle)))
4559          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4560          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4561     (when contents
4562         (if printer
4563             (unwind-protect
4564                 (progn
4565                   (mm-save-part-to-file handle file)
4566                   (call-process shell-file-name nil
4567                                 (generate-new-buffer " *mm*")
4568                                 nil
4569                                 shell-command-switch
4570                                 (mm-mailcap-command
4571                                  printer file (mm-handle-type handle))))
4572               (delete-file file))
4573           (with-temp-buffer
4574             (insert contents)
4575             (gnus-print-buffer))
4576           (ps-despool filename)))))
4577
4578 (defun gnus-mime-inline-part (&optional handle arg)
4579   "Insert the MIME part under point into the current buffer.
4580 Compressed files like .gz and .bz2 are decompressed."
4581   (interactive (list nil current-prefix-arg))
4582   (gnus-article-check-buffer)
4583   (unless handle
4584     (setq handle (get-text-property (point) 'gnus-data)))
4585   (when handle
4586     (let ((b (point))
4587           (inhibit-read-only t)
4588           contents charset coding-system)
4589       (if (and (not arg) (mm-handle-undisplayer handle))
4590           (mm-remove-part handle)
4591         (mm-with-unibyte-buffer
4592           (mm-insert-part handle)
4593           (setq contents
4594                 (or (mm-decompress-buffer
4595                      (or (mail-content-type-get (mm-handle-disposition handle)
4596                                                 'name)
4597                          (mail-content-type-get (mm-handle-disposition handle)
4598                                                 'filename))
4599                      nil t)
4600                     (buffer-string))))
4601         (cond
4602          ((not arg)
4603           (unless (setq charset (mail-content-type-get
4604                                  (mm-handle-type handle) 'charset))
4605             (unless (setq coding-system
4606                           (mm-with-unibyte-buffer
4607                             (insert contents)
4608                             (mm-find-buffer-file-coding-system)))
4609               (setq charset gnus-newsgroup-charset))))
4610          ((numberp arg)
4611           (if (mm-handle-undisplayer handle)
4612               (mm-remove-part handle))
4613           (setq charset
4614                 (or (cdr (assq arg
4615                                gnus-summary-show-article-charset-alist))
4616                     (mm-read-coding-system "Charset: "))))
4617          (t
4618           (if (mm-handle-undisplayer handle)
4619               (mm-remove-part handle))))
4620         (forward-line 2)
4621         (mm-insert-inline
4622          handle
4623          (if (or coding-system
4624                  (and charset
4625                       (setq coding-system
4626                             (mm-charset-to-coding-system charset))
4627                       (not (eq coding-system 'ascii))))
4628              (mm-decode-coding-string contents coding-system)
4629            (mm-string-to-multibyte contents)))
4630         (goto-char b)))))
4631
4632 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4633   "Insert the MIME part under point into the current buffer using the
4634 specified charset."
4635   (interactive (list nil current-prefix-arg))
4636   (gnus-article-check-buffer)
4637   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4638          contents charset
4639          (b (point))
4640          (inhibit-read-only t))
4641     (when handle
4642       (if (mm-handle-undisplayer handle)
4643           (mm-remove-part handle))
4644       (let ((gnus-newsgroup-charset
4645              (or (cdr (assq arg
4646                             gnus-summary-show-article-charset-alist))
4647                  (mm-read-coding-system "Charset: ")))
4648           (gnus-newsgroup-ignored-charsets 'gnus-all))
4649         (gnus-article-press-button)))))
4650
4651 (defun gnus-mime-view-part-externally (&optional handle)
4652   "View the MIME part under point with an external viewer."
4653   (interactive)
4654   (gnus-article-check-buffer)
4655   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4656          (mm-user-display-methods nil)
4657          (mm-inlined-types nil)
4658          (mail-parse-charset gnus-newsgroup-charset)
4659          (mail-parse-ignored-charsets
4660           (with-current-buffer gnus-summary-buffer
4661             gnus-newsgroup-ignored-charsets)))
4662     (when handle
4663       (if (mm-handle-undisplayer handle)
4664           (mm-remove-part handle)
4665         (mm-display-part handle)))))
4666
4667 (defun gnus-mime-view-part-internally (&optional handle)
4668   "View the MIME part under point with an internal viewer.
4669 If no internal viewer is available, use an external viewer."
4670   (interactive)
4671   (gnus-article-check-buffer)
4672   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4673          (mm-inlined-types '(".*"))
4674          (mm-inline-large-images t)
4675          (mail-parse-charset gnus-newsgroup-charset)
4676          (mail-parse-ignored-charsets
4677           (with-current-buffer gnus-summary-buffer
4678             gnus-newsgroup-ignored-charsets))
4679          (inhibit-read-only t))
4680     (when handle
4681       (if (mm-handle-undisplayer handle)
4682           (mm-remove-part handle)
4683         (mm-display-part handle)))))
4684
4685 (defun gnus-mime-action-on-part (&optional action)
4686   "Do something with the MIME attachment at \(point\)."
4687   (interactive
4688    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4689   (gnus-article-check-buffer)
4690   (let ((action-pair (assoc action gnus-mime-action-alist)))
4691     (if action-pair
4692         (funcall (cdr action-pair)))))
4693
4694 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
4695   "Call FUNCTION on MIME part N.
4696 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
4697 If INTERACTIVE, call FUNCTION interactivly."
4698   (let (window frame)
4699     ;; Check whether the article is displayed.
4700     (unless (and (gnus-buffer-live-p gnus-article-buffer)
4701                  (setq window (get-buffer-window gnus-article-buffer t))
4702                  (frame-visible-p (setq frame (window-frame window))))
4703       (error "No article is displayed"))
4704     (with-current-buffer gnus-article-buffer
4705       ;; Check whether the article displays the right contents.
4706       (unless (with-current-buffer gnus-summary-buffer
4707                 (eq gnus-current-article (gnus-summary-article-number)))
4708         (error "You should select the right article first"))
4709       ;; Check whether the specified part exists.
4710       (when (> n (length gnus-article-mime-handle-alist))
4711         (error "No such part")))
4712     (unless
4713         (progn
4714           ;; To select the window is needed so that the cursor
4715           ;; might be visible on the MIME button.
4716           (select-window (prog1
4717                              window
4718                            (setq window (selected-window))
4719                            ;; Article may be displayed in the other frame.
4720                            (gnus-select-frame-set-input-focus
4721                             (prog1
4722                                 frame
4723                               (setq frame (selected-frame))))))
4724           (when (gnus-article-goto-part n)
4725             ;; We point the cursor and the arrow at the MIME button
4726             ;; when the `function' prompt the user for something.
4727             (let ((cursor-in-non-selected-windows t)
4728                   (overlay-arrow-string "=>")
4729                   (overlay-arrow-position (point-marker)))
4730               (unwind-protect
4731                   (cond
4732                    ((and no-handle interactive)
4733                     (call-interactively function))
4734                    (no-handle
4735                     (funcall function))
4736                    (interactive
4737                     (call-interactively
4738                      function
4739                      (cdr (assq n gnus-article-mime-handle-alist))))
4740                    (t
4741                     (funcall function
4742                              (cdr (assq n gnus-article-mime-handle-alist)))))
4743                 (set-marker overlay-arrow-position nil)
4744                 (unless gnus-auto-select-part
4745                   (gnus-select-frame-set-input-focus frame)
4746                   (select-window window))))
4747             t))
4748       (if gnus-inhibit-mime-unbuttonizing
4749           ;; This is the default though the program shouldn't reach here.
4750           (error "No such part")
4751         ;; The part which doesn't have the MIME button is selected.
4752         ;; So, we display all the buttons and redo it.
4753         (let ((gnus-inhibit-mime-unbuttonizing t))
4754           (gnus-summary-show-article)
4755           (gnus-article-part-wrapper n function no-handle))))))
4756
4757 (defun gnus-article-pipe-part (n)
4758   "Pipe MIME part N, which is the numerical prefix."
4759   (interactive "p")
4760   (gnus-article-part-wrapper n 'mm-pipe-part))
4761
4762 (defun gnus-article-save-part (n)
4763   "Save MIME part N, which is the numerical prefix."
4764   (interactive "p")
4765   (gnus-article-part-wrapper n 'mm-save-part))
4766
4767 (defun gnus-article-interactively-view-part (n)
4768   "View MIME part N interactively, which is the numerical prefix."
4769   (interactive "p")
4770   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4771
4772 (defun gnus-article-copy-part (n)
4773   "Copy MIME part N, which is the numerical prefix."
4774   (interactive "p")
4775   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4776
4777 (defun gnus-article-view-part-as-charset (n)
4778   "View MIME part N using a specified charset.
4779 N is the numerical prefix."
4780   (interactive "p")
4781   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4782
4783 (defun gnus-article-view-part-externally (n)
4784   "View MIME part N externally, which is the numerical prefix."
4785   (interactive "p")
4786   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4787
4788 (defun gnus-article-inline-part (n)
4789   "Inline MIME part N, which is the numerical prefix."
4790   (interactive "p")
4791   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4792
4793 (defun gnus-article-save-part-and-strip (n)
4794   "Save MIME part N and replace it with an external body.
4795 N is the numerical prefix."
4796   (interactive "p")
4797   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
4798
4799 (defun gnus-article-replace-part (n)
4800   "Replace MIME part N with an external body.
4801 N is the numerical prefix."
4802   (interactive "p")
4803   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
4804
4805 (defun gnus-article-delete-part (n)
4806   "Delete MIME part N and add some information about the removed part.
4807 N is the numerical prefix."
4808   (interactive "p")
4809   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
4810
4811 (defun gnus-article-mime-match-handle-first (condition)
4812   (if condition
4813       (let (n)
4814         (dolist (ihandle gnus-article-mime-handle-alist)
4815           (if (and (cond
4816                     ((functionp condition)
4817                      (funcall condition (cdr ihandle)))
4818                     ((eq condition 'undisplayed)
4819                      (not (or (mm-handle-undisplayer (cdr ihandle))
4820                               (equal (mm-handle-media-type (cdr ihandle))
4821                                      "multipart/alternative"))))
4822                     ((eq condition 'undisplayed-alternative)
4823                      (not (mm-handle-undisplayer (cdr ihandle))))
4824                     (t t))
4825                    (gnus-article-goto-part (car ihandle))
4826                    (or (not n) (< (car ihandle) n)))
4827               (setq n (car ihandle))))
4828         (or n 1))
4829     1))
4830
4831 (defun gnus-article-view-part (&optional n)
4832   "View MIME part N, which is the numerical prefix."
4833   (interactive "P")
4834   (with-current-buffer gnus-article-buffer
4835     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4836                              gnus-article-mime-match-handle-function)))
4837     (when (> n (length gnus-article-mime-handle-alist))
4838       (error "No such part"))
4839     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4840       (when (gnus-article-goto-part n)
4841         (if (equal (car handle) "multipart/alternative")
4842             (gnus-article-press-button)
4843           (when (eq (gnus-mm-display-part handle) 'internal)
4844             (gnus-set-window-start)))))))
4845
4846 (defsubst gnus-article-mime-total-parts ()
4847   (if (bufferp (car gnus-article-mime-handles))
4848       1 ;; single part
4849     (1- (length gnus-article-mime-handles))))
4850
4851 (defun gnus-mm-display-part (handle)
4852   "Display HANDLE and fix MIME button."
4853   (let ((id (get-text-property (point) 'gnus-part))
4854         (point (point))
4855         (inhibit-read-only t))
4856     (forward-line 1)
4857     (prog1
4858         (let ((window (selected-window))
4859               (mail-parse-charset gnus-newsgroup-charset)
4860               (mail-parse-ignored-charsets
4861                (if (gnus-buffer-live-p gnus-summary-buffer)
4862                    (with-current-buffer gnus-summary-buffer
4863                      gnus-newsgroup-ignored-charsets)
4864                  nil)))
4865           (save-excursion
4866             (unwind-protect
4867                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4868                       (beg (point)))
4869                   (when win
4870                     (select-window win))
4871                   (goto-char point)
4872                   (forward-line)
4873                   (if (mm-handle-displayed-p handle)
4874                       ;; This will remove the part.
4875                       (mm-display-part handle)
4876                     (save-restriction
4877                       (narrow-to-region (point)
4878                                         (if (eobp) (point) (1+ (point))))
4879                       (mm-display-part handle)
4880                       ;; We narrow to the part itself and
4881                       ;; then call the treatment functions.
4882                       (goto-char (point-min))
4883                       (forward-line 1)
4884                       (narrow-to-region (point) (point-max))
4885                       (gnus-treat-article
4886                        nil id
4887                        (gnus-article-mime-total-parts)
4888                        (mm-handle-media-type handle)))))
4889               (if (window-live-p window)
4890                   (select-window window)))))
4891       (goto-char point)
4892       (gnus-delete-line)
4893       (gnus-insert-mime-button
4894        handle id (list (mm-handle-displayed-p handle)))
4895       (goto-char point))))
4896
4897 (defun gnus-article-goto-part (n)
4898   "Go to MIME part N."
4899   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4900
4901 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4902   (let ((gnus-tmp-name
4903          (or (mail-content-type-get (mm-handle-type handle) 'name)
4904              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4905              (mail-content-type-get (mm-handle-type handle) 'url)
4906              ""))
4907         (gnus-tmp-type (mm-handle-media-type handle))
4908         (gnus-tmp-description
4909          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4910                                               "")))
4911         (gnus-tmp-dots
4912          (if (if displayed (car displayed)
4913                (mm-handle-displayed-p handle))
4914              "" "..."))
4915         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4916                            (buffer-size)))
4917         gnus-tmp-type-long b e)
4918     (when (string-match ".*/" gnus-tmp-name)
4919       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4920     (setq gnus-tmp-type-long (concat gnus-tmp-type
4921                                      (and (not (equal gnus-tmp-name ""))
4922                                           (concat "; " gnus-tmp-name))))
4923     (unless (equal gnus-tmp-description "")
4924       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4925     (unless (bolp)
4926       (insert "\n"))
4927     (setq b (point))
4928     (gnus-eval-format
4929      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4930      `(keymap ,gnus-mime-button-map
4931               gnus-callback gnus-mm-display-part
4932               gnus-part ,gnus-tmp-id
4933               article-type annotation
4934               gnus-data ,handle))
4935     (setq e (if (bolp)
4936                 ;; Exclude a newline.
4937                 (1- (point))
4938               (point)))
4939     (widget-convert-button
4940      'link b e
4941      :mime-handle handle
4942      :action 'gnus-widget-press-button
4943      :button-keymap gnus-mime-button-map
4944      :help-echo
4945      (lambda (widget/window &optional overlay pos)
4946        ;; Needed to properly clear the message due to a bug in
4947        ;; wid-edit (XEmacs only).
4948        (if (boundp 'help-echo-owns-message)
4949            (setq help-echo-owns-message t))
4950        (format
4951         "%S: %s the MIME part; %S: more options"
4952         (aref gnus-mouse-2 0)
4953         ;; XEmacs will get a single widget arg; Emacs 21 will get
4954         ;; window, overlay, position.
4955         (if (mm-handle-displayed-p
4956              (if overlay
4957                  (with-current-buffer (gnus-overlay-buffer overlay)
4958                    (widget-get (widget-at (gnus-overlay-start overlay))
4959                                :mime-handle))
4960                (widget-get widget/window :mime-handle)))
4961             "hide" "show")
4962         (aref gnus-down-mouse-3 0))))))
4963
4964 (defun gnus-widget-press-button (elems el)
4965   (goto-char (widget-get elems :from))
4966   (gnus-article-press-button))
4967
4968 (defvar gnus-displaying-mime nil)
4969
4970 (defun gnus-display-mime (&optional ihandles)
4971   "Display the MIME parts."
4972   (save-excursion
4973     (save-selected-window
4974       (let ((window (get-buffer-window gnus-article-buffer))
4975             (point (point)))
4976         (when window
4977           (select-window window)
4978           ;; We have to do this since selecting the window
4979           ;; may change the point.  So we set the window point.
4980           (set-window-point window point)))
4981       (let ((handles ihandles)
4982             (inhibit-read-only t)
4983             handle)
4984         (cond (handles)
4985               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
4986                (when gnus-article-emulate-mime
4987                  (mm-uu-dissect-text-parts handles)))
4988               (gnus-article-emulate-mime
4989                (setq handles (mm-uu-dissect))))
4990         (when (and (not ihandles)
4991                    (not gnus-displaying-mime))
4992           ;; Top-level call; we clean up.
4993           (when gnus-article-mime-handles
4994             (mm-destroy-parts gnus-article-mime-handles)
4995             (setq gnus-article-mime-handle-alist nil));; A trick.
4996           (setq gnus-article-mime-handles handles)
4997           ;; We allow users to glean info from the handles.
4998           (when gnus-article-mime-part-function
4999             (gnus-mime-part-function handles)))
5000         (if (and handles
5001                  (or (not (stringp (car handles)))
5002                      (cdr handles)))
5003             (progn
5004               (when (and (not ihandles)
5005                          (not gnus-displaying-mime))
5006                 ;; Clean up for mime parts.
5007                 (article-goto-body)
5008                 (delete-region (point) (point-max)))
5009               (let ((gnus-displaying-mime t))
5010                 (gnus-mime-display-part handles)))
5011           (save-restriction
5012             (article-goto-body)
5013             (narrow-to-region (point) (point-max))
5014             (gnus-treat-article nil 1 1)
5015             (widen)))
5016         (unless ihandles
5017           ;; Highlight the headers.
5018           (save-excursion
5019             (save-restriction
5020               (article-goto-body)
5021               (narrow-to-region (point-min) (point))
5022               (gnus-article-save-original-date
5023                (gnus-treat-article 'head)))))))))
5024
5025 (defcustom gnus-mime-display-multipart-as-mixed nil
5026   "Display \"multipart\" parts as  \"multipart/mixed\".
5027
5028 If t, it overrides nil values of
5029 `gnus-mime-display-multipart-alternative-as-mixed' and
5030 `gnus-mime-display-multipart-related-as-mixed'."
5031   :group 'gnus-article-mime
5032   :type 'boolean)
5033
5034 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5035   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5036   :version "22.1"
5037   :group 'gnus-article-mime
5038   :type 'boolean)
5039
5040 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5041   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5042
5043 If displaying \"text/html\" is discouraged \(see
5044 `mm-discouraged-alternatives'\) images or other material inside a
5045 \"multipart/related\" part might be overlooked when this variable is nil."
5046   :version "22.1"
5047   :group 'gnus-article-mime
5048   :type 'boolean)
5049
5050 (defun gnus-mime-display-part (handle)
5051   (cond
5052    ;; Maybe a broken MIME message.
5053    ((null handle))
5054    ;; Single part.
5055    ((not (stringp (car handle)))
5056     (gnus-mime-display-single handle))
5057    ;; User-defined multipart
5058    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5059     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5060              handle))
5061    ;; multipart/alternative
5062    ((and (equal (car handle) "multipart/alternative")
5063          (not (or gnus-mime-display-multipart-as-mixed
5064                   gnus-mime-display-multipart-alternative-as-mixed)))
5065     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5066       (push (cons id handle) gnus-article-mime-handle-alist)
5067       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5068    ;; multipart/related
5069    ((and (equal (car handle) "multipart/related")
5070          (not (or gnus-mime-display-multipart-as-mixed
5071                   gnus-mime-display-multipart-related-as-mixed)))
5072     ;;;!!!We should find the start part, but we just default
5073     ;;;!!!to the first part.
5074     ;;(gnus-mime-display-part (cadr handle))
5075     ;;;!!! Most multipart/related is an HTML message plus images.
5076     ;;;!!! Unfortunately we are unable to let W3 display those
5077     ;;;!!! included images, so we just display it as a mixed multipart.
5078     ;;(gnus-mime-display-mixed (cdr handle))
5079     ;;;!!! No, w3 can display everything just fine.
5080     (gnus-mime-display-part (cadr handle)))
5081    ((equal (car handle) "multipart/signed")
5082     (gnus-add-wash-type 'signed)
5083     (gnus-mime-display-security handle))
5084    ((equal (car handle) "multipart/encrypted")
5085     (gnus-add-wash-type 'encrypted)
5086     (gnus-mime-display-security handle))
5087    ;; Other multiparts are handled like multipart/mixed.
5088    (t
5089     (gnus-mime-display-mixed (cdr handle)))))
5090
5091 (defun gnus-mime-part-function (handles)
5092   (if (stringp (car handles))
5093       (mapcar 'gnus-mime-part-function (cdr handles))
5094     (funcall gnus-article-mime-part-function handles)))
5095
5096 (defun gnus-mime-display-mixed (handles)
5097   (mapcar 'gnus-mime-display-part handles))
5098
5099 (defun gnus-mime-display-single (handle)
5100   (let ((type (mm-handle-media-type handle))
5101         (ignored gnus-ignored-mime-types)
5102         (not-attachment t)
5103         (move nil)
5104         display text)
5105     (catch 'ignored
5106       (progn
5107         (while ignored
5108           (when (string-match (pop ignored) type)
5109             (throw 'ignored nil)))
5110         (if (and (setq not-attachment
5111                        (and (not (mm-inline-override-p handle))
5112                             (or (not (mm-handle-disposition handle))
5113                                 (equal (car (mm-handle-disposition handle))
5114                                        "inline")
5115                                 (mm-attachment-override-p handle))))
5116                  (mm-automatic-display-p handle)
5117                  (or (and
5118                       (mm-inlinable-p handle)
5119                       (mm-inlined-p handle))
5120                      (mm-automatic-external-display-p type)))
5121             (setq display t)
5122           (when (equal (mm-handle-media-supertype handle) "text")
5123             (setq text t)))
5124         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5125               beg)
5126           (push (cons id handle) gnus-article-mime-handle-alist)
5127           (when (or (not display)
5128                     (not (gnus-unbuttonized-mime-type-p type)))
5129             (gnus-insert-mime-button
5130              handle id (list (or display (and not-attachment text))))
5131             (gnus-article-insert-newline)
5132             ;; Remember modify the number of forward lines.
5133             (setq move t))
5134           (setq beg (point))
5135           (cond
5136            (display
5137             (when move
5138               (forward-line -1)
5139               (setq beg (point)))
5140             (let ((mail-parse-charset gnus-newsgroup-charset)
5141                   (mail-parse-ignored-charsets
5142                    (save-excursion (condition-case ()
5143                                        (set-buffer gnus-summary-buffer)
5144                                      (error))
5145                                    gnus-newsgroup-ignored-charsets)))
5146               (mm-display-part handle t))
5147             (goto-char (point-max)))
5148            ((and text not-attachment)
5149             (when move
5150               (forward-line -1)
5151               (setq beg (point)))
5152             (gnus-article-insert-newline)
5153             (mm-insert-inline
5154              handle
5155              (let ((charset (mail-content-type-get (mm-handle-type handle)
5156                                                    'charset)))
5157                (cond ((not charset)
5158                       (mm-string-as-multibyte (mm-get-part handle)))
5159                      ((eq charset 'gnus-decoded)
5160                       (with-current-buffer (mm-handle-buffer handle)
5161                         (buffer-string)))
5162                      (t
5163                       (mm-decode-string (mm-get-part handle) charset)))))
5164             (goto-char (point-max))))
5165           ;; Do highlighting.
5166           (save-excursion
5167             (save-restriction
5168               (narrow-to-region beg (point))
5169               (gnus-treat-article
5170                nil id
5171                (gnus-article-mime-total-parts)
5172                (mm-handle-media-type handle)))))))))
5173
5174 (defun gnus-unbuttonized-mime-type-p (type)
5175   "Say whether TYPE is to be unbuttonized."
5176   (unless gnus-inhibit-mime-unbuttonizing
5177     (when (catch 'found
5178             (let ((types gnus-unbuttonized-mime-types))
5179               (while types
5180                 (when (string-match (pop types) type)
5181                   (throw 'found t)))))
5182       (not (catch 'found
5183              (let ((types gnus-buttonized-mime-types))
5184                (while types
5185                  (when (string-match (pop types) type)
5186                    (throw 'found t)))))))))
5187
5188 (defun gnus-article-insert-newline ()
5189   "Insert a newline, but mark it as undeletable."
5190   (gnus-put-text-property
5191    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5192
5193 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5194   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5195          (ihandles handles)
5196          (point (point))
5197          handle (inhibit-read-only t) from props begend not-pref)
5198     (save-window-excursion
5199       (save-restriction
5200         (when ibegend
5201           (narrow-to-region (car ibegend)
5202                             (or (cdr ibegend)
5203                                 (progn
5204                                   (goto-char (car ibegend))
5205                                   (forward-line 2)
5206                                   (point))))
5207           (delete-region (point-min) (point-max))
5208           (mm-remove-parts handles))
5209         (setq begend (list (point-marker)))
5210         ;; Do the toggle.
5211         (unless (setq not-pref (cadr (member preferred ihandles)))
5212           (setq not-pref (car ihandles)))
5213         (when (or ibegend
5214                   (not preferred)
5215                   (not (gnus-unbuttonized-mime-type-p
5216                         "multipart/alternative")))
5217           (gnus-add-text-properties
5218            (setq from (point))
5219            (progn
5220              (insert (format "%d.  " id))
5221              (point))
5222            `(gnus-callback
5223              (lambda (handles)
5224                (unless ,(not ibegend)
5225                  (setq gnus-article-mime-handle-alist
5226                        ',gnus-article-mime-handle-alist))
5227                (gnus-mime-display-alternative
5228                 ',ihandles ',not-pref ',begend ,id))
5229              keymap ,gnus-mime-button-map
5230              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5231              face ,gnus-article-button-face
5232              gnus-part ,id
5233              article-type multipart))
5234           (widget-convert-button 'link from (point)
5235                                  :action 'gnus-widget-press-button
5236                                  :button-keymap gnus-widget-button-keymap)
5237           ;; Do the handles
5238           (while (setq handle (pop handles))
5239             (gnus-add-text-properties
5240              (setq from (point))
5241              (progn
5242                (insert (format "(%c) %-18s"
5243                                (if (equal handle preferred) ?* ? )
5244                                (mm-handle-media-type handle)))
5245                (point))
5246              `(gnus-callback
5247                (lambda (handles)
5248                  (unless ,(not ibegend)
5249                    (setq gnus-article-mime-handle-alist
5250                          ',gnus-article-mime-handle-alist))
5251                  (gnus-mime-display-alternative
5252                   ',ihandles ',handle ',begend ,id))
5253                keymap ,gnus-mime-button-map
5254                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5255                face ,gnus-article-button-face
5256                gnus-part ,id
5257                gnus-data ,handle))
5258             (widget-convert-button 'link from (point)
5259                                    :action 'gnus-widget-press-button
5260                                    :button-keymap gnus-widget-button-keymap)
5261             (insert "  "))
5262           (insert "\n\n"))
5263         (when preferred
5264           (if (stringp (car preferred))
5265               (gnus-display-mime preferred)
5266             (let ((mail-parse-charset gnus-newsgroup-charset)
5267                   (mail-parse-ignored-charsets
5268                    (with-current-buffer gnus-summary-buffer
5269                      gnus-newsgroup-ignored-charsets)))
5270               (mm-display-part preferred)
5271               ;; Do highlighting.
5272               (save-excursion
5273                 (save-restriction
5274                   (narrow-to-region (car begend) (point-max))
5275                   (gnus-treat-article
5276                    nil (length gnus-article-mime-handle-alist)
5277                    (gnus-article-mime-total-parts)
5278                    (mm-handle-media-type handle))))))
5279           (goto-char (point-max))
5280           (setcdr begend (point-marker)))))
5281     (when ibegend
5282       (goto-char point))))
5283
5284 (defconst gnus-article-wash-status-strings
5285   (let ((alist '((cite "c" "Possible hidden citation text"
5286                        " " "All citation text visible")
5287                  (headers "h" "Hidden headers"
5288                           " " "All headers visible.")
5289                  (pgp "p" "Encrypted or signed message status hidden"
5290                       " " "No hidden encryption nor digital signature status")
5291                  (signature "s" "Signature has been hidden"
5292                             " " "Signature is visible")
5293                  (overstrike "o" "Overstrike (^H) characters applied"
5294                              " " "No overstrike characters applied")
5295                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5296                            " " "No /*_emphasis_*/ characters applied")))
5297         result)
5298     (dolist (entry alist result)
5299       (let ((key (nth 0 entry))
5300             (on (copy-sequence (nth 1 entry)))
5301             (on-help (nth 2 entry))
5302             (off (copy-sequence (nth 3 entry)))
5303             (off-help (nth 4 entry)))
5304         (put-text-property 0 1 'help-echo on-help on)
5305         (put-text-property 0 1 'help-echo off-help off)
5306         (push (list key on off) result))))
5307   "Alist of strings describing wash status in the mode line.
5308 Each entry has the form (KEY ON OF), where the KEY is a symbol
5309 representing the particular washing function, ON is the string to use
5310 in the article mode line when the washing function is active, and OFF
5311 is the string to use when it is inactive.")
5312
5313 (defun gnus-article-wash-status-entry (key value)
5314   (let ((entry (assoc key gnus-article-wash-status-strings)))
5315     (if value (nth 1 entry) (nth 2 entry))))
5316
5317 (defun gnus-article-wash-status ()
5318   "Return a string which display status of article washing."
5319   (with-current-buffer gnus-article-buffer
5320     (let ((cite (memq 'cite gnus-article-wash-types))
5321           (headers (memq 'headers gnus-article-wash-types))
5322           (boring (memq 'boring-headers gnus-article-wash-types))
5323           (pgp (memq 'pgp gnus-article-wash-types))
5324           (pem (memq 'pem gnus-article-wash-types))
5325           (signed (memq 'signed gnus-article-wash-types))
5326           (encrypted (memq 'encrypted gnus-article-wash-types))
5327           (signature (memq 'signature gnus-article-wash-types))
5328           (overstrike (memq 'overstrike gnus-article-wash-types))
5329           (emphasis (memq 'emphasis gnus-article-wash-types)))
5330       (concat
5331        (gnus-article-wash-status-entry 'cite cite)
5332        (gnus-article-wash-status-entry 'headers (or headers boring))
5333        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5334        (gnus-article-wash-status-entry 'signature signature)
5335        (gnus-article-wash-status-entry 'overstrike overstrike)
5336        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5337
5338 (defun gnus-add-wash-type (type)
5339   "Add a washing of TYPE to the current status."
5340   (add-to-list 'gnus-article-wash-types type))
5341
5342 (defun gnus-delete-wash-type (type)
5343   "Add a washing of TYPE to the current status."
5344   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5345
5346 (defun gnus-add-image (category image)
5347   "Add IMAGE of CATEGORY to the list of displayed images."
5348   (let ((entry (assq category gnus-article-image-alist)))
5349     (unless entry
5350       (setq entry (list category))
5351       (push entry gnus-article-image-alist))
5352     (nconc entry (list image))))
5353
5354 (defun gnus-delete-images (category)
5355   "Delete all images in CATEGORY."
5356   (let ((entry (assq category gnus-article-image-alist)))
5357     (dolist (image (cdr entry))
5358       (gnus-remove-image image category))
5359     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5360     (gnus-delete-wash-type category)))
5361
5362 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5363
5364 (defun gnus-article-maybe-hide-headers ()
5365   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5366 Provided for backwards compatibility."
5367   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5368                  (not (with-current-buffer gnus-summary-buffer
5369                         gnus-have-all-headers)))
5370              (not gnus-inhibit-hiding))
5371     (gnus-article-hide-headers)))
5372
5373 ;;; Article savers.
5374
5375 (defun gnus-output-to-file (file-name)
5376   "Append the current article to a file named FILE-NAME."
5377   (let ((artbuf (current-buffer)))
5378     (with-temp-buffer
5379       (insert-buffer-substring artbuf)
5380       ;; Append newline at end of the buffer as separator, and then
5381       ;; save it to file.
5382       (goto-char (point-max))
5383       (insert "\n")
5384       (let ((file-name-coding-system nnmail-pathname-coding-system))
5385         (mm-append-to-file (point-min) (point-max) file-name))
5386       t)))
5387
5388 (defun gnus-narrow-to-page (&optional arg)
5389   "Narrow the article buffer to a page.
5390 If given a numerical ARG, move forward ARG pages."
5391   (interactive "P")
5392   (setq arg (if arg (prefix-numeric-value arg) 0))
5393   (save-excursion
5394     (set-buffer gnus-article-buffer)
5395     (goto-char (point-min))
5396     (widen)
5397     ;; Remove any old next/prev buttons.
5398     (when (gnus-visual-p 'page-marker)
5399       (let ((inhibit-read-only t))
5400         (gnus-remove-text-with-property 'gnus-prev)
5401         (gnus-remove-text-with-property 'gnus-next)))
5402     (if
5403         (cond ((< arg 0)
5404                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5405               ((> arg 0)
5406                (re-search-forward page-delimiter nil 'move arg)))
5407         (goto-char (match-end 0))
5408       (save-excursion
5409         (goto-char (point-min))
5410         (setq gnus-page-broken
5411               (and (re-search-forward page-delimiter nil t) t))))
5412     (when gnus-page-broken
5413       (narrow-to-region
5414        (point)
5415        (if (re-search-forward page-delimiter nil 'move)
5416            (match-beginning 0)
5417          (point)))
5418       (when (and (gnus-visual-p 'page-marker)
5419                  (> (point-min) (save-restriction (widen) (point-min))))
5420         (save-excursion
5421           (goto-char (point-min))
5422           (gnus-insert-prev-page-button)))
5423       (when (and (gnus-visual-p 'page-marker)
5424                  (< (point-max) (save-restriction (widen) (point-max))))
5425         (save-excursion
5426           (goto-char (point-max))
5427           (gnus-insert-next-page-button))))))
5428
5429 ;; Article mode commands
5430
5431 (defun gnus-article-goto-next-page ()
5432   "Show the next page of the article."
5433   (interactive)
5434   (when (gnus-article-next-page)
5435     (goto-char (point-min))
5436     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5437
5438
5439 (defun gnus-article-goto-prev-page ()
5440   "Show the previous page of the article."
5441   (interactive)
5442   (if (bobp)
5443       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5444     (gnus-article-prev-page nil)))
5445
5446 ;; This is cleaner but currently breaks `gnus-pick-mode':
5447 ;;
5448 ;; (defun gnus-article-goto-next-page ()
5449 ;;   "Show the next page of the article."
5450 ;;   (interactive)
5451 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5452 ;;     (gnus-summary-next-page)))
5453 ;;
5454 ;; (defun gnus-article-goto-prev-page ()
5455 ;;   "Show the next page of the article."
5456 ;;   (interactive)
5457 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5458 ;;     (gnus-summary-prev-page)))
5459
5460 (defun gnus-article-next-page (&optional lines)
5461   "Show the next page of the current article.
5462 If end of article, return non-nil.  Otherwise return nil.
5463 Argument LINES specifies lines to be scrolled up."
5464   (interactive "p")
5465   (move-to-window-line -1)
5466   (if (save-excursion
5467         (end-of-line)
5468         (and (pos-visible-in-window-p)  ;Not continuation line.
5469              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5470       ;; Nothing in this page.
5471       (if (or (not gnus-page-broken)
5472               (save-excursion
5473                 (save-restriction
5474                   (widen)
5475                   (forward-line)
5476                   (eobp)))) ;Real end-of-buffer?
5477           (progn
5478             (when gnus-article-over-scroll
5479               (gnus-article-next-page-1 lines))
5480             t)                  ;Nothing more.
5481         (gnus-narrow-to-page 1)         ;Go to next page.
5482         nil)
5483     ;; More in this page.
5484     (gnus-article-next-page-1 lines)
5485     nil))
5486
5487 (defmacro gnus-article-beginning-of-window ()
5488   "Move point to the beginning of the window.
5489 In Emacs, the point is placed at the line number which `scroll-margin'
5490 specifies."
5491   (if (featurep 'xemacs)
5492       '(move-to-window-line 0)
5493     '(move-to-window-line
5494       (min (max 0 scroll-margin)
5495            (max 1 (- (window-height)
5496                      (if mode-line-format 1 0)
5497                      (if header-line-format 1 0)))))))
5498
5499 (defun gnus-article-next-page-1 (lines)
5500   (when (and (not (featurep 'xemacs))
5501              (numberp lines)
5502              (> lines 0)
5503              (numberp (symbol-value 'scroll-margin))
5504              (> (symbol-value 'scroll-margin) 0))
5505     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
5506     ;; too many number of lines if `scroll-margin' is set as two or greater.
5507     (setq lines (min lines
5508                      (max 0 (- (count-lines (window-start) (point-max))
5509                                (symbol-value 'scroll-margin))))))
5510   (condition-case ()
5511       (let ((scroll-in-place nil))
5512         (scroll-up lines))
5513     (end-of-buffer
5514      ;; Long lines may cause an end-of-buffer error.
5515      (goto-char (point-max))))
5516   (gnus-article-beginning-of-window))
5517
5518 (defun gnus-article-prev-page (&optional lines)
5519   "Show previous page of current article.
5520 Argument LINES specifies lines to be scrolled down."
5521   (interactive "p")
5522   (move-to-window-line 0)
5523   (if (and gnus-page-broken
5524            (bobp)
5525            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5526       (progn
5527         (gnus-narrow-to-page -1)        ;Go to previous page.
5528         (goto-char (point-max))
5529         (recenter -1))
5530     (prog1
5531         (condition-case ()
5532             (let ((scroll-in-place nil))
5533               (scroll-down lines))
5534           (beginning-of-buffer
5535            (goto-char (point-min))))
5536       (gnus-article-beginning-of-window))))
5537
5538 (defun gnus-article-only-boring-p ()
5539   "Decide whether there is only boring text remaining in the article.
5540 Something \"interesting\" is a word of at least two letters that does
5541 not have a face in `gnus-article-boring-faces'."
5542   (when (and gnus-article-skip-boring
5543              (boundp 'gnus-article-boring-faces)
5544              (symbol-value 'gnus-article-boring-faces))
5545     (save-excursion
5546       (catch 'only-boring
5547         (while (re-search-forward "\\b\\w\\w" nil t)
5548           (forward-char -1)
5549           (when (not (gnus-intersection
5550                       (gnus-faces-at (point))
5551                       (symbol-value 'gnus-article-boring-faces)))
5552             (throw 'only-boring nil)))
5553         (throw 'only-boring t)))))
5554
5555 (defun gnus-article-refer-article ()
5556   "Read article specified by message-id around point."
5557   (interactive)
5558   (save-excursion
5559     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
5560     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
5561     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
5562         (let ((msg-id (concat "<" (match-string 0) ">")))
5563           (set-buffer gnus-summary-buffer)
5564           (gnus-summary-refer-article msg-id))
5565       (error "No references around point"))))
5566
5567 (defun gnus-article-show-summary ()
5568   "Reconfigure windows to show summary buffer."
5569   (interactive)
5570   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5571       (error "There is no summary buffer for this article buffer")
5572     (gnus-article-set-globals)
5573     (gnus-configure-windows 'article)
5574     (gnus-summary-goto-subject gnus-current-article)
5575     (gnus-summary-position-point)))
5576
5577 (defun gnus-article-describe-briefly ()
5578   "Describe article mode commands briefly."
5579   (interactive)
5580   (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")))
5581
5582 (defun gnus-article-summary-command ()
5583   "Execute the last keystroke in the summary buffer."
5584   (interactive)
5585   (let ((obuf (current-buffer))
5586         (owin (current-window-configuration))
5587         func)
5588     (switch-to-buffer gnus-article-current-summary 'norecord)
5589     (setq func (lookup-key (current-local-map) (this-command-keys)))
5590     (call-interactively func)
5591     (set-buffer obuf)
5592     (set-window-configuration owin)
5593     (set-window-point (get-buffer-window (current-buffer)) (point))))
5594
5595 (defun gnus-article-summary-command-nosave ()
5596   "Execute the last keystroke in the summary buffer."
5597   (interactive)
5598   (let (func)
5599     (pop-to-buffer gnus-article-current-summary 'norecord)
5600     (setq func (lookup-key (current-local-map) (this-command-keys)))
5601     (call-interactively func)))
5602
5603 (defun gnus-article-check-buffer ()
5604   "Beep if not in an article buffer."
5605   (unless (equal major-mode 'gnus-article-mode)
5606     (error "Command invoked outside of a Gnus article buffer")))
5607
5608 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5609   "Read a summary buffer key sequence and execute it from the article buffer."
5610   (interactive "P")
5611   (gnus-article-check-buffer)
5612   (let ((nosaves
5613          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5614            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5615            "=" "^" "\M-^" "|"))
5616         (nosave-but-article
5617          '("A\r"))
5618         (nosave-in-article
5619          '("\C-d"))
5620         (up-to-top
5621          '("n" "Gn" "p" "Gp"))
5622         keys new-sum-point)
5623     (save-excursion
5624       (set-buffer gnus-article-current-summary)
5625       (let (gnus-pick-mode)
5626         (push (or key last-command-event) unread-command-events)
5627         (setq keys (if (featurep 'xemacs)
5628                        (events-to-keys (read-key-sequence nil))
5629                      (read-key-sequence nil)))))
5630
5631     (message "")
5632
5633     (if (or (member keys nosaves)
5634             (member keys nosave-but-article)
5635             (member keys nosave-in-article))
5636         (let (func)
5637           (save-window-excursion
5638             (pop-to-buffer gnus-article-current-summary 'norecord)
5639             ;; We disable the pick minor mode commands.
5640             (let (gnus-pick-mode)
5641               (setq func (lookup-key (current-local-map) keys))))
5642           (if (or (not func)
5643                   (numberp func))
5644               (ding)
5645             (unless (member keys nosave-in-article)
5646               (set-buffer gnus-article-current-summary))
5647             (call-interactively func)
5648             (setq new-sum-point (point)))
5649           (when (member keys nosave-but-article)
5650             (pop-to-buffer gnus-article-buffer 'norecord)))
5651       ;; These commands should restore window configuration.
5652       (let ((obuf (current-buffer))
5653             (owin (current-window-configuration))
5654             (opoint (point))
5655             win func in-buffer selected new-sum-start new-sum-hscroll)
5656         (cond (not-restore-window
5657                (pop-to-buffer gnus-article-current-summary 'norecord))
5658               ((setq win (get-buffer-window gnus-article-current-summary))
5659                (select-window win))
5660               (t
5661                (switch-to-buffer gnus-article-current-summary 'norecord)))
5662         (setq in-buffer (current-buffer))
5663         ;; We disable the pick minor mode commands.
5664         (if (and (setq func (let (gnus-pick-mode)
5665                               (lookup-key (current-local-map) keys)))
5666                  (functionp func))
5667             (progn
5668               (call-interactively func)
5669               (when (eq win (selected-window))
5670                 (setq new-sum-point (point)
5671                       new-sum-start (window-start win)
5672                       new-sum-hscroll (window-hscroll win)))
5673               (when (eq in-buffer (current-buffer))
5674                 (setq selected (gnus-summary-select-article))
5675                 (set-buffer obuf)
5676                 (unless not-restore-window
5677                   (set-window-configuration owin))
5678                 (when (eq selected 'old)
5679                   (article-goto-body)
5680                   (set-window-start (get-buffer-window (current-buffer))
5681                                     1)
5682                   (set-window-point (get-buffer-window (current-buffer))
5683                                     (point)))
5684                 (when (and (not not-restore-window)
5685                            new-sum-point)
5686                   (set-window-point win new-sum-point)
5687                   (set-window-start win new-sum-start)
5688                   (set-window-hscroll win new-sum-hscroll))))
5689           (set-window-configuration owin)
5690           (ding))))))
5691
5692 (defun gnus-article-describe-key (key)
5693   "Display documentation of the function invoked by KEY.  KEY is a string."
5694   (interactive "kDescribe key: ")
5695   (gnus-article-check-buffer)
5696   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5697       (save-excursion
5698         (set-buffer gnus-article-current-summary)
5699         (let (gnus-pick-mode)
5700           (if (featurep 'xemacs)
5701               (progn
5702                 (push (elt key 0) unread-command-events)
5703                 (setq key (events-to-keys
5704                            (read-key-sequence "Describe key: "))))
5705             (setq unread-command-events
5706                   (mapcar
5707                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5708                    (string-to-list key)))
5709             (setq key (read-key-sequence "Describe key: "))))
5710         (describe-key key))
5711     (describe-key key)))
5712
5713 (defun gnus-article-describe-key-briefly (key &optional insert)
5714   "Display documentation of the function invoked by KEY.  KEY is a string."
5715   (interactive "kDescribe key: \nP")
5716   (gnus-article-check-buffer)
5717   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5718       (save-excursion
5719         (set-buffer gnus-article-current-summary)
5720         (let (gnus-pick-mode)
5721           (if (featurep 'xemacs)
5722               (progn
5723                 (push (elt key 0) unread-command-events)
5724                 (setq key (events-to-keys
5725                            (read-key-sequence "Describe key: "))))
5726             (setq unread-command-events
5727                   (mapcar
5728                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5729                    (string-to-list key)))
5730             (setq key (read-key-sequence "Describe key: "))))
5731         (describe-key-briefly key insert))
5732     (describe-key-briefly key insert)))
5733
5734 (defun gnus-article-reply-with-original (&optional wide)
5735   "Start composing a reply mail to the current message.
5736 The text in the region will be yanked.  If the region isn't active,
5737 the entire article will be yanked."
5738   (interactive "P")
5739   (let ((article (cdr gnus-article-current))
5740         contents)
5741     (if (not (gnus-region-active-p))
5742         (with-current-buffer gnus-summary-buffer
5743           (gnus-summary-reply (list (list article)) wide))
5744       (setq contents (buffer-substring (point) (mark t)))
5745       ;; Deactivate active regions.
5746       (when (and (boundp 'transient-mark-mode)
5747                  transient-mark-mode)
5748         (setq mark-active nil))
5749       (with-current-buffer gnus-summary-buffer
5750         (gnus-summary-reply
5751          (list (list article contents)) wide)))))
5752
5753 (defun gnus-article-followup-with-original ()
5754   "Compose a followup to the current article.
5755 The text in the region will be yanked.  If the region isn't active,
5756 the entire article will be yanked."
5757   (interactive)
5758   (let ((article (cdr gnus-article-current))
5759         contents)
5760       (if (not (gnus-region-active-p))
5761           (with-current-buffer gnus-summary-buffer
5762             (gnus-summary-followup (list (list article))))
5763         (setq contents (buffer-substring (point) (mark t)))
5764         ;; Deactivate active regions.
5765         (when (and (boundp 'transient-mark-mode)
5766                    transient-mark-mode)
5767           (setq mark-active nil))
5768         (with-current-buffer gnus-summary-buffer
5769           (gnus-summary-followup
5770            (list (list article contents)))))))
5771
5772 (defun gnus-article-hide (&optional arg force)
5773   "Hide all the gruft in the current article.
5774 This means that signatures, cited text and (some) headers will be
5775 hidden.
5776 If given a prefix, show the hidden text instead."
5777   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5778   (gnus-article-hide-headers arg)
5779   (gnus-article-hide-list-identifiers arg)
5780   (gnus-article-hide-citation-maybe arg force)
5781   (gnus-article-hide-signature arg))
5782
5783 (defun gnus-article-maybe-highlight ()
5784   "Do some article highlighting if article highlighting is requested."
5785   (when (gnus-visual-p 'article-highlight 'highlight)
5786     (gnus-article-highlight-some)))
5787
5788 (defun gnus-check-group-server ()
5789   ;; Make sure the connection to the server is alive.
5790   (unless (gnus-server-opened
5791            (gnus-find-method-for-group gnus-newsgroup-name))
5792     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5793     (gnus-request-group gnus-newsgroup-name t)))
5794
5795 (eval-when-compile
5796   (autoload 'nneething-get-file-name "nneething"))
5797
5798 (defun gnus-request-article-this-buffer (article group)
5799   "Get an article and insert it into this buffer."
5800   (let (do-update-line sparse-header)
5801     (prog1
5802         (save-excursion
5803           (erase-buffer)
5804           (gnus-kill-all-overlays)
5805           (setq group (or group gnus-newsgroup-name))
5806
5807           ;; Using `gnus-request-article' directly will insert the article into
5808           ;; `nntp-server-buffer' - so we'll save some time by not having to
5809           ;; copy it from the server buffer into the article buffer.
5810
5811           ;; We only request an article by message-id when we do not have the
5812           ;; headers for it, so we'll have to get those.
5813           (when (stringp article)
5814             (gnus-read-header article))
5815
5816           ;; If the article number is negative, that means that this article
5817           ;; doesn't belong in this newsgroup (possibly), so we find its
5818           ;; message-id and request it by id instead of number.
5819           (when (and (numberp article)
5820                      gnus-summary-buffer
5821                      (get-buffer gnus-summary-buffer)
5822                      (gnus-buffer-exists-p gnus-summary-buffer))
5823             (save-excursion
5824               (set-buffer gnus-summary-buffer)
5825               (let ((header (gnus-summary-article-header article)))
5826                 (when (< article 0)
5827                   (cond
5828                    ((memq article gnus-newsgroup-sparse)
5829                     ;; This is a sparse gap article.
5830                     (setq do-update-line article)
5831                     (setq article (mail-header-id header))
5832                     (setq sparse-header (gnus-read-header article))
5833                     (setq gnus-newsgroup-sparse
5834                           (delq article gnus-newsgroup-sparse)))
5835                    ((vectorp header)
5836                     ;; It's a real article.
5837                     (setq article (mail-header-id header)))
5838                    (t
5839                     ;; It is an extracted pseudo-article.
5840                     (setq article 'pseudo)
5841                     (gnus-request-pseudo-article header))))
5842
5843                 (let ((method (gnus-find-method-for-group
5844                                gnus-newsgroup-name)))
5845                   (when (and (eq (car method) 'nneething)
5846                              (vectorp header))
5847                     (let ((dir (nneething-get-file-name
5848                                 (mail-header-id header))))
5849                       (when (and (stringp dir)
5850                                  (file-directory-p dir))
5851                         (setq article 'nneething)
5852                         (gnus-group-enter-directory dir))))))))
5853
5854           (cond
5855            ;; Refuse to select canceled articles.
5856            ((and (numberp article)
5857                  gnus-summary-buffer
5858                  (get-buffer gnus-summary-buffer)
5859                  (gnus-buffer-exists-p gnus-summary-buffer)
5860                  (eq (cdr (with-current-buffer gnus-summary-buffer
5861                             (assq article gnus-newsgroup-reads)))
5862                      gnus-canceled-mark))
5863             nil)
5864            ;; We first check `gnus-original-article-buffer'.
5865            ((and (get-buffer gnus-original-article-buffer)
5866                  (numberp article)
5867                  (with-current-buffer gnus-original-article-buffer
5868                    (and (equal (car gnus-original-article) group)
5869                         (eq (cdr gnus-original-article) article))))
5870             (insert-buffer-substring gnus-original-article-buffer)
5871             'article)
5872            ;; Check the backlog.
5873            ((and gnus-keep-backlog
5874                  (gnus-backlog-request-article group article (current-buffer)))
5875             'article)
5876            ;; Check asynchronous pre-fetch.
5877            ((gnus-async-request-fetched-article group article (current-buffer))
5878             (gnus-async-prefetch-next group article gnus-summary-buffer)
5879             (when (and (numberp article) gnus-keep-backlog)
5880               (gnus-backlog-enter-article group article (current-buffer)))
5881             'article)
5882            ;; Check the cache.
5883            ((and gnus-use-cache
5884                  (numberp article)
5885                  (gnus-cache-request-article article group))
5886             'article)
5887            ;; Check the agent cache.
5888            ((gnus-agent-request-article article group)
5889             'article)
5890            ;; Get the article and put into the article buffer.
5891            ((or (stringp article)
5892                 (numberp article))
5893             (let ((gnus-override-method gnus-override-method)
5894                   (methods (and (stringp article)
5895                                 gnus-refer-article-method))
5896                   (backend (car (gnus-find-method-for-group
5897                                  gnus-newsgroup-name)))
5898                   result
5899                   (inhibit-read-only t))
5900               (if (or (not (listp methods))
5901                       (and (symbolp (car methods))
5902                            (assq (car methods) nnoo-definition-alist)))
5903                   (setq methods (list methods)))
5904               (when (and (null gnus-override-method)
5905                          methods)
5906                 (setq gnus-override-method (pop methods)))
5907               (while (not result)
5908                 (when (eq gnus-override-method 'current)
5909                   (setq gnus-override-method
5910                         (with-current-buffer gnus-summary-buffer
5911                           gnus-current-select-method)))
5912                 (erase-buffer)
5913                 (gnus-kill-all-overlays)
5914                 (let ((gnus-newsgroup-name group))
5915                   (gnus-check-group-server))
5916                 (cond
5917                  ((gnus-request-article article group (current-buffer))
5918                   (when (numberp article)
5919                     (gnus-async-prefetch-next group article
5920                                               gnus-summary-buffer)
5921                     (when gnus-keep-backlog
5922                       (gnus-backlog-enter-article
5923                        group article (current-buffer))))
5924                   (setq result 'article))
5925                  (methods
5926                   (setq gnus-override-method (pop methods)))
5927                  ((not (string-match "^400 "
5928                                      (nnheader-get-report backend)))
5929                   ;; If we get 400 server disconnect, reconnect and
5930                   ;; retry; otherwise, assume the article has expired.
5931                   (setq result 'done))))
5932               (and (eq result 'article) 'article)))
5933            ;; It was a pseudo.
5934            (t article)))
5935
5936       ;; Associate this article with the current summary buffer.
5937       (setq gnus-article-current-summary gnus-summary-buffer)
5938
5939       ;; Take the article from the original article buffer
5940       ;; and place it in the buffer it's supposed to be in.
5941       (when (and (get-buffer gnus-article-buffer)
5942                  (equal (buffer-name (current-buffer))
5943                         (buffer-name (get-buffer gnus-article-buffer))))
5944         (save-excursion
5945           (if (get-buffer gnus-original-article-buffer)
5946               (set-buffer gnus-original-article-buffer)
5947             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5948             (buffer-disable-undo)
5949             (setq major-mode 'gnus-original-article-mode)
5950             (setq buffer-read-only t))
5951           (let ((inhibit-read-only t))
5952             (erase-buffer)
5953             (insert-buffer-substring gnus-article-buffer))
5954           (setq gnus-original-article (cons group article)))
5955
5956         ;; Decode charsets.
5957         (run-hooks 'gnus-article-decode-hook)
5958         ;; Mark article as decoded or not.
5959         (setq gnus-article-decoded-p gnus-article-decode-hook))
5960
5961       ;; Update sparse articles.
5962       (when (and do-update-line
5963                  (or (numberp article)
5964                      (stringp article)))
5965         (let ((buf (current-buffer)))
5966           (set-buffer gnus-summary-buffer)
5967           (gnus-summary-update-article do-update-line sparse-header)
5968           (gnus-summary-goto-subject do-update-line nil t)
5969           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5970                             (point))
5971           (set-buffer buf))))))
5972
5973 ;;;
5974 ;;; Article editing
5975 ;;;
5976
5977 (defcustom gnus-article-edit-mode-hook nil
5978   "Hook run in article edit mode buffers."
5979   :group 'gnus-article-various
5980   :type 'hook)
5981
5982 (defvar gnus-article-edit-done-function nil)
5983
5984 (defvar gnus-article-edit-mode-map nil)
5985
5986 ;; Should we be using derived.el for this?
5987 (unless gnus-article-edit-mode-map
5988   (setq gnus-article-edit-mode-map (make-keymap))
5989   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5990
5991   (gnus-define-keys gnus-article-edit-mode-map
5992     "\C-c?"    describe-mode
5993     "\C-c\C-c" gnus-article-edit-done
5994     "\C-c\C-k" gnus-article-edit-exit
5995     "\C-c\C-f\C-t" message-goto-to
5996     "\C-c\C-f\C-o" message-goto-from
5997     "\C-c\C-f\C-b" message-goto-bcc
5998     ;;"\C-c\C-f\C-w" message-goto-fcc
5999     "\C-c\C-f\C-c" message-goto-cc
6000     "\C-c\C-f\C-s" message-goto-subject
6001     "\C-c\C-f\C-r" message-goto-reply-to
6002     "\C-c\C-f\C-n" message-goto-newsgroups
6003     "\C-c\C-f\C-d" message-goto-distribution
6004     "\C-c\C-f\C-f" message-goto-followup-to
6005     "\C-c\C-f\C-m" message-goto-mail-followup-to
6006     "\C-c\C-f\C-k" message-goto-keywords
6007     "\C-c\C-f\C-u" message-goto-summary
6008     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6009     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6010     "\C-c\C-b" message-goto-body
6011     "\C-c\C-i" message-goto-signature
6012
6013     "\C-c\C-t" message-insert-to
6014     "\C-c\C-n" message-insert-newsgroups
6015     "\C-c\C-o" message-sort-headers
6016     "\C-c\C-e" message-elide-region
6017     "\C-c\C-v" message-delete-not-region
6018     "\C-c\C-z" message-kill-to-signature
6019     "\M-\r" message-newline-and-reformat
6020     "\C-c\C-a" mml-attach-file
6021     "\C-a" message-beginning-of-line
6022     "\t" message-tab
6023     "\M-;" comment-region)
6024
6025   (gnus-define-keys (gnus-article-edit-wash-map
6026                      "\C-c\C-w" gnus-article-edit-mode-map)
6027     "f" gnus-article-edit-full-stops))
6028
6029 (easy-menu-define
6030   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6031   '("Field"
6032     ["Fetch To" message-insert-to t]
6033     ["Fetch Newsgroups" message-insert-newsgroups t]
6034     "----"
6035     ["To" message-goto-to t]
6036     ["From" message-goto-from t]
6037     ["Subject" message-goto-subject t]
6038     ["Cc" message-goto-cc t]
6039     ["Reply-To" message-goto-reply-to t]
6040     ["Summary" message-goto-summary t]
6041     ["Keywords" message-goto-keywords t]
6042     ["Newsgroups" message-goto-newsgroups t]
6043     ["Followup-To" message-goto-followup-to t]
6044     ["Mail-Followup-To" message-goto-mail-followup-to t]
6045     ["Distribution" message-goto-distribution t]
6046     ["Body" message-goto-body t]
6047     ["Signature" message-goto-signature t]))
6048
6049 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6050   "Major mode for editing articles.
6051 This is an extended text-mode.
6052
6053 \\{gnus-article-edit-mode-map}"
6054   (make-local-variable 'gnus-article-edit-done-function)
6055   (make-local-variable 'gnus-prev-winconf)
6056   (set (make-local-variable 'font-lock-defaults)
6057        '(message-font-lock-keywords t))
6058   (set (make-local-variable 'mail-header-separator) "")
6059   (set (make-local-variable 'gnus-article-edit-mode) t)
6060   (easy-menu-add message-mode-field-menu message-mode-map)
6061   (mml-mode)
6062   (setq buffer-read-only nil)
6063   (buffer-enable-undo)
6064   (widen))
6065
6066 (defun gnus-article-edit (&optional force)
6067   "Edit the current article.
6068 This will have permanent effect only in mail groups.
6069 If FORCE is non-nil, allow editing of articles even in read-only
6070 groups."
6071   (interactive "P")
6072   (when (and (not force)
6073              (gnus-group-read-only-p))
6074     (error "The current newsgroup does not support article editing"))
6075   (gnus-article-date-original)
6076   (gnus-article-edit-article
6077    'ignore
6078    `(lambda (no-highlight)
6079       'ignore
6080       (gnus-summary-edit-article-done
6081        ,(or (mail-header-references gnus-current-headers) "")
6082        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6083
6084 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6085   "Start editing the contents of the current article buffer."
6086   (let ((winconf (current-window-configuration)))
6087     (set-buffer gnus-article-buffer)
6088     (let ((message-auto-save-directory
6089            ;; Don't associate the article buffer with a draft file.
6090            nil))
6091       (gnus-article-edit-mode))
6092     (funcall start-func)
6093     (set-buffer-modified-p nil)
6094     (gnus-configure-windows 'edit-article)
6095     (setq gnus-article-edit-done-function exit-func)
6096     (setq gnus-prev-winconf winconf)
6097     (unless quiet
6098       (gnus-message 6 "C-c C-c to end edits"))))
6099
6100 (defun gnus-article-edit-done (&optional arg)
6101   "Update the article edits and exit."
6102   (interactive "P")
6103   (let ((func gnus-article-edit-done-function)
6104         (buf (current-buffer))
6105         (start (window-start))
6106         (p (point))
6107         (winconf gnus-prev-winconf))
6108     (widen) ;; Widen it in case that users narrowed the buffer.
6109     (funcall func arg)
6110     (set-buffer buf)
6111     ;; The cache and backlog have to be flushed somewhat.
6112     (when gnus-keep-backlog
6113       (gnus-backlog-remove-article
6114        (car gnus-article-current) (cdr gnus-article-current)))
6115     ;; Flush original article as well.
6116     (save-excursion
6117       (when (get-buffer gnus-original-article-buffer)
6118         (set-buffer gnus-original-article-buffer)
6119         (setq gnus-original-article nil)))
6120     (when gnus-use-cache
6121       (gnus-cache-update-article
6122        (car gnus-article-current) (cdr gnus-article-current)))
6123     ;; We remove all text props from the article buffer.
6124     (kill-all-local-variables)
6125     (set-text-properties (point-min) (point-max) nil)
6126     (gnus-article-mode)
6127     (set-window-configuration winconf)
6128     (set-buffer buf)
6129     (set-window-start (get-buffer-window buf) start)
6130     (set-window-point (get-buffer-window buf) (point)))
6131   (gnus-summary-show-article))
6132
6133 (defun gnus-article-edit-exit ()
6134   "Exit the article editing without updating."
6135   (interactive)
6136   (when (or (not (buffer-modified-p))
6137             (yes-or-no-p "Article modified; kill anyway? "))
6138     (let ((curbuf (current-buffer))
6139           (p (point))
6140           (window-start (window-start)))
6141       (erase-buffer)
6142       (if (gnus-buffer-live-p gnus-original-article-buffer)
6143           (insert-buffer-substring gnus-original-article-buffer))
6144       (let ((winconf gnus-prev-winconf))
6145         (kill-all-local-variables)
6146         (gnus-article-mode)
6147         (set-window-configuration winconf)
6148         ;; Tippy-toe some to make sure that point remains where it was.
6149         (save-current-buffer
6150           (set-buffer curbuf)
6151           (set-window-start (get-buffer-window (current-buffer)) window-start)
6152           (goto-char p))))
6153     (gnus-summary-show-article)))
6154
6155 (defun gnus-article-edit-full-stops ()
6156   "Interactively repair spacing at end of sentences."
6157   (interactive)
6158   (save-excursion
6159     (goto-char (point-min))
6160     (search-forward-regexp "^$" nil t)
6161     (let ((case-fold-search nil))
6162       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6163
6164 ;;;
6165 ;;; Article highlights
6166 ;;;
6167
6168 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6169
6170 ;;; Internal Variables:
6171
6172 (defcustom gnus-button-url-regexp
6173   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6174       "\\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:]]\\)"
6175     "\\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\\)\\)")
6176   "Regular expression that matches URLs."
6177   :group 'gnus-article-buttons
6178   :type 'regexp)
6179
6180 (defcustom gnus-button-valid-fqdn-regexp
6181   message-valid-fqdn-regexp
6182   "Regular expression that matches a valid FQDN."
6183   :version "22.1"
6184   :group 'gnus-article-buttons
6185   :type 'regexp)
6186
6187 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6188 (defcustom gnus-button-valid-localpart-regexp
6189   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t ]*"
6190   "Regular expression that matches a localpart of mail addresses or MIDs."
6191   :version "22.1"
6192   :group 'gnus-article-buttons
6193   :type 'regexp)
6194
6195 (defcustom gnus-button-man-handler 'manual-entry
6196   "Function to use for displaying man pages.
6197 The function must take at least one argument with a string naming the
6198 man page."
6199   :version "22.1"
6200   :type '(choice (function-item :tag "Man" manual-entry)
6201                  (function-item :tag "Woman" woman)
6202                  (function :tag "Other"))
6203   :group 'gnus-article-buttons)
6204
6205 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6206   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6207 If the default site is too slow, try to find a CTAN mirror, see
6208 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6209 the variable `gnus-button-handle-ctan'."
6210   :version "22.1"
6211   :group 'gnus-article-buttons
6212   :link '(custom-manual "(gnus)Group Parameters")
6213   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6214                  (const "http://tug.ctan.org/tex-archive/")
6215                  (const "http://www.dante.de/CTAN/")
6216                  (string :tag "Other")))
6217
6218 (defcustom gnus-button-ctan-handler 'browse-url
6219   "Function to use for displaying CTAN links.
6220 The function must take one argument, the string naming the URL."
6221   :version "22.1"
6222   :type '(choice (function-item :tag "Browse Url" browse-url)
6223                  (function :tag "Other"))
6224   :group 'gnus-article-buttons)
6225
6226 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6227   "Bogus strings removed from CTAN URLs."
6228   :version "22.1"
6229   :group 'gnus-article-buttons
6230   :type '(choice (const "^/?tex-archive/\\|/")
6231                  (regexp :tag "Other")))
6232
6233 (defcustom gnus-button-ctan-directory-regexp
6234   (regexp-opt
6235    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6236          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6237          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6238          "tds" "tools" "usergrps" "web") t)
6239   "Regular expression for ctan directories.
6240 It should match all directories in the top level of `gnus-ctan-url'."
6241   :version "22.1"
6242   :group 'gnus-article-buttons
6243   :type 'regexp)
6244
6245 (defcustom gnus-button-mid-or-mail-regexp
6246   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6247           gnus-button-valid-fqdn-regexp
6248           ">?\\)\\b")
6249   "Regular expression that matches a message ID or a mail address."
6250   :version "22.1"
6251   :group 'gnus-article-buttons
6252   :type 'regexp)
6253
6254 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6255   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6256 Strings like this can be either a message ID or a mail address.  If it is one
6257 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6258 message ID or a mail address, respectively.  If this variable is set to the
6259 symbol `ask', always query the user what do do.  If it is a function, this
6260 function will be called with the string as it's only argument.  The function
6261 must return `mid', `mail', `invalid' or `ask'."
6262   :version "22.1"
6263   :group 'gnus-article-buttons
6264   :type '(choice (function-item :tag "Heuristic function"
6265                                 gnus-button-mid-or-mail-heuristic)
6266                  (const ask)
6267                  (const mid)
6268                  (const mail)))
6269
6270 (defcustom gnus-button-mid-or-mail-heuristic-alist
6271   '((-10.0 . ".+\\$.+@")
6272     (-10.0 . "#")
6273     (-10.0 . "\\*")
6274     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6275     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6276     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6277     (-1.0  . "^[^a-z]+@")
6278     ;;
6279     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6280     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6281     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6282     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6283     ;;
6284     (-2.0  . "^[0-9]")
6285     (-1.0  . "^[0-9][0-9]")
6286     ;;
6287     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6288     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6289     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6290     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6291     ;;
6292     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6293     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6294     ;;       "[0-9]{8,}.*\@"
6295     (-3.0
6296      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6297     ;; "[0-9]{12,}.*\@"
6298     ;; compensation for TDMA dated mail addresses:
6299     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6300     ;;
6301     (-20.0 . "\\.fsf@") ;; Gnus
6302     (-20.0 . "^slrn")
6303     (-20.0 . "^Pine")
6304     (-20.0 . "_-_") ;; Subject change in thread
6305     ;;
6306     (-20.0 . "\\.ln@") ;; leafnode
6307     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6308     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6309     ;;
6310     ;; (5.0 . "") ;; $local_part_len <= 7
6311     (10.0  . "^[^0-9]+@")
6312     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6313     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6314     (3.0   . "\@stud")
6315     ;;
6316     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6317     ;;
6318     (0.5   . "^[A-Z][a-z]")
6319     (0.5   . "^[A-Z][a-z][a-z]")
6320     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6321     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6322   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6323
6324 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6325 address.  The REGEXP is processed with `case-fold-search' set to nil."
6326   :version "22.1"
6327   :group 'gnus-article-buttons
6328   :type '(repeat (cons (number :tag "Rate")
6329                        (regexp :tag "Regexp"))))
6330
6331 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6332   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6333 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6334 address, `ask' if unsure and `invalid' if the string is invalid."
6335   (let ((case-fold-search nil)
6336         (list gnus-button-mid-or-mail-heuristic-alist)
6337         (result 0) rate regexp lpartlen elem)
6338     (setq lpartlen
6339           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6340     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6341     ;; Certain special cases...
6342     (when (string-match
6343            (concat
6344             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
6345             "^[0-9]+\\.[0-9]+@compuserve\\|"
6346             "@public\\.gmane\\.org")
6347            mid-or-mail)
6348       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
6349       (setq result 'mail))
6350     (when (string-match "@.*@\\| " mid-or-mail)
6351       (gnus-message 8 "`%s' is invalid." mid-or-mail)
6352       (setq result 'invalid))
6353     ;; Nothing more to do, if result is not a number here...
6354     (when (numberp result)
6355       (while list
6356         (setq elem (car list)
6357               rate (car elem)
6358               regexp (cdr elem)
6359               list (cdr list))
6360         (when (string-match regexp mid-or-mail)
6361           (setq result (+ result rate))
6362           (gnus-message
6363            9 "`%s' matched `%s', rate `%s', result `%s'."
6364            mid-or-mail regexp rate result)))
6365       (when (<= lpartlen 7)
6366         (setq result (+ result 5.0))
6367         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6368                       mid-or-mail result))
6369       (when (>= lpartlen 12)
6370         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6371         (cond
6372          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6373           ;; Long local part should contain realname if e-mail address,
6374           ;; too many digits: message-id.
6375           ;; $score -= 5.0 + 0.1 * $local_part_len;
6376           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6377           (setq result (+ result rate))
6378           (gnus-message
6379            9 "Many digits in `%s', rate `%s', result `%s'."
6380            mid-or-mail rate result))
6381          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6382                         mid-or-mail)
6383           ;; Too few vowels [^aeiouy]{4,}.*\@
6384           (setq result (+ result -5.0))
6385           (gnus-message
6386            9 "Few vowels in `%s', rate `%s', result `%s'."
6387            mid-or-mail -5.0 result))
6388          (t
6389           (setq result (+ result 5.0))
6390           (gnus-message
6391            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6392     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6393     ;; Maybe we should make this a customizable alist: (condition . 'result)
6394     (cond
6395      ((symbolp result) result)
6396      ;; Now convert number into proper results:
6397      ((< result -10.0) 'mid)
6398      ((> result  10.0) 'mail)
6399      (t 'ask))))
6400
6401 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6402   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6403          (url-mid (concat "news" ":" mid-or-mail))
6404          (url-mailto (concat "mailto" ":" mid-or-mail)))
6405     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6406     (when (fboundp pref)
6407       (setq guessed
6408             ;; get rid of surrounding angles...
6409             (funcall pref
6410                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6411       (if (or (eq 'mid guessed) (eq 'mail guessed))
6412           (setq pref guessed)
6413         (setq pref 'ask)))
6414     (if (eq pref 'ask)
6415         (save-window-excursion
6416           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6417               (setq pref 'mail)
6418             (setq pref 'mid))))
6419     (cond ((eq pref 'mid)
6420            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6421            (gnus-button-handle-news url-mid))
6422           ((eq pref 'mail)
6423            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6424            (gnus-url-mailto url-mailto))
6425           (t (gnus-message 3 "Invalid string.")))))
6426
6427 (defun gnus-button-handle-custom (fun arg)
6428   "Call function FUN on argument ARG.
6429 Both FUN and ARG are supposed to be strings.  ARG will be passed
6430 as a symbol to FUN."
6431   (funcall (intern fun) (intern arg)))
6432
6433 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6434
6435 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6436 ;; stuff?
6437
6438 (defun gnus-button-handle-describe-function (url)
6439   "Call `describe-function' when pushing the corresponding URL button."
6440   (describe-function
6441    (intern
6442     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6443
6444 (defun gnus-button-handle-describe-variable (url)
6445   "Call `describe-variable' when pushing the corresponding URL button."
6446   (describe-variable
6447    (intern
6448     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6449
6450 (defun gnus-button-handle-symbol (url)
6451 "Display help on variable or function.
6452 Calls `describe-variable' or `describe-function'."
6453   (let ((sym (intern url)))
6454     (cond
6455      ((fboundp sym) (describe-function sym))
6456      ((boundp sym) (describe-variable sym))
6457      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6458
6459 (defun gnus-button-handle-describe-key (url)
6460   "Call `describe-key' when pushing the corresponding URL button."
6461   (let* ((key-string
6462           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6463          (keys (ignore-errors (eval `(kbd ,key-string)))))
6464     (if keys
6465         (describe-key keys)
6466       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6467
6468 (defun gnus-button-handle-apropos (url)
6469   "Call `apropos' when pushing the corresponding URL button."
6470   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6471
6472 (defun gnus-button-handle-apropos-command (url)
6473   "Call `apropos' when pushing the corresponding URL button."
6474   (apropos-command
6475    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6476
6477 (defun gnus-button-handle-apropos-variable (url)
6478   "Call `apropos' when pushing the corresponding URL button."
6479   (funcall
6480    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6481    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6482
6483 (defun gnus-button-handle-apropos-documentation (url)
6484   "Call `apropos' when pushing the corresponding URL button."
6485   (funcall
6486    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6487    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6488
6489 (defun gnus-button-handle-library (url)
6490   "Call `locate-library' when pushing the corresponding URL button."
6491   (gnus-message 9 "url=`%s'" url)
6492   (let* ((lib (locate-library url))
6493          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6494     (if (not lib)
6495         (gnus-message 1 "Cannot locale library `%s'." url)
6496       (find-file-read-only file))))
6497
6498 (defun gnus-button-handle-ctan (url)
6499   "Call `browse-url' when pushing a CTAN URL button."
6500   (funcall
6501    gnus-button-ctan-handler
6502    (concat
6503     gnus-ctan-url
6504     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6505
6506 (defcustom gnus-button-tex-level 5
6507   "*Integer that says how many TeX-related buttons Gnus will show.
6508 The higher the number, the more buttons will appear and the more false
6509 positives are possible.  Note that you can set this variable local to
6510 specific groups.  Setting it higher in TeX groups is probably a good idea.
6511 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6512 how to set variables in specific groups."
6513   :version "22.1"
6514   :group 'gnus-article-buttons
6515   :link '(custom-manual "(gnus)Group Parameters")
6516   :type 'integer)
6517
6518 (defcustom gnus-button-man-level 5
6519   "*Integer that says how many man-related buttons Gnus will show.
6520 The higher the number, the more buttons will appear and the more false
6521 positives are possible.  Note that you can set this variable local to
6522 specific groups.  Setting it higher in Unix groups is probably a good idea.
6523 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6524 how to set variables in specific groups."
6525   :version "22.1"
6526   :group 'gnus-article-buttons
6527   :link '(custom-manual "(gnus)Group Parameters")
6528   :type 'integer)
6529
6530 (defcustom gnus-button-emacs-level 5
6531   "*Integer that says how many emacs-related buttons Gnus will show.
6532 The higher the number, the more buttons will appear and the more false
6533 positives are possible.  Note that you can set this variable local to
6534 specific groups.  Setting it higher in Emacs or Gnus related groups is
6535 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6536 `gnus-parameters' on how to set variables in specific groups."
6537   :version "22.1"
6538   :group 'gnus-article-buttons
6539   :link '(custom-manual "(gnus)Group Parameters")
6540   :type 'integer)
6541
6542 (defcustom gnus-button-message-level 5
6543   "*Integer that says how many buttons for news or mail messages will appear.
6544 The higher the number, the more buttons will appear and the more false
6545 positives are possible."
6546   ;; mail addresses, MIDs, URLs for news, ...
6547   :version "22.1"
6548   :group 'gnus-article-buttons
6549   :type 'integer)
6550
6551 (defcustom gnus-button-browse-level 5
6552   "*Integer that says how many buttons for browsing will appear.
6553 The higher the number, the more buttons will appear and the more false
6554 positives are possible."
6555   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6556   :version "22.1"
6557   :group 'gnus-article-buttons
6558   :type 'integer)
6559
6560 (defcustom gnus-button-alist
6561   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6562      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6563     ((concat "\\b\\(nntp\\|news\\):\\("
6564              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
6565      0 t gnus-button-handle-news 2)
6566     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6567      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6568     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6569      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6570     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
6571     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
6572      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
6573     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6574      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6575     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6576      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6577     ;; RFC 2368 (The mailto URL scheme)
6578     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6579      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6580     ("\\bmailto:\\([^ \n\t]+\\)"
6581      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6582     ;; CTAN
6583     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6584              gnus-button-ctan-directory-regexp
6585              "[^][>)!;:,'\n\t ]+\\)")
6586      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6587     ((concat "\\btex-archive/\\("
6588              gnus-button-ctan-directory-regexp
6589              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6590      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6591     ((concat
6592       "\\b\\("
6593       gnus-button-ctan-directory-regexp
6594       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6595      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6596     ;; This is info (home-grown style) <info://foo/bar+baz>
6597     ("\\binfo://\\([^'\">\n\t ]+\\)"
6598      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
6599     ;; Info GNOME style <info:foo#bar_baz>
6600     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
6601      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
6602     ;; Info KDE style <info:(foo)bar baz>
6603     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
6604      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
6605     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6606      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6607     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6608      ;; Info links like `C-h i d m CC Mode RET'
6609      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6610     ;; This is custom
6611     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6612      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
6613     ;; Emacs help commands
6614     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6615      ;; regexp doesn't match arguments containing ` '.
6616      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6617     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6618      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6619     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6620      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6621     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6622      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6623     ;; The following entries may lead to many false positives so don't enable
6624     ;; them by default (use a high button level).
6625     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
6626      ;; Exclude [.?] for URLs in gmane.emacs.cvs
6627      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6628     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6629      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6630     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6631      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6632     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6633      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6634     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6635      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6636     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6637      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
6638     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6639      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6640     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6641      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6642     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6643      ;; Unlike the other regexps we really have to require quoting
6644      ;; here to determine where it ends.
6645      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6646     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
6647     ("<URL: *\\([^<>]*\\)>"
6648      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6649     ;; RFC 2396 (2.4.3., delims) ...
6650     ("\"URL: *\\([^\"]*\\)\""
6651      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6652     ;; RFC 2396 (2.4.3., delims) ...
6653     ("\"URL: *\\([^\"]*\\)\""
6654      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6655     ;; Raw URLs.
6656     (gnus-button-url-regexp
6657      0 (>= gnus-button-browse-level 0) browse-url 0)
6658     ;; man pages
6659     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
6660      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6661      gnus-button-handle-man 1)
6662     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6663     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
6664      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6665      gnus-button-handle-man 1)
6666     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6667     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6668     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
6669      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6670     ;; MID or mail: To avoid too many false positives we don't try to catch
6671     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6672     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6673     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6674     ;; so that non-ambiguous entries (see above) match first.
6675     (gnus-button-mid-or-mail-regexp
6676      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6677   "*Alist of regexps matching buttons in article bodies.
6678
6679 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6680 REGEXP: is the string (case insensitive) matching text around the button (can
6681 also be Lisp expression evaluating to a string),
6682 BUTTON: is the number of the regexp grouping actually matching the button,
6683 FORM: is a Lisp expression which must eval to true for the button to
6684 be added,
6685 CALLBACK: is the function to call when the user push this button, and each
6686 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6687
6688 CALLBACK can also be a variable, in that case the value of that
6689 variable it the real callback function."
6690   :group 'gnus-article-buttons
6691   :type '(repeat (list (choice regexp variable sexp)
6692                        (integer :tag "Button")
6693                        (sexp :tag "Form")
6694                        (function :tag "Callback")
6695                        (repeat :tag "Par"
6696                                :inline t
6697                                (integer :tag "Regexp group")))))
6698
6699 (defcustom gnus-header-button-alist
6700   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
6701      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6702     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6703      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6704     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6705      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
6706     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6707      0 (>= gnus-button-browse-level 0) browse-url 0)
6708     ("^Subject:" gnus-button-url-regexp
6709      0 (>= gnus-button-browse-level 0) browse-url 0)
6710     ("^[^:]+:" gnus-button-url-regexp
6711      0 (>= gnus-button-browse-level 0) browse-url 0)
6712     ("^OpenPGP:.*url=" gnus-button-url-regexp
6713      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
6714     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6715      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6716     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6717      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6718   "*Alist of headers and regexps to match buttons in article heads.
6719
6720 This alist is very similar to `gnus-button-alist', except that each
6721 alist has an additional HEADER element first in each entry:
6722
6723 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6724
6725 HEADER is a regexp to match a header.  For a fuller explanation, see
6726 `gnus-button-alist'."
6727   :group 'gnus-article-buttons
6728   :group 'gnus-article-headers
6729   :type '(repeat (list (regexp :tag "Header")
6730                        (choice regexp variable)
6731                        (integer :tag "Button")
6732                        (sexp :tag "Form")
6733                        (function :tag "Callback")
6734                        (repeat :tag "Par"
6735                                :inline t
6736                                (integer :tag "Regexp group")))))
6737
6738 ;;; Commands:
6739
6740 (defun gnus-article-push-button (event)
6741   "Check text under the mouse pointer for a callback function.
6742 If the text under the mouse pointer has a `gnus-callback' property,
6743 call it with the value of the `gnus-data' text property."
6744   (interactive "e")
6745   (set-buffer (window-buffer (posn-window (event-start event))))
6746   (let* ((pos (posn-point (event-start event)))
6747          (data (get-text-property pos 'gnus-data))
6748          (fun (get-text-property pos 'gnus-callback)))
6749     (goto-char pos)
6750     (when fun
6751       (funcall fun data))))
6752
6753 (defun gnus-article-press-button ()
6754   "Check text at point for a callback function.
6755 If the text at point has a `gnus-callback' property,
6756 call it with the value of the `gnus-data' text property."
6757   (interactive)
6758   (let ((data (get-text-property (point) 'gnus-data))
6759         (fun (get-text-property (point) 'gnus-callback)))
6760     (when fun
6761       (funcall fun data))))
6762
6763 (defun gnus-article-highlight (&optional force)
6764   "Highlight current article.
6765 This function calls `gnus-article-highlight-headers',
6766 `gnus-article-highlight-citation',
6767 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6768 do the highlighting.  See the documentation for those functions."
6769   (interactive (list 'force))
6770   (gnus-article-highlight-headers)
6771   (gnus-article-highlight-citation force)
6772   (gnus-article-highlight-signature)
6773   (gnus-article-add-buttons force)
6774   (gnus-article-add-buttons-to-head))
6775
6776 (defun gnus-article-highlight-some (&optional force)
6777   "Highlight current article.
6778 This function calls `gnus-article-highlight-headers',
6779 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6780 do the highlighting.  See the documentation for those functions."
6781   (interactive (list 'force))
6782   (gnus-article-highlight-headers)
6783   (gnus-article-highlight-signature)
6784   (gnus-article-add-buttons))
6785
6786 (defun gnus-article-highlight-headers ()
6787   "Highlight article headers as specified by `gnus-header-face-alist'."
6788   (interactive)
6789   (gnus-with-article-headers
6790     (let (regexp header-face field-face from hpoints fpoints)
6791       (dolist (entry gnus-header-face-alist)
6792         (goto-char (point-min))
6793         (setq regexp (concat "^\\("
6794                              (if (string-equal "" (nth 0 entry))
6795                                  "[^\t ]"
6796                                (nth 0 entry))
6797                              "\\)")
6798               header-face (nth 1 entry)
6799               field-face (nth 2 entry))
6800         (while (and (re-search-forward regexp nil t)
6801                     (not (eobp)))
6802           (beginning-of-line)
6803           (setq from (point))
6804           (unless (search-forward ":" nil t)
6805             (forward-char 1))
6806           (when (and header-face
6807                      (not (memq (point) hpoints)))
6808             (push (point) hpoints)
6809             (gnus-put-text-property from (point) 'face header-face))
6810           (when (and field-face
6811                      (not (memq (setq from (point)) fpoints)))
6812             (push from fpoints)
6813             (if (re-search-forward "^[^ \t]" nil t)
6814                 (forward-char -2)
6815               (goto-char (point-max)))
6816             (gnus-put-text-property from (point) 'face field-face)))))))
6817
6818 (defun gnus-article-highlight-signature ()
6819   "Highlight the signature in an article.
6820 It does this by highlighting everything after
6821 `gnus-signature-separator' using the face `gnus-signature'."
6822   (interactive)
6823   (gnus-with-article-buffer
6824     (let ((inhibit-point-motion-hooks t))
6825       (save-restriction
6826         (when (and gnus-signature-face
6827                    (gnus-article-narrow-to-signature))
6828           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6829                             'face gnus-signature-face)
6830           (widen)
6831           (gnus-article-search-signature)
6832           (let ((start (match-beginning 0))
6833                 (end (set-marker (make-marker) (1+ (match-end 0)))))
6834             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
6835                                      end)))))))
6836
6837 (defun gnus-button-in-region-p (b e prop)
6838   "Say whether PROP exists in the region."
6839   (text-property-not-all b e prop nil))
6840
6841 (defun gnus-article-add-buttons (&optional force)
6842   "Find external references in the article and make buttons of them.
6843 \"External references\" are things like Message-IDs and URLs, as
6844 specified by `gnus-button-alist'."
6845   (interactive (list 'force))
6846   (gnus-with-article-buffer
6847     (let ((inhibit-point-motion-hooks t)
6848           (case-fold-search t)
6849           (alist gnus-button-alist)
6850           beg entry regexp)
6851       ;; Remove all old markers.
6852       (let (marker entry new-list)
6853         (while (setq marker (pop gnus-button-marker-list))
6854           (if (or (< marker (point-min)) (>= marker (point-max)))
6855               (push marker new-list)
6856             (goto-char marker)
6857             (when (setq entry (gnus-button-entry))
6858               (put-text-property (match-beginning (nth 1 entry))
6859                                  (match-end (nth 1 entry))
6860                                  'gnus-callback nil))
6861             (set-marker marker nil)))
6862         (setq gnus-button-marker-list new-list))
6863       ;; We skip the headers.
6864       (article-goto-body)
6865       (setq beg (point))
6866       (while (setq entry (pop alist))
6867         (setq regexp (eval (car entry)))
6868         (goto-char beg)
6869         (while (re-search-forward regexp nil t)
6870           (let* ((start (and entry (match-beginning (nth 1 entry))))
6871                  (end (and entry (match-end (nth 1 entry))))
6872                  (from (match-beginning 0)))
6873             (when (and (or (eq t (nth 2 entry))
6874                            (eval (nth 2 entry)))
6875                        (not (gnus-button-in-region-p
6876                              start end 'gnus-callback)))
6877               ;; That optional form returned non-nil, so we add the
6878               ;; button.
6879               (gnus-article-add-button
6880                start end 'gnus-button-push
6881                (car (push (set-marker (make-marker) from)
6882                           gnus-button-marker-list))))))))))
6883
6884 ;; Add buttons to the head of an article.
6885 (defun gnus-article-add-buttons-to-head ()
6886   "Add buttons to the head of the article."
6887   (interactive)
6888   (gnus-with-article-headers
6889     (let (beg end)
6890       (dolist (entry gnus-header-button-alist)
6891         ;; Each alist entry.
6892         (goto-char (point-min))
6893         (while (re-search-forward (car entry) nil t)
6894           ;; Each header matching the entry.
6895           (setq beg (match-beginning 0))
6896           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6897                              (match-beginning 0))
6898                         (point-max)))
6899           (goto-char beg)
6900           (while (re-search-forward (eval (nth 1 entry)) end t)
6901             ;; Each match within a header.
6902             (let* ((entry (cdr entry))
6903                    (start (match-beginning (nth 1 entry)))
6904                    (end (match-end (nth 1 entry)))
6905                    (form (nth 2 entry)))
6906               (goto-char (match-end 0))
6907               (when (eval form)
6908                 (gnus-article-add-button
6909                  start end (nth 3 entry)
6910                  (buffer-substring (match-beginning (nth 4 entry))
6911                                    (match-end (nth 4 entry)))))))
6912           (goto-char end))))))
6913
6914 ;;; External functions:
6915
6916 (defun gnus-article-add-button (from to fun &optional data)
6917   "Create a button between FROM and TO with callback FUN and data DATA."
6918   (when gnus-article-button-face
6919     (gnus-overlay-put (gnus-make-overlay from to)
6920                       'face gnus-article-button-face))
6921   (gnus-add-text-properties
6922    from to
6923    (nconc (and gnus-article-mouse-face
6924                (list gnus-mouse-face-prop gnus-article-mouse-face))
6925           (list 'gnus-callback fun)
6926           (and data (list 'gnus-data data))))
6927   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6928                          :button-keymap gnus-widget-button-keymap))
6929
6930 ;;; Internal functions:
6931
6932 (defun gnus-article-set-globals ()
6933   (with-current-buffer gnus-summary-buffer
6934     (gnus-set-global-variables)))
6935
6936 (defun gnus-signature-toggle (end)
6937   (gnus-with-article-buffer
6938     (let ((inhibit-point-motion-hooks t))
6939       (if (text-property-any end (point-max) 'article-type 'signature)
6940           (progn
6941             (gnus-delete-wash-type 'signature)
6942             (gnus-remove-text-properties-when
6943              'article-type 'signature end (point-max)
6944              (cons 'article-type (cons 'signature
6945                                        gnus-hidden-properties))))
6946         (gnus-add-wash-type 'signature)
6947         (gnus-add-text-properties-when
6948          'article-type nil end (point-max)
6949          (cons 'article-type (cons 'signature
6950                                    gnus-hidden-properties)))))
6951     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6952       (gnus-set-mode-line 'article))))
6953
6954 (defun gnus-button-entry ()
6955   ;; Return the first entry in `gnus-button-alist' matching this place.
6956   (let ((alist gnus-button-alist)
6957         (entry nil))
6958     (while alist
6959       (setq entry (pop alist))
6960       (if (looking-at (eval (car entry)))
6961           (setq alist nil)
6962         (setq entry nil)))
6963     entry))
6964
6965 (defun gnus-button-push (marker)
6966   ;; Push button starting at MARKER.
6967   (save-excursion
6968     (goto-char marker)
6969     (let* ((entry (gnus-button-entry))
6970            (inhibit-point-motion-hooks t)
6971            (fun (nth 3 entry))
6972            (args (mapcar (lambda (group)
6973                            (let ((string (match-string group)))
6974                              (set-text-properties
6975                               0 (length string) nil string)
6976                              string))
6977                          (nthcdr 4 entry))))
6978       (cond
6979        ((fboundp fun)
6980         (apply fun args))
6981        ((and (boundp fun)
6982              (fboundp (symbol-value fun)))
6983         (apply (symbol-value fun) args))
6984        (t
6985         (gnus-message 1 "You must define `%S' to use this button"
6986                       (cons fun args)))))))
6987
6988 (defun gnus-parse-news-url (url)
6989   (let (scheme server port group message-id articles)
6990     (with-temp-buffer
6991       (insert url)
6992       (goto-char (point-min))
6993       (when (looking-at "\\([A-Za-z]+\\):")
6994         (setq scheme (match-string 1))
6995         (goto-char (match-end 0)))
6996       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
6997         (setq server (match-string 1))
6998         (setq port (if (stringp (match-string 3))
6999                        (string-to-number (match-string 3))
7000                      (match-string 3)))
7001         (goto-char (match-end 0)))
7002
7003       (cond
7004        ((looking-at "\\(.*@.*\\)")
7005         (setq message-id (match-string 1)))
7006        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7007         (setq group (match-string 1)
7008               articles (split-string (match-string 2) "-")))
7009        ((looking-at "\\([^/]+\\)/?")
7010         (setq group (match-string 1)))
7011        (t
7012         (error "Unknown news URL syntax"))))
7013     (list scheme server port group message-id articles)))
7014
7015 (defun gnus-button-handle-news (url)
7016   "Fetch a news URL."
7017   (destructuring-bind (scheme server port group message-id articles)
7018       (gnus-parse-news-url url)
7019     (cond
7020      (message-id
7021       (save-excursion
7022         (set-buffer gnus-summary-buffer)
7023         (if server
7024             (let ((gnus-refer-article-method
7025                    (nconc (list (list 'nntp server))
7026                           gnus-refer-article-method))
7027                   (nntp-port-number (or port "nntp")))
7028               (gnus-message 7 "Fetching %s with %s"
7029                             message-id gnus-refer-article-method)
7030               (gnus-summary-refer-article message-id))
7031           (gnus-summary-refer-article message-id))))
7032      (group
7033       (gnus-button-fetch-group url)))))
7034
7035 (defun gnus-button-handle-man (url)
7036   "Fetch a man page."
7037   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7038   (when (eq gnus-button-man-handler 'woman)
7039     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7040   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7041   (funcall gnus-button-man-handler url))
7042
7043 (defun gnus-button-handle-info-url (url)
7044   "Fetch an info URL."
7045   (setq url (mm-subst-char-in-string ?+ ?\  url))
7046   (cond
7047    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7048     (gnus-info-find-node
7049      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7050                      "Gnus")
7051              ")" (gnus-url-unhex-string (match-string 2 url)))))
7052    ((string-match "([^)\"]+)[^\"]+" url)
7053     (setq url
7054           (gnus-replace-in-string
7055            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7056     (gnus-info-find-node url))
7057    (t (error "Can't parse %s" url))))
7058
7059 (defun gnus-button-handle-info-url-gnome (url)
7060   "Fetch GNOME style info URL."
7061   (setq url (mm-subst-char-in-string ?_ ?\  url))
7062   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7063       (gnus-info-find-node
7064        (concat "("
7065                (gnus-url-unhex-string
7066                  (match-string 1 url))
7067                ")"
7068                (or (gnus-url-unhex-string
7069                     (match-string 2 url))
7070                    "Top")))
7071     (error "Can't parse %s" url)))
7072
7073 (defun gnus-button-handle-info-url-kde (url)
7074   "Fetch KDE style info URL."
7075   (gnus-info-find-node (gnus-url-unhex-string url)))
7076
7077 (defun gnus-button-handle-info-keystrokes (url)
7078   "Call `info' when pushing the corresponding URL button."
7079   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7080   (info)
7081   (Info-directory)
7082   (Info-menu url))
7083
7084 (defun gnus-button-openpgp (url)
7085   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7086   (with-temp-buffer
7087     (mm-url-insert-file-contents-external url)
7088     (pgg-snarf-keys-region (point-min) (point-max))
7089     (pgg-display-output-buffer nil nil nil)))
7090
7091 (defun gnus-button-message-id (message-id)
7092   "Fetch MESSAGE-ID."
7093   (with-current-buffer gnus-summary-buffer
7094     (gnus-summary-refer-article message-id)))
7095
7096 (defun gnus-button-fetch-group (address)
7097   "Fetch GROUP specified by ADDRESS."
7098   (if (not (string-match "[:/]" address))
7099       ;; This is just a simple group url.
7100       (gnus-group-read-ephemeral-group address gnus-select-method)
7101     (if (not
7102          (string-match
7103           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7104           address))
7105         (error "Can't parse %s" address)
7106       (gnus-group-read-ephemeral-group
7107        (match-string 4 address)
7108        `(nntp ,(match-string 1 address)
7109               (nntp-address ,(match-string 1 address))
7110               (nntp-port-number ,(if (match-end 3)
7111                                      (match-string 3 address)
7112                                    "nntp")))
7113        nil nil nil
7114        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7115
7116 (defun gnus-url-parse-query-string (query &optional downcase)
7117   (let (retval pairs cur key val)
7118     (setq pairs (split-string query "&"))
7119     (while pairs
7120       (setq cur (car pairs)
7121             pairs (cdr pairs))
7122       (if (not (string-match "=" cur))
7123           nil                           ; Grace
7124         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7125               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7126         (if downcase
7127             (setq key (downcase key)))
7128         (setq cur (assoc key retval))
7129         (if cur
7130             (setcdr cur (cons val (cdr cur)))
7131           (setq retval (cons (list key val) retval)))))
7132     retval))
7133
7134 (defun gnus-url-mailto (url)
7135   ;; Send mail to someone
7136   (when (string-match "mailto:/*\\(.*\\)" url)
7137     (setq url (substring url (match-beginning 1) nil)))
7138   (let (to args subject func)
7139     (setq args (gnus-url-parse-query-string
7140                 (if (string-match "^\\?" url)
7141                     (substring url 1)
7142                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7143                       (concat "to=" (match-string 1 url) "&"
7144                               (match-string 2 url))
7145                     (concat "to=" url)))
7146                 t)
7147           subject (cdr-safe (assoc "subject" args)))
7148     (gnus-msg-mail)
7149     (while args
7150       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7151       (if (fboundp func)
7152           (funcall func)
7153         (message-position-on-field (caar args)))
7154       (insert (gnus-replace-in-string
7155                (mapconcat 'identity (reverse (cdar args)) ", ")
7156                "\r\n" "\n" t))
7157       (setq args (cdr args)))
7158     (if subject
7159         (message-goto-body)
7160       (message-goto-subject))))
7161
7162 (defun gnus-button-embedded-url (address)
7163   "Activate ADDRESS with `browse-url'."
7164   (browse-url (gnus-strip-whitespace address)))
7165
7166 ;;; Next/prev buttons in the article buffer.
7167
7168 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7169 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7170
7171 (defvar gnus-prev-page-map
7172   (let ((map (make-sparse-keymap)))
7173     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7174     (define-key map "\r" 'gnus-button-prev-page)
7175     map))
7176
7177 (defvar gnus-next-page-map
7178   (let ((map (make-sparse-keymap)))
7179     (unless (>= emacs-major-version 21)
7180       ;; XEmacs doesn't care.
7181       (set-keymap-parent map gnus-article-mode-map))
7182     (define-key map gnus-mouse-2 'gnus-button-next-page)
7183     (define-key map "\r" 'gnus-button-next-page)
7184     map))
7185
7186 (defun gnus-insert-prev-page-button ()
7187   (let ((b (point))
7188         (inhibit-read-only t))
7189     (gnus-eval-format
7190      gnus-prev-page-line-format nil
7191      `(keymap ,gnus-prev-page-map
7192          gnus-prev t
7193          gnus-callback gnus-article-button-prev-page
7194          article-type annotation))
7195     (widget-convert-button
7196      'link b (if (bolp)
7197                  ;; Exclude a newline.
7198                  (1- (point))
7199                (point))
7200      :action 'gnus-button-prev-page
7201      :button-keymap gnus-prev-page-map)))
7202
7203 (defun gnus-button-next-page (&optional args more-args)
7204   "Go to the next page."
7205   (interactive)
7206   (let ((win (selected-window)))
7207     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7208     (gnus-article-next-page)
7209     (select-window win)))
7210
7211 (defun gnus-button-prev-page (&optional args more-args)
7212   "Go to the prev page."
7213   (interactive)
7214   (let ((win (selected-window)))
7215     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7216     (gnus-article-prev-page)
7217     (select-window win)))
7218
7219 (defun gnus-insert-next-page-button ()
7220   (let ((b (point))
7221         (inhibit-read-only t))
7222     (gnus-eval-format gnus-next-page-line-format nil
7223                       `(keymap ,gnus-next-page-map
7224                           gnus-next t
7225                           gnus-callback gnus-article-button-next-page
7226                           article-type annotation))
7227     (widget-convert-button
7228      'link b (if (bolp)
7229                  ;; Exclude a newline.
7230                  (1- (point))
7231                (point))
7232      :action 'gnus-button-next-page
7233      :button-keymap gnus-next-page-map)))
7234
7235 (defun gnus-article-button-next-page (arg)
7236   "Go to the next page."
7237   (interactive "P")
7238   (let ((win (selected-window)))
7239     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7240     (gnus-article-next-page)
7241     (select-window win)))
7242
7243 (defun gnus-article-button-prev-page (arg)
7244   "Go to the prev page."
7245   (interactive "P")
7246   (let ((win (selected-window)))
7247     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7248     (gnus-article-prev-page)
7249     (select-window win)))
7250
7251 (defvar gnus-decode-header-methods
7252   '(mail-decode-encoded-word-region)
7253   "List of methods used to decode headers.
7254
7255 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7256 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
7257 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
7258 whose names match REGEXP.
7259
7260 For example:
7261 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7262  mail-decode-encoded-word-region
7263  (\"chinese\" . rfc1843-decode-region))
7264 ")
7265
7266 (defvar gnus-decode-header-methods-cache nil)
7267
7268 (defun gnus-multi-decode-header (start end)
7269   "Apply the functions from `gnus-encoded-word-methods' that match."
7270   (unless (and gnus-decode-header-methods-cache
7271                (eq gnus-newsgroup-name
7272                    (car gnus-decode-header-methods-cache)))
7273     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7274     (mapcar (lambda (x)
7275               (if (symbolp x)
7276                   (nconc gnus-decode-header-methods-cache (list x))
7277                 (if (and gnus-newsgroup-name
7278                          (string-match (car x) gnus-newsgroup-name))
7279                     (nconc gnus-decode-header-methods-cache
7280                            (list (cdr x))))))
7281           gnus-decode-header-methods))
7282   (let ((xlist gnus-decode-header-methods-cache))
7283     (pop xlist)
7284     (save-restriction
7285       (narrow-to-region start end)
7286       (while xlist
7287         (funcall (pop xlist) (point-min) (point-max))))))
7288
7289 ;;;
7290 ;;; Treatment top-level handling.
7291 ;;;
7292
7293 (defun gnus-treat-article (condition &optional part-number total-parts type)
7294   (let ((length (- (point-max) (point-min)))
7295         (alist gnus-treatment-function-alist)
7296         (article-goto-body-goes-to-point-min-p t)
7297         (treated-type
7298          (or (not type)
7299              (catch 'found
7300                (let ((list gnus-article-treat-types))
7301                  (while list
7302                    (when (string-match (pop list) type)
7303                      (throw 'found t)))))))
7304         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7305         val elem)
7306     (gnus-run-hooks 'gnus-part-display-hook)
7307     (dolist (elem alist)
7308       (setq val
7309             (save-excursion
7310               (when (gnus-buffer-live-p gnus-summary-buffer)
7311                 (set-buffer gnus-summary-buffer))
7312               (symbol-value (car elem))))
7313       (when (and (or (consp val)
7314                      treated-type)
7315                  (gnus-treat-predicate val)
7316                  (or (not (get (car elem) 'highlight))
7317                      highlightp))
7318         (save-restriction
7319           (funcall (cadr elem)))))))
7320
7321 ;; Dynamic variables.
7322 (eval-when-compile
7323   (defvar part-number)
7324   (defvar total-parts)
7325   (defvar type)
7326   (defvar condition)
7327   (defvar length))
7328
7329 (defun gnus-treat-predicate (val)
7330   (cond
7331    ((null val)
7332     nil)
7333    (condition
7334     (eq condition val))
7335    ((and (listp val)
7336          (stringp (car val)))
7337     (apply 'gnus-or (mapcar `(lambda (s)
7338                                (string-match s ,(or gnus-newsgroup-name "")))
7339                             val)))
7340    ((listp val)
7341     (let ((pred (pop val)))
7342       (cond
7343        ((eq pred 'or)
7344         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7345        ((eq pred 'and)
7346         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7347        ((eq pred 'not)
7348         (not (gnus-treat-predicate (car val))))
7349        ((eq pred 'typep)
7350         (equal (car val) type))
7351        (t
7352         (error "%S is not a valid predicate" pred)))))
7353    ((eq val t)
7354     t)
7355    ((eq val 'head)
7356     nil)
7357    ((eq val 'first)
7358     (eq part-number 1))
7359    ((eq val 'last)
7360     (eq part-number total-parts))
7361    ((numberp val)
7362     (< length val))
7363    (t
7364     (error "%S is not a valid value" val))))
7365
7366 (defun gnus-article-encrypt-body (protocol &optional n)
7367   "Encrypt the article body."
7368   (interactive
7369    (list
7370     (or gnus-article-encrypt-protocol
7371         (completing-read "Encrypt protocol: "
7372                          gnus-article-encrypt-protocol-alist
7373                          nil t))
7374     current-prefix-arg))
7375   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7376     (unless func
7377       (error "Can't find the encrypt protocol %s" protocol))
7378     (if (member gnus-newsgroup-name '("nndraft:delayed"
7379                                       "nndraft:drafts"
7380                                       "nndraft:queue"))
7381         (error "Can't encrypt the article in group %s"
7382                gnus-newsgroup-name))
7383     (gnus-summary-iterate n
7384       (save-excursion
7385         (set-buffer gnus-summary-buffer)
7386         (let ((mail-parse-charset gnus-newsgroup-charset)
7387               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7388               (summary-buffer gnus-summary-buffer)
7389               references point)
7390           (gnus-set-global-variables)
7391           (when (gnus-group-read-only-p)
7392             (error "The current newsgroup does not support article encrypt"))
7393           (gnus-summary-show-article t)
7394           (setq references
7395               (or (mail-header-references gnus-current-headers) ""))
7396           (set-buffer gnus-article-buffer)
7397           (let* ((inhibit-read-only t)
7398                  (headers
7399                   (mapcar (lambda (field)
7400                             (and (save-restriction
7401                                    (message-narrow-to-head)
7402                                    (goto-char (point-min))
7403                                    (search-forward field nil t))
7404                                  (prog2
7405                                      (message-narrow-to-field)
7406                                      (buffer-string)
7407                                    (delete-region (point-min) (point-max))
7408                                    (widen))))
7409                           '("Content-Type:" "Content-Transfer-Encoding:"
7410                             "Content-Disposition:"))))
7411             (message-narrow-to-head)
7412             (message-remove-header "MIME-Version")
7413             (goto-char (point-max))
7414             (setq point (point))
7415             (insert (apply 'concat headers))
7416             (widen)
7417             (narrow-to-region point (point-max))
7418             (let ((message-options message-options))
7419               (message-options-set 'message-sender user-mail-address)
7420               (message-options-set 'message-recipients user-mail-address)
7421               (message-options-set 'message-sign-encrypt 'not)
7422               (funcall func))
7423             (goto-char (point-min))
7424             (insert "MIME-Version: 1.0\n")
7425             (widen)
7426             (gnus-summary-edit-article-done
7427              references nil summary-buffer t))
7428           (when gnus-keep-backlog
7429             (gnus-backlog-remove-article
7430              (car gnus-article-current) (cdr gnus-article-current)))
7431           (save-excursion
7432             (when (get-buffer gnus-original-article-buffer)
7433               (set-buffer gnus-original-article-buffer)
7434               (setq gnus-original-article nil)))
7435           (when gnus-use-cache
7436             (gnus-cache-update-article
7437              (car gnus-article-current) (cdr gnus-article-current))))))))
7438
7439 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7440   "The following specs can be used:
7441 %t  The security MIME type
7442 %i  Additional info
7443 %d  Details
7444 %D  Details if button is pressed")
7445
7446 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7447   "The following specs can be used:
7448 %t  The security MIME type
7449 %i  Additional info
7450 %d  Details
7451 %D  Details if button is pressed")
7452
7453 (defvar gnus-mime-security-button-line-format-alist
7454   '((?t gnus-tmp-type ?s)
7455     (?i gnus-tmp-info ?s)
7456     (?d gnus-tmp-details ?s)
7457     (?D gnus-tmp-pressed-details ?s)))
7458
7459 (defvar gnus-mime-security-button-commands
7460   '((gnus-article-press-button "\r" "Show Detail")
7461     (undefined "v")
7462     (undefined "t")
7463     (undefined "C")
7464     (gnus-mime-security-save-part "o" "Save...")
7465     (undefined "\C-o")
7466     (undefined "r")
7467     (undefined "d")
7468     (undefined "c")
7469     (undefined "i")
7470     (undefined "E")
7471     (undefined "e")
7472     (undefined "p")
7473     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
7474     (undefined ".")))
7475
7476 (defvar gnus-mime-security-button-map
7477   (let ((map (make-sparse-keymap)))
7478     (define-key map gnus-mouse-2 'gnus-article-push-button)
7479     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
7480     (dolist (c gnus-mime-security-button-commands)
7481       (define-key map (cadr c) (car c)))
7482     map))
7483
7484 (easy-menu-define
7485   gnus-mime-security-button-menu gnus-mime-security-button-map
7486   "Security button menu."
7487   `("Security Part"
7488     ,@(delq nil
7489             (mapcar (lambda (c)
7490                       (unless (eq (car c) 'undefined)
7491                         (vector (caddr c) (car c) :enable t)))
7492                     gnus-mime-security-button-commands))))
7493
7494 (defun gnus-mime-security-button-menu (event prefix)
7495   "Construct a context-sensitive menu of security commands."
7496   (interactive "e\nP")
7497   (save-window-excursion
7498     (let ((pos (event-start event)))
7499       (select-window (posn-window pos))
7500       (goto-char (posn-point pos))
7501       (gnus-article-check-buffer)
7502       (popup-menu gnus-mime-security-button-menu nil prefix))))
7503
7504 (defvar gnus-mime-security-details-buffer nil)
7505
7506 (defvar gnus-mime-security-button-pressed nil)
7507
7508 (defvar gnus-mime-security-show-details-inline t
7509   "If non-nil, show details in the article buffer.")
7510
7511 (defun gnus-mime-security-verify-or-decrypt (handle)
7512   (mm-remove-parts (cdr handle))
7513   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7514         point (inhibit-read-only t))
7515     (if region
7516         (goto-char (car region)))
7517     (save-restriction
7518       (narrow-to-region (point) (point))
7519       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7520         (let* ((mm-verify-option 'known)
7521                (mm-decrypt-option 'known)
7522                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7523           (unless (eq nparts (cdr handle))
7524             (mm-destroy-parts (cdr handle))
7525             (setcdr handle nparts))))
7526       (setq point (point))
7527       (gnus-mime-display-security handle)
7528       (goto-char (point-max)))
7529     (when region
7530       (delete-region (point) (cdr region))
7531       (set-marker (car region) nil)
7532       (set-marker (cdr region) nil))
7533     (goto-char point)))
7534
7535 (defun gnus-mime-security-show-details (handle)
7536   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7537     (if (not details)
7538         (gnus-message 5 "No details.")
7539       (if gnus-mime-security-show-details-inline
7540           (let ((gnus-mime-security-button-pressed
7541                  (not (get-text-property (point) 'gnus-mime-details)))
7542                 (gnus-mime-security-button-line-format
7543                  (get-text-property (point) 'gnus-line-format))
7544                 (inhibit-read-only t))
7545             (forward-char -1)
7546             (while (eq (get-text-property (point) 'gnus-line-format)
7547                        gnus-mime-security-button-line-format)
7548               (forward-char -1))
7549             (forward-char)
7550             (save-restriction
7551               (narrow-to-region (point) (point))
7552               (gnus-insert-mime-security-button handle))
7553             (delete-region (point)
7554                            (or (text-property-not-all
7555                                 (point) (point-max)
7556                                 'gnus-line-format
7557                                 gnus-mime-security-button-line-format)
7558                                (point-max))))
7559         ;; Not inlined.
7560         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7561             (with-current-buffer gnus-mime-security-details-buffer
7562               (erase-buffer)
7563               t)
7564           (setq gnus-mime-security-details-buffer
7565                 (gnus-get-buffer-create "*MIME Security Details*")))
7566         (with-current-buffer gnus-mime-security-details-buffer
7567           (insert details)
7568           (goto-char (point-min)))
7569         (pop-to-buffer gnus-mime-security-details-buffer)))))
7570
7571 (defun gnus-mime-security-press-button (handle)
7572   (save-excursion
7573     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7574         (gnus-mime-security-show-details handle)
7575       (gnus-mime-security-verify-or-decrypt handle))))
7576
7577 (defun gnus-insert-mime-security-button (handle &optional displayed)
7578   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7579          (gnus-tmp-type
7580           (concat
7581            (or (nth 2 (assoc protocol mm-verify-function-alist))
7582                (nth 2 (assoc protocol mm-decrypt-function-alist))
7583                "Unknown")
7584            (if (equal (car handle) "multipart/signed")
7585                " Signed" " Encrypted")
7586            " Part"))
7587          (gnus-tmp-info
7588           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7589               "Undecided"))
7590          (gnus-tmp-details
7591           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7592          gnus-tmp-pressed-details
7593          b e)
7594     (setq gnus-tmp-details
7595           (if gnus-tmp-details
7596               (concat "\n" gnus-tmp-details)
7597             ""))
7598     (setq gnus-tmp-pressed-details
7599           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7600     (unless (bolp)
7601       (insert "\n"))
7602     (setq b (point))
7603     (gnus-eval-format
7604      gnus-mime-security-button-line-format
7605      gnus-mime-security-button-line-format-alist
7606      `(keymap ,gnus-mime-security-button-map
7607          gnus-callback gnus-mime-security-press-button
7608          gnus-line-format ,gnus-mime-security-button-line-format
7609          gnus-mime-details ,gnus-mime-security-button-pressed
7610          article-type annotation
7611          gnus-data ,handle))
7612     (setq e (if (bolp)
7613                 ;; Exclude a newline.
7614                 (1- (point))
7615               (point)))
7616     (widget-convert-button
7617      'link b e
7618      :mime-handle handle
7619      :action 'gnus-widget-press-button
7620      :button-keymap gnus-mime-security-button-map
7621      :help-echo
7622      (lambda (widget/window &optional overlay pos)
7623        ;; Needed to properly clear the message due to a bug in
7624        ;; wid-edit (XEmacs only).
7625        (when (boundp 'help-echo-owns-message)
7626          (setq help-echo-owns-message t))
7627        (format
7628         "%S: show detail; %S: more options"
7629         (aref gnus-mouse-2 0)
7630         (aref gnus-down-mouse-3 0))))))
7631
7632 (defun gnus-mime-display-security (handle)
7633   (save-restriction
7634     (narrow-to-region (point) (point))
7635     (unless (gnus-unbuttonized-mime-type-p (car handle))
7636       (gnus-insert-mime-security-button handle))
7637     (gnus-mime-display-part (cadr handle))
7638     (unless (bolp)
7639       (insert "\n"))
7640     (unless (gnus-unbuttonized-mime-type-p (car handle))
7641       (let ((gnus-mime-security-button-line-format
7642              gnus-mime-security-button-end-line-format))
7643         (gnus-insert-mime-security-button handle)))
7644     (mm-set-handle-multipart-parameter
7645      handle 'gnus-region
7646      (cons (set-marker (make-marker) (point-min))
7647            (set-marker (make-marker) (point-max))))))
7648
7649 (defun gnus-mime-security-run-function (function)
7650   "Run FUNCTION with the security part under point."
7651   (gnus-article-check-buffer)
7652   (let ((data (get-text-property (point) 'gnus-data))
7653         buffer handle)
7654     (when (and (stringp (car-safe data))
7655                (setq buffer (mm-handle-multipart-original-buffer data))
7656                (setq handle (cadr data)))
7657       (if (bufferp (mm-handle-buffer handle))
7658           (progn
7659             (setq handle (cons buffer (copy-sequence (cdr handle))))
7660             (mm-handle-set-undisplayer handle nil))
7661         (setq handle (mm-make-handle
7662                       buffer
7663                       (mm-handle-multipart-ctl-parameter handle 'protocol)
7664                       nil nil nil nil nil nil)))
7665       (funcall function handle))))
7666
7667 (defun gnus-mime-security-save-part ()
7668   "Save the security part under point."
7669   (interactive)
7670   (gnus-mime-security-run-function 'mm-save-part))
7671
7672 (defun gnus-mime-security-pipe-part ()
7673   "Pipe the security part under point to a process."
7674   (interactive)
7675   (gnus-mime-security-run-function 'mm-pipe-part))
7676
7677 (gnus-ems-redefine)
7678
7679 (provide 'gnus-art)
7680
7681 (run-hooks 'gnus-art-load-hook)
7682
7683 ;;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
7684 ;;; gnus-art.el ends here