gnus-art.el (gnus-article-browse-html-parts): Replace LWSPs with ` 's in header
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-util)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48 (require 'mouse)
49
50 (autoload 'gnus-msg-mail "gnus-msg" nil t)
51 (autoload 'gnus-button-mailto "gnus-msg")
52 (autoload 'gnus-button-reply "gnus-msg" nil t)
53 (autoload 'parse-time-string "parse-time" nil nil)
54 (autoload 'ansi-color-apply-on-region "ansi-color")
55 (autoload 'mm-url-insert-file-contents-external "mm-url")
56 (autoload 'mm-extern-cache-contents "mm-extern")
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      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
162      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
163      "Envelope-Sender" "Envelope-Recipients"))
164   "*All headers that start with this regexp will be hidden.
165 This variable can also be a list of regexps of headers to be ignored.
166 If `gnus-visible-headers' is non-nil, this variable will be ignored."
167   :type '(choice regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^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:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo \
273 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
274 ; uncompface; } | icontopbm | ee -")
275     (if (gnus-image-type-available-p 'pbm)
276         'gnus-display-x-face-in-from
277       "{ echo \
278 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
279 ; uncompface; } | icontopbm | display -"))
280   "*String or function to be executed to display an X-Face header.
281 If it is a string, the command will be executed in a sub-shell
282 asynchronously.  The compressed face will be piped to this command."
283   :type `(choice string
284                  (function-item gnus-display-x-face-in-from)
285                  function)
286   :version "21.1"
287   :group 'gnus-picon
288   :group 'gnus-article-washing)
289
290 (defcustom gnus-article-x-face-too-ugly nil
291   "Regexp matching posters whose face shouldn't be shown automatically."
292   :type '(choice regexp (const nil))
293   :group 'gnus-article-washing)
294
295 (defcustom gnus-article-banner-alist nil
296   "Banner alist for stripping.
297 For example,
298      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
299   :version "21.1"
300   :type '(repeat (cons symbol regexp))
301   :group 'gnus-article-washing)
302
303 (gnus-define-group-parameter
304  banner
305  :variable-document
306  "Alist of regexps (to match group names) and banner."
307  :variable-group gnus-article-washing
308  :parameter-type
309  '(choice :tag "Banner"
310           :value nil
311           (const :tag "Remove signature" signature)
312           (symbol :tag "Item in `gnus-article-banner-alist'" none)
313           regexp
314           (const :tag "None" nil))
315  :parameter-document
316  "If non-nil, specify how to remove `banners' from articles.
317
318 Symbol `signature' means to remove signatures delimited by
319 `gnus-signature-separator'.  Any other symbol is used to look up a
320 regular expression to match the banner in `gnus-article-banner-alist'.
321 A string is used as a regular expression to match the banner
322 directly.")
323
324 (defcustom gnus-article-address-banner-alist nil
325   "Alist of mail addresses and banners.
326 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
327 to match a mail address in the From: header, BANNER is one of a symbol
328 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
329 If ADDRESS matches author's mail address, it will remove things like
330 advertisements.  For example:
331
332 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
333 "
334   :type '(repeat
335           (cons
336            (regexp :tag "Address")
337            (choice :tag "Banner" :value nil
338                    (const :tag "Remove signature" signature)
339                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
340                    regexp
341                    (const :tag "None" nil))))
342   :version "22.1"
343   :group 'gnus-article-washing)
344
345 (defmacro gnus-emphasis-custom-with-format (&rest body)
346   `(let ((format "\
347 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
348 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
349      ,@body))
350
351 (defun gnus-emphasis-custom-value-to-external (value)
352   (gnus-emphasis-custom-with-format
353    (if (consp (car value))
354        (list (format format (car (car value)) (cdr (car value)))
355              2
356              (if (nth 1 value) 2 3)
357              (nth 2 value))
358      value)))
359
360 (defun gnus-emphasis-custom-value-to-internal (value)
361   (gnus-emphasis-custom-with-format
362    (let ((regexp (concat "\\`"
363                          (format (regexp-quote format)
364                                  "\\([^()]+\\)" "\\([^()]+\\)")
365                          "\\'"))
366          pattern)
367      (if (string-match regexp (setq pattern (car value)))
368          (list (cons (match-string 1 pattern) (match-string 2 pattern))
369                (= (nth 2 value) 2)
370                (nth 3 value))
371        value))))
372
373 (defcustom gnus-emphasis-alist
374   (let ((types
375          '(("\\*" "\\*" bold nil 2)
376            ("_" "_" underline)
377            ("/" "/" italic)
378            ("_/" "/_" underline-italic)
379            ("_\\*" "\\*_" underline-bold)
380            ("\\*/" "/\\*" bold-italic)
381            ("_\\*/" "/\\*_" underline-bold-italic))))
382     (nconc
383      (gnus-emphasis-custom-with-format
384       (mapcar (lambda (spec)
385                 (list (format format (car spec) (cadr spec))
386                       (or (nth 3 spec) 2)
387                       (or (nth 4 spec) 3)
388                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
389               types))
390      '(;; I've never seen anyone use this strikethru convention whereas I've
391        ;; several times seen it triggered by normal text.  --Stef
392        ;; Miles suggests that this form is sometimes used but for italics,
393        ;; so maybe we should map it to `italic'.
394        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
395        ;; 2 3 gnus-emphasis-strikethru)
396        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
397         2 3 gnus-emphasis-underline))))
398   "*Alist that says how to fontify certain phrases.
399 Each item looks like this:
400
401   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
402
403 The first element is a regular expression to be matched.  The second
404 is a number that says what regular expression grouping used to find
405 the entire emphasized word.  The third is a number that says what
406 regexp grouping should be displayed and highlighted.  The fourth
407 is the face used for highlighting."
408   :type
409   '(repeat
410     (menu-choice
411      :format "%[Customizing Style%]\n%v"
412      :indent 2
413      (group :tag "Default"
414             :value ("" 0 0 default)
415             :value-create
416             (lambda (widget)
417               (let ((value (widget-get
418                             (cadr (widget-get (widget-get widget :parent)
419                                               :args))
420                             :value)))
421                 (if (not (eq (nth 2 value) 'default))
422                     (widget-put
423                      widget
424                      :value
425                      (gnus-emphasis-custom-value-to-external value))))
426               (widget-group-value-create widget))
427             regexp
428             (integer :format "Match group: %v")
429             (integer :format "Emphasize group: %v")
430             face)
431      (group :tag "Simple"
432             :value (("_" . "_") nil default)
433             (cons :format "%v"
434                   (regexp :format "Start regexp: %v")
435                   (regexp :format "End regexp: %v"))
436             (boolean :format "Show start and end patterns: %[%v%]\n"
437                      :on " On " :off " Off ")
438             face)))
439   :get (lambda (symbol)
440          (mapcar 'gnus-emphasis-custom-value-to-internal
441                  (default-value symbol)))
442   :set (lambda (symbol value)
443          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
444                                      value)))
445   :group 'gnus-article-emphasis)
446
447 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
448   "A regexp to describe whitespace which should not be emphasized.
449 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
450 The former avoids underlining of leading and trailing whitespace,
451 and the latter avoids underlining any whitespace at all."
452   :version "21.1"
453   :group 'gnus-article-emphasis
454   :type 'regexp)
455
456 (defface gnus-emphasis-bold '((t (:bold t)))
457   "Face used for displaying strong emphasized text (*word*)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-italic '((t (:italic t)))
461   "Face used for displaying italic emphasized text (/word/)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline '((t (:underline t)))
465   "Face used for displaying underlined emphasized text (_word_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
469   "Face used for displaying underlined bold emphasized text (_*word*_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
473   "Face used for displaying underlined italic emphasized text (_/word/_)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
477   "Face used for displaying bold italic emphasized text (/*word*/)."
478   :group 'gnus-article-emphasis)
479
480 (defface gnus-emphasis-underline-bold-italic
481   '((t (:bold t :italic t :underline t)))
482   "Face used for displaying underlined bold italic emphasized text.
483 Example: (_/*word*/_)."
484   :group 'gnus-article-emphasis)
485
486 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
487                                       '((t (:strikethru t)))
488                                     '((t (:strike-through t))))
489   "Face used for displaying strike-through text (-word-)."
490   :group 'gnus-article-emphasis)
491
492 (defface gnus-emphasis-highlight-words
493   '((t (:background "black" :foreground "yellow")))
494   "Face used for displaying highlighted words."
495   :group 'gnus-article-emphasis)
496
497 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
498   "Format for display of Date headers in article bodies.
499 See `format-time-string' for the possible values.
500
501 The variable can also be function, which should return a complete Date
502 header.  The function is called with one argument, the time, which can
503 be fed to `format-time-string'."
504   :type '(choice string function)
505   :link '(custom-manual "(gnus)Article Date")
506   :group 'gnus-article-washing)
507
508 (defcustom gnus-save-all-headers t
509   "*If non-nil, don't remove any headers before saving.
510 This will be overridden by the `:headers' property that the symbol of
511 the saver function, which is specified by `gnus-default-article-saver',
512 might have."
513   :group 'gnus-article-saving
514   :type 'boolean)
515
516 (defcustom gnus-prompt-before-saving 'always
517   "*This variable says how much prompting is to be done when saving articles.
518 If it is nil, no prompting will be done, and the articles will be
519 saved to the default files.  If this variable is `always', each and
520 every article that is saved will be preceded by a prompt, even when
521 saving large batches of articles.  If this variable is neither nil not
522 `always', there the user will be prompted once for a file name for
523 each invocation of the saving commands."
524   :group 'gnus-article-saving
525   :type '(choice (item always)
526                  (item :tag "never" nil)
527                  (sexp :tag "once" :format "%t\n" :value t)))
528
529 (defcustom gnus-saved-headers gnus-visible-headers
530   "Headers to keep if `gnus-save-all-headers' is nil.
531 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
532 If that variable is nil, however, all headers that match this regexp
533 will be kept while the rest will be deleted before saving.  This and
534 `gnus-save-all-headers' will be overridden by the `:headers' property
535 that the symbol of the saver function, which is specified by
536 `gnus-default-article-saver', might have."
537   :group 'gnus-article-saving
538   :type 'regexp)
539
540 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
541 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
542   "A function to save articles in your favorite format.
543 The function will be called by way of the `gnus-summary-save-article'
544 command, and friends such as `gnus-summary-save-article-rmail'.
545
546 Gnus provides the following functions:
547
548 * gnus-summary-save-in-rmail (Rmail format)
549 * gnus-summary-save-in-mail (Unix mail format)
550 * gnus-summary-save-in-folder (MH folder)
551 * gnus-summary-save-in-file (article format)
552 * gnus-summary-save-body-in-file (article body)
553 * gnus-summary-save-in-vm (use VM's folder format)
554 * gnus-summary-write-to-file (article format -- overwrite)
555 * gnus-summary-write-body-to-file (article body -- overwrite)
556 * gnus-summary-save-in-pipe (article format)
557
558 The symbol of each function may have the following properties:
559
560 * :decode
561 The value non-nil means save decoded articles.  This is meaningful
562 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
563 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
564 `gnus-summary-save-in-pipe'.
565
566 * :function
567 The value specifies an alternative function which appends, not
568 overwrites, articles to a file.  This implies that when saving many
569 articles at a time, `gnus-prompt-before-saving' is bound to t and all
570 articles are saved in a single file.  This is meaningful only with
571 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
572
573 * :headers
574 The value specifies the symbol of a variable of which the value
575 specifies headers to be saved.  If it is omitted,
576 `gnus-save-all-headers' and `gnus-saved-headers' control what
577 headers should be saved."
578   :group 'gnus-article-saving
579   :type '(radio (function-item gnus-summary-save-in-rmail)
580                 (function-item gnus-summary-save-in-mail)
581                 (function-item gnus-summary-save-in-folder)
582                 (function-item gnus-summary-save-in-file)
583                 (function-item gnus-summary-save-body-in-file)
584                 (function-item gnus-summary-save-in-vm)
585                 (function-item gnus-summary-write-to-file)
586                 (function-item gnus-summary-write-body-to-file)
587                 (function-item gnus-summary-save-in-pipe)
588                 (function)))
589
590 (defcustom gnus-article-save-coding-system
591   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
592       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
593       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
594       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
595   "Coding system used to save decoded articles to a file.
596
597 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
598 which can safely encode any characters in text.  This is used by the
599 commands including:
600
601 * gnus-summary-save-article-file
602 * gnus-summary-save-article-body-file
603 * gnus-summary-write-article-file
604 * gnus-summary-write-article-body-file
605
606 and the functions to which you may set `gnus-default-article-saver':
607
608 * gnus-summary-save-in-file
609 * gnus-summary-save-body-in-file
610 * gnus-summary-write-to-file
611 * gnus-summary-write-body-to-file
612
613 Those commands and functions save just text displayed in the article
614 buffer to a file if the value of this variable is non-nil.  Note that
615 buttonized MIME parts will be lost in a saved file in that case.
616 Otherwise, raw articles will be saved."
617   :group 'gnus-article-saving
618   :type `(choice
619           :format "%{%t%}:\n %[Value Menu%] %v"
620           (const :tag "Save raw articles" nil)
621           ,@(delq nil
622                   (mapcar
623                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
624                    '((const :tag "UTF-8" utf-8)
625                      (const :tag "iso-2022-7bit" iso-2022-7bit)
626                      (const :tag "Emacs internal" emacs-mule)
627                      (const :tag "escape-quoted" escape-quoted))))
628           (symbol :tag "Coding system")))
629
630 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
631   "A function generating a file name to save articles in Rmail format.
632 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
633   :group 'gnus-article-saving
634   :type 'function)
635
636 (defcustom gnus-mail-save-name 'gnus-plain-save-name
637   "A function generating a file name to save articles in Unix mail format.
638 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
639   :group 'gnus-article-saving
640   :type 'function)
641
642 (defcustom gnus-folder-save-name 'gnus-folder-save-name
643   "A function generating a file name to save articles in MH folder.
644 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
645   :group 'gnus-article-saving
646   :type 'function)
647
648 (defcustom gnus-file-save-name 'gnus-numeric-save-name
649   "A function generating a file name to save articles in article format.
650 The function is called with NEWSGROUP, HEADERS, and optional
651 LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-split-methods
656   '((gnus-article-archive-name)
657     (gnus-article-nndoc-name))
658   "*Variable used to suggest where articles are to be saved.
659 For instance, if you would like to save articles related to Gnus in
660 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
661 you could set this variable to something like:
662
663  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
664    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
665
666 This variable is an alist where the key is the match and the
667 value is a list of possible files to save in if the match is
668 non-nil.
669
670 If the match is a string, it is used as a regexp match on the
671 article.  If the match is a symbol, that symbol will be funcalled
672 from the buffer of the article to be saved with the newsgroup as the
673 parameter.  If it is a list, it will be evalled in the same buffer.
674
675 If this form or function returns a string, this string will be used as a
676 possible file name; and if it returns a non-nil list, that list will be
677 used as possible file names."
678   :group 'gnus-article-saving
679   :type '(repeat (choice (list :value (fun) function)
680                          (cons :value ("" "") regexp (repeat string))
681                          (sexp :value nil))))
682
683 (defcustom gnus-page-delimiter "^\^L"
684   "*Regexp describing what to use as article page delimiters.
685 The default value is \"^\^L\", which is a form linefeed at the
686 beginning of a line."
687   :type 'regexp
688   :group 'gnus-article-various)
689
690 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
691   "*The format specification for the article mode line.
692 See `gnus-summary-mode-line-format' for a closer description.
693
694 The following additional specs are available:
695
696 %w  The article washing status.
697 %m  The number of MIME parts in the article."
698   :version "24.1"
699   :type 'string
700   :group 'gnus-article-various)
701
702 (defcustom gnus-article-mode-hook nil
703   "*A hook for Gnus article mode."
704   :type 'hook
705   :group 'gnus-article-various)
706
707 (when (featurep 'xemacs)
708   ;; Extracted from gnus-xmas-define in order to preserve user settings
709   (when (fboundp 'turn-off-scroll-in-place)
710     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
711   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
712   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
713
714 (defcustom gnus-article-menu-hook nil
715   "*Hook run after the creation of the article mode menu."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-article-prepare-hook nil
720   "*A hook called after an article has been prepared in the article buffer."
721   :type 'hook
722   :group 'gnus-article-various)
723
724 (defcustom gnus-copy-article-ignored-headers nil
725   "List of headers to be removed when copying an article.
726 Each element is a regular expression."
727   :version "23.1" ;; No Gnus
728   :type '(repeat regexp)
729   :group 'gnus-article-various)
730
731 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
732                         "Gnus 5.10 (Emacs 22.1)")
733
734 (defface gnus-button
735   '((t (:weight bold)))
736   "Face used for highlighting a button in the article buffer."
737   :group 'gnus-article-buttons)
738
739 (defcustom gnus-article-button-face 'gnus-button
740   "Face used for highlighting buttons in the article buffer.
741
742 An article button is a piece of text that you can activate by pressing
743 `RET' or `mouse-2' above it."
744   :type 'face
745   :group 'gnus-article-buttons)
746
747 (defcustom gnus-article-mouse-face 'highlight
748   "Face used for mouse highlighting in the article buffer.
749
750 Article buttons will be displayed in this face when the cursor is
751 above them."
752   :type 'face
753   :group 'gnus-article-buttons)
754
755 (defcustom gnus-signature-face 'gnus-signature
756   "Face used for highlighting a signature in the article buffer.
757 Obsolete; use the face `gnus-signature' for customizations instead."
758   :type 'face
759   :group 'gnus-article-highlight
760   :group 'gnus-article-signature)
761
762 (defface gnus-signature
763   '((t
764      (:italic t)))
765   "Face used for highlighting a signature in the article buffer."
766   :group 'gnus-article-highlight
767   :group 'gnus-article-signature)
768 ;; backward-compatibility alias
769 (put 'gnus-signature-face 'face-alias 'gnus-signature)
770 (put 'gnus-signature-face 'obsolete-face "22.1")
771
772 (defface gnus-header-from
773   '((((class color)
774       (background dark))
775      (:foreground "PaleGreen1"))
776     (((class color)
777       (background light))
778      (:foreground "red3"))
779     (t
780      (:italic t)))
781   "Face used for displaying from headers."
782   :group 'gnus-article-headers
783   :group 'gnus-article-highlight)
784 ;; backward-compatibility alias
785 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
786 (put 'gnus-header-from-face 'obsolete-face "22.1")
787
788 (defface gnus-header-subject
789   '((((class color)
790       (background dark))
791      (:foreground "SeaGreen1"))
792     (((class color)
793       (background light))
794      (:foreground "red4"))
795     (t
796      (:bold t :italic t)))
797   "Face used for displaying subject headers."
798   :group 'gnus-article-headers
799   :group 'gnus-article-highlight)
800 ;; backward-compatibility alias
801 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
802 (put 'gnus-header-subject-face 'obsolete-face "22.1")
803
804 (defface gnus-header-newsgroups
805   '((((class color)
806       (background dark))
807      (:foreground "yellow" :italic t))
808     (((class color)
809       (background light))
810      (:foreground "MidnightBlue" :italic t))
811     (t
812      (:italic t)))
813   "Face used for displaying newsgroups headers.
814 In the default setup this face is only used for crossposted
815 articles."
816   :group 'gnus-article-headers
817   :group 'gnus-article-highlight)
818 ;; backward-compatibility alias
819 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
820 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
821
822 (defface gnus-header-name
823   '((((class color)
824       (background dark))
825      (:foreground "SpringGreen2"))
826     (((class color)
827       (background light))
828      (:foreground "maroon"))
829     (t
830      (:bold t)))
831   "Face used for displaying header names."
832   :group 'gnus-article-headers
833   :group 'gnus-article-highlight)
834 ;; backward-compatibility alias
835 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
836 (put 'gnus-header-name-face 'obsolete-face "22.1")
837
838 (defface gnus-header-content
839   '((((class color)
840       (background dark))
841      (:foreground "SpringGreen1" :italic t))
842     (((class color)
843       (background light))
844      (:foreground "indianred4" :italic t))
845     (t
846      (:italic t)))  "Face used for displaying header content."
847   :group 'gnus-article-headers
848   :group 'gnus-article-highlight)
849 ;; backward-compatibility alias
850 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
851 (put 'gnus-header-content-face 'obsolete-face "22.1")
852
853 (defcustom gnus-header-face-alist
854   '(("From" nil gnus-header-from)
855     ("Subject" nil gnus-header-subject)
856     ("Newsgroups:.*," nil gnus-header-newsgroups)
857     ("" gnus-header-name gnus-header-content))
858   "*Controls highlighting of article headers.
859
860 An alist of the form (HEADER NAME CONTENT).
861
862 HEADER is a regular expression which should match the name of a
863 header and NAME and CONTENT are either face names or nil.
864
865 The name of each header field will be displayed using the face
866 specified by the first element in the list where HEADER matches
867 the header name and NAME is non-nil.  Similarly, the content will
868 be displayed by the first non-nil matching CONTENT face."
869   :group 'gnus-article-headers
870   :group 'gnus-article-highlight
871   :type '(repeat (list (regexp :tag "Header")
872                        (choice :tag "Name"
873                                (item :tag "skip" nil)
874                                (face :value default))
875                        (choice :tag "Content"
876                                (item :tag "skip" nil)
877                                (face :value default)))))
878
879 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
880                                           '((xface . (:face gnus-x-face)))
881                                         '((pbm . (:face gnus-x-face))
882                                           (png . nil)))
883   "Alist of image types and properties applied to Face and X-Face images.
884 Here are examples:
885
886 ;; Specify the altitude of Face images in the From header.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :ascent 80))
889         (png . (:ascent 80))))
890
891 ;; Show Face images as pressed buttons.
892 \(setq gnus-face-properties-alist
893       '((pbm . (:face gnus-x-face :relief -2))
894         (png . (:relief -2))))
895
896 See the manual for the valid properties for various image types.
897 Currently, `pbm' is used for X-Face images and `png' is used for Face
898 images in Emacs.  Only the `:face' property is effective on the `xface'
899 image type in XEmacs if it is built with the libcompface library."
900   :version "23.1" ;; No Gnus
901   :group 'gnus-article-headers
902   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
903
904 (defcustom gnus-article-decode-hook
905   '(article-decode-charset article-decode-encoded-words
906                            article-decode-group-name article-decode-idna-rhs)
907   "*Hook run to decode charsets in articles."
908   :group 'gnus-article-headers
909   :type 'hook)
910
911 (defcustom gnus-display-mime-function 'gnus-display-mime
912   "Function to display MIME articles."
913   :group 'gnus-article-mime
914   :type 'function)
915
916 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
917   "Function used to decode headers.")
918
919 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
920   "Function used to decode addresses.")
921
922 (defvar gnus-article-dumbquotes-map
923   '((?\200 "EUR")
924     (?\202 ",")
925     (?\203 "f")
926     (?\204 ",,")
927     (?\205 "...")
928     (?\213 "<")
929     (?\214 "OE")
930     (?\221 "`")
931     (?\222 "'")
932     (?\223 "``")
933     (?\224 "\"")
934     (?\225 "*")
935     (?\226 "-")
936     (?\227 "--")
937     (?\230 "~")
938     (?\231 "(TM)")
939     (?\233 ">")
940     (?\234 "oe")
941     (?\264 "'"))
942   "Table for MS-to-Latin1 translation.")
943
944 (defcustom gnus-ignored-mime-types nil
945   "List of MIME types that should be ignored by Gnus."
946   :version "21.1"
947   :group 'gnus-article-mime
948   :type '(repeat regexp))
949
950 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
951   "List of MIME types that should not be given buttons when rendered inline.
952 See also `gnus-buttonized-mime-types' which may override this variable.
953 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
954   :version "21.1"
955   :group 'gnus-article-mime
956   :type '(repeat regexp))
957
958 (defcustom gnus-buttonized-mime-types nil
959   "List of MIME types that should be given buttons when rendered inline.
960 If set, this variable overrides `gnus-unbuttonized-mime-types'.
961 To see e.g. security buttons you could set this to
962 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
963 this list to display radio buttons that allow you to choose one of two
964 media types those mails include.  See also `mm-discouraged-alternatives'.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "22.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-inhibit-mime-unbuttonizing nil
971   "If non-nil, all MIME parts get buttons.
972 When nil (the default value), then some MIME parts do not get buttons,
973 as described by the variables `gnus-buttonized-mime-types' and
974 `gnus-unbuttonized-mime-types'."
975   :version "22.1"
976   :group 'gnus-article-mime
977   :type 'boolean)
978
979 (defcustom gnus-body-boundary-delimiter "_"
980   "String used to delimit header and body.
981 This variable is used by `gnus-article-treat-body-boundary' which can
982 be controlled by `gnus-treat-body-boundary'."
983   :version "22.1"
984   :group 'gnus-article-various
985   :type '(choice (item :tag "None" :value nil)
986                  string))
987
988 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
989                                   "/usr/share/picons")
990   "Defines the location of the faces database.
991 For information on obtaining this database of pretty pictures, please
992 see http://www.cs.indiana.edu/picons/ftp/index.html"
993   :version "22.1"
994   :type '(repeat directory)
995   :link '(url-link :tag "download"
996                    "http://www.cs.indiana.edu/picons/ftp/index.html")
997   :link '(custom-manual "(gnus)Picons")
998   :group 'gnus-picon)
999
1000 (defun gnus-picons-installed-p ()
1001   "Say whether picons are installed on your machine."
1002   (let ((installed nil))
1003     (dolist (database gnus-picon-databases)
1004       (when (file-exists-p database)
1005         (setq installed t)))
1006     installed))
1007
1008 (defcustom gnus-article-mime-part-function nil
1009   "Function called with a MIME handle as the argument.
1010 This is meant for people who want to do something automatic based
1011 on parts -- for instance, adding Vcard info to a database."
1012   :group 'gnus-article-mime
1013   :type '(choice (const nil)
1014                  function))
1015
1016 (defcustom gnus-mime-multipart-functions nil
1017   "An alist of MIME types to functions to display them."
1018   :version "21.1"
1019   :group 'gnus-article-mime
1020   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1021
1022 (defcustom gnus-article-date-headers '(combined-lapsed)
1023   "A list of Date header formats to display.
1024 Valid formats are `ut' (universal time), `local' (local time
1025 zone), `english' (readable English), `lapsed' (elapsed time),
1026 `combined-lapsed' (both the original date and the elapsed time),
1027 `original' (the original date header), `iso8601' (ISO8601
1028 format), and `user-defined' (a user-defined format defined by the
1029 `gnus-article-time-format' variable).
1030
1031 You have as many date headers as you want in the article buffer.
1032 Some of these headers are updated automatically.  See
1033 `gnus-article-update-date-headers' for details."
1034   :version "24.1"
1035   :group 'gnus-article-headers
1036   :type '(set
1037           (const :tag "Universal time (UT)" ut)
1038           (const :tag "Local time zone" local)
1039           (const :tag "Readable English" english)
1040           (const :tag "Elapsed time" lapsed)
1041           (const :tag "Original and elapsed time" combined-lapsed)
1042           (const :tag "Original date header" original)
1043           (const :tag "ISO8601 format" iso8601)
1044           (const :tag "User-defined" user-defined)))
1045
1046 (defcustom gnus-article-update-date-headers nil
1047   "A number that says how often to update the date header (in seconds).
1048 If nil, don't update it at all."
1049   :version "24.1"
1050   :group 'gnus-article-headers
1051   :type '(choice
1052           (item :tag "Don't update" :value nil)
1053           integer))
1054
1055 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1056   "Function called with a MIME handle as the argument.
1057 This is meant for people who want to view first matched part.
1058 For `undisplayed-alternative' (default), the first undisplayed
1059 part or alternative part is used.  For `undisplayed', the first
1060 undisplayed part is used.  For a function, the first part which
1061 the function return t is used.  For nil, the first part is
1062 used."
1063   :version "21.1"
1064   :group 'gnus-article-mime
1065   :type '(choice
1066           (item :tag "first" :value nil)
1067           (item :tag "undisplayed" :value undisplayed)
1068           (item :tag "undisplayed or alternative"
1069                 :value undisplayed-alternative)
1070           (function)))
1071
1072 (defcustom gnus-mime-action-alist
1073   '(("save to file" . gnus-mime-save-part)
1074     ("save and strip" . gnus-mime-save-part-and-strip)
1075     ("replace with file" . gnus-mime-replace-part)
1076     ("delete part" . gnus-mime-delete-part)
1077     ("display as text" . gnus-mime-inline-part)
1078     ("view the part" . gnus-mime-view-part)
1079     ("pipe to command" . gnus-mime-pipe-part)
1080     ("toggle display" . gnus-article-press-button)
1081     ("toggle display" . gnus-article-view-part-as-charset)
1082     ("view as type" . gnus-mime-view-part-as-type)
1083     ("view internally" . gnus-mime-view-part-internally)
1084     ("view externally" . gnus-mime-view-part-externally))
1085   "An alist of actions that run on the MIME attachment."
1086   :group 'gnus-article-mime
1087   :type '(repeat (cons (string :tag "name")
1088                        (function))))
1089
1090 (defcustom gnus-auto-select-part 1
1091   "Advance to next MIME part when deleting or stripping parts.
1092
1093 When 0, point will be placed on the same part as before.  When
1094 positive (negative), move point forward (backwards) this many
1095 parts.  When nil, redisplay article."
1096   :version "23.1" ;; No Gnus
1097   :group 'gnus-article-mime
1098   :type '(choice (const nil :tag "Redisplay article.")
1099                  (const 1 :tag "Next part.")
1100                  (const 0 :tag "Current part.")
1101                  integer))
1102
1103 ;;;
1104 ;;; The treatment variables
1105 ;;;
1106
1107 (defvar gnus-part-display-hook nil
1108   "Hook called on parts that are to receive treatment.")
1109
1110 (defvar gnus-article-treat-custom
1111   '(choice (const :tag "Off" nil)
1112            (const :tag "On" t)
1113            (const :tag "Header" head)
1114            (const :tag "First" first)
1115            (const :tag "Last" last)
1116            (integer :tag "Less")
1117            (repeat :tag "Groups" regexp)
1118            (sexp :tag "Predicate")))
1119
1120 (defvar gnus-article-treat-head-custom
1121   '(choice (const :tag "Off" nil)
1122            (const :tag "Header" head)))
1123
1124 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1125                                    "text/x-patch" "text/html")
1126   "Part types eligible for treatment.")
1127
1128 (defvar gnus-inhibit-treatment nil
1129   "Whether to inhibit treatment.")
1130
1131 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1132   "Highlight the signature.
1133 Valid values are nil, t, `head', `first', `last', an integer or a
1134 predicate.  See Info node `(gnus)Customizing Articles'."
1135   :group 'gnus-article-treat
1136   :link '(custom-manual "(gnus)Customizing Articles")
1137   :type gnus-article-treat-custom)
1138 (put 'gnus-treat-highlight-signature 'highlight t)
1139
1140 (defcustom gnus-treat-buttonize 100000
1141   "Add buttons.
1142 Valid values are nil, t, `head', `first', `last', an integer or a
1143 predicate.  See Info node `(gnus)Customizing Articles'."
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147 (put 'gnus-treat-buttonize 'highlight t)
1148
1149 (defcustom gnus-treat-buttonize-head 'head
1150   "Add buttons to the head.
1151 Valid values are nil, t, `head', `first', `last', an integer or a
1152 predicate.  See Info node `(gnus)Customizing Articles'."
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-head-custom)
1156 (put 'gnus-treat-buttonize-head 'highlight t)
1157
1158 (defcustom gnus-treat-date 'head
1159   "Display dates according to the `gnus-article-date-headers' variable.
1160 Valid values are nil, t, `head', `first', `last', an integer or a
1161 predicate.  See Info node `(gnus)Customizing Articles'."
1162   :version "24.1"
1163   :group 'gnus-article-treat
1164   :link '(custom-manual "(gnus)Customizing Articles")
1165   :type gnus-article-treat-head-custom)
1166
1167 (defcustom gnus-treat-emphasize 50000
1168   "Emphasize text.
1169 Valid values are nil, t, `head', `first', `last', an integer or a
1170 predicate.  See Info node `(gnus)Customizing Articles'."
1171   :group 'gnus-article-treat
1172   :link '(custom-manual "(gnus)Customizing Articles")
1173   :type gnus-article-treat-custom)
1174 (put 'gnus-treat-emphasize 'highlight t)
1175
1176 (defcustom gnus-treat-strip-cr nil
1177   "Remove carriage returns.
1178 Valid values are nil, t, `head', `first', `last', an integer or a
1179 predicate.  See Info node `(gnus)Customizing Articles'."
1180   :version "22.1"
1181   :group 'gnus-article-treat
1182   :link '(custom-manual "(gnus)Customizing Articles")
1183   :type gnus-article-treat-custom)
1184
1185 (defcustom gnus-treat-unsplit-urls nil
1186   "Remove newlines from within URLs.
1187 Valid values are nil, t, `head', `first', `last', an integer or a
1188 predicate.  See Info node `(gnus)Customizing Articles'."
1189   :version "22.1"
1190   :group 'gnus-article-treat
1191   :link '(custom-manual "(gnus)Customizing Articles")
1192   :type gnus-article-treat-custom)
1193
1194 (defcustom gnus-treat-leading-whitespace nil
1195   "Remove leading whitespace in headers.
1196 Valid values are nil, t, `head', `first', `last', an integer or a
1197 predicate.  See Info node `(gnus)Customizing Articles'."
1198   :version "22.1"
1199   :group 'gnus-article-treat
1200   :link '(custom-manual "(gnus)Customizing Articles")
1201   :type gnus-article-treat-custom)
1202
1203 (defcustom gnus-treat-hide-headers 'head
1204   "Hide headers.
1205 Valid values are nil, t, `head', `first', `last', an integer or a
1206 predicate.  See Info node `(gnus)Customizing Articles'."
1207   :group 'gnus-article-treat
1208   :link '(custom-manual "(gnus)Customizing Articles")
1209   :type gnus-article-treat-head-custom)
1210
1211 (defcustom gnus-treat-hide-boring-headers nil
1212   "Hide boring headers.
1213 Valid values are nil, t, `head', `first', `last', an integer or a
1214 predicate.  See Info node `(gnus)Customizing Articles'."
1215   :group 'gnus-article-treat
1216   :link '(custom-manual "(gnus)Customizing Articles")
1217   :type gnus-article-treat-head-custom)
1218
1219 (defcustom gnus-treat-hide-signature nil
1220   "Hide the signature.
1221 Valid values are nil, t, `head', `first', `last', an integer or a
1222 predicate.  See Info node `(gnus)Customizing Articles'."
1223   :group 'gnus-article-treat
1224   :link '(custom-manual "(gnus)Customizing Articles")
1225   :type gnus-article-treat-custom)
1226
1227 (defcustom gnus-treat-fill-article nil
1228   "Fill the article.
1229 Valid values are nil, t, `head', `first', `last', an integer or a
1230 predicate.  See Info node `(gnus)Customizing Articles'."
1231   :group 'gnus-article-treat
1232   :link '(custom-manual "(gnus)Customizing Articles")
1233   :type gnus-article-treat-custom)
1234
1235 (defcustom gnus-treat-hide-citation nil
1236   "Hide cited text.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'.
1239
1240 See `gnus-article-highlight-citation' for variables used to
1241 control what it hides."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-custom)
1245
1246 (defcustom gnus-treat-hide-citation-maybe nil
1247   "Hide cited text according to certain conditions.
1248 Valid values are nil, t, `head', `first', `last', an integer or a
1249 predicate.  See Info node `(gnus)Customizing Articles'.
1250
1251 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1252 how to control what it hides."
1253   :group 'gnus-article-treat
1254   :link '(custom-manual "(gnus)Customizing Articles")
1255   :type gnus-article-treat-custom)
1256
1257 (defcustom gnus-treat-strip-list-identifiers 'head
1258   "Strip list identifiers from `gnus-list-identifiers`.
1259 Valid values are nil, t, `head', `first', `last', an integer or a
1260 predicate.  See Info node `(gnus)Customizing Articles'."
1261   :version "21.1"
1262   :group 'gnus-article-treat
1263   :link '(custom-manual "(gnus)Customizing Articles")
1264   :type gnus-article-treat-custom)
1265
1266 (gnus-define-group-parameter
1267  list-identifier
1268  :variable-document
1269  "Alist of regexps and correspondent identifiers."
1270  :variable-group gnus-article-washing
1271  :parameter-type
1272  '(choice :tag "Identifier"
1273           :value nil
1274           (symbol :tag "Item in `gnus-list-identifiers'" none)
1275           regexp
1276           (const :tag "None" nil))
1277  :parameter-document
1278  "If non-nil, specify how to remove `identifiers' from articles' subject.
1279
1280 Any symbol is used to look up a regular expression to match the
1281 banner in `gnus-list-identifiers'.  A string is used as a regular
1282 expression to match the identifier directly.")
1283
1284 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1285                         "Gnus 5.10 (Emacs 22.1)")
1286
1287 (defcustom gnus-treat-strip-pem nil
1288   "Strip PEM signatures.
1289 Valid values are nil, t, `head', `first', `last', an integer or a
1290 predicate.  See Info node `(gnus)Customizing Articles'."
1291   :group 'gnus-article-treat
1292   :link '(custom-manual "(gnus)Customizing Articles")
1293   :type gnus-article-treat-custom)
1294
1295 (defcustom gnus-treat-strip-banner t
1296   "Strip banners from articles.
1297 The banner to be stripped is specified in the `banner' group parameter.
1298 Valid values are nil, t, `head', `first', `last', an integer or a
1299 predicate.  See Info node `(gnus)Customizing Articles'."
1300   :group 'gnus-article-treat
1301   :link '(custom-manual "(gnus)Customizing Articles")
1302   :type gnus-article-treat-custom)
1303
1304 (defcustom gnus-treat-highlight-headers 'head
1305   "Highlight the headers.
1306 Valid values are nil, t, `head', `first', `last', an integer or a
1307 predicate.  See Info node `(gnus)Customizing Articles'."
1308   :group 'gnus-article-treat
1309   :link '(custom-manual "(gnus)Customizing Articles")
1310   :type gnus-article-treat-head-custom)
1311 (put 'gnus-treat-highlight-headers 'highlight t)
1312
1313 (defcustom gnus-treat-highlight-citation t
1314   "Highlight cited text.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'."
1317   :group 'gnus-article-treat
1318   :link '(custom-manual "(gnus)Customizing Articles")
1319   :type gnus-article-treat-custom)
1320 (put 'gnus-treat-highlight-citation 'highlight t)
1321
1322 (defcustom gnus-treat-strip-headers-in-body t
1323   "Strip the X-No-Archive header line from the beginning of the body.
1324 Valid values are nil, t, `head', `first', `last', an integer or a
1325 predicate.  See Info node `(gnus)Customizing Articles'."
1326   :version "21.1"
1327   :group 'gnus-article-treat
1328   :link '(custom-manual "(gnus)Customizing Articles")
1329   :type gnus-article-treat-custom)
1330
1331 (defcustom gnus-treat-strip-trailing-blank-lines nil
1332   "Strip trailing blank lines.
1333 Valid values are nil, t, `head', `first', `last', an integer or a
1334 predicate.  See Info node `(gnus)Customizing Articles'.
1335
1336 When set to t, it also strips trailing blanks in all MIME parts.
1337 Consider to use `last' instead."
1338   :group 'gnus-article-treat
1339   :link '(custom-manual "(gnus)Customizing Articles")
1340   :type gnus-article-treat-custom)
1341
1342 (defcustom gnus-treat-strip-leading-blank-lines nil
1343   "Strip leading blank lines.
1344 Valid values are nil, t, `head', `first', `last', an integer or a
1345 predicate.  See Info node `(gnus)Customizing Articles'.
1346
1347 When set to t, it also strips trailing blanks in all MIME parts."
1348   :group 'gnus-article-treat
1349   :link '(custom-manual "(gnus)Customizing Articles")
1350   :type gnus-article-treat-custom)
1351
1352 (defcustom gnus-treat-strip-multiple-blank-lines nil
1353   "Strip multiple blank lines.
1354 Valid values are nil, t, `head', `first', `last', an integer or a
1355 predicate.  See Info node `(gnus)Customizing Articles'."
1356   :group 'gnus-article-treat
1357   :link '(custom-manual "(gnus)Customizing Articles")
1358   :type gnus-article-treat-custom)
1359
1360 (defcustom gnus-treat-unfold-headers 'head
1361   "Unfold folded header lines.
1362 Valid values are nil, t, `head', `first', `last', an integer or a
1363 predicate.  See Info node `(gnus)Customizing Articles'."
1364   :version "22.1"
1365   :group 'gnus-article-treat
1366   :link '(custom-manual "(gnus)Customizing Articles")
1367   :type gnus-article-treat-custom)
1368
1369 (defcustom gnus-article-unfold-long-headers nil
1370   "If non-nil, allow unfolding headers even if the header is long.
1371 If it is a regexp, only long headers matching this regexp are unfolded.
1372 If it is t, all long headers are unfolded.
1373
1374 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1375   :version "23.1" ;; No Gnus
1376   :group 'gnus-article-treat
1377   :type '(choice (const nil)
1378                  (const :tag "all" t)
1379                  (regexp)))
1380
1381 (defcustom gnus-treat-fold-headers nil
1382   "Fold headers.
1383 Valid values are nil, t, `head', `first', `last', an integer or a
1384 predicate.  See Info node `(gnus)Customizing Articles'."
1385   :version "22.1"
1386   :group 'gnus-article-treat
1387   :link '(custom-manual "(gnus)Customizing Articles")
1388   :type gnus-article-treat-custom)
1389
1390 (defcustom gnus-treat-fold-newsgroups 'head
1391   "Fold the Newsgroups and Followup-To headers.
1392 Valid values are nil, t, `head', `first', `last', an integer or a
1393 predicate.  See Info node `(gnus)Customizing Articles'."
1394   :version "22.1"
1395   :group 'gnus-article-treat
1396   :link '(custom-manual "(gnus)Customizing Articles")
1397   :type gnus-article-treat-custom)
1398
1399 (defcustom gnus-treat-overstrike t
1400   "Treat overstrike highlighting.
1401 Valid values are nil, t, `head', `first', `last', an integer or a
1402 predicate.  See Info node `(gnus)Customizing Articles'."
1403   :group 'gnus-article-treat
1404   :link '(custom-manual "(gnus)Customizing Articles")
1405   :type gnus-article-treat-custom)
1406 (put 'gnus-treat-overstrike 'highlight t)
1407
1408 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1409   "Treat ANSI SGR control sequences.
1410 Valid values are nil, t, `head', `first', `last', an integer or a
1411 predicate.  See Info node `(gnus)Customizing Articles'."
1412   :group 'gnus-article-treat
1413   :link '(custom-manual "(gnus)Customizing Articles")
1414   :type gnus-article-treat-custom)
1415
1416 (make-obsolete-variable 'gnus-treat-display-xface
1417                         'gnus-treat-display-x-face "Emacs 22.1")
1418
1419 (defcustom gnus-treat-display-x-face
1420   (and (not noninteractive)
1421        (gnus-image-type-available-p 'xbm)
1422        (if (featurep 'xemacs)
1423            (featurep 'xface)
1424          (condition-case nil
1425              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1426                   (executable-find "icontopbm"))
1427            ;; shell-command-to-string may signal an error, e.g. if
1428            ;; shell-file-name is not found.
1429            (error nil)))
1430        'head)
1431   "Display X-Face headers.
1432 Valid values are nil and `head'.
1433 See Info node `(gnus)Customizing Articles' and Info node
1434 `(gnus)X-Face' for details."
1435   :group 'gnus-article-treat
1436   :version "21.1"
1437   :link '(custom-manual "(gnus)Customizing Articles")
1438   :link '(custom-manual "(gnus)X-Face")
1439   :type gnus-article-treat-head-custom
1440   :set (lambda (symbol value)
1441          (set-default
1442           symbol
1443           (cond ((or (boundp symbol) (get symbol 'saved-value))
1444                  value)
1445                 ((boundp 'gnus-treat-display-xface)
1446                  (message "\
1447 ** gnus-treat-display-xface is an obsolete variable;\
1448  use gnus-treat-display-x-face instead")
1449                  (default-value 'gnus-treat-display-xface))
1450                 ((get 'gnus-treat-display-xface 'saved-value)
1451                  (message "\
1452 ** gnus-treat-display-xface is an obsolete variable;\
1453  use gnus-treat-display-x-face instead")
1454                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1455                 (t
1456                  value)))))
1457 (put 'gnus-treat-display-x-face 'highlight t)
1458
1459 (defcustom gnus-treat-display-face
1460   (and (not noninteractive)
1461        (gnus-image-type-available-p 'png)
1462        'head)
1463   "Display Face headers.
1464 Valid values are nil, t, `head', `first', `last', an integer or a
1465 predicate.  See Info node `(gnus)Customizing Articles' and Info
1466 node `(gnus)Face' for details."
1467   :group 'gnus-article-treat
1468   :version "22.1"
1469   :link '(custom-manual "(gnus)Customizing Articles")
1470   :link '(custom-manual "(gnus)X-Face")
1471   :type gnus-article-treat-head-custom)
1472 (put 'gnus-treat-display-face 'highlight t)
1473
1474 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1475   "Display smileys.
1476 Valid values are nil, t, `head', `first', `last', an integer or a
1477 predicate.  See Info node `(gnus)Customizing Articles' and Info
1478 node `(gnus)Smileys' for details."
1479   :group 'gnus-article-treat
1480   :version "21.1"
1481   :link '(custom-manual "(gnus)Customizing Articles")
1482   :link '(custom-manual "(gnus)Smileys")
1483   :type gnus-article-treat-custom)
1484 (put 'gnus-treat-display-smileys 'highlight t)
1485
1486 (defcustom gnus-treat-from-picon
1487   (if (and (gnus-image-type-available-p 'xpm)
1488            (gnus-picons-installed-p))
1489       'head nil)
1490   "Display picons in the From header.
1491 Valid values are nil, t, `head', `first', `last', an integer or a
1492 predicate.  See Info node `(gnus)Customizing Articles' and Info
1493 node `(gnus)Picons' for details."
1494   :version "22.1"
1495   :group 'gnus-article-treat
1496   :group 'gnus-picon
1497   :link '(custom-manual "(gnus)Customizing Articles")
1498   :link '(custom-manual "(gnus)Picons")
1499   :type gnus-article-treat-head-custom)
1500 (put 'gnus-treat-from-picon 'highlight t)
1501
1502 (defcustom gnus-treat-mail-picon
1503   (if (and (gnus-image-type-available-p 'xpm)
1504            (gnus-picons-installed-p))
1505       'head nil)
1506   "Display picons in To and Cc headers.
1507 Valid values are nil, t, `head', `first', `last', an integer or a
1508 predicate.  See Info node `(gnus)Customizing Articles' and Info
1509 node `(gnus)Picons' for details."
1510   :version "22.1"
1511   :group 'gnus-article-treat
1512   :group 'gnus-picon
1513   :link '(custom-manual "(gnus)Customizing Articles")
1514   :link '(custom-manual "(gnus)Picons")
1515   :type gnus-article-treat-head-custom)
1516 (put 'gnus-treat-mail-picon 'highlight t)
1517
1518 (defcustom gnus-treat-newsgroups-picon
1519   (if (and (gnus-image-type-available-p 'xpm)
1520            (gnus-picons-installed-p))
1521       'head nil)
1522   "Display picons in the Newsgroups and Followup-To headers.
1523 Valid values are nil, t, `head', `first', `last', an integer or a
1524 predicate.  See Info node `(gnus)Customizing Articles' and Info
1525 node `(gnus)Picons' for details."
1526   :version "22.1"
1527   :group 'gnus-article-treat
1528   :group 'gnus-picon
1529   :link '(custom-manual "(gnus)Customizing Articles")
1530   :link '(custom-manual "(gnus)Picons")
1531   :type gnus-article-treat-head-custom)
1532 (put 'gnus-treat-newsgroups-picon 'highlight t)
1533
1534 (defcustom gnus-treat-from-gravatar nil
1535   "Display gravatars in the From header.
1536 Valid values are nil, t, `head', `first', `last', an integer or a
1537 predicate.  See Info node `(gnus)Customizing Articles' and Info
1538 node `(gnus)Gravatars' for details."
1539   :version "24.1"
1540   :group 'gnus-article-treat
1541   :group 'gnus-gravatar
1542   :link '(custom-manual "(gnus)Customizing Articles")
1543   :link '(custom-manual "(gnus)Gravatars")
1544   :type gnus-article-treat-head-custom)
1545 (put 'gnus-treat-from-gravatar 'highlight t)
1546
1547 (defcustom gnus-treat-mail-gravatar nil
1548   "Display gravatars in To and Cc headers.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles' and Info
1551 node `(gnus)Gravatars' for details."
1552   :version "24.1"
1553   :group 'gnus-article-treat
1554   :group 'gnus-gravatar
1555   :link '(custom-manual "(gnus)Customizing Articles")
1556   :link '(custom-manual "(gnus)Gravatars")
1557   :type gnus-article-treat-head-custom)
1558 (put 'gnus-treat-mail-gravatar 'highlight t)
1559
1560 (defcustom gnus-treat-body-boundary
1561   (if (or gnus-treat-newsgroups-picon
1562           gnus-treat-mail-picon
1563           gnus-treat-from-picon
1564           gnus-treat-from-gravatar
1565           gnus-treat-mail-gravatar)
1566       ;; If there's much decoration, the user might prefer a boundary.
1567       'head
1568     nil)
1569   "Draw a boundary at the end of the headers.
1570 Valid values are nil and `head'.
1571 See Info node `(gnus)Customizing Articles' for details."
1572   :version "22.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-head-custom)
1576
1577 (defcustom gnus-treat-capitalize-sentences nil
1578   "Capitalize sentence-starting words.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "21.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-wash-html nil
1587   "Format as HTML.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "22.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1596   "Fill long lines.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :version "24.1"
1600   :group 'gnus-article-treat
1601   :link '(custom-manual "(gnus)Customizing Articles")
1602   :type gnus-article-treat-custom)
1603
1604 (defcustom gnus-treat-x-pgp-sig nil
1605   "Verify X-PGP-Sig.
1606 To automatically treat X-PGP-Sig, set it to head.
1607 Valid values are nil, t, `head', `first', `last', an integer or a
1608 predicate.  See Info node `(gnus)Customizing Articles'."
1609   :version "22.1"
1610   :group 'gnus-article-treat
1611   :group 'mime-security
1612   :link '(custom-manual "(gnus)Customizing Articles")
1613   :type gnus-article-treat-custom)
1614
1615 (defvar gnus-article-encrypt-protocol-alist
1616   '(("PGP" . mml2015-self-encrypt)))
1617
1618 ;; Set to nil if more than one protocol added to
1619 ;; gnus-article-encrypt-protocol-alist.
1620 (defcustom gnus-article-encrypt-protocol "PGP"
1621   "The protocol used for encrypt articles.
1622 It is a string, such as \"PGP\". If nil, ask user."
1623   :version "22.1"
1624   :type 'string
1625   :group 'mime-security)
1626
1627 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1628                               (mm-coding-system-p 'utf-8)
1629                               (executable-find idna-program))
1630   "Whether IDNA decoding of headers is used when viewing messages.
1631 This requires GNU Libidn, and by default only enabled if it is found."
1632   :version "22.1"
1633   :group 'gnus-article-headers
1634   :type 'boolean)
1635
1636 (defcustom gnus-article-over-scroll nil
1637   "If non-nil, allow scrolling the article buffer even when there no more text."
1638   :version "22.1"
1639   :group 'gnus-article
1640   :type 'boolean)
1641
1642 (defcustom gnus-inhibit-images nil
1643   "Non-nil means inhibit displaying of images inline in the article body."
1644   :version "24.1"
1645   :group 'gnus-article
1646   :type 'boolean)
1647
1648 (defcustom gnus-blocked-images 'gnus-block-private-groups
1649   "Images that have URLs matching this regexp will be blocked.
1650 This can also be a function to be evaluated.  If so, it will be
1651 called with the group name as the parameter, and should return a
1652 regexp."
1653   :version "24.1"
1654   :group 'gnus-art
1655   :type '(choice regexp function))
1656
1657 ;;; Internal variables
1658
1659 (defvar gnus-english-month-names
1660   '("January" "February" "March" "April" "May" "June" "July" "August"
1661     "September" "October" "November" "December"))
1662
1663 (defvar article-goto-body-goes-to-point-min-p nil)
1664 (defvar gnus-article-wash-types nil)
1665 (defvar gnus-article-emphasis-alist nil)
1666 (defvar gnus-article-image-alist nil)
1667
1668 (defvar gnus-article-mime-handle-alist-1 nil)
1669 (defvar gnus-treatment-function-alist
1670   '((gnus-treat-strip-cr gnus-article-remove-cr)
1671     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1672     (gnus-treat-strip-banner gnus-article-strip-banner)
1673     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1674     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1675     (gnus-treat-buttonize gnus-article-add-buttons)
1676     (gnus-treat-fill-article gnus-article-fill-cited-article)
1677     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1678     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1679     (gnus-treat-display-x-face gnus-article-display-x-face)
1680     (gnus-treat-display-face gnus-article-display-face)
1681     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1682     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1683     (gnus-treat-hide-signature gnus-article-hide-signature)
1684     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1685     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1686     (gnus-treat-from-picon gnus-treat-from-picon)
1687     (gnus-treat-mail-picon gnus-treat-mail-picon)
1688     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1689     (gnus-treat-strip-pem gnus-article-hide-pem)
1690     (gnus-treat-date gnus-article-treat-date)
1691     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1692     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1693     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1694     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1695     (gnus-treat-strip-trailing-blank-lines
1696      gnus-article-remove-trailing-blank-lines)
1697     (gnus-treat-strip-leading-blank-lines
1698      gnus-article-strip-leading-blank-lines)
1699     (gnus-treat-strip-multiple-blank-lines
1700      gnus-article-strip-multiple-blank-lines)
1701     (gnus-treat-overstrike gnus-article-treat-overstrike)
1702     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1703     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1704     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1705     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1706     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1707     (gnus-treat-display-smileys gnus-treat-smiley)
1708     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1709     (gnus-treat-wash-html gnus-article-wash-html)
1710     (gnus-treat-emphasize gnus-article-emphasize)
1711     (gnus-treat-hide-citation gnus-article-hide-citation)
1712     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1713     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1714     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1715
1716 (defvar gnus-article-mime-handle-alist nil)
1717 (defvar article-lapsed-timer nil)
1718 (defvar gnus-article-current-summary nil)
1719
1720 (defvar gnus-article-mode-syntax-table
1721   (let ((table (copy-syntax-table text-mode-syntax-table)))
1722     ;; This causes the citation match run O(2^n).
1723     ;; (modify-syntax-entry ?- "w" table)
1724     (modify-syntax-entry ?> ")<" table)
1725     (modify-syntax-entry ?< "(>" table)
1726     ;; make M-. in article buffers work for `foo' strings
1727     (modify-syntax-entry ?' " " table)
1728     (modify-syntax-entry ?` " " table)
1729     table)
1730   "Syntax table used in article mode buffers.
1731 Initialized from `text-mode-syntax-table.")
1732
1733 (defvar gnus-save-article-buffer nil)
1734
1735 (defvar gnus-number-of-articles-to-be-saved nil)
1736
1737 (defvar gnus-inhibit-hiding nil)
1738
1739 (defvar gnus-article-edit-mode nil)
1740
1741 ;;; Macros for dealing with the article buffer.
1742
1743 (defmacro gnus-with-article-headers (&rest forms)
1744   `(with-current-buffer gnus-article-buffer
1745      (save-restriction
1746        (let ((inhibit-read-only t)
1747              (inhibit-point-motion-hooks t)
1748              (case-fold-search t))
1749          (article-narrow-to-head)
1750          ,@forms))))
1751
1752 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1753 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1754
1755 (defmacro gnus-with-article-buffer (&rest forms)
1756   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1757      (with-current-buffer gnus-article-buffer
1758        (let ((inhibit-read-only t))
1759          ,@forms))))
1760
1761 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1762 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1763
1764 (defun gnus-article-goto-header (header)
1765   "Go to HEADER, which is a regular expression."
1766   (re-search-forward (concat "^\\(" header "\\):") nil t))
1767
1768 (defsubst gnus-article-hide-text (b e props)
1769   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1770   (gnus-add-text-properties-when 'article-type nil b e props)
1771   (when (memq 'intangible props)
1772     (put-text-property
1773      (max (1- b) (point-min))
1774      b 'intangible (cddr (memq 'intangible props)))))
1775
1776 (defsubst gnus-article-unhide-text (b e)
1777   "Remove hidden text properties from region between B and E."
1778   (remove-text-properties b e gnus-hidden-properties)
1779   (when (memq 'intangible gnus-hidden-properties)
1780     (put-text-property (max (1- b) (point-min))
1781                        b 'intangible nil)))
1782
1783 (defun gnus-article-hide-text-type (b e type)
1784   "Hide text of TYPE between B and E."
1785   (gnus-add-wash-type type)
1786   (gnus-article-hide-text
1787    b e (cons 'article-type (cons type gnus-hidden-properties))))
1788
1789 (defun gnus-article-unhide-text-type (b e type)
1790   "Unhide text of TYPE between B and E."
1791   (gnus-delete-wash-type type)
1792   (remove-text-properties
1793    b e (cons 'article-type (cons type gnus-hidden-properties)))
1794   (when (memq 'intangible gnus-hidden-properties)
1795     (put-text-property (max (1- b) (point-min))
1796                        b 'intangible nil)))
1797
1798 (defun gnus-article-delete-text-of-type (type)
1799   "Delete text of TYPE in the current buffer."
1800   (save-excursion
1801     (let ((b (point-min)))
1802       (if (eq type 'multipart)
1803           ;; Remove MIME buttons associated with multipart/alternative parts.
1804           (progn
1805             (goto-char b)
1806             (while (if (get-text-property (point) 'gnus-part)
1807                        (setq b (point))
1808                      (when (setq b (next-single-property-change (point)
1809                                                                 'gnus-part))
1810                        (goto-char b)
1811                        t))
1812               (end-of-line)
1813               (skip-chars-forward "\n")
1814               (when (eq (get-text-property b 'article-type) 'multipart)
1815                 (delete-region b (point)))))
1816         (while (setq b (text-property-any b (point-max) 'article-type type))
1817           (delete-region
1818            b (or (text-property-not-all b (point-max) 'article-type type)
1819                  (point-max))))))))
1820
1821 (defun gnus-article-delete-invisible-text ()
1822   "Delete all invisible text in the current buffer."
1823   (save-excursion
1824     (let ((b (point-min)))
1825       (while (setq b (text-property-any b (point-max) 'invisible t))
1826         (delete-region
1827          b (or (text-property-not-all b (point-max) 'invisible t)
1828                (point-max)))))))
1829
1830 (defsubst gnus-article-header-rank ()
1831   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1832   (let ((list gnus-sorted-header-list)
1833         (i 1))
1834     (while list
1835       (if (looking-at (car list))
1836           (setq list nil)
1837         (setq list (cdr list))
1838         (incf i)))
1839       i))
1840
1841 (defun article-hide-headers (&optional arg delete)
1842   "Hide unwanted headers and possibly sort them as well."
1843   (interactive)
1844   ;; This function might be inhibited.
1845   (unless gnus-inhibit-hiding
1846     (let ((inhibit-read-only t)
1847           (case-fold-search t)
1848           (max (1+ (length gnus-sorted-header-list)))
1849           (inhibit-point-motion-hooks t)
1850           (cur (current-buffer))
1851           ignored visible beg)
1852       (save-excursion
1853         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1854         ;; group parameters, so we should go to the summary buffer.
1855         (when (prog1
1856                   (condition-case nil
1857                       (progn (set-buffer gnus-summary-buffer) t)
1858                     (error nil))
1859                 (setq ignored (when (not gnus-visible-headers)
1860                                 (cond ((stringp gnus-ignored-headers)
1861                                        gnus-ignored-headers)
1862                                       ((listp gnus-ignored-headers)
1863                                        (mapconcat 'identity
1864                                                   gnus-ignored-headers
1865                                                   "\\|"))))
1866                       visible (cond ((stringp gnus-visible-headers)
1867                                      gnus-visible-headers)
1868                                     ((and gnus-visible-headers
1869                                           (listp gnus-visible-headers))
1870                                      (mapconcat 'identity
1871                                                 gnus-visible-headers
1872                                                 "\\|")))))
1873           (set-buffer cur))
1874         (save-restriction
1875           ;; First we narrow to just the headers.
1876           (article-narrow-to-head)
1877           ;; Hide any "From " lines at the beginning of (mail) articles.
1878           (while (looking-at "From ")
1879             (forward-line 1))
1880           (unless (bobp)
1881             (delete-region (point-min) (point)))
1882           ;; Then treat the rest of the header lines.
1883           ;; Then we use the two regular expressions
1884           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1885           ;; select which header lines is to remain visible in the
1886           ;; article buffer.
1887           (while (re-search-forward "^[^ \t:]*:" nil t)
1888             (beginning-of-line)
1889             ;; Mark the rank of the header.
1890             (put-text-property
1891              (point) (1+ (point)) 'message-rank
1892              (if (or (and visible (looking-at visible))
1893                      (and ignored
1894                           (not (looking-at ignored))))
1895                  (gnus-article-header-rank)
1896                (+ 2 max)))
1897             (forward-line 1))
1898           (message-sort-headers-1)
1899           (when (setq beg (text-property-any
1900                            (point-min) (point-max) 'message-rank (+ 2 max)))
1901             ;; We delete the unwanted headers.
1902             (gnus-add-wash-type 'headers)
1903             (add-text-properties (point-min) (+ 5 (point-min))
1904                                  '(article-type headers dummy-invisible t))
1905             (delete-region beg (point-max))))))))
1906
1907 (defun article-hide-boring-headers (&optional arg)
1908   "Toggle hiding of headers that aren't very interesting.
1909 If given a negative prefix, always show; if given a positive prefix,
1910 always hide."
1911   (interactive (gnus-article-hidden-arg))
1912   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1913              (not gnus-show-all-headers))
1914     (save-excursion
1915       (save-restriction
1916         (let ((inhibit-read-only t)
1917               (inhibit-point-motion-hooks t))
1918           (article-narrow-to-head)
1919           (dolist (elem gnus-boring-article-headers)
1920             (goto-char (point-min))
1921             (cond
1922              ;; Hide empty headers.
1923              ((eq elem 'empty)
1924               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1925                 (forward-line -1)
1926                 (gnus-article-hide-text-type
1927                  (point-at-bol)
1928                  (progn
1929                    (end-of-line)
1930                    (if (re-search-forward "^[^ \t]" nil t)
1931                        (match-beginning 0)
1932                      (point-max)))
1933                  'boring-headers)))
1934              ;; Hide boring Newsgroups header.
1935              ((eq elem 'newsgroups)
1936               (when (gnus-string-equal
1937                      (gnus-fetch-field "newsgroups")
1938                      (gnus-group-real-name
1939                       (if (boundp 'gnus-newsgroup-name)
1940                           gnus-newsgroup-name
1941                         "")))
1942                 (gnus-article-hide-header "newsgroups")))
1943              ((eq elem 'to-address)
1944               (let ((to (message-fetch-field "to"))
1945                     (to-address
1946                      (gnus-parameter-to-address
1947                       (if (boundp 'gnus-newsgroup-name)
1948                           gnus-newsgroup-name ""))))
1949                 (when (and to to-address
1950                            (ignore-errors
1951                              (gnus-string-equal
1952                               ;; only one address in To
1953                               (nth 1 (mail-extract-address-components to))
1954                               to-address)))
1955                   (gnus-article-hide-header "to"))))
1956              ((eq elem 'to-list)
1957               (let ((to (message-fetch-field "to"))
1958                     (to-list
1959                      (gnus-parameter-to-list
1960                       (if (boundp 'gnus-newsgroup-name)
1961                           gnus-newsgroup-name ""))))
1962                 (when (and to to-list
1963                            (ignore-errors
1964                              (gnus-string-equal
1965                               ;; only one address in To
1966                               (nth 1 (mail-extract-address-components to))
1967                               to-list)))
1968                   (gnus-article-hide-header "to"))))
1969              ((eq elem 'cc-list)
1970               (let ((cc (message-fetch-field "cc"))
1971                     (to-list
1972                      (gnus-parameter-to-list
1973                       (if (boundp 'gnus-newsgroup-name)
1974                           gnus-newsgroup-name ""))))
1975                 (when (and cc to-list
1976                            (ignore-errors
1977                              (gnus-string-equal
1978                               ;; only one address in CC
1979                               (nth 1 (mail-extract-address-components cc))
1980                               to-list)))
1981                   (gnus-article-hide-header "cc"))))
1982              ((eq elem 'followup-to)
1983               (when (gnus-string-equal
1984                      (message-fetch-field "followup-to")
1985                      (message-fetch-field "newsgroups"))
1986                 (gnus-article-hide-header "followup-to")))
1987              ((eq elem 'reply-to)
1988               (if (gnus-group-find-parameter
1989                    gnus-newsgroup-name 'broken-reply-to)
1990                   (gnus-article-hide-header "reply-to")
1991                 (let ((from (message-fetch-field "from"))
1992                       (reply-to (message-fetch-field "reply-to")))
1993                   (when
1994                       (and
1995                        from reply-to
1996                        (ignore-errors
1997                          (equal
1998                           (sort (mapcar
1999                                  (lambda (x) (downcase (cadr x)))
2000                                  (mail-extract-address-components from t))
2001                                 'string<)
2002                           (sort (mapcar
2003                                  (lambda (x) (downcase (cadr x)))
2004                                  (mail-extract-address-components reply-to t))
2005                                 'string<))))
2006                     (gnus-article-hide-header "reply-to")))))
2007              ((eq elem 'date)
2008               (let ((date (with-current-buffer gnus-original-article-buffer
2009                             ;; If date in `gnus-article-buffer' is localized
2010                             ;; (`gnus-treat-date-user-defined'),
2011                             ;; `days-between' might fail.
2012                             (message-fetch-field "date"))))
2013                 (when (and date
2014                            (< (days-between (current-time-string) date)
2015                               4))
2016                   (gnus-article-hide-header "date"))))
2017              ((eq elem 'long-to)
2018               (let ((to (message-fetch-field "to"))
2019                     (cc (message-fetch-field "cc")))
2020                 (when (> (length to) 1024)
2021                   (gnus-article-hide-header "to"))
2022                 (when (> (length cc) 1024)
2023                   (gnus-article-hide-header "cc"))))
2024              ((eq elem 'many-to)
2025               (let ((to-count 0)
2026                     (cc-count 0))
2027                 (goto-char (point-min))
2028                 (while (re-search-forward "^to:" nil t)
2029                   (setq to-count (1+ to-count)))
2030                 (when (> to-count 1)
2031                   (while (> to-count 0)
2032                     (goto-char (point-min))
2033                     (save-restriction
2034                       (re-search-forward "^to:" nil nil to-count)
2035                       (forward-line -1)
2036                       (narrow-to-region (point) (point-max))
2037                       (gnus-article-hide-header "to"))
2038                     (setq to-count (1- to-count))))
2039                 (goto-char (point-min))
2040                 (while (re-search-forward "^cc:" nil t)
2041                   (setq cc-count (1+ cc-count)))
2042                 (when (> cc-count 1)
2043                   (while (> cc-count 0)
2044                     (goto-char (point-min))
2045                     (save-restriction
2046                       (re-search-forward "^cc:" nil nil cc-count)
2047                       (forward-line -1)
2048                       (narrow-to-region (point) (point-max))
2049                       (gnus-article-hide-header "cc"))
2050                     (setq cc-count (1- cc-count)))))))))))))
2051
2052 (defun gnus-article-hide-header (header)
2053   (save-excursion
2054     (goto-char (point-min))
2055     (when (re-search-forward (concat "^" header ":") nil t)
2056       (gnus-article-hide-text-type
2057        (point-at-bol)
2058        (progn
2059          (end-of-line)
2060          (if (re-search-forward "^[^ \t]" nil t)
2061              (match-beginning 0)
2062            (point-max)))
2063        'boring-headers))))
2064
2065 (defvar gnus-article-normalized-header-length 40
2066   "Length of normalized headers.")
2067
2068 (defun article-normalize-headers ()
2069   "Make all header lines 40 characters long."
2070   (interactive)
2071   (let ((inhibit-read-only t)
2072         column)
2073     (save-excursion
2074       (save-restriction
2075         (article-narrow-to-head)
2076         (while (not (eobp))
2077           (cond
2078            ((< (setq column (- (point-at-eol) (point)))
2079                gnus-article-normalized-header-length)
2080             (end-of-line)
2081             (insert (make-string
2082                      (- gnus-article-normalized-header-length column)
2083                      ? )))
2084            ((> column gnus-article-normalized-header-length)
2085             (gnus-put-text-property
2086              (progn
2087                (forward-char gnus-article-normalized-header-length)
2088                (point))
2089              (point-at-eol)
2090              'invisible t))
2091            (t
2092             ;; Do nothing.
2093             ))
2094           (forward-line 1))))))
2095
2096 (defun article-treat-dumbquotes ()
2097   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2098 Note that this function guesses whether a character is a sm*rtq**t* or
2099 not, so it should only be used interactively.
2100
2101 Sm*rtq**t*s are M****s***'s unilateral extension to the
2102 iso-8859-1 character map in an attempt to provide more quoting
2103 characters.  If you see something like \\222 or \\264 where
2104 you're expecting some kind of apostrophe or quotation mark, then
2105 try this wash."
2106   (interactive)
2107   (article-translate-strings gnus-article-dumbquotes-map))
2108
2109 (defvar org-entities)
2110
2111 (defun article-treat-non-ascii ()
2112   "Translate many Unicode characters into their ASCII equivalents."
2113   (interactive)
2114   (require 'org-entities)
2115   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2116     (dolist (elem org-entities)
2117       (when (and (listp elem)
2118                  (= (length (nth 6 elem)) 1))
2119         (if (featurep 'xemacs)
2120             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2121           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2122     (save-excursion
2123       (when (article-goto-body)
2124         (let ((inhibit-read-only t)
2125               replace props)
2126           (while (not (eobp))
2127             (if (not (setq replace (if (featurep 'xemacs)
2128                                        (get-char-table (following-char) table)
2129                                      (aref table (following-char)))))
2130                 (forward-char 1)
2131               (if (prog1
2132                       (setq props (text-properties-at (point)))
2133                     (delete-char 1))
2134                   (add-text-properties (point) (progn (insert replace) (point))
2135                                        props)
2136                 (insert replace)))))))))
2137
2138 (defun article-translate-strings (map)
2139   "Translate all string in the body of the article according to MAP.
2140 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2141   (save-excursion
2142     (when (article-goto-body)
2143       (let ((inhibit-read-only t))
2144         (dolist (elem map)
2145           (let ((from (car elem))
2146                 (to (cadr elem)))
2147             (save-excursion
2148               (if (stringp from)
2149                   (while (search-forward from nil t)
2150                     (replace-match to))
2151                 (while (not (eobp))
2152                   (if (eq (following-char) from)
2153                       (progn
2154                         (delete-char 1)
2155                         (insert to))
2156                     (forward-char 1)))))))))))
2157
2158 (defun article-treat-overstrike ()
2159   "Translate overstrikes into bold text."
2160   (interactive)
2161   (save-excursion
2162     (when (article-goto-body)
2163       (let ((inhibit-read-only t))
2164         (while (search-forward "\b" nil t)
2165           (let ((next (char-after))
2166                 (previous (char-after (- (point) 2))))
2167             ;; We do the boldification/underlining by hiding the
2168             ;; overstrikes and putting the proper text property
2169             ;; on the letters.
2170             (cond
2171              ((eq next previous)
2172               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2173               (put-text-property (point) (1+ (point)) 'face 'bold))
2174              ((eq next ?_)
2175               (gnus-article-hide-text-type
2176                (1- (point)) (1+ (point)) 'overstrike)
2177               (put-text-property
2178                (- (point) 2) (1- (point)) 'face 'underline))
2179              ((eq previous ?_)
2180               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2181               (put-text-property
2182                (point) (1+ (point)) 'face 'underline)))))))))
2183
2184 (defun article-treat-ansi-sequences ()
2185   "Translate ANSI SGR control sequences into overlays or extents."
2186   (interactive)
2187   (save-excursion
2188     (when (article-goto-body)
2189       (let ((inhibit-read-only t))
2190         (ansi-color-apply-on-region (point) (point-max))))))
2191
2192 (defun gnus-article-treat-unfold-headers ()
2193   "Unfold folded message headers.
2194 Only the headers that fit into the current window width will be
2195 unfolded."
2196   (interactive)
2197   (gnus-with-article-headers
2198     (let (length)
2199       (while (not (eobp))
2200         (save-restriction
2201           (mail-header-narrow-to-field)
2202           (let* ((header (buffer-string))
2203                  (unfoldable
2204                   (or (equal gnus-article-unfold-long-headers t)
2205                       (and (stringp gnus-article-unfold-long-headers)
2206                            (string-match gnus-article-unfold-long-headers
2207                                          header)))))
2208             (with-temp-buffer
2209               (insert header)
2210               (goto-char (point-min))
2211               (while (re-search-forward "\n[\t ]" nil t)
2212                 (replace-match " " t t)))
2213             (setq length (- (point-max) (point-min) 1))
2214             (when (or unfoldable
2215                       (< length (window-width)))
2216               (while (re-search-forward "\n[\t ]" nil t)
2217                 (replace-match " " t t))))
2218           (goto-char (point-max)))))))
2219
2220 (defun gnus-article-treat-fold-headers ()
2221   "Fold message headers."
2222   (interactive)
2223   (gnus-with-article-headers
2224     (while (not (eobp))
2225       (save-restriction
2226         (mail-header-narrow-to-field)
2227         (mail-header-fold-field)
2228         (goto-char (point-max))))))
2229
2230 (defun gnus-treat-smiley ()
2231   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2232   (interactive)
2233   (gnus-with-article-buffer
2234     (if (memq 'smiley gnus-article-wash-types)
2235         (gnus-delete-images 'smiley)
2236       (article-goto-body)
2237       (let ((images (smiley-region (point) (point-max))))
2238         (when images
2239           (gnus-add-wash-type 'smiley)
2240           (dolist (image images)
2241             (gnus-add-image 'smiley image)))))))
2242
2243 (defun gnus-article-remove-images ()
2244   "Remove all images from the article buffer."
2245   (interactive)
2246   (gnus-with-article-buffer
2247     (save-restriction
2248       (widen)
2249       (dolist (elem gnus-article-image-alist)
2250         (gnus-delete-images (car elem))))))
2251
2252 (autoload 'w3m-toggle-inline-images "w3m")
2253
2254 (defun gnus-article-show-images ()
2255   "Show any images that are in the HTML-rendered article buffer.
2256 This only works if the article in question is HTML."
2257   (interactive)
2258   (gnus-with-article-buffer
2259     (save-restriction
2260       (widen)
2261       (if (eq mm-text-html-renderer 'w3m)
2262           (let ((mm-inline-text-html-with-images nil))
2263             (w3m-toggle-inline-images))
2264         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2265                                                         'image-displayer))
2266           (destructuring-bind (start end function) region
2267             (funcall function (get-text-property start 'image-url)
2268                      start end)))))))
2269
2270 (defun gnus-article-treat-fold-newsgroups ()
2271   "Unfold folded message headers.
2272 Only the headers that fit into the current window width will be
2273 unfolded."
2274   (interactive)
2275   (gnus-with-article-headers
2276     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2277       (save-restriction
2278         (mail-header-narrow-to-field)
2279         (while (re-search-forward ", *" nil t)
2280           (replace-match ", " t t))
2281         (mail-header-fold-field)
2282         (goto-char (point-max))))))
2283
2284 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2285   "Value of `truncate-lines' in Gnus Article buffer.
2286 Valid values are nil, t, `head', `first', `last', an integer or a
2287 predicate.  See Info node `(gnus)Customizing Articles'."
2288   :version "23.1" ;; No Gnus
2289   :group 'gnus-article
2290   ;; :link '(custom-manual "(gnus)Customizing Articles")
2291   :type 'boolean)
2292
2293 (defun gnus-article-toggle-truncate-lines (&optional arg)
2294   "Toggle whether to fold or truncate long lines in article the buffer.
2295 If ARG is non-nil and not a number, toggle
2296 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2297 long lines if and only if arg is positive."
2298   (interactive "P")
2299   (cond
2300    ((and (numberp arg) (> arg 0))
2301     (setq gnus-article-truncate-lines t))
2302    ((numberp arg)
2303     (setq gnus-article-truncate-lines nil))
2304    (arg
2305     (setq gnus-article-truncate-lines
2306           (not gnus-article-truncate-lines))))
2307   (gnus-with-article-buffer
2308     (cond
2309      ((and (numberp arg) (> arg 0))
2310       (setq truncate-lines nil))
2311      ((numberp arg)
2312       (setq truncate-lines t)))
2313     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2314     ;; `toggle-truncate-lines' needs an argument.
2315     (toggle-truncate-lines)))
2316
2317 (defun gnus-article-treat-body-boundary ()
2318   "Place a boundary line at the end of the headers."
2319   (interactive)
2320   (when (and gnus-body-boundary-delimiter
2321              (> (length gnus-body-boundary-delimiter) 0))
2322     (gnus-with-article-headers
2323       (goto-char (point-max))
2324       (let ((start (point)))
2325         (insert "X-Boundary: ")
2326         (gnus-add-text-properties start (point) '(invisible t intangible t))
2327        (insert (let (str (max (window-width)))
2328                  (if (featurep 'xemacs)
2329                      (setq max (1- max)))
2330                  (while (>= max (length str))
2331                     (setq str (concat str gnus-body-boundary-delimiter)))
2332                  (substring str 0 max))
2333                 "\n")
2334         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2335
2336 (defun article-fill-long-lines ()
2337   "Fill lines that are wider than the window width."
2338   (interactive)
2339   (save-excursion
2340     (let ((inhibit-read-only t)
2341           (width (window-width (get-buffer-window (current-buffer)))))
2342       (save-restriction
2343         (article-goto-body)
2344         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2345           (while (not (eobp))
2346             (end-of-line)
2347             (when (>= (current-column) (min fill-column width))
2348               (narrow-to-region (min (1+ (point)) (point-max))
2349                                 (point-at-bol))
2350               (let ((goback (point-marker)))
2351                 (fill-paragraph nil)
2352                 (goto-char (marker-position goback)))
2353               (widen))
2354             (forward-line 1)))))))
2355
2356 (defun article-capitalize-sentences ()
2357   "Capitalize the first word in each sentence."
2358   (interactive)
2359   (save-excursion
2360     (let ((inhibit-read-only t)
2361           (paragraph-start "^[\n\^L]"))
2362       (article-goto-body)
2363       (while (not (eobp))
2364         (capitalize-word 1)
2365         (forward-sentence)))))
2366
2367 (defun article-remove-cr ()
2368   "Remove trailing CRs and then translate remaining CRs into LFs."
2369   (interactive)
2370   (save-excursion
2371     (let ((inhibit-read-only t))
2372       (goto-char (point-min))
2373       (while (re-search-forward "\r+$" nil t)
2374         (replace-match "" t t))
2375       (goto-char (point-min))
2376       (while (search-forward "\r" nil t)
2377         (replace-match "\n" t t)))))
2378
2379 (defun article-remove-trailing-blank-lines ()
2380   "Remove all trailing blank lines from the article."
2381   (interactive)
2382   (save-excursion
2383     (let ((inhibit-read-only t))
2384       (goto-char (point-max))
2385       (delete-region
2386        (point)
2387        (progn
2388          (while (and (not (bobp))
2389                      (looking-at "^[ \t]*$")
2390                      (not (gnus-annotation-in-region-p
2391                            (point) (point-at-eol))))
2392            (forward-line -1))
2393          (forward-line 1)
2394          (point))))))
2395
2396 (defvar gnus-face-properties-alist)
2397
2398 (defun article-display-face (&optional force)
2399   "Display any Face headers in the header."
2400   (interactive (list 'force))
2401   (let ((wash-face-p buffer-read-only))
2402     (gnus-with-article-headers
2403       ;; When displaying parts, this function can be called several times on
2404       ;; the same article, without any intended toggle semantic (as typing `W
2405       ;; D d' would have). So face deletion must occur only when we come from
2406       ;; an interactive command, that is when the *Article* buffer is
2407       ;; read-only.
2408       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2409           (gnus-delete-images 'face)
2410         (let ((from (message-fetch-field "from"))
2411               face faces)
2412           (save-current-buffer
2413             (when (and wash-face-p
2414                        (gnus-buffer-live-p gnus-original-article-buffer)
2415                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2416               (set-buffer gnus-original-article-buffer))
2417             (save-restriction
2418               (mail-narrow-to-head)
2419               (when (or force
2420                         ;; Check whether this face is censored.
2421                         (not (and gnus-article-x-face-too-ugly
2422                                   (or from
2423                                       (setq from (message-fetch-field "from")))
2424                                   (string-match gnus-article-x-face-too-ugly
2425                                                 from))))
2426                 (while (gnus-article-goto-header "Face")
2427                   (push (mail-header-field-value) faces)))))
2428           (when faces
2429             (goto-char (point-min))
2430             (let (png image)
2431               (unless (setq from (gnus-article-goto-header "from"))
2432                 (insert "From:")
2433                 (setq from (point))
2434                 (insert " [no `from' set]\n"))
2435               (while faces
2436                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2437                   (setq image
2438                         (apply 'gnus-create-image png 'png t
2439                                (cdr (assq 'png gnus-face-properties-alist))))
2440                   (goto-char from)
2441                   (when image
2442                     (gnus-add-wash-type 'face)
2443                     (gnus-add-image 'face image)
2444                     (gnus-put-image image nil 'face)))))))))))
2445
2446 (defun article-display-x-face (&optional force)
2447   "Look for an X-Face header and display it if present."
2448   (interactive (list 'force))
2449   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2450     (gnus-with-article-headers
2451       ;; Delete the old process, if any.
2452       (when (process-status "article-x-face")
2453         (delete-process "article-x-face"))
2454       ;; See the comment in `article-display-face'.
2455       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2456           ;; We have already displayed X-Faces, so we remove them
2457           ;; instead.
2458           (gnus-delete-images 'xface)
2459         ;; Display X-Faces.
2460         (let ((from (message-fetch-field "from"))
2461               x-faces face)
2462           (save-current-buffer
2463             (when (and wash-face-p
2464                        (gnus-buffer-live-p gnus-original-article-buffer)
2465                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2466               ;; If type `W f', use gnus-original-article-buffer,
2467               ;; otherwise use the current buffer because displaying
2468               ;; RFC822 parts calls this function too.
2469               (set-buffer gnus-original-article-buffer))
2470             (save-restriction
2471               (mail-narrow-to-head)
2472               (and gnus-article-x-face-command
2473                    (or force
2474                        ;; Check whether this face is censored.
2475                        (not (and gnus-article-x-face-too-ugly
2476                                  (or from
2477                                      (setq from (message-fetch-field "from")))
2478                                  (string-match gnus-article-x-face-too-ugly
2479                                                from))))
2480                    (while (gnus-article-goto-header "X-Face")
2481                      (push (mail-header-field-value) x-faces)))))
2482           (when x-faces
2483             ;; We display the face.
2484             (cond ((functionp gnus-article-x-face-command)
2485                    ;; The command is a lisp function, so we call it.
2486                    (mapc gnus-article-x-face-command x-faces))
2487                   ((stringp gnus-article-x-face-command)
2488                    ;; The command is a string, so we interpret the command
2489                    ;; as a, well, command, and fork it off.
2490                    (let ((process-connection-type nil))
2491                      (gnus-set-process-query-on-exit-flag
2492                       (start-process
2493                        "article-x-face" nil shell-file-name
2494                        shell-command-switch gnus-article-x-face-command)
2495                       nil)
2496                      ;; Sending multiple EOFs to xv doesn't work,
2497                      ;; so we only do a single external face.
2498                      (with-temp-buffer
2499                        (insert (car x-faces))
2500                        (process-send-region "article-x-face"
2501                                             (point-min) (point-max)))
2502                      (process-send-eof "article-x-face")))
2503                   (t
2504                    (error "`%s' set to `%s' is not a function"
2505                           gnus-article-x-face-command
2506                           'gnus-article-x-face-command)))))))))
2507
2508 (defun article-decode-mime-words ()
2509   "Decode all MIME-encoded words in the article."
2510   (interactive)
2511   (gnus-with-article-buffer
2512     (let ((inhibit-point-motion-hooks t)
2513           (mail-parse-charset gnus-newsgroup-charset)
2514           (mail-parse-ignored-charsets
2515            (with-current-buffer gnus-summary-buffer
2516              gnus-newsgroup-ignored-charsets)))
2517       (mail-decode-encoded-word-region (point-min) (point-max)))))
2518
2519 (defun article-decode-charset (&optional prompt)
2520   "Decode charset-encoded text in the article.
2521 If PROMPT (the prefix), prompt for a coding system to use."
2522   (interactive "P")
2523   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2524         (inhibit-read-only t)
2525         (mail-parse-charset gnus-newsgroup-charset)
2526         (mail-parse-ignored-charsets
2527          (save-excursion (condition-case nil
2528                              (set-buffer gnus-summary-buffer)
2529                            (error))
2530                          gnus-newsgroup-ignored-charsets))
2531         ct cte ctl charset format)
2532     (save-excursion
2533       (save-restriction
2534         (article-narrow-to-head)
2535         (setq ct (message-fetch-field "Content-Type" t)
2536               cte (message-fetch-field "Content-Transfer-Encoding" t)
2537               ctl (and ct (mail-header-parse-content-type ct))
2538               charset (cond
2539                        (prompt
2540                         (mm-read-coding-system "Charset to decode: "))
2541                        (ctl
2542                         (mail-content-type-get ctl 'charset)))
2543               format (and ctl (mail-content-type-get ctl 'format)))
2544         (when cte
2545           (setq cte (mail-header-strip cte)))
2546         (if (and ctl (not (string-match "/" (car ctl))))
2547             (setq ctl nil))
2548         (goto-char (point-max)))
2549       (forward-line 1)
2550       (save-restriction
2551         (narrow-to-region (point) (point-max))
2552         (when (and (eq mail-parse-charset 'gnus-decoded)
2553                    (eq (mm-body-7-or-8) '8bit))
2554           ;; The text code could have been decoded.
2555           (setq charset mail-parse-charset))
2556         (when (and (or (not ctl)
2557                        (equal (car ctl) "text/plain"))
2558                    (not format)) ;; article with format will decode later.
2559           (mm-decode-body
2560            charset (and cte (intern (downcase
2561                                      (gnus-strip-whitespace cte))))
2562            (car ctl)))))))
2563
2564 (defun article-decode-encoded-words ()
2565   "Remove encoded-word encoding from headers."
2566   (let ((inhibit-point-motion-hooks t)
2567         (mail-parse-charset gnus-newsgroup-charset)
2568         (mail-parse-ignored-charsets
2569          (save-excursion (condition-case nil
2570                              (set-buffer gnus-summary-buffer)
2571                            (error))
2572                          gnus-newsgroup-ignored-charsets))
2573         (inhibit-read-only t)
2574         end start)
2575     (goto-char (point-min))
2576     (when (search-forward "\n\n" nil 'move)
2577       (forward-line -1))
2578     (setq end (point))
2579     (while (not (bobp))
2580       (while (progn
2581                (forward-line -1)
2582                (and (not (bobp))
2583                     (memq (char-after) '(?\t ? )))))
2584       (setq start (point))
2585       (if (looking-at "\
2586 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2587 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2588           (funcall gnus-decode-address-function start end)
2589         (funcall gnus-decode-header-function start end))
2590       (goto-char (setq end start)))))
2591
2592 (defun article-decode-group-name ()
2593   "Decode group names in Newsgroups, Followup-To and Xref headers."
2594   (let ((inhibit-point-motion-hooks t)
2595         (inhibit-read-only t)
2596         (method (gnus-find-method-for-group gnus-newsgroup-name))
2597         regexp)
2598     (when (and (or gnus-group-name-charset-method-alist
2599                    gnus-group-name-charset-group-alist)
2600                (gnus-buffer-live-p gnus-original-article-buffer))
2601       (save-restriction
2602         (article-narrow-to-head)
2603         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2604           (with-current-buffer gnus-original-article-buffer
2605             (goto-char (point-min)))
2606           (setq regexp (concat "^" header
2607                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2608           (while (re-search-forward regexp nil t)
2609             (replace-match (save-match-data
2610                              (gnus-decode-newsgroups
2611                               ;; XXX how to use data in article buffer?
2612                               (with-current-buffer gnus-original-article-buffer
2613                                 (re-search-forward regexp nil t)
2614                                 (match-string 1))
2615                               gnus-newsgroup-name method))
2616                            t t nil 1))
2617           (goto-char (point-min)))))))
2618
2619 (autoload 'idna-to-unicode "idna")
2620
2621 (defun article-decode-idna-rhs ()
2622   "Decode IDNA strings in RHS in various headers in current buffer.
2623 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2624 Mail-Reply-To: and Mail-Followup-To:."
2625   (when gnus-use-idna
2626     (save-restriction
2627       (let ((inhibit-point-motion-hooks t)
2628             (inhibit-read-only t))
2629         (article-narrow-to-head)
2630         (goto-char (point-min))
2631         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2632           (let (ace unicode)
2633             (when (save-match-data
2634                     (and (setq ace (match-string 1))
2635                          (save-excursion
2636                            (and (re-search-backward "^[^ \t]" nil t)
2637                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2638                          (setq unicode (idna-to-unicode ace))))
2639               (unless (string= ace unicode)
2640                 (replace-match unicode nil nil nil 1)))))))))
2641
2642 (defun article-de-quoted-unreadable (&optional force read-charset)
2643   "Translate a quoted-printable-encoded article.
2644 If FORCE, decode the article whether it is marked as quoted-printable
2645 or not.
2646 If READ-CHARSET, ask for a coding system."
2647   (interactive (list 'force current-prefix-arg))
2648   (save-excursion
2649     (let ((inhibit-read-only t) type charset)
2650       (if (gnus-buffer-live-p gnus-original-article-buffer)
2651           (with-current-buffer gnus-original-article-buffer
2652             (setq type
2653                   (gnus-fetch-field "content-transfer-encoding"))
2654             (let* ((ct (gnus-fetch-field "content-type"))
2655                    (ctl (and ct (mail-header-parse-content-type ct))))
2656               (setq charset (and ctl
2657                                  (mail-content-type-get ctl 'charset)))
2658               (if (stringp charset)
2659                   (setq charset (intern (downcase charset)))))))
2660       (if read-charset
2661           (setq charset (mm-read-coding-system "Charset: " charset)))
2662       (unless charset
2663         (setq charset gnus-newsgroup-charset))
2664       (when (or force
2665                 (and type (let ((case-fold-search t))
2666                             (string-match "quoted-printable" type))))
2667         (article-goto-body)
2668         (quoted-printable-decode-region
2669          (point) (point-max) (mm-charset-to-coding-system charset))))))
2670
2671 (defun article-de-base64-unreadable (&optional force read-charset)
2672   "Translate a base64 article.
2673 If FORCE, decode the article whether it is marked as base64 not.
2674 If READ-CHARSET, ask for a coding system."
2675   (interactive (list 'force current-prefix-arg))
2676   (save-excursion
2677     (let ((inhibit-read-only t) type charset)
2678       (if (gnus-buffer-live-p gnus-original-article-buffer)
2679           (with-current-buffer gnus-original-article-buffer
2680             (setq type
2681                   (gnus-fetch-field "content-transfer-encoding"))
2682             (let* ((ct (gnus-fetch-field "content-type"))
2683                    (ctl (and ct (mail-header-parse-content-type ct))))
2684               (setq charset (and ctl
2685                                  (mail-content-type-get ctl 'charset)))
2686               (if (stringp charset)
2687                   (setq charset (intern (downcase charset)))))))
2688       (if read-charset
2689           (setq charset (mm-read-coding-system "Charset: " charset)))
2690       (unless charset
2691         (setq charset gnus-newsgroup-charset))
2692       (when (or force
2693                 (and type (let ((case-fold-search t))
2694                             (string-match "base64" type))))
2695         (article-goto-body)
2696         (save-restriction
2697           (narrow-to-region (point) (point-max))
2698           (base64-decode-region (point-min) (point-max))
2699           (mm-decode-coding-region
2700            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2701
2702 (eval-when-compile
2703   (require 'rfc1843))
2704
2705 (defun article-decode-HZ ()
2706   "Translate a HZ-encoded article."
2707   (interactive)
2708   (require 'rfc1843)
2709   (save-excursion
2710     (let ((inhibit-read-only t))
2711       (rfc1843-decode-region (point-min) (point-max)))))
2712
2713 (defun article-unsplit-urls ()
2714   "Remove the newlines that some other mailers insert into URLs."
2715   (interactive)
2716   (save-excursion
2717     (let ((inhibit-read-only t))
2718       (goto-char (point-min))
2719       (while (re-search-forward
2720               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2721         (replace-match "\\1\\3" t)))
2722     (when (gmm-called-interactively-p 'any)
2723       (gnus-treat-article nil))))
2724
2725 (defun article-wash-html ()
2726   "Format an HTML article."
2727   (interactive)
2728   (let ((handles nil)
2729         (buffer-read-only nil))
2730     (when (gnus-buffer-live-p gnus-original-article-buffer)
2731       (with-current-buffer gnus-original-article-buffer
2732         (setq handles (mm-dissect-buffer t t))))
2733     (article-goto-body)
2734     (delete-region (point) (point-max))
2735     (mm-enable-multibyte)
2736     (mm-inline-text-html handles)))
2737
2738 (defvar gnus-article-browse-html-temp-list nil
2739   "List of temporary files created by `gnus-article-browse-html-parts'.
2740 Internal variable.")
2741
2742 (defcustom gnus-article-browse-delete-temp 'ask
2743   "What to do with temporary files from `gnus-article-browse-html-parts'.
2744 If nil, don't delete temporary files.  If it is t, delete them on
2745 exit from the summary buffer.  If it is the symbol `file', query
2746 on each file, if it is `ask' ask once when exiting from the
2747 summary buffer."
2748   :group 'gnus-article
2749   :version "23.1" ;; No Gnus
2750   :type '(choice (const :tag "Don't delete" nil)
2751                  (const :tag "Don't ask" t)
2752                  (const :tag "Ask" ask)
2753                  (const :tag "Ask for each file" file)))
2754
2755 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2756
2757 (defun gnus-article-browse-delete-temp-files (&optional how)
2758   "Delete temp-files created by `gnus-article-browse-html-parts'."
2759   (when (and gnus-article-browse-html-temp-list
2760              (progn
2761                (or how (setq how gnus-article-browse-delete-temp))
2762                (if (eq how 'ask)
2763                    (let ((files (length gnus-article-browse-html-temp-list)))
2764                      (or (gnus-y-or-n-p
2765                           (if (= files 1)
2766                               "Delete the temporary HTML file? "
2767                             (format "Delete all %s temporary HTML files? "
2768                                     files)))
2769                          (setq gnus-article-browse-html-temp-list nil)))
2770                  how)))
2771     (dolist (file gnus-article-browse-html-temp-list)
2772       (cond ((file-directory-p file)
2773              (when (or (not (eq how 'file))
2774                        (gnus-y-or-n-p
2775                         (format
2776                          "Delete temporary HTML file(s) in directory `%s'? "
2777                          (file-name-as-directory file))))
2778                (gnus-delete-directory file)))
2779             ((file-exists-p file)
2780              (when (or (not (eq how 'file))
2781                        (gnus-y-or-n-p
2782                         (format "Delete temporary HTML file `%s'? " file)))
2783                (delete-file file)))))
2784     ;; Also remove file from the list when not deleted or if file doesn't
2785     ;; exist anymore.
2786     (setq gnus-article-browse-html-temp-list nil))
2787   gnus-article-browse-html-temp-list)
2788
2789 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2790   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2791 Return file name."
2792   (save-match-data
2793     (let (file type)
2794       (catch 'found
2795         (dolist (handle handles)
2796           (cond
2797            ((not (listp handle)))
2798            ;; Exclude broken handles that `gnus-summary-enter-digest-group'
2799            ;; may create.
2800            ((not (or (bufferp (car handle)) (stringp (car handle)))))
2801            ((equal (mm-handle-media-supertype handle) "multipart")
2802             (when (setq file (gnus-article-browse-html-save-cid-content
2803                               cid handle directory))
2804               (throw 'found file)))
2805            ((equal (concat "<" cid ">") (mm-handle-id handle))
2806             (setq file
2807                   (expand-file-name
2808                    (or (mm-handle-filename handle)
2809                        (concat
2810                         (make-temp-name "cid")
2811                         (car (rassoc (car (mm-handle-type handle))
2812                                      mailcap-mime-extensions))))
2813                    directory))
2814             (mm-save-part-to-file handle file)
2815             (throw 'found file))))))))
2816
2817 (defun gnus-article-browse-html-parts (list &optional header)
2818   "View all \"text/html\" parts from LIST.
2819 Recurse into multiparts.  The optional HEADER that should be a decoded
2820 message header will be added to the bodies of the \"text/html\" parts."
2821   ;; Internal function used by `gnus-article-browse-html-article'.
2822   (let (type file charset content cid-dir tmp-file showed)
2823     ;; Find and show the html-parts.
2824     (dolist (handle list)
2825       ;; If HTML, show it:
2826       (cond ((not (listp handle)))
2827             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2828                  (and (equal (car type) "message/external-body")
2829                       (or header
2830                           (setq file (mm-handle-filename handle)))
2831                       (or (mm-handle-cache handle)
2832                           (condition-case code
2833                               (progn (mm-extern-cache-contents handle) t)
2834                             (error
2835                              (gnus-message 3 "%s" (error-message-string code))
2836                              (when (>= gnus-verbose 3) (sit-for 2))
2837                              nil)))
2838                       (progn
2839                         (setq handle (mm-handle-cache handle)
2840                               type (mm-handle-type handle))
2841                         (equal (car type) "text/html"))))
2842              (setq charset (mail-content-type-get type 'charset)
2843                    content (mm-get-part handle))
2844              (with-temp-buffer
2845                (if (eq charset 'gnus-decoded)
2846                    (mm-enable-multibyte)
2847                  (mm-disable-multibyte))
2848                (insert content)
2849                ;; resolve cid contents
2850                (let ((case-fold-search t)
2851                      cid-file)
2852                  (goto-char (point-min))
2853                  (while (re-search-forward "\
2854 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2855                                            nil t)
2856                    (unless cid-dir
2857                      (setq cid-dir (mm-make-temp-file "cid" t))
2858                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2859                    (setq file nil
2860                          content nil)
2861                    (when (setq cid-file
2862                                (gnus-article-browse-html-save-cid-content
2863                                 (match-string 2)
2864                                 (with-current-buffer gnus-article-buffer
2865                                   gnus-article-mime-handles)
2866                                 cid-dir))
2867                      (when (eq system-type 'cygwin)
2868                        (setq cid-file
2869                              (concat "/" (substring
2870                                           (with-output-to-string
2871                                             (call-process "cygpath" nil
2872                                                           standard-output
2873                                                           nil "-m" cid-file))
2874                                           0 -1))))
2875                      (replace-match (concat "file://" cid-file)
2876                                     nil nil nil 1))))
2877                (unless content (setq content (buffer-string))))
2878              (when (or charset header (not file))
2879                (setq tmp-file (mm-make-temp-file
2880                                ;; Do we need to care for 8.3 filenames?
2881                                "mm-" nil ".html")))
2882              ;; Add a meta html tag to specify charset and a header.
2883              (cond
2884               (header
2885                (let (title eheader body hcharset coding)
2886                  (with-temp-buffer
2887                    (mm-enable-multibyte)
2888                    (setq case-fold-search t)
2889                    (insert header "\n")
2890                    (setq title (message-fetch-field "subject"))
2891                    (goto-char (point-min))
2892                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|\\(&\\)\\|\n"
2893                                              nil t)
2894                      (replace-match (cond ((match-beginning 1) "&lt;")
2895                                           ((match-beginning 2) "&gt;")
2896                                           ((match-beginning 3) "&amp;")
2897                                           (t "<br>\n"))))
2898                    (goto-char (point-min))
2899                    (while (re-search-forward "^[\t ]+" nil t)
2900                      (dotimes (i (prog1
2901                                      (current-column)
2902                                    (delete-region (match-beginning 0)
2903                                                   (match-end 0))))
2904                        (insert "&nbsp;")))
2905                    (goto-char (point-min))
2906                    (insert "<div align=\"left\">\n")
2907                    (goto-char (point-max))
2908                    (insert "</div>\n<hr>\n")
2909                    ;; We have to examine charset one by one since
2910                    ;; charset specified in parts might be different.
2911                    (if (eq charset 'gnus-decoded)
2912                        (setq charset 'utf-8
2913                              eheader (mm-encode-coding-string (buffer-string)
2914                                                               charset)
2915                              title (when title
2916                                      (mm-encode-coding-string title charset))
2917                              body (mm-encode-coding-string content charset))
2918                      (setq hcharset (mm-find-mime-charset-region (point-min)
2919                                                                  (point-max)))
2920                      (cond ((= (length hcharset) 1)
2921                             (setq hcharset (car hcharset)
2922                                   coding (mm-charset-to-coding-system
2923                                           hcharset nil t)))
2924                            ((> (length hcharset) 1)
2925                             (setq hcharset 'utf-8
2926                                   coding hcharset)))
2927                      (if coding
2928                          (if charset
2929                              (progn
2930                                (setq body
2931                                      (mm-charset-to-coding-system charset
2932                                                                   nil t))
2933                                (if (eq coding body)
2934                                    (setq eheader (mm-encode-coding-string
2935                                                   (buffer-string) coding)
2936                                          title (when title
2937                                                  (mm-encode-coding-string
2938                                                   title coding))
2939                                          body content)
2940                                  (setq charset 'utf-8
2941                                        eheader (mm-encode-coding-string
2942                                                 (buffer-string) charset)
2943                                        title (when title
2944                                                (mm-encode-coding-string
2945                                                 title charset))
2946                                        body (mm-encode-coding-string
2947                                              (mm-decode-coding-string
2948                                               content body)
2949                                              charset))))
2950                            (setq charset hcharset
2951                                  eheader (mm-encode-coding-string
2952                                           (buffer-string) coding)
2953                                  title (when title
2954                                          (mm-encode-coding-string
2955                                           title coding))
2956                                  body content))
2957                        (setq eheader (mm-string-as-unibyte (buffer-string))
2958                              body content)))
2959                    (erase-buffer)
2960                    (mm-disable-multibyte)
2961                    (insert body)
2962                    (when charset
2963                      (mm-add-meta-html-tag handle charset t))
2964                    (when title
2965                      (goto-char (point-min))
2966                      (unless (search-forward "<title>" nil t)
2967                        (re-search-forward "<head>\\s-*" nil t)
2968                        (insert "<title>" title "</title>\n")))
2969                    (goto-char (point-min))
2970                    (or (re-search-forward
2971                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2972                        (re-search-forward
2973                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2974                    (insert eheader)
2975                    (mm-write-region (point-min) (point-max)
2976                                     tmp-file nil nil nil 'binary t))))
2977               (charset
2978                (mm-with-unibyte-buffer
2979                  (insert (if (eq charset 'gnus-decoded)
2980                              (mm-encode-coding-string content
2981                                                       (setq charset 'utf-8))
2982                            content))
2983                  (if (or (mm-add-meta-html-tag handle charset)
2984                          (not file))
2985                      (mm-write-region (point-min) (point-max)
2986                                       tmp-file nil nil nil 'binary t)
2987                    (setq tmp-file nil))))
2988               (tmp-file
2989                (mm-save-part-to-file handle tmp-file)))
2990              (when tmp-file
2991                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2992              (add-hook 'gnus-summary-prepare-exit-hook
2993                        'gnus-article-browse-delete-temp-files)
2994              (add-hook 'gnus-exit-gnus-hook
2995                        (lambda  ()
2996                          (gnus-article-browse-delete-temp-files t)))
2997              ;; FIXME: Warn if there's an <img> tag?
2998              (browse-url-of-file (or tmp-file (expand-file-name file)))
2999              (setq showed t))
3000             ;; If multipart, recurse
3001             ((equal (mm-handle-media-supertype handle) "multipart")
3002              (when (gnus-article-browse-html-parts handle header)
3003                (setq showed t)))
3004             ((equal (mm-handle-media-type handle) "message/rfc822")
3005              (mm-with-multibyte-buffer
3006                (mm-insert-part handle)
3007                (setq handle (mm-dissect-buffer t t))
3008                (when (and (bufferp (car handle))
3009                           (stringp (car (mm-handle-type handle))))
3010                  (setq handle (list handle)))
3011                (when header
3012                  (article-decode-encoded-words)
3013                  (let ((gnus-visible-headers
3014                         (or (get 'gnus-visible-headers 'standard-value)
3015                             gnus-visible-headers)))
3016                    (article-hide-headers))
3017                  (goto-char (point-min))
3018                  (search-forward "\n\n" nil 'move)
3019                  (skip-chars-backward "\t\n ")
3020                  (setq header (buffer-substring (point-min) (point)))))
3021              (when (prog1
3022                        (gnus-article-browse-html-parts handle header)
3023                      (mm-destroy-parts handle))
3024                (setq showed t)))))
3025     showed))
3026
3027 (defun gnus-article-browse-html-article (&optional arg)
3028   "View \"text/html\" parts of the current article with a WWW browser.
3029 Inline images embedded in a message using the cid scheme, as they are
3030 generally considered to be safe, will be processed properly.
3031 The message header is added to the beginning of every html part unless
3032 the prefix argument ARG is given.
3033
3034 Warning: Spammers use links to images (using the http scheme) in HTML
3035 articles to verify whether you have read the message.  As
3036 `gnus-article-browse-html-article' passes the HTML content to the
3037 browser without eliminating these \"web bugs\" you should only
3038 use it for mails from trusted senders.
3039
3040 If you always want to display HTML parts in the browser, set
3041 `mm-text-html-renderer' to nil.
3042
3043 This command creates temporary files to pass HTML contents including
3044 images if any to the browser, and deletes them when exiting the group
3045 \(if you want)."
3046   ;; Cf. `mm-w3m-safe-url-regexp'
3047   (interactive "P")
3048   (if arg
3049       (gnus-summary-show-article)
3050     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3051                                     gnus-visible-headers))
3052           ;; As we insert a <hr>, there's no need for the body boundary.
3053           (gnus-treat-body-boundary nil))
3054       (gnus-summary-show-article)))
3055   (with-current-buffer gnus-article-buffer
3056     (let ((header (unless arg
3057                     (save-restriction
3058                       (widen)
3059                       (buffer-substring-no-properties
3060                        (goto-char (point-min))
3061                        (if (search-forward "\n\n" nil t)
3062                            (match-beginning 0)
3063                          (goto-char (point-max))
3064                          (skip-chars-backward "\t\n ")
3065                          (point))))))
3066           parts)
3067       (set-buffer gnus-original-article-buffer)
3068       (setq parts (mm-dissect-buffer t t))
3069       ;; If singlepart, enforce a list.
3070       (when (and (bufferp (car parts))
3071                  (stringp (car (mm-handle-type parts))))
3072         (setq parts (list parts)))
3073       ;; Process the list
3074       (unless (gnus-article-browse-html-parts parts header)
3075         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3076       (mm-destroy-parts parts)
3077       (unless arg
3078         (gnus-summary-show-article)))))
3079
3080 (defun article-hide-list-identifiers ()
3081   "Remove list identifies from the Subject header.
3082 The `gnus-list-identifiers' variable specifies what to do."
3083   (interactive)
3084   (let ((inhibit-point-motion-hooks t)
3085         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3086         (inhibit-read-only t))
3087     (when regexp
3088       (save-excursion
3089         (save-restriction
3090           (article-narrow-to-head)
3091           (goto-char (point-min))
3092           (while (re-search-forward
3093                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3094                   nil t)
3095             (delete-region (match-beginning 2) (match-end 0))
3096             (beginning-of-line))
3097           (when (re-search-forward
3098                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3099             (delete-region (match-beginning 1) (match-end 1))))))))
3100
3101 (defun article-hide-pem (&optional arg)
3102   "Toggle hiding of any PEM headers and signatures in the current article.
3103 If given a negative prefix, always show; if given a positive prefix,
3104 always hide."
3105   (interactive (gnus-article-hidden-arg))
3106   (unless (gnus-article-check-hidden-text 'pem arg)
3107     (save-excursion
3108       (let ((inhibit-read-only t) end)
3109         (goto-char (point-min))
3110         ;; Hide the horrendously ugly "header".
3111         (when (and (search-forward
3112                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3113                     nil t)
3114                    (setq end (1+ (match-beginning 0))))
3115           (gnus-add-wash-type 'pem)
3116           (gnus-article-hide-text-type
3117            end
3118            (if (search-forward "\n\n" nil t)
3119                (match-end 0)
3120              (point-max))
3121            'pem)
3122           ;; Hide the trailer as well
3123           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3124                                 nil t)
3125             (gnus-article-hide-text-type
3126              (match-beginning 0) (match-end 0) 'pem)))))))
3127
3128 (defun article-strip-banner ()
3129   "Strip the banners specified by the `banner' group parameter and by
3130 `gnus-article-address-banner-alist'."
3131   (interactive)
3132   (save-excursion
3133     (save-restriction
3134       (let ((inhibit-point-motion-hooks t))
3135         (when (gnus-parameter-banner gnus-newsgroup-name)
3136           (article-really-strip-banner
3137            (gnus-parameter-banner gnus-newsgroup-name)))
3138         (when gnus-article-address-banner-alist
3139           ;; Note that the From header is decoded here, so it is
3140           ;; required that the *-extract-address-components function
3141           ;; supports non-ASCII text.
3142           (let ((from (save-restriction
3143                         (widen)
3144                         (article-narrow-to-head)
3145                         (mail-fetch-field "from"))))
3146             (when (and from
3147                        (setq from
3148                              (cadr (funcall gnus-extract-address-components
3149                                             from))))
3150               (catch 'found
3151                 (dolist (pair gnus-article-address-banner-alist)
3152                   (when (string-match (car pair) from)
3153                     (throw 'found
3154                            (article-really-strip-banner (cdr pair)))))))))))))
3155
3156 (defun article-really-strip-banner (banner)
3157   "Strip the banner specified by the argument."
3158   (save-excursion
3159     (save-restriction
3160       (let ((inhibit-point-motion-hooks t)
3161             (gnus-signature-limit nil)
3162             (inhibit-read-only t))
3163         (article-goto-body)
3164         (cond
3165          ((eq banner 'signature)
3166           (when (gnus-article-narrow-to-signature)
3167             (widen)
3168             (forward-line -1)
3169             (delete-region (point) (point-max))))
3170          ((symbolp banner)
3171           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3172               (while (re-search-forward banner nil t)
3173                 (delete-region (match-beginning 0) (match-end 0)))))
3174          ((stringp banner)
3175           (while (re-search-forward banner nil t)
3176             (delete-region (match-beginning 0) (match-end 0)))))))))
3177
3178 (defun article-babel ()
3179   "Translate article using an online translation service."
3180   (interactive)
3181   (require 'babel)
3182   (gnus-with-article-buffer
3183     (when (article-goto-body)
3184       (let* ((start (point))
3185              (end (point-max))
3186              (orig (buffer-substring start end))
3187              (trans (babel-as-string orig)))
3188         (save-restriction
3189           (narrow-to-region start end)
3190           (delete-region start end)
3191           (insert trans))))))
3192
3193 (defun article-hide-signature (&optional arg)
3194   "Hide the signature in the current article.
3195 If given a negative prefix, always show; if given a positive prefix,
3196 always hide."
3197   (interactive (gnus-article-hidden-arg))
3198   (unless (gnus-article-check-hidden-text 'signature arg)
3199     (save-excursion
3200       (save-restriction
3201         (let ((inhibit-read-only t))
3202           (when (gnus-article-narrow-to-signature)
3203             (gnus-article-hide-text-type
3204              (point-min) (point-max) 'signature))))))
3205   (gnus-set-mode-line 'article))
3206
3207 (defun article-strip-headers-in-body ()
3208   "Strip offensive headers from bodies."
3209   (interactive)
3210   (save-excursion
3211     (article-goto-body)
3212     (let ((case-fold-search t))
3213       (when (looking-at "x-no-archive:")
3214         (gnus-delete-line)))))
3215
3216 (defun article-strip-leading-blank-lines ()
3217   "Remove all blank lines from the beginning of the article."
3218   (interactive)
3219   (save-excursion
3220     (let ((inhibit-point-motion-hooks t)
3221           (inhibit-read-only t))
3222       (when (article-goto-body)
3223         (while (and (not (eobp))
3224                     (looking-at "[ \t]*$"))
3225           (gnus-delete-line))))))
3226
3227 (defun article-narrow-to-head ()
3228   "Narrow the buffer to the head of the message.
3229 Point is left at the beginning of the narrowed-to region."
3230   (narrow-to-region
3231    (goto-char (point-min))
3232    (cond
3233     ;; Absolutely no headers displayed.
3234     ((looking-at "\n")
3235      (point))
3236     ;; Normal headers.
3237     ((search-forward "\n\n" nil 1)
3238      (1- (point)))
3239     ;; Nothing but headers.
3240     (t
3241      (point-max))))
3242   (goto-char (point-min)))
3243
3244 (defun article-goto-body ()
3245   "Place point at the start of the body."
3246   (goto-char (point-min))
3247   (cond
3248    ;; This variable is only bound when dealing with separate
3249    ;; MIME body parts.
3250    (article-goto-body-goes-to-point-min-p
3251     t)
3252    ((search-forward "\n\n" nil t)
3253     t)
3254    (t
3255     (goto-char (point-max))
3256     nil)))
3257
3258 (defun article-strip-multiple-blank-lines ()
3259   "Replace consecutive blank lines with one empty line."
3260   (interactive)
3261   (save-excursion
3262     (let ((inhibit-point-motion-hooks t)
3263           (inhibit-read-only t))
3264       ;; First make all blank lines empty.
3265       (article-goto-body)
3266       (while (re-search-forward "^[ \t]+$" nil t)
3267         (unless (gnus-annotation-in-region-p
3268                  (match-beginning 0) (match-end 0))
3269           (replace-match "" nil t)))
3270       ;; Then replace multiple empty lines with a single empty line.
3271       (article-goto-body)
3272       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3273         (unless (gnus-annotation-in-region-p
3274                  (match-beginning 0) (match-end 0))
3275           (delete-region (match-beginning 1) (match-end 1)))))))
3276
3277 (defun article-strip-leading-space ()
3278   "Remove all white space from the beginning of the lines in the article."
3279   (interactive)
3280   (save-excursion
3281     (let ((inhibit-point-motion-hooks t)
3282           (inhibit-read-only t))
3283       (article-goto-body)
3284       (while (re-search-forward "^[ \t]+" nil t)
3285         (replace-match "" t t)))))
3286
3287 (defun article-strip-trailing-space ()
3288   "Remove all white space from the end of the lines in the article."
3289   (interactive)
3290   (save-excursion
3291     (let ((inhibit-point-motion-hooks t)
3292           (inhibit-read-only t))
3293       (article-goto-body)
3294       (while (re-search-forward "[ \t]+$" nil t)
3295         (replace-match "" t t)))))
3296
3297 (defun article-strip-blank-lines ()
3298   "Strip leading, trailing and multiple blank lines."
3299   (interactive)
3300   (article-strip-leading-blank-lines)
3301   (article-remove-trailing-blank-lines)
3302   (article-strip-multiple-blank-lines))
3303
3304 (defun article-strip-all-blank-lines ()
3305   "Strip all blank lines."
3306   (interactive)
3307   (save-excursion
3308     (let ((inhibit-point-motion-hooks t)
3309           (inhibit-read-only t))
3310       (article-goto-body)
3311       (while (re-search-forward "^[ \t]*\n" nil t)
3312         (replace-match "" t t)))))
3313
3314 (defun gnus-article-narrow-to-signature ()
3315   "Narrow to the signature; return t if a signature is found, else nil."
3316   (let ((inhibit-point-motion-hooks t))
3317     (when (gnus-article-search-signature)
3318       (forward-line 1)
3319       ;; Check whether we have some limits to what we consider
3320       ;; to be a signature.
3321       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3322                       (list gnus-signature-limit)))
3323             limit limited)
3324         (while (setq limit (pop limits))
3325           (if (or (and (integerp limit)
3326                        (< (- (point-max) (point)) limit))
3327                   (and (floatp limit)
3328                        (< (count-lines (point) (point-max)) limit))
3329                   (and (functionp limit)
3330                        (funcall limit))
3331                   (and (stringp limit)
3332                        (not (re-search-forward limit nil t))))
3333               ()                        ; This limit did not succeed.
3334             (setq limited t
3335                   limits nil)))
3336         (unless limited
3337           (narrow-to-region (point) (point-max))
3338           t)))))
3339
3340 (defun gnus-article-search-signature ()
3341   "Search the current buffer for the signature separator.
3342 Put point at the beginning of the signature separator."
3343   (let ((cur (point)))
3344     (goto-char (point-max))
3345     (if (if (stringp gnus-signature-separator)
3346             (re-search-backward gnus-signature-separator nil t)
3347           (let ((seps gnus-signature-separator))
3348             (while (and seps
3349                         (not (re-search-backward (car seps) nil t)))
3350               (pop seps))
3351             seps))
3352         t
3353       (goto-char cur)
3354       nil)))
3355
3356 (defun gnus-article-hidden-arg ()
3357   "Return the current prefix arg as a number, or 0 if no prefix."
3358   (list (if current-prefix-arg
3359             (prefix-numeric-value current-prefix-arg)
3360           0)))
3361
3362 (defun gnus-article-check-hidden-text (type arg)
3363   "Return nil if hiding is necessary.
3364 Arg can be nil or a number.  nil and positive means hide, negative
3365 means show, 0 means toggle."
3366   (save-excursion
3367     (save-restriction
3368       (let ((hide (gnus-article-hidden-text-p type)))
3369         (cond
3370          ((or (null arg)
3371               (> arg 0))
3372           nil)
3373          ((< arg 0)
3374           (gnus-article-show-hidden-text type)
3375           t)
3376          (t
3377           (if (eq hide 'hidden)
3378               (progn
3379                 (gnus-article-show-hidden-text type)
3380                 t)
3381             nil)))))))
3382
3383 (defun gnus-article-hidden-text-p (type)
3384   "Say whether the current buffer contains hidden text of type TYPE."
3385   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3386     (while (and pos
3387                 (not (get-text-property pos 'invisible))
3388                 (not (get-text-property pos 'dummy-invisible)))
3389       (setq pos
3390             (text-property-any (1+ pos) (point-max) 'article-type type)))
3391     (if pos
3392         'hidden
3393       nil)))
3394
3395 (defun gnus-article-show-hidden-text (type &optional dummy)
3396   "Show all hidden text of type TYPE.
3397 Originally it is hide instead of DUMMY."
3398   (let ((inhibit-read-only t)
3399         (inhibit-point-motion-hooks t))
3400     (gnus-remove-text-properties-when
3401      'article-type type
3402      (point-min) (point-max)
3403      (cons 'article-type (cons type
3404                                gnus-hidden-properties)))
3405     (gnus-delete-wash-type type)))
3406
3407 (defconst article-time-units
3408   `((year . ,(* 365.25 24 60 60))
3409     (week . ,(* 7 24 60 60))
3410     (day . ,(* 24 60 60))
3411     (hour . ,(* 60 60))
3412     (minute . 60)
3413     (second . 1))
3414   "Mapping from time units to seconds.")
3415
3416 (defun gnus-article-forward-header ()
3417   "Move point to the start of the next header.
3418 If the current header is a continuation header, this can be several
3419 lines forward."
3420   (let ((ended nil))
3421     (while (not ended)
3422       (forward-line 1)
3423       (if (looking-at "[ \t]+[^ \t]")
3424           (forward-line 1)
3425         (setq ended t)))))
3426
3427 (defun article-treat-date ()
3428   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3429                        (with-current-buffer gnus-summary-buffer
3430                          gnus-article-date-headers)
3431                      gnus-article-date-headers)
3432                    t))
3433
3434 (defun article-date-ut (&optional type highlight date-position)
3435   "Convert DATE date to TYPE in the current article.
3436 The default type is `ut'.  See `gnus-article-date-headers' for
3437 possible values."
3438   (interactive (list 'ut t))
3439   (let* ((case-fold-search t)
3440          (inhibit-read-only t)
3441          (inhibit-point-motion-hooks t)
3442          (first t)
3443          (visible-date (mail-fetch-field "Date"))
3444          pos date bface eface)
3445     (save-excursion
3446       (if date-position
3447           (progn
3448             (goto-char date-position)
3449             (setq date (get-text-property (point) 'original-date))
3450             (when (looking-at "[^:]+:[\t ]*")
3451               (setq bface (get-text-property (match-beginning 0) 'face)
3452                     eface (get-text-property (match-end 0) 'face)))
3453             (delete-region (point)
3454                            (progn
3455                              (gnus-article-forward-header)
3456                              (point)))
3457             (article-transform-date date type bface eface))
3458         (save-restriction
3459           (widen)
3460           (goto-char (point-min))
3461           (while (or (get-text-property (setq pos (point)) 'original-date)
3462                      (and (setq pos (next-single-property-change
3463                                      (point) 'original-date))
3464                           (goto-char pos)))
3465             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3466                                       (1+ (match-beginning 0))
3467                                     (point-max)))
3468             (while (setq pos (text-property-not-all pos (point-max)
3469                                                     'gnus-date-type nil))
3470               (setq date (get-text-property pos 'original-date))
3471               (goto-char pos)
3472               (when (looking-at "[^:]+:[\t ]*")
3473                 (setq bface (get-text-property (match-beginning 0) 'face)
3474                       eface (get-text-property (match-end 0) 'face)))
3475               (delete-region pos (or (text-property-any pos (point-max)
3476                                                         'gnus-date-type nil)
3477                                      (point-max))))
3478             (unless date ;; the 1st time
3479               (goto-char (point-min))
3480               (while (re-search-forward "^Date:[\t ]*" nil t)
3481                 (setq date (get-text-property (match-beginning 0)
3482                                               'original-date)
3483                       bface (get-text-property (match-beginning 0) 'face)
3484                       eface (get-text-property (match-end 0) 'face))
3485                 (delete-region (point-at-bol) (progn
3486                                                 (gnus-article-forward-header)
3487                                                 (point)))))
3488             (when (and (not date)
3489                        visible-date)
3490               (setq date visible-date))
3491             (when date
3492               (article-transform-date date type bface eface))
3493             (goto-char (point-max))
3494             (widen)))))))
3495
3496 (defun article-transform-date (date type bface eface)
3497   (dolist (this-type (cond
3498                       ((null type)
3499                        (list 'ut))
3500                       ((atom type)
3501                        (list type))
3502                       (t
3503                        type)))
3504     (goto-char
3505      (prog1
3506          (point)
3507        (add-text-properties
3508         (point)
3509         (progn
3510           (insert (article-make-date-line date (or this-type 'ut)) "\n")
3511           (point))
3512         (list 'original-date date 'gnus-date-type this-type))))
3513     ;; Do highlighting.
3514     (when (looking-at
3515            "\\([^:]+:\\)[\t ]*\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?")
3516       (put-text-property (match-beginning 1) (match-end 1) 'face bface)
3517       (when (match-beginning 2)
3518         (put-text-property (match-beginning 2) (match-end 2) 'face eface))
3519       (while (and (zerop (forward-line 1))
3520                   (looking-at "[\t ]+\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?"))
3521         (when (match-beginning 1)
3522           (put-text-property (match-beginning 1) (match-end 1) 'face eface))))))
3523
3524 (defun article-make-date-line (date type)
3525   "Return a DATE line of TYPE."
3526   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3527                              combined-lapsed))
3528     (error "Unknown conversion type: %s" type))
3529   (condition-case ()
3530       (let ((time (ignore-errors (date-to-time date))))
3531         (cond
3532          ;; Convert to the local timezone.
3533          ((eq type 'local)
3534           (concat "Date: " (message-make-date time)))
3535          ;; Convert to Universal Time.
3536          ((eq type 'ut)
3537           (concat "Date: "
3538                   (substring
3539                    (message-make-date
3540                     (let* ((e (parse-time-string date))
3541                            (tm (apply 'encode-time e))
3542                            (ms (car tm))
3543                            (ls (- (cadr tm) (car (current-time-zone time)))))
3544                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3545                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3546                             (t (list ms ls)))))
3547                    0 -5)
3548                   "UT"))
3549          ;; Get the original date from the article.
3550          ((eq type 'original)
3551           (concat "Date: " (if (string-match "\n+$" date)
3552                                (substring date 0 (match-beginning 0))
3553                              date)))
3554          ;; Let the user define the format.
3555          ((eq type 'user-defined)
3556           (let ((format (or (condition-case nil
3557                                 (with-current-buffer gnus-summary-buffer
3558                                   gnus-article-time-format)
3559                               (error nil))
3560                             gnus-article-time-format)))
3561             (if (functionp format)
3562                 (funcall format time)
3563               (concat "Date: " (format-time-string format time)))))
3564          ;; ISO 8601.
3565          ((eq type 'iso8601)
3566           (let ((tz (car (current-time-zone time))))
3567             (concat
3568              "Date: "
3569              (format-time-string "%Y%m%dT%H%M%S" time)
3570              (format "%s%02d%02d"
3571                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3572                      (/ (% (abs tz) 3600) 60)))))
3573          ;; Do a lapsed format.
3574          ((eq type 'lapsed)
3575           (concat "Date: " (article-lapsed-string time)))
3576          ;; A combined date/lapsed format.
3577          ((eq type 'combined-lapsed)
3578           (let ((date-string (article-make-date-line date 'original))
3579                 (segments 3)
3580                 lapsed-string)
3581             (while (and
3582                     time
3583                     (setq lapsed-string
3584                           (concat " (" (article-lapsed-string time segments) ")"))
3585                     (> (+ (length date-string)
3586                           (length lapsed-string))
3587                        (+ fill-column 6))
3588                     (> segments 0))
3589               (setq segments (1- segments)))
3590             (if (> segments 0)
3591                 (concat date-string lapsed-string)
3592               date-string)))
3593          ;; Display the date in proper English
3594          ((eq type 'english)
3595           (let ((dtime (decode-time time)))
3596             (concat
3597              "Date: the "
3598              (number-to-string (nth 3 dtime))
3599              (let ((digit (% (nth 3 dtime) 10)))
3600                (cond
3601                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3602                 ((= digit 1) "st")
3603                 ((= digit 2) "nd")
3604                 ((= digit 3) "rd")
3605                 (t "th")))
3606              " of "
3607              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3608              " "
3609              (number-to-string (nth 5 dtime))
3610              " at "
3611              (format "%02d" (nth 2 dtime))
3612              ":"
3613              (format "%02d" (nth 1 dtime)))))))
3614     (foo
3615      (format "Date: %s (from Gnus)" date))))
3616
3617 (defun article-lapsed-string (time &optional max-segments)
3618   ;; If the date is seriously mangled, the timezone functions are
3619   ;; liable to bug out, so we ignore all errors.
3620   (let* ((now (current-time))
3621          (real-time (subtract-time now time))
3622          (real-sec (and real-time
3623                         (+ (* (float (car real-time)) 65536)
3624                            (cadr real-time))))
3625          (sec (and real-time (abs real-sec)))
3626          (segments 0)
3627          num prev)
3628     (unless max-segments
3629       (setq max-segments (length article-time-units)))
3630     (cond
3631      ((null real-time)
3632       "Unknown")
3633      ((zerop sec)
3634       "Now")
3635      (t
3636       (concat
3637        ;; This is a bit convoluted, but basically we go
3638        ;; through the time units for years, weeks, etc,
3639        ;; and divide things to see whether that results
3640        ;; in positive answers.
3641        (mapconcat
3642         (lambda (unit)
3643           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3644                   (>= segments max-segments))
3645               ;; The (remaining) seconds are too few to
3646               ;; be divided into this time unit.
3647               ""
3648             ;; It's big enough, so we output it.
3649             (setq sec (- sec (* num (cdr unit))))
3650             (prog1
3651                 (concat (if prev ", " "") (int-to-string
3652                                            (floor num))
3653                         " " (symbol-name (car unit))
3654                         (if (> num 1) "s" ""))
3655               (setq prev t
3656                     segments (1+ segments)))))
3657         article-time-units "")
3658        ;; If dates are odd, then it might appear like the
3659        ;; article was sent in the future.
3660        (if (> real-sec 0)
3661            " ago"
3662          " in the future"))))))
3663
3664 (defun article-date-local (&optional highlight)
3665   "Convert the current article date to the local timezone."
3666   (interactive (list t))
3667   (article-date-ut 'local highlight))
3668
3669 (defun article-date-english (&optional highlight)
3670   "Convert the current article date to something that is proper English."
3671   (interactive (list t))
3672   (article-date-ut 'english highlight))
3673
3674 (defun article-date-original (&optional highlight)
3675   "Convert the current article date to what it was originally.
3676 This is only useful if you have used some other date conversion
3677 function and want to see what the date was before converting."
3678   (interactive (list t))
3679   (article-date-ut 'original highlight))
3680
3681 (defun article-date-lapsed (&optional highlight)
3682   "Convert the current article date to time lapsed since it was sent."
3683   (interactive (list t))
3684   (article-date-ut 'lapsed highlight))
3685
3686 (defun article-date-combined-lapsed (&optional highlight)
3687   "Convert the current article date to time lapsed since it was sent."
3688   (interactive (list t))
3689   (article-date-ut 'combined-lapsed highlight))
3690
3691 (defun article-update-date-lapsed ()
3692   "Function to be run from a timer to update the lapsed time line."
3693   (save-match-data
3694     (let ((buffer (current-buffer)))
3695       (ignore-errors
3696         (walk-windows
3697          (lambda (w)
3698            (set-buffer (window-buffer w))
3699            (when (derived-mode-p 'gnus-article-mode)
3700              (let ((old-line (count-lines (point-min) (point)))
3701                    (old-column (- (point) (line-beginning-position)))
3702                    (window-start (window-start w))
3703                    (pos (point-min))
3704                    type next end)
3705                (while (setq pos (text-property-not-all pos (point-max)
3706                                                        'gnus-date-type nil))
3707                  (setq next (or (next-single-property-change pos
3708                                                              'gnus-date-type)
3709                                 (point-max)))
3710                  (setq type (get-text-property pos 'gnus-date-type))
3711                  (when (memq type '(lapsed combined-lapsed user-defined))
3712                    (article-date-ut type t pos)
3713                    (setq end (or (next-single-property-change pos
3714                                                               'gnus-date-type)
3715                                  (point-max)))
3716                    (when window-start
3717                      (if (/= window-start next)
3718                          (setq window-start nil)
3719                        (set-window-start w end)))
3720                    (setq next end))
3721                  (setq pos next))
3722                (goto-char (point-min))
3723                (when (> old-column 0)
3724                  (setq old-line (1- old-line)))
3725                (forward-line old-line)
3726                (end-of-line)
3727                (when (> (current-column) old-column)
3728                  (beginning-of-line)
3729                  (forward-char old-column)))))
3730          nil 'visible))
3731       (set-buffer buffer))))
3732
3733 (defun gnus-start-date-timer (&optional n)
3734   "Start a timer to update the Date headers in the article buffers.
3735 The numerical prefix says how frequently (in seconds) the function
3736 is to run."
3737   (interactive "p")
3738   (unless n
3739     (setq n 1))
3740   (gnus-stop-date-timer)
3741   (setq article-lapsed-timer
3742         (run-at-time 1 n 'article-update-date-lapsed)))
3743
3744 (defun gnus-stop-date-timer ()
3745   "Stop the Date timer."
3746   (interactive)
3747   (when article-lapsed-timer
3748     (nnheader-cancel-timer article-lapsed-timer)
3749     (setq article-lapsed-timer nil)))
3750
3751 (defun article-date-user (&optional highlight)
3752   "Convert the current article date to the user-defined format.
3753 This format is defined by the `gnus-article-time-format' variable."
3754   (interactive (list t))
3755   (article-date-ut 'user highlight))
3756
3757 (defun article-date-iso8601 (&optional highlight)
3758   "Convert the current article date to ISO8601."
3759   (interactive (list t))
3760   (article-date-ut 'iso8601 highlight))
3761
3762 (defmacro gnus-article-save-original-date (&rest forms)
3763   "Save the original date as a text property and evaluate FORMS."
3764   `(let* ((case-fold-search t)
3765           (start (progn
3766                    (goto-char (point-min))
3767                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3768                               (not (bolp)))
3769                      (match-end 0))))
3770           (date (when (and start
3771                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3772                                               nil t))
3773                   (buffer-substring-no-properties start
3774                                                   (match-beginning 0)))))
3775      (goto-char (point-max))
3776      (skip-chars-backward "\n")
3777      (put-text-property (point-min) (point) 'original-date date)
3778      ,@forms
3779      (goto-char (point-max))
3780      (skip-chars-backward "\n")
3781      (put-text-property (point-min) (point) 'original-date date)))
3782
3783 ;; (defun article-show-all ()
3784 ;;   "Show all hidden text in the article buffer."
3785 ;;   (interactive)
3786 ;;   (save-excursion
3787 ;;     (let ((inhibit-read-only t))
3788 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3789
3790 (defun article-remove-leading-whitespace ()
3791   "Remove excessive whitespace from all headers."
3792   (interactive)
3793   (save-excursion
3794     (save-restriction
3795       (let ((inhibit-read-only t))
3796         (article-narrow-to-head)
3797         (goto-char (point-min))
3798         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3799           (delete-region (match-beginning 1) (match-end 1)))))))
3800
3801 (defun article-emphasize (&optional arg)
3802   "Emphasize text according to `gnus-emphasis-alist'."
3803   (interactive (gnus-article-hidden-arg))
3804   (unless (gnus-article-check-hidden-text 'emphasis arg)
3805     (save-excursion
3806       (let ((alist (or
3807                     (condition-case nil
3808                         (with-current-buffer gnus-summary-buffer
3809                           gnus-article-emphasis-alist)
3810                       (error))
3811                     gnus-emphasis-alist))
3812             (inhibit-read-only t)
3813             (props (append '(article-type emphasis)
3814                            gnus-hidden-properties))
3815             regexp elem beg invisible visible face)
3816         (article-goto-body)
3817         (setq beg (point))
3818         (while (setq elem (pop alist))
3819           (goto-char beg)
3820           (setq regexp (car elem)
3821                 invisible (nth 1 elem)
3822                 visible (nth 2 elem)
3823                 face (nth 3 elem))
3824           (while (re-search-forward regexp nil t)
3825             (when (and (match-beginning visible) (match-beginning invisible))
3826               (gnus-article-hide-text
3827                (match-beginning invisible) (match-end invisible) props)
3828               (gnus-article-unhide-text-type
3829                (match-beginning visible) (match-end visible) 'emphasis)
3830               (gnus-put-overlay-excluding-newlines
3831                (match-beginning visible) (match-end visible) 'face face)
3832               (gnus-add-wash-type 'emphasis)
3833               (goto-char (match-end invisible)))))))))
3834
3835 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3836   "Setup newsgroup emphasis alist."
3837   (unless gnus-article-emphasis-alist
3838     (let ((name (and gnus-newsgroup-name
3839                      (gnus-group-real-name gnus-newsgroup-name))))
3840       (make-local-variable 'gnus-article-emphasis-alist)
3841       (setq gnus-article-emphasis-alist
3842             (nconc
3843              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3844                (while (setq elem (pop alist))
3845                  (when (and name (string-match (car elem) name))
3846                    (setq alist nil
3847                          highlight (copy-sequence (cdr elem)))))
3848                highlight)
3849              (copy-sequence highlight-words)
3850              (if gnus-newsgroup-name
3851                  (copy-sequence (gnus-group-find-parameter
3852                                  gnus-newsgroup-name 'highlight-words t)))
3853              gnus-emphasis-alist)))))
3854
3855 (defvar gnus-summary-article-menu)
3856 (defvar gnus-summary-post-menu)
3857
3858 ;;; Saving functions.
3859
3860 (defun gnus-article-save (save-buffer file &optional num)
3861   "Save the currently selected article."
3862   (when (or (get gnus-default-article-saver :headers)
3863             (not gnus-save-all-headers))
3864     ;; Remove headers according to `gnus-saved-headers' or the value
3865     ;; of the `:headers' property that the saver function might have.
3866     (let ((gnus-visible-headers
3867            (or (symbol-value (get gnus-default-article-saver :headers))
3868                gnus-saved-headers gnus-visible-headers))
3869           ;; Ignore group parameter.  See `article-hide-headers'.
3870           (gnus-summary-buffer nil))
3871       (with-current-buffer save-buffer
3872         (article-hide-headers 1 t))))
3873   (save-window-excursion
3874     (if (not gnus-default-article-saver)
3875         (error "No default saver is defined")
3876       ;; !!! Magic!  The saving functions all save
3877       ;; `gnus-save-article-buffer' (or so they think), but we
3878       ;; bind that variable to our save-buffer.
3879       (set-buffer gnus-article-buffer)
3880       (let* ((gnus-save-article-buffer save-buffer)
3881              (filename
3882               (cond
3883                ((not gnus-prompt-before-saving) 'default)
3884                ((eq gnus-prompt-before-saving 'always) nil)
3885                (t file)))
3886              (gnus-number-of-articles-to-be-saved
3887               (when (eq gnus-prompt-before-saving t)
3888                 num)))                  ; Magic
3889         (set-buffer gnus-article-current-summary)
3890         (funcall gnus-default-article-saver filename)))))
3891
3892 (defun gnus-read-save-file-name (prompt &optional filename
3893                                         function group headers variable
3894                                         dir-var)
3895   (let ((default-name
3896           (funcall function group headers (symbol-value variable)))
3897         result)
3898     (setq result
3899           (expand-file-name
3900            (cond
3901             ((eq filename 'default)
3902              default-name)
3903             ((eq filename t)
3904              default-name)
3905             (filename filename)
3906             (t
3907              (when (symbol-value dir-var)
3908                (setq default-name (expand-file-name
3909                                    (file-name-nondirectory default-name)
3910                                    (symbol-value dir-var))))
3911              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3912                     (prompt
3913                      (format prompt
3914                              (if (and gnus-number-of-articles-to-be-saved
3915                                       (> gnus-number-of-articles-to-be-saved 1))
3916                                  (format "these %d articles"
3917                                          gnus-number-of-articles-to-be-saved)
3918                                "this article")))
3919                     (file
3920                      ;; Let the split methods have their say.
3921                      (cond
3922                       ;; No split name was found.
3923                       ((null split-name)
3924                        (read-file-name
3925                         (concat prompt " (default "
3926                                 (file-name-nondirectory default-name) "): ")
3927                         (file-name-directory default-name)
3928                         default-name))
3929                       ;; A single group name is returned.
3930                       ((stringp split-name)
3931                        (setq default-name
3932                              (funcall function split-name headers
3933                                       (symbol-value variable)))
3934                        (read-file-name
3935                         (concat prompt " (default "
3936                                 (file-name-nondirectory default-name) "): ")
3937                         (file-name-directory default-name)
3938                         default-name))
3939                       ;; A single split name was found
3940                       ((= 1 (length split-name))
3941                        (let* ((name (expand-file-name
3942                                      (car split-name)
3943                                      gnus-article-save-directory))
3944                               (dir (cond ((file-directory-p name)
3945                                           (file-name-as-directory name))
3946                                          ((file-exists-p name) name)
3947                                          (t gnus-article-save-directory))))
3948                          (read-file-name
3949                           (concat prompt " (default " name "): ")
3950                           dir name)))
3951                       ;; A list of splits was found.
3952                       (t
3953                        (setq split-name (nreverse split-name))
3954                        (let (result)
3955                          (let ((file-name-history
3956                                 (nconc split-name file-name-history)))
3957                            (setq result
3958                                  (expand-file-name
3959                                   (read-file-name
3960                                    (concat prompt " (`M-p' for defaults): ")
3961                                    gnus-article-save-directory
3962                                    (car split-name))
3963                                   gnus-article-save-directory)))
3964                          (car (push result file-name-history)))))))
3965                ;; Create the directory.
3966                (gnus-make-directory (file-name-directory file))
3967                ;; If we have read a directory, we append the default file name.
3968                (when (file-directory-p file)
3969                  (setq file (expand-file-name (file-name-nondirectory
3970                                                default-name)
3971                                               (file-name-as-directory file))))
3972                ;; Possibly translate some characters.
3973                (nnheader-translate-file-chars file))))))
3974     (gnus-make-directory (file-name-directory result))
3975     (when variable
3976       (set variable result))
3977     (when dir-var
3978       (set dir-var (file-name-directory result)))
3979     result))
3980
3981 (defun gnus-article-archive-name (group)
3982   "Return the first instance of an \"Archive-name\" in the current buffer."
3983   (let ((case-fold-search t))
3984     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3985       (nnheader-concat gnus-article-save-directory
3986                        (match-string 1)))))
3987
3988 (defun gnus-article-nndoc-name (group)
3989   "If GROUP is an nndoc group, return the name of the parent group."
3990   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3991     (gnus-group-get-parameter group 'save-article-group)))
3992
3993 (defun gnus-summary-save-in-rmail (&optional filename)
3994   "Append this article to Rmail file.
3995 Optional argument FILENAME specifies file name.
3996 Directory to save to is default to `gnus-article-save-directory'."
3997   (setq filename (gnus-read-save-file-name
3998                   "Save %s in rmail file" filename
3999                   gnus-rmail-save-name gnus-newsgroup-name
4000                   gnus-current-headers 'gnus-newsgroup-last-rmail))
4001   (with-current-buffer gnus-save-article-buffer
4002     (save-excursion
4003       (save-restriction
4004         (widen)
4005         ;; Note that unlike gnus-summary-save-in-mail, there is no
4006         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4007         ;; not use Babyl.
4008         (gnus-output-to-rmail filename))))
4009   filename)
4010
4011 (defun gnus-summary-save-in-mail (&optional filename)
4012   "Append this article to Unix mail file.
4013 Optional argument FILENAME specifies file name.
4014 Directory to save to is default to `gnus-article-save-directory'."
4015   (setq filename (gnus-read-save-file-name
4016                   "Save %s in Unix mail file" filename
4017                   gnus-mail-save-name gnus-newsgroup-name
4018                   gnus-current-headers 'gnus-newsgroup-last-mail))
4019   (with-current-buffer gnus-save-article-buffer
4020     (save-excursion
4021       (save-restriction
4022         (widen)
4023         (if (and (file-readable-p filename)
4024                  (file-regular-p filename)
4025                  (mail-file-babyl-p filename))
4026             (gnus-output-to-rmail filename)
4027           (gnus-output-to-mail filename)))))
4028   filename)
4029
4030 (put 'gnus-summary-save-in-file :decode t)
4031 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4032 (defun gnus-summary-save-in-file (&optional filename overwrite)
4033   "Append this article to file.
4034 Optional argument FILENAME specifies file name.
4035 Directory to save to is default to `gnus-article-save-directory'."
4036   (setq filename (gnus-read-save-file-name
4037                   "Save %s in file" filename
4038                   gnus-file-save-name gnus-newsgroup-name
4039                   gnus-current-headers 'gnus-newsgroup-last-file))
4040   (with-current-buffer gnus-save-article-buffer
4041     (save-excursion
4042       (save-restriction
4043         (widen)
4044         (when (and overwrite
4045                    (file-exists-p filename))
4046           (delete-file filename))
4047         (gnus-output-to-file filename))))
4048   filename)
4049
4050 (put 'gnus-summary-write-to-file :decode t)
4051 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4052 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4053 (defun gnus-summary-write-to-file (&optional filename)
4054   "Write this article to a file, overwriting it if the file exists.
4055 Optional argument FILENAME specifies file name.
4056 The directory to save in defaults to `gnus-article-save-directory'."
4057   (setq filename (gnus-read-save-file-name
4058                   "Save %s in file" filename
4059                   gnus-file-save-name gnus-newsgroup-name
4060                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4061   (gnus-summary-save-in-file filename t))
4062
4063 (put 'gnus-summary-save-body-in-file :decode t)
4064 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4065   "Append this article body to a file.
4066 Optional argument FILENAME specifies file name.
4067 The directory to save in defaults to `gnus-article-save-directory'."
4068   (setq filename (gnus-read-save-file-name
4069                   "Save %s body in file" filename
4070                   gnus-file-save-name gnus-newsgroup-name
4071                   gnus-current-headers 'gnus-newsgroup-last-file))
4072   (with-current-buffer gnus-save-article-buffer
4073     (save-excursion
4074       (save-restriction
4075         (widen)
4076         (when (article-goto-body)
4077           (narrow-to-region (point) (point-max)))
4078         (when (and overwrite
4079                    (file-exists-p filename))
4080           (delete-file filename))
4081         (gnus-output-to-file filename))))
4082   filename)
4083
4084 (put 'gnus-summary-write-body-to-file :decode t)
4085 (put 'gnus-summary-write-body-to-file
4086      :function 'gnus-summary-save-body-in-file)
4087 (defun gnus-summary-write-body-to-file (&optional filename)
4088   "Write this article body to a file, overwriting it if the file exists.
4089 Optional argument FILENAME specifies file name.
4090 The directory to save in defaults to `gnus-article-save-directory'."
4091   (setq filename (gnus-read-save-file-name
4092                   "Save %s body in file" filename
4093                   gnus-file-save-name gnus-newsgroup-name
4094                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4095   (gnus-summary-save-body-in-file filename t))
4096
4097 (put 'gnus-summary-save-in-pipe :decode t)
4098 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4099 (defun gnus-summary-save-in-pipe (&optional command raw)
4100   "Pipe this article to subprocess COMMAND.
4101 Valid values for COMMAND include:
4102   a string
4103     The executable command name and possibly arguments.
4104   nil
4105     You will be prompted for the command in the minibuffer.
4106   the symbol `default'
4107     It will be replaced with the command which the variable
4108     `gnus-summary-pipe-output-default-command' holds or the command
4109     last used for saving.
4110 Non-nil value for RAW overrides `:decode' and `:headers' properties
4111 and the raw article including all headers will be piped."
4112   (let ((article (gnus-summary-article-number))
4113         (decode (unless raw
4114                   (get 'gnus-summary-save-in-pipe :decode)))
4115         save-buffer default)
4116     (if article
4117         (if (vectorp (gnus-summary-article-header article))
4118             (save-current-buffer
4119               (gnus-summary-select-article decode decode nil article)
4120               (insert-buffer-substring
4121                (prog1
4122                    (if decode
4123                        gnus-article-buffer
4124                      gnus-original-article-buffer)
4125                  (setq save-buffer
4126                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4127               ;; Remove unwanted headers.
4128               (when (and (not raw)
4129                          (or (get 'gnus-summary-save-in-pipe :headers)
4130                              (not gnus-save-all-headers)))
4131                 (let ((gnus-visible-headers
4132                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4133                                               :headers))
4134                            gnus-saved-headers gnus-visible-headers))
4135                       (gnus-summary-buffer nil))
4136                   (article-hide-headers 1 t))))
4137           (error "%d is not a real article" article))
4138       (error "No article to pipe"))
4139     (setq default (or gnus-summary-pipe-output-default-command
4140                       gnus-last-shell-command))
4141     (unless (stringp command)
4142       (setq command
4143             (if (and (eq command 'default) default)
4144                 default
4145               (gnus-read-shell-command "Shell command on this article: "
4146                                        default))))
4147     (when (string-equal command "")
4148       (if default
4149           (setq command default)
4150         (error "A command is required")))
4151     (with-current-buffer save-buffer
4152       (save-restriction
4153         (widen)
4154         (shell-command-on-region (point-min) (point-max) command nil)))
4155     (gnus-kill-buffer save-buffer))
4156   (setq gnus-summary-pipe-output-default-command command))
4157
4158 (defun gnus-summary-pipe-to-muttprint (&optional command)
4159   "Pipe this article to muttprint."
4160   (unless (stringp command)
4161     (setq command (read-string
4162                    "Print using command: " gnus-summary-muttprint-program
4163                    nil gnus-summary-muttprint-program)))
4164   (let ((gnus-summary-pipe-output-default-command
4165          gnus-summary-pipe-output-default-command))
4166     (gnus-summary-save-in-pipe command))
4167   (setq gnus-summary-muttprint-program command))
4168
4169 ;;; Article file names when saving.
4170
4171 (defun gnus-capitalize-newsgroup (newsgroup)
4172   "Capitalize NEWSGROUP name."
4173   (when (not (zerop (length newsgroup)))
4174     (concat (char-to-string (upcase (aref newsgroup 0)))
4175             (substring newsgroup 1))))
4176
4177 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4178   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4179 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4180 Otherwise, it is like ~/News/news/group/num."
4181   (let ((default
4182           (expand-file-name
4183            (concat (if (gnus-use-long-file-name 'not-save)
4184                        (gnus-capitalize-newsgroup newsgroup)
4185                      (gnus-newsgroup-directory-form newsgroup))
4186                    "/" (int-to-string (mail-header-number headers)))
4187            gnus-article-save-directory)))
4188     (if (and last-file
4189              (string-equal (file-name-directory default)
4190                            (file-name-directory last-file))
4191              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4192         default
4193       (or last-file default))))
4194
4195 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4196   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4197 If variable `gnus-use-long-file-name' is non-nil, it is
4198 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4199   (let ((default
4200           (expand-file-name
4201            (concat (if (gnus-use-long-file-name 'not-save)
4202                        newsgroup
4203                      (gnus-newsgroup-directory-form newsgroup))
4204                    "/" (int-to-string (mail-header-number headers)))
4205            gnus-article-save-directory)))
4206     (if (and last-file
4207              (string-equal (file-name-directory default)
4208                            (file-name-directory last-file))
4209              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4210         default
4211       (or last-file default))))
4212
4213 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4214   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4215 If variable `gnus-use-long-file-name' is non-nil, it is
4216 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4217   (or last-file
4218       (expand-file-name
4219        (if (gnus-use-long-file-name 'not-save)
4220            newsgroup
4221          (file-relative-name
4222           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4223           default-directory))
4224        gnus-article-save-directory)))
4225
4226 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4227   "Generate file name from sender."
4228   (let ((from (mail-header-from headers)))
4229     (expand-file-name
4230      (if (and from (string-match "\\([^ <]+\\)@" from))
4231          (match-string 1 from)
4232        "nobody")
4233      gnus-article-save-directory)))
4234
4235 (defun article-verify-x-pgp-sig ()
4236   "Verify X-PGP-Sig."
4237   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4238   (interactive)
4239   (if (gnus-buffer-live-p gnus-original-article-buffer)
4240       (let ((sig (with-current-buffer gnus-original-article-buffer
4241                    (gnus-fetch-field "X-PGP-Sig")))
4242             items info headers)
4243         (when (and sig
4244                    mml2015-use
4245                    (mml2015-clear-verify-function))
4246           (with-temp-buffer
4247             (insert-buffer-substring gnus-original-article-buffer)
4248             (setq items (split-string sig))
4249             (message-narrow-to-head)
4250             (let ((inhibit-point-motion-hooks t)
4251                   (case-fold-search t))
4252               ;; Don't verify multiple headers.
4253               (setq headers (mapconcat (lambda (header)
4254                                          (concat header ": "
4255                                                  (mail-fetch-field header)
4256                                                  "\n"))
4257                                        (split-string (nth 1 items) ",") "")))
4258             (delete-region (point-min) (point-max))
4259             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4260             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4261             (insert headers)
4262             (widen)
4263             (forward-line)
4264             (while (not (eobp))
4265               (if (looking-at "^-")
4266                   (insert "- "))
4267               (forward-line))
4268             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4269             (insert "Version: " (car items) "\n\n")
4270             (insert (mapconcat 'identity (cddr items) "\n"))
4271             (insert "\n-----END PGP SIGNATURE-----\n")
4272             (let ((mm-security-handle (list (format "multipart/signed"))))
4273               (mml2015-clean-buffer)
4274               (let ((coding-system-for-write (or gnus-newsgroup-charset
4275                                                  'iso-8859-1)))
4276                 (funcall (mml2015-clear-verify-function)))
4277               (setq info
4278                     (or (mm-handle-multipart-ctl-parameter
4279                          mm-security-handle 'gnus-details)
4280                         (mm-handle-multipart-ctl-parameter
4281                          mm-security-handle 'gnus-info)))))
4282           (when info
4283             (let ((inhibit-read-only t) bface eface)
4284               (save-restriction
4285                 (message-narrow-to-head)
4286                 (goto-char (point-max))
4287                 (forward-line -1)
4288                 (setq bface (get-text-property (point-at-bol) 'face)
4289                       eface (get-text-property (1- (point-at-eol)) 'face))
4290                 (message-remove-header "X-Gnus-PGP-Verify")
4291                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4292                     (forward-line)
4293                   (goto-char (point-max)))
4294                 (narrow-to-region (point) (point))
4295                 (insert "X-Gnus-PGP-Verify: " info "\n")
4296                 (goto-char (point-min))
4297                 (forward-line)
4298                 (while (not (eobp))
4299                   (if (not (looking-at "^[ \t]"))
4300                       (insert " "))
4301                   (forward-line))
4302                 ;; Do highlighting.
4303                 (goto-char (point-min))
4304                 (when (looking-at "\\([^:]+\\): *")
4305                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4306                                      'face bface)
4307                   (put-text-property (match-end 0) (point-max)
4308                                      'face eface)))))))))
4309
4310 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4311
4312 (defun article-verify-cancel-lock ()
4313   "Verify Cancel-Lock header."
4314   (interactive)
4315   (if (gnus-buffer-live-p gnus-original-article-buffer)
4316       (canlock-verify gnus-original-article-buffer)))
4317
4318 (eval-and-compile
4319   (mapc
4320    (lambda (func)
4321      (let (afunc gfunc)
4322        (if (consp func)
4323            (setq afunc (car func)
4324                  gfunc (cdr func))
4325          (setq afunc func
4326                gfunc (intern (format "gnus-%s" func))))
4327        (defalias gfunc
4328          (when (fboundp afunc)
4329            `(lambda (&optional interactive &rest args)
4330               ,(documentation afunc t)
4331               (interactive (list t))
4332               (with-current-buffer gnus-article-buffer
4333                 (if interactive
4334                     (call-interactively ',afunc)
4335                   (apply ',afunc args))))))))
4336    '(article-hide-headers
4337      article-verify-x-pgp-sig
4338      article-verify-cancel-lock
4339      article-hide-boring-headers
4340      article-treat-overstrike
4341      article-treat-ansi-sequences
4342      article-fill-long-lines
4343      article-capitalize-sentences
4344      article-remove-cr
4345      article-remove-leading-whitespace
4346      article-display-x-face
4347      article-display-face
4348      article-de-quoted-unreadable
4349      article-de-base64-unreadable
4350      article-decode-HZ
4351      article-wash-html
4352      article-unsplit-urls
4353      article-hide-list-identifiers
4354      article-strip-banner
4355      article-babel
4356      article-hide-pem
4357      article-hide-signature
4358      article-strip-headers-in-body
4359      article-remove-trailing-blank-lines
4360      article-strip-leading-blank-lines
4361      article-strip-multiple-blank-lines
4362      article-strip-leading-space
4363      article-strip-trailing-space
4364      article-strip-blank-lines
4365      article-strip-all-blank-lines
4366      article-date-local
4367      article-date-english
4368      article-date-iso8601
4369      article-date-original
4370      article-treat-date
4371      article-date-ut
4372      article-decode-mime-words
4373      article-decode-charset
4374      article-decode-encoded-words
4375      article-date-user
4376      article-date-lapsed
4377      article-date-combined-lapsed
4378      article-emphasize
4379      article-treat-dumbquotes
4380      article-treat-non-ascii
4381      article-normalize-headers
4382      ;;(article-show-all . gnus-article-show-all-headers)
4383      )))
4384 \f
4385 ;;;
4386 ;;; Gnus article mode
4387 ;;;
4388
4389 (put 'gnus-article-mode 'mode-class 'special)
4390
4391 (set-keymap-parent gnus-article-mode-map widget-keymap)
4392
4393 (gnus-define-keys gnus-article-mode-map
4394   " " gnus-article-goto-next-page
4395   [?\S-\ ] gnus-article-goto-prev-page
4396   "\177" gnus-article-goto-prev-page
4397   [delete] gnus-article-goto-prev-page
4398   [backspace] gnus-article-goto-prev-page
4399   "\C-c^" gnus-article-refer-article
4400   "h" gnus-article-show-summary
4401   "s" gnus-article-show-summary
4402   "\C-c\C-m" gnus-article-mail
4403   "?" gnus-article-describe-briefly
4404   "<" beginning-of-buffer
4405   ">" end-of-buffer
4406   "\C-c\C-i" gnus-info-find-node
4407   "\C-c\C-b" gnus-bug
4408   "R" gnus-article-reply-with-original
4409   "F" gnus-article-followup-with-original
4410   "\C-hk" gnus-article-describe-key
4411   "\C-hc" gnus-article-describe-key-briefly
4412   "\C-hb" gnus-article-describe-bindings
4413
4414   "e" gnus-article-read-summary-keys
4415   "\C-d" gnus-article-read-summary-keys
4416   "\M-*" gnus-article-read-summary-keys
4417   "\M-#" gnus-article-read-summary-keys
4418   "\M-^" gnus-article-read-summary-keys
4419   "\M-g" gnus-article-read-summary-keys)
4420
4421 (substitute-key-definition
4422  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4423
4424 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4425   "W" gnus-article-wide-reply-with-original)
4426 (if (featurep 'xemacs)
4427     (set-keymap-default-binding gnus-article-send-map
4428                                 'gnus-article-read-summary-send-keys)
4429   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4430
4431 (defun gnus-article-make-menu-bar ()
4432   (unless (boundp 'gnus-article-commands-menu)
4433     (gnus-summary-make-menu-bar))
4434   (unless (boundp 'gnus-article-article-menu)
4435     (easy-menu-define
4436      gnus-article-article-menu gnus-article-mode-map ""
4437      '("Article"
4438        ["Scroll forwards" gnus-article-goto-next-page t]
4439        ["Scroll backwards" gnus-article-goto-prev-page t]
4440        ["Show summary" gnus-article-show-summary t]
4441        ["Fetch Message-ID at point" gnus-article-refer-article t]
4442        ["Mail to address at point" gnus-article-mail t]
4443        ["Send a bug report" gnus-bug t]))
4444
4445     (easy-menu-define
4446      gnus-article-treatment-menu gnus-article-mode-map ""
4447      ;; Fixme: this should use :active (and maybe :visible).
4448      '("Treatment"
4449        ["Hide headers" gnus-article-hide-headers t]
4450        ["Hide signature" gnus-article-hide-signature t]
4451        ["Hide citation" gnus-article-hide-citation t]
4452        ["Treat overstrike" gnus-article-treat-overstrike t]
4453        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4454        ["Remove carriage return" gnus-article-remove-cr t]
4455        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4456        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4457        ["Remove base64" gnus-article-de-base64-unreadable t]
4458        ["Treat html" gnus-article-wash-html t]
4459        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4460        ["Decode HZ" gnus-article-decode-HZ t]))
4461
4462     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4463
4464     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4465
4466     (gnus-run-hooks 'gnus-article-menu-hook)))
4467
4468 (defvar bookmark-make-record-function)
4469 (defvar shr-put-image-function)
4470
4471 (define-derived-mode gnus-article-mode fundamental-mode "Article"
4472   "Major mode for displaying an article.
4473
4474 All normal editing commands are switched off.
4475
4476 The following commands are available in addition to all summary mode
4477 commands:
4478 \\<gnus-article-mode-map>
4479 \\[gnus-article-next-page]\t Scroll the article one page forwards
4480 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4481 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4482 \\[gnus-article-show-summary]\t Display the summary buffer
4483 \\[gnus-article-mail]\t Send a reply to the address near point
4484 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4485 \\[gnus-info-find-node]\t Go to the Gnus info node"
4486   (gnus-simplify-mode-line)
4487   (make-local-variable 'minor-mode-alist)
4488   (when (gnus-visual-p 'article-menu 'menu)
4489     (gnus-article-make-menu-bar)
4490     (when gnus-summary-tool-bar-map
4491       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4492   (gnus-update-format-specifications nil 'article-mode)
4493   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4494   (set (make-local-variable 'gnus-page-broken) nil)
4495   (make-local-variable 'gnus-article-current-summary)
4496   (make-local-variable 'gnus-article-mime-handles)
4497   (make-local-variable 'gnus-article-decoded-p)
4498   (make-local-variable 'gnus-article-mime-handle-alist)
4499   (make-local-variable 'gnus-article-wash-types)
4500   (make-local-variable 'gnus-article-image-alist)
4501   (make-local-variable 'gnus-article-charset)
4502   (make-local-variable 'gnus-article-ignored-charsets)
4503   (set (make-local-variable 'bookmark-make-record-function)
4504        'gnus-summary-bookmark-make-record)
4505   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4506   ;; face.
4507   (set (make-local-variable 'nobreak-char-display) nil)
4508   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4509   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4510   (setq cursor-in-non-selected-windows nil)
4511   (gnus-set-default-directory)
4512   (buffer-disable-undo)
4513   (setq buffer-read-only t
4514         show-trailing-whitespace nil)
4515   (mm-enable-multibyte))
4516
4517 (defun gnus-article-setup-buffer ()
4518   "Initialize the article buffer."
4519   (let* ((name (if gnus-single-article-buffer "*Article*"
4520                  (concat "*Article "
4521                          (gnus-group-decoded-name gnus-newsgroup-name)
4522                          "*")))
4523          (original
4524           (progn (string-match "\\*Article" name)
4525                  (concat " *Original Article"
4526                          (substring name (match-end 0))))))
4527     (setq gnus-article-buffer name)
4528     (setq gnus-original-article-buffer original)
4529     (setq gnus-article-mime-handle-alist nil)
4530     (with-current-buffer gnus-summary-buffer
4531       ;; This might be a variable local to the summary buffer.
4532       (unless gnus-single-article-buffer
4533         (setq gnus-article-buffer name)
4534         (setq gnus-original-article-buffer original)
4535         (gnus-set-global-variables)))
4536     (gnus-article-setup-highlight-words)
4537     ;; Init original article buffer.
4538     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4539       (mm-enable-multibyte)
4540       (setq major-mode 'gnus-original-article-mode)
4541       (make-local-variable 'gnus-original-article))
4542     (if (and (get-buffer name)
4543              (with-current-buffer name
4544                (if gnus-article-edit-mode
4545                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4546                        (progn
4547                          (set-buffer-modified-p nil)
4548                          (gnus-kill-buffer name)
4549                          (message "")
4550                          nil)
4551                      (error "Action aborted"))
4552                  t)))
4553         (with-current-buffer name
4554           (set (make-local-variable 'gnus-article-edit-mode) nil)
4555           (gnus-article-stop-animations)
4556           (when gnus-article-mime-handles
4557             (mm-destroy-parts gnus-article-mime-handles)
4558             (setq gnus-article-mime-handles nil))
4559           ;; Set it to nil in article-buffer!
4560           (setq gnus-article-mime-handle-alist nil)
4561           (buffer-disable-undo)
4562           (setq buffer-read-only t)
4563           (unless (derived-mode-p 'gnus-article-mode)
4564             (gnus-article-mode))
4565           (setq truncate-lines gnus-article-truncate-lines)
4566           (current-buffer))
4567       (let ((summary gnus-summary-buffer))
4568         (with-current-buffer (gnus-get-buffer-create name)
4569           (gnus-article-mode)
4570           (setq truncate-lines gnus-article-truncate-lines)
4571           (set (make-local-variable 'gnus-summary-buffer) summary)
4572           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4573           (when article-lapsed-timer
4574             (gnus-stop-date-timer))
4575           (when gnus-article-update-date-headers
4576             (gnus-start-date-timer gnus-article-update-date-headers))
4577           (current-buffer))))))
4578
4579 (defun gnus-article-stop-animations ()
4580   (dolist (timer (and (boundp 'timer-list)
4581                       timer-list))
4582     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4583       (cancel-timer timer))))
4584
4585 (defun gnus-stop-downloads ()
4586   (when (boundp 'url-queue)
4587     (set (intern "url-queue" obarray) nil)))
4588
4589 ;; Set article window start at LINE, where LINE is the number of lines
4590 ;; from the head of the article.
4591 (defun gnus-article-set-window-start (&optional line)
4592   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4593     (when article-window
4594       (set-window-start
4595        article-window
4596        (with-current-buffer gnus-article-buffer
4597          (goto-char (point-min))
4598          (if (not line)
4599              (point-min)
4600            (gnus-message 6 "Moved to bookmark")
4601            (search-forward "\n\n" nil t)
4602            (forward-line line)
4603            (point)))))))
4604
4605 (defun gnus-article-prepare (article &optional all-headers header)
4606   "Prepare ARTICLE in article mode buffer.
4607 ARTICLE should either be an article number or a Message-ID.
4608 If ARTICLE is an id, HEADER should be the article headers.
4609 If ALL-HEADERS is non-nil, no headers are hidden."
4610   (save-excursion
4611     ;; Make sure we start in a summary buffer.
4612     (unless (derived-mode-p 'gnus-summary-mode)
4613       (set-buffer gnus-summary-buffer))
4614     (setq gnus-summary-buffer (current-buffer))
4615     (let* ((gnus-article (if header (mail-header-number header) article))
4616            (summary-buffer (current-buffer))
4617            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4618            (group gnus-newsgroup-name)
4619            result)
4620       (save-excursion
4621         (gnus-article-setup-buffer)
4622         (set-buffer gnus-article-buffer)
4623         ;; Deactivate active regions.
4624         (when (and (boundp 'transient-mark-mode)
4625                    transient-mark-mode)
4626           (setq mark-active nil))
4627         (if (not (setq result (let ((inhibit-read-only t))
4628                                 (gnus-request-article-this-buffer
4629                                  article group))))
4630             ;; There is no such article.
4631             (save-excursion
4632               (when (and (numberp article)
4633                          (not (memq article gnus-newsgroup-sparse)))
4634                 (setq gnus-article-current
4635                       (cons gnus-newsgroup-name article))
4636                 (set-buffer gnus-summary-buffer)
4637                 (setq gnus-current-article article)
4638                 (if (and (memq article gnus-newsgroup-undownloaded)
4639                          (not (gnus-online (gnus-find-method-for-group
4640                                             gnus-newsgroup-name))))
4641                     (progn
4642                       (gnus-summary-set-agent-mark article)
4643                       (message "Message marked for downloading"))
4644                   (gnus-summary-mark-article article gnus-canceled-mark)
4645                   (unless (memq article gnus-newsgroup-sparse)
4646                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4647           (if (or (eq result 'pseudo)
4648                   (eq result 'nneething))
4649               (progn
4650                 (with-current-buffer summary-buffer
4651                   (push article gnus-newsgroup-history)
4652                   (setq gnus-last-article gnus-current-article
4653                         gnus-current-article 0
4654                         gnus-current-headers nil
4655                         gnus-article-current nil)
4656                   (if (eq result 'nneething)
4657                       (gnus-configure-windows 'summary)
4658                     (gnus-configure-windows 'article))
4659                   (gnus-set-global-variables))
4660                 (let ((gnus-article-mime-handle-alist-1
4661                        gnus-article-mime-handle-alist))
4662                   (gnus-set-mode-line 'article)))
4663             ;; The result from the `request' was an actual article -
4664             ;; or at least some text that is now displayed in the
4665             ;; article buffer.
4666             (when (and (numberp article)
4667                        (not (eq article gnus-current-article)))
4668               ;; Seems like a new article has been selected.
4669               ;; `gnus-current-article' must be an article number.
4670               (with-current-buffer summary-buffer
4671                 (push article gnus-newsgroup-history)
4672                 (setq gnus-last-article gnus-current-article
4673                       gnus-current-article article
4674                       gnus-current-headers
4675                       (gnus-summary-article-header gnus-current-article)
4676                       gnus-article-current
4677                       (cons gnus-newsgroup-name gnus-current-article))
4678                 (unless (vectorp gnus-current-headers)
4679                   (setq gnus-current-headers nil))
4680                 (gnus-summary-goto-subject gnus-current-article)
4681                 (when (gnus-summary-show-thread)
4682                   ;; If the summary buffer really was folded, the
4683                   ;; previous goto may not actually have gone to
4684                   ;; the right article, but the thread root instead.
4685                   ;; So we go again.
4686                   (gnus-summary-goto-subject gnus-current-article))
4687                 (gnus-run-hooks 'gnus-mark-article-hook)
4688                 (gnus-set-mode-line 'summary)
4689                 (when (gnus-visual-p 'article-highlight 'highlight)
4690                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4691                 ;; Set the global newsgroup variables here.
4692                 (gnus-set-global-variables)
4693                 (setq gnus-have-all-headers
4694                       (or all-headers gnus-show-all-headers))))
4695             (save-excursion
4696               (gnus-configure-windows 'article))
4697             (when (or (numberp article)
4698                       (stringp article))
4699               (gnus-article-prepare-display)
4700               ;; Do page break.
4701               (goto-char (point-min))
4702               (when gnus-break-pages
4703                 (gnus-narrow-to-page)))
4704             (let ((gnus-article-mime-handle-alist-1
4705                    gnus-article-mime-handle-alist))
4706               (gnus-set-mode-line 'article))
4707             (article-goto-body)
4708             (unless (bobp)
4709               (forward-line -1))
4710             (set-window-point (get-buffer-window (current-buffer)) (point))
4711             (gnus-configure-windows 'article)
4712             (gnus-run-hooks 'gnus-article-prepare-hook)
4713             t))))))
4714
4715 ;;;###autoload
4716 (defun gnus-article-prepare-display ()
4717   "Make the current buffer look like a nice article."
4718   ;; Hooks for getting information from the article.
4719   ;; This hook must be called before being narrowed.
4720   (let ((gnus-article-buffer (current-buffer))
4721         buffer-read-only
4722         (inhibit-read-only t))
4723     (unless (derived-mode-p 'gnus-article-mode)
4724       (gnus-article-mode))
4725     (setq buffer-read-only nil
4726           gnus-article-wash-types nil
4727           gnus-article-image-alist nil)
4728     (gnus-run-hooks 'gnus-tmp-internal-hook)
4729     (when gnus-display-mime-function
4730       (funcall gnus-display-mime-function))))
4731
4732 ;;;
4733 ;;; Gnus Sticky Article Mode
4734 ;;;
4735
4736 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4737   "Mode for sticky articles."
4738   ;; Release bindings that won't work.
4739   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4740                              gnus-sticky-article-mode-map)
4741   (substitute-key-definition 'gnus-article-refer-article 'undefined
4742                              gnus-sticky-article-mode-map)
4743   (dolist (k '("e" "h" "s" "F" "R"))
4744     (define-key gnus-sticky-article-mode-map k nil))
4745   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4746   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4747   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4748   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4749
4750 (defun gnus-sticky-article (arg)
4751   "Make the current article sticky.
4752 If a prefix ARG is given, ask for a name for this sticky article buffer."
4753   (interactive "P")
4754   (gnus-summary-show-thread)
4755   (gnus-summary-select-article nil nil 'pseudo)
4756   (let (new-art-buf-name)
4757     (gnus-eval-in-buffer-window gnus-article-buffer
4758       (setq new-art-buf-name
4759             (concat
4760              "*Sticky Article: "
4761              (if arg
4762                  (read-from-minibuffer "Sticky article buffer name: ")
4763                (gnus-with-article-headers
4764                  (gnus-article-goto-header "subject")
4765                  (setq new-art-buf-name
4766                        (buffer-substring-no-properties
4767                         (line-beginning-position) (line-end-position)))
4768                  (goto-char (point-min))
4769                  (gnus-article-goto-header "from")
4770                  (setq new-art-buf-name
4771                        (concat
4772                         new-art-buf-name ", "
4773                         (buffer-substring-no-properties
4774                          (line-beginning-position) (line-end-position))))
4775                  (goto-char (point-min))
4776                  (gnus-article-goto-header "date")
4777                  (setq new-art-buf-name
4778                        (concat
4779                         new-art-buf-name ", "
4780                         (buffer-substring-no-properties
4781                          (line-beginning-position) (line-end-position))))))
4782              "*"))
4783       (if (and (gnus-buffer-live-p new-art-buf-name)
4784                (with-current-buffer new-art-buf-name
4785                  (derived-mode-p 'gnus-sticky-article-mode)))
4786           (switch-to-buffer new-art-buf-name)
4787         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4788       (gnus-sticky-article-mode))
4789     (setq gnus-article-buffer new-art-buf-name))
4790   (gnus-summary-recenter)
4791   (gnus-summary-position-point))
4792
4793 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4794   "Kill the given sticky article BUFFER.
4795 If none is given, assume the current buffer and kill it if it has
4796 `gnus-sticky-article-mode'."
4797   (interactive)
4798   (unless buffer
4799     (setq buffer (current-buffer)))
4800   (with-current-buffer buffer
4801     (when (derived-mode-p 'gnus-sticky-article-mode)
4802       (gnus-kill-buffer buffer))))
4803
4804 (defun gnus-kill-sticky-article-buffers (arg)
4805   "Kill all sticky article buffers.
4806 If a prefix ARG is given, ask for confirmation."
4807   (interactive "P")
4808   (dolist (buf (gnus-buffers))
4809     (with-current-buffer buf
4810       (when (derived-mode-p 'gnus-sticky-article-mode)
4811         (if (not arg)
4812             (gnus-kill-buffer buf)
4813           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4814             (gnus-kill-buffer buf)))))))
4815
4816 ;;;
4817 ;;; Gnus MIME viewing functions
4818 ;;;
4819
4820 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4821   "Format of the MIME buttons.
4822
4823 Valid specifiers include:
4824 %t  The MIME type
4825 %T  MIME type, along with additional info
4826 %n  The `name' parameter
4827 %d  The description, if any
4828 %l  The length of the encoded part
4829 %p  The part identifier number
4830 %e  Dots if the part isn't displayed
4831
4832 General format specifiers can also be used.  See Info node
4833 `(gnus)Formatting Variables'.")
4834
4835 (defvar gnus-mime-button-line-format-alist
4836   '((?t gnus-tmp-type ?s)
4837     (?T gnus-tmp-type-long ?s)
4838     (?n gnus-tmp-name ?s)
4839     (?d gnus-tmp-description ?s)
4840     (?p gnus-tmp-id ?s)
4841     (?l gnus-tmp-length ?d)
4842     (?e gnus-tmp-dots ?s)))
4843
4844 (defvar gnus-mime-button-commands
4845   '((gnus-article-press-button "\r" "Toggle Display")
4846     (gnus-mime-view-part "v" "View Interactively...")
4847     (gnus-mime-view-part-as-type "t" "View As Type...")
4848     (gnus-mime-view-part-as-charset "C" "View As charset...")
4849     (gnus-mime-save-part "o" "Save...")
4850     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4851     (gnus-mime-replace-part "r" "Replace part")
4852     (gnus-mime-delete-part "d" "Delete part")
4853     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4854     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4855     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4856     (gnus-mime-view-part-externally "e" "View Externally")
4857     (gnus-mime-print-part "p" "Print")
4858     (gnus-mime-pipe-part "|" "Pipe To Command...")
4859     (gnus-mime-action-on-part "." "Take action on the part...")))
4860
4861 (defun gnus-article-mime-part-status ()
4862   (if gnus-article-mime-handle-alist-1
4863       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4864           " (1 part)"
4865         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4866     ""))
4867
4868 (defvar gnus-mime-button-map
4869   (let ((map (make-sparse-keymap)))
4870     (define-key map gnus-mouse-2 'gnus-article-push-button)
4871     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4872     (dolist (c gnus-mime-button-commands)
4873       (define-key map (cadr c) (car c)))
4874     map))
4875
4876 (easy-menu-define
4877   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4878   `("MIME Part"
4879     ,@(mapcar (lambda (c)
4880                 (vector (caddr c) (car c) :active t))
4881               gnus-mime-button-commands)))
4882
4883 (defvar gnus-url-button-commands
4884   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4885
4886 (defvar gnus-url-button-map
4887   (let ((map (make-sparse-keymap)))
4888     (dolist (c gnus-url-button-commands)
4889       (define-key map (cadr c) (car c)))
4890     map))
4891
4892 (easy-menu-define
4893   gnus-url-button-menu gnus-url-button-map "URL button menu."
4894   `("Url Button"
4895     ,@(mapcar (lambda (c)
4896                 (vector (caddr c) (car c) :active t))
4897               gnus-url-button-commands)))
4898
4899 (defmacro gnus-bind-safe-url-regexp (&rest body)
4900   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4901   `(let ((mm-w3m-safe-url-regexp
4902           (let ((group (if (and (derived-mode-p 'gnus-article-mode)
4903                                 (gnus-buffer-live-p
4904                                  gnus-article-current-summary))
4905                            (with-current-buffer gnus-article-current-summary
4906                              gnus-newsgroup-name)
4907                          gnus-newsgroup-name)))
4908             (if (cond ((not group)
4909                        ;; Maybe we're in a mml-preview buffer
4910                        ;; and no group is selected.
4911                        t)
4912                       ((stringp gnus-safe-html-newsgroups)
4913                        (string-match gnus-safe-html-newsgroups group))
4914                       ((consp gnus-safe-html-newsgroups)
4915                        (member group gnus-safe-html-newsgroups)))
4916                 nil
4917               mm-w3m-safe-url-regexp))))
4918      ,@body))
4919
4920 (defun gnus-mime-button-menu (event prefix)
4921  "Construct a context-sensitive menu of MIME commands."
4922  (interactive "e\nP")
4923  (save-window-excursion
4924    (let ((pos (event-start event)))
4925      (select-window (posn-window pos))
4926      (goto-char (posn-point pos))
4927      (gnus-article-check-buffer)
4928      (popup-menu gnus-mime-button-menu nil prefix))))
4929
4930 (defun gnus-mime-view-all-parts (&optional handles)
4931   "View all the MIME parts."
4932   (interactive)
4933   (with-current-buffer gnus-article-buffer
4934     (let ((handles (or handles gnus-article-mime-handles))
4935           (mail-parse-charset gnus-newsgroup-charset)
4936           (mail-parse-ignored-charsets
4937            (with-current-buffer gnus-summary-buffer
4938              gnus-newsgroup-ignored-charsets)))
4939       (when handles
4940         (mm-remove-parts handles)
4941         (goto-char (point-min))
4942         (or (search-forward "\n\n") (goto-char (point-max)))
4943         (let ((inhibit-read-only t))
4944           (delete-region (point) (point-max))
4945           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4946
4947 (defun gnus-article-jump-to-part (n)
4948   "Jump to MIME part N."
4949   (interactive "P")
4950   (let ((parts (with-current-buffer gnus-article-buffer
4951                  (length gnus-article-mime-handle-alist))))
4952     (when (zerop parts)
4953       (error "No such part"))
4954     (pop-to-buffer gnus-article-buffer)
4955     (or n
4956         (setq n (if (= parts 1)
4957                     1
4958                   (read-number (format "Jump to part (1..%s): " parts)))))
4959     (unless (and (integerp n) (<= n parts) (>= n 1))
4960       (setq n
4961             (progn
4962               (gnus-message 7 "Invalid part `%s', using %s instead."
4963                             n parts)
4964               parts)))
4965     (gnus-message 9 "Jumping to part %s." n)
4966     (cond ((>= gnus-auto-select-part 1)
4967            (while (and (<= n parts)
4968                        (not (gnus-article-goto-part n)))
4969              (setq n (1+ n))))
4970           ((< gnus-auto-select-part 0)
4971            (while (and (>= n 1)
4972                        (not (gnus-article-goto-part n)))
4973              (setq n (1- n))))
4974           (t
4975            (gnus-article-goto-part n)))))
4976
4977 (defvar gnus-mime-buttonized-part-id nil
4978   "ID of a mime part that should be buttonized.
4979 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4980
4981 (eval-when-compile
4982   (defsubst gnus-article-edit-part (handles &optional current-id)
4983     "Edit an article in order to delete a mime part.
4984 This function is exclusively used by `gnus-mime-save-part-and-strip'
4985 and `gnus-mime-delete-part', and not provided at run-time normally."
4986     (gnus-article-edit-article
4987      `(lambda ()
4988         (buffer-disable-undo)
4989         (erase-buffer)
4990         (let ((mail-parse-charset (or gnus-article-charset
4991                                       ',gnus-newsgroup-charset))
4992               (mail-parse-ignored-charsets
4993                (or gnus-article-ignored-charsets
4994                    ',gnus-newsgroup-ignored-charsets))
4995               (mbl mml-buffer-list))
4996           (setq mml-buffer-list nil)
4997           (insert-buffer-substring gnus-original-article-buffer)
4998           (mime-to-mml ',handles)
4999           (setq gnus-article-mime-handles nil)
5000           (let ((mbl1 mml-buffer-list))
5001             (setq mml-buffer-list mbl)
5002             (set (make-local-variable 'mml-buffer-list) mbl1))
5003           (gnus-make-local-hook 'kill-buffer-hook)
5004           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5005      `(lambda (no-highlight)
5006         (let ((mail-parse-charset (or gnus-article-charset
5007                                       ',gnus-newsgroup-charset))
5008               (message-options message-options)
5009               (message-options-set-recipient)
5010               (mail-parse-ignored-charsets
5011                (or gnus-article-ignored-charsets
5012                    ',gnus-newsgroup-ignored-charsets)))
5013           (mml-to-mime)
5014           (mml-destroy-buffers)
5015           (remove-hook 'kill-buffer-hook
5016                        'mml-destroy-buffers t)
5017           (kill-local-variable 'mml-buffer-list))
5018         (gnus-summary-edit-article-done
5019          ,(or (mail-header-references gnus-current-headers) "")
5020          ,(gnus-group-read-only-p)
5021          ,gnus-summary-buffer no-highlight))
5022      t)
5023     ;; Force buttonizing this part.
5024     (let ((gnus-mime-buttonized-part-id current-id))
5025       (gnus-article-edit-done))
5026     (gnus-configure-windows 'article)
5027     (when (and current-id (integerp gnus-auto-select-part))
5028       (gnus-article-jump-to-part
5029        (min (max (+ current-id gnus-auto-select-part) 1)
5030             (with-current-buffer gnus-article-buffer
5031               (length gnus-article-mime-handle-alist)))))))
5032
5033 (defun gnus-mime-replace-part (file)
5034   "Replace MIME part under point with an external body."
5035   ;; Useful if file has already been saved to disk
5036   (interactive
5037    (list
5038     (read-file-name "Replace MIME part with file: "
5039                     (or mm-default-directory default-directory)
5040                     nil nil)))
5041   (gnus-mime-save-part-and-strip file))
5042
5043 (defun gnus-mime-save-part-and-strip (&optional file)
5044   "Save the MIME part under point then replace it with an external body.
5045 If FILE is given, use it for the external part."
5046   (interactive)
5047   (gnus-article-check-buffer)
5048   (when (gnus-group-read-only-p)
5049     (error "The current group does not support deleting of parts"))
5050   (when (mm-complicated-handles gnus-article-mime-handles)
5051     (error "\
5052 The current article has a complicated MIME structure, giving up..."))
5053   (let* ((data (get-text-property (point) 'gnus-data))
5054          (id (get-text-property (point) 'gnus-part))
5055          param
5056          (handles gnus-article-mime-handles))
5057     (unless file
5058       (setq file
5059             (and data (mm-save-part data "Delete MIME part and save to: "))))
5060     (when file
5061       (with-current-buffer (mm-handle-buffer data)
5062         (erase-buffer)
5063         (insert "Content-Type: " (mm-handle-media-type data))
5064         (mml-insert-parameter-string (cdr (mm-handle-type data))
5065                                      '(charset))
5066         ;; Add a filename for the sake of saving the part again.
5067         (mml-insert-parameter
5068          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5069         (insert "\n")
5070         (insert "Content-ID: " (message-make-message-id) "\n")
5071         (insert "Content-Transfer-Encoding: binary\n")
5072         (insert "\n"))
5073       (setcdr data
5074               (cdr (mm-make-handle nil
5075                                    `("message/external-body"
5076                                      (access-type . "LOCAL-FILE")
5077                                      (name . ,file)))))
5078       ;; (set-buffer gnus-summary-buffer)
5079       (gnus-article-edit-part handles id))))
5080
5081 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5082 ;; parts...>') but with stripping would be nice.
5083
5084 (defun gnus-mime-delete-part ()
5085   "Delete the MIME part under point.
5086 Replace it with some information about the removed part."
5087   (interactive)
5088   (gnus-article-check-buffer)
5089   (when (gnus-group-read-only-p)
5090     (error "The current group does not support deleting of parts"))
5091   (when (mm-complicated-handles gnus-article-mime-handles)
5092     (error "\
5093 The current article has a complicated MIME structure, giving up..."))
5094   (when (or gnus-expert-user
5095             (gnus-yes-or-no-p "\
5096 Deleting parts may malfunction or destroy the article; continue? "))
5097     (let* ((data (get-text-property (point) 'gnus-data))
5098            (id (get-text-property (point) 'gnus-part))
5099            (handles gnus-article-mime-handles)
5100            (description
5101             (let ((desc (mm-handle-description data)))
5102               (when desc
5103                 (mail-decode-encoded-word-string desc))))
5104            (filename (or (mm-handle-filename data) "(none)"))
5105            (type (mm-handle-media-type data)))
5106       (unless data
5107         (error "No MIME part under point"))
5108       (with-current-buffer (mm-handle-buffer data)
5109         (let ((bsize (buffer-size)))
5110           (erase-buffer)
5111           (insert
5112            (concat
5113             ",----\n"
5114             "| The following attachment has been deleted:\n"
5115             "|\n"
5116             "| Type:           " type "\n"
5117             "| Filename:       " filename "\n"
5118             "| Size (encoded): " (format "%s byte%s\n"
5119                                          bsize (if (= bsize 1)
5120                                                    ""
5121                                                  "s"))
5122             (when description
5123               (concat    "| Description:    " description "\n"))
5124             "`----\n"))
5125           (setcdr data
5126                   (cdr (mm-make-handle
5127                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5128                         (list "attachment")
5129                         (format "Deleted attachment (%s bytes)" bsize))))))
5130       ;; (set-buffer gnus-summary-buffer)
5131       (gnus-article-edit-part handles id))))
5132
5133 (defun gnus-mime-save-part ()
5134   "Save the MIME part under point."
5135   (interactive)
5136   (gnus-article-check-buffer)
5137   (let ((data (get-text-property (point) 'gnus-data)))
5138     (when data
5139       (mm-save-part data))))
5140
5141 (defun gnus-mime-pipe-part (&optional cmd)
5142   "Pipe the MIME part under point to a process.
5143 Use CMD as the process."
5144   (interactive)
5145   (gnus-article-check-buffer)
5146   (let ((data (get-text-property (point) 'gnus-data)))
5147     (when data
5148       (mm-pipe-part data cmd))))
5149
5150 (defun gnus-mime-view-part ()
5151   "Interactively choose a viewing method for the MIME part under point."
5152   (interactive)
5153   (gnus-article-check-buffer)
5154   (let ((data (get-text-property (point) 'gnus-data)))
5155     (when data
5156       (setq gnus-article-mime-handles
5157             (mm-merge-handles
5158              gnus-article-mime-handles (setq data (copy-sequence data))))
5159       (mm-interactively-view-part data))))
5160
5161 (defun gnus-mime-view-part-as-type-internal ()
5162   (gnus-article-check-buffer)
5163   (let* ((handle (get-text-property (point) 'gnus-data))
5164          (name (or
5165                 ;; Content-Type: foo/bar; name=...
5166                 (mail-content-type-get (mm-handle-type handle) 'name)
5167                 ;; Content-Disposition: attachment; filename=...
5168                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5169          (def-type (and name (mm-default-file-encoding name))))
5170     (or (and def-type (cons def-type 0))
5171         (and handle
5172              (equal (mm-handle-media-supertype handle) "text")
5173              '("text/plain" . 0))
5174         '("application/octet-stream" . 0))))
5175
5176 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5177   "Choose a MIME media type, and view the part as such.
5178 If non-nil, PRED is a predicate to use during completion to limit the
5179 available media-types."
5180   (interactive)
5181   (unless mime-type
5182     (setq mime-type
5183           (let ((default (gnus-mime-view-part-as-type-internal)))
5184             (gnus-completing-read
5185              "View as MIME type"
5186              (if pred
5187                  (gnus-remove-if-not pred (mailcap-mime-types))
5188                (mailcap-mime-types))
5189              nil nil nil
5190              (car default)))))
5191   (gnus-article-check-buffer)
5192   (let ((handle (get-text-property (point) 'gnus-data)))
5193     (when handle
5194       (when (equal (mm-handle-media-type handle) "message/external-body")
5195         (unless (mm-handle-cache handle)
5196           (mm-extern-cache-contents handle))
5197         (setq handle (mm-handle-cache handle)))
5198       (setq handle
5199             (mm-make-handle (mm-handle-buffer handle)
5200                             (cons mime-type (cdr (mm-handle-type handle)))
5201                             (mm-handle-encoding handle)
5202                             (mm-handle-undisplayer handle)
5203                             (mm-handle-disposition handle)
5204                             (mm-handle-description handle)
5205                             nil
5206                             (mm-handle-id handle)))
5207       (setq gnus-article-mime-handles
5208             (mm-merge-handles gnus-article-mime-handles handle))
5209       (when (mm-handle-displayed-p handle)
5210         (mm-remove-part handle))
5211       (gnus-mm-display-part handle))))
5212
5213 (defun gnus-mime-copy-part (&optional handle arg)
5214   "Put the MIME part under point into a new buffer.
5215 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5216 are decompressed."
5217   (interactive (list nil current-prefix-arg))
5218   (gnus-article-check-buffer)
5219   (unless handle
5220     (setq handle (get-text-property (point) 'gnus-data)))
5221   (when handle
5222     (let ((filename (mm-handle-filename handle))
5223           contents dont-decode charset coding-system)
5224       (mm-with-unibyte-buffer
5225         (mm-insert-part handle)
5226         (setq contents (or (condition-case nil
5227                                (mm-decompress-buffer filename nil 'sig)
5228                              (error
5229                               (setq dont-decode t)
5230                               nil))
5231                            (buffer-string))))
5232       (setq filename (cond (filename (file-name-nondirectory filename))
5233                            (dont-decode "*raw data*")
5234                            (t "*decoded*")))
5235       (cond
5236        (dont-decode)
5237        ((not arg)
5238         (unless (setq charset (mail-content-type-get
5239                                (mm-handle-type handle) 'charset))
5240           (unless (setq coding-system (mm-with-unibyte-buffer
5241                                         (insert contents)
5242                                         (mm-find-buffer-file-coding-system)))
5243             (setq charset gnus-newsgroup-charset))))
5244        ((numberp arg)
5245         (setq charset (or (cdr (assq arg
5246                                      gnus-summary-show-article-charset-alist))
5247                           (mm-read-coding-system "Charset: ")))))
5248       (switch-to-buffer (generate-new-buffer filename))
5249       (if (or coding-system
5250               (and charset
5251                    (setq coding-system (mm-charset-to-coding-system charset))
5252                    (not (eq coding-system 'ascii))))
5253           (progn
5254             (mm-enable-multibyte)
5255             (insert (mm-decode-coding-string contents coding-system))
5256             (setq buffer-file-coding-system
5257                   (if (boundp 'last-coding-system-used)
5258                       (symbol-value 'last-coding-system-used)
5259                     coding-system)))
5260         (mm-disable-multibyte)
5261         (insert contents)
5262         (setq buffer-file-coding-system mm-binary-coding-system))
5263       ;; We do it this way to make `normal-mode' set the appropriate mode.
5264       (unwind-protect
5265           (progn
5266             (setq buffer-file-name (expand-file-name filename))
5267             (normal-mode))
5268         (setq buffer-file-name nil))
5269       (goto-char (point-min)))))
5270
5271 (defun gnus-mime-print-part (&optional handle filename)
5272   "Print the MIME part under point."
5273   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5274   (gnus-article-check-buffer)
5275   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5276          (contents (and handle (mm-get-part handle)))
5277          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5278          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5279     (when contents
5280         (if printer
5281             (unwind-protect
5282                 (progn
5283                   (mm-save-part-to-file handle file)
5284                   (call-process shell-file-name nil
5285                                 (generate-new-buffer " *mm*")
5286                                 nil
5287                                 shell-command-switch
5288                                 (mm-mailcap-command
5289                                  printer file (mm-handle-type handle))))
5290               (delete-file file))
5291           (with-temp-buffer
5292             (insert contents)
5293             (gnus-print-buffer))
5294           (ps-despool filename)))))
5295
5296 (defun gnus-mime-inline-part (&optional handle arg)
5297   "Insert the MIME part under point into the current buffer.
5298 Compressed files like .gz and .bz2 are decompressed."
5299   (interactive (list nil current-prefix-arg))
5300   (gnus-article-check-buffer)
5301   (unless handle
5302     (setq handle (get-text-property (point) 'gnus-data)))
5303   (when handle
5304     (let ((b (point))
5305           (inhibit-read-only t)
5306           contents charset coding-system)
5307       (if (and (not arg) (mm-handle-undisplayer handle))
5308           (mm-remove-part handle)
5309         (mm-with-unibyte-buffer
5310           (mm-insert-part handle)
5311           (setq contents
5312                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5313                     (buffer-string))))
5314         (cond
5315          ((not arg)
5316           (unless (setq charset (mail-content-type-get
5317                                  (mm-handle-type handle) 'charset))
5318             (unless (setq coding-system
5319                           (mm-with-unibyte-buffer
5320                             (insert contents)
5321                             (mm-find-buffer-file-coding-system)))
5322               (setq charset gnus-newsgroup-charset))))
5323          ((numberp arg)
5324           (if (mm-handle-undisplayer handle)
5325               (mm-remove-part handle))
5326           (setq charset
5327                 (or (cdr (assq arg
5328                                gnus-summary-show-article-charset-alist))
5329                     (mm-read-coding-system "Charset: "))))
5330          ((mm-handle-undisplayer handle)
5331           (mm-remove-part handle)))
5332         (forward-line 2)
5333         (mm-display-inline handle)
5334         (goto-char b)))))
5335
5336 (defun gnus-mime-set-charset-parameters (handle charset)
5337   "Set CHARSET to parameters in HANDLE.
5338 CHARSET may either be a string or a symbol."
5339   (unless (stringp charset)
5340     (setq charset (symbol-name charset)))
5341   (if (stringp (car handle))
5342       (dolist (h (cdr handle))
5343         (gnus-mime-set-charset-parameters h charset))
5344     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5345                                             "message/external-body")
5346                                      (progn
5347                                        (unless (mm-handle-cache handle)
5348                                          (mm-extern-cache-contents handle))
5349                                        (mm-handle-cache handle))
5350                                    handle)))
5351            (param (assq 'charset (cdr type))))
5352       (if param
5353           (setcdr param charset)
5354         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5355
5356 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5357   "Insert the MIME part under point into the current buffer using the
5358 specified charset."
5359   (interactive (list nil current-prefix-arg))
5360   (gnus-article-check-buffer)
5361   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5362         (fun (get-text-property (point) 'gnus-callback))
5363         (gnus-newsgroup-ignored-charsets 'gnus-all)
5364         charset form preferred parts)
5365     (when handle
5366       (when (prog1
5367                 (and fun
5368                      (setq charset
5369                            (or (cdr (assq
5370                                      arg
5371                                      gnus-summary-show-article-charset-alist))
5372                                (mm-read-coding-system "Charset: "))))
5373               (if (mm-handle-undisplayer handle)
5374                   (mm-remove-part handle)))
5375         (gnus-mime-set-charset-parameters handle charset)
5376         (when (and (consp (setq form (cdr-safe fun)))
5377                    (setq form (ignore-errors
5378                                 (assq 'gnus-mime-display-alternative form)))
5379                    (setq preferred (caddr form))
5380                    (progn
5381                      (when (eq (car preferred) 'quote)
5382                        (setq preferred (cadr preferred)))
5383                      (not (equal preferred
5384                                  (get-text-property (point) 'gnus-data))))
5385                    (setq parts (get-text-property (point) 'gnus-part))
5386                    (setq parts (cdr (assq parts
5387                                           gnus-article-mime-handle-alist)))
5388                    (equal (mm-handle-media-type parts) "multipart/alternative")
5389                    (setq parts (reverse (cdr parts))))
5390           (setcar (cddr form)
5391                   (list 'quote (or (cadr (member preferred parts))
5392                                    (car parts)))))
5393         (funcall fun handle)))))
5394
5395 (defun gnus-mime-view-part-externally (&optional handle)
5396   "View the MIME part under point with an external viewer."
5397   (interactive)
5398   (gnus-article-check-buffer)
5399   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5400          (mm-user-display-methods nil)
5401          (mm-inlined-types nil)
5402          (mail-parse-charset gnus-newsgroup-charset)
5403          (mail-parse-ignored-charsets
5404           (with-current-buffer gnus-summary-buffer
5405             gnus-newsgroup-ignored-charsets))
5406          (type (mm-handle-media-type handle))
5407          (method (mailcap-mime-info type))
5408          (mm-enable-external t))
5409     (if (not (stringp method))
5410         (gnus-mime-view-part-as-type
5411          nil (lambda (type) (stringp (mailcap-mime-info type))))
5412       (when handle
5413         (mm-display-part handle nil t)))))
5414
5415 (defun gnus-mime-view-part-internally (&optional handle)
5416   "View the MIME part under point with an internal viewer.
5417 If no internal viewer is available, use an external viewer."
5418   (interactive)
5419   (gnus-article-check-buffer)
5420   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5421          (mm-inlined-types '(".*"))
5422          (mm-inline-large-images t)
5423          (mail-parse-charset gnus-newsgroup-charset)
5424          (mail-parse-ignored-charsets
5425           (with-current-buffer gnus-summary-buffer
5426             gnus-newsgroup-ignored-charsets))
5427          (inhibit-read-only t))
5428     (if (not (mm-inlinable-p handle))
5429         (gnus-mime-view-part-as-type
5430          nil (lambda (type) (mm-inlinable-p handle type)))
5431       (when handle
5432         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5433
5434 (defun gnus-mime-action-on-part (&optional action)
5435   "Do something with the MIME attachment at \(point\)."
5436   (interactive
5437    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5438   (gnus-article-check-buffer)
5439   (let ((action-pair (assoc action gnus-mime-action-alist)))
5440     (if action-pair
5441         (funcall (cdr action-pair)))))
5442
5443 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5444   "Call FUNCTION on MIME part N.
5445 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5446 If INTERACTIVE, call FUNCTION interactively."
5447   (let (window frame)
5448     ;; Check whether the article is displayed.
5449     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5450                  (setq window (get-buffer-window gnus-article-buffer t))
5451                  (frame-visible-p (setq frame (window-frame window))))
5452       (error "No article is displayed"))
5453     (with-current-buffer gnus-article-buffer
5454       ;; Check whether the article displays the right contents.
5455       (unless (with-current-buffer gnus-summary-buffer
5456                 (eq gnus-current-article (gnus-summary-article-number)))
5457         (error "You should select the right article first"))
5458       (if n
5459           (setq n (prefix-numeric-value n))
5460         (let ((pt (point)))
5461           (setq n (or (get-text-property pt 'gnus-part)
5462                       (and (not (bobp))
5463                            (get-text-property (1- pt) 'gnus-part))
5464                       (get-text-property (prog2
5465                                              (forward-line 1)
5466                                              (point)
5467                                            (goto-char pt))
5468                                          'gnus-part)
5469                       (get-text-property
5470                        (or (and (setq pt (previous-single-property-change
5471                                           pt 'gnus-part))
5472                                 (1- pt))
5473                            (next-single-property-change (point) 'gnus-part)
5474                            (point))
5475                        'gnus-part)
5476                       1))))
5477       ;; Check whether the specified part exists.
5478       (when (> n (length gnus-article-mime-handle-alist))
5479         (error "No such part")))
5480     (unless
5481         (progn
5482           ;; To select the window is needed so that the cursor
5483           ;; might be visible on the MIME button.
5484           (select-window (prog1
5485                              window
5486                            (setq window (selected-window))
5487                            ;; Article may be displayed in the other frame.
5488                            (gnus-select-frame-set-input-focus
5489                             (prog1
5490                                 frame
5491                               (setq frame (selected-frame))))))
5492           (when (gnus-article-goto-part n)
5493             ;; We point the cursor and the arrow at the MIME button
5494             ;; when the `function' prompt the user for something.
5495             (unless (and (pos-visible-in-window-p)
5496                          (> (count-lines (point) (window-end))
5497                             (/ (1- (window-height)) 3)))
5498               (recenter (/ (1- (window-height)) 3)))
5499             (let ((cursor-in-non-selected-windows t)
5500                   (overlay-arrow-string "=>")
5501                   (overlay-arrow-position (point-marker)))
5502               (unwind-protect
5503                   (cond
5504                    ((and no-handle interactive)
5505                     (call-interactively function))
5506                    (no-handle
5507                     (funcall function))
5508                    (interactive
5509                     (call-interactively
5510                      function (get-text-property (point) 'gnus-data)))
5511                    (t
5512                     (funcall function
5513                              (get-text-property (point) 'gnus-data))))
5514                 (set-marker overlay-arrow-position nil)
5515                 (unless gnus-auto-select-part
5516                   (gnus-select-frame-set-input-focus frame)
5517                   (select-window window))))
5518             t))
5519       (if gnus-inhibit-mime-unbuttonizing
5520           ;; This is the default though the program shouldn't reach here.
5521           (error "No such part")
5522         ;; The part which doesn't have the MIME button is selected.
5523         ;; So, we display all the buttons and redo it.
5524         (let ((gnus-inhibit-mime-unbuttonizing t))
5525           (gnus-summary-show-article)
5526           (gnus-article-part-wrapper n function no-handle))))))
5527
5528 (defun gnus-article-pipe-part (n)
5529   "Pipe MIME part N, which is the numerical prefix."
5530   (interactive "P")
5531   (gnus-article-part-wrapper n 'mm-pipe-part))
5532
5533 (defun gnus-article-save-part (n)
5534   "Save MIME part N, which is the numerical prefix."
5535   (interactive "P")
5536   (gnus-article-part-wrapper n 'mm-save-part))
5537
5538 (defun gnus-article-interactively-view-part (n)
5539   "View MIME part N interactively, which is the numerical prefix."
5540   (interactive "P")
5541   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5542
5543 (defun gnus-article-copy-part (n)
5544   "Copy MIME part N, which is the numerical prefix."
5545   (interactive "P")
5546   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5547
5548 (defun gnus-article-view-part-as-charset (n)
5549   "View MIME part N using a specified charset.
5550 N is the numerical prefix."
5551   (interactive "P")
5552   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5553
5554 (defun gnus-article-view-part-externally (n)
5555   "View MIME part N externally, which is the numerical prefix."
5556   (interactive "P")
5557   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5558
5559 (defun gnus-article-inline-part (n)
5560   "Inline MIME part N, which is the numerical prefix."
5561   (interactive "P")
5562   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5563
5564 (defun gnus-article-save-part-and-strip (n)
5565   "Save MIME part N and replace it with an external body.
5566 N is the numerical prefix."
5567   (interactive "P")
5568   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5569
5570 (defun gnus-article-replace-part (n)
5571   "Replace MIME part N with an external body.
5572 N is the numerical prefix."
5573   (interactive "P")
5574   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5575
5576 (defun gnus-article-delete-part (n)
5577   "Delete MIME part N and add some information about the removed part.
5578 N is the numerical prefix."
5579   (interactive "P")
5580   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5581
5582 (defun gnus-article-view-part-as-type (n)
5583   "Choose a MIME media type, and view part N as such.
5584 N is the numerical prefix."
5585   (interactive "P")
5586   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5587
5588 (defun gnus-article-mime-match-handle-first (condition)
5589   (if condition
5590       (let (n)
5591         (dolist (ihandle gnus-article-mime-handle-alist)
5592           (if (and (cond
5593                     ((functionp condition)
5594                      (funcall condition (cdr ihandle)))
5595                     ((eq condition 'undisplayed)
5596                      (not (or (mm-handle-undisplayer (cdr ihandle))
5597                               (equal (mm-handle-media-type (cdr ihandle))
5598                                      "multipart/alternative"))))
5599                     ((eq condition 'undisplayed-alternative)
5600                      (not (mm-handle-undisplayer (cdr ihandle))))
5601                     (t t))
5602                    (gnus-article-goto-part (car ihandle))
5603                    (or (not n) (< (car ihandle) n)))
5604               (setq n (car ihandle))))
5605         (or n 1))
5606     1))
5607
5608 (defun gnus-article-view-part (&optional n)
5609   "View MIME part N, which is the numerical prefix.
5610 If the part is already shown, hide the part.  If N is nil, view
5611 all parts."
5612   (interactive "P")
5613   (with-current-buffer gnus-article-buffer
5614     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5615                              gnus-article-mime-match-handle-function)))
5616     (when (> n (length gnus-article-mime-handle-alist))
5617       (error "No such part"))
5618     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5619       (when (gnus-article-goto-part n)
5620         (if (equal (car handle) "multipart/alternative")
5621             (progn
5622               (beginning-of-line) ;; Make it toggle subparts
5623               (gnus-article-press-button))
5624           (when (eq (gnus-mm-display-part handle) 'internal)
5625             (gnus-set-window-start)))))))
5626
5627 (defsubst gnus-article-mime-total-parts ()
5628   (if (bufferp (car gnus-article-mime-handles))
5629       1 ;; single part
5630     (1- (length gnus-article-mime-handles))))
5631
5632 (defun gnus-mm-display-part (handle)
5633   "Display HANDLE and fix MIME button."
5634   (let ((id (get-text-property (point) 'gnus-part))
5635         (point (point))
5636         (inhibit-read-only t))
5637     (forward-line 1)
5638     (prog1
5639         (let ((window (selected-window))
5640               (mail-parse-charset gnus-newsgroup-charset)
5641               (mail-parse-ignored-charsets
5642                (if (gnus-buffer-live-p gnus-summary-buffer)
5643                    (with-current-buffer gnus-summary-buffer
5644                      gnus-newsgroup-ignored-charsets)
5645                  nil)))
5646           (save-excursion
5647             (unwind-protect
5648                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5649                       (beg (point)))
5650                   (when win
5651                     (select-window win))
5652                   (goto-char point)
5653                   (forward-line)
5654                   (if (mm-handle-displayed-p handle)
5655                       ;; This will remove the part.
5656                       (mm-display-part handle)
5657                     (save-restriction
5658                       (narrow-to-region (point)
5659                                         (if (eobp) (point) (1+ (point))))
5660                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5661                       ;; We narrow to the part itself and
5662                       ;; then call the treatment functions.
5663                       (goto-char (point-min))
5664                       (forward-line 1)
5665                       (narrow-to-region (point) (point-max))
5666                       (gnus-treat-article
5667                        nil id
5668                        (gnus-article-mime-total-parts)
5669                        (mm-handle-media-type handle)))))
5670               (if (window-live-p window)
5671                   (select-window window)))))
5672       (goto-char point)
5673       (gnus-delete-line)
5674       (gnus-insert-mime-button
5675        handle id (list (mm-handle-displayed-p handle)))
5676       (goto-char point))))
5677
5678 (defun gnus-article-goto-part (n)
5679   "Go to MIME part N."
5680   (when gnus-break-pages
5681     (widen))
5682   (prog1
5683       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5684             part handle end next handles)
5685         (when start
5686           (goto-char start)
5687           (if (setq handle (get-text-property start 'gnus-data))
5688               start
5689             ;; Go to the displayed subpart, assuming this is
5690             ;; multipart/alternative.
5691             (setq part start
5692                   end (point-at-eol))
5693             (while (and (not handle)
5694                         part
5695                         (< part end)
5696                         (setq next (text-property-not-all part end
5697                                                           'gnus-data nil)))
5698               (setq part next
5699                     handle (get-text-property part 'gnus-data))
5700               (push (cons handle part) handles)
5701               (unless (mm-handle-displayed-p handle)
5702                 (setq handle nil
5703                       part (text-property-any part end 'gnus-data nil))))
5704             (unless handle
5705               ;; No subpart is displayed, so we find preferred one.
5706               (setq part
5707                     (cdr (assq (mm-preferred-alternative
5708                                 (nreverse (mapcar 'car handles)))
5709                                handles))))
5710             (if part
5711                 (goto-char (1+ part))
5712               start))))
5713     (when gnus-break-pages
5714       (gnus-narrow-to-page))))
5715
5716 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5717   (let ((gnus-tmp-name
5718          (or (mm-handle-filename handle)
5719              (mail-content-type-get (mm-handle-type handle) 'url)
5720              ""))
5721         (gnus-tmp-type (mm-handle-media-type handle))
5722         (gnus-tmp-description (or (mm-handle-description handle) ""))
5723         (gnus-tmp-dots
5724          (if (if displayed (car displayed)
5725                (mm-handle-displayed-p handle))
5726              "" "..."))
5727         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5728                            (buffer-size)))
5729         gnus-tmp-type-long b e)
5730     (when (string-match ".*/" gnus-tmp-name)
5731       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5732     (setq gnus-tmp-type-long (concat gnus-tmp-type
5733                                      (and (not (equal gnus-tmp-name ""))
5734                                           (concat "; " gnus-tmp-name))))
5735     (unless (equal gnus-tmp-description "")
5736       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5737     (unless (bolp)
5738       (insert "\n"))
5739     (setq b (point))
5740     (gnus-eval-format
5741      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5742      `(keymap ,gnus-mime-button-map
5743               gnus-callback gnus-mm-display-part
5744               gnus-part ,gnus-tmp-id
5745               article-type annotation
5746               gnus-data ,handle
5747               rear-nonsticky t))
5748     (setq e (if (bolp)
5749                 ;; Exclude a newline.
5750                 (1- (point))
5751               (point)))
5752     (when gnus-article-button-face
5753       (gnus-overlay-put (gnus-make-overlay b e nil t)
5754                         'face gnus-article-button-face))
5755     (widget-convert-button
5756      'link b e
5757      :mime-handle handle
5758      :action 'gnus-widget-press-button
5759      :button-keymap gnus-mime-button-map
5760      :help-echo
5761      (lambda (widget)
5762        ;; Needed to properly clear the message due to a bug in
5763        ;; wid-edit (XEmacs only).
5764        (if (boundp 'help-echo-owns-message)
5765            (setq help-echo-owns-message t))
5766        (format
5767         "%S: %s the MIME part; %S: more options"
5768         (aref gnus-mouse-2 0)
5769         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5770             "hide" "show")
5771         (aref gnus-down-mouse-3 0))))))
5772
5773 (defun gnus-widget-press-button (elems el)
5774   (goto-char (widget-get elems :from))
5775   (gnus-article-press-button))
5776
5777 (defvar gnus-displaying-mime nil)
5778
5779 (defun gnus-display-mime (&optional ihandles)
5780   "Display the MIME parts."
5781   (save-excursion
5782     (save-selected-window
5783       (let ((window (get-buffer-window gnus-article-buffer))
5784             (point (point)))
5785         (when window
5786           (select-window window)
5787           ;; We have to do this since selecting the window
5788           ;; may change the point.  So we set the window point.
5789           (set-window-point window point)))
5790       (let ((handles ihandles)
5791             (inhibit-read-only t)
5792             handle)
5793         (cond (handles)
5794               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5795                (when gnus-article-emulate-mime
5796                  (mm-uu-dissect-text-parts handles)))
5797               (gnus-article-emulate-mime
5798                (setq handles (mm-uu-dissect))))
5799         (when (and (not ihandles)
5800                    (not gnus-displaying-mime))
5801           ;; Top-level call; we clean up.
5802           (when gnus-article-mime-handles
5803             (mm-destroy-parts gnus-article-mime-handles)
5804             (setq gnus-article-mime-handle-alist nil));; A trick.
5805           (setq gnus-article-mime-handles handles)
5806           ;; We allow users to glean info from the handles.
5807           (when gnus-article-mime-part-function
5808             (gnus-mime-part-function handles)))
5809         (if (and handles
5810                  (or (not (stringp (car handles)))
5811                      (cdr handles)))
5812             (progn
5813               (when (and (not ihandles)
5814                          (not gnus-displaying-mime))
5815                 ;; Clean up for mime parts.
5816                 (article-goto-body)
5817                 (delete-region (point) (point-max)))
5818               (let ((gnus-displaying-mime t))
5819                 (gnus-mime-display-part handles)))
5820           (save-restriction
5821             (article-goto-body)
5822             (narrow-to-region (point) (point-max))
5823             (gnus-treat-article nil 1 1 "text/plain")
5824             (widen)))
5825         (unless ihandles
5826           ;; Highlight the headers.
5827           (save-excursion
5828             (save-restriction
5829               (article-goto-body)
5830               (narrow-to-region (point-min) (point))
5831               (gnus-article-save-original-date
5832                (gnus-treat-article 'head)))))))
5833     ;; Cope with broken MIME messages.
5834     (goto-char (point-max))
5835     (unless (bolp)
5836       (insert "\n"))))
5837
5838 (defcustom gnus-mime-display-multipart-as-mixed nil
5839   "Display \"multipart\" parts as  \"multipart/mixed\".
5840
5841 If t, it overrides nil values of
5842 `gnus-mime-display-multipart-alternative-as-mixed' and
5843 `gnus-mime-display-multipart-related-as-mixed'."
5844   :group 'gnus-article-mime
5845   :type 'boolean)
5846
5847 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5848   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5849   :version "22.1"
5850   :group 'gnus-article-mime
5851   :type 'boolean)
5852
5853 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5854   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5855
5856 If displaying \"text/html\" is discouraged \(see
5857 `mm-discouraged-alternatives'\) images or other material inside a
5858 \"multipart/related\" part might be overlooked when this variable is nil."
5859   :version "22.1"
5860   :group 'gnus-article-mime
5861   :type 'boolean)
5862
5863 (defun gnus-mime-display-part (handle)
5864   (cond
5865    ;; Maybe a broken MIME message.
5866    ((null handle))
5867    ;; Single part.
5868    ((not (stringp (car handle)))
5869     (gnus-mime-display-single handle))
5870    ;; User-defined multipart
5871    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5872     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5873              handle))
5874    ;; multipart/alternative
5875    ((and (equal (car handle) "multipart/alternative")
5876          (not (or gnus-mime-display-multipart-as-mixed
5877                   gnus-mime-display-multipart-alternative-as-mixed)))
5878     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5879       (push (cons id handle) gnus-article-mime-handle-alist)
5880       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5881    ;; multipart/related
5882    ((and (equal (car handle) "multipart/related")
5883          (not (or gnus-mime-display-multipart-as-mixed
5884                   gnus-mime-display-multipart-related-as-mixed)))
5885     ;;;!!!We should find the start part, but we just default
5886     ;;;!!!to the first part.
5887     ;;(gnus-mime-display-part (cadr handle))
5888     ;;;!!! Most multipart/related is an HTML message plus images.
5889     ;;;!!! Unfortunately we are unable to let W3 display those
5890     ;;;!!! included images, so we just display it as a mixed multipart.
5891     ;;(gnus-mime-display-mixed (cdr handle))
5892     ;;;!!! No, w3 can display everything just fine.
5893     (gnus-mime-display-part (cadr handle)))
5894    ((equal (car handle) "multipart/signed")
5895     (gnus-add-wash-type 'signed)
5896     (gnus-mime-display-security handle))
5897    ((equal (car handle) "multipart/encrypted")
5898     (gnus-add-wash-type 'encrypted)
5899     (gnus-mime-display-security handle))
5900    ;; Other multiparts are handled like multipart/mixed.
5901    (t
5902     (gnus-mime-display-mixed (cdr handle)))))
5903
5904 (defun gnus-mime-part-function (handles)
5905   (if (stringp (car handles))
5906       (mapcar 'gnus-mime-part-function (cdr handles))
5907     (funcall gnus-article-mime-part-function handles)))
5908
5909 (defun gnus-mime-display-mixed (handles)
5910   (mapcar 'gnus-mime-display-part handles))
5911
5912 (defun gnus-mime-display-single (handle)
5913   (let ((type (mm-handle-media-type handle))
5914         (ignored gnus-ignored-mime-types)
5915         (not-attachment t)
5916         (move nil)
5917         display text)
5918     (catch 'ignored
5919       (progn
5920         (while ignored
5921           (when (string-match (pop ignored) type)
5922             (throw 'ignored nil)))
5923         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5924                                (with-current-buffer gnus-summary-buffer
5925                                  gnus-inhibit-images)
5926                              gnus-inhibit-images)
5927                            (string-match "\\`image/" type)))
5928                  (setq not-attachment
5929                        (and (not (mm-inline-override-p handle))
5930                             (or (not (mm-handle-disposition handle))
5931                                 (equal (car (mm-handle-disposition handle))
5932                                        "inline")
5933                                 (mm-attachment-override-p handle))))
5934                  (mm-automatic-display-p handle)
5935                  (or (and
5936                       (mm-inlinable-p handle)
5937                       (mm-inlined-p handle))
5938                      (mm-automatic-external-display-p type)))
5939             (setq display t)
5940           (when (equal (mm-handle-media-supertype handle) "text")
5941             (setq text t)))
5942         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5943               beg)
5944           (push (cons id handle) gnus-article-mime-handle-alist)
5945           (when (and display
5946                      (equal (mm-handle-media-supertype handle) "message"))
5947             (insert-char
5948              ?\n
5949              (cond ((not (bolp)) 2)
5950                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5951                    (t 1))))
5952           (when (or (not display)
5953                     (not (gnus-unbuttonized-mime-type-p type))
5954                     (eq id gnus-mime-buttonized-part-id))
5955             (gnus-insert-mime-button
5956              handle id (list (or display (and not-attachment text))))
5957             (gnus-article-insert-newline)
5958             ;; Remember modify the number of forward lines.
5959             (setq move t))
5960           (setq beg (point))
5961           (cond
5962            (display
5963             (when move
5964               (forward-line -1)
5965               (setq beg (point)))
5966             (let ((mail-parse-charset gnus-newsgroup-charset)
5967                   (mail-parse-ignored-charsets
5968                    (save-excursion (condition-case ()
5969                                        (set-buffer gnus-summary-buffer)
5970                                      (error))
5971                                    gnus-newsgroup-ignored-charsets)))
5972               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5973             (goto-char (point-max)))
5974            ((and text not-attachment)
5975             (when move
5976               (forward-line -1)
5977               (setq beg (point)))
5978             (gnus-article-insert-newline)
5979             (mm-display-inline handle)
5980             (goto-char (point-max))))
5981           ;; Do highlighting.
5982           (save-excursion
5983             (save-restriction
5984               (narrow-to-region beg (point))
5985               (if (eq handle gnus-article-mime-handles)
5986                   ;; The format=flowed case.
5987                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5988                 ;; Don't count signature parts that are never displayed.
5989                 ;; The part number should be re-calculated supposing this
5990                 ;; might be a message/rfc822 part.
5991                 (let (handles)
5992                   (dolist (part gnus-article-mime-handles)
5993                     (unless (or (stringp part)
5994                                 (equal (car (mm-handle-type part))
5995                                        "application/pgp-signature"))
5996                       (push part handles)))
5997                   (gnus-treat-article
5998                    nil (length (memq handle handles)) (length handles)
5999                    (mm-handle-media-type handle)))))))))))
6000
6001 (defun gnus-unbuttonized-mime-type-p (type)
6002   "Say whether TYPE is to be unbuttonized."
6003   (unless gnus-inhibit-mime-unbuttonizing
6004     (when (catch 'found
6005             (let ((types gnus-unbuttonized-mime-types))
6006               (while types
6007                 (when (string-match (pop types) type)
6008                   (throw 'found t)))))
6009       (not (catch 'found
6010              (let ((types gnus-buttonized-mime-types))
6011                (while types
6012                  (when (string-match (pop types) type)
6013                    (throw 'found t)))))))))
6014
6015 (defun gnus-article-insert-newline ()
6016   "Insert a newline, but mark it as undeletable."
6017   (gnus-put-text-property
6018    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6019
6020 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6021   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6022          (ihandles handles)
6023          (point (point))
6024          handle (inhibit-read-only t) from props begend not-pref)
6025     (save-window-excursion
6026       (save-restriction
6027         (when ibegend
6028           (narrow-to-region (car ibegend)
6029                             (or (cdr ibegend)
6030                                 (progn
6031                                   (goto-char (car ibegend))
6032                                   (forward-line 2)
6033                                   (point))))
6034           (delete-region (point-min) (point-max))
6035           (mm-remove-parts handles))
6036         (setq begend (list (point-marker)))
6037         ;; Do the toggle.
6038         (unless (setq not-pref (cadr (member preferred ihandles)))
6039           (setq not-pref (car ihandles)))
6040         (when (or ibegend
6041                   (not preferred)
6042                   (not (gnus-unbuttonized-mime-type-p
6043                         "multipart/alternative")))
6044           (gnus-add-text-properties
6045            (setq from (point))
6046            (progn
6047              (insert (format "%d.  " id))
6048              (point))
6049            `(gnus-callback
6050              (lambda (handles)
6051                (unless ,(not ibegend)
6052                  (setq gnus-article-mime-handle-alist
6053                        ',gnus-article-mime-handle-alist))
6054                (gnus-mime-display-alternative
6055                 ',ihandles ',not-pref ',begend ,id))
6056              keymap ,gnus-mime-button-map
6057              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6058              face ,gnus-article-button-face
6059              gnus-part ,id
6060              article-type multipart
6061              rear-nonsticky t))
6062           (widget-convert-button 'link from (point)
6063                                  :action 'gnus-widget-press-button
6064                                  :button-keymap gnus-widget-button-keymap)
6065           ;; Do the handles
6066           (while (setq handle (pop handles))
6067             (gnus-add-text-properties
6068              (setq from (point))
6069              (progn
6070                (insert (format "(%c) %-18s"
6071                                (if (equal handle preferred) ?* ? )
6072                                (mm-handle-media-type handle)))
6073                (point))
6074              `(gnus-callback
6075                (lambda (handles)
6076                  (unless ,(not ibegend)
6077                    (setq gnus-article-mime-handle-alist
6078                          ',gnus-article-mime-handle-alist))
6079                  (gnus-mime-display-alternative
6080                   ',ihandles ',handle ',begend ,id))
6081                keymap ,gnus-mime-button-map
6082                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6083                face ,gnus-article-button-face
6084                gnus-part ,id
6085                gnus-data ,handle
6086                rear-nonsticky t))
6087             (widget-convert-button 'link from (point)
6088                                    :action 'gnus-widget-press-button
6089                                    :button-keymap gnus-widget-button-keymap)
6090             (insert "  "))
6091           (insert "\n\n"))
6092         (when preferred
6093           (if (stringp (car preferred))
6094               (gnus-display-mime preferred)
6095             (let ((mail-parse-charset gnus-newsgroup-charset)
6096                   (mail-parse-ignored-charsets
6097                    (with-current-buffer gnus-summary-buffer
6098                      gnus-newsgroup-ignored-charsets)))
6099               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6100               ;; Do highlighting.
6101               (save-excursion
6102                 (save-restriction
6103                   (narrow-to-region (car begend) (point-max))
6104                   (gnus-treat-article
6105                    nil (length gnus-article-mime-handle-alist)
6106                    (gnus-article-mime-total-parts)
6107                    (mm-handle-media-type preferred))))))
6108           (goto-char (point-max))
6109           (setcdr begend (point-marker)))))
6110     (when ibegend
6111       (goto-char point))))
6112
6113 (defconst gnus-article-wash-status-strings
6114   (let ((alist '((cite "c" "Possible hidden citation text"
6115                        " " "All citation text visible")
6116                  (headers "h" "Hidden headers"
6117                           " " "All headers visible.")
6118                  (pgp "p" "Encrypted or signed message status hidden"
6119                       " " "No hidden encryption nor digital signature status")
6120                  (signature "s" "Signature has been hidden"
6121                             " " "Signature is visible")
6122                  (overstrike "o" "Overstrike (^H) characters applied"
6123                              " " "No overstrike characters applied")
6124                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6125                            " " "No /*_emphasis_*/ characters applied")))
6126         result)
6127     (dolist (entry alist result)
6128       (let ((key (nth 0 entry))
6129             (on (copy-sequence (nth 1 entry)))
6130             (on-help (nth 2 entry))
6131             (off (copy-sequence (nth 3 entry)))
6132             (off-help (nth 4 entry)))
6133         (put-text-property 0 1 'help-echo on-help on)
6134         (put-text-property 0 1 'help-echo off-help off)
6135         (push (list key on off) result))))
6136   "Alist of strings describing wash status in the mode line.
6137 Each entry has the form (KEY ON OF), where the KEY is a symbol
6138 representing the particular washing function, ON is the string to use
6139 in the article mode line when the washing function is active, and OFF
6140 is the string to use when it is inactive.")
6141
6142 (defun gnus-article-wash-status-entry (key value)
6143   (let ((entry (assoc key gnus-article-wash-status-strings)))
6144     (if value (nth 1 entry) (nth 2 entry))))
6145
6146 (defun gnus-article-wash-status ()
6147   "Return a string which display status of article washing."
6148   (with-current-buffer gnus-article-buffer
6149     (let ((cite (memq 'cite gnus-article-wash-types))
6150           (headers (memq 'headers gnus-article-wash-types))
6151           (boring (memq 'boring-headers gnus-article-wash-types))
6152           (pgp (memq 'pgp gnus-article-wash-types))
6153           (pem (memq 'pem gnus-article-wash-types))
6154           (signed (memq 'signed gnus-article-wash-types))
6155           (encrypted (memq 'encrypted gnus-article-wash-types))
6156           (signature (memq 'signature gnus-article-wash-types))
6157           (overstrike (memq 'overstrike gnus-article-wash-types))
6158           (emphasis (memq 'emphasis gnus-article-wash-types)))
6159       (concat
6160        (gnus-article-wash-status-entry 'cite cite)
6161        (gnus-article-wash-status-entry 'headers (or headers boring))
6162        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6163        (gnus-article-wash-status-entry 'signature signature)
6164        (gnus-article-wash-status-entry 'overstrike overstrike)
6165        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6166
6167 (defun gnus-add-wash-type (type)
6168   "Add a washing of TYPE to the current status."
6169   (add-to-list 'gnus-article-wash-types type))
6170
6171 (defun gnus-delete-wash-type (type)
6172   "Add a washing of TYPE to the current status."
6173   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6174
6175 (defun gnus-add-image (category image)
6176   "Add IMAGE of CATEGORY to the list of displayed images."
6177   (let ((entry (assq category gnus-article-image-alist)))
6178     (unless entry
6179       (setq entry (list category))
6180       (push entry gnus-article-image-alist))
6181     (nconc entry (list image))))
6182
6183 (defun gnus-delete-images (category)
6184   "Delete all images in CATEGORY."
6185   (let ((entry (assq category gnus-article-image-alist)))
6186     (dolist (image (cdr entry))
6187       (gnus-remove-image image category))
6188     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6189     (gnus-delete-wash-type category)))
6190
6191 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6192
6193 (defun gnus-article-maybe-hide-headers ()
6194   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6195 Provided for backwards compatibility."
6196   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6197                  (not (with-current-buffer gnus-summary-buffer
6198                         gnus-have-all-headers)))
6199              (not gnus-inhibit-hiding))
6200     (gnus-article-hide-headers)))
6201
6202 (declare-function shr-put-image "shr" (data alt &optional flags))
6203
6204 (defun gnus-shr-put-image (data alt &optional flags)
6205   "Put image DATA with a string ALT.  Enable image to be deleted."
6206   (let ((image (if flags
6207                    (shr-put-image data (propertize (or alt "*")
6208                                                    'gnus-image-category 'shr)
6209                                   flags)
6210                  ;; Old `shr-put-image' doesn't take the optional `flags'
6211                  ;; argument.
6212                  (shr-put-image data (propertize (or alt "*")
6213                                                  'gnus-image-category 'shr)))))
6214     (when image
6215       (gnus-add-image 'shr image))))
6216
6217 ;;; Article savers.
6218
6219 (defun gnus-output-to-file (file-name)
6220   "Append the current article to a file named FILE-NAME.
6221 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6222 text and used as the value of the coding cookie which is added to the
6223 top of a file.  Otherwise, this function saves a raw article without
6224 the coding cookie."
6225   (let* ((artbuf (current-buffer))
6226          (file-name-coding-system nnmail-pathname-coding-system)
6227          (coding gnus-article-save-coding-system)
6228          (coding-system-for-read (if coding
6229                                      nil ;; Rely on the coding cookie.
6230                                    mm-text-coding-system))
6231          (coding-system-for-write (or coding
6232                                       mm-text-coding-system-for-write
6233                                       mm-text-coding-system))
6234          (exists (file-exists-p file-name)))
6235     (with-temp-buffer
6236       (when exists
6237         (insert-file-contents file-name)
6238         (goto-char (point-min))
6239         ;; Remove the existing coding cookie.
6240         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6241           (delete-region (match-beginning 0) (match-end 0))))
6242       (goto-char (point-max))
6243       (insert-buffer-substring artbuf)
6244       ;; Append newline at end of the buffer as separator, and then
6245       ;; save it to file.
6246       (goto-char (point-max))
6247       (insert "\n")
6248       (when coding
6249         ;; If the coding system is not suitable to encode the text,
6250         ;; ask a user for a proper one.
6251         (when (fboundp 'select-safe-coding-system)
6252           (setq coding (coding-system-base
6253                         (save-window-excursion
6254                           (select-safe-coding-system (point-min) (point-max)
6255                                                      coding))))
6256           (setq coding-system-for-write
6257                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6258                     coding)))
6259         (goto-char (point-min))
6260         ;; Add the coding cookie.
6261         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6262                         coding-system-for-write)))
6263       (if exists
6264           (progn
6265             (write-region (point-min) (point-max) file-name nil 'no-message)
6266             (message "Appended to %s" file-name))
6267         (write-region (point-min) (point-max) file-name))))
6268   t)
6269
6270 (defun gnus-narrow-to-page (&optional arg)
6271   "Narrow the article buffer to a page.
6272 If given a numerical ARG, move forward ARG pages."
6273   (interactive "P")
6274   (setq arg (if arg (prefix-numeric-value arg) 0))
6275   (with-current-buffer gnus-article-buffer
6276     (widen)
6277     ;; Remove any old next/prev buttons.
6278     (when (gnus-visual-p 'page-marker)
6279       (let ((inhibit-read-only t))
6280         (gnus-remove-text-with-property 'gnus-prev)
6281         (gnus-remove-text-with-property 'gnus-next)))
6282     (let (st nd pt)
6283       (when (save-excursion
6284               (cond ((< arg 0)
6285                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6286                          (prog1
6287                              (setq nd (match-beginning 0)
6288                                    pt nd)
6289                            (when (re-search-backward page-delimiter nil t)
6290                              (setq st (match-end 0))))
6291                        (when (re-search-forward page-delimiter nil t)
6292                          (setq nd (match-beginning 0)
6293                                pt (point-min)))))
6294                     ((> arg 0)
6295                      (if (re-search-forward page-delimiter nil 'move arg)
6296                          (prog1
6297                              (setq st (match-end 0)
6298                                    pt st)
6299                            (when (re-search-forward page-delimiter nil t)
6300                              (setq nd (match-beginning 0))))
6301                        (when (re-search-backward page-delimiter nil t)
6302                          (setq st (match-end 0)
6303                                pt (point-max)))))
6304                     (t
6305                      (when (re-search-backward page-delimiter nil t)
6306                        (goto-char (setq st (match-end 0))))
6307                      (when (re-search-forward page-delimiter nil t)
6308                        (setq nd (match-beginning 0)))
6309                      (or st nd))))
6310         (setq gnus-page-broken t)
6311         (when pt (goto-char pt))
6312         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6313         (when (gnus-visual-p 'page-marker)
6314           (save-excursion
6315             (when nd
6316               (goto-char nd)
6317               (gnus-insert-next-page-button))
6318             (when st
6319               (goto-char st)
6320               (gnus-insert-prev-page-button))))))))
6321
6322 ;; Article mode commands
6323
6324 (defun gnus-article-goto-next-page ()
6325   "Show the next page of the article."
6326   (interactive)
6327   (when (gnus-article-next-page)
6328     (goto-char (point-min))
6329     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6330
6331
6332 (defun gnus-article-goto-prev-page ()
6333   "Show the previous page of the article."
6334   (interactive)
6335   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6336       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6337     (gnus-article-prev-page nil)))
6338
6339 ;; This is cleaner but currently breaks `gnus-pick-mode':
6340 ;;
6341 ;; (defun gnus-article-goto-next-page ()
6342 ;;   "Show the next page of the article."
6343 ;;   (interactive)
6344 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6345 ;;     (gnus-summary-next-page)))
6346 ;;
6347 ;; (defun gnus-article-goto-prev-page ()
6348 ;;   "Show the next page of the article."
6349 ;;   (interactive)
6350 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6351 ;;     (gnus-summary-prev-page)))
6352
6353 (defun gnus-article-next-page (&optional lines)
6354   "Show the next page of the current article.
6355 If end of article, return non-nil.  Otherwise return nil.
6356 Argument LINES specifies lines to be scrolled up."
6357   (interactive "p")
6358   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6359   (if (and (not (and gnus-article-over-scroll
6360                      (> (count-lines (window-start) (point-max))
6361                         (if (featurep 'xemacs)
6362                             (or lines (1- (window-height)))
6363                           (+ (or lines (1- (window-height))) scroll-margin)))))
6364            (save-excursion
6365              (end-of-line)
6366              (and (pos-visible-in-window-p)     ;Not continuation line.
6367                   (>= (point) (point-max)))))
6368       ;; Nothing in this page.
6369       (if (or (not gnus-page-broken)
6370               (save-excursion
6371                 (save-restriction
6372                   (widen)
6373                   (forward-line)
6374                   (eobp)))) ;Real end-of-buffer?
6375           (progn
6376             (when gnus-article-over-scroll
6377               (gnus-article-next-page-1 lines))
6378             t)                  ;Nothing more.
6379         (gnus-narrow-to-page 1)         ;Go to next page.
6380         nil)
6381     ;; More in this page.
6382     (gnus-article-next-page-1 lines)
6383     nil))
6384
6385 (defun gnus-article-beginning-of-window ()
6386   "Move point to the beginning of the window.
6387 In Emacs, the point is placed at the line number which `scroll-margin'
6388 specifies."
6389   (if (featurep 'xemacs)
6390       (move-to-window-line 0)
6391     ;; There is an obscure bug in Emacs that makes it impossible to
6392     ;; scroll past big pictures in the article buffer.  Try to fix
6393     ;; this by adding a sanity check by counting the lines visible.
6394     (when (> (count-lines (window-start) (window-end)) 30)
6395       (move-to-window-line
6396        (min (max 0 scroll-margin)
6397             (max 1 (- (window-height)
6398                       (if mode-line-format 1 0)
6399                       (if header-line-format 1 0)
6400                       2)))))))
6401
6402 (defun gnus-article-next-page-1 (lines)
6403   (condition-case ()
6404       (let ((scroll-in-place nil)
6405             (auto-window-vscroll nil))
6406         (scroll-up lines))
6407     (end-of-buffer
6408      ;; Long lines may cause an end-of-buffer error.
6409      (goto-char (point-max))))
6410   (gnus-article-beginning-of-window))
6411
6412 (defun gnus-article-prev-page (&optional lines)
6413   "Show previous page of current article.
6414 Argument LINES specifies lines to be scrolled down."
6415   (interactive "p")
6416   (move-to-window-line 0)
6417   (if (and gnus-page-broken
6418            (bobp)
6419            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6420       (progn
6421         (gnus-narrow-to-page -1)        ;Go to previous page.
6422         (goto-char (point-max))
6423         (recenter (if gnus-article-over-scroll
6424                       (if lines
6425                           (max (if (featurep 'xemacs)
6426                                    lines
6427                                  (+ lines scroll-margin))
6428                                3)
6429                         (- (window-height) 2))
6430                     -1)))
6431     (prog1
6432         (condition-case ()
6433             (let ((scroll-in-place nil))
6434               (scroll-down lines))
6435           (beginning-of-buffer
6436            (goto-char (point-min))))
6437       (gnus-article-beginning-of-window))))
6438
6439 (defun gnus-article-only-boring-p ()
6440   "Decide whether there is only boring text remaining in the article.
6441 Something \"interesting\" is a word of at least two letters that does
6442 not have a face in `gnus-article-boring-faces'."
6443   (when (and gnus-article-skip-boring
6444              (boundp 'gnus-article-boring-faces)
6445              (symbol-value 'gnus-article-boring-faces))
6446     (save-excursion
6447       (let ((inhibit-point-motion-hooks t))
6448         (catch 'only-boring
6449           (while (re-search-forward "\\b\\w\\w" nil t)
6450             (forward-char -1)
6451             (when (not (gnus-intersection
6452                         (gnus-faces-at (point))
6453                         (symbol-value 'gnus-article-boring-faces)))
6454               (throw 'only-boring nil)))
6455           (throw 'only-boring t))))))
6456
6457 (defun gnus-article-refer-article ()
6458   "Read article specified by message-id around point."
6459   (interactive)
6460   (save-excursion
6461     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6462     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6463     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6464         (let ((msg-id (concat "<" (match-string 0) ">")))
6465           (set-buffer gnus-summary-buffer)
6466           (gnus-summary-refer-article msg-id))
6467       (error "No references around point"))))
6468
6469 (defun gnus-article-show-summary ()
6470   "Reconfigure windows to show summary buffer."
6471   (interactive)
6472   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6473       (error "There is no summary buffer for this article buffer")
6474     (gnus-article-set-globals)
6475     (gnus-configure-windows 'article)
6476     (gnus-summary-goto-subject gnus-current-article)
6477     (gnus-summary-position-point)))
6478
6479 (defun gnus-article-describe-briefly ()
6480   "Describe article mode commands briefly."
6481   (interactive)
6482   (gnus-message 6 "%s" (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")))
6483
6484 (defun gnus-article-check-buffer ()
6485   "Beep if not in an article buffer."
6486   (unless (derived-mode-p 'gnus-article-mode)
6487     (error "Command invoked outside of a Gnus article buffer")))
6488
6489 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6490   "Read a summary buffer key sequence and execute it from the article buffer."
6491   (interactive "P")
6492   (gnus-article-check-buffer)
6493   (let ((nosaves
6494          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6495            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6496            "=" "^" "\M-^" "|"))
6497         (nosave-but-article
6498          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6499            "An" "Ap" [?A (meta return)] [?A delete]))
6500         (nosave-in-article
6501          '("AS" "\C-d"))
6502         (up-to-top
6503          '("n" "Gn" "p" "Gp"))
6504         keys new-sum-point)
6505     (with-current-buffer gnus-article-current-summary
6506       (let (gnus-pick-mode)
6507         (setq unread-command-events (nconc unread-command-events
6508                                            (list (or key last-command-event)))
6509               keys (if (featurep 'xemacs)
6510                        (events-to-keys (read-key-sequence nil t))
6511                      (read-key-sequence nil t)))))
6512
6513     (message "")
6514
6515     (cond
6516      ((eq (aref keys (1- (length keys))) ?\C-h)
6517       (gnus-article-describe-bindings (substring keys 0 -1)))
6518      ((or (member keys nosaves)
6519           (member keys nosave-but-article)
6520           (member keys nosave-in-article))
6521       (let (func)
6522         (save-window-excursion
6523           (pop-to-buffer gnus-article-current-summary)
6524           ;; We disable the pick minor mode commands.
6525           (let (gnus-pick-mode)
6526             (setq func (key-binding keys t))))
6527         (if (or (not func)
6528                 (numberp func))
6529             (ding)
6530           (unless (member keys nosave-in-article)
6531             (set-buffer gnus-article-current-summary))
6532           (when (and (symbolp func)
6533                      (get func 'disabled))
6534             (error "Function %s disabled" func))
6535           (call-interactively func)
6536           (setq new-sum-point (point)))
6537         (when (member keys nosave-but-article)
6538           (pop-to-buffer gnus-article-buffer))))
6539      (t
6540       ;; These commands should restore window configuration.
6541       (let ((obuf (current-buffer))
6542             (owin (current-window-configuration))
6543             win func in-buffer selected new-sum-start new-sum-hscroll err)
6544         (cond (not-restore-window
6545                (pop-to-buffer gnus-article-current-summary)
6546                (setq win (selected-window)))
6547               ((setq win (get-buffer-window gnus-article-current-summary))
6548                (select-window win))
6549               (t
6550                (let ((summary-buffer gnus-article-current-summary))
6551                  (gnus-configure-windows 'article)
6552                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6553                    (let ((gnus-buffer-configuration
6554                           '((article ((vertical 1.0
6555                                                 (summary 0.25 point)
6556                                                 (article 1.0)))))))
6557                      (gnus-configure-windows 'article))
6558                    (setq win (get-buffer-window summary-buffer 'visible)))
6559                  (gnus-select-frame-set-input-focus (window-frame win))
6560                  (select-window win))))
6561         (setq in-buffer (current-buffer))
6562         ;; We disable the pick minor mode commands.
6563         (setq func (let (gnus-pick-mode)
6564                      (key-binding keys t)))
6565         (when (get func 'disabled)
6566           (error "Function %s disabled" func))
6567         (if (and func
6568                  (functionp func)
6569                  (condition-case code
6570                      (progn
6571                        (call-interactively func)
6572                        t)
6573                    (error
6574                     (setq err code)
6575                     nil)))
6576             (progn
6577               (when (eq win (selected-window))
6578                 (setq new-sum-point (point)
6579                       new-sum-start (window-start win)
6580                       new-sum-hscroll (window-hscroll win)))
6581               (when (or (eq in-buffer (current-buffer))
6582                         (when (eq obuf (current-buffer))
6583                           (set-buffer in-buffer)
6584                           t))
6585                 (setq selected (gnus-summary-select-article))
6586                 (set-buffer obuf)
6587                 (unless not-restore-window
6588                   (set-window-configuration owin))
6589                 (when (and (eq selected 'old)
6590                            new-sum-point)
6591                   (set-window-start (get-buffer-window (current-buffer))
6592                                     1)
6593                   (set-window-point (get-buffer-window (current-buffer))
6594                                     (if (article-goto-body)
6595                                         (1- (point))
6596                                       (point))))
6597                 (when (and (not not-restore-window)
6598                            new-sum-point
6599                            (window-live-p win)
6600                            (with-current-buffer (window-buffer win)
6601                              (derived-mode-p 'gnus-summary-mode)))
6602                   (set-window-point win new-sum-point)
6603                   (set-window-start win new-sum-start)
6604                   (set-window-hscroll win new-sum-hscroll))))
6605           (set-window-configuration owin)
6606           (if err
6607               (signal (car err) (cdr err))
6608             (ding))))))))
6609
6610 (defun gnus-article-read-summary-send-keys ()
6611   (interactive)
6612   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6613     (gnus-article-read-summary-keys)))
6614
6615 (defun gnus-article-describe-key (key)
6616   "Display documentation of the function invoked by KEY.
6617 KEY is a string or a vector."
6618   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6619                        (read-key-sequence "Describe key: "))))
6620   (gnus-article-check-buffer)
6621   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6622                                   gnus-article-read-summary-send-keys))
6623       (with-current-buffer gnus-article-current-summary
6624         (setq unread-command-events
6625               (if (featurep 'xemacs)
6626                   (append key nil)
6627                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6628                                         (list 'meta (- x 128))
6629                                       x))
6630                         key)))
6631         (let ((cursor-in-echo-area t)
6632               gnus-pick-mode)
6633           (describe-key (read-key-sequence nil t))))
6634     (describe-key key)))
6635
6636 (defun gnus-article-describe-key-briefly (key &optional insert)
6637   "Display documentation of the function invoked by KEY.
6638 KEY is a string or a vector."
6639   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6640                        (read-key-sequence "Describe key: "))
6641                      current-prefix-arg))
6642   (gnus-article-check-buffer)
6643   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6644                                   gnus-article-read-summary-send-keys))
6645       (with-current-buffer gnus-article-current-summary
6646         (setq unread-command-events
6647               (if (featurep 'xemacs)
6648                   (append key nil)
6649                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6650                                         (list 'meta (- x 128))
6651                                       x))
6652                         key)))
6653         (let ((cursor-in-echo-area t)
6654               gnus-pick-mode)
6655           (describe-key-briefly (read-key-sequence nil t) insert)))
6656     (describe-key-briefly key insert)))
6657
6658 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6659 (defvar gnus-agent-summary-mode)
6660 (defvar gnus-draft-mode)
6661 (defvar help-xref-stack-item)
6662
6663 (defun gnus-article-describe-bindings (&optional prefix)
6664   "Show a list of all defined keys, and their definitions.
6665 The optional argument PREFIX, if non-nil, should be a key sequence;
6666 then we display only bindings that start with that prefix."
6667   (interactive)
6668   (gnus-article-check-buffer)
6669   (let ((keymap (copy-keymap gnus-article-mode-map))
6670         (map (copy-keymap gnus-article-send-map))
6671         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6672         parent agent draft)
6673     (define-key keymap "S" map)
6674     (define-key map [t] nil)
6675     (with-current-buffer gnus-article-current-summary
6676       (set-keymap-parent
6677        keymap
6678        (if (setq parent (keymap-parent gnus-article-mode-map))
6679            (prog1
6680                (setq parent (copy-keymap parent))
6681              (set-keymap-parent parent (current-local-map)))
6682          (current-local-map)))
6683       (set-keymap-parent map (key-binding "S"))
6684       (let (key def gnus-pick-mode)
6685         (while sumkeys
6686           (setq key (pop sumkeys))
6687           (cond ((and (vectorp key) (= (length key) 1)
6688                       (consp (setq def (aref key 0)))
6689                       (numberp (car def)) (numberp (cdr def)))
6690                  (when (< (max (car def) (cdr def)) 128)
6691                    (setq sumkeys
6692                          (append (mapcar
6693                                   #'vector
6694                                   (nreverse (gnus-uncompress-range def)))
6695                                  sumkeys))))
6696                 ((setq def (key-binding key))
6697                  (unless (eq def 'undefined)
6698                    (define-key keymap key def))))))
6699       (when (boundp 'gnus-agent-summary-mode)
6700         (setq agent gnus-agent-summary-mode))
6701       (when (boundp 'gnus-draft-mode)
6702         (setq draft gnus-draft-mode)))
6703     (with-temp-buffer
6704       (use-local-map keymap)
6705       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6706       (set (make-local-variable 'gnus-draft-mode) draft)
6707       (describe-bindings prefix))
6708     (let ((item `((lambda (prefix)
6709                     (with-current-buffer ,(current-buffer)
6710                       (gnus-article-describe-bindings prefix)))
6711                   ,prefix)))
6712       ;; Loading `help-mode' here is necessary if `describe-bindings'
6713       ;; is replaced with something, e.g. `helm-descbinds'.
6714       (require 'help-mode)
6715       (with-current-buffer (let (help-xref-following) (help-buffer))
6716         (setq help-xref-stack-item item)))))
6717
6718 (defun gnus-article-reply-with-original (&optional wide)
6719   "Start composing a reply mail to the current message.
6720 The text in the region will be yanked.  If the region isn't active,
6721 the entire article will be yanked."
6722   (interactive)
6723   (let ((article (cdr gnus-article-current))
6724         contents)
6725     (if (not (gnus-region-active-p))
6726         (with-current-buffer gnus-summary-buffer
6727           (gnus-summary-reply (list (list article)) wide))
6728       (setq contents (buffer-substring (point) (mark t)))
6729       ;; Deactivate active regions.
6730       (when (and (boundp 'transient-mark-mode)
6731                  transient-mark-mode)
6732         (setq mark-active nil))
6733       (with-current-buffer gnus-summary-buffer
6734         (gnus-summary-reply
6735          (list (list article contents)) wide)))))
6736
6737 (defun gnus-article-wide-reply-with-original ()
6738   "Start composing a wide reply mail to the current message.
6739 The text in the region will be yanked.  If the region isn't active,
6740 the entire article will be yanked."
6741   (interactive)
6742   (gnus-article-reply-with-original t))
6743
6744 (defun gnus-article-followup-with-original ()
6745   "Compose a followup to the current article.
6746 The text in the region will be yanked.  If the region isn't active,
6747 the entire article will be yanked."
6748   (interactive)
6749   (let ((article (cdr gnus-article-current))
6750         contents)
6751       (if (not (gnus-region-active-p))
6752           (with-current-buffer gnus-summary-buffer
6753             (gnus-summary-followup (list (list article))))
6754         (setq contents (buffer-substring (point) (mark t)))
6755         ;; Deactivate active regions.
6756         (when (and (boundp 'transient-mark-mode)
6757                    transient-mark-mode)
6758           (setq mark-active nil))
6759         (with-current-buffer gnus-summary-buffer
6760           (gnus-summary-followup
6761            (list (list article contents)))))))
6762
6763 (defun gnus-article-hide (&optional arg force)
6764   "Hide all the gruft in the current article.
6765 This means that signatures, cited text and (some) headers will be
6766 hidden.
6767 If given a prefix, show the hidden text instead."
6768   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6769   (gnus-article-hide-headers arg)
6770   (gnus-article-hide-list-identifiers arg)
6771   (gnus-article-hide-citation-maybe arg force)
6772   (gnus-article-hide-signature arg))
6773
6774 (defun gnus-check-group-server ()
6775   ;; Make sure the connection to the server is alive.
6776   (unless (gnus-server-opened
6777            (gnus-find-method-for-group gnus-newsgroup-name))
6778     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6779     (gnus-request-group gnus-newsgroup-name t)))
6780
6781 (eval-when-compile
6782   (autoload 'nneething-get-file-name "nneething"))
6783
6784 (defun gnus-request-article-this-buffer (article group)
6785   "Get an article and insert it into this buffer."
6786   (let (do-update-line sparse-header)
6787     (prog1
6788         (save-excursion
6789           (erase-buffer)
6790           (gnus-kill-all-overlays)
6791           (setq group (or group gnus-newsgroup-name))
6792
6793           ;; Using `gnus-request-article' directly will insert the article into
6794           ;; `nntp-server-buffer' - so we'll save some time by not having to
6795           ;; copy it from the server buffer into the article buffer.
6796
6797           ;; We only request an article by message-id when we do not have the
6798           ;; headers for it, so we'll have to get those.
6799           (when (stringp article)
6800             (gnus-read-header article))
6801
6802           ;; If the article number is negative, that means that this article
6803           ;; doesn't belong in this newsgroup (possibly), so we find its
6804           ;; message-id and request it by id instead of number.
6805           (when (and (numberp article)
6806                      gnus-summary-buffer
6807                      (get-buffer gnus-summary-buffer)
6808                      (gnus-buffer-exists-p gnus-summary-buffer))
6809             (with-current-buffer gnus-summary-buffer
6810               (let ((header (gnus-summary-article-header article)))
6811                 (when (< article 0)
6812                   (cond
6813                    ((memq article gnus-newsgroup-sparse)
6814                     ;; This is a sparse gap article.
6815                     (setq do-update-line article)
6816                     (setq article (mail-header-id header))
6817                     (setq sparse-header (gnus-read-header article))
6818                     (setq gnus-newsgroup-sparse
6819                           (delq article gnus-newsgroup-sparse)))
6820                    ((vectorp header)
6821                     ;; It's a real article.
6822                     (setq article (mail-header-id header)))
6823                    (t
6824                     ;; It is an extracted pseudo-article.
6825                     (setq article 'pseudo)
6826                     (gnus-request-pseudo-article header))))
6827
6828                 (let ((method (gnus-find-method-for-group
6829                                gnus-newsgroup-name)))
6830                   (when (and (eq (car method) 'nneething)
6831                              (vectorp header))
6832                     (let ((dir (nneething-get-file-name
6833                                 (mail-header-id header))))
6834                       (when (and (stringp dir)
6835                                  (file-directory-p dir))
6836                         (setq article 'nneething)
6837                         (gnus-group-enter-directory dir))))))))
6838
6839           (cond
6840            ;; Refuse to select canceled articles.
6841            ((and (numberp article)
6842                  gnus-summary-buffer
6843                  (get-buffer gnus-summary-buffer)
6844                  (gnus-buffer-exists-p gnus-summary-buffer)
6845                  (eq (cdr (with-current-buffer gnus-summary-buffer
6846                             (assq article gnus-newsgroup-reads)))
6847                      gnus-canceled-mark))
6848             nil)
6849            ;; We first check `gnus-original-article-buffer'.
6850            ((and (get-buffer gnus-original-article-buffer)
6851                  (numberp article)
6852                  (with-current-buffer gnus-original-article-buffer
6853                    (and (equal (car gnus-original-article) group)
6854                         (eq (cdr gnus-original-article) article))))
6855             ;; `insert-buffer-substring' would incorrectly use the
6856             ;; equivalent of string-make-multibyte which amount to decoding
6857             ;; with locale-coding-system, causing failure of
6858             ;; subsequent decoding.
6859             (insert (mm-string-to-multibyte
6860                      (with-current-buffer gnus-original-article-buffer
6861                        (buffer-substring (point-min) (point-max)))))
6862             'article)
6863            ;; Check the backlog.
6864            ((and gnus-keep-backlog
6865                  (gnus-backlog-request-article group article (current-buffer)))
6866             'article)
6867            ;; Check asynchronous pre-fetch.
6868            ((gnus-async-request-fetched-article group article (current-buffer))
6869             (gnus-async-prefetch-next group article gnus-summary-buffer)
6870             (when (and (numberp article) gnus-keep-backlog)
6871               (gnus-backlog-enter-article group article (current-buffer)))
6872             'article)
6873            ;; Check the cache.
6874            ((and gnus-use-cache
6875                  (numberp article)
6876                  (gnus-cache-request-article article group))
6877             'article)
6878            ;; Check the agent cache.
6879            ((gnus-agent-request-article article group)
6880             'article)
6881            ;; Get the article and put into the article buffer.
6882            ((or (stringp article)
6883                 (numberp article))
6884             (let ((gnus-override-method gnus-override-method)
6885                   (methods (and (stringp article)
6886                                 (with-current-buffer gnus-summary-buffer
6887                                   (gnus-refer-article-methods))))
6888                   (backend (car (gnus-find-method-for-group
6889                                  gnus-newsgroup-name)))
6890                   result
6891                   (inhibit-read-only t))
6892               (when (and (null gnus-override-method)
6893                          methods)
6894                 (setq gnus-override-method (pop methods)))
6895               (while (not result)
6896                 (erase-buffer)
6897                 (gnus-kill-all-overlays)
6898                 (let ((gnus-newsgroup-name group))
6899                   (gnus-check-group-server))
6900                 (cond
6901                  ((gnus-request-article article group (current-buffer))
6902                   (when (numberp article)
6903                     (gnus-async-prefetch-next group article
6904                                               gnus-summary-buffer)
6905                     (when gnus-keep-backlog
6906                       (gnus-backlog-enter-article
6907                        group article (current-buffer)))
6908                     (when (and gnus-agent
6909                                (gnus-agent-group-covered-p group))
6910                       (gnus-agent-store-article article group)))
6911                   (setq result 'article))
6912                  (methods
6913                   (setq gnus-override-method (pop methods)))
6914                  ((not (string-match "^400 "
6915                                      (nnheader-get-report backend)))
6916                   ;; If we get 400 server disconnect, reconnect and
6917                   ;; retry; otherwise, assume the article has expired.
6918                   (setq result 'done))))
6919               (and (eq result 'article) 'article)))
6920            ;; It was a pseudo.
6921            (t article)))
6922
6923       ;; Associate this article with the current summary buffer.
6924       (setq gnus-article-current-summary gnus-summary-buffer)
6925
6926       ;; Take the article from the original article buffer
6927       ;; and place it in the buffer it's supposed to be in.
6928       (when (and (get-buffer gnus-article-buffer)
6929                  (equal (buffer-name (current-buffer))
6930                         (buffer-name (get-buffer gnus-article-buffer))))
6931         (save-excursion
6932           (if (get-buffer gnus-original-article-buffer)
6933               (set-buffer gnus-original-article-buffer)
6934             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6935             (buffer-disable-undo)
6936             (setq major-mode 'gnus-original-article-mode)
6937             (setq buffer-read-only t))
6938           (let ((inhibit-read-only t))
6939             (erase-buffer)
6940             (insert-buffer-substring gnus-article-buffer))
6941           (setq gnus-original-article (cons group article)))
6942
6943         ;; Decode charsets.
6944         (run-hooks 'gnus-article-decode-hook)
6945         ;; Mark article as decoded or not.
6946         (setq gnus-article-decoded-p gnus-article-decode-hook))
6947
6948       ;; Update sparse articles.
6949       (when (and do-update-line
6950                  (or (numberp article)
6951                      (stringp article)))
6952         (let ((buf (current-buffer)))
6953           (set-buffer gnus-summary-buffer)
6954           (gnus-summary-update-article do-update-line sparse-header)
6955           (gnus-summary-goto-subject do-update-line nil t)
6956           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6957                             (point))
6958           (set-buffer buf))))))
6959
6960 (defun gnus-block-private-groups (group)
6961   (if (or (gnus-news-group-p group)
6962           (gnus-member-of-valid 'global group))
6963       ;; Block nothing in news groups.
6964       nil
6965     ;; Block everything anywhere else.
6966     "."))
6967
6968 (defun gnus-blocked-images ()
6969   (if (functionp gnus-blocked-images)
6970       (funcall gnus-blocked-images gnus-newsgroup-name)
6971     gnus-blocked-images))
6972
6973 ;;;
6974 ;;; Article editing
6975 ;;;
6976
6977 (defcustom gnus-article-edit-mode-hook nil
6978   "Hook run in article edit mode buffers."
6979   :group 'gnus-article-various
6980   :type 'hook)
6981
6982 (defvar gnus-article-edit-done-function nil)
6983
6984 (defvar gnus-article-edit-mode-map nil)
6985
6986 ;; Should we be using derived.el for this?
6987 (unless gnus-article-edit-mode-map
6988   (setq gnus-article-edit-mode-map (make-keymap))
6989   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6990
6991   (gnus-define-keys gnus-article-edit-mode-map
6992     "\C-c?"    describe-mode
6993     "\C-c\C-c" gnus-article-edit-done
6994     "\C-c\C-k" gnus-article-edit-exit
6995     "\C-c\C-f\C-t" message-goto-to
6996     "\C-c\C-f\C-o" message-goto-from
6997     "\C-c\C-f\C-b" message-goto-bcc
6998     ;;"\C-c\C-f\C-w" message-goto-fcc
6999     "\C-c\C-f\C-c" message-goto-cc
7000     "\C-c\C-f\C-s" message-goto-subject
7001     "\C-c\C-f\C-r" message-goto-reply-to
7002     "\C-c\C-f\C-n" message-goto-newsgroups
7003     "\C-c\C-f\C-d" message-goto-distribution
7004     "\C-c\C-f\C-f" message-goto-followup-to
7005     "\C-c\C-f\C-m" message-goto-mail-followup-to
7006     "\C-c\C-f\C-k" message-goto-keywords
7007     "\C-c\C-f\C-u" message-goto-summary
7008     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7009     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7010     "\C-c\C-b" message-goto-body
7011     "\C-c\C-i" message-goto-signature
7012
7013     "\C-c\C-t" message-insert-to
7014     "\C-c\C-n" message-insert-newsgroups
7015     "\C-c\C-o" message-sort-headers
7016     "\C-c\C-e" message-elide-region
7017     "\C-c\C-v" message-delete-not-region
7018     "\C-c\C-z" message-kill-to-signature
7019     "\M-\r" message-newline-and-reformat
7020     "\C-c\C-a" mml-attach-file
7021     "\C-a" message-beginning-of-line
7022     "\t" message-tab
7023     "\M-;" comment-region)
7024
7025   (gnus-define-keys (gnus-article-edit-wash-map
7026                      "\C-c\C-w" gnus-article-edit-mode-map)
7027     "f" gnus-article-edit-full-stops))
7028
7029 (easy-menu-define
7030   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7031   '("Field"
7032     ["Fetch To" message-insert-to t]
7033     ["Fetch Newsgroups" message-insert-newsgroups t]
7034     "----"
7035     ["To" message-goto-to t]
7036     ["From" message-goto-from t]
7037     ["Subject" message-goto-subject t]
7038     ["Cc" message-goto-cc t]
7039     ["Reply-To" message-goto-reply-to t]
7040     ["Summary" message-goto-summary t]
7041     ["Keywords" message-goto-keywords t]
7042     ["Newsgroups" message-goto-newsgroups t]
7043     ["Followup-To" message-goto-followup-to t]
7044     ["Mail-Followup-To" message-goto-mail-followup-to t]
7045     ["Distribution" message-goto-distribution t]
7046     ["Body" message-goto-body t]
7047     ["Signature" message-goto-signature t]))
7048
7049 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7050   "Major mode for editing articles.
7051 This is an extended text-mode.
7052
7053 \\{gnus-article-edit-mode-map}"
7054   (make-local-variable 'gnus-article-edit-done-function)
7055   (make-local-variable 'gnus-prev-winconf)
7056   (set (make-local-variable 'font-lock-defaults)
7057        '(message-font-lock-keywords t))
7058   (set (make-local-variable 'mail-header-separator) "")
7059   (set (make-local-variable 'gnus-article-edit-mode) t)
7060   (easy-menu-add message-mode-field-menu message-mode-map)
7061   (mml-mode)
7062   (setq buffer-read-only nil)
7063   (buffer-enable-undo)
7064   (widen))
7065
7066 (defun gnus-article-edit (&optional force)
7067   "Edit the current article.
7068 This will have permanent effect only in mail groups.
7069 If FORCE is non-nil, allow editing of articles even in read-only
7070 groups."
7071   (interactive "P")
7072   (when (and (not force)
7073              (gnus-group-read-only-p))
7074     (error "The current newsgroup does not support article editing"))
7075   (gnus-article-date-original)
7076   (gnus-article-edit-article
7077    'ignore
7078    `(lambda (no-highlight)
7079       'ignore
7080       (gnus-summary-edit-article-done
7081        ,(or (mail-header-references gnus-current-headers) "")
7082        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7083
7084 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7085   "Start editing the contents of the current article buffer."
7086   (let ((winconf (current-window-configuration)))
7087     (set-buffer gnus-article-buffer)
7088     (let ((message-auto-save-directory
7089            ;; Don't associate the article buffer with a draft file.
7090            nil))
7091       (gnus-article-edit-mode))
7092     (funcall start-func)
7093     (set-buffer-modified-p nil)
7094     (gnus-configure-windows 'edit-article)
7095     (setq gnus-article-edit-done-function exit-func)
7096     (setq gnus-prev-winconf winconf)
7097     (unless quiet
7098       (gnus-message 6 "C-c C-c to end edits"))))
7099
7100 (defun gnus-article-edit-done (&optional arg)
7101   "Update the article edits and exit."
7102   (interactive "P")
7103   (let ((func gnus-article-edit-done-function)
7104         (buf (current-buffer))
7105         (start (window-start))
7106         (p (point))
7107         (winconf gnus-prev-winconf))
7108     (widen) ;; Widen it in case that users narrowed the buffer.
7109     (funcall func arg)
7110     (set-buffer buf)
7111     ;; The cache and backlog have to be flushed somewhat.
7112     (when gnus-keep-backlog
7113       (gnus-backlog-remove-article
7114        (car gnus-article-current) (cdr gnus-article-current)))
7115     ;; Flush original article as well.
7116     (gnus-flush-original-article-buffer)
7117     (when gnus-use-cache
7118       (gnus-cache-update-article
7119        (car gnus-article-current) (cdr gnus-article-current)))
7120     ;; We remove all text props from the article buffer.
7121     (kill-all-local-variables)
7122     (set-text-properties (point-min) (point-max) nil)
7123     (gnus-article-mode)
7124     (set-window-configuration winconf)
7125     (set-buffer buf)
7126     (set-window-start (get-buffer-window buf) start)
7127     (set-window-point (get-buffer-window buf) (point)))
7128   (gnus-summary-show-article))
7129
7130 (defun gnus-flush-original-article-buffer ()
7131   (when (get-buffer gnus-original-article-buffer)
7132     (with-current-buffer gnus-original-article-buffer
7133       (setq gnus-original-article nil))))
7134
7135 (defun gnus-article-edit-exit ()
7136   "Exit the article editing without updating."
7137   (interactive)
7138   (when (or (not (buffer-modified-p))
7139             (yes-or-no-p "Article modified; kill anyway? "))
7140     (let ((curbuf (current-buffer))
7141           (p (point))
7142           (window-start (window-start)))
7143       (erase-buffer)
7144       (if (gnus-buffer-live-p gnus-original-article-buffer)
7145           (insert-buffer-substring gnus-original-article-buffer))
7146       (let ((winconf gnus-prev-winconf))
7147         (kill-all-local-variables)
7148         (gnus-article-mode)
7149         (set-window-configuration winconf)
7150         ;; Tippy-toe some to make sure that point remains where it was.
7151         (save-current-buffer
7152           (set-buffer curbuf)
7153           (set-window-start (get-buffer-window (current-buffer)) window-start)
7154           (goto-char p))))
7155     (gnus-summary-show-article)))
7156
7157 (defun gnus-article-edit-full-stops ()
7158   "Interactively repair spacing at end of sentences."
7159   (interactive)
7160   (save-excursion
7161     (goto-char (point-min))
7162     (search-forward-regexp "^$" nil t)
7163     (let ((case-fold-search nil))
7164       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7165
7166 ;;;
7167 ;;; Article highlights
7168 ;;;
7169
7170 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7171
7172 ;;; Internal Variables:
7173
7174 (defcustom gnus-button-url-regexp
7175   (concat
7176    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7177    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7178    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7179    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7180        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7181              (punct "!?:;.,"))
7182          (concat
7183           "\\(?:"
7184           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7185           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7186           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)"
7187           "\\(?:" "[" chars punct "]+" "[" chars "]" "\\)?"
7188           "\\|"
7189           "[" chars punct "]+" "[" chars "]"
7190           "\\)"))
7191      (concat ;; XEmacs 21.4 doesn't support POSIX.
7192       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7193       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7194    "\\)")
7195   "Regular expression that matches URLs."
7196   :version "24.4"
7197   :group 'gnus-article-buttons
7198   :type 'regexp)
7199
7200 (defcustom gnus-button-valid-fqdn-regexp
7201   message-valid-fqdn-regexp
7202   "Regular expression that matches a valid FQDN."
7203   :version "22.1"
7204   :group 'gnus-article-buttons
7205   :type 'regexp)
7206
7207 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7208 (defcustom gnus-button-valid-localpart-regexp
7209   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7210   "Regular expression that matches a localpart of mail addresses or MIDs."
7211   :version "22.1"
7212   :group 'gnus-article-buttons
7213   :type 'regexp)
7214
7215 (defcustom gnus-button-man-handler 'manual-entry
7216   "Function to use for displaying man pages.
7217 The function must take at least one argument with a string naming the
7218 man page."
7219   :version "22.1"
7220   :type '(choice (function-item :tag "Man" manual-entry)
7221                  (function-item :tag "Woman" woman)
7222                  (function :tag "Other"))
7223   :group 'gnus-article-buttons)
7224
7225 (defcustom gnus-button-mid-or-mail-regexp
7226   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7227           gnus-button-valid-fqdn-regexp
7228           ">?\\)\\b")
7229   "Regular expression that matches a message ID or a mail address."
7230   :version "22.1"
7231   :group 'gnus-article-buttons
7232   :type 'regexp)
7233
7234 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7235   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7236 Strings like this can be either a message ID or a mail address.  If it is one
7237 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7238 message ID or a mail address, respectively.  If this variable is set to the
7239 symbol `ask', always query the user what do do.  If it is a function, this
7240 function will be called with the string as its only argument.  The function
7241 must return `mid', `mail', `invalid' or `ask'."
7242   :version "22.1"
7243   :group 'gnus-article-buttons
7244   :type '(choice (function-item :tag "Heuristic function"
7245                                 gnus-button-mid-or-mail-heuristic)
7246                  (const ask)
7247                  (const mid)
7248                  (const mail)))
7249
7250 (defcustom gnus-button-mid-or-mail-heuristic-alist
7251   '((-10.0 . ".+\\$.+@")
7252     (-10.0 . "#")
7253     (-10.0 . "\\*")
7254     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7255     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7256     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7257     (-1.0  . "^[^a-z]+@")
7258     ;;
7259     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7260     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7261     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7262     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7263     ;;
7264     (-2.0  . "^[0-9]")
7265     (-1.0  . "^[0-9][0-9]")
7266     ;;
7267     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7268     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7269     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7270     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7271     ;;
7272     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7273     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7274     ;;       "[0-9]{8,}.*\@"
7275     (-3.0
7276      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7277     ;; "[0-9]{12,}.*\@"
7278     ;; compensation for TDMA dated mail addresses:
7279     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7280     ;;
7281     (-20.0 . "\\.fsf@") ;; Gnus
7282     (-20.0 . "^slrn")
7283     (-20.0 . "^Pine")
7284     (-20.0 . "^alpine\\.")
7285     (-20.0 . "_-_") ;; Subject change in thread
7286     ;;
7287     (-20.0 . "\\.ln@") ;; leafnode
7288     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7289     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7290     ;;
7291     ;; (5.0 . "") ;; $local_part_len <= 7
7292     (10.0  . "^[^0-9]+@")
7293     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7294     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7295     (3.0   . "\@stud")
7296     ;;
7297     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7298     ;;
7299     (0.5   . "^[A-Z][a-z]")
7300     (0.5   . "^[A-Z][a-z][a-z]")
7301     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7302     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7303   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7304
7305 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7306 address.  The REGEXP is processed with `case-fold-search' set to nil."
7307   :version "22.1"
7308   :group 'gnus-article-buttons
7309   :type '(repeat (cons (number :tag "Rate")
7310                        (regexp :tag "Regexp"))))
7311
7312 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7313   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7314 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7315 address, `ask' if unsure and `invalid' if the string is invalid."
7316   (let ((case-fold-search nil)
7317         (list gnus-button-mid-or-mail-heuristic-alist)
7318         (result 0) rate regexp lpartlen elem)
7319     (setq lpartlen
7320           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7321     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7322     ;; Certain special cases...
7323     (when (string-match
7324            (concat
7325             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7326             "^[0-9]+\\.[0-9]+@compuserve\\|"
7327             "@public\\.gmane\\.org")
7328            mid-or-mail)
7329       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7330       (setq result 'mail))
7331     (when (string-match "@.*@\\| " mid-or-mail)
7332       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7333       (setq result 'invalid))
7334     ;; Nothing more to do, if result is not a number here...
7335     (when (numberp result)
7336       (while list
7337         (setq elem (car list)
7338               rate (car elem)
7339               regexp (cdr elem)
7340               list (cdr list))
7341         (when (string-match regexp mid-or-mail)
7342           (setq result (+ result rate))
7343           (gnus-message
7344            9 "`%s' matched `%s', rate `%s', result `%s'."
7345            mid-or-mail regexp rate result)))
7346       (when (<= lpartlen 7)
7347         (setq result (+ result 5.0))
7348         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7349                       mid-or-mail result))
7350       (when (>= lpartlen 12)
7351         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7352         (cond
7353          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7354           ;; Long local part should contain realname if e-mail address,
7355           ;; too many digits: message-id.
7356           ;; $score -= 5.0 + 0.1 * $local_part_len;
7357           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7358           (setq result (+ result rate))
7359           (gnus-message
7360            9 "Many digits in `%s', rate `%s', result `%s'."
7361            mid-or-mail rate result))
7362          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7363                         mid-or-mail)
7364           ;; Too few vowels [^aeiouy]{4,}.*\@
7365           (setq result (+ result -5.0))
7366           (gnus-message
7367            9 "Few vowels in `%s', rate `%s', result `%s'."
7368            mid-or-mail -5.0 result))
7369          (t
7370           (setq result (+ result 5.0))
7371           (gnus-message
7372            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7373     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7374     ;; Maybe we should make this a customizable alist: (condition . 'result)
7375     (cond
7376      ((symbolp result) result)
7377      ;; Now convert number into proper results:
7378      ((< result -10.0) 'mid)
7379      ((> result  10.0) 'mail)
7380      (t 'ask))))
7381
7382 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7383   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7384          (url-mid (concat "news" ":" mid-or-mail))
7385          (url-mailto (concat "mailto" ":" mid-or-mail)))
7386     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7387     (when (fboundp pref)
7388       (setq guessed
7389             ;; get rid of surrounding angles...
7390             (funcall pref
7391                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7392       (if (or (eq 'mid guessed) (eq 'mail guessed))
7393           (setq pref guessed)
7394         (setq pref 'ask)))
7395     (if (eq pref 'ask)
7396         (save-window-excursion
7397           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7398               (setq pref 'mail)
7399             (setq pref 'mid))))
7400     (cond ((eq pref 'mid)
7401            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7402            (gnus-button-handle-news url-mid))
7403           ((eq pref 'mail)
7404            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7405            (gnus-url-mailto url-mailto))
7406           (t (gnus-message 3 "Invalid string.")))))
7407
7408 (defun gnus-button-handle-custom (fun arg)
7409   "Call function FUN on argument ARG.
7410 Both FUN and ARG are supposed to be strings.  ARG will be passed
7411 as a symbol to FUN."
7412   (funcall (intern fun)
7413            (if (string-match "^customize-apropos" fun)
7414                arg
7415              (intern arg))))
7416
7417 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7418
7419 (defun gnus-button-handle-describe-function (url)
7420   "Call `describe-function' when pushing the corresponding URL button."
7421   (describe-function
7422    (intern
7423     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7424
7425 (defun gnus-button-handle-describe-variable (url)
7426   "Call `describe-variable' when pushing the corresponding URL button."
7427   (describe-variable
7428    (intern
7429     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7430
7431 (defun gnus-button-handle-symbol (url)
7432 "Display help on variable or function.
7433 Calls `describe-variable' or `describe-function'."
7434   (let ((sym (intern url)))
7435     (cond
7436      ((fboundp sym) (describe-function sym))
7437      ((boundp sym) (describe-variable sym))
7438      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7439
7440 (defun gnus-button-handle-describe-key (url)
7441   "Call `describe-key' when pushing the corresponding URL button."
7442   (let* ((key-string
7443           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7444          (keys (ignore-errors (eval `(kbd ,key-string)))))
7445     (if keys
7446         (describe-key keys)
7447       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7448
7449 (defun gnus-button-handle-apropos (url)
7450   "Call `apropos' when pushing the corresponding URL button."
7451   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7452
7453 (defun gnus-button-handle-apropos-command (url)
7454   "Call `apropos' when pushing the corresponding URL button."
7455   (apropos-command
7456    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7457
7458 (defun gnus-button-handle-apropos-variable (url)
7459   "Call `apropos' when pushing the corresponding URL button."
7460   (funcall
7461    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7462    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7463
7464 (defun gnus-button-handle-apropos-documentation (url)
7465   "Call `apropos' when pushing the corresponding URL button."
7466   (funcall
7467    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7468    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7469
7470 (defun gnus-button-handle-library (url)
7471   "Call `locate-library' when pushing the corresponding URL button."
7472   (gnus-message 9 "url=`%s'" url)
7473   (let* ((lib (locate-library url))
7474          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7475     (if (not lib)
7476         (gnus-message 1 "Cannot locale library `%s'." url)
7477       (find-file-read-only file))))
7478
7479 (defcustom gnus-button-man-level 5
7480   "*Integer that says how many man-related buttons Gnus will show.
7481 The higher the number, the more buttons will appear and the more false
7482 positives are possible.  Note that you can set this variable local to
7483 specific groups.  Setting it higher in Unix groups is probably a good idea.
7484 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7485 how to set variables in specific groups."
7486   :version "22.1"
7487   :group 'gnus-article-buttons
7488   :link '(custom-manual "(gnus)Group Parameters")
7489   :type 'integer)
7490
7491 (defcustom gnus-button-emacs-level 5
7492   "*Integer that says how many emacs-related buttons Gnus will show.
7493 The higher the number, the more buttons will appear and the more false
7494 positives are possible.  Note that you can set this variable local to
7495 specific groups.  Setting it higher in Emacs or Gnus related groups is
7496 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7497 `gnus-parameters' on how to set variables in specific groups."
7498   :version "22.1"
7499   :group 'gnus-article-buttons
7500   :link '(custom-manual "(gnus)Group Parameters")
7501   :type 'integer)
7502
7503 (defcustom gnus-button-message-level 5
7504   "*Integer that says how many buttons for news or mail messages will appear.
7505 The higher the number, the more buttons will appear and the more false
7506 positives are possible."
7507   ;; mail addresses, MIDs, URLs for news, ...
7508   :version "22.1"
7509   :group 'gnus-article-buttons
7510   :type 'integer)
7511
7512 (defcustom gnus-button-browse-level 5
7513   "*Integer that says how many buttons for browsing will appear.
7514 The higher the number, the more buttons will appear and the more false
7515 positives are possible."
7516   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7517   :version "22.1"
7518   :group 'gnus-article-buttons
7519   :type 'integer)
7520
7521 (defcustom gnus-button-alist
7522   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7523      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7524     ((concat "\\b\\(nntp\\|news\\):\\("
7525              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7526      0 t gnus-button-handle-news 2)
7527     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7528      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7529     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7530      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7531     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7532     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7533      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7534     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7535      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7536     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7537      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7538     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7539      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7540     ;; RFC 2368 (The mailto URL scheme)
7541     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7542      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7543     ("\\bmailto:\\([^ \n\t]+\\)"
7544      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7545     ;; Info Konqueror style <info:/foo/bar baz>.
7546     ;; Must come before " Gnus home-grown style".
7547     ("\\binfo://?\\([^'\">\n\t]+\\)"
7548      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7549    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7550     ("\\binfo://\\([^'\">\n\t ]+\\)"
7551      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7552     ;; Info GNOME style <info:foo#bar_baz>
7553     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7554      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7555     ;; Info KDE style <info:(foo)bar baz>
7556     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7557      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7558     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7559      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7560     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7561      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7563     ;; This is custom
7564     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7565      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7566     ;; Emacs help commands
7567     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7568      ;; regexp doesn't match arguments containing ` '.
7569      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7570     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7571      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7572     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7573      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7574     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7575      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7576     ;; The following entries may lead to many false positives so don't enable
7577     ;; them by default (use a high button level).
7578     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7579      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7580      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7581     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7582      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7583     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7584      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7585     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7586      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7587     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7588      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7589     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7590      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7591     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7592      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7593     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7594      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7595     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7596      ;; Unlike the other regexps we really have to require quoting
7597      ;; here to determine where it ends.
7598      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7599     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7600     ("<URL: *\\([^\n<>]*\\)>"
7601      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7602     ;; RFC 2396 (2.4.3., delims) ...
7603     ("\"URL: *\\([^\n\"]*\\)\""
7604      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7605     ;; Raw URLs.
7606     (gnus-button-url-regexp
7607      0 (>= gnus-button-browse-level 0) browse-url 0)
7608     ;; man pages
7609     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7610      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7611      gnus-button-handle-man 1)
7612     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7613     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7614      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7615      gnus-button-handle-man 1)
7616     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7617     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7618     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7619      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7620     ;; Recognizing patches to .el files.  This is somewhat obscure,
7621     ;; but considering the percentage of Gnus users who hack Emacs
7622     ;; Lisp files...
7623     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7624      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7625     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7626      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7627     ;; MID or mail: To avoid too many false positives we don't try to catch
7628     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7629     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7630     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7631     ;; so that non-ambiguous entries (see above) match first.
7632     (gnus-button-mid-or-mail-regexp
7633      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7634   "*Alist of regexps matching buttons in article bodies.
7635
7636 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7637 REGEXP: is the string (case insensitive) matching text around the button (can
7638 also be Lisp expression evaluating to a string),
7639 BUTTON: is the number of the regexp grouping actually matching the button,
7640 FORM: is a Lisp expression which must eval to true for the button to
7641 be added,
7642 CALLBACK: is the function to call when the user push this button, and each
7643 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7644
7645 CALLBACK can also be a variable, in that case the value of that
7646 variable it the real callback function."
7647   :group 'gnus-article-buttons
7648   :type '(repeat (list (choice regexp variable sexp)
7649                        (integer :tag "Button")
7650                        (sexp :tag "Form")
7651                        (function :tag "Callback")
7652                        (repeat :tag "Par"
7653                                :inline t
7654                                (integer :tag "Regexp group")))))
7655 (put 'gnus-button-alist 'risky-local-variable t)
7656
7657 (defcustom gnus-header-button-alist
7658   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7659      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7660     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7661      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7662     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7663      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7664     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7665      0 (>= gnus-button-browse-level 0) browse-url 0)
7666     ("^Subject:" gnus-button-url-regexp
7667      0 (>= gnus-button-browse-level 0) browse-url 0)
7668     ("^[^:]+:" gnus-button-url-regexp
7669      0 (>= gnus-button-browse-level 0) browse-url 0)
7670     ("^OpenPGP:.*url=" gnus-button-url-regexp
7671      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7672     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7673      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7674     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7675      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7676   "*Alist of headers and regexps to match buttons in article heads.
7677
7678 This alist is very similar to `gnus-button-alist', except that each
7679 alist has an additional HEADER element first in each entry:
7680
7681 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7682
7683 HEADER is a regexp to match a header.  For a fuller explanation, see
7684 `gnus-button-alist'."
7685   :group 'gnus-article-buttons
7686   :group 'gnus-article-headers
7687   :type '(repeat (list (regexp :tag "Header")
7688                        (choice regexp variable)
7689                        (integer :tag "Button")
7690                        (sexp :tag "Form")
7691                        (function :tag "Callback")
7692                        (repeat :tag "Par"
7693                                :inline t
7694                                (integer :tag "Regexp group")))))
7695 (put 'gnus-header-button-alist 'risky-local-variable t)
7696
7697 ;;; Commands:
7698
7699 (defun gnus-article-push-button (event)
7700   "Check text under the mouse pointer for a callback function.
7701 If the text under the mouse pointer has a `gnus-callback' property,
7702 call it with the value of the `gnus-data' text property."
7703   (interactive "e")
7704   (set-buffer (window-buffer (posn-window (event-start event))))
7705   (let* ((pos (posn-point (event-start event)))
7706          (data (get-text-property pos 'gnus-data))
7707          (fun (get-text-property pos 'gnus-callback)))
7708     (goto-char pos)
7709     (when fun
7710       (funcall fun data))))
7711
7712 (defun gnus-article-press-button ()
7713   "Check text at point for a callback function.
7714 If the text at point has a `gnus-callback' property,
7715 call it with the value of the `gnus-data' text property."
7716   (interactive)
7717   (let ((data (get-text-property (point) 'gnus-data))
7718         (fun (get-text-property (point) 'gnus-callback)))
7719     (when fun
7720       (funcall fun data))))
7721
7722 (defun gnus-article-highlight (&optional force)
7723   "Highlight current article.
7724 This function calls `gnus-article-highlight-headers',
7725 `gnus-article-highlight-citation',
7726 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7727 do the highlighting.  See the documentation for those functions."
7728   (interactive (list 'force))
7729   (gnus-article-highlight-headers)
7730   (gnus-article-highlight-citation force)
7731   (gnus-article-highlight-signature)
7732   (gnus-article-add-buttons)
7733   (gnus-article-add-buttons-to-head))
7734
7735 (defun gnus-article-highlight-some (&optional force)
7736   "Highlight current article.
7737 This function calls `gnus-article-highlight-headers',
7738 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7739 do the highlighting.  See the documentation for those functions."
7740   (interactive (list 'force))
7741   (gnus-article-highlight-headers)
7742   (gnus-article-highlight-signature)
7743   (gnus-article-add-buttons))
7744
7745 (defun gnus-article-highlight-headers ()
7746   "Highlight article headers as specified by `gnus-header-face-alist'."
7747   (interactive)
7748   (gnus-with-article-headers
7749     (let (regexp header-face field-face from hpoints fpoints)
7750       (dolist (entry gnus-header-face-alist)
7751         (goto-char (point-min))
7752         (setq regexp (concat "^\\("
7753                              (if (string-equal "" (nth 0 entry))
7754                                  "[^\t ]"
7755                                (nth 0 entry))
7756                              "\\)")
7757               header-face (nth 1 entry)
7758               field-face (nth 2 entry))
7759         (while (and (re-search-forward regexp nil t)
7760                     (not (eobp)))
7761           (beginning-of-line)
7762           (setq from (point))
7763           (unless (search-forward ":" nil t)
7764             (forward-char 1))
7765           (when (and header-face
7766                      (not (memq (point) hpoints)))
7767             (push (point) hpoints)
7768             (gnus-put-text-property from (point) 'face header-face))
7769           (when (and field-face
7770                      (not (memq (setq from (point)) fpoints)))
7771             (push from fpoints)
7772             (if (re-search-forward "^[^ \t]" nil t)
7773                 (forward-char -2)
7774               (goto-char (point-max)))
7775             (gnus-put-text-property from (point) 'face field-face)))))))
7776
7777 (defun gnus-article-highlight-signature ()
7778   "Highlight the signature in an article.
7779 It does this by highlighting everything after
7780 `gnus-signature-separator' using the face `gnus-signature'."
7781   (interactive)
7782   (gnus-with-article-buffer
7783     (let ((inhibit-point-motion-hooks t))
7784       (save-restriction
7785         (when (and gnus-signature-face
7786                    (gnus-article-narrow-to-signature))
7787           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7788                             'face gnus-signature-face)
7789           (widen)
7790           (gnus-article-search-signature)
7791           (let ((start (match-beginning 0))
7792                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7793             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7794                                      end)))))))
7795
7796 (defun gnus-button-in-region-p (b e prop)
7797   "Say whether PROP exists in the region."
7798   (text-property-not-all b e prop nil))
7799
7800 (defun gnus-article-add-buttons ()
7801   "Find external references in the article and make buttons of them.
7802 \"External references\" are things like Message-IDs and URLs, as
7803 specified by `gnus-button-alist'."
7804   (interactive)
7805   (gnus-with-article-buffer
7806     (let ((inhibit-point-motion-hooks t)
7807           (case-fold-search t)
7808           (alist gnus-button-alist)
7809           beg entry regexp)
7810       ;; We skip the headers.
7811       (article-goto-body)
7812       (setq beg (point))
7813       (while (setq entry (pop alist))
7814         (setq regexp (eval (car entry)))
7815         (goto-char beg)
7816         (while (re-search-forward regexp nil t)
7817           (let ((start (match-beginning (nth 1 entry)))
7818                 (end (match-end (nth 1 entry)))
7819                 (from (match-beginning 0)))
7820             (when (and (eval (nth 2 entry))
7821                        (not (gnus-button-in-region-p
7822                              start end 'gnus-callback)))
7823               ;; That optional form returned non-nil, so we add the
7824               ;; button.
7825               (setq from (set-marker (make-marker) from))
7826               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7827                            (gnus-article-extend-url-button from start end))
7828                 (gnus-article-add-button start end
7829                                          'gnus-button-push (list from entry))
7830                 (gnus-put-text-property
7831                  start end
7832                  'gnus-string (buffer-substring-no-properties
7833                                start end))))))))))
7834
7835 (defun gnus-article-extend-url-button (beg start end)
7836   "Extend url button if url is folded into two or more lines.
7837 Return non-nil if button is extended.  BEG is a marker that points to
7838 the beginning position of a text containing url.  START and END are
7839 the endpoints of a url button before it is extended.  The concatenated
7840 url is put as the `gnus-button-url' overlay property on the button."
7841   (let ((opoint (point))
7842         (points (list start end))
7843         url delim regexp)
7844     (prog1
7845         (when (and (progn
7846                      (goto-char end)
7847                      (not (looking-at "[\t ]*[\">]")))
7848                    (progn
7849                      (goto-char start)
7850                      (string-match
7851                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7852                       (buffer-substring (point-at-bol) start)))
7853                    (progn
7854                      (setq url (list (buffer-substring start end))
7855                            delim (if (match-beginning 1) ">" "\""))
7856                      (beginning-of-line)
7857                      (setq regexp (concat
7858                                    (when (and (looking-at
7859                                                message-cite-prefix-regexp)
7860                                               (< (match-end 0) start))
7861                                      (regexp-quote (match-string 0)))
7862                                    "\
7863 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7864                                    delim "\\)"))
7865                      (while (progn
7866                               (forward-line 1)
7867                               (and (looking-at regexp)
7868                                    (prog1
7869                                        (match-beginning 1)
7870                                      (push (or (match-string 2)
7871                                                (match-string 1))
7872                                            url)
7873                                      (push (setq end (or (match-end 2)
7874                                                          (match-end 1)))
7875                                            points)
7876                                      (push (or (match-beginning 2)
7877                                                (match-beginning 1))
7878                                            points)))))
7879                      (match-beginning 2)))
7880           (let (gnus-article-mouse-face widget-mouse-face)
7881             (while points
7882               (gnus-article-add-button (pop points) (pop points)
7883                                        'gnus-button-push
7884                                        (list beg (assq 'gnus-button-url-regexp
7885                                                        gnus-button-alist)))))
7886           (let ((overlay (gnus-make-overlay start end)))
7887             (gnus-overlay-put overlay 'evaporate t)
7888             (gnus-overlay-put overlay 'gnus-button-url
7889                               (list (mapconcat 'identity (nreverse url) "")))
7890             (when gnus-article-mouse-face
7891               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7892           t)
7893       (goto-char opoint))))
7894
7895 ;; Add buttons to the head of an article.
7896 (defun gnus-article-add-buttons-to-head ()
7897   "Add buttons to the head of the article."
7898   (interactive)
7899   (gnus-with-article-headers
7900     (let (beg end)
7901       (dolist (entry gnus-header-button-alist)
7902         ;; Each alist entry.
7903         (goto-char (point-min))
7904         (while (re-search-forward (car entry) nil t)
7905           ;; Each header matching the entry.
7906           (setq beg (match-beginning 0))
7907           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7908                              (match-beginning 0))
7909                         (point-max)))
7910           (goto-char beg)
7911           (while (re-search-forward (eval (nth 1 entry)) end t)
7912             ;; Each match within a header.
7913             (let* ((entry (cdr entry))
7914                    (start (match-beginning (nth 1 entry)))
7915                    (end (match-end (nth 1 entry)))
7916                    (form (nth 2 entry)))
7917               (goto-char (match-end 0))
7918               (when (eval form)
7919                 (gnus-article-add-button
7920                  start end (nth 3 entry)
7921                  (buffer-substring (match-beginning (nth 4 entry))
7922                                    (match-end (nth 4 entry)))))))
7923           (goto-char end))))))
7924
7925 ;;; External functions:
7926
7927 (defun gnus-article-add-button (from to fun &optional data text)
7928   "Create a button between FROM and TO with callback FUN and data DATA."
7929   (when gnus-article-button-face
7930     (gnus-overlay-put (gnus-make-overlay from to nil t)
7931                       'face gnus-article-button-face))
7932   (gnus-add-text-properties
7933    from to
7934    (nconc (and gnus-article-mouse-face
7935                (list gnus-mouse-face-prop gnus-article-mouse-face))
7936           (list 'gnus-callback fun)
7937           (and data (list 'gnus-data data))))
7938   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7939                          :help-echo (or text "Follow the link")
7940                          :keymap gnus-url-button-map
7941                          :button-keymap gnus-widget-button-keymap))
7942
7943 (defun gnus-article-copy-string ()
7944   "Copy the string in the button to the kill ring."
7945   (interactive)
7946   (gnus-article-check-buffer)
7947   (let ((data (get-text-property (point) 'gnus-string)))
7948     (when data
7949       (with-temp-buffer
7950         (insert data)
7951         (copy-region-as-kill (point-min) (point-max))
7952         (message "Copied %s" data)))))
7953
7954 ;;; Internal functions:
7955
7956 (defun gnus-article-set-globals ()
7957   (with-current-buffer gnus-summary-buffer
7958     (gnus-set-global-variables)))
7959
7960 (defun gnus-signature-toggle (end)
7961   (gnus-with-article-buffer
7962     (let ((inhibit-point-motion-hooks t))
7963       (if (text-property-any end (point-max) 'article-type 'signature)
7964           (progn
7965             (gnus-delete-wash-type 'signature)
7966             (gnus-remove-text-properties-when
7967              'article-type 'signature end (point-max)
7968              (cons 'article-type (cons 'signature
7969                                        gnus-hidden-properties))))
7970         (gnus-add-wash-type 'signature)
7971         (gnus-add-text-properties-when
7972          'article-type nil end (point-max)
7973          (cons 'article-type (cons 'signature
7974                                    gnus-hidden-properties)))))
7975     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7976       (gnus-set-mode-line 'article))))
7977
7978 (defun gnus-button-push (marker-and-entry)
7979   ;; Push button starting at MARKER.
7980   (save-excursion
7981     (let* ((marker (car marker-and-entry))
7982            (entry (cadr marker-and-entry))
7983            (regexp (car entry))
7984            (inhibit-point-motion-hooks t))
7985       (goto-char marker)
7986       ;; This is obviously true, or something bad is happening :)
7987       ;; But we need it to have the match-data
7988       (when (looking-at (or (if (symbolp regexp)
7989                                 (symbol-value regexp)
7990                               regexp)))
7991         (let ((fun (nth 3 entry))
7992               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7993                              (get-char-property marker 'gnus-button-url))
7994                         (mapcar (lambda (group)
7995                                   (let ((string (match-string group)))
7996                                     (set-text-properties
7997                                      0 (length string) nil string)
7998                                     string))
7999                                 (nthcdr 4 entry)))))
8000
8001           (cond
8002            ((fboundp fun)
8003             (apply fun args))
8004            ((and (boundp fun)
8005                  (fboundp (symbol-value fun)))
8006             (apply (symbol-value fun) args))
8007            (t
8008             (gnus-message 1 "You must define `%S' to use this button"
8009                           (cons fun args)))))))))
8010
8011 (defun gnus-parse-news-url (url)
8012   (let (scheme server port group message-id articles)
8013     (with-temp-buffer
8014       (insert url)
8015       (goto-char (point-min))
8016       (when (looking-at "\\([A-Za-z]+\\):")
8017         (setq scheme (match-string 1))
8018         (goto-char (match-end 0)))
8019       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8020         (setq server (match-string 1))
8021         (setq port (if (stringp (match-string 3))
8022                        (string-to-number (match-string 3))
8023                      (match-string 3)))
8024         (goto-char (match-end 0)))
8025
8026       (cond
8027        ((looking-at "\\(.*@.*\\)")
8028         (setq message-id (match-string 1)))
8029        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8030         (setq group (match-string 1)
8031               articles (split-string (match-string 2) "-")))
8032        ((looking-at "\\([^/]+\\)/?")
8033         (setq group (match-string 1)))
8034        (t
8035         (error "Unknown news URL syntax"))))
8036     (list scheme server port group message-id articles)))
8037
8038 (defun gnus-button-handle-news (url)
8039   "Fetch a news URL."
8040   (destructuring-bind (scheme server port group message-id articles)
8041       (gnus-parse-news-url url)
8042     (cond
8043      (message-id
8044       (with-current-buffer gnus-summary-buffer
8045         (if server
8046             (let ((gnus-refer-article-method
8047                    (nconc (list (list 'nntp server))
8048                           gnus-refer-article-method))
8049                   (nntp-port-number (or port "nntp")))
8050               (gnus-message 7 "Fetching %s with %s"
8051                             message-id gnus-refer-article-method)
8052               (gnus-summary-refer-article message-id))
8053           (gnus-summary-refer-article message-id))))
8054      (group
8055       (gnus-button-fetch-group url)))))
8056
8057 (defun gnus-button-patch (library line)
8058   "Visit an Emacs Lisp library LIBRARY on line LINE."
8059   (interactive)
8060   (let ((file (locate-library (file-name-nondirectory library))))
8061     (unless file
8062       (error "Couldn't find library %s" library))
8063     (find-file file)
8064     (goto-char (point-min))
8065     (forward-line (1- (string-to-number line)))))
8066
8067 (defun gnus-button-handle-man (url)
8068   "Fetch a man page."
8069   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8070   (when (eq gnus-button-man-handler 'woman)
8071     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8072   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8073   (funcall gnus-button-man-handler url))
8074
8075 (defun gnus-button-handle-info-url (url)
8076   "Fetch an info URL."
8077   (setq url (mm-subst-char-in-string ?+ ?\  url))
8078   (cond
8079    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8080     (gnus-info-find-node
8081      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8082                      "Gnus")
8083              ")" (gnus-url-unhex-string (match-string 2 url)))))
8084    ((string-match "([^)\"]+)[^\"]+" url)
8085     (setq url
8086           (gnus-replace-in-string
8087            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8088     (gnus-info-find-node url))
8089    (t (error "Can't parse %s" url))))
8090
8091 (defun gnus-button-handle-info-url-gnome (url)
8092   "Fetch GNOME style info URL."
8093   (setq url (mm-subst-char-in-string ?_ ?\  url))
8094   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8095       (gnus-info-find-node
8096        (concat "("
8097                (gnus-url-unhex-string
8098                  (match-string 1 url))
8099                ")"
8100                (or (gnus-url-unhex-string
8101                     (match-string 2 url))
8102                    "Top")))
8103     (error "Can't parse %s" url)))
8104
8105 (defun gnus-button-handle-info-url-kde (url)
8106   "Fetch KDE style info URL."
8107   (gnus-info-find-node (gnus-url-unhex-string url)))
8108
8109 ;; (info) will autoload info.el
8110 (declare-function Info-menu "info" (menu-item &optional fork))
8111 (declare-function Info-index-next "info" (num))
8112
8113 (defun gnus-button-handle-info-keystrokes (url)
8114   "Call `info' when pushing the corresponding URL button."
8115   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8116   (let (node indx comma)
8117     (if (string-match
8118          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8119                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8120                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8121                  "\\(?:[ \t\n,]*\\)\\)?")
8122          url)
8123         (setq node (match-string 2 url)
8124               indx (match-string 3 url))
8125       (error "Can't parse %s" url))
8126     (info)
8127     (Info-directory)
8128     (Info-menu node)
8129     (when (> (length indx) 0)
8130       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8131                             "\\([ \t\n,]*\\)")
8132                     indx)
8133       (setq comma (match-string 2 indx))
8134       (setq indx  (match-string 1 indx))
8135       (Info-index indx)
8136       (when comma
8137         (dotimes (i (with-temp-buffer
8138                       (insert comma)
8139                       ;; Note: the XEmacs version of `how-many' takes
8140                       ;; no optional argument.
8141                       (goto-char (point-min))
8142                       (how-many ",")))
8143           (Info-index-next 1)))
8144       nil)))
8145
8146 (autoload 'pgg-snarf-keys-region "pgg")
8147 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8148 (declare-function pgg-display-output-buffer "pgg" (start end status))
8149
8150 (defun gnus-button-openpgp (url)
8151   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8152   (with-temp-buffer
8153     (mm-url-insert-file-contents-external url)
8154     (pgg-snarf-keys-region (point-min) (point-max))
8155     (pgg-display-output-buffer nil nil nil)))
8156
8157 (defun gnus-button-message-id (message-id)
8158   "Fetch MESSAGE-ID."
8159   (with-current-buffer gnus-summary-buffer
8160     (gnus-summary-refer-article message-id)))
8161
8162 (defun gnus-button-fetch-group (address &rest ignore)
8163   "Fetch GROUP specified by ADDRESS."
8164   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8165                       address)
8166     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8167     ;; for nntp:// and news://
8168     (setq address (match-string 3 address)))
8169   (if (not (string-match "[:/]" address))
8170       ;; This is just a simple group url.
8171       (gnus-group-read-ephemeral-group address gnus-select-method)
8172     (if (not
8173          (string-match
8174           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8175           address))
8176         (error "Can't parse %s" address)
8177       (gnus-group-read-ephemeral-group
8178        (match-string 4 address)
8179        `(nntp ,(match-string 1 address)
8180               (nntp-address ,(match-string 1 address))
8181               (nntp-port-number ,(if (match-end 3)
8182                                      (match-string 3 address)
8183                                    "nntp")))
8184        nil nil nil
8185        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8186
8187 (defun gnus-url-parse-query-string (query &optional downcase)
8188   (let (retval pairs cur key val)
8189     (setq pairs (split-string query "&"))
8190     (while pairs
8191       (setq cur (car pairs)
8192             pairs (cdr pairs))
8193       (if (not (string-match "=" cur))
8194           nil                           ; Grace
8195         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8196               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8197         (if downcase
8198             (setq key (downcase key)))
8199         (setq cur (assoc key retval))
8200         (if cur
8201             (setcdr cur (cons val (cdr cur)))
8202           (setq retval (cons (list key val) retval)))))
8203     retval))
8204
8205 (defun gnus-url-mailto (url)
8206   ;; Send mail to someone
8207   (setq url (replace-regexp-in-string "\n" " " url))
8208   (when (string-match "mailto:/*\\(.*\\)" url)
8209     (setq url (substring url (match-beginning 1) nil)))
8210   (let (to args subject func)
8211     (setq args (gnus-url-parse-query-string
8212                 (if (string-match "^\\?" url)
8213                     (substring url 1)
8214                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8215                       (concat "to=" (match-string 1 url) "&"
8216                               (match-string 2 url))
8217                     (concat "to=" url))))
8218           subject (cdr-safe (assoc "subject" args)))
8219     (gnus-msg-mail)
8220     (while args
8221       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8222       (if (fboundp func)
8223           (funcall func)
8224         (message-position-on-field (caar args)))
8225       (insert (gnus-replace-in-string
8226                (mapconcat 'identity (reverse (cdar args)) ", ")
8227                "\r\n" "\n" t))
8228       (setq args (cdr args)))
8229     (if subject
8230         (message-goto-body)
8231       (message-goto-subject))))
8232
8233 (defun gnus-button-embedded-url (address)
8234   "Activate ADDRESS with `browse-url'."
8235   (browse-url (gnus-strip-whitespace address)))
8236
8237 ;;; Next/prev buttons in the article buffer.
8238
8239 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8240 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8241
8242 (defvar gnus-prev-page-map
8243   (let ((map (make-sparse-keymap)))
8244     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8245     (define-key map "\r" 'gnus-button-prev-page)
8246     map))
8247
8248 (defvar gnus-next-page-map
8249   (let ((map (make-sparse-keymap)))
8250     (define-key map gnus-mouse-2 'gnus-button-next-page)
8251     (define-key map "\r" 'gnus-button-next-page)
8252     map))
8253
8254 (defun gnus-insert-prev-page-button ()
8255   (let ((b (point)) e
8256         (inhibit-read-only t))
8257     (gnus-eval-format
8258      gnus-prev-page-line-format nil
8259      `(keymap ,gnus-prev-page-map
8260               gnus-prev t
8261               gnus-callback gnus-article-button-prev-page
8262               article-type annotation))
8263     (setq e (if (bolp)
8264                 ;; Exclude a newline.
8265                 (1- (point))
8266               (point)))
8267     (when gnus-article-button-face
8268       (gnus-overlay-put (gnus-make-overlay b e nil t)
8269                         'face gnus-article-button-face))
8270     (widget-convert-button
8271      'link b e
8272      :action 'gnus-button-prev-page
8273      :button-keymap gnus-prev-page-map)))
8274
8275 (defun gnus-button-next-page (&optional args more-args)
8276   "Go to the next page."
8277   (interactive)
8278   (let ((win (selected-window)))
8279     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8280     (gnus-article-next-page)
8281     (select-window win)))
8282
8283 (defun gnus-button-prev-page (&optional args more-args)
8284   "Go to the prev page."
8285   (interactive)
8286   (let ((win (selected-window)))
8287     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8288     (gnus-article-prev-page)
8289     (select-window win)))
8290
8291 (defun gnus-insert-next-page-button ()
8292   (let ((b (point)) e
8293         (inhibit-read-only t))
8294     (gnus-eval-format gnus-next-page-line-format nil
8295                       `(keymap ,gnus-next-page-map
8296                                gnus-next t
8297                                gnus-callback gnus-article-button-next-page
8298                                article-type annotation))
8299     (setq e (if (bolp)
8300                 ;; Exclude a newline.
8301                 (1- (point))
8302               (point)))
8303     (when gnus-article-button-face
8304       (gnus-overlay-put (gnus-make-overlay b e nil t)
8305                         'face gnus-article-button-face))
8306     (widget-convert-button
8307      'link b e
8308      :action 'gnus-button-next-page
8309      :button-keymap gnus-next-page-map)))
8310
8311 (defun gnus-article-button-next-page (arg)
8312   "Go to the next page."
8313   (interactive "P")
8314   (let ((win (selected-window)))
8315     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8316     (gnus-article-next-page)
8317     (select-window win)))
8318
8319 (defun gnus-article-button-prev-page (arg)
8320   "Go to the prev page."
8321   (interactive "P")
8322   (let ((win (selected-window)))
8323     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8324     (gnus-article-prev-page)
8325     (select-window win)))
8326
8327 (defvar gnus-decode-header-methods
8328   '(mail-decode-encoded-word-region)
8329   "List of methods used to decode headers.
8330
8331 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8332 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8333 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8334 whose names match REGEXP.
8335
8336 For example:
8337 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8338  mail-decode-encoded-word-region
8339  (\"chinese\" . rfc1843-decode-region))
8340 ")
8341
8342 (defvar gnus-decode-header-methods-cache nil)
8343
8344 (defun gnus-multi-decode-header (start end)
8345   "Apply the functions from `gnus-encoded-word-methods' that match."
8346   (unless (and gnus-decode-header-methods-cache
8347                (eq gnus-newsgroup-name
8348                    (car gnus-decode-header-methods-cache)))
8349     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8350     (dolist (x gnus-decode-header-methods)
8351       (if (symbolp x)
8352           (nconc gnus-decode-header-methods-cache (list x))
8353         (if (and gnus-newsgroup-name
8354                  (string-match (car x) gnus-newsgroup-name))
8355             (nconc gnus-decode-header-methods-cache
8356                    (list (cdr x)))))))
8357   (let ((xlist gnus-decode-header-methods-cache))
8358     (pop xlist)
8359     (save-restriction
8360       (narrow-to-region start end)
8361       (while xlist
8362         (funcall (pop xlist) (point-min) (point-max))))))
8363
8364 ;;;
8365 ;;; Treatment top-level handling.
8366 ;;;
8367
8368 (defvar gnus-inhibit-article-treatments nil)
8369
8370 (defun gnus-treat-article (gnus-treat-condition
8371                            &optional part-number total-parts gnus-treat-type)
8372   (let ((gnus-treat-length (- (point-max) (point-min)))
8373         (alist gnus-treatment-function-alist)
8374         (article-goto-body-goes-to-point-min-p t)
8375         (treated-type
8376          (or (not gnus-treat-type)
8377              (catch 'found
8378                (let ((list gnus-article-treat-types))
8379                  (while list
8380                    (when (string-match (pop list) gnus-treat-type)
8381                      (throw 'found t)))))))
8382         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8383         val elem)
8384     (gnus-run-hooks 'gnus-part-display-hook)
8385     (dolist (elem alist)
8386       (setq val
8387             (save-excursion
8388               (when (gnus-buffer-live-p gnus-summary-buffer)
8389                 (set-buffer gnus-summary-buffer))
8390               (symbol-value (car elem))))
8391       (when (and (or (consp val)
8392                      treated-type)
8393                  (or (not gnus-inhibit-article-treatments)
8394                      (eq gnus-treat-condition 'head))
8395                  (gnus-treat-predicate val)
8396                  (or (not (get (car elem) 'highlight))
8397                      highlightp))
8398         (save-restriction
8399           (funcall (cadr elem)))))))
8400
8401 ;; Dynamic variables.
8402 (defvar part-number)
8403 (defvar total-parts)
8404 (defvar gnus-treat-type)
8405 (defvar gnus-treat-condition)
8406 (defvar gnus-treat-length)
8407
8408 (defun gnus-treat-predicate (val)
8409   (cond
8410    ((null val)
8411     nil)
8412    (gnus-treat-condition
8413     (eq gnus-treat-condition val))
8414    ((and (listp val)
8415          (stringp (car val)))
8416     (apply 'gnus-or (mapcar `(lambda (s)
8417                                (string-match s ,(or gnus-newsgroup-name "")))
8418                             val)))
8419    ((listp val)
8420     (let ((pred (pop val)))
8421       (cond
8422        ((eq pred 'or)
8423         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8424        ((eq pred 'and)
8425         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8426        ((eq pred 'not)
8427         (not (gnus-treat-predicate (car val))))
8428        ((eq pred 'typep)
8429         (equal (car val) gnus-treat-type))
8430        ((functionp pred)
8431         (funcall pred))
8432        (t
8433         (error "%S is not a valid predicate" pred)))))
8434    ((eq val t)
8435     t)
8436    ((eq val 'head)
8437     nil)
8438    ((eq val 'first)
8439     (eq part-number 1))
8440    ((eq val 'last)
8441     (eq part-number total-parts))
8442    ((numberp val)
8443     (< gnus-treat-length val))
8444    (t
8445     (error "%S is not a valid value" val))))
8446
8447 (defun gnus-article-encrypt-body (protocol &optional n)
8448   "Encrypt the article body."
8449   (interactive
8450    (list
8451     (or gnus-article-encrypt-protocol
8452         (gnus-completing-read "Encrypt protocol"
8453                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8454                               t))
8455     current-prefix-arg))
8456   ;; User might hit `K E' instead of `K e', so prompt once.
8457   (when (and gnus-article-encrypt-protocol
8458              gnus-novice-user)
8459     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8460       (error "Encrypt aborted")))
8461   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8462     (unless func
8463       (error "Can't find the encrypt protocol %s" protocol))
8464     (if (member gnus-newsgroup-name '("nndraft:delayed"
8465                                       "nndraft:drafts"
8466                                       "nndraft:queue"))
8467         (error "Can't encrypt the article in group %s"
8468                gnus-newsgroup-name))
8469     (gnus-summary-iterate n
8470       (with-current-buffer gnus-summary-buffer
8471         (let ((mail-parse-charset gnus-newsgroup-charset)
8472               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8473               (summary-buffer gnus-summary-buffer)
8474               references point)
8475           (gnus-set-global-variables)
8476           (when (gnus-group-read-only-p)
8477             (error "The current newsgroup does not support article encrypt"))
8478           (gnus-summary-show-article t)
8479           (setq references
8480               (or (mail-header-references gnus-current-headers) ""))
8481           (set-buffer gnus-article-buffer)
8482           (let* ((inhibit-read-only t)
8483                  (headers
8484                   (mapcar (lambda (field)
8485                             (and (save-restriction
8486                                    (message-narrow-to-head)
8487                                    (goto-char (point-min))
8488                                    (search-forward field nil t))
8489                                  (prog2
8490                                      (message-narrow-to-field)
8491                                      (buffer-string)
8492                                    (delete-region (point-min) (point-max))
8493                                    (widen))))
8494                           '("Content-Type:" "Content-Transfer-Encoding:"
8495                             "Content-Disposition:"))))
8496             (message-narrow-to-head)
8497             (message-remove-header "MIME-Version")
8498             (goto-char (point-max))
8499             (setq point (point))
8500             (insert (apply 'concat headers))
8501             (widen)
8502             (narrow-to-region point (point-max))
8503             (let ((message-options message-options))
8504               (message-options-set 'message-sender user-mail-address)
8505               (message-options-set 'message-recipients user-mail-address)
8506               (message-options-set 'message-sign-encrypt 'not)
8507               (funcall func))
8508             (goto-char (point-min))
8509             (insert "MIME-Version: 1.0\n")
8510             (widen)
8511             (gnus-summary-edit-article-done
8512              references nil summary-buffer t))
8513           (when gnus-keep-backlog
8514             (gnus-backlog-remove-article
8515              (car gnus-article-current) (cdr gnus-article-current)))
8516           (gnus-flush-original-article-buffer)
8517           (when gnus-use-cache
8518             (gnus-cache-update-article
8519              (car gnus-article-current) (cdr gnus-article-current))))))))
8520
8521 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8522   "The following specs can be used:
8523 %t  The security MIME type
8524 %i  Additional info
8525 %d  Details
8526 %D  Details if button is pressed")
8527
8528 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8529   "The following specs can be used:
8530 %t  The security MIME type
8531 %i  Additional info
8532 %d  Details
8533 %D  Details if button is pressed")
8534
8535 (defvar gnus-mime-security-button-line-format-alist
8536   '((?t gnus-tmp-type ?s)
8537     (?i gnus-tmp-info ?s)
8538     (?d gnus-tmp-details ?s)
8539     (?D gnus-tmp-pressed-details ?s)))
8540
8541 (defvar gnus-mime-security-button-commands
8542   '((gnus-article-press-button "\r" "Show Detail")
8543     (undefined "v")
8544     (undefined "t")
8545     (undefined "C")
8546     (gnus-mime-security-save-part "o" "Save...")
8547     (undefined "\C-o")
8548     (undefined "r")
8549     (undefined "d")
8550     (undefined "c")
8551     (undefined "i")
8552     (undefined "E")
8553     (undefined "e")
8554     (undefined "p")
8555     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8556     (undefined ".")))
8557
8558 (defvar gnus-mime-security-button-map
8559   (let ((map (make-sparse-keymap)))
8560     (define-key map gnus-mouse-2 'gnus-article-push-button)
8561     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8562     (dolist (c gnus-mime-security-button-commands)
8563       (define-key map (cadr c) (car c)))
8564     map))
8565
8566 (easy-menu-define
8567   gnus-mime-security-button-menu gnus-mime-security-button-map
8568   "Security button menu."
8569   `("Security Part"
8570     ,@(delq nil
8571             (mapcar (lambda (c)
8572                       (unless (eq (car c) 'undefined)
8573                         (vector (caddr c) (car c) :active t)))
8574                     gnus-mime-security-button-commands))))
8575
8576 (defun gnus-mime-security-button-menu (event prefix)
8577   "Construct a context-sensitive menu of security commands."
8578   (interactive "e\nP")
8579   (save-window-excursion
8580     (let ((pos (event-start event)))
8581       (select-window (posn-window pos))
8582       (goto-char (posn-point pos))
8583       (gnus-article-check-buffer)
8584       (popup-menu gnus-mime-security-button-menu nil prefix))))
8585
8586 (defvar gnus-mime-security-details-buffer nil)
8587
8588 (defvar gnus-mime-security-button-pressed nil)
8589
8590 (defvar gnus-mime-security-show-details-inline t
8591   "If non-nil, show details in the article buffer.")
8592
8593 (defun gnus-mime-security-verify-or-decrypt (handle)
8594   (mm-remove-parts (cdr handle))
8595   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8596         point (inhibit-read-only t))
8597     (if region
8598         (goto-char (car region)))
8599     (setq point (point))
8600     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8601       (let* ((mm-verify-option 'known)
8602              (mm-decrypt-option 'known)
8603              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8604         (unless (eq nparts (cdr handle))
8605           (mm-destroy-parts (cdr handle))
8606           (setcdr handle nparts))))
8607     (gnus-mime-display-security handle)
8608     (when region
8609       (delete-region (point) (cdr region))
8610       (set-marker (car region) nil)
8611       (set-marker (cdr region) nil))
8612     (goto-char point)))
8613
8614 (defun gnus-mime-security-show-details (handle)
8615   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8616     (if (not details)
8617         (gnus-message 5 "No details.")
8618       (if gnus-mime-security-show-details-inline
8619           (let ((gnus-mime-security-button-pressed
8620                  (not (get-text-property (point) 'gnus-mime-details)))
8621                 (gnus-mime-security-button-line-format
8622                  (get-text-property (point) 'gnus-line-format))
8623                 (inhibit-read-only t))
8624             (forward-char -1)
8625             (while (eq (get-text-property (point) 'gnus-line-format)
8626                        gnus-mime-security-button-line-format)
8627               (forward-char -1))
8628             (forward-char)
8629             (save-restriction
8630               (narrow-to-region (point) (point))
8631               (gnus-insert-mime-security-button handle))
8632             (delete-region (point)
8633                            (or (text-property-not-all
8634                                 (point) (point-max)
8635                                 'gnus-line-format
8636                                 gnus-mime-security-button-line-format)
8637                                (point-max))))
8638         ;; Not inlined.
8639         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8640             (with-current-buffer gnus-mime-security-details-buffer
8641               (erase-buffer)
8642               t)
8643           (setq gnus-mime-security-details-buffer
8644                 (gnus-get-buffer-create "*MIME Security Details*")))
8645         (with-current-buffer gnus-mime-security-details-buffer
8646           (insert details)
8647           (goto-char (point-min)))
8648         (pop-to-buffer gnus-mime-security-details-buffer)))))
8649
8650 (defun gnus-mime-security-press-button (handle)
8651   (save-excursion
8652     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8653         (gnus-mime-security-show-details handle)
8654       (gnus-mime-security-verify-or-decrypt handle))))
8655
8656 (defun gnus-insert-mime-security-button (handle &optional displayed)
8657   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8658          (gnus-tmp-type
8659           (concat
8660            (or (nth 2 (assoc protocol mm-verify-function-alist))
8661                (nth 2 (assoc protocol mm-decrypt-function-alist))
8662                "Unknown")
8663            (if (equal (car handle) "multipart/signed")
8664                " Signed" " Encrypted")
8665            " Part"))
8666          (gnus-tmp-info
8667           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8668               "Undecided"))
8669          (gnus-tmp-details
8670           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8671          gnus-tmp-pressed-details
8672          b e)
8673     (setq gnus-tmp-details
8674           (if gnus-tmp-details
8675               (concat "\n" gnus-tmp-details)
8676             ""))
8677     (setq gnus-tmp-pressed-details
8678           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8679     (unless (bolp)
8680       (insert "\n"))
8681     (setq b (point))
8682     (gnus-eval-format
8683      gnus-mime-security-button-line-format
8684      gnus-mime-security-button-line-format-alist
8685      `(keymap ,gnus-mime-security-button-map
8686          gnus-callback gnus-mime-security-press-button
8687          gnus-line-format ,gnus-mime-security-button-line-format
8688          gnus-mime-details ,gnus-mime-security-button-pressed
8689          article-type annotation
8690          gnus-data ,handle))
8691     (setq e (if (bolp)
8692                 ;; Exclude a newline.
8693                 (1- (point))
8694               (point)))
8695     (when gnus-article-button-face
8696       (gnus-overlay-put (gnus-make-overlay b e nil t)
8697                         'face gnus-article-button-face))
8698     (widget-convert-button
8699      'link b e
8700      :mime-handle handle
8701      :action 'gnus-widget-press-button
8702      :button-keymap gnus-mime-security-button-map
8703      :help-echo
8704      (lambda (widget)
8705        ;; Needed to properly clear the message due to a bug in
8706        ;; wid-edit (XEmacs only).
8707        (when (boundp 'help-echo-owns-message)
8708          (setq help-echo-owns-message t))
8709        (format
8710         "%S: show detail; %S: more options"
8711         (aref gnus-mouse-2 0)
8712         (aref gnus-down-mouse-3 0))))))
8713
8714 (defun gnus-mime-display-security (handle)
8715   (save-restriction
8716     (narrow-to-region (point) (point))
8717     (unless (gnus-unbuttonized-mime-type-p (car handle))
8718       (gnus-insert-mime-security-button handle))
8719     (gnus-mime-display-part (cadr handle))
8720     (unless (bolp)
8721       (insert "\n"))
8722     (unless (gnus-unbuttonized-mime-type-p (car handle))
8723       (let ((gnus-mime-security-button-line-format
8724              gnus-mime-security-button-end-line-format))
8725         (gnus-insert-mime-security-button handle)))
8726     (mm-set-handle-multipart-parameter
8727      handle 'gnus-region (cons (point-min-marker) (point-max-marker)))
8728     (goto-char (point-max))))
8729
8730 (defun gnus-mime-security-run-function (function)
8731   "Run FUNCTION with the security part under point."
8732   (gnus-article-check-buffer)
8733   (let ((data (get-text-property (point) 'gnus-data))
8734         buffer handle)
8735     (when (and (stringp (car-safe data))
8736                (setq buffer (mm-handle-multipart-original-buffer data))
8737                (setq handle (cadr data)))
8738       (if (bufferp (mm-handle-buffer handle))
8739           (progn
8740             (setq handle (cons buffer (copy-sequence (cdr handle))))
8741             (mm-handle-set-undisplayer handle nil))
8742         (setq handle (mm-make-handle
8743                       buffer
8744                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8745                       nil nil nil nil nil nil)))
8746       (funcall function handle))))
8747
8748 (defun gnus-mime-security-save-part ()
8749   "Save the security part under point."
8750   (interactive)
8751   (gnus-mime-security-run-function 'mm-save-part))
8752
8753 (defun gnus-mime-security-pipe-part ()
8754   "Pipe the security part under point to a process."
8755   (interactive)
8756   (gnus-mime-security-run-function 'mm-pipe-part))
8757
8758 (gnus-ems-redefine)
8759
8760 (provide 'gnus-art)
8761
8762 (run-hooks 'gnus-art-load-hook)
8763
8764 ;;; gnus-art.el ends here