Merge remote branch 'origin/no-gnus'
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 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-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'mm-bodies)
41 (require 'mail-parse)
42 (require 'mm-decode)
43 (require 'mm-view)
44 (require 'wid-edit)
45 (require 'mm-uu)
46 (require 'message)
47 (require 'mouse)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice regexp
167                  (repeat regexp))
168   :group 'gnus-article-hiding)
169
170 (defcustom gnus-visible-headers
171   "^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:"
172   "*All headers that do not match this regexp will be hidden.
173 This variable can also be a list of regexp of headers to remain visible.
174 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
175   :type '(choice
176           (repeat :value-to-internal (lambda (widget value)
177                                        (custom-split-regexp-maybe value))
178                   :match (lambda (widget value)
179                            (or (stringp value)
180                                (widget-editable-list-match widget value)))
181                   regexp)
182           (const :tag "Use gnus-ignored-headers" nil)
183           regexp)
184   :group 'gnus-article-hiding)
185
186 (defcustom gnus-sorted-header-list
187   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
188     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
189   "*This variable is a list of regular expressions.
190 If it is non-nil, headers that match the regular expressions will
191 be placed first in the article buffer in the sequence specified by
192 this list."
193   :type '(repeat regexp)
194   :group 'gnus-article-hiding)
195
196 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
197   "Headers that are only to be displayed if they have interesting data.
198 Possible values in this list are:
199
200   'empty       Headers with no content.
201   'newsgroups  Newsgroup identical to Gnus group.
202   'to-address  To identical to To-address.
203   'to-list     To identical to To-list.
204   'cc-list     CC identical to To-list.
205   'followup-to Followup-to identical to Newsgroups.
206   'reply-to    Reply-to identical to From.
207   'date        Date less than four days old.
208   'long-to     To and/or Cc longer than 1024 characters.
209   'many-to     Multiple To and/or Cc."
210   :type '(set (const :tag "Headers with no content." empty)
211               (const :tag "Newsgroups identical to Gnus group." newsgroups)
212               (const :tag "To identical to To-address." to-address)
213               (const :tag "To identical to To-list." to-list)
214               (const :tag "CC identical to To-list." cc-list)
215               (const :tag "Followup-to identical to Newsgroups." followup-to)
216               (const :tag "Reply-to identical to From." reply-to)
217               (const :tag "Date less than four days old." date)
218               (const :tag "To and/or Cc longer than 1024 characters." long-to)
219               (const :tag "Multiple To and/or Cc headers." many-to))
220   :group 'gnus-article-hiding)
221
222 (defcustom gnus-article-skip-boring nil
223   "Skip over text that is not worth reading.
224 By default, if you set this t, then Gnus will display citations and
225 signatures, but will never scroll down to show you a page consisting
226 only of boring text.  Boring text is controlled by
227 `gnus-article-boring-faces'."
228   :version "22.1"
229   :type 'boolean
230   :group 'gnus-article-hiding)
231
232 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
233   "Regexp matching signature separator.
234 This can also be a list of regexps.  In that case, it will be checked
235 from head to tail looking for a separator.  Searches will be done from
236 the end of the buffer."
237   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
238                  (regexp)
239                  (repeat :tag "List of regexp" regexp))
240   :group 'gnus-article-signature)
241
242 (defcustom gnus-signature-limit nil
243   "Provide a limit to what is considered a signature.
244 If it is a number, no signature may not be longer (in characters) than
245 that number.  If it is a floating point number, no signature may be
246 longer (in lines) than that number.  If it is a function, the function
247 will be called without any parameters, and if it returns nil, there is
248 no signature in the buffer.  If it is a string, it will be used as a
249 regexp.  If it matches, the text in question is not a signature.
250
251 This can also be a list of the above values."
252   :type '(choice (const nil)
253                  (integer :value 200)
254                  (number :value 4.0)
255                  function
256                  (regexp :value ".*"))
257   :group 'gnus-article-signature)
258
259 (defcustom gnus-hidden-properties '(invisible t intangible t)
260   "Property list to use for hiding text."
261   :type 'sexp
262   :group 'gnus-article-hiding)
263
264 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
265 ;; frames in a session.
266 (defcustom gnus-article-x-face-command
267   (if (featurep 'xemacs)
268       (if (or (gnus-image-type-available-p 'xface)
269               (gnus-image-type-available-p 'pbm))
270           'gnus-display-x-face-in-from
271         "{ echo \
272 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
273 ; uncompface; } | icontopbm | ee -")
274     (if (gnus-image-type-available-p 'pbm)
275         'gnus-display-x-face-in-from
276       "{ echo \
277 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
278 ; uncompface; } | icontopbm | display -"))
279   "*String or function to be executed to display an X-Face header.
280 If it is a string, the command will be executed in a sub-shell
281 asynchronously.  The compressed face will be piped to this command."
282   :type `(choice string
283                  (function-item gnus-display-x-face-in-from)
284                  function)
285   :version "21.1"
286   :group 'gnus-picon
287   :group 'gnus-article-washing)
288
289 (defcustom gnus-article-x-face-too-ugly nil
290   "Regexp matching posters whose face shouldn't be shown automatically."
291   :type '(choice regexp (const nil))
292   :group 'gnus-article-washing)
293
294 (defcustom gnus-article-banner-alist nil
295   "Banner alist for stripping.
296 For example,
297      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
298   :version "21.1"
299   :type '(repeat (cons symbol regexp))
300   :group 'gnus-article-washing)
301
302 (gnus-define-group-parameter
303  banner
304  :variable-document
305  "Alist of regexps (to match group names) and banner."
306  :variable-group gnus-article-washing
307  :parameter-type
308  '(choice :tag "Banner"
309           :value nil
310           (const :tag "Remove signature" signature)
311           (symbol :tag "Item in `gnus-article-banner-alist'" none)
312           regexp
313           (const :tag "None" nil))
314  :parameter-document
315  "If non-nil, specify how to remove `banners' from articles.
316
317 Symbol `signature' means to remove signatures delimited by
318 `gnus-signature-separator'.  Any other symbol is used to look up a
319 regular expression to match the banner in `gnus-article-banner-alist'.
320 A string is used as a regular expression to match the banner
321 directly.")
322
323 (defcustom gnus-article-address-banner-alist nil
324   "Alist of mail addresses and banners.
325 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
326 to match a mail address in the From: header, BANNER is one of a symbol
327 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
328 If ADDRESS matches author's mail address, it will remove things like
329 advertisements.  For example:
330
331 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
332 "
333   :type '(repeat
334           (cons
335            (regexp :tag "Address")
336            (choice :tag "Banner" :value nil
337                    (const :tag "Remove signature" signature)
338                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
339                    regexp
340                    (const :tag "None" nil))))
341   :version "22.1"
342   :group 'gnus-article-washing)
343
344 (defmacro gnus-emphasis-custom-with-format (&rest body)
345   `(let ((format "\
346 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
347 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
348      ,@body))
349
350 (defun gnus-emphasis-custom-value-to-external (value)
351   (gnus-emphasis-custom-with-format
352    (if (consp (car value))
353        (list (format format (car (car value)) (cdr (car value)))
354              2
355              (if (nth 1 value) 2 3)
356              (nth 2 value))
357      value)))
358
359 (defun gnus-emphasis-custom-value-to-internal (value)
360   (gnus-emphasis-custom-with-format
361    (let ((regexp (concat "\\`"
362                          (format (regexp-quote format)
363                                  "\\([^()]+\\)" "\\([^()]+\\)")
364                          "\\'"))
365          pattern)
366      (if (string-match regexp (setq pattern (car value)))
367          (list (cons (match-string 1 pattern) (match-string 2 pattern))
368                (= (nth 2 value) 2)
369                (nth 3 value))
370        value))))
371
372 (defcustom gnus-emphasis-alist
373   (let ((types
374          '(("\\*" "\\*" bold nil 2)
375            ("_" "_" underline)
376            ("/" "/" italic)
377            ("_/" "/_" underline-italic)
378            ("_\\*" "\\*_" underline-bold)
379            ("\\*/" "/\\*" bold-italic)
380            ("_\\*/" "/\\*_" underline-bold-italic))))
381     (nconc
382      (gnus-emphasis-custom-with-format
383       (mapcar (lambda (spec)
384                 (list (format format (car spec) (cadr spec))
385                       (or (nth 3 spec) 2)
386                       (or (nth 4 spec) 3)
387                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
388               types))
389      '(;; I've never seen anyone use this strikethru convention whereas I've
390        ;; several times seen it triggered by normal text.  --Stef
391        ;; Miles suggests that this form is sometimes used but for italics,
392        ;; so maybe we should map it to `italic'.
393        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
394        ;; 2 3 gnus-emphasis-strikethru)
395        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
396         2 3 gnus-emphasis-underline))))
397   "*Alist that says how to fontify certain phrases.
398 Each item looks like this:
399
400   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
401
402 The first element is a regular expression to be matched.  The second
403 is a number that says what regular expression grouping used to find
404 the entire emphasized word.  The third is a number that says what
405 regexp grouping should be displayed and highlighted.  The fourth
406 is the face used for highlighting."
407   :type
408   '(repeat
409     (menu-choice
410      :format "%[Customizing Style%]\n%v"
411      :indent 2
412      (group :tag "Default"
413             :value ("" 0 0 default)
414             :value-create
415             (lambda (widget)
416               (let ((value (widget-get
417                             (cadr (widget-get (widget-get widget :parent)
418                                               :args))
419                             :value)))
420                 (if (not (eq (nth 2 value) 'default))
421                     (widget-put
422                      widget
423                      :value
424                      (gnus-emphasis-custom-value-to-external value))))
425               (widget-group-value-create widget))
426             regexp
427             (integer :format "Match group: %v")
428             (integer :format "Emphasize group: %v")
429             face)
430      (group :tag "Simple"
431             :value (("_" . "_") nil default)
432             (cons :format "%v"
433                   (regexp :format "Start regexp: %v")
434                   (regexp :format "End regexp: %v"))
435             (boolean :format "Show start and end patterns: %[%v%]\n"
436                      :on " On " :off " Off ")
437             face)))
438   :get (lambda (symbol)
439          (mapcar 'gnus-emphasis-custom-value-to-internal
440                  (default-value symbol)))
441   :set (lambda (symbol value)
442          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
443                                      value)))
444   :group 'gnus-article-emphasis)
445
446 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
447   "A regexp to describe whitespace which should not be emphasized.
448 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
449 The former avoids underlining of leading and trailing whitespace,
450 and the latter avoids underlining any whitespace at all."
451   :version "21.1"
452   :group 'gnus-article-emphasis
453   :type 'regexp)
454
455 (defface gnus-emphasis-bold '((t (:bold t)))
456   "Face used for displaying strong emphasized text (*word*)."
457   :group 'gnus-article-emphasis)
458
459 (defface gnus-emphasis-italic '((t (:italic t)))
460   "Face used for displaying italic emphasized text (/word/)."
461   :group 'gnus-article-emphasis)
462
463 (defface gnus-emphasis-underline '((t (:underline t)))
464   "Face used for displaying underlined emphasized text (_word_)."
465   :group 'gnus-article-emphasis)
466
467 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
468   "Face used for displaying underlined bold emphasized text (_*word*_)."
469   :group 'gnus-article-emphasis)
470
471 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
472   "Face used for displaying underlined italic emphasized text (_/word/_)."
473   :group 'gnus-article-emphasis)
474
475 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
476   "Face used for displaying bold italic emphasized text (/*word*/)."
477   :group 'gnus-article-emphasis)
478
479 (defface gnus-emphasis-underline-bold-italic
480   '((t (:bold t :italic t :underline t)))
481   "Face used for displaying underlined bold italic emphasized text.
482 Example: (_/*word*/_)."
483   :group 'gnus-article-emphasis)
484
485 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
486                                       '((t (:strikethru t)))
487                                     '((t (:strike-through t))))
488   "Face used for displaying strike-through text (-word-)."
489   :group 'gnus-article-emphasis)
490
491 (defface gnus-emphasis-highlight-words
492   '((t (:background "black" :foreground "yellow")))
493   "Face used for displaying highlighted words."
494   :group 'gnus-article-emphasis)
495
496 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
497   "Format for display of Date headers in article bodies.
498 See `format-time-string' for the possible values.
499
500 The variable can also be function, which should return a complete Date
501 header.  The function is called with one argument, the time, which can
502 be fed to `format-time-string'."
503   :type '(choice string function)
504   :link '(custom-manual "(gnus)Article Date")
505   :group 'gnus-article-washing)
506
507 (defcustom gnus-save-all-headers t
508   "*If non-nil, don't remove any headers before saving.
509 This will be overridden by the `:headers' property that the symbol of
510 the saver function, which is specified by `gnus-default-article-saver',
511 might have."
512   :group 'gnus-article-saving
513   :type 'boolean)
514
515 (defcustom gnus-prompt-before-saving 'always
516   "*This variable says how much prompting is to be done when saving articles.
517 If it is nil, no prompting will be done, and the articles will be
518 saved to the default files.  If this variable is `always', each and
519 every article that is saved will be preceded by a prompt, even when
520 saving large batches of articles.  If this variable is neither nil not
521 `always', there the user will be prompted once for a file name for
522 each invocation of the saving commands."
523   :group 'gnus-article-saving
524   :type '(choice (item always)
525                  (item :tag "never" nil)
526                  (sexp :tag "once" :format "%t\n" :value t)))
527
528 (defcustom gnus-saved-headers gnus-visible-headers
529   "Headers to keep if `gnus-save-all-headers' is nil.
530 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
531 If that variable is nil, however, all headers that match this regexp
532 will be kept while the rest will be deleted before saving.  This and
533 `gnus-save-all-headers' will be overridden by the `:headers' property
534 that the symbol of the saver function, which is specified by
535 `gnus-default-article-saver', might have."
536   :group 'gnus-article-saving
537   :type 'regexp)
538
539 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
540 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
541   "A function to save articles in your favorite format.
542 The function will be called by way of the `gnus-summary-save-article'
543 command, and friends such as `gnus-summary-save-article-rmail'.
544
545 Gnus provides the following functions:
546
547 * gnus-summary-save-in-rmail (Rmail format)
548 * gnus-summary-save-in-mail (Unix mail format)
549 * gnus-summary-save-in-folder (MH folder)
550 * gnus-summary-save-in-file (article format)
551 * gnus-summary-save-body-in-file (article body)
552 * gnus-summary-save-in-vm (use VM's folder format)
553 * gnus-summary-write-to-file (article format -- overwrite)
554 * gnus-summary-write-body-to-file (article body -- overwrite)
555 * gnus-summary-save-in-pipe (article format)
556
557 The symbol of each function may have the following properties:
558
559 * :decode
560 The value non-nil means save decoded articles.  This is meaningful
561 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
562 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
563 `gnus-summary-save-in-pipe'.
564
565 * :function
566 The value specifies an alternative function which appends, not
567 overwrites, articles to a file.  This implies that when saving many
568 articles at a time, `gnus-prompt-before-saving' is bound to t and all
569 articles are saved in a single file.  This is meaningful only with
570 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
571
572 * :headers
573 The value specifies the symbol of a variable of which the value
574 specifies headers to be saved.  If it is omitted,
575 `gnus-save-all-headers' and `gnus-saved-headers' control what
576 headers should be saved."
577   :group 'gnus-article-saving
578   :type '(radio (function-item gnus-summary-save-in-rmail)
579                 (function-item gnus-summary-save-in-mail)
580                 (function-item gnus-summary-save-in-folder)
581                 (function-item gnus-summary-save-in-file)
582                 (function-item gnus-summary-save-body-in-file)
583                 (function-item gnus-summary-save-in-vm)
584                 (function-item gnus-summary-write-to-file)
585                 (function-item gnus-summary-write-body-to-file)
586                 (function-item gnus-summary-save-in-pipe)
587                 (function)))
588
589 (defcustom gnus-article-save-coding-system
590   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
591       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
592       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
593       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
594   "Coding system used to save decoded articles to a file.
595
596 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
597 which can safely encode any characters in text.  This is used by the
598 commands including:
599
600 * gnus-summary-save-article-file
601 * gnus-summary-save-article-body-file
602 * gnus-summary-write-article-file
603 * gnus-summary-write-article-body-file
604
605 and the functions to which you may set `gnus-default-article-saver':
606
607 * gnus-summary-save-in-file
608 * gnus-summary-save-body-in-file
609 * gnus-summary-write-to-file
610 * gnus-summary-write-body-to-file
611
612 Those commands and functions save just text displayed in the article
613 buffer to a file if the value of this variable is non-nil.  Note that
614 buttonized MIME parts will be lost in a saved file in that case.
615 Otherwise, raw articles will be saved."
616   :group 'gnus-article-saving
617   :type `(choice
618           :format "%{%t%}:\n %[Value Menu%] %v"
619           (const :tag "Save raw articles" nil)
620           ,@(delq nil
621                   (mapcar
622                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
623                    '((const :tag "UTF-8" utf-8)
624                      (const :tag "iso-2022-7bit" iso-2022-7bit)
625                      (const :tag "Emacs internal" emacs-mule)
626                      (const :tag "escape-quoted" escape-quoted))))
627           (symbol :tag "Coding system")))
628
629 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
630   "A function generating a file name to save articles in Rmail format.
631 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
632   :group 'gnus-article-saving
633   :type 'function)
634
635 (defcustom gnus-mail-save-name 'gnus-plain-save-name
636   "A function generating a file name to save articles in Unix mail format.
637 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
638   :group 'gnus-article-saving
639   :type 'function)
640
641 (defcustom gnus-folder-save-name 'gnus-folder-save-name
642   "A function generating a file name to save articles in MH folder.
643 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
644   :group 'gnus-article-saving
645   :type 'function)
646
647 (defcustom gnus-file-save-name 'gnus-numeric-save-name
648   "A function generating a file name to save articles in article format.
649 The function is called with NEWSGROUP, HEADERS, and optional
650 LAST-FILE."
651   :group 'gnus-article-saving
652   :type 'function)
653
654 (defcustom gnus-split-methods
655   '((gnus-article-archive-name)
656     (gnus-article-nndoc-name))
657   "*Variable used to suggest where articles are to be saved.
658 For instance, if you would like to save articles related to Gnus in
659 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
660 you could set this variable to something like:
661
662  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
663    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
664
665 This variable is an alist where the key is the match and the
666 value is a list of possible files to save in if the match is
667 non-nil.
668
669 If the match is a string, it is used as a regexp match on the
670 article.  If the match is a symbol, that symbol will be funcalled
671 from the buffer of the article to be saved with the newsgroup as the
672 parameter.  If it is a list, it will be evalled in the same buffer.
673
674 If this form or function returns a string, this string will be used as a
675 possible file name; and if it returns a non-nil list, that list will be
676 used as possible file names."
677   :group 'gnus-article-saving
678   :type '(repeat (choice (list :value (fun) function)
679                          (cons :value ("" "") regexp (repeat string))
680                          (sexp :value nil))))
681
682 (defcustom gnus-page-delimiter "^\^L"
683   "*Regexp describing what to use as article page delimiters.
684 The default value is \"^\^L\", which is a form linefeed at the
685 beginning of a line."
686   :type 'regexp
687   :group 'gnus-article-various)
688
689 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
690   "*The format specification for the article mode line.
691 See `gnus-summary-mode-line-format' for a closer description.
692
693 The following additional specs are available:
694
695 %w  The article washing status.
696 %m  The number of MIME parts in the article."
697   :version "24.1"
698   :type 'string
699   :group 'gnus-article-various)
700
701 (defcustom gnus-article-mode-hook nil
702   "*A hook for Gnus article mode."
703   :type 'hook
704   :group 'gnus-article-various)
705
706 (when (featurep 'xemacs)
707   ;; Extracted from gnus-xmas-define in order to preserve user settings
708   (when (fboundp 'turn-off-scroll-in-place)
709     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
710   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
711   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
712
713 (defcustom gnus-article-menu-hook nil
714   "*Hook run after the creation of the article mode menu."
715   :type 'hook
716   :group 'gnus-article-various)
717
718 (defcustom gnus-article-prepare-hook nil
719   "*A hook called after an article has been prepared in the article buffer."
720   :type 'hook
721   :group 'gnus-article-various)
722
723 (defcustom gnus-copy-article-ignored-headers nil
724   "List of headers to be removed when copying an article.
725 Each element is a regular expression."
726   :version "23.1" ;; No Gnus
727   :type '(repeat regexp)
728   :group 'gnus-article-various)
729
730 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
731                         "Gnus 5.10 (Emacs 22.1)")
732
733 (defface gnus-button
734   '((t (:weight bold)))
735   "Face used for highlighting a button in the article buffer."
736   :group 'gnus-article-buttons)
737
738 (defcustom gnus-article-button-face 'gnus-button
739   "Face used for highlighting buttons in the article buffer.
740
741 An article button is a piece of text that you can activate by pressing
742 `RET' or `mouse-2' above it."
743   :type 'face
744   :group 'gnus-article-buttons)
745
746 (defcustom gnus-article-mouse-face 'highlight
747   "Face used for mouse highlighting in the article buffer.
748
749 Article buttons will be displayed in this face when the cursor is
750 above them."
751   :type 'face
752   :group 'gnus-article-buttons)
753
754 (defcustom gnus-signature-face 'gnus-signature
755   "Face used for highlighting a signature in the article buffer.
756 Obsolete; use the face `gnus-signature' for customizations instead."
757   :type 'face
758   :group 'gnus-article-highlight
759   :group 'gnus-article-signature)
760
761 (defface gnus-signature
762   '((t
763      (:italic t)))
764   "Face used for highlighting a signature in the article buffer."
765   :group 'gnus-article-highlight
766   :group 'gnus-article-signature)
767 ;; backward-compatibility alias
768 (put 'gnus-signature-face 'face-alias 'gnus-signature)
769 (put 'gnus-signature-face 'obsolete-face "22.1")
770
771 (defface gnus-header-from
772   '((((class color)
773       (background dark))
774      (:foreground "PaleGreen1"))
775     (((class color)
776       (background light))
777      (:foreground "red3"))
778     (t
779      (:italic t)))
780   "Face used for displaying from headers."
781   :group 'gnus-article-headers
782   :group 'gnus-article-highlight)
783 ;; backward-compatibility alias
784 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
785 (put 'gnus-header-from-face 'obsolete-face "22.1")
786
787 (defface gnus-header-subject
788   '((((class color)
789       (background dark))
790      (:foreground "SeaGreen1"))
791     (((class color)
792       (background light))
793      (:foreground "red4"))
794     (t
795      (:bold t :italic t)))
796   "Face used for displaying subject headers."
797   :group 'gnus-article-headers
798   :group 'gnus-article-highlight)
799 ;; backward-compatibility alias
800 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
801 (put 'gnus-header-subject-face 'obsolete-face "22.1")
802
803 (defface gnus-header-newsgroups
804   '((((class color)
805       (background dark))
806      (:foreground "yellow" :italic t))
807     (((class color)
808       (background light))
809      (:foreground "MidnightBlue" :italic t))
810     (t
811      (:italic t)))
812   "Face used for displaying newsgroups headers.
813 In the default setup this face is only used for crossposted
814 articles."
815   :group 'gnus-article-headers
816   :group 'gnus-article-highlight)
817 ;; backward-compatibility alias
818 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
819 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
820
821 (defface gnus-header-name
822   '((((class color)
823       (background dark))
824      (:foreground "SpringGreen2"))
825     (((class color)
826       (background light))
827      (:foreground "maroon"))
828     (t
829      (:bold t)))
830   "Face used for displaying header names."
831   :group 'gnus-article-headers
832   :group 'gnus-article-highlight)
833 ;; backward-compatibility alias
834 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
835 (put 'gnus-header-name-face 'obsolete-face "22.1")
836
837 (defface gnus-header-content
838   '((((class color)
839       (background dark))
840      (:foreground "SpringGreen1" :italic t))
841     (((class color)
842       (background light))
843      (:foreground "indianred4" :italic t))
844     (t
845      (:italic t)))  "Face used for displaying header content."
846   :group 'gnus-article-headers
847   :group 'gnus-article-highlight)
848 ;; backward-compatibility alias
849 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
850 (put 'gnus-header-content-face 'obsolete-face "22.1")
851
852 (defcustom gnus-header-face-alist
853   '(("From" nil gnus-header-from)
854     ("Subject" nil gnus-header-subject)
855     ("Newsgroups:.*," nil gnus-header-newsgroups)
856     ("" gnus-header-name gnus-header-content))
857   "*Controls highlighting of article headers.
858
859 An alist of the form (HEADER NAME CONTENT).
860
861 HEADER is a regular expression which should match the name of a
862 header and NAME and CONTENT are either face names or nil.
863
864 The name of each header field will be displayed using the face
865 specified by the first element in the list where HEADER matches
866 the header name and NAME is non-nil.  Similarly, the content will
867 be displayed by the first non-nil matching CONTENT face."
868   :group 'gnus-article-headers
869   :group 'gnus-article-highlight
870   :type '(repeat (list (regexp :tag "Header")
871                        (choice :tag "Name"
872                                (item :tag "skip" nil)
873                                (face :value default))
874                        (choice :tag "Content"
875                                (item :tag "skip" nil)
876                                (face :value default)))))
877
878 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
879                                           '((xface . (:face gnus-x-face)))
880                                         '((pbm . (:face gnus-x-face))
881                                           (png . nil)))
882   "Alist of image types and properties applied to Face and X-Face images.
883 Here are examples:
884
885 ;; Specify the altitude of Face images in the From header.
886 \(setq gnus-face-properties-alist
887       '((pbm . (:face gnus-x-face :ascent 80))
888         (png . (:ascent 80))))
889
890 ;; Show Face images as pressed buttons.
891 \(setq gnus-face-properties-alist
892       '((pbm . (:face gnus-x-face :relief -2))
893         (png . (:relief -2))))
894
895 See the manual for the valid properties for various image types.
896 Currently, `pbm' is used for X-Face images and `png' is used for Face
897 images in Emacs.  Only the `:face' property is effective on the `xface'
898 image type in XEmacs if it is built with the libcompface library."
899   :version "23.1" ;; No Gnus
900   :group 'gnus-article-headers
901   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
902
903 (defcustom gnus-article-decode-hook
904   '(article-decode-charset article-decode-encoded-words
905                            article-decode-group-name article-decode-idna-rhs)
906   "*Hook run to decode charsets in articles."
907   :group 'gnus-article-headers
908   :type 'hook)
909
910 (defcustom gnus-display-mime-function 'gnus-display-mime
911   "Function to display MIME articles."
912   :group 'gnus-article-mime
913   :type 'function)
914
915 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
916   "Function used to decode headers.")
917
918 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
919   "Function used to decode addresses.")
920
921 (defvar gnus-article-dumbquotes-map
922   '((?\200 "EUR")
923     (?\202 ",")
924     (?\203 "f")
925     (?\204 ",,")
926     (?\205 "...")
927     (?\213 "<")
928     (?\214 "OE")
929     (?\221 "`")
930     (?\222 "'")
931     (?\223 "``")
932     (?\224 "\"")
933     (?\225 "*")
934     (?\226 "-")
935     (?\227 "--")
936     (?\230 "~")
937     (?\231 "(TM)")
938     (?\233 ">")
939     (?\234 "oe")
940     (?\264 "'"))
941   "Table for MS-to-Latin1 translation.")
942
943 (defcustom gnus-ignored-mime-types nil
944   "List of MIME types that should be ignored by Gnus."
945   :version "21.1"
946   :group 'gnus-article-mime
947   :type '(repeat regexp))
948
949 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
950   "List of MIME types that should not be given buttons when rendered inline.
951 See also `gnus-buttonized-mime-types' which may override this variable.
952 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
953   :version "21.1"
954   :group 'gnus-article-mime
955   :type '(repeat regexp))
956
957 (defcustom gnus-buttonized-mime-types nil
958   "List of MIME types that should be given buttons when rendered inline.
959 If set, this variable overrides `gnus-unbuttonized-mime-types'.
960 To see e.g. security buttons you could set this to
961 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
962 this list to display radio buttons that allow you to choose one of two
963 media types those mails include.  See also `mm-discouraged-alternatives'.
964 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
965   :version "22.1"
966   :group 'gnus-article-mime
967   :type '(repeat regexp))
968
969 (defcustom gnus-inhibit-mime-unbuttonizing nil
970   "If non-nil, all MIME parts get buttons.
971 When nil (the default value), then some MIME parts do not get buttons,
972 as described by the variables `gnus-buttonized-mime-types' and
973 `gnus-unbuttonized-mime-types'."
974   :version "22.1"
975   :group 'gnus-article-mime
976   :type 'boolean)
977
978 (defcustom gnus-body-boundary-delimiter "_"
979   "String used to delimit header and body.
980 This variable is used by `gnus-article-treat-body-boundary' which can
981 be controlled by `gnus-treat-body-boundary'."
982   :version "22.1"
983   :group 'gnus-article-various
984   :type '(choice (item :tag "None" :value nil)
985                  string))
986
987 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
988                                   "/usr/share/picons")
989   "Defines the location of the faces database.
990 For information on obtaining this database of pretty pictures, please
991 see http://www.cs.indiana.edu/picons/ftp/index.html"
992   :version "22.1"
993   :type '(repeat directory)
994   :link '(url-link :tag "download"
995                    "http://www.cs.indiana.edu/picons/ftp/index.html")
996   :link '(custom-manual "(gnus)Picons")
997   :group 'gnus-picon)
998
999 (defun gnus-picons-installed-p ()
1000   "Say whether picons are installed on your machine."
1001   (let ((installed nil))
1002     (dolist (database gnus-picon-databases)
1003       (when (file-exists-p database)
1004         (setq installed t)))
1005     installed))
1006
1007 (defcustom gnus-article-mime-part-function nil
1008   "Function called with a MIME handle as the argument.
1009 This is meant for people who want to do something automatic based
1010 on parts -- for instance, adding Vcard info to a database."
1011   :group 'gnus-article-mime
1012   :type '(choice (const nil)
1013                  function))
1014
1015 (defcustom gnus-mime-multipart-functions nil
1016   "An alist of MIME types to functions to display them."
1017   :version "21.1"
1018   :group 'gnus-article-mime
1019   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1020
1021 (defcustom gnus-article-date-headers '(combined-lapsed)
1022   "A list of Date header formats to display.
1023 Valid formats are `ut' (universal time), `local' (local time
1024 zone), `english' (readable English), `lapsed' (elapsed time),
1025 `combined-lapsed' (both the original date and the elapsed time),
1026 `original' (the original date header), `iso8601' (ISO8601
1027 format), and `user-defined' (a user-defined format defined by the
1028 `gnus-article-time-format' variable).
1029
1030 You have as many date headers as you want in the article buffer.
1031 Some of these headers are updated automatically.  See
1032 `gnus-article-update-date-headers' for details."
1033   :version "24.1"
1034   :group 'gnus-article-headers
1035   :type '(repeat
1036           (item :tag "Universal time (UT)" :value 'ut)
1037           (item :tag "Local time zone" :value 'local)
1038           (item :tag "Readable English" :value 'english)
1039           (item :tag "Elapsed time" :value 'lapsed)
1040           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1041           (item :tag "Original date header" :value 'original)
1042           (item :tag "ISO8601 format" :value 'iso8601)
1043           (item :tag "User-defined" :value 'user-defined)))
1044
1045 (defcustom gnus-article-update-date-headers nil
1046   "A number that says how often to update the date header (in seconds).
1047 If nil, don't update it at all."
1048   :version "24.1"
1049   :group 'gnus-article-headers
1050   :type '(choice
1051           (item :tag "Don't update" :value nil)
1052           integer))
1053
1054 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1055   "Function called with a MIME handle as the argument.
1056 This is meant for people who want to view first matched part.
1057 For `undisplayed-alternative' (default), the first undisplayed
1058 part or alternative part is used.  For `undisplayed', the first
1059 undisplayed part is used.  For a function, the first part which
1060 the function return t is used.  For nil, the first part is
1061 used."
1062   :version "21.1"
1063   :group 'gnus-article-mime
1064   :type '(choice
1065           (item :tag "first" :value nil)
1066           (item :tag "undisplayed" :value undisplayed)
1067           (item :tag "undisplayed or alternative"
1068                 :value undisplayed-alternative)
1069           (function)))
1070
1071 (defcustom gnus-mime-action-alist
1072   '(("save to file" . gnus-mime-save-part)
1073     ("save and strip" . gnus-mime-save-part-and-strip)
1074     ("replace with file" . gnus-mime-replace-part)
1075     ("delete part" . gnus-mime-delete-part)
1076     ("display as text" . gnus-mime-inline-part)
1077     ("view the part" . gnus-mime-view-part)
1078     ("pipe to command" . gnus-mime-pipe-part)
1079     ("toggle display" . gnus-article-press-button)
1080     ("toggle display" . gnus-article-view-part-as-charset)
1081     ("view as type" . gnus-mime-view-part-as-type)
1082     ("view internally" . gnus-mime-view-part-internally)
1083     ("view externally" . gnus-mime-view-part-externally))
1084   "An alist of actions that run on the MIME attachment."
1085   :group 'gnus-article-mime
1086   :type '(repeat (cons (string :tag "name")
1087                        (function))))
1088
1089 (defcustom gnus-auto-select-part 1
1090   "Advance to next MIME part when deleting or stripping parts.
1091
1092 When 0, point will be placed on the same part as before.  When
1093 positive (negative), move point forward (backwards) this many
1094 parts.  When nil, redisplay article."
1095   :version "23.1" ;; No Gnus
1096   :group 'gnus-article-mime
1097   :type '(choice (const nil :tag "Redisplay article.")
1098                  (const 1 :tag "Next part.")
1099                  (const 0 :tag "Current part.")
1100                  integer))
1101
1102 ;;;
1103 ;;; The treatment variables
1104 ;;;
1105
1106 (defvar gnus-part-display-hook nil
1107   "Hook called on parts that are to receive treatment.")
1108
1109 (defvar gnus-article-treat-custom
1110   '(choice (const :tag "Off" nil)
1111            (const :tag "On" t)
1112            (const :tag "Header" head)
1113            (const :tag "First" first)
1114            (const :tag "Last" last)
1115            (integer :tag "Less")
1116            (repeat :tag "Groups" regexp)
1117            (sexp :tag "Predicate")))
1118
1119 (defvar gnus-article-treat-head-custom
1120   '(choice (const :tag "Off" nil)
1121            (const :tag "Header" head)))
1122
1123 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1124                                    "text/x-patch")
1125   "Parts to treat.")
1126
1127 (defvar gnus-inhibit-treatment nil
1128   "Whether to inhibit treatment.")
1129
1130 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1131   "Highlight the signature.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-custom)
1137 (put 'gnus-treat-highlight-signature 'highlight t)
1138
1139 (defcustom gnus-treat-buttonize 100000
1140   "Add buttons.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :group 'gnus-article-treat
1144   :link '(custom-manual "(gnus)Customizing Articles")
1145   :type gnus-article-treat-custom)
1146 (put 'gnus-treat-buttonize 'highlight t)
1147
1148 (defcustom gnus-treat-buttonize-head 'head
1149   "Add buttons to the head.
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :group 'gnus-article-treat
1153   :link '(custom-manual "(gnus)Customizing Articles")
1154   :type gnus-article-treat-head-custom)
1155 (put 'gnus-treat-buttonize-head 'highlight t)
1156
1157 (defcustom gnus-treat-date 'head
1158   "Display dates according to the `gnus-article-date-headers' variable.
1159 Valid values are nil, t, `head', `first', `last', an integer or a
1160 predicate.  See Info node `(gnus)Customizing Articles'."
1161   :version "24.1"
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-head-custom)
1165
1166 (defcustom gnus-treat-emphasize 50000
1167   "Emphasize text.
1168 Valid values are nil, t, `head', `first', `last', an integer or a
1169 predicate.  See Info node `(gnus)Customizing Articles'."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-custom)
1173 (put 'gnus-treat-emphasize 'highlight t)
1174
1175 (defcustom gnus-treat-strip-cr nil
1176   "Remove carriage returns.
1177 Valid values are nil, t, `head', `first', `last', an integer or a
1178 predicate.  See Info node `(gnus)Customizing Articles'."
1179   :version "22.1"
1180   :group 'gnus-article-treat
1181   :link '(custom-manual "(gnus)Customizing Articles")
1182   :type gnus-article-treat-custom)
1183
1184 (defcustom gnus-treat-unsplit-urls nil
1185   "Remove newlines from within URLs.
1186 Valid values are nil, t, `head', `first', `last', an integer or a
1187 predicate.  See Info node `(gnus)Customizing Articles'."
1188   :version "22.1"
1189   :group 'gnus-article-treat
1190   :link '(custom-manual "(gnus)Customizing Articles")
1191   :type gnus-article-treat-custom)
1192
1193 (defcustom gnus-treat-leading-whitespace nil
1194   "Remove leading whitespace in headers.
1195 Valid values are nil, t, `head', `first', `last', an integer or a
1196 predicate.  See Info node `(gnus)Customizing Articles'."
1197   :version "22.1"
1198   :group 'gnus-article-treat
1199   :link '(custom-manual "(gnus)Customizing Articles")
1200   :type gnus-article-treat-custom)
1201
1202 (defcustom gnus-treat-hide-headers 'head
1203   "Hide headers.
1204 Valid values are nil, t, `head', `first', `last', an integer or a
1205 predicate.  See Info node `(gnus)Customizing Articles'."
1206   :group 'gnus-article-treat
1207   :link '(custom-manual "(gnus)Customizing Articles")
1208   :type gnus-article-treat-head-custom)
1209
1210 (defcustom gnus-treat-hide-boring-headers nil
1211   "Hide boring headers.
1212 Valid values are nil, t, `head', `first', `last', an integer or a
1213 predicate.  See Info node `(gnus)Customizing Articles'."
1214   :group 'gnus-article-treat
1215   :link '(custom-manual "(gnus)Customizing Articles")
1216   :type gnus-article-treat-head-custom)
1217
1218 (defcustom gnus-treat-hide-signature nil
1219   "Hide the signature.
1220 Valid values are nil, t, `head', `first', `last', an integer or a
1221 predicate.  See Info node `(gnus)Customizing Articles'."
1222   :group 'gnus-article-treat
1223   :link '(custom-manual "(gnus)Customizing Articles")
1224   :type gnus-article-treat-custom)
1225
1226 (defcustom gnus-treat-fill-article nil
1227   "Fill the article.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'."
1230   :group 'gnus-article-treat
1231   :link '(custom-manual "(gnus)Customizing Articles")
1232   :type gnus-article-treat-custom)
1233
1234 (defcustom gnus-treat-hide-citation nil
1235   "Hide cited text.
1236 Valid values are nil, t, `head', `first', `last', an integer or a
1237 predicate.  See Info node `(gnus)Customizing Articles'.
1238
1239 See `gnus-article-highlight-citation' for variables used to
1240 control what it hides."
1241   :group 'gnus-article-treat
1242   :link '(custom-manual "(gnus)Customizing Articles")
1243   :type gnus-article-treat-custom)
1244
1245 (defcustom gnus-treat-hide-citation-maybe nil
1246   "Hide cited text according to certain conditions.
1247 Valid values are nil, t, `head', `first', `last', an integer or a
1248 predicate.  See Info node `(gnus)Customizing Articles'.
1249
1250 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1251 how to control what it hides."
1252   :group 'gnus-article-treat
1253   :link '(custom-manual "(gnus)Customizing Articles")
1254   :type gnus-article-treat-custom)
1255
1256 (defcustom gnus-treat-strip-list-identifiers 'head
1257   "Strip list identifiers from `gnus-list-identifiers`.
1258 Valid values are nil, t, `head', `first', `last', an integer or a
1259 predicate.  See Info node `(gnus)Customizing Articles'."
1260   :version "21.1"
1261   :group 'gnus-article-treat
1262   :link '(custom-manual "(gnus)Customizing Articles")
1263   :type gnus-article-treat-custom)
1264
1265 (gnus-define-group-parameter
1266  list-identifier
1267  :variable-document
1268  "Alist of regexps and correspondent identifiers."
1269  :variable-group gnus-article-washing
1270  :parameter-type
1271  '(choice :tag "Identifier"
1272           :value nil
1273           (symbol :tag "Item in `gnus-list-identifiers'" none)
1274           regexp
1275           (const :tag "None" nil))
1276  :parameter-document
1277  "If non-nil, specify how to remove `identifiers' from articles' subject.
1278
1279 Any symbol is used to look up a regular expression to match the
1280 banner in `gnus-list-identifiers'.  A string is used as a regular
1281 expression to match the identifier directly.")
1282
1283 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1284                         "Gnus 5.10 (Emacs 22.1)")
1285
1286 (defcustom gnus-treat-strip-pem nil
1287   "Strip PEM signatures.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-custom)
1293
1294 (defcustom gnus-treat-strip-banner t
1295   "Strip banners from articles.
1296 The banner to be stripped is specified in the `banner' group parameter.
1297 Valid values are nil, t, `head', `first', `last', an integer or a
1298 predicate.  See Info node `(gnus)Customizing Articles'."
1299   :group 'gnus-article-treat
1300   :link '(custom-manual "(gnus)Customizing Articles")
1301   :type gnus-article-treat-custom)
1302
1303 (defcustom gnus-treat-highlight-headers 'head
1304   "Highlight the headers.
1305 Valid values are nil, t, `head', `first', `last', an integer or a
1306 predicate.  See Info node `(gnus)Customizing Articles'."
1307   :group 'gnus-article-treat
1308   :link '(custom-manual "(gnus)Customizing Articles")
1309   :type gnus-article-treat-head-custom)
1310 (put 'gnus-treat-highlight-headers 'highlight t)
1311
1312 (defcustom gnus-treat-highlight-citation t
1313   "Highlight cited text.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :group 'gnus-article-treat
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :type gnus-article-treat-custom)
1319 (put 'gnus-treat-highlight-citation 'highlight t)
1320
1321 (defcustom gnus-treat-strip-headers-in-body t
1322   "Strip the X-No-Archive header line from the beginning of the body.
1323 Valid values are nil, t, `head', `first', `last', an integer or a
1324 predicate.  See Info node `(gnus)Customizing Articles'."
1325   :version "21.1"
1326   :group 'gnus-article-treat
1327   :link '(custom-manual "(gnus)Customizing Articles")
1328   :type gnus-article-treat-custom)
1329
1330 (defcustom gnus-treat-strip-trailing-blank-lines nil
1331   "Strip trailing blank lines.
1332 Valid values are nil, t, `head', `first', `last', an integer or a
1333 predicate.  See Info node `(gnus)Customizing Articles'.
1334
1335 When set to t, it also strips trailing blanks in all MIME parts.
1336 Consider to use `last' instead."
1337   :group 'gnus-article-treat
1338   :link '(custom-manual "(gnus)Customizing Articles")
1339   :type gnus-article-treat-custom)
1340
1341 (defcustom gnus-treat-strip-leading-blank-lines nil
1342   "Strip leading blank lines.
1343 Valid values are nil, t, `head', `first', `last', an integer or a
1344 predicate.  See Info node `(gnus)Customizing Articles'.
1345
1346 When set to t, it also strips trailing blanks in all MIME parts."
1347   :group 'gnus-article-treat
1348   :link '(custom-manual "(gnus)Customizing Articles")
1349   :type gnus-article-treat-custom)
1350
1351 (defcustom gnus-treat-strip-multiple-blank-lines nil
1352   "Strip multiple blank lines.
1353 Valid values are nil, t, `head', `first', `last', an integer or a
1354 predicate.  See Info node `(gnus)Customizing Articles'."
1355   :group 'gnus-article-treat
1356   :link '(custom-manual "(gnus)Customizing Articles")
1357   :type gnus-article-treat-custom)
1358
1359 (defcustom gnus-treat-unfold-headers 'head
1360   "Unfold folded header lines.
1361 Valid values are nil, t, `head', `first', `last', an integer or a
1362 predicate.  See Info node `(gnus)Customizing Articles'."
1363   :version "22.1"
1364   :group 'gnus-article-treat
1365   :link '(custom-manual "(gnus)Customizing Articles")
1366   :type gnus-article-treat-custom)
1367
1368 (defcustom gnus-article-unfold-long-headers nil
1369   "If non-nil, allow unfolding headers even if the header is long.
1370 If it is a regexp, only long headers matching this regexp are unfolded.
1371 If it is t, all long headers are unfolded.
1372
1373 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1374   :version "23.1" ;; No Gnus
1375   :group 'gnus-article-treat
1376   :type '(choice (const nil)
1377                  (const :tag "all" t)
1378                  (regexp)))
1379
1380 (defcustom gnus-treat-fold-headers nil
1381   "Fold headers.
1382 Valid values are nil, t, `head', `first', `last', an integer or a
1383 predicate.  See Info node `(gnus)Customizing Articles'."
1384   :version "22.1"
1385   :group 'gnus-article-treat
1386   :link '(custom-manual "(gnus)Customizing Articles")
1387   :type gnus-article-treat-custom)
1388
1389 (defcustom gnus-treat-fold-newsgroups 'head
1390   "Fold the Newsgroups and Followup-To headers.
1391 Valid values are nil, t, `head', `first', `last', an integer or a
1392 predicate.  See Info node `(gnus)Customizing Articles'."
1393   :version "22.1"
1394   :group 'gnus-article-treat
1395   :link '(custom-manual "(gnus)Customizing Articles")
1396   :type gnus-article-treat-custom)
1397
1398 (defcustom gnus-treat-overstrike t
1399   "Treat overstrike highlighting.
1400 Valid values are nil, t, `head', `first', `last', an integer or a
1401 predicate.  See Info node `(gnus)Customizing Articles'."
1402   :group 'gnus-article-treat
1403   :link '(custom-manual "(gnus)Customizing Articles")
1404   :type gnus-article-treat-custom)
1405 (put 'gnus-treat-overstrike 'highlight t)
1406
1407 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1408   "Treat ANSI SGR control sequences.
1409 Valid values are nil, t, `head', `first', `last', an integer or a
1410 predicate.  See Info node `(gnus)Customizing Articles'."
1411   :group 'gnus-article-treat
1412   :link '(custom-manual "(gnus)Customizing Articles")
1413   :type gnus-article-treat-custom)
1414
1415 (make-obsolete-variable 'gnus-treat-display-xface
1416                         'gnus-treat-display-x-face "Emacs 22.1")
1417
1418 (defcustom gnus-treat-display-x-face
1419   (and (not noninteractive)
1420        (gnus-image-type-available-p 'xbm)
1421        (if (featurep 'xemacs)
1422            (featurep 'xface)
1423          (condition-case nil
1424              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1425                   (executable-find "icontopbm"))
1426            ;; shell-command-to-string may signal an error, e.g. if
1427            ;; shell-file-name is not found.
1428            (error nil)))
1429        'head)
1430   "Display X-Face headers.
1431 Valid values are nil and `head'.
1432 See Info node `(gnus)Customizing Articles' and Info node
1433 `(gnus)X-Face' for details."
1434   :group 'gnus-article-treat
1435   :version "21.1"
1436   :link '(custom-manual "(gnus)Customizing Articles")
1437   :link '(custom-manual "(gnus)X-Face")
1438   :type gnus-article-treat-head-custom
1439   :set (lambda (symbol value)
1440          (set-default
1441           symbol
1442           (cond ((or (boundp symbol) (get symbol 'saved-value))
1443                  value)
1444                 ((boundp 'gnus-treat-display-xface)
1445                  (message "\
1446 ** gnus-treat-display-xface is an obsolete variable;\
1447  use gnus-treat-display-x-face instead")
1448                  (default-value 'gnus-treat-display-xface))
1449                 ((get 'gnus-treat-display-xface 'saved-value)
1450                  (message "\
1451 ** gnus-treat-display-xface is an obsolete variable;\
1452  use gnus-treat-display-x-face instead")
1453                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1454                 (t
1455                  value)))))
1456 (put 'gnus-treat-display-x-face 'highlight t)
1457
1458 (defcustom gnus-treat-display-face
1459   (and (not noninteractive)
1460        (gnus-image-type-available-p 'png)
1461        'head)
1462   "Display Face headers.
1463 Valid values are nil, t, `head', `first', `last', an integer or a
1464 predicate.  See Info node `(gnus)Customizing Articles' and Info
1465 node `(gnus)Face' for details."
1466   :group 'gnus-article-treat
1467   :version "22.1"
1468   :link '(custom-manual "(gnus)Customizing Articles")
1469   :link '(custom-manual "(gnus)X-Face")
1470   :type gnus-article-treat-head-custom)
1471 (put 'gnus-treat-display-face 'highlight t)
1472
1473 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1474   "Display smileys.
1475 Valid values are nil, t, `head', `first', `last', an integer or a
1476 predicate.  See Info node `(gnus)Customizing Articles' and Info
1477 node `(gnus)Smileys' for details."
1478   :group 'gnus-article-treat
1479   :version "21.1"
1480   :link '(custom-manual "(gnus)Customizing Articles")
1481   :link '(custom-manual "(gnus)Smileys")
1482   :type gnus-article-treat-custom)
1483 (put 'gnus-treat-display-smileys 'highlight t)
1484
1485 (defcustom gnus-treat-from-picon
1486   (if (and (gnus-image-type-available-p 'xpm)
1487            (gnus-picons-installed-p))
1488       'head nil)
1489   "Display picons in the From header.
1490 Valid values are nil, t, `head', `first', `last', an integer or a
1491 predicate.  See Info node `(gnus)Customizing Articles' and Info
1492 node `(gnus)Picons' for details."
1493   :version "22.1"
1494   :group 'gnus-article-treat
1495   :group 'gnus-picon
1496   :link '(custom-manual "(gnus)Customizing Articles")
1497   :link '(custom-manual "(gnus)Picons")
1498   :type gnus-article-treat-head-custom)
1499 (put 'gnus-treat-from-picon 'highlight t)
1500
1501 (defcustom gnus-treat-mail-picon
1502   (if (and (gnus-image-type-available-p 'xpm)
1503            (gnus-picons-installed-p))
1504       'head nil)
1505   "Display picons in To and Cc headers.
1506 Valid values are nil, t, `head', `first', `last', an integer or a
1507 predicate.  See Info node `(gnus)Customizing Articles' and Info
1508 node `(gnus)Picons' for details."
1509   :version "22.1"
1510   :group 'gnus-article-treat
1511   :group 'gnus-picon
1512   :link '(custom-manual "(gnus)Customizing Articles")
1513   :link '(custom-manual "(gnus)Picons")
1514   :type gnus-article-treat-head-custom)
1515 (put 'gnus-treat-mail-picon 'highlight t)
1516
1517 (defcustom gnus-treat-newsgroups-picon
1518   (if (and (gnus-image-type-available-p 'xpm)
1519            (gnus-picons-installed-p))
1520       'head nil)
1521   "Display picons in the Newsgroups and Followup-To headers.
1522 Valid values are nil, t, `head', `first', `last', an integer or a
1523 predicate.  See Info node `(gnus)Customizing Articles' and Info
1524 node `(gnus)Picons' for details."
1525   :version "22.1"
1526   :group 'gnus-article-treat
1527   :group 'gnus-picon
1528   :link '(custom-manual "(gnus)Customizing Articles")
1529   :link '(custom-manual "(gnus)Picons")
1530   :type gnus-article-treat-head-custom)
1531 (put 'gnus-treat-newsgroups-picon 'highlight t)
1532
1533 (defcustom gnus-treat-from-gravatar nil
1534   "Display gravatars in the From header.
1535 Valid values are nil, t, `head', `first', `last', an integer or a
1536 predicate.  See Info node `(gnus)Customizing Articles' and Info
1537 node `(gnus)Gravatars' for details."
1538   :version "24.1"
1539   :group 'gnus-article-treat
1540   :group 'gnus-gravatar
1541   :link '(custom-manual "(gnus)Customizing Articles")
1542   :link '(custom-manual "(gnus)Gravatars")
1543   :type gnus-article-treat-head-custom)
1544 (put 'gnus-treat-from-gravatar 'highlight t)
1545
1546 (defcustom gnus-treat-mail-gravatar nil
1547   "Display gravatars in To and Cc headers.
1548 Valid values are nil, t, `head', `first', `last', an integer or a
1549 predicate.  See Info node `(gnus)Customizing Articles' and Info
1550 node `(gnus)Gravatars' for details."
1551   :version "24.1"
1552   :group 'gnus-article-treat
1553   :group 'gnus-gravatar
1554   :link '(custom-manual "(gnus)Customizing Articles")
1555   :link '(custom-manual "(gnus)Gravatars")
1556   :type gnus-article-treat-head-custom)
1557 (put 'gnus-treat-mail-gravatar 'highlight t)
1558
1559 (defcustom gnus-treat-body-boundary
1560   (if (or gnus-treat-newsgroups-picon
1561           gnus-treat-mail-picon
1562           gnus-treat-from-picon
1563           gnus-treat-from-gravatar
1564           gnus-treat-mail-gravatar)
1565       ;; If there's much decoration, the user might prefer a boundary.
1566       'head
1567     nil)
1568   "Draw a boundary at the end of the headers.
1569 Valid values are nil and `head'.
1570 See Info node `(gnus)Customizing Articles' for details."
1571   :version "22.1"
1572   :group 'gnus-article-treat
1573   :link '(custom-manual "(gnus)Customizing Articles")
1574   :type gnus-article-treat-head-custom)
1575
1576 (defcustom gnus-treat-capitalize-sentences nil
1577   "Capitalize sentence-starting words.
1578 Valid values are nil, t, `head', `first', `last', an integer or a
1579 predicate.  See Info node `(gnus)Customizing Articles'."
1580   :version "21.1"
1581   :group 'gnus-article-treat
1582   :link '(custom-manual "(gnus)Customizing Articles")
1583   :type gnus-article-treat-custom)
1584
1585 (defcustom gnus-treat-wash-html nil
1586   "Format as HTML.
1587 Valid values are nil, t, `head', `first', `last', an integer or a
1588 predicate.  See Info node `(gnus)Customizing Articles'."
1589   :version "22.1"
1590   :group 'gnus-article-treat
1591   :link '(custom-manual "(gnus)Customizing Articles")
1592   :type gnus-article-treat-custom)
1593
1594 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1595   "Fill long lines.
1596 Valid values are nil, t, `head', `first', `last', an integer or a
1597 predicate.  See Info node `(gnus)Customizing Articles'."
1598   :version "24.1"
1599   :group 'gnus-article-treat
1600   :link '(custom-manual "(gnus)Customizing Articles")
1601   :type gnus-article-treat-custom)
1602
1603 (defcustom gnus-treat-x-pgp-sig nil
1604   "Verify X-PGP-Sig.
1605 To automatically treat X-PGP-Sig, set it to head.
1606 Valid values are nil, t, `head', `first', `last', an integer or a
1607 predicate.  See Info node `(gnus)Customizing Articles'."
1608   :version "22.1"
1609   :group 'gnus-article-treat
1610   :group 'mime-security
1611   :link '(custom-manual "(gnus)Customizing Articles")
1612   :type gnus-article-treat-custom)
1613
1614 (defvar gnus-article-encrypt-protocol-alist
1615   '(("PGP" . mml2015-self-encrypt)))
1616
1617 ;; Set to nil if more than one protocol added to
1618 ;; gnus-article-encrypt-protocol-alist.
1619 (defcustom gnus-article-encrypt-protocol "PGP"
1620   "The protocol used for encrypt articles.
1621 It is a string, such as \"PGP\". If nil, ask user."
1622   :version "22.1"
1623   :type 'string
1624   :group 'mime-security)
1625
1626 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1627                               (mm-coding-system-p 'utf-8)
1628                               (executable-find idna-program))
1629   "Whether IDNA decoding of headers is used when viewing messages.
1630 This requires GNU Libidn, and by default only enabled if it is found."
1631   :version "22.1"
1632   :group 'gnus-article-headers
1633   :type 'boolean)
1634
1635 (defcustom gnus-article-over-scroll nil
1636   "If non-nil, allow scrolling the article buffer even when there no more text."
1637   :version "22.1"
1638   :group 'gnus-article
1639   :type 'boolean)
1640
1641 (defcustom gnus-inhibit-images nil
1642   "Non-nil means inhibit displaying of images inline in the article body."
1643   :version "24.1"
1644   :group 'gnus-article
1645   :type 'boolean)
1646
1647 (defcustom gnus-blocked-images 'gnus-block-private-groups
1648   "Images that have URLs matching this regexp will be blocked.
1649 This can also be a function to be evaluated.  If so, it will be
1650 called with the group name as the parameter, and should return a
1651 regexp."
1652   :version "24.1"
1653   :group 'gnus-art
1654   :type 'regexp)
1655
1656 ;;; Internal variables
1657
1658 (defvar gnus-english-month-names
1659   '("January" "February" "March" "April" "May" "June" "July" "August"
1660     "September" "October" "November" "December"))
1661
1662 (defvar article-goto-body-goes-to-point-min-p nil)
1663 (defvar gnus-article-wash-types nil)
1664 (defvar gnus-article-emphasis-alist nil)
1665 (defvar gnus-article-image-alist nil)
1666
1667 (defvar gnus-article-mime-handle-alist-1 nil)
1668 (defvar gnus-treatment-function-alist
1669   '((gnus-treat-strip-cr gnus-article-remove-cr)
1670     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1671     (gnus-treat-strip-banner gnus-article-strip-banner)
1672     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1673     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1674     (gnus-treat-buttonize gnus-article-add-buttons)
1675     (gnus-treat-fill-article gnus-article-fill-cited-article)
1676     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1677     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1678     (gnus-treat-display-x-face gnus-article-display-x-face)
1679     (gnus-treat-display-face gnus-article-display-face)
1680     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1681     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1682     (gnus-treat-hide-signature gnus-article-hide-signature)
1683     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1684     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1685     (gnus-treat-from-picon gnus-treat-from-picon)
1686     (gnus-treat-mail-picon gnus-treat-mail-picon)
1687     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1688     (gnus-treat-strip-pem gnus-article-hide-pem)
1689     (gnus-treat-date gnus-article-treat-date)
1690     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1691     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1692     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1693     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1694     (gnus-treat-strip-trailing-blank-lines
1695      gnus-article-remove-trailing-blank-lines)
1696     (gnus-treat-strip-leading-blank-lines
1697      gnus-article-strip-leading-blank-lines)
1698     (gnus-treat-strip-multiple-blank-lines
1699      gnus-article-strip-multiple-blank-lines)
1700     (gnus-treat-overstrike gnus-article-treat-overstrike)
1701     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1702     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1703     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1704     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1705     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1706     (gnus-treat-display-smileys gnus-treat-smiley)
1707     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1708     (gnus-treat-wash-html gnus-article-wash-html)
1709     (gnus-treat-emphasize gnus-article-emphasize)
1710     (gnus-treat-hide-citation gnus-article-hide-citation)
1711     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1712     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1713     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1714
1715 (defvar gnus-article-mime-handle-alist nil)
1716 (defvar article-lapsed-timer nil)
1717 (defvar gnus-article-current-summary nil)
1718
1719 (defvar gnus-article-mode-syntax-table
1720   (let ((table (copy-syntax-table text-mode-syntax-table)))
1721     ;; This causes the citation match run O(2^n).
1722     ;; (modify-syntax-entry ?- "w" table)
1723     (modify-syntax-entry ?> ")<" table)
1724     (modify-syntax-entry ?< "(>" table)
1725     ;; make M-. in article buffers work for `foo' strings
1726     (modify-syntax-entry ?' " " table)
1727     (modify-syntax-entry ?` " " table)
1728     table)
1729   "Syntax table used in article mode buffers.
1730 Initialized from `text-mode-syntax-table.")
1731
1732 (defvar gnus-save-article-buffer nil)
1733
1734 (defvar gnus-number-of-articles-to-be-saved nil)
1735
1736 (defvar gnus-inhibit-hiding nil)
1737
1738 (defvar gnus-article-edit-mode nil)
1739
1740 ;;; Macros for dealing with the article buffer.
1741
1742 (defmacro gnus-with-article-headers (&rest forms)
1743   `(with-current-buffer gnus-article-buffer
1744      (save-restriction
1745        (let ((inhibit-read-only t)
1746              (inhibit-point-motion-hooks t)
1747              (case-fold-search t))
1748          (article-narrow-to-head)
1749          ,@forms))))
1750
1751 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1752 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1753
1754 (defmacro gnus-with-article-buffer (&rest forms)
1755   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1756      (with-current-buffer gnus-article-buffer
1757        (let ((inhibit-read-only t))
1758          ,@forms))))
1759
1760 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1761 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1762
1763 (defun gnus-article-goto-header (header)
1764   "Go to HEADER, which is a regular expression."
1765   (re-search-forward (concat "^\\(" header "\\):") nil t))
1766
1767 (defsubst gnus-article-hide-text (b e props)
1768   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1769   (gnus-add-text-properties-when 'article-type nil b e props)
1770   (when (memq 'intangible props)
1771     (put-text-property
1772      (max (1- b) (point-min))
1773      b 'intangible (cddr (memq 'intangible props)))))
1774
1775 (defsubst gnus-article-unhide-text (b e)
1776   "Remove hidden text properties from region between B and E."
1777   (remove-text-properties b e gnus-hidden-properties)
1778   (when (memq 'intangible gnus-hidden-properties)
1779     (put-text-property (max (1- b) (point-min))
1780                        b 'intangible nil)))
1781
1782 (defun gnus-article-hide-text-type (b e type)
1783   "Hide text of TYPE between B and E."
1784   (gnus-add-wash-type type)
1785   (gnus-article-hide-text
1786    b e (cons 'article-type (cons type gnus-hidden-properties))))
1787
1788 (defun gnus-article-unhide-text-type (b e type)
1789   "Unhide text of TYPE between B and E."
1790   (gnus-delete-wash-type type)
1791   (remove-text-properties
1792    b e (cons 'article-type (cons type gnus-hidden-properties)))
1793   (when (memq 'intangible gnus-hidden-properties)
1794     (put-text-property (max (1- b) (point-min))
1795                        b 'intangible nil)))
1796
1797 (defun gnus-article-hide-text-of-type (type)
1798   "Hide text of TYPE in the current buffer."
1799   (save-excursion
1800     (let ((b (point-min))
1801           (e (point-max)))
1802       (while (setq b (text-property-any b e 'article-type type))
1803         (add-text-properties b (incf b) gnus-hidden-properties)))))
1804
1805 (defun gnus-article-delete-text-of-type (type)
1806   "Delete text of TYPE in the current buffer."
1807   (save-excursion
1808     (let ((b (point-min)))
1809       (if (eq type 'multipart)
1810           ;; Remove MIME buttons associated with multipart/alternative parts.
1811           (progn
1812             (goto-char b)
1813             (while (if (get-text-property (point) 'gnus-part)
1814                        (setq b (point))
1815                      (when (setq b (next-single-property-change (point)
1816                                                                 'gnus-part))
1817                        (goto-char b)
1818                        t))
1819               (end-of-line)
1820               (skip-chars-forward "\n")
1821               (when (eq (get-text-property b 'article-type) 'multipart)
1822                 (delete-region b (point)))))
1823         (while (setq b (text-property-any b (point-max) 'article-type type))
1824           (delete-region
1825            b (or (text-property-not-all b (point-max) 'article-type type)
1826                  (point-max))))))))
1827
1828 (defun gnus-article-delete-invisible-text ()
1829   "Delete all invisible text in the current buffer."
1830   (save-excursion
1831     (let ((b (point-min)))
1832       (while (setq b (text-property-any b (point-max) 'invisible t))
1833         (delete-region
1834          b (or (text-property-not-all b (point-max) 'invisible t)
1835                (point-max)))))))
1836
1837 (defun gnus-article-text-type-exists-p (type)
1838   "Say whether any text of type TYPE exists in the buffer."
1839   (text-property-any (point-min) (point-max) 'article-type type))
1840
1841 (defsubst gnus-article-header-rank ()
1842   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1843   (let ((list gnus-sorted-header-list)
1844         (i 1))
1845     (while list
1846       (if (looking-at (car list))
1847           (setq list nil)
1848         (setq list (cdr list))
1849         (incf i)))
1850       i))
1851
1852 (defun article-hide-headers (&optional arg delete)
1853   "Hide unwanted headers and possibly sort them as well."
1854   (interactive)
1855   ;; This function might be inhibited.
1856   (unless gnus-inhibit-hiding
1857     (let ((inhibit-read-only t)
1858           (case-fold-search t)
1859           (max (1+ (length gnus-sorted-header-list)))
1860           (inhibit-point-motion-hooks t)
1861           (cur (current-buffer))
1862           ignored visible beg)
1863       (save-excursion
1864         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1865         ;; group parameters, so we should go to the summary buffer.
1866         (when (prog1
1867                   (condition-case nil
1868                       (progn (set-buffer gnus-summary-buffer) t)
1869                     (error nil))
1870                 (setq ignored (when (not gnus-visible-headers)
1871                                 (cond ((stringp gnus-ignored-headers)
1872                                        gnus-ignored-headers)
1873                                       ((listp gnus-ignored-headers)
1874                                        (mapconcat 'identity
1875                                                   gnus-ignored-headers
1876                                                   "\\|"))))
1877                       visible (cond ((stringp gnus-visible-headers)
1878                                      gnus-visible-headers)
1879                                     ((and gnus-visible-headers
1880                                           (listp gnus-visible-headers))
1881                                      (mapconcat 'identity
1882                                                 gnus-visible-headers
1883                                                 "\\|")))))
1884           (set-buffer cur))
1885         (save-restriction
1886           ;; First we narrow to just the headers.
1887           (article-narrow-to-head)
1888           ;; Hide any "From " lines at the beginning of (mail) articles.
1889           (while (looking-at "From ")
1890             (forward-line 1))
1891           (unless (bobp)
1892             (delete-region (point-min) (point)))
1893           ;; Then treat the rest of the header lines.
1894           ;; Then we use the two regular expressions
1895           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1896           ;; select which header lines is to remain visible in the
1897           ;; article buffer.
1898           (while (re-search-forward "^[^ \t:]*:" nil t)
1899             (beginning-of-line)
1900             ;; Mark the rank of the header.
1901             (put-text-property
1902              (point) (1+ (point)) 'message-rank
1903              (if (or (and visible (looking-at visible))
1904                      (and ignored
1905                           (not (looking-at ignored))))
1906                  (gnus-article-header-rank)
1907                (+ 2 max)))
1908             (forward-line 1))
1909           (message-sort-headers-1)
1910           (when (setq beg (text-property-any
1911                            (point-min) (point-max) 'message-rank (+ 2 max)))
1912             ;; We delete the unwanted headers.
1913             (gnus-add-wash-type 'headers)
1914             (add-text-properties (point-min) (+ 5 (point-min))
1915                                  '(article-type headers dummy-invisible t))
1916             (delete-region beg (point-max))))))))
1917
1918 (defun article-hide-boring-headers (&optional arg)
1919   "Toggle hiding of headers that aren't very interesting.
1920 If given a negative prefix, always show; if given a positive prefix,
1921 always hide."
1922   (interactive (gnus-article-hidden-arg))
1923   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1924              (not gnus-show-all-headers))
1925     (save-excursion
1926       (save-restriction
1927         (let ((inhibit-read-only t)
1928               (inhibit-point-motion-hooks t))
1929           (article-narrow-to-head)
1930           (dolist (elem gnus-boring-article-headers)
1931             (goto-char (point-min))
1932             (cond
1933              ;; Hide empty headers.
1934              ((eq elem 'empty)
1935               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1936                 (forward-line -1)
1937                 (gnus-article-hide-text-type
1938                  (point-at-bol)
1939                  (progn
1940                    (end-of-line)
1941                    (if (re-search-forward "^[^ \t]" nil t)
1942                        (match-beginning 0)
1943                      (point-max)))
1944                  'boring-headers)))
1945              ;; Hide boring Newsgroups header.
1946              ((eq elem 'newsgroups)
1947               (when (gnus-string-equal
1948                      (gnus-fetch-field "newsgroups")
1949                      (gnus-group-real-name
1950                       (if (boundp 'gnus-newsgroup-name)
1951                           gnus-newsgroup-name
1952                         "")))
1953                 (gnus-article-hide-header "newsgroups")))
1954              ((eq elem 'to-address)
1955               (let ((to (message-fetch-field "to"))
1956                     (to-address
1957                      (gnus-parameter-to-address
1958                       (if (boundp 'gnus-newsgroup-name)
1959                           gnus-newsgroup-name ""))))
1960                 (when (and to to-address
1961                            (ignore-errors
1962                              (gnus-string-equal
1963                               ;; only one address in To
1964                               (nth 1 (mail-extract-address-components to))
1965                               to-address)))
1966                   (gnus-article-hide-header "to"))))
1967              ((eq elem 'to-list)
1968               (let ((to (message-fetch-field "to"))
1969                     (to-list
1970                      (gnus-parameter-to-list
1971                       (if (boundp 'gnus-newsgroup-name)
1972                           gnus-newsgroup-name ""))))
1973                 (when (and to to-list
1974                            (ignore-errors
1975                              (gnus-string-equal
1976                               ;; only one address in To
1977                               (nth 1 (mail-extract-address-components to))
1978                               to-list)))
1979                   (gnus-article-hide-header "to"))))
1980              ((eq elem 'cc-list)
1981               (let ((cc (message-fetch-field "cc"))
1982                     (to-list
1983                      (gnus-parameter-to-list
1984                       (if (boundp 'gnus-newsgroup-name)
1985                           gnus-newsgroup-name ""))))
1986                 (when (and cc to-list
1987                            (ignore-errors
1988                              (gnus-string-equal
1989                               ;; only one address in CC
1990                               (nth 1 (mail-extract-address-components cc))
1991                               to-list)))
1992                   (gnus-article-hide-header "cc"))))
1993              ((eq elem 'followup-to)
1994               (when (gnus-string-equal
1995                      (message-fetch-field "followup-to")
1996                      (message-fetch-field "newsgroups"))
1997                 (gnus-article-hide-header "followup-to")))
1998              ((eq elem 'reply-to)
1999               (if (gnus-group-find-parameter
2000                    gnus-newsgroup-name 'broken-reply-to)
2001                   (gnus-article-hide-header "reply-to")
2002                 (let ((from (message-fetch-field "from"))
2003                       (reply-to (message-fetch-field "reply-to")))
2004                   (when
2005                       (and
2006                        from reply-to
2007                        (ignore-errors
2008                          (equal
2009                           (sort (mapcar
2010                                  (lambda (x) (downcase (cadr x)))
2011                                  (mail-extract-address-components from t))
2012                                 'string<)
2013                           (sort (mapcar
2014                                  (lambda (x) (downcase (cadr x)))
2015                                  (mail-extract-address-components reply-to t))
2016                                 'string<))))
2017                     (gnus-article-hide-header "reply-to")))))
2018              ((eq elem 'date)
2019               (let ((date (with-current-buffer gnus-original-article-buffer
2020                             ;; If date in `gnus-article-buffer' is localized
2021                             ;; (`gnus-treat-date-user-defined'),
2022                             ;; `days-between' might fail.
2023                             (message-fetch-field "date"))))
2024                 (when (and date
2025                            (< (days-between (current-time-string) date)
2026                               4))
2027                   (gnus-article-hide-header "date"))))
2028              ((eq elem 'long-to)
2029               (let ((to (message-fetch-field "to"))
2030                     (cc (message-fetch-field "cc")))
2031                 (when (> (length to) 1024)
2032                   (gnus-article-hide-header "to"))
2033                 (when (> (length cc) 1024)
2034                   (gnus-article-hide-header "cc"))))
2035              ((eq elem 'many-to)
2036               (let ((to-count 0)
2037                     (cc-count 0))
2038                 (goto-char (point-min))
2039                 (while (re-search-forward "^to:" nil t)
2040                   (setq to-count (1+ to-count)))
2041                 (when (> to-count 1)
2042                   (while (> to-count 0)
2043                     (goto-char (point-min))
2044                     (save-restriction
2045                       (re-search-forward "^to:" nil nil to-count)
2046                       (forward-line -1)
2047                       (narrow-to-region (point) (point-max))
2048                       (gnus-article-hide-header "to"))
2049                     (setq to-count (1- to-count))))
2050                 (goto-char (point-min))
2051                 (while (re-search-forward "^cc:" nil t)
2052                   (setq cc-count (1+ cc-count)))
2053                 (when (> cc-count 1)
2054                   (while (> cc-count 0)
2055                     (goto-char (point-min))
2056                     (save-restriction
2057                       (re-search-forward "^cc:" nil nil cc-count)
2058                       (forward-line -1)
2059                       (narrow-to-region (point) (point-max))
2060                       (gnus-article-hide-header "cc"))
2061                     (setq cc-count (1- cc-count)))))))))))))
2062
2063 (defun gnus-article-hide-header (header)
2064   (save-excursion
2065     (goto-char (point-min))
2066     (when (re-search-forward (concat "^" header ":") nil t)
2067       (gnus-article-hide-text-type
2068        (point-at-bol)
2069        (progn
2070          (end-of-line)
2071          (if (re-search-forward "^[^ \t]" nil t)
2072              (match-beginning 0)
2073            (point-max)))
2074        'boring-headers))))
2075
2076 (defvar gnus-article-normalized-header-length 40
2077   "Length of normalized headers.")
2078
2079 (defun article-normalize-headers ()
2080   "Make all header lines 40 characters long."
2081   (interactive)
2082   (let ((inhibit-read-only t)
2083         column)
2084     (save-excursion
2085       (save-restriction
2086         (article-narrow-to-head)
2087         (while (not (eobp))
2088           (cond
2089            ((< (setq column (- (point-at-eol) (point)))
2090                gnus-article-normalized-header-length)
2091             (end-of-line)
2092             (insert (make-string
2093                      (- gnus-article-normalized-header-length column)
2094                      ? )))
2095            ((> column gnus-article-normalized-header-length)
2096             (gnus-put-text-property
2097              (progn
2098                (forward-char gnus-article-normalized-header-length)
2099                (point))
2100              (point-at-eol)
2101              'invisible t))
2102            (t
2103             ;; Do nothing.
2104             ))
2105           (forward-line 1))))))
2106
2107 (defun article-treat-dumbquotes ()
2108   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2109 Note that this function guesses whether a character is a sm*rtq**t* or
2110 not, so it should only be used interactively.
2111
2112 Sm*rtq**t*s are M****s***'s unilateral extension to the
2113 iso-8859-1 character map in an attempt to provide more quoting
2114 characters.  If you see something like \\222 or \\264 where
2115 you're expecting some kind of apostrophe or quotation mark, then
2116 try this wash."
2117   (interactive)
2118   (article-translate-strings gnus-article-dumbquotes-map))
2119
2120 (defvar org-entities)
2121
2122 (defun article-treat-non-ascii ()
2123   "Translate many Unicode characters into their ASCII equivalents."
2124   (interactive)
2125   (require 'org-entities)
2126   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2127     (dolist (elem org-entities)
2128       (when (and (listp elem)
2129                  (= (length (nth 6 elem)) 1))
2130         (if (featurep 'xemacs)
2131             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2132           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2133     (save-excursion
2134       (when (article-goto-body)
2135         (let ((inhibit-read-only t)
2136               replace props)
2137           (while (not (eobp))
2138             (if (not (setq replace (if (featurep 'xemacs)
2139                                        (get-char-table (following-char) table)
2140                                      (aref table (following-char)))))
2141                 (forward-char 1)
2142               (if (prog1
2143                       (setq props (text-properties-at (point)))
2144                     (delete-char 1))
2145                   (add-text-properties (point) (progn (insert replace) (point))
2146                                        props)
2147                 (insert replace)))))))))
2148
2149 (defun article-translate-characters (from to)
2150   "Translate all characters in the body of the article according to FROM and TO.
2151 FROM is a string of characters to translate from; to is a string of
2152 characters to translate to."
2153   (save-excursion
2154     (when (article-goto-body)
2155       (let ((inhibit-read-only t)
2156             (x (make-string 225 ?x))
2157             (i -1))
2158         (while (< (incf i) (length x))
2159           (aset x i i))
2160         (setq i 0)
2161         (while (< i (length from))
2162           (aset x (aref from i) (aref to i))
2163           (incf i))
2164         (translate-region (point) (point-max) x)))))
2165
2166 (defun article-translate-strings (map)
2167   "Translate all string in the body of the article according to MAP.
2168 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2169   (save-excursion
2170     (when (article-goto-body)
2171       (let ((inhibit-read-only t))
2172         (dolist (elem map)
2173           (let ((from (car elem))
2174                 (to (cadr elem)))
2175             (save-excursion
2176               (if (stringp from)
2177                   (while (search-forward from nil t)
2178                     (replace-match to))
2179                 (while (not (eobp))
2180                   (if (eq (following-char) from)
2181                       (progn
2182                         (delete-char 1)
2183                         (insert to))
2184                     (forward-char 1)))))))))))
2185
2186 (defun article-treat-overstrike ()
2187   "Translate overstrikes into bold text."
2188   (interactive)
2189   (save-excursion
2190     (when (article-goto-body)
2191       (let ((inhibit-read-only t))
2192         (while (search-forward "\b" nil t)
2193           (let ((next (char-after))
2194                 (previous (char-after (- (point) 2))))
2195             ;; We do the boldification/underlining by hiding the
2196             ;; overstrikes and putting the proper text property
2197             ;; on the letters.
2198             (cond
2199              ((eq next previous)
2200               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2201               (put-text-property (point) (1+ (point)) 'face 'bold))
2202              ((eq next ?_)
2203               (gnus-article-hide-text-type
2204                (1- (point)) (1+ (point)) 'overstrike)
2205               (put-text-property
2206                (- (point) 2) (1- (point)) 'face 'underline))
2207              ((eq previous ?_)
2208               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2209               (put-text-property
2210                (point) (1+ (point)) 'face 'underline)))))))))
2211
2212 (defun article-treat-ansi-sequences ()
2213   "Translate ANSI SGR control sequences into overlays or extents."
2214   (interactive)
2215   (save-excursion
2216     (when (article-goto-body)
2217       (let ((inhibit-read-only t))
2218         (ansi-color-apply-on-region (point) (point-max))))))
2219
2220 (defun gnus-article-treat-unfold-headers ()
2221   "Unfold folded message headers.
2222 Only the headers that fit into the current window width will be
2223 unfolded."
2224   (interactive)
2225   (gnus-with-article-headers
2226     (let (length)
2227       (while (not (eobp))
2228         (save-restriction
2229           (mail-header-narrow-to-field)
2230           (let* ((header (buffer-string))
2231                  (unfoldable
2232                   (or (equal gnus-article-unfold-long-headers t)
2233                       (and (stringp gnus-article-unfold-long-headers)
2234                            (string-match gnus-article-unfold-long-headers
2235                                          header)))))
2236             (with-temp-buffer
2237               (insert header)
2238               (goto-char (point-min))
2239               (while (re-search-forward "\n[\t ]" nil t)
2240                 (replace-match " " t t)))
2241             (setq length (- (point-max) (point-min) 1))
2242             (when (or unfoldable
2243                       (< length (window-width)))
2244               (while (re-search-forward "\n[\t ]" nil t)
2245                 (replace-match " " t t))))
2246           (goto-char (point-max)))))))
2247
2248 (defun gnus-article-treat-fold-headers ()
2249   "Fold message headers."
2250   (interactive)
2251   (gnus-with-article-headers
2252     (while (not (eobp))
2253       (save-restriction
2254         (mail-header-narrow-to-field)
2255         (mail-header-fold-field)
2256         (goto-char (point-max))))))
2257
2258 (defun gnus-treat-smiley ()
2259   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2260   (interactive)
2261   (gnus-with-article-buffer
2262     (if (memq 'smiley gnus-article-wash-types)
2263         (gnus-delete-images 'smiley)
2264       (article-goto-body)
2265       (let ((images (smiley-region (point) (point-max))))
2266         (when images
2267           (gnus-add-wash-type 'smiley)
2268           (dolist (image images)
2269             (gnus-add-image 'smiley image)))))))
2270
2271 (defun gnus-article-remove-images ()
2272   "Remove all images from the article buffer."
2273   (interactive)
2274   (gnus-with-article-buffer
2275     (save-restriction
2276       (widen)
2277       (dolist (elem gnus-article-image-alist)
2278         (gnus-delete-images (car elem))))))
2279
2280 (autoload 'w3m-toggle-inline-images "w3m")
2281
2282 (defun gnus-article-show-images ()
2283   "Show any images that are in the HTML-rendered article buffer.
2284 This only works if the article in question is HTML."
2285   (interactive)
2286   (gnus-with-article-buffer
2287     (save-restriction
2288       (widen)
2289       (if (eq mm-text-html-renderer 'w3m)
2290           (let ((mm-inline-text-html-with-images nil))
2291             (w3m-toggle-inline-images))
2292         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2293                                                         'image-displayer))
2294           (destructuring-bind (start end function) region
2295             (funcall function (get-text-property start 'image-url)
2296                      start end)))))))
2297
2298 (defun gnus-article-treat-fold-newsgroups ()
2299   "Unfold folded message headers.
2300 Only the headers that fit into the current window width will be
2301 unfolded."
2302   (interactive)
2303   (gnus-with-article-headers
2304     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2305       (save-restriction
2306         (mail-header-narrow-to-field)
2307         (while (re-search-forward ", *" nil t)
2308           (replace-match ", " t t))
2309         (mail-header-fold-field)
2310         (goto-char (point-max))))))
2311
2312 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2313   "Value of `truncate-lines' in Gnus Article buffer.
2314 Valid values are nil, t, `head', `first', `last', an integer or a
2315 predicate.  See Info node `(gnus)Customizing Articles'."
2316   :version "23.1" ;; No Gnus
2317   :group 'gnus-article
2318   ;; :link '(custom-manual "(gnus)Customizing Articles")
2319   :type 'boolean)
2320
2321 (defun gnus-article-toggle-truncate-lines (&optional arg)
2322   "Toggle whether to fold or truncate long lines in article the buffer.
2323 If ARG is non-nil and not a number, toggle
2324 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2325 long lines if and only if arg is positive."
2326   (interactive "P")
2327   (cond
2328    ((and (numberp arg) (> arg 0))
2329     (setq gnus-article-truncate-lines t))
2330    ((numberp arg)
2331     (setq gnus-article-truncate-lines nil))
2332    (arg
2333     (setq gnus-article-truncate-lines
2334           (not gnus-article-truncate-lines))))
2335   (gnus-with-article-buffer
2336     (cond
2337      ((and (numberp arg) (> arg 0))
2338       (setq truncate-lines nil))
2339      ((numberp arg)
2340       (setq truncate-lines t)))
2341     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2342     ;; `toggle-truncate-lines' needs an argument.
2343     (toggle-truncate-lines)))
2344
2345 (defun gnus-article-treat-body-boundary ()
2346   "Place a boundary line at the end of the headers."
2347   (interactive)
2348   (when (and gnus-body-boundary-delimiter
2349              (> (length gnus-body-boundary-delimiter) 0))
2350     (gnus-with-article-headers
2351       (goto-char (point-max))
2352       (let ((start (point)))
2353         (insert "X-Boundary: ")
2354         (gnus-add-text-properties start (point) '(invisible t intangible t))
2355        (insert (let (str (max (window-width)))
2356                  (if (featurep 'xemacs)
2357                      (setq max (1- max)))
2358                  (while (>= max (length str))
2359                     (setq str (concat str gnus-body-boundary-delimiter)))
2360                  (substring str 0 max))
2361                 "\n")
2362         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2363
2364 (defun article-fill-long-lines ()
2365   "Fill lines that are wider than the window width."
2366   (interactive)
2367   (save-excursion
2368     (let ((inhibit-read-only t)
2369           (width (window-width (get-buffer-window (current-buffer)))))
2370       (save-restriction
2371         (article-goto-body)
2372         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2373           (while (not (eobp))
2374             (end-of-line)
2375             (when (>= (current-column) (min fill-column width))
2376               (narrow-to-region (min (1+ (point)) (point-max))
2377                                 (point-at-bol))
2378               (let ((goback (point-marker)))
2379                 (fill-paragraph nil)
2380                 (goto-char (marker-position goback)))
2381               (widen))
2382             (forward-line 1)))))))
2383
2384 (defun article-capitalize-sentences ()
2385   "Capitalize the first word in each sentence."
2386   (interactive)
2387   (save-excursion
2388     (let ((inhibit-read-only t)
2389           (paragraph-start "^[\n\^L]"))
2390       (article-goto-body)
2391       (while (not (eobp))
2392         (capitalize-word 1)
2393         (forward-sentence)))))
2394
2395 (defun article-remove-cr ()
2396   "Remove trailing CRs and then translate remaining CRs into LFs."
2397   (interactive)
2398   (save-excursion
2399     (let ((inhibit-read-only t))
2400       (goto-char (point-min))
2401       (while (re-search-forward "\r+$" nil t)
2402         (replace-match "" t t))
2403       (goto-char (point-min))
2404       (while (search-forward "\r" nil t)
2405         (replace-match "\n" t t)))))
2406
2407 (defun article-remove-trailing-blank-lines ()
2408   "Remove all trailing blank lines from the article."
2409   (interactive)
2410   (save-excursion
2411     (let ((inhibit-read-only t))
2412       (goto-char (point-max))
2413       (delete-region
2414        (point)
2415        (progn
2416          (while (and (not (bobp))
2417                      (looking-at "^[ \t]*$")
2418                      (not (gnus-annotation-in-region-p
2419                            (point) (point-at-eol))))
2420            (forward-line -1))
2421          (forward-line 1)
2422          (point))))))
2423
2424 (defvar gnus-face-properties-alist)
2425
2426 (defun article-display-face (&optional force)
2427   "Display any Face headers in the header."
2428   (interactive (list 'force))
2429   (let ((wash-face-p buffer-read-only))
2430     (gnus-with-article-headers
2431       ;; When displaying parts, this function can be called several times on
2432       ;; the same article, without any intended toggle semantic (as typing `W
2433       ;; D d' would have). So face deletion must occur only when we come from
2434       ;; an interactive command, that is when the *Article* buffer is
2435       ;; read-only.
2436       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2437           (gnus-delete-images 'face)
2438         (let ((from (message-fetch-field "from"))
2439               face faces)
2440           (save-current-buffer
2441             (when (and wash-face-p
2442                        (gnus-buffer-live-p gnus-original-article-buffer)
2443                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2444               (set-buffer gnus-original-article-buffer))
2445             (save-restriction
2446               (mail-narrow-to-head)
2447               (when (or force
2448                         ;; Check whether this face is censored.
2449                         (not (and gnus-article-x-face-too-ugly
2450                                   (or from
2451                                       (setq from (message-fetch-field "from")))
2452                                   (string-match gnus-article-x-face-too-ugly
2453                                                 from))))
2454                 (while (gnus-article-goto-header "Face")
2455                   (push (mail-header-field-value) faces)))))
2456           (when faces
2457             (goto-char (point-min))
2458             (let (png image)
2459               (unless (setq from (gnus-article-goto-header "from"))
2460                 (insert "From:")
2461                 (setq from (point))
2462                 (insert " [no `from' set]\n"))
2463               (while faces
2464                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2465                   (setq image
2466                         (apply 'gnus-create-image png 'png t
2467                                (cdr (assq 'png gnus-face-properties-alist))))
2468                   (goto-char from)
2469                   (gnus-add-wash-type 'face)
2470                   (gnus-add-image 'face image)
2471                   (gnus-put-image image nil 'face))))))))))
2472
2473 (defun article-display-x-face (&optional force)
2474   "Look for an X-Face header and display it if present."
2475   (interactive (list 'force))
2476   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2477     (gnus-with-article-headers
2478       ;; Delete the old process, if any.
2479       (when (process-status "article-x-face")
2480         (delete-process "article-x-face"))
2481       ;; See the comment in `article-display-face'.
2482       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2483           ;; We have already displayed X-Faces, so we remove them
2484           ;; instead.
2485           (gnus-delete-images 'xface)
2486         ;; Display X-Faces.
2487         (let ((from (message-fetch-field "from"))
2488               x-faces face)
2489           (save-current-buffer
2490             (when (and wash-face-p
2491                        (gnus-buffer-live-p gnus-original-article-buffer)
2492                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2493               ;; If type `W f', use gnus-original-article-buffer,
2494               ;; otherwise use the current buffer because displaying
2495               ;; RFC822 parts calls this function too.
2496               (set-buffer gnus-original-article-buffer))
2497             (save-restriction
2498               (mail-narrow-to-head)
2499               (and gnus-article-x-face-command
2500                    (or force
2501                        ;; Check whether this face is censored.
2502                        (not (and gnus-article-x-face-too-ugly
2503                                  (or from
2504                                      (setq from (message-fetch-field "from")))
2505                                  (string-match gnus-article-x-face-too-ugly
2506                                                from))))
2507                    (while (gnus-article-goto-header "X-Face")
2508                      (push (mail-header-field-value) x-faces)))))
2509           (when x-faces
2510             ;; We display the face.
2511             (cond ((functionp gnus-article-x-face-command)
2512                    ;; The command is a lisp function, so we call it.
2513                    (mapc gnus-article-x-face-command x-faces))
2514                   ((stringp gnus-article-x-face-command)
2515                    ;; The command is a string, so we interpret the command
2516                    ;; as a, well, command, and fork it off.
2517                    (let ((process-connection-type nil))
2518                      (gnus-set-process-query-on-exit-flag
2519                       (start-process
2520                        "article-x-face" nil shell-file-name
2521                        shell-command-switch gnus-article-x-face-command)
2522                       nil)
2523                      ;; Sending multiple EOFs to xv doesn't work,
2524                      ;; so we only do a single external face.
2525                      (with-temp-buffer
2526                        (insert (car x-faces))
2527                        (process-send-region "article-x-face"
2528                                             (point-min) (point-max)))
2529                      (process-send-eof "article-x-face")))
2530                   (t
2531                    (error "`%s' set to `%s' is not a function"
2532                           gnus-article-x-face-command
2533                           'gnus-article-x-face-command)))))))))
2534
2535 (defun article-decode-mime-words ()
2536   "Decode all MIME-encoded words in the article."
2537   (interactive)
2538   (gnus-with-article-buffer
2539     (let ((inhibit-point-motion-hooks t)
2540           (mail-parse-charset gnus-newsgroup-charset)
2541           (mail-parse-ignored-charsets
2542            (with-current-buffer gnus-summary-buffer
2543              gnus-newsgroup-ignored-charsets)))
2544       (mail-decode-encoded-word-region (point-min) (point-max)))))
2545
2546 (defun article-decode-charset (&optional prompt)
2547   "Decode charset-encoded text in the article.
2548 If PROMPT (the prefix), prompt for a coding system to use."
2549   (interactive "P")
2550   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2551         (inhibit-read-only t)
2552         (mail-parse-charset gnus-newsgroup-charset)
2553         (mail-parse-ignored-charsets
2554          (save-excursion (condition-case nil
2555                              (set-buffer gnus-summary-buffer)
2556                            (error))
2557                          gnus-newsgroup-ignored-charsets))
2558         ct cte ctl charset format)
2559     (save-excursion
2560       (save-restriction
2561         (article-narrow-to-head)
2562         (setq ct (message-fetch-field "Content-Type" t)
2563               cte (message-fetch-field "Content-Transfer-Encoding" t)
2564               ctl (and ct (mail-header-parse-content-type ct))
2565               charset (cond
2566                        (prompt
2567                         (mm-read-coding-system "Charset to decode: "))
2568                        (ctl
2569                         (mail-content-type-get ctl 'charset)))
2570               format (and ctl (mail-content-type-get ctl 'format)))
2571         (when cte
2572           (setq cte (mail-header-strip cte)))
2573         (if (and ctl (not (string-match "/" (car ctl))))
2574             (setq ctl nil))
2575         (goto-char (point-max)))
2576       (forward-line 1)
2577       (save-restriction
2578         (narrow-to-region (point) (point-max))
2579         (when (and (eq mail-parse-charset 'gnus-decoded)
2580                    (eq (mm-body-7-or-8) '8bit))
2581           ;; The text code could have been decoded.
2582           (setq charset mail-parse-charset))
2583         (when (and (or (not ctl)
2584                        (equal (car ctl) "text/plain"))
2585                    (not format)) ;; article with format will decode later.
2586           (mm-decode-body
2587            charset (and cte (intern (downcase
2588                                      (gnus-strip-whitespace cte))))
2589            (car ctl)))))))
2590
2591 (defun article-decode-encoded-words ()
2592   "Remove encoded-word encoding from headers."
2593   (let ((inhibit-point-motion-hooks t)
2594         (mail-parse-charset gnus-newsgroup-charset)
2595         (mail-parse-ignored-charsets
2596          (save-excursion (condition-case nil
2597                              (set-buffer gnus-summary-buffer)
2598                            (error))
2599                          gnus-newsgroup-ignored-charsets))
2600         (inhibit-read-only t)
2601         end start)
2602     (goto-char (point-min))
2603     (when (search-forward "\n\n" nil 'move)
2604       (forward-line -1))
2605     (setq end (point))
2606     (while (not (bobp))
2607       (while (progn
2608                (forward-line -1)
2609                (and (not (bobp))
2610                     (memq (char-after) '(?\t ? )))))
2611       (setq start (point))
2612       (if (looking-at "\
2613 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2614 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2615           (funcall gnus-decode-address-function start end)
2616         (funcall gnus-decode-header-function start end))
2617       (goto-char (setq end start)))))
2618
2619 (defun article-decode-group-name ()
2620   "Decode group names in Newsgroups, Followup-To and Xref headers."
2621   (let ((inhibit-point-motion-hooks t)
2622         (inhibit-read-only t)
2623         (method (gnus-find-method-for-group gnus-newsgroup-name))
2624         regexp)
2625     (when (and (or gnus-group-name-charset-method-alist
2626                    gnus-group-name-charset-group-alist)
2627                (gnus-buffer-live-p gnus-original-article-buffer))
2628       (save-restriction
2629         (article-narrow-to-head)
2630         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2631           (with-current-buffer gnus-original-article-buffer
2632             (goto-char (point-min)))
2633           (setq regexp (concat "^" header
2634                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2635           (while (re-search-forward regexp nil t)
2636             (replace-match (save-match-data
2637                              (gnus-decode-newsgroups
2638                               ;; XXX how to use data in article buffer?
2639                               (with-current-buffer gnus-original-article-buffer
2640                                 (re-search-forward regexp nil t)
2641                                 (match-string 1))
2642                               gnus-newsgroup-name method))
2643                            t t nil 1))
2644           (goto-char (point-min)))))))
2645
2646 (autoload 'idna-to-unicode "idna")
2647
2648 (defun article-decode-idna-rhs ()
2649   "Decode IDNA strings in RHS in various headers in current buffer.
2650 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2651 Mail-Reply-To: and Mail-Followup-To:."
2652   (when gnus-use-idna
2653     (save-restriction
2654       (let ((inhibit-point-motion-hooks t)
2655             (inhibit-read-only t))
2656         (article-narrow-to-head)
2657         (goto-char (point-min))
2658         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2659           (let (ace unicode)
2660             (when (save-match-data
2661                     (and (setq ace (match-string 1))
2662                          (save-excursion
2663                            (and (re-search-backward "^[^ \t]" nil t)
2664                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2665                          (setq unicode (idna-to-unicode ace))))
2666               (unless (string= ace unicode)
2667                 (replace-match unicode nil nil nil 1)))))))))
2668
2669 (defun article-de-quoted-unreadable (&optional force read-charset)
2670   "Translate a quoted-printable-encoded article.
2671 If FORCE, decode the article whether it is marked as quoted-printable
2672 or not.
2673 If READ-CHARSET, ask for a coding system."
2674   (interactive (list 'force current-prefix-arg))
2675   (save-excursion
2676     (let ((inhibit-read-only t) type charset)
2677       (if (gnus-buffer-live-p gnus-original-article-buffer)
2678           (with-current-buffer gnus-original-article-buffer
2679             (setq type
2680                   (gnus-fetch-field "content-transfer-encoding"))
2681             (let* ((ct (gnus-fetch-field "content-type"))
2682                    (ctl (and ct (mail-header-parse-content-type ct))))
2683               (setq charset (and ctl
2684                                  (mail-content-type-get ctl 'charset)))
2685               (if (stringp charset)
2686                   (setq charset (intern (downcase charset)))))))
2687       (if read-charset
2688           (setq charset (mm-read-coding-system "Charset: " charset)))
2689       (unless charset
2690         (setq charset gnus-newsgroup-charset))
2691       (when (or force
2692                 (and type (let ((case-fold-search t))
2693                             (string-match "quoted-printable" type))))
2694         (article-goto-body)
2695         (quoted-printable-decode-region
2696          (point) (point-max) (mm-charset-to-coding-system charset))))))
2697
2698 (defun article-de-base64-unreadable (&optional force read-charset)
2699   "Translate a base64 article.
2700 If FORCE, decode the article whether it is marked as base64 not.
2701 If READ-CHARSET, ask for a coding system."
2702   (interactive (list 'force current-prefix-arg))
2703   (save-excursion
2704     (let ((inhibit-read-only t) type charset)
2705       (if (gnus-buffer-live-p gnus-original-article-buffer)
2706           (with-current-buffer gnus-original-article-buffer
2707             (setq type
2708                   (gnus-fetch-field "content-transfer-encoding"))
2709             (let* ((ct (gnus-fetch-field "content-type"))
2710                    (ctl (and ct (mail-header-parse-content-type ct))))
2711               (setq charset (and ctl
2712                                  (mail-content-type-get ctl 'charset)))
2713               (if (stringp charset)
2714                   (setq charset (intern (downcase charset)))))))
2715       (if read-charset
2716           (setq charset (mm-read-coding-system "Charset: " charset)))
2717       (unless charset
2718         (setq charset gnus-newsgroup-charset))
2719       (when (or force
2720                 (and type (let ((case-fold-search t))
2721                             (string-match "base64" type))))
2722         (article-goto-body)
2723         (save-restriction
2724           (narrow-to-region (point) (point-max))
2725           (base64-decode-region (point-min) (point-max))
2726           (mm-decode-coding-region
2727            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2728
2729 (eval-when-compile
2730   (require 'rfc1843))
2731
2732 (defun article-decode-HZ ()
2733   "Translate a HZ-encoded article."
2734   (interactive)
2735   (require 'rfc1843)
2736   (save-excursion
2737     (let ((inhibit-read-only t))
2738       (rfc1843-decode-region (point-min) (point-max)))))
2739
2740 (defun article-unsplit-urls ()
2741   "Remove the newlines that some other mailers insert into URLs."
2742   (interactive)
2743   (save-excursion
2744     (let ((inhibit-read-only t))
2745       (goto-char (point-min))
2746       (while (re-search-forward
2747               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2748         (replace-match "\\1\\3" t)))
2749     (when (interactive-p)
2750       (gnus-treat-article nil))))
2751
2752 (defun article-wash-html ()
2753   "Format an HTML article."
2754   (interactive)
2755   (let ((handles nil)
2756         (buffer-read-only nil))
2757     (when (gnus-buffer-live-p gnus-original-article-buffer)
2758       (setq handles (mm-dissect-buffer t t)))
2759     (article-goto-body)
2760     (delete-region (point) (point-max))
2761     (mm-enable-multibyte)
2762     (mm-inline-text-html handles)))
2763
2764 (defvar gnus-article-browse-html-temp-list nil
2765   "List of temporary files created by `gnus-article-browse-html-parts'.
2766 Internal variable.")
2767
2768 (defcustom gnus-article-browse-delete-temp 'ask
2769   "What to do with temporary files from `gnus-article-browse-html-parts'.
2770 If nil, don't delete temporary files.  If it is t, delete them on
2771 exit from the summary buffer.  If it is the symbol `file', query
2772 on each file, if it is `ask' ask once when exiting from the
2773 summary buffer."
2774   :group 'gnus-article
2775   :version "23.1" ;; No Gnus
2776   :type '(choice (const :tag "Don't delete" nil)
2777                  (const :tag "Don't ask" t)
2778                  (const :tag "Ask" ask)
2779                  (const :tag "Ask for each file" file)))
2780
2781 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2782
2783 (defun gnus-article-browse-delete-temp-files (&optional how)
2784   "Delete temp-files created by `gnus-article-browse-html-parts'."
2785   (when (and gnus-article-browse-html-temp-list
2786              (progn
2787                (or how (setq how gnus-article-browse-delete-temp))
2788                (if (eq how 'ask)
2789                    (let ((files (length gnus-article-browse-html-temp-list)))
2790                      (gnus-y-or-n-p
2791                       (if (= files 1)
2792                           "Delete the temporary HTML file? "
2793                         (format "Delete all %s temporary HTML files? "
2794                                 files))))
2795                  how)))
2796     (dolist (file gnus-article-browse-html-temp-list)
2797       (cond ((file-directory-p file)
2798              (when (or (not (eq how 'file))
2799                        (gnus-y-or-n-p
2800                         (format
2801                          "Delete temporary HTML file(s) in directory `%s'? "
2802                          (file-name-as-directory file))))
2803                (gnus-delete-directory file)))
2804             ((file-exists-p file)
2805              (when (or (not (eq how 'file))
2806                        (gnus-y-or-n-p
2807                         (format "Delete temporary HTML file `%s'? " file)))
2808                (delete-file file)))))
2809     ;; Also remove file from the list when not deleted or if file doesn't
2810     ;; exist anymore.
2811     (setq gnus-article-browse-html-temp-list nil))
2812   gnus-article-browse-html-temp-list)
2813
2814 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2815   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2816 Return file name."
2817   (save-match-data
2818     (let (file type)
2819       (catch 'found
2820         (dolist (handle handles)
2821           (cond
2822            ((not (listp handle)))
2823            ((equal (mm-handle-media-supertype handle) "multipart")
2824             (when (setq file (gnus-article-browse-html-save-cid-content
2825                               cid handle directory))
2826               (throw 'found file)))
2827            ((equal (concat "<" cid ">") (mm-handle-id handle))
2828             (setq file
2829                   (expand-file-name
2830                    (or (mm-handle-filename handle)
2831                        (concat
2832                         (make-temp-name "cid")
2833                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2834                    directory))
2835             (mm-save-part-to-file handle file)
2836             (throw 'found file))))))))
2837
2838 (defun gnus-article-browse-html-parts (list &optional header)
2839   "View all \"text/html\" parts from LIST.
2840 Recurse into multiparts.  The optional HEADER that should be a decoded
2841 message header will be added to the bodies of the \"text/html\" parts."
2842   ;; Internal function used by `gnus-article-browse-html-article'.
2843   (let (type file charset content cid-dir tmp-file showed)
2844     ;; Find and show the html-parts.
2845     (dolist (handle list)
2846       ;; If HTML, show it:
2847       (cond ((not (listp handle)))
2848             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2849                  (and (equal (car type) "message/external-body")
2850                       (or header
2851                           (setq file (mm-handle-filename handle)))
2852                       (or (mm-handle-cache handle)
2853                           (condition-case code
2854                               (progn (mm-extern-cache-contents handle) t)
2855                             (error
2856                              (gnus-message 3 "%s" (error-message-string code))
2857                              (when (>= gnus-verbose 3) (sit-for 2))
2858                              nil)))
2859                       (progn
2860                         (setq handle (mm-handle-cache handle)
2861                               type (mm-handle-type handle))
2862                         (equal (car type) "text/html"))))
2863              (setq charset (mail-content-type-get type 'charset)
2864                    content (mm-get-part handle))
2865              (with-temp-buffer
2866                (if (eq charset 'gnus-decoded)
2867                    (mm-enable-multibyte)
2868                  (mm-disable-multibyte))
2869                (insert content)
2870                ;; resolve cid contents
2871                (let ((case-fold-search t)
2872                      cid-file)
2873                  (goto-char (point-min))
2874                  (while (re-search-forward "\
2875 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2876                                            nil t)
2877                    (unless cid-dir
2878                      (setq cid-dir (mm-make-temp-file "cid" t))
2879                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2880                    (setq file nil
2881                          content nil)
2882                    (when (setq cid-file
2883                                (gnus-article-browse-html-save-cid-content
2884                                 (match-string 2)
2885                                 (with-current-buffer gnus-article-buffer
2886                                   gnus-article-mime-handles)
2887                                 cid-dir))
2888                      (when (eq system-type 'cygwin)
2889                        (setq cid-file
2890                              (concat "/" (substring
2891                                           (with-output-to-string
2892                                             (call-process "cygpath" nil
2893                                                           standard-output
2894                                                           nil "-m" cid-file))
2895                                           0 -1))))
2896                      (replace-match (concat "file://" cid-file)
2897                                     nil nil nil 1))))
2898                (unless content (setq content (buffer-string))))
2899              (when (or charset header (not file))
2900                (setq tmp-file (mm-make-temp-file
2901                                ;; Do we need to care for 8.3 filenames?
2902                                "mm-" nil ".html")))
2903              ;; Add a meta html tag to specify charset and a header.
2904              (cond
2905               (header
2906                (let (title eheader body hcharset coding force-charset)
2907                  (with-temp-buffer
2908                    (mm-enable-multibyte)
2909                    (setq case-fold-search t)
2910                    (insert header "\n")
2911                    (setq title (message-fetch-field "subject"))
2912                    (goto-char (point-min))
2913                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2914                      (replace-match (cond ((match-beginning 1) "&lt;")
2915                                           ((match-beginning 2) "&gt;")
2916                                           (t "&amp;"))))
2917                    (goto-char (point-min))
2918                    (insert "<pre>\n")
2919                    (goto-char (point-max))
2920                    (insert "</pre>\n<hr>\n")
2921                    ;; We have to examine charset one by one since
2922                    ;; charset specified in parts might be different.
2923                    (if (eq charset 'gnus-decoded)
2924                        (setq charset 'utf-8
2925                              eheader (mm-encode-coding-string (buffer-string)
2926                                                               charset)
2927                              title (when title
2928                                      (mm-encode-coding-string title charset))
2929                              body (mm-encode-coding-string content charset)
2930                              force-charset t)
2931                      (setq hcharset (mm-find-mime-charset-region (point-min)
2932                                                                  (point-max)))
2933                      (cond ((= (length hcharset) 1)
2934                             (setq hcharset (car hcharset)
2935                                   coding (mm-charset-to-coding-system
2936                                           hcharset)))
2937                            ((> (length hcharset) 1)
2938                             (setq hcharset 'utf-8
2939                                   coding hcharset)))
2940                      (if coding
2941                          (if charset
2942                              (progn
2943                                (setq body
2944                                      (mm-charset-to-coding-system charset))
2945                                (if (eq coding body)
2946                                    (setq eheader (mm-encode-coding-string
2947                                                   (buffer-string) coding)
2948                                          title (when title
2949                                                  (mm-encode-coding-string
2950                                                   title coding))
2951                                          body content)
2952                                  (setq charset 'utf-8
2953                                        eheader (mm-encode-coding-string
2954                                                 (buffer-string) charset)
2955                                        title (when title
2956                                                (mm-encode-coding-string
2957                                                 title charset))
2958                                        body (mm-encode-coding-string
2959                                              (mm-decode-coding-string
2960                                               content body)
2961                                              charset)
2962                                        force-charset t)))
2963                            (setq charset hcharset
2964                                  eheader (mm-encode-coding-string
2965                                           (buffer-string) coding)
2966                                  title (when title
2967                                          (mm-encode-coding-string
2968                                           title coding))
2969                                  body content))
2970                        (setq eheader (mm-string-as-unibyte (buffer-string))
2971                              body content)))
2972                    (erase-buffer)
2973                    (mm-disable-multibyte)
2974                    (insert body)
2975                    (when charset
2976                      (mm-add-meta-html-tag handle charset force-charset))
2977                    (when title
2978                      (goto-char (point-min))
2979                      (unless (search-forward "<title>" nil t)
2980                        (re-search-forward "<head>\\s-*" nil t)
2981                        (insert "<title>" title "</title>\n")))
2982                    (goto-char (point-min))
2983                    (or (re-search-forward
2984                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2985                        (re-search-forward
2986                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2987                    (insert eheader)
2988                    (mm-write-region (point-min) (point-max)
2989                                     tmp-file nil nil nil 'binary t))))
2990               (charset
2991                (mm-with-unibyte-buffer
2992                  (insert (if (eq charset 'gnus-decoded)
2993                              (mm-encode-coding-string content
2994                                                       (setq charset 'utf-8))
2995                            content))
2996                  (if (or (mm-add-meta-html-tag handle charset)
2997                          (not file))
2998                      (mm-write-region (point-min) (point-max)
2999                                       tmp-file nil nil nil 'binary t)
3000                    (setq tmp-file nil))))
3001               (tmp-file
3002                (mm-save-part-to-file handle tmp-file)))
3003              (when tmp-file
3004                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3005              (add-hook 'gnus-summary-prepare-exit-hook
3006                        'gnus-article-browse-delete-temp-files)
3007              (add-hook 'gnus-exit-gnus-hook
3008                        (lambda  ()
3009                          (gnus-article-browse-delete-temp-files t)))
3010              ;; FIXME: Warn if there's an <img> tag?
3011              (browse-url-of-file (or tmp-file (expand-file-name file)))
3012              (setq showed t))
3013             ;; If multipart, recurse
3014             ((equal (mm-handle-media-supertype handle) "multipart")
3015              (when (gnus-article-browse-html-parts handle header)
3016                (setq showed t)))
3017             ((equal (mm-handle-media-type handle) "message/rfc822")
3018              (mm-with-multibyte-buffer
3019                (mm-insert-part handle)
3020                (setq handle (mm-dissect-buffer t t))
3021                (when (and (bufferp (car handle))
3022                           (stringp (car (mm-handle-type handle))))
3023                  (setq handle (list handle)))
3024                (when header
3025                  (article-decode-encoded-words)
3026                  (let ((gnus-visible-headers
3027                         (or (get 'gnus-visible-headers 'standard-value)
3028                             gnus-visible-headers)))
3029                    (article-hide-headers))
3030                  (goto-char (point-min))
3031                  (search-forward "\n\n" nil 'move)
3032                  (skip-chars-backward "\t\n ")
3033                  (setq header (buffer-substring (point-min) (point)))))
3034              (when (prog1
3035                        (gnus-article-browse-html-parts handle header)
3036                      (mm-destroy-parts handle))
3037                (setq showed t)))))
3038     showed))
3039
3040 (defun gnus-article-browse-html-article (&optional arg)
3041   "View \"text/html\" parts of the current article with a WWW browser.
3042 Inline images embedded in a message using the cid scheme, as they are
3043 generally considered to be safe, will be processed properly.
3044 The message header is added to the beginning of every html part unless
3045 the prefix argument ARG is given.
3046
3047 Warning: Spammers use links to images (using the http scheme) in HTML
3048 articles to verify whether you have read the message.  As
3049 `gnus-article-browse-html-article' passes the HTML content to the
3050 browser without eliminating these \"web bugs\" you should only
3051 use it for mails from trusted senders.
3052
3053 If you always want to display HTML parts in the browser, set
3054 `mm-text-html-renderer' to nil.
3055
3056 This command creates temporary files to pass HTML contents including
3057 images if any to the browser, and deletes them when exiting the group
3058 \(if you want)."
3059   ;; Cf. `mm-w3m-safe-url-regexp'
3060   (interactive "P")
3061   (if arg
3062       (gnus-summary-show-article)
3063     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3064                                     gnus-visible-headers))
3065           ;; As we insert a <hr>, there's no need for the body boundary.
3066           (gnus-treat-body-boundary nil))
3067       (gnus-summary-show-article)))
3068   (with-current-buffer gnus-article-buffer
3069     (let ((header (unless arg
3070                     (save-restriction
3071                       (widen)
3072                       (buffer-substring-no-properties
3073                        (goto-char (point-min))
3074                        (if (search-forward "\n\n" nil t)
3075                            (match-beginning 0)
3076                          (goto-char (point-max))
3077                          (skip-chars-backward "\t\n ")
3078                          (point))))))
3079           parts)
3080       (set-buffer gnus-original-article-buffer)
3081       (setq parts (mm-dissect-buffer t t))
3082       ;; If singlepart, enforce a list.
3083       (when (and (bufferp (car parts))
3084                  (stringp (car (mm-handle-type parts))))
3085         (setq parts (list parts)))
3086       ;; Process the list
3087       (unless (gnus-article-browse-html-parts parts header)
3088         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3089       (mm-destroy-parts parts)
3090       (unless arg
3091         (gnus-summary-show-article)))))
3092
3093 (defun article-hide-list-identifiers ()
3094   "Remove list identifies from the Subject header.
3095 The `gnus-list-identifiers' variable specifies what to do."
3096   (interactive)
3097   (let ((inhibit-point-motion-hooks t)
3098         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3099         (inhibit-read-only t))
3100     (when regexp
3101       (save-excursion
3102         (save-restriction
3103           (article-narrow-to-head)
3104           (goto-char (point-min))
3105           (while (re-search-forward
3106                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3107                   nil t)
3108             (delete-region (match-beginning 2) (match-end 0))
3109             (beginning-of-line))
3110           (when (re-search-forward
3111                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3112             (delete-region (match-beginning 1) (match-end 1))))))))
3113
3114 (defun article-hide-pem (&optional arg)
3115   "Toggle hiding of any PEM headers and signatures in the current article.
3116 If given a negative prefix, always show; if given a positive prefix,
3117 always hide."
3118   (interactive (gnus-article-hidden-arg))
3119   (unless (gnus-article-check-hidden-text 'pem arg)
3120     (save-excursion
3121       (let ((inhibit-read-only t) end)
3122         (goto-char (point-min))
3123         ;; Hide the horrendously ugly "header".
3124         (when (and (search-forward
3125                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3126                     nil t)
3127                    (setq end (1+ (match-beginning 0))))
3128           (gnus-add-wash-type 'pem)
3129           (gnus-article-hide-text-type
3130            end
3131            (if (search-forward "\n\n" nil t)
3132                (match-end 0)
3133              (point-max))
3134            'pem)
3135           ;; Hide the trailer as well
3136           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3137                                 nil t)
3138             (gnus-article-hide-text-type
3139              (match-beginning 0) (match-end 0) 'pem)))))))
3140
3141 (defun article-strip-banner ()
3142   "Strip the banners specified by the `banner' group parameter and by
3143 `gnus-article-address-banner-alist'."
3144   (interactive)
3145   (save-excursion
3146     (save-restriction
3147       (let ((inhibit-point-motion-hooks t))
3148         (when (gnus-parameter-banner gnus-newsgroup-name)
3149           (article-really-strip-banner
3150            (gnus-parameter-banner gnus-newsgroup-name)))
3151         (when gnus-article-address-banner-alist
3152           ;; Note that the From header is decoded here, so it is
3153           ;; required that the *-extract-address-components function
3154           ;; supports non-ASCII text.
3155           (let ((from (save-restriction
3156                         (widen)
3157                         (article-narrow-to-head)
3158                         (mail-fetch-field "from"))))
3159             (when (and from
3160                        (setq from
3161                              (cadr (funcall gnus-extract-address-components
3162                                             from))))
3163               (catch 'found
3164                 (dolist (pair gnus-article-address-banner-alist)
3165                   (when (string-match (car pair) from)
3166                     (throw 'found
3167                            (article-really-strip-banner (cdr pair)))))))))))))
3168
3169 (defun article-really-strip-banner (banner)
3170   "Strip the banner specified by the argument."
3171   (save-excursion
3172     (save-restriction
3173       (let ((inhibit-point-motion-hooks t)
3174             (gnus-signature-limit nil)
3175             (inhibit-read-only t))
3176         (article-goto-body)
3177         (cond
3178          ((eq banner 'signature)
3179           (when (gnus-article-narrow-to-signature)
3180             (widen)
3181             (forward-line -1)
3182             (delete-region (point) (point-max))))
3183          ((symbolp banner)
3184           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3185               (while (re-search-forward banner nil t)
3186                 (delete-region (match-beginning 0) (match-end 0)))))
3187          ((stringp banner)
3188           (while (re-search-forward banner nil t)
3189             (delete-region (match-beginning 0) (match-end 0)))))))))
3190
3191 (defun article-babel ()
3192   "Translate article using an online translation service."
3193   (interactive)
3194   (require 'babel)
3195   (gnus-with-article-buffer
3196     (when (article-goto-body)
3197       (let* ((start (point))
3198              (end (point-max))
3199              (orig (buffer-substring start end))
3200              (trans (babel-as-string orig)))
3201         (save-restriction
3202           (narrow-to-region start end)
3203           (delete-region start end)
3204           (insert trans))))))
3205
3206 (defun article-hide-signature (&optional arg)
3207   "Hide the signature in the current article.
3208 If given a negative prefix, always show; if given a positive prefix,
3209 always hide."
3210   (interactive (gnus-article-hidden-arg))
3211   (unless (gnus-article-check-hidden-text 'signature arg)
3212     (save-excursion
3213       (save-restriction
3214         (let ((inhibit-read-only t))
3215           (when (gnus-article-narrow-to-signature)
3216             (gnus-article-hide-text-type
3217              (point-min) (point-max) 'signature))))))
3218   (gnus-set-mode-line 'article))
3219
3220 (defun article-strip-headers-in-body ()
3221   "Strip offensive headers from bodies."
3222   (interactive)
3223   (save-excursion
3224     (article-goto-body)
3225     (let ((case-fold-search t))
3226       (when (looking-at "x-no-archive:")
3227         (gnus-delete-line)))))
3228
3229 (defun article-strip-leading-blank-lines ()
3230   "Remove all blank lines from the beginning of the article."
3231   (interactive)
3232   (save-excursion
3233     (let ((inhibit-point-motion-hooks t)
3234           (inhibit-read-only t))
3235       (when (article-goto-body)
3236         (while (and (not (eobp))
3237                     (looking-at "[ \t]*$"))
3238           (gnus-delete-line))))))
3239
3240 (defun article-narrow-to-head ()
3241   "Narrow the buffer to the head of the message.
3242 Point is left at the beginning of the narrowed-to region."
3243   (narrow-to-region
3244    (goto-char (point-min))
3245    (cond
3246     ;; Absolutely no headers displayed.
3247     ((looking-at "\n")
3248      (point))
3249     ;; Normal headers.
3250     ((search-forward "\n\n" nil 1)
3251      (1- (point)))
3252     ;; Nothing but headers.
3253     (t
3254      (point-max))))
3255   (goto-char (point-min)))
3256
3257 (defun article-goto-body ()
3258   "Place point at the start of the body."
3259   (goto-char (point-min))
3260   (cond
3261    ;; This variable is only bound when dealing with separate
3262    ;; MIME body parts.
3263    (article-goto-body-goes-to-point-min-p
3264     t)
3265    ((search-forward "\n\n" nil t)
3266     t)
3267    (t
3268     (goto-char (point-max))
3269     nil)))
3270
3271 (defun article-strip-multiple-blank-lines ()
3272   "Replace consecutive blank lines with one empty line."
3273   (interactive)
3274   (save-excursion
3275     (let ((inhibit-point-motion-hooks t)
3276           (inhibit-read-only t))
3277       ;; First make all blank lines empty.
3278       (article-goto-body)
3279       (while (re-search-forward "^[ \t]+$" nil t)
3280         (unless (gnus-annotation-in-region-p
3281                  (match-beginning 0) (match-end 0))
3282           (replace-match "" nil t)))
3283       ;; Then replace multiple empty lines with a single empty line.
3284       (article-goto-body)
3285       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3286         (unless (gnus-annotation-in-region-p
3287                  (match-beginning 0) (match-end 0))
3288           (delete-region (match-beginning 1) (match-end 1)))))))
3289
3290 (defun article-strip-leading-space ()
3291   "Remove all white space from the beginning of the lines in the article."
3292   (interactive)
3293   (save-excursion
3294     (let ((inhibit-point-motion-hooks t)
3295           (inhibit-read-only t))
3296       (article-goto-body)
3297       (while (re-search-forward "^[ \t]+" nil t)
3298         (replace-match "" t t)))))
3299
3300 (defun article-strip-trailing-space ()
3301   "Remove all white space from the end of the lines in the article."
3302   (interactive)
3303   (save-excursion
3304     (let ((inhibit-point-motion-hooks t)
3305           (inhibit-read-only t))
3306       (article-goto-body)
3307       (while (re-search-forward "[ \t]+$" nil t)
3308         (replace-match "" t t)))))
3309
3310 (defun article-strip-blank-lines ()
3311   "Strip leading, trailing and multiple blank lines."
3312   (interactive)
3313   (article-strip-leading-blank-lines)
3314   (article-remove-trailing-blank-lines)
3315   (article-strip-multiple-blank-lines))
3316
3317 (defun article-strip-all-blank-lines ()
3318   "Strip all blank lines."
3319   (interactive)
3320   (save-excursion
3321     (let ((inhibit-point-motion-hooks t)
3322           (inhibit-read-only t))
3323       (article-goto-body)
3324       (while (re-search-forward "^[ \t]*\n" nil t)
3325         (replace-match "" t t)))))
3326
3327 (defun gnus-article-narrow-to-signature ()
3328   "Narrow to the signature; return t if a signature is found, else nil."
3329   (let ((inhibit-point-motion-hooks t))
3330     (when (gnus-article-search-signature)
3331       (forward-line 1)
3332       ;; Check whether we have some limits to what we consider
3333       ;; to be a signature.
3334       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3335                       (list gnus-signature-limit)))
3336             limit limited)
3337         (while (setq limit (pop limits))
3338           (if (or (and (integerp limit)
3339                        (< (- (point-max) (point)) limit))
3340                   (and (floatp limit)
3341                        (< (count-lines (point) (point-max)) limit))
3342                   (and (functionp limit)
3343                        (funcall limit))
3344                   (and (stringp limit)
3345                        (not (re-search-forward limit nil t))))
3346               ()                        ; This limit did not succeed.
3347             (setq limited t
3348                   limits nil)))
3349         (unless limited
3350           (narrow-to-region (point) (point-max))
3351           t)))))
3352
3353 (defun gnus-article-search-signature ()
3354   "Search the current buffer for the signature separator.
3355 Put point at the beginning of the signature separator."
3356   (let ((cur (point)))
3357     (goto-char (point-max))
3358     (if (if (stringp gnus-signature-separator)
3359             (re-search-backward gnus-signature-separator nil t)
3360           (let ((seps gnus-signature-separator))
3361             (while (and seps
3362                         (not (re-search-backward (car seps) nil t)))
3363               (pop seps))
3364             seps))
3365         t
3366       (goto-char cur)
3367       nil)))
3368
3369 (defun gnus-article-hidden-arg ()
3370   "Return the current prefix arg as a number, or 0 if no prefix."
3371   (list (if current-prefix-arg
3372             (prefix-numeric-value current-prefix-arg)
3373           0)))
3374
3375 (defun gnus-article-check-hidden-text (type arg)
3376   "Return nil if hiding is necessary.
3377 Arg can be nil or a number.  nil and positive means hide, negative
3378 means show, 0 means toggle."
3379   (save-excursion
3380     (save-restriction
3381       (let ((hide (gnus-article-hidden-text-p type)))
3382         (cond
3383          ((or (null arg)
3384               (> arg 0))
3385           nil)
3386          ((< arg 0)
3387           (gnus-article-show-hidden-text type)
3388           t)
3389          (t
3390           (if (eq hide 'hidden)
3391               (progn
3392                 (gnus-article-show-hidden-text type)
3393                 t)
3394             nil)))))))
3395
3396 (defun gnus-article-hidden-text-p (type)
3397   "Say whether the current buffer contains hidden text of type TYPE."
3398   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3399     (while (and pos
3400                 (not (get-text-property pos 'invisible))
3401                 (not (get-text-property pos 'dummy-invisible)))
3402       (setq pos
3403             (text-property-any (1+ pos) (point-max) 'article-type type)))
3404     (if pos
3405         'hidden
3406       nil)))
3407
3408 (defun gnus-article-show-hidden-text (type &optional dummy)
3409   "Show all hidden text of type TYPE.
3410 Originally it is hide instead of DUMMY."
3411   (let ((inhibit-read-only t)
3412         (inhibit-point-motion-hooks t))
3413     (gnus-remove-text-properties-when
3414      'article-type type
3415      (point-min) (point-max)
3416      (cons 'article-type (cons type
3417                                gnus-hidden-properties)))
3418     (gnus-delete-wash-type type)))
3419
3420 (defconst article-time-units
3421   `((year . ,(* 365.25 24 60 60))
3422     (week . ,(* 7 24 60 60))
3423     (day . ,(* 24 60 60))
3424     (hour . ,(* 60 60))
3425     (minute . 60)
3426     (second . 1))
3427   "Mapping from time units to seconds.")
3428
3429 (defun gnus-article-forward-header ()
3430   "Move point to the start of the next header.
3431 If the current header is a continuation header, this can be several
3432 lines forward."
3433   (let ((ended nil))
3434     (while (not ended)
3435       (forward-line 1)
3436       (if (looking-at "[ \t]+[^ \t]")
3437           (forward-line 1)
3438         (setq ended t)))))
3439
3440 (defun article-treat-date ()
3441   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3442                        (with-current-buffer gnus-summary-buffer
3443                          gnus-article-date-headers)
3444                      gnus-article-date-headers)
3445                    t))
3446
3447 (defun article-date-ut (&optional type highlight date-position)
3448   "Convert DATE date to TYPE in the current article.
3449 The default type is `ut'.  See `gnus-article-date-headers' for
3450 possible values."
3451   (interactive (list 'ut t))
3452   (let* ((case-fold-search t)
3453          (inhibit-read-only t)
3454          (inhibit-point-motion-hooks t)
3455          (first t)
3456          (visible-date (mail-fetch-field "Date"))
3457          pos date bface eface)
3458     (save-excursion
3459       (goto-char (point-min))
3460       (when (re-search-forward "^Date:" nil t)
3461         (setq bface (get-text-property (point-at-bol) 'face)
3462               eface (get-text-property (1- (point-at-eol)) 'face)))
3463       ;; Delete any old Date headers.
3464       (if date-position
3465           (progn
3466             (goto-char date-position)
3467             (setq date (get-text-property (point) 'original-date))
3468             (delete-region (point)
3469                            (progn
3470                              (gnus-article-forward-header)
3471                              (point)))
3472             (article-transform-date date type bface eface))
3473         (save-restriction
3474           (widen)
3475           (goto-char (point-min))
3476           (while (or (get-text-property (setq pos (point)) 'original-date)
3477                      (and (setq pos (next-single-property-change
3478                                      (point) 'original-date))
3479                           (goto-char pos)))
3480             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3481                                       (1+ (match-beginning 0))
3482                                     (point-max)))
3483             (goto-char (point-min))
3484             (while (re-search-forward "^Date:" nil t)
3485               (setq date (get-text-property (match-beginning 0) 'original-date))
3486               (delete-region (point-at-bol) (progn
3487                                               (gnus-article-forward-header)
3488                                               (point))))
3489             (when (and (not date)
3490                        visible-date)
3491               (setq date visible-date))
3492             (when date
3493               (article-transform-date date type bface eface))
3494             (goto-char (point-max))
3495             (widen)))))))
3496
3497 (defun article-transform-date (date type bface eface)
3498   (dolist (this-type (cond
3499                       ((null type)
3500                        (list 'ut))
3501                       ((atom type)
3502                        (list type))
3503                       (t
3504                        type)))
3505     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3506     (forward-line -1)
3507     (beginning-of-line)
3508     (put-text-property (point) (1+ (point))
3509                        'original-date date)
3510     (put-text-property (point) (1+ (point))
3511                        'gnus-date-type this-type)
3512     ;; Do highlighting.
3513     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3514       (put-text-property (match-beginning 1) (1+ (match-end 1))
3515                          'face bface)
3516       (put-text-property (match-beginning 2) (match-end 2)
3517                          'face eface))
3518     (forward-line 1)))
3519
3520 (defun article-make-date-line (date type)
3521   "Return a DATE line of TYPE."
3522   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3523                              combined-lapsed))
3524     (error "Unknown conversion type: %s" type))
3525   (condition-case ()
3526       (let ((time (ignore-errors (date-to-time date))))
3527         (cond
3528          ;; Convert to the local timezone.
3529          ((eq type 'local)
3530           (concat "Date: " (message-make-date time)))
3531          ;; Convert to Universal Time.
3532          ((eq type 'ut)
3533           (concat "Date: "
3534                   (substring
3535                    (message-make-date
3536                     (let* ((e (parse-time-string date))
3537                            (tm (apply 'encode-time e))
3538                            (ms (car tm))
3539                            (ls (- (cadr tm) (car (current-time-zone time)))))
3540                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3541                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3542                             (t (list ms ls)))))
3543                    0 -5)
3544                   "UT"))
3545          ;; Get the original date from the article.
3546          ((eq type 'original)
3547           (concat "Date: " (if (string-match "\n+$" date)
3548                                (substring date 0 (match-beginning 0))
3549                              date)))
3550          ;; Let the user define the format.
3551          ((eq type 'user-defined)
3552           (let ((format (or (condition-case nil
3553                                 (with-current-buffer gnus-summary-buffer
3554                                   gnus-article-time-format)
3555                               (error nil))
3556                             gnus-article-time-format)))
3557             (if (functionp format)
3558                 (funcall format time)
3559               (concat "Date: " (format-time-string format time)))))
3560          ;; ISO 8601.
3561          ((eq type 'iso8601)
3562           (let ((tz (car (current-time-zone time))))
3563             (concat
3564              "Date: "
3565              (format-time-string "%Y%m%dT%H%M%S" time)
3566              (format "%s%02d%02d"
3567                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3568                      (/ (% (abs tz) 3600) 60)))))
3569          ;; Do a lapsed format.
3570          ((eq type 'lapsed)
3571           (concat "Date: " (article-lapsed-string time)))
3572          ;; A combined date/lapsed format.
3573          ((eq type 'combined-lapsed)
3574           (let ((date-string (article-make-date-line date 'original))
3575                 (segments 3)
3576                 lapsed-string)
3577             (while (and
3578                     time
3579                     (setq lapsed-string
3580                           (concat " (" (article-lapsed-string time segments) ")"))
3581                     (> (+ (length date-string)
3582                           (length lapsed-string))
3583                        (+ fill-column 6))
3584                     (> segments 0))
3585               (setq segments (1- segments)))
3586             (if (> segments 0)
3587                 (concat date-string lapsed-string)
3588               date-string)))
3589          ;; Display the date in proper English
3590          ((eq type 'english)
3591           (let ((dtime (decode-time time)))
3592             (concat
3593              "Date: the "
3594              (number-to-string (nth 3 dtime))
3595              (let ((digit (% (nth 3 dtime) 10)))
3596                (cond
3597                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3598                 ((= digit 1) "st")
3599                 ((= digit 2) "nd")
3600                 ((= digit 3) "rd")
3601                 (t "th")))
3602              " of "
3603              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3604              " "
3605              (number-to-string (nth 5 dtime))
3606              " at "
3607              (format "%02d" (nth 2 dtime))
3608              ":"
3609              (format "%02d" (nth 1 dtime)))))))
3610     (foo
3611      (format "Date: %s (from Gnus)" date))))
3612
3613 (defun article-lapsed-string (time &optional max-segments)
3614   ;; If the date is seriously mangled, the timezone functions are
3615   ;; liable to bug out, so we ignore all errors.
3616   (let* ((now (current-time))
3617          (real-time (subtract-time now time))
3618          (real-sec (and real-time
3619                         (+ (* (float (car real-time)) 65536)
3620                            (cadr real-time))))
3621          (sec (and real-time (abs real-sec)))
3622          (segments 0)
3623          num prev)
3624     (unless max-segments
3625       (setq max-segments (length article-time-units)))
3626     (cond
3627      ((null real-time)
3628       "Unknown")
3629      ((zerop sec)
3630       "Now")
3631      (t
3632       (concat
3633        ;; This is a bit convoluted, but basically we go
3634        ;; through the time units for years, weeks, etc,
3635        ;; and divide things to see whether that results
3636        ;; in positive answers.
3637        (mapconcat
3638         (lambda (unit)
3639           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3640                   (>= segments max-segments))
3641               ;; The (remaining) seconds are too few to
3642               ;; be divided into this time unit.
3643               ""
3644             ;; It's big enough, so we output it.
3645             (setq sec (- sec (* num (cdr unit))))
3646             (prog1
3647                 (concat (if prev ", " "") (int-to-string
3648                                            (floor num))
3649                         " " (symbol-name (car unit))
3650                         (if (> num 1) "s" ""))
3651               (setq prev t
3652                     segments (1+ segments)))))
3653         article-time-units "")
3654        ;; If dates are odd, then it might appear like the
3655        ;; article was sent in the future.
3656        (if (> real-sec 0)
3657            " ago"
3658          " in the future"))))))
3659
3660 (defun article-date-local (&optional highlight)
3661   "Convert the current article date to the local timezone."
3662   (interactive (list t))
3663   (article-date-ut 'local highlight))
3664
3665 (defun article-date-english (&optional highlight)
3666   "Convert the current article date to something that is proper English."
3667   (interactive (list t))
3668   (article-date-ut 'english highlight))
3669
3670 (defun article-date-original (&optional highlight)
3671   "Convert the current article date to what it was originally.
3672 This is only useful if you have used some other date conversion
3673 function and want to see what the date was before converting."
3674   (interactive (list t))
3675   (article-date-ut 'original highlight))
3676
3677 (defun article-date-lapsed (&optional highlight)
3678   "Convert the current article date to time lapsed since it was sent."
3679   (interactive (list t))
3680   (article-date-ut 'lapsed highlight))
3681
3682 (defun article-date-combined-lapsed (&optional highlight)
3683   "Convert the current article date to time lapsed since it was sent."
3684   (interactive (list t))
3685   (article-date-ut 'combined-lapsed highlight))
3686
3687 (defun article-update-date-lapsed ()
3688   "Function to be run from a timer to update the lapsed time line."
3689   (save-match-data
3690     (let ((buffer (current-buffer)))
3691       (ignore-errors
3692         (walk-windows
3693          (lambda (w)
3694            (set-buffer (window-buffer w))
3695            (when (eq major-mode 'gnus-article-mode)
3696              (let ((old-line (count-lines (point-min) (point)))
3697                    (old-column (- (point) (line-beginning-position)))
3698                    (window-start
3699                     (window-start (get-buffer-window (current-buffer)))))
3700                (goto-char (point-min))
3701                (while (re-search-forward "^Date:" nil t)
3702                  (let ((type (get-text-property (match-beginning 0)
3703                                                 'gnus-date-type)))
3704                    (when (memq type '(lapsed combined-lapsed user-format))
3705                      (when (and window-start
3706                                 (not (= window-start
3707                                         (save-excursion
3708                                           (forward-line 1)
3709                                           (point)))))
3710                        (setq window-start nil))
3711                      (save-excursion
3712                        (article-date-ut type t (match-beginning 0)))
3713                      (forward-line 1)
3714                      (when window-start
3715                        (set-window-start (get-buffer-window (current-buffer))
3716                                          (point))))))
3717                (goto-char (point-min))
3718                (when (> old-column 0)
3719                  (setq old-line (1- old-line)))
3720                (forward-line old-line)
3721                (end-of-line)
3722                (when (> (current-column) old-column)
3723                  (beginning-of-line)
3724                  (forward-char old-column)))))
3725          nil 'visible))
3726       (set-buffer buffer))))
3727
3728 (defun gnus-start-date-timer (&optional n)
3729   "Start a timer to update the Date headers in the article buffers.
3730 The numerical prefix says how frequently (in seconds) the function
3731 is to run."
3732   (interactive "p")
3733   (unless n
3734     (setq n 1))
3735   (gnus-stop-date-timer)
3736   (setq article-lapsed-timer
3737         (run-at-time 1 n 'article-update-date-lapsed)))
3738
3739 (defun gnus-stop-date-timer ()
3740   "Stop the Date timer."
3741   (interactive)
3742   (when article-lapsed-timer
3743     (nnheader-cancel-timer article-lapsed-timer)
3744     (setq article-lapsed-timer nil)))
3745
3746 (defun article-date-user (&optional highlight)
3747   "Convert the current article date to the user-defined format.
3748 This format is defined by the `gnus-article-time-format' variable."
3749   (interactive (list t))
3750   (article-date-ut 'user highlight))
3751
3752 (defun article-date-iso8601 (&optional highlight)
3753   "Convert the current article date to ISO8601."
3754   (interactive (list t))
3755   (article-date-ut 'iso8601 highlight))
3756
3757 (defmacro gnus-article-save-original-date (&rest forms)
3758   "Save the original date as a text property and evaluate FORMS."
3759   `(let* ((case-fold-search t)
3760           (start (progn
3761                    (goto-char (point-min))
3762                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3763                               (not (bolp)))
3764                      (match-end 0))))
3765           (date (when (and start
3766                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3767                                               nil t))
3768                   (buffer-substring-no-properties start
3769                                                   (match-beginning 0)))))
3770      (goto-char (point-max))
3771      (skip-chars-backward "\n")
3772      (put-text-property (point-min) (point) 'original-date date)
3773      ,@forms
3774      (goto-char (point-max))
3775      (skip-chars-backward "\n")
3776      (put-text-property (point-min) (point) 'original-date date)))
3777
3778 ;; (defun article-show-all ()
3779 ;;   "Show all hidden text in the article buffer."
3780 ;;   (interactive)
3781 ;;   (save-excursion
3782 ;;     (let ((inhibit-read-only t))
3783 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3784
3785 (defun article-remove-leading-whitespace ()
3786   "Remove excessive whitespace from all headers."
3787   (interactive)
3788   (save-excursion
3789     (save-restriction
3790       (let ((inhibit-read-only t))
3791         (article-narrow-to-head)
3792         (goto-char (point-min))
3793         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3794           (delete-region (match-beginning 1) (match-end 1)))))))
3795
3796 (defun article-emphasize (&optional arg)
3797   "Emphasize text according to `gnus-emphasis-alist'."
3798   (interactive (gnus-article-hidden-arg))
3799   (unless (gnus-article-check-hidden-text 'emphasis arg)
3800     (save-excursion
3801       (let ((alist (or
3802                     (condition-case nil
3803                         (with-current-buffer gnus-summary-buffer
3804                           gnus-article-emphasis-alist)
3805                       (error))
3806                     gnus-emphasis-alist))
3807             (inhibit-read-only t)
3808             (props (append '(article-type emphasis)
3809                            gnus-hidden-properties))
3810             regexp elem beg invisible visible face)
3811         (article-goto-body)
3812         (setq beg (point))
3813         (while (setq elem (pop alist))
3814           (goto-char beg)
3815           (setq regexp (car elem)
3816                 invisible (nth 1 elem)
3817                 visible (nth 2 elem)
3818                 face (nth 3 elem))
3819           (while (re-search-forward regexp nil t)
3820             (when (and (match-beginning visible) (match-beginning invisible))
3821               (gnus-article-hide-text
3822                (match-beginning invisible) (match-end invisible) props)
3823               (gnus-article-unhide-text-type
3824                (match-beginning visible) (match-end visible) 'emphasis)
3825               (gnus-put-overlay-excluding-newlines
3826                (match-beginning visible) (match-end visible) 'face face)
3827               (gnus-add-wash-type 'emphasis)
3828               (goto-char (match-end invisible)))))))))
3829
3830 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3831   "Setup newsgroup emphasis alist."
3832   (unless gnus-article-emphasis-alist
3833     (let ((name (and gnus-newsgroup-name
3834                      (gnus-group-real-name gnus-newsgroup-name))))
3835       (make-local-variable 'gnus-article-emphasis-alist)
3836       (setq gnus-article-emphasis-alist
3837             (nconc
3838              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3839                (while (setq elem (pop alist))
3840                  (when (and name (string-match (car elem) name))
3841                    (setq alist nil
3842                          highlight (copy-sequence (cdr elem)))))
3843                highlight)
3844              (copy-sequence highlight-words)
3845              (if gnus-newsgroup-name
3846                  (copy-sequence (gnus-group-find-parameter
3847                                  gnus-newsgroup-name 'highlight-words t)))
3848              gnus-emphasis-alist)))))
3849
3850 (defvar gnus-summary-article-menu)
3851 (defvar gnus-summary-post-menu)
3852
3853 ;;; Saving functions.
3854
3855 (defun gnus-article-save (save-buffer file &optional num)
3856   "Save the currently selected article."
3857   (when (or (get gnus-default-article-saver :headers)
3858             (not gnus-save-all-headers))
3859     ;; Remove headers according to `gnus-saved-headers' or the value
3860     ;; of the `:headers' property that the saver function might have.
3861     (let ((gnus-visible-headers
3862            (or (symbol-value (get gnus-default-article-saver :headers))
3863                gnus-saved-headers gnus-visible-headers))
3864           ;; Ignore group parameter.  See `article-hide-headers'.
3865           (gnus-summary-buffer nil))
3866       (with-current-buffer save-buffer
3867         (article-hide-headers 1 t))))
3868   (save-window-excursion
3869     (if (not gnus-default-article-saver)
3870         (error "No default saver is defined")
3871       ;; !!! Magic!  The saving functions all save
3872       ;; `gnus-save-article-buffer' (or so they think), but we
3873       ;; bind that variable to our save-buffer.
3874       (set-buffer gnus-article-buffer)
3875       (let* ((gnus-save-article-buffer save-buffer)
3876              (filename
3877               (cond
3878                ((not gnus-prompt-before-saving) 'default)
3879                ((eq gnus-prompt-before-saving 'always) nil)
3880                (t file)))
3881              (gnus-number-of-articles-to-be-saved
3882               (when (eq gnus-prompt-before-saving t)
3883                 num)))                  ; Magic
3884         (set-buffer gnus-article-current-summary)
3885         (funcall gnus-default-article-saver filename)))))
3886
3887 (defun gnus-read-save-file-name (prompt &optional filename
3888                                         function group headers variable
3889                                         dir-var)
3890   (let ((default-name
3891           (funcall function group headers (symbol-value variable)))
3892         result)
3893     (setq result
3894           (expand-file-name
3895            (cond
3896             ((eq filename 'default)
3897              default-name)
3898             ((eq filename t)
3899              default-name)
3900             (filename filename)
3901             (t
3902              (when (symbol-value dir-var)
3903                (setq default-name (expand-file-name
3904                                    (file-name-nondirectory default-name)
3905                                    (symbol-value dir-var))))
3906              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3907                     (prompt
3908                      (format prompt
3909                              (if (and gnus-number-of-articles-to-be-saved
3910                                       (> gnus-number-of-articles-to-be-saved 1))
3911                                  (format "these %d articles"
3912                                          gnus-number-of-articles-to-be-saved)
3913                                "this article")))
3914                     (file
3915                      ;; Let the split methods have their say.
3916                      (cond
3917                       ;; No split name was found.
3918                       ((null split-name)
3919                        (read-file-name
3920                         (concat prompt " (default "
3921                                 (file-name-nondirectory default-name) "): ")
3922                         (file-name-directory default-name)
3923                         default-name))
3924                       ;; A single group name is returned.
3925                       ((stringp split-name)
3926                        (setq default-name
3927                              (funcall function split-name headers
3928                                       (symbol-value variable)))
3929                        (read-file-name
3930                         (concat prompt " (default "
3931                                 (file-name-nondirectory default-name) "): ")
3932                         (file-name-directory default-name)
3933                         default-name))
3934                       ;; A single split name was found
3935                       ((= 1 (length split-name))
3936                        (let* ((name (expand-file-name
3937                                      (car split-name)
3938                                      gnus-article-save-directory))
3939                               (dir (cond ((file-directory-p name)
3940                                           (file-name-as-directory name))
3941                                          ((file-exists-p name) name)
3942                                          (t gnus-article-save-directory))))
3943                          (read-file-name
3944                           (concat prompt " (default " name "): ")
3945                           dir name)))
3946                       ;; A list of splits was found.
3947                       (t
3948                        (setq split-name (nreverse split-name))
3949                        (let (result)
3950                          (let ((file-name-history
3951                                 (nconc split-name file-name-history)))
3952                            (setq result
3953                                  (expand-file-name
3954                                   (read-file-name
3955                                    (concat prompt " (`M-p' for defaults): ")
3956                                    gnus-article-save-directory
3957                                    (car split-name))
3958                                   gnus-article-save-directory)))
3959                          (car (push result file-name-history)))))))
3960                ;; Create the directory.
3961                (gnus-make-directory (file-name-directory file))
3962                ;; If we have read a directory, we append the default file name.
3963                (when (file-directory-p file)
3964                  (setq file (expand-file-name (file-name-nondirectory
3965                                                default-name)
3966                                               (file-name-as-directory file))))
3967                ;; Possibly translate some characters.
3968                (nnheader-translate-file-chars file))))))
3969     (gnus-make-directory (file-name-directory result))
3970     (when variable
3971       (set variable result))
3972     (when dir-var
3973       (set dir-var (file-name-directory result)))
3974     result))
3975
3976 (defun gnus-article-archive-name (group)
3977   "Return the first instance of an \"Archive-name\" in the current buffer."
3978   (let ((case-fold-search t))
3979     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3980       (nnheader-concat gnus-article-save-directory
3981                        (match-string 1)))))
3982
3983 (defun gnus-article-nndoc-name (group)
3984   "If GROUP is an nndoc group, return the name of the parent group."
3985   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3986     (gnus-group-get-parameter group 'save-article-group)))
3987
3988 (defun gnus-summary-save-in-rmail (&optional filename)
3989   "Append this article to Rmail file.
3990 Optional argument FILENAME specifies file name.
3991 Directory to save to is default to `gnus-article-save-directory'."
3992   (setq filename (gnus-read-save-file-name
3993                   "Save %s in rmail file" filename
3994                   gnus-rmail-save-name gnus-newsgroup-name
3995                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3996   (with-current-buffer gnus-save-article-buffer
3997     (save-excursion
3998       (save-restriction
3999         (widen)
4000         ;; Note that unlike gnus-summary-save-in-mail, there is no
4001         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4002         ;; not use Babyl.
4003         (gnus-output-to-rmail filename))))
4004   filename)
4005
4006 (defun gnus-summary-save-in-mail (&optional filename)
4007   "Append this article to Unix mail file.
4008 Optional argument FILENAME specifies file name.
4009 Directory to save to is default to `gnus-article-save-directory'."
4010   (setq filename (gnus-read-save-file-name
4011                   "Save %s in Unix mail file" filename
4012                   gnus-mail-save-name gnus-newsgroup-name
4013                   gnus-current-headers 'gnus-newsgroup-last-mail))
4014   (with-current-buffer gnus-save-article-buffer
4015     (save-excursion
4016       (save-restriction
4017         (widen)
4018         (if (and (file-readable-p filename)
4019                  (file-regular-p filename)
4020                  (mail-file-babyl-p filename))
4021             (gnus-output-to-rmail filename)
4022           (gnus-output-to-mail filename)))))
4023   filename)
4024
4025 (put 'gnus-summary-save-in-file :decode t)
4026 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4027 (defun gnus-summary-save-in-file (&optional filename overwrite)
4028   "Append this article to file.
4029 Optional argument FILENAME specifies file name.
4030 Directory to save to is default to `gnus-article-save-directory'."
4031   (setq filename (gnus-read-save-file-name
4032                   "Save %s in file" filename
4033                   gnus-file-save-name gnus-newsgroup-name
4034                   gnus-current-headers 'gnus-newsgroup-last-file))
4035   (with-current-buffer gnus-save-article-buffer
4036     (save-excursion
4037       (save-restriction
4038         (widen)
4039         (when (and overwrite
4040                    (file-exists-p filename))
4041           (delete-file filename))
4042         (gnus-output-to-file filename))))
4043   filename)
4044
4045 (put 'gnus-summary-write-to-file :decode t)
4046 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4047 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4048 (defun gnus-summary-write-to-file (&optional filename)
4049   "Write this article to a file, overwriting it if the file exists.
4050 Optional argument FILENAME specifies file name.
4051 The directory to save in defaults to `gnus-article-save-directory'."
4052   (setq filename (gnus-read-save-file-name
4053                   "Save %s in file" filename
4054                   gnus-file-save-name gnus-newsgroup-name
4055                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4056   (gnus-summary-save-in-file filename t))
4057
4058 (put 'gnus-summary-save-body-in-file :decode t)
4059 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4060   "Append this article body to a file.
4061 Optional argument FILENAME specifies file name.
4062 The directory to save in defaults to `gnus-article-save-directory'."
4063   (setq filename (gnus-read-save-file-name
4064                   "Save %s body in file" filename
4065                   gnus-file-save-name gnus-newsgroup-name
4066                   gnus-current-headers 'gnus-newsgroup-last-file))
4067   (with-current-buffer gnus-save-article-buffer
4068     (save-excursion
4069       (save-restriction
4070         (widen)
4071         (when (article-goto-body)
4072           (narrow-to-region (point) (point-max)))
4073         (when (and overwrite
4074                    (file-exists-p filename))
4075           (delete-file filename))
4076         (gnus-output-to-file filename))))
4077   filename)
4078
4079 (put 'gnus-summary-write-body-to-file :decode t)
4080 (put 'gnus-summary-write-body-to-file
4081      :function 'gnus-summary-save-body-in-file)
4082 (defun gnus-summary-write-body-to-file (&optional filename)
4083   "Write this article body to a file, overwriting it if the file exists.
4084 Optional argument FILENAME specifies file name.
4085 The directory to save in defaults to `gnus-article-save-directory'."
4086   (setq filename (gnus-read-save-file-name
4087                   "Save %s body in file" filename
4088                   gnus-file-save-name gnus-newsgroup-name
4089                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4090   (gnus-summary-save-body-in-file filename t))
4091
4092 (put 'gnus-summary-save-in-pipe :decode t)
4093 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4094 (defun gnus-summary-save-in-pipe (&optional command raw)
4095   "Pipe this article to subprocess COMMAND.
4096 Valid values for COMMAND include:
4097   a string
4098     The executable command name and possibly arguments.
4099   nil
4100     You will be prompted for the command in the minibuffer.
4101   the symbol `default'
4102     It will be replaced with the command which the variable
4103     `gnus-summary-pipe-output-default-command' holds or the command
4104     last used for saving.
4105 Non-nil value for RAW overrides `:decode' and `:headers' properties
4106 and the raw article including all headers will be piped."
4107   (let ((article (gnus-summary-article-number))
4108         (decode (unless raw
4109                   (get 'gnus-summary-save-in-pipe :decode)))
4110         save-buffer default)
4111     (if article
4112         (if (vectorp (gnus-summary-article-header article))
4113             (save-current-buffer
4114               (gnus-summary-select-article decode decode nil article)
4115               (insert-buffer-substring
4116                (prog1
4117                    (if decode
4118                        gnus-article-buffer
4119                      gnus-original-article-buffer)
4120                  (setq save-buffer
4121                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4122               ;; Remove unwanted headers.
4123               (when (and (not raw)
4124                          (or (get 'gnus-summary-save-in-pipe :headers)
4125                              (not gnus-save-all-headers)))
4126                 (let ((gnus-visible-headers
4127                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4128                                               :headers))
4129                            gnus-saved-headers gnus-visible-headers))
4130                       (gnus-summary-buffer nil))
4131                   (article-hide-headers 1 t))))
4132           (error "%d is not a real article" article))
4133       (error "No article to pipe"))
4134     (setq default (or gnus-summary-pipe-output-default-command
4135                       gnus-last-shell-command))
4136     (unless (stringp command)
4137       (setq command
4138             (if (and (eq command 'default) default)
4139                 default
4140               (gnus-read-shell-command "Shell command on this article: "
4141                                        default))))
4142     (when (string-equal command "")
4143       (if default
4144           (setq command default)
4145         (error "A command is required")))
4146     (with-current-buffer save-buffer
4147       (save-restriction
4148         (widen)
4149         (shell-command-on-region (point-min) (point-max) command nil)))
4150     (gnus-kill-buffer save-buffer))
4151   (setq gnus-summary-pipe-output-default-command command))
4152
4153 (defun gnus-summary-pipe-to-muttprint (&optional command)
4154   "Pipe this article to muttprint."
4155   (unless (stringp command)
4156     (setq command (read-string
4157                    "Print using command: " gnus-summary-muttprint-program
4158                    nil gnus-summary-muttprint-program)))
4159   (let ((gnus-summary-pipe-output-default-command
4160          gnus-summary-pipe-output-default-command))
4161     (gnus-summary-save-in-pipe command))
4162   (setq gnus-summary-muttprint-program command))
4163
4164 ;;; Article file names when saving.
4165
4166 (defun gnus-capitalize-newsgroup (newsgroup)
4167   "Capitalize NEWSGROUP name."
4168   (when (not (zerop (length newsgroup)))
4169     (concat (char-to-string (upcase (aref newsgroup 0)))
4170             (substring newsgroup 1))))
4171
4172 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4173   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4174 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4175 Otherwise, it is like ~/News/news/group/num."
4176   (let ((default
4177           (expand-file-name
4178            (concat (if (gnus-use-long-file-name 'not-save)
4179                        (gnus-capitalize-newsgroup newsgroup)
4180                      (gnus-newsgroup-directory-form newsgroup))
4181                    "/" (int-to-string (mail-header-number headers)))
4182            gnus-article-save-directory)))
4183     (if (and last-file
4184              (string-equal (file-name-directory default)
4185                            (file-name-directory last-file))
4186              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4187         default
4188       (or last-file default))))
4189
4190 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4191   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4192 If variable `gnus-use-long-file-name' is non-nil, it is
4193 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4194   (let ((default
4195           (expand-file-name
4196            (concat (if (gnus-use-long-file-name 'not-save)
4197                        newsgroup
4198                      (gnus-newsgroup-directory-form newsgroup))
4199                    "/" (int-to-string (mail-header-number headers)))
4200            gnus-article-save-directory)))
4201     (if (and last-file
4202              (string-equal (file-name-directory default)
4203                            (file-name-directory last-file))
4204              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4205         default
4206       (or last-file default))))
4207
4208 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4209   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4210 If variable `gnus-use-long-file-name' is non-nil, it is
4211 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4212   (or last-file
4213       (expand-file-name
4214        (if (gnus-use-long-file-name 'not-save)
4215            newsgroup
4216          (file-relative-name
4217           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4218           default-directory))
4219        gnus-article-save-directory)))
4220
4221 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4222   "Generate file name from sender."
4223   (let ((from (mail-header-from headers)))
4224     (expand-file-name
4225      (if (and from (string-match "\\([^ <]+\\)@" from))
4226          (match-string 1 from)
4227        "nobody")
4228      gnus-article-save-directory)))
4229
4230 (defun article-verify-x-pgp-sig ()
4231   "Verify X-PGP-Sig."
4232   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4233   (interactive)
4234   (if (gnus-buffer-live-p gnus-original-article-buffer)
4235       (let ((sig (with-current-buffer gnus-original-article-buffer
4236                    (gnus-fetch-field "X-PGP-Sig")))
4237             items info headers)
4238         (when (and sig
4239                    mml2015-use
4240                    (mml2015-clear-verify-function))
4241           (with-temp-buffer
4242             (insert-buffer-substring gnus-original-article-buffer)
4243             (setq items (split-string sig))
4244             (message-narrow-to-head)
4245             (let ((inhibit-point-motion-hooks t)
4246                   (case-fold-search t))
4247               ;; Don't verify multiple headers.
4248               (setq headers (mapconcat (lambda (header)
4249                                          (concat header ": "
4250                                                  (mail-fetch-field header)
4251                                                  "\n"))
4252                                        (split-string (nth 1 items) ",") "")))
4253             (delete-region (point-min) (point-max))
4254             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4255             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4256             (insert headers)
4257             (widen)
4258             (forward-line)
4259             (while (not (eobp))
4260               (if (looking-at "^-")
4261                   (insert "- "))
4262               (forward-line))
4263             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4264             (insert "Version: " (car items) "\n\n")
4265             (insert (mapconcat 'identity (cddr items) "\n"))
4266             (insert "\n-----END PGP SIGNATURE-----\n")
4267             (let ((mm-security-handle (list (format "multipart/signed"))))
4268               (mml2015-clean-buffer)
4269               (let ((coding-system-for-write (or gnus-newsgroup-charset
4270                                                  'iso-8859-1)))
4271                 (funcall (mml2015-clear-verify-function)))
4272               (setq info
4273                     (or (mm-handle-multipart-ctl-parameter
4274                          mm-security-handle 'gnus-details)
4275                         (mm-handle-multipart-ctl-parameter
4276                          mm-security-handle 'gnus-info)))))
4277           (when info
4278             (let ((inhibit-read-only t) bface eface)
4279               (save-restriction
4280                 (message-narrow-to-head)
4281                 (goto-char (point-max))
4282                 (forward-line -1)
4283                 (setq bface (get-text-property (point-at-bol) 'face)
4284                       eface (get-text-property (1- (point-at-eol)) 'face))
4285                 (message-remove-header "X-Gnus-PGP-Verify")
4286                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4287                     (forward-line)
4288                   (goto-char (point-max)))
4289                 (narrow-to-region (point) (point))
4290                 (insert "X-Gnus-PGP-Verify: " info "\n")
4291                 (goto-char (point-min))
4292                 (forward-line)
4293                 (while (not (eobp))
4294                   (if (not (looking-at "^[ \t]"))
4295                       (insert " "))
4296                   (forward-line))
4297                 ;; Do highlighting.
4298                 (goto-char (point-min))
4299                 (when (looking-at "\\([^:]+\\): *")
4300                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4301                                      'face bface)
4302                   (put-text-property (match-end 0) (point-max)
4303                                      'face eface)))))))))
4304
4305 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4306
4307 (defun article-verify-cancel-lock ()
4308   "Verify Cancel-Lock header."
4309   (interactive)
4310   (if (gnus-buffer-live-p gnus-original-article-buffer)
4311       (canlock-verify gnus-original-article-buffer)))
4312
4313 (eval-and-compile
4314   (mapc
4315    (lambda (func)
4316      (let (afunc gfunc)
4317        (if (consp func)
4318            (setq afunc (car func)
4319                  gfunc (cdr func))
4320          (setq afunc func
4321                gfunc (intern (format "gnus-%s" func))))
4322        (defalias gfunc
4323          (when (fboundp afunc)
4324            `(lambda (&optional interactive &rest args)
4325               ,(documentation afunc t)
4326               (interactive (list t))
4327               (with-current-buffer gnus-article-buffer
4328                 (if interactive
4329                     (call-interactively ',afunc)
4330                   (apply ',afunc args))))))))
4331    '(article-hide-headers
4332      article-verify-x-pgp-sig
4333      article-verify-cancel-lock
4334      article-hide-boring-headers
4335      article-treat-overstrike
4336      article-treat-ansi-sequences
4337      article-fill-long-lines
4338      article-capitalize-sentences
4339      article-remove-cr
4340      article-remove-leading-whitespace
4341      article-display-x-face
4342      article-display-face
4343      article-de-quoted-unreadable
4344      article-de-base64-unreadable
4345      article-decode-HZ
4346      article-wash-html
4347      article-unsplit-urls
4348      article-hide-list-identifiers
4349      article-strip-banner
4350      article-babel
4351      article-hide-pem
4352      article-hide-signature
4353      article-strip-headers-in-body
4354      article-remove-trailing-blank-lines
4355      article-strip-leading-blank-lines
4356      article-strip-multiple-blank-lines
4357      article-strip-leading-space
4358      article-strip-trailing-space
4359      article-strip-blank-lines
4360      article-strip-all-blank-lines
4361      article-date-local
4362      article-date-english
4363      article-date-iso8601
4364      article-date-original
4365      article-treat-date
4366      article-date-ut
4367      article-decode-mime-words
4368      article-decode-charset
4369      article-decode-encoded-words
4370      article-date-user
4371      article-date-lapsed
4372      article-date-combined-lapsed
4373      article-emphasize
4374      article-treat-dumbquotes
4375      article-treat-non-ascii
4376      article-normalize-headers
4377      ;;(article-show-all . gnus-article-show-all-headers)
4378      )))
4379 \f
4380 ;;;
4381 ;;; Gnus article mode
4382 ;;;
4383
4384 (put 'gnus-article-mode 'mode-class 'special)
4385
4386 (set-keymap-parent gnus-article-mode-map widget-keymap)
4387
4388 (gnus-define-keys gnus-article-mode-map
4389   " " gnus-article-goto-next-page
4390   "\177" gnus-article-goto-prev-page
4391   [delete] gnus-article-goto-prev-page
4392   [backspace] gnus-article-goto-prev-page
4393   "\C-c^" gnus-article-refer-article
4394   "h" gnus-article-show-summary
4395   "s" gnus-article-show-summary
4396   "\C-c\C-m" gnus-article-mail
4397   "?" gnus-article-describe-briefly
4398   "<" beginning-of-buffer
4399   ">" end-of-buffer
4400   "\C-c\C-i" gnus-info-find-node
4401   "\C-c\C-b" gnus-bug
4402   "R" gnus-article-reply-with-original
4403   "F" gnus-article-followup-with-original
4404   "\C-hk" gnus-article-describe-key
4405   "\C-hc" gnus-article-describe-key-briefly
4406   "\C-hb" gnus-article-describe-bindings
4407
4408   "e" gnus-article-read-summary-keys
4409   "\C-d" gnus-article-read-summary-keys
4410   "\M-*" gnus-article-read-summary-keys
4411   "\M-#" gnus-article-read-summary-keys
4412   "\M-^" gnus-article-read-summary-keys
4413   "\M-g" gnus-article-read-summary-keys)
4414
4415 (substitute-key-definition
4416  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4417
4418 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4419   "W" gnus-article-wide-reply-with-original)
4420 (if (featurep 'xemacs)
4421     (set-keymap-default-binding gnus-article-send-map
4422                                 'gnus-article-read-summary-send-keys)
4423   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4424
4425 (defun gnus-article-make-menu-bar ()
4426   (unless (boundp 'gnus-article-commands-menu)
4427     (gnus-summary-make-menu-bar))
4428   (unless (boundp 'gnus-article-article-menu)
4429     (easy-menu-define
4430      gnus-article-article-menu gnus-article-mode-map ""
4431      '("Article"
4432        ["Scroll forwards" gnus-article-goto-next-page t]
4433        ["Scroll backwards" gnus-article-goto-prev-page t]
4434        ["Show summary" gnus-article-show-summary t]
4435        ["Fetch Message-ID at point" gnus-article-refer-article t]
4436        ["Mail to address at point" gnus-article-mail t]
4437        ["Send a bug report" gnus-bug t]))
4438
4439     (easy-menu-define
4440      gnus-article-treatment-menu gnus-article-mode-map ""
4441      ;; Fixme: this should use :active (and maybe :visible).
4442      '("Treatment"
4443        ["Hide headers" gnus-article-hide-headers t]
4444        ["Hide signature" gnus-article-hide-signature t]
4445        ["Hide citation" gnus-article-hide-citation t]
4446        ["Treat overstrike" gnus-article-treat-overstrike t]
4447        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4448        ["Remove carriage return" gnus-article-remove-cr t]
4449        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4450        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4451        ["Remove base64" gnus-article-de-base64-unreadable t]
4452        ["Treat html" gnus-article-wash-html t]
4453        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4454        ["Decode HZ" gnus-article-decode-HZ t]))
4455
4456     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4457
4458     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4459
4460     (gnus-run-hooks 'gnus-article-menu-hook)))
4461
4462 (defvar bookmark-make-record-function)
4463 (defvar shr-put-image-function)
4464
4465 (defun gnus-article-mode ()
4466   "Major mode for displaying an article.
4467
4468 All normal editing commands are switched off.
4469
4470 The following commands are available in addition to all summary mode
4471 commands:
4472 \\<gnus-article-mode-map>
4473 \\[gnus-article-next-page]\t Scroll the article one page forwards
4474 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4475 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4476 \\[gnus-article-show-summary]\t Display the summary buffer
4477 \\[gnus-article-mail]\t Send a reply to the address near point
4478 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4479 \\[gnus-info-find-node]\t Go to the Gnus info node"
4480   (interactive)
4481   (kill-all-local-variables)
4482   (gnus-simplify-mode-line)
4483   (setq mode-name "Article")
4484   (setq major-mode 'gnus-article-mode)
4485   (make-local-variable 'minor-mode-alist)
4486   (use-local-map gnus-article-mode-map)
4487   (when (gnus-visual-p 'article-menu 'menu)
4488     (gnus-article-make-menu-bar)
4489     (when gnus-summary-tool-bar-map
4490       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4491   (gnus-update-format-specifications nil 'article-mode)
4492   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4493   (set (make-local-variable 'gnus-page-broken) nil)
4494   (make-local-variable 'gnus-article-current-summary)
4495   (make-local-variable 'gnus-article-mime-handles)
4496   (make-local-variable 'gnus-article-decoded-p)
4497   (make-local-variable 'gnus-article-mime-handle-alist)
4498   (make-local-variable 'gnus-article-wash-types)
4499   (make-local-variable 'gnus-article-image-alist)
4500   (make-local-variable 'gnus-article-charset)
4501   (make-local-variable 'gnus-article-ignored-charsets)
4502   (set (make-local-variable 'bookmark-make-record-function)
4503        'gnus-summary-bookmark-make-record)
4504   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4505   ;; face.
4506   (set (make-local-variable 'nobreak-char-display) nil)
4507   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4508   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4509   (setq cursor-in-non-selected-windows nil)
4510   (gnus-set-default-directory)
4511   (buffer-disable-undo)
4512   (setq buffer-read-only t
4513         show-trailing-whitespace nil)
4514   (set-syntax-table gnus-article-mode-syntax-table)
4515   (mm-enable-multibyte)
4516   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4517
4518 (defun gnus-article-setup-buffer ()
4519   "Initialize the article buffer."
4520   (let* ((name (if gnus-single-article-buffer "*Article*"
4521                  (concat "*Article "
4522                          (gnus-group-decoded-name gnus-newsgroup-name)
4523                          "*")))
4524          (original
4525           (progn (string-match "\\*Article" name)
4526                  (concat " *Original Article"
4527                          (substring name (match-end 0))))))
4528     (setq gnus-article-buffer name)
4529     (setq gnus-original-article-buffer original)
4530     (setq gnus-article-mime-handle-alist nil)
4531     (with-current-buffer gnus-summary-buffer
4532       ;; This might be a variable local to the summary buffer.
4533       (unless gnus-single-article-buffer
4534         (setq gnus-article-buffer name)
4535         (setq gnus-original-article-buffer original)
4536         (gnus-set-global-variables)))
4537     (gnus-article-setup-highlight-words)
4538     ;; Init original article buffer.
4539     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4540       (mm-enable-multibyte)
4541       (setq major-mode 'gnus-original-article-mode)
4542       (make-local-variable 'gnus-original-article))
4543     (if (and (get-buffer name)
4544              (with-current-buffer name
4545                (if gnus-article-edit-mode
4546                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4547                        (progn
4548                          (set-buffer-modified-p nil)
4549                          (gnus-kill-buffer name)
4550                          (message "")
4551                          nil)
4552                      (error "Action aborted"))
4553                  t)))
4554         (with-current-buffer name
4555           (set (make-local-variable 'gnus-article-edit-mode) nil)
4556           (gnus-article-stop-animations)
4557           (when gnus-article-mime-handles
4558             (mm-destroy-parts gnus-article-mime-handles)
4559             (setq gnus-article-mime-handles nil))
4560           ;; Set it to nil in article-buffer!
4561           (setq gnus-article-mime-handle-alist nil)
4562           (buffer-disable-undo)
4563           (setq buffer-read-only t)
4564           (unless (eq major-mode 'gnus-article-mode)
4565             (gnus-article-mode))
4566           (setq truncate-lines gnus-article-truncate-lines)
4567           (current-buffer))
4568       (with-current-buffer (gnus-get-buffer-create name)
4569         (gnus-article-mode)
4570         (setq truncate-lines gnus-article-truncate-lines)
4571         (make-local-variable 'gnus-summary-buffer)
4572         (setq gnus-summary-buffer
4573               (gnus-summary-buffer-name gnus-newsgroup-name))
4574         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4575         (when article-lapsed-timer
4576           (gnus-stop-date-timer))
4577         (when gnus-article-update-date-headers
4578           (gnus-start-date-timer gnus-article-update-date-headers))
4579         (current-buffer)))))
4580
4581 (defun gnus-article-stop-animations ()
4582   (dolist (timer (and (boundp 'timer-list)
4583                       timer-list))
4584     (when (eq (elt timer 5) 'image-animate-timeout)
4585       (cancel-timer timer))))
4586
4587 (defun gnus-stop-downloads ()
4588   (when (boundp 'url-queue)
4589     (set (intern "url-queue" obarray) nil)))
4590
4591 ;; Set article window start at LINE, where LINE is the number of lines
4592 ;; from the head of the article.
4593 (defun gnus-article-set-window-start (&optional line)
4594   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4595     (when article-window
4596       (set-window-start
4597        article-window
4598        (with-current-buffer gnus-article-buffer
4599          (goto-char (point-min))
4600          (if (not line)
4601              (point-min)
4602            (gnus-message 6 "Moved to bookmark")
4603            (search-forward "\n\n" nil t)
4604            (forward-line line)
4605            (point)))))))
4606
4607 (defun gnus-article-prepare (article &optional all-headers header)
4608   "Prepare ARTICLE in article mode buffer.
4609 ARTICLE should either be an article number or a Message-ID.
4610 If ARTICLE is an id, HEADER should be the article headers.
4611 If ALL-HEADERS is non-nil, no headers are hidden."
4612   (save-excursion
4613     ;; Make sure we start in a summary buffer.
4614     (unless (eq major-mode 'gnus-summary-mode)
4615       (set-buffer gnus-summary-buffer))
4616     (setq gnus-summary-buffer (current-buffer))
4617     (let* ((gnus-article (if header (mail-header-number header) article))
4618            (summary-buffer (current-buffer))
4619            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4620            (group gnus-newsgroup-name)
4621            result)
4622       (save-excursion
4623         (gnus-article-setup-buffer)
4624         (set-buffer gnus-article-buffer)
4625         ;; Deactivate active regions.
4626         (when (and (boundp 'transient-mark-mode)
4627                    transient-mark-mode)
4628           (setq mark-active nil))
4629         (if (not (setq result (let ((inhibit-read-only t))
4630                                 (gnus-request-article-this-buffer
4631                                  article group))))
4632             ;; There is no such article.
4633             (save-excursion
4634               (when (and (numberp article)
4635                          (not (memq article gnus-newsgroup-sparse)))
4636                 (setq gnus-article-current
4637                       (cons gnus-newsgroup-name article))
4638                 (set-buffer gnus-summary-buffer)
4639                 (setq gnus-current-article article)
4640                 (if (and (memq article gnus-newsgroup-undownloaded)
4641                          (not (gnus-online (gnus-find-method-for-group
4642                                             gnus-newsgroup-name))))
4643                     (progn
4644                       (gnus-summary-set-agent-mark article)
4645                       (message "Message marked for downloading"))
4646                   (gnus-summary-mark-article article gnus-canceled-mark)
4647                   (unless (memq article gnus-newsgroup-sparse)
4648                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4649           (if (or (eq result 'pseudo)
4650                   (eq result 'nneething))
4651               (progn
4652                 (with-current-buffer summary-buffer
4653                   (push article gnus-newsgroup-history)
4654                   (setq gnus-last-article gnus-current-article
4655                         gnus-current-article 0
4656                         gnus-current-headers nil
4657                         gnus-article-current nil)
4658                   (if (eq result 'nneething)
4659                       (gnus-configure-windows 'summary)
4660                     (gnus-configure-windows 'article))
4661                   (gnus-set-global-variables))
4662                 (let ((gnus-article-mime-handle-alist-1
4663                        gnus-article-mime-handle-alist))
4664                   (gnus-set-mode-line 'article)))
4665             ;; The result from the `request' was an actual article -
4666             ;; or at least some text that is now displayed in the
4667             ;; article buffer.
4668             (when (and (numberp article)
4669                        (not (eq article gnus-current-article)))
4670               ;; Seems like a new article has been selected.
4671               ;; `gnus-current-article' must be an article number.
4672               (with-current-buffer summary-buffer
4673                 (push article gnus-newsgroup-history)
4674                 (setq gnus-last-article gnus-current-article
4675                       gnus-current-article article
4676                       gnus-current-headers
4677                       (gnus-summary-article-header gnus-current-article)
4678                       gnus-article-current
4679                       (cons gnus-newsgroup-name gnus-current-article))
4680                 (unless (vectorp gnus-current-headers)
4681                   (setq gnus-current-headers nil))
4682                 (gnus-summary-goto-subject gnus-current-article)
4683                 (when (gnus-summary-show-thread)
4684                   ;; If the summary buffer really was folded, the
4685                   ;; previous goto may not actually have gone to
4686                   ;; the right article, but the thread root instead.
4687                   ;; So we go again.
4688                   (gnus-summary-goto-subject gnus-current-article))
4689                 (gnus-run-hooks 'gnus-mark-article-hook)
4690                 (gnus-set-mode-line 'summary)
4691                 (when (gnus-visual-p 'article-highlight 'highlight)
4692                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4693                 ;; Set the global newsgroup variables here.
4694                 (gnus-set-global-variables)
4695                 (setq gnus-have-all-headers
4696                       (or all-headers gnus-show-all-headers))))
4697             (save-excursion
4698               (gnus-configure-windows 'article))
4699             (when (or (numberp article)
4700                       (stringp article))
4701               (gnus-article-prepare-display)
4702               ;; Do page break.
4703               (goto-char (point-min))
4704               (when gnus-break-pages
4705                 (gnus-narrow-to-page)))
4706             (let ((gnus-article-mime-handle-alist-1
4707                    gnus-article-mime-handle-alist))
4708               (gnus-set-mode-line 'article))
4709             (article-goto-body)
4710             (unless (bobp)
4711               (forward-line -1))
4712             (set-window-point (get-buffer-window (current-buffer)) (point))
4713             (gnus-configure-windows 'article)
4714             (gnus-run-hooks 'gnus-article-prepare-hook)
4715             t))))))
4716
4717 ;;;###autoload
4718 (defun gnus-article-prepare-display ()
4719   "Make the current buffer look like a nice article."
4720   ;; Hooks for getting information from the article.
4721   ;; This hook must be called before being narrowed.
4722   (let ((gnus-article-buffer (current-buffer))
4723         buffer-read-only
4724         (inhibit-read-only t))
4725     (unless (eq major-mode 'gnus-article-mode)
4726       (gnus-article-mode))
4727     (setq buffer-read-only nil
4728           gnus-article-wash-types nil
4729           gnus-article-image-alist nil)
4730     (gnus-run-hooks 'gnus-tmp-internal-hook)
4731     (when gnus-display-mime-function
4732       (funcall gnus-display-mime-function))))
4733
4734 ;;;
4735 ;;; Gnus Sticky Article Mode
4736 ;;;
4737
4738 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4739   "Mode for sticky articles."
4740   ;; Release bindings that won't work.
4741   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4742                              gnus-sticky-article-mode-map)
4743   (substitute-key-definition 'gnus-article-refer-article 'undefined
4744                              gnus-sticky-article-mode-map)
4745   (dolist (k '("e" "h" "s" "F" "R"))
4746     (define-key gnus-sticky-article-mode-map k nil))
4747   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4748   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4749   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4750   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4751
4752 (defun gnus-sticky-article (arg)
4753   "Make the current article sticky.
4754 If a prefix ARG is given, ask for a name for this sticky article buffer."
4755   (interactive "P")
4756   (gnus-summary-show-thread)
4757   (gnus-summary-select-article nil nil 'pseudo)
4758   (let (new-art-buf-name)
4759     (gnus-eval-in-buffer-window gnus-article-buffer
4760       (setq new-art-buf-name
4761             (concat
4762              "*Sticky Article: "
4763              (if arg
4764                  (read-from-minibuffer "Sticky article buffer name: ")
4765                (gnus-with-article-headers
4766                  (gnus-article-goto-header "subject")
4767                  (setq new-art-buf-name
4768                        (buffer-substring-no-properties
4769                         (line-beginning-position) (line-end-position)))
4770                  (goto-char (point-min))
4771                  (gnus-article-goto-header "from")
4772                  (setq new-art-buf-name
4773                        (concat
4774                         new-art-buf-name ", "
4775                         (buffer-substring-no-properties
4776                          (line-beginning-position) (line-end-position))))
4777                  (goto-char (point-min))
4778                  (gnus-article-goto-header "date")
4779                  (setq new-art-buf-name
4780                        (concat
4781                         new-art-buf-name ", "
4782                         (buffer-substring-no-properties
4783                          (line-beginning-position) (line-end-position))))))
4784              "*"))
4785       (if (and (gnus-buffer-live-p new-art-buf-name)
4786                (with-current-buffer new-art-buf-name
4787                  (eq major-mode 'gnus-sticky-article-mode)))
4788           (switch-to-buffer new-art-buf-name)
4789         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4790       (gnus-sticky-article-mode))
4791     (setq gnus-article-buffer new-art-buf-name))
4792   (gnus-summary-recenter)
4793   (gnus-summary-position-point))
4794
4795 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4796   "Kill the given sticky article BUFFER.
4797 If none is given, assume the current buffer and kill it if it has
4798 `gnus-sticky-article-mode'."
4799   (interactive)
4800   (unless buffer
4801     (setq buffer (current-buffer)))
4802   (with-current-buffer buffer
4803     (when (eq major-mode 'gnus-sticky-article-mode)
4804       (gnus-kill-buffer buffer))))
4805
4806 (defun gnus-kill-sticky-article-buffers (arg)
4807   "Kill all sticky article buffers.
4808 If a prefix ARG is given, ask for confirmation."
4809   (interactive "P")
4810   (dolist (buf (gnus-buffers))
4811     (with-current-buffer buf
4812       (when (eq major-mode 'gnus-sticky-article-mode)
4813         (if (not arg)
4814             (gnus-kill-buffer buf)
4815           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4816             (gnus-kill-buffer buf)))))))
4817
4818 ;;;
4819 ;;; Gnus MIME viewing functions
4820 ;;;
4821
4822 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4823   "Format of the MIME buttons.
4824
4825 Valid specifiers include:
4826 %t  The MIME type
4827 %T  MIME type, along with additional info
4828 %n  The `name' parameter
4829 %d  The description, if any
4830 %l  The length of the encoded part
4831 %p  The part identifier number
4832 %e  Dots if the part isn't displayed
4833
4834 General format specifiers can also be used.  See Info node
4835 `(gnus)Formatting Variables'.")
4836
4837 (defvar gnus-mime-button-line-format-alist
4838   '((?t gnus-tmp-type ?s)
4839     (?T gnus-tmp-type-long ?s)
4840     (?n gnus-tmp-name ?s)
4841     (?d gnus-tmp-description ?s)
4842     (?p gnus-tmp-id ?s)
4843     (?l gnus-tmp-length ?d)
4844     (?e gnus-tmp-dots ?s)))
4845
4846 (defvar gnus-mime-button-commands
4847   '((gnus-article-press-button "\r" "Toggle Display")
4848     (gnus-mime-view-part "v" "View Interactively...")
4849     (gnus-mime-view-part-as-type "t" "View As Type...")
4850     (gnus-mime-view-part-as-charset "C" "View As charset...")
4851     (gnus-mime-save-part "o" "Save...")
4852     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4853     (gnus-mime-replace-part "r" "Replace part")
4854     (gnus-mime-delete-part "d" "Delete part")
4855     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4856     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4857     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4858     (gnus-mime-view-part-externally "e" "View Externally")
4859     (gnus-mime-print-part "p" "Print")
4860     (gnus-mime-pipe-part "|" "Pipe To Command...")
4861     (gnus-mime-action-on-part "." "Take action on the part...")))
4862
4863 (defun gnus-article-mime-part-status ()
4864   (if gnus-article-mime-handle-alist-1
4865       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4866           " (1 part)"
4867         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4868     ""))
4869
4870 (defvar gnus-mime-button-map
4871   (let ((map (make-sparse-keymap)))
4872     (define-key map gnus-mouse-2 'gnus-article-push-button)
4873     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4874     (dolist (c gnus-mime-button-commands)
4875       (define-key map (cadr c) (car c)))
4876     map))
4877
4878 (easy-menu-define
4879   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4880   `("MIME Part"
4881     ,@(mapcar (lambda (c)
4882                 (vector (caddr c) (car c) :active t))
4883               gnus-mime-button-commands)))
4884
4885 (defvar gnus-url-button-commands
4886   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4887
4888 (defvar gnus-url-button-map
4889   (let ((map (make-sparse-keymap)))
4890     (dolist (c gnus-url-button-commands)
4891       (define-key map (cadr c) (car c)))
4892     map))
4893
4894 (easy-menu-define
4895   gnus-url-button-menu gnus-url-button-map "URL button menu."
4896   `("Url Button"
4897     ,@(mapcar (lambda (c)
4898                 (vector (caddr c) (car c) :active t))
4899               gnus-url-button-commands)))
4900
4901 (defmacro gnus-bind-safe-url-regexp (&rest body)
4902   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4903   `(let ((mm-w3m-safe-url-regexp
4904           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4905                                 (gnus-buffer-live-p
4906                                  gnus-article-current-summary))
4907                            (with-current-buffer gnus-article-current-summary
4908                              gnus-newsgroup-name)
4909                          gnus-newsgroup-name)))
4910             (if (cond ((not group)
4911                        ;; Maybe we're in a mml-preview buffer
4912                        ;; and no group is selected.
4913                        t)
4914                       ((stringp gnus-safe-html-newsgroups)
4915                        (string-match gnus-safe-html-newsgroups group))
4916                       ((consp gnus-safe-html-newsgroups)
4917                        (member group gnus-safe-html-newsgroups)))
4918                 nil
4919               mm-w3m-safe-url-regexp))))
4920      ,@body))
4921
4922 (defun gnus-mime-button-menu (event prefix)
4923  "Construct a context-sensitive menu of MIME commands."
4924  (interactive "e\nP")
4925  (save-window-excursion
4926    (let ((pos (event-start event)))
4927      (select-window (posn-window pos))
4928      (goto-char (posn-point pos))
4929      (gnus-article-check-buffer)
4930      (popup-menu gnus-mime-button-menu nil prefix))))
4931
4932 (defun gnus-mime-view-all-parts (&optional handles)
4933   "View all the MIME parts."
4934   (interactive)
4935   (with-current-buffer gnus-article-buffer
4936     (let ((handles (or handles gnus-article-mime-handles))
4937           (mail-parse-charset gnus-newsgroup-charset)
4938           (mail-parse-ignored-charsets
4939            (with-current-buffer gnus-summary-buffer
4940              gnus-newsgroup-ignored-charsets)))
4941       (when handles
4942         (mm-remove-parts handles)
4943         (goto-char (point-min))
4944         (or (search-forward "\n\n") (goto-char (point-max)))
4945         (let ((inhibit-read-only t))
4946           (delete-region (point) (point-max))
4947           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4948
4949 (defun gnus-article-jump-to-part (n)
4950   "Jump to MIME part N."
4951   (interactive "P")
4952   (let ((parts (with-current-buffer gnus-article-buffer
4953                  (length gnus-article-mime-handle-alist))))
4954     (when (zerop parts)
4955       (error "No such part"))
4956     (pop-to-buffer gnus-article-buffer)
4957     (or n
4958         (setq n (if (= parts 1)
4959                     1
4960                   (read-number (format "Jump to part (1..%s): " parts)))))
4961     (unless (and (integerp n) (<= n parts) (>= n 1))
4962       (setq n
4963             (progn
4964               (gnus-message 7 "Invalid part `%s', using %s instead."
4965                             n parts)
4966               parts)))
4967     (gnus-message 9 "Jumping to part %s." n)
4968     (cond ((>= gnus-auto-select-part 1)
4969            (while (and (<= n parts)
4970                        (not (gnus-article-goto-part n)))
4971              (setq n (1+ n))))
4972           ((< gnus-auto-select-part 0)
4973            (while (and (>= n 1)
4974                        (not (gnus-article-goto-part n)))
4975              (setq n (1- n))))
4976           (t
4977            (gnus-article-goto-part n)))))
4978
4979 (defvar gnus-mime-buttonized-part-id nil
4980   "ID of a mime part that should be buttonized.
4981 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4982
4983 (eval-when-compile
4984   (defsubst gnus-article-edit-part (handles &optional current-id)
4985     "Edit an article in order to delete a mime part.
4986 This function is exclusively used by `gnus-mime-save-part-and-strip'
4987 and `gnus-mime-delete-part', and not provided at run-time normally."
4988     (gnus-article-edit-article
4989      `(lambda ()
4990         (buffer-disable-undo)
4991         (erase-buffer)
4992         (let ((mail-parse-charset (or gnus-article-charset
4993                                       ',gnus-newsgroup-charset))
4994               (mail-parse-ignored-charsets
4995                (or gnus-article-ignored-charsets
4996                    ',gnus-newsgroup-ignored-charsets))
4997               (mbl mml-buffer-list))
4998           (setq mml-buffer-list nil)
4999           (insert-buffer-substring gnus-original-article-buffer)
5000           (mime-to-mml ',handles)
5001           (setq gnus-article-mime-handles nil)
5002           (let ((mbl1 mml-buffer-list))
5003             (setq mml-buffer-list mbl)
5004             (set (make-local-variable 'mml-buffer-list) mbl1))
5005           (gnus-make-local-hook 'kill-buffer-hook)
5006           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5007      `(lambda (no-highlight)
5008         (let ((mail-parse-charset (or gnus-article-charset
5009                                       ',gnus-newsgroup-charset))
5010               (message-options message-options)
5011               (message-options-set-recipient)
5012               (mail-parse-ignored-charsets
5013                (or gnus-article-ignored-charsets
5014                    ',gnus-newsgroup-ignored-charsets)))
5015           (mml-to-mime)
5016           (mml-destroy-buffers)
5017           (remove-hook 'kill-buffer-hook
5018                        'mml-destroy-buffers t)
5019           (kill-local-variable 'mml-buffer-list))
5020         (gnus-summary-edit-article-done
5021          ,(or (mail-header-references gnus-current-headers) "")
5022          ,(gnus-group-read-only-p)
5023          ,gnus-summary-buffer no-highlight))
5024      t)
5025     ;; Force buttonizing this part.
5026     (let ((gnus-mime-buttonized-part-id current-id))
5027       (gnus-article-edit-done))
5028     (gnus-configure-windows 'article)
5029     (when (and current-id (integerp gnus-auto-select-part))
5030       (gnus-article-jump-to-part
5031        (min (max (+ current-id gnus-auto-select-part) 1)
5032             (with-current-buffer gnus-article-buffer
5033               (length gnus-article-mime-handle-alist)))))))
5034
5035 (defun gnus-mime-replace-part (file)
5036   "Replace MIME part under point with an external body."
5037   ;; Useful if file has already been saved to disk
5038   (interactive
5039    (list
5040     (read-file-name "Replace MIME part with file: "
5041                     (or mm-default-directory default-directory)
5042                     nil nil)))
5043   (gnus-mime-save-part-and-strip file))
5044
5045 (defun gnus-mime-save-part-and-strip (&optional file)
5046   "Save the MIME part under point then replace it with an external body.
5047 If FILE is given, use it for the external part."
5048   (interactive)
5049   (gnus-article-check-buffer)
5050   (when (gnus-group-read-only-p)
5051     (error "The current group does not support deleting of parts"))
5052   (when (mm-complicated-handles gnus-article-mime-handles)
5053     (error "\
5054 The current article has a complicated MIME structure, giving up..."))
5055   (let* ((data (get-text-property (point) 'gnus-data))
5056          (id (get-text-property (point) 'gnus-part))
5057          param
5058          (handles gnus-article-mime-handles))
5059     (unless file
5060       (setq file
5061             (and data (mm-save-part data "Delete MIME part and save to: "))))
5062     (when file
5063       (with-current-buffer (mm-handle-buffer data)
5064         (erase-buffer)
5065         (insert "Content-Type: " (mm-handle-media-type data))
5066         (mml-insert-parameter-string (cdr (mm-handle-type data))
5067                                      '(charset))
5068         ;; Add a filename for the sake of saving the part again.
5069         (mml-insert-parameter
5070          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5071         (insert "\n")
5072         (insert "Content-ID: " (message-make-message-id) "\n")
5073         (insert "Content-Transfer-Encoding: binary\n")
5074         (insert "\n"))
5075       (setcdr data
5076               (cdr (mm-make-handle nil
5077                                    `("message/external-body"
5078                                      (access-type . "LOCAL-FILE")
5079                                      (name . ,file)))))
5080       ;; (set-buffer gnus-summary-buffer)
5081       (gnus-article-edit-part handles id))))
5082
5083 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5084 ;; parts...>') but with stripping would be nice.
5085
5086 (defun gnus-mime-delete-part ()
5087   "Delete the MIME part under point.
5088 Replace it with some information about the removed part."
5089   (interactive)
5090   (gnus-article-check-buffer)
5091   (when (gnus-group-read-only-p)
5092     (error "The current group does not support deleting of parts"))
5093   (when (mm-complicated-handles gnus-article-mime-handles)
5094     (error "\
5095 The current article has a complicated MIME structure, giving up..."))
5096   (when (or gnus-expert-user
5097             (gnus-yes-or-no-p "\
5098 Deleting parts may malfunction or destroy the article; continue? "))
5099     (let* ((data (get-text-property (point) 'gnus-data))
5100            (id (get-text-property (point) 'gnus-part))
5101            (handles gnus-article-mime-handles)
5102            (description
5103             (let ((desc (mm-handle-description data)))
5104               (when desc
5105                 (mail-decode-encoded-word-string desc))))
5106            (filename (or (mm-handle-filename data) "(none)"))
5107            (type (mm-handle-media-type data)))
5108       (unless data
5109         (error "No MIME part under point"))
5110       (with-current-buffer (mm-handle-buffer data)
5111         (let ((bsize (buffer-size)))
5112           (erase-buffer)
5113           (insert
5114            (concat
5115             ",----\n"
5116             "| The following attachment has been deleted:\n"
5117             "|\n"
5118             "| Type:           " type "\n"
5119             "| Filename:       " filename "\n"
5120             "| Size (encoded): " (format "%s byte%s\n"
5121                                          bsize (if (= bsize 1)
5122                                                    ""
5123                                                  "s"))
5124             (when description
5125               (concat    "| Description:    " description "\n"))
5126             "`----\n"))
5127           (setcdr data
5128                   (cdr (mm-make-handle
5129                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5130                         (list "attachment")
5131                         (format "Deleted attachment (%s bytes)" bsize))))))
5132       ;; (set-buffer gnus-summary-buffer)
5133       (gnus-article-edit-part handles id))))
5134
5135 (defun gnus-mime-save-part ()
5136   "Save the MIME part under point."
5137   (interactive)
5138   (gnus-article-check-buffer)
5139   (let ((data (get-text-property (point) 'gnus-data)))
5140     (when data
5141       (mm-save-part data))))
5142
5143 (defun gnus-mime-pipe-part (&optional cmd)
5144   "Pipe the MIME part under point to a process.
5145 Use CMD as the process."
5146   (interactive)
5147   (gnus-article-check-buffer)
5148   (let ((data (get-text-property (point) 'gnus-data)))
5149     (when data
5150       (mm-pipe-part data cmd))))
5151
5152 (defun gnus-mime-view-part ()
5153   "Interactively choose a viewing method for the MIME part under point."
5154   (interactive)
5155   (gnus-article-check-buffer)
5156   (let ((data (get-text-property (point) 'gnus-data)))
5157     (when data
5158       (setq gnus-article-mime-handles
5159             (mm-merge-handles
5160              gnus-article-mime-handles (setq data (copy-sequence data))))
5161       (mm-interactively-view-part data))))
5162
5163 (defun gnus-mime-view-part-as-type-internal ()
5164   (gnus-article-check-buffer)
5165   (let* ((handle (get-text-property (point) 'gnus-data))
5166          (name (or
5167                 ;; Content-Type: foo/bar; name=...
5168                 (mail-content-type-get (mm-handle-type handle) 'name)
5169                 ;; Content-Disposition: attachment; filename=...
5170                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5171          (def-type (and name (mm-default-file-encoding name))))
5172     (or (and def-type (cons def-type 0))
5173         (and handle
5174              (equal (mm-handle-media-supertype handle) "text")
5175              '("text/plain" . 0))
5176         '("application/octet-stream" . 0))))
5177
5178 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5179   "Choose a MIME media type, and view the part as such.
5180 If non-nil, PRED is a predicate to use during completion to limit the
5181 available media-types."
5182   (interactive)
5183   (unless mime-type
5184     (setq mime-type
5185           (let ((default (gnus-mime-view-part-as-type-internal)))
5186             (gnus-completing-read
5187              "View as MIME type"
5188              (if pred
5189                  (gnus-remove-if-not pred (mailcap-mime-types))
5190                (mailcap-mime-types))
5191              nil nil nil
5192              (car default)))))
5193   (gnus-article-check-buffer)
5194   (let ((handle (get-text-property (point) 'gnus-data)))
5195     (when handle
5196       (when (equal (mm-handle-media-type handle) "message/external-body")
5197         (unless (mm-handle-cache handle)
5198           (mm-extern-cache-contents handle))
5199         (setq handle (mm-handle-cache handle)))
5200       (setq handle
5201             (mm-make-handle (mm-handle-buffer handle)
5202                             (cons mime-type (cdr (mm-handle-type handle)))
5203                             (mm-handle-encoding handle)
5204                             (mm-handle-undisplayer handle)
5205                             (mm-handle-disposition handle)
5206                             (mm-handle-description handle)
5207                             nil
5208                             (mm-handle-id handle)))
5209       (setq gnus-article-mime-handles
5210             (mm-merge-handles gnus-article-mime-handles handle))
5211       (when (mm-handle-displayed-p handle)
5212         (mm-remove-part handle))
5213       (gnus-mm-display-part handle))))
5214
5215 (defun gnus-mime-copy-part (&optional handle arg)
5216   "Put the MIME part under point into a new buffer.
5217 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5218 are decompressed."
5219   (interactive (list nil current-prefix-arg))
5220   (gnus-article-check-buffer)
5221   (unless handle
5222     (setq handle (get-text-property (point) 'gnus-data)))
5223   (when handle
5224     (let ((filename (mm-handle-filename handle))
5225           contents dont-decode charset coding-system)
5226       (mm-with-unibyte-buffer
5227         (mm-insert-part handle)
5228         (setq contents (or (condition-case nil
5229                                (mm-decompress-buffer filename nil 'sig)
5230                              (error
5231                               (setq dont-decode t)
5232                               nil))
5233                            (buffer-string))))
5234       (setq filename (cond (filename (file-name-nondirectory filename))
5235                            (dont-decode "*raw data*")
5236                            (t "*decoded*")))
5237       (cond
5238        (dont-decode)
5239        ((not arg)
5240         (unless (setq charset (mail-content-type-get
5241                                (mm-handle-type handle) 'charset))
5242           (unless (setq coding-system (mm-with-unibyte-buffer
5243                                         (insert contents)
5244                                         (mm-find-buffer-file-coding-system)))
5245             (setq charset gnus-newsgroup-charset))))
5246        ((numberp arg)
5247         (setq charset (or (cdr (assq arg
5248                                      gnus-summary-show-article-charset-alist))
5249                           (mm-read-coding-system "Charset: ")))))
5250       (switch-to-buffer (generate-new-buffer filename))
5251       (if (or coding-system
5252               (and charset
5253                    (setq coding-system (mm-charset-to-coding-system charset))
5254                    (not (eq coding-system 'ascii))))
5255           (progn
5256             (mm-enable-multibyte)
5257             (insert (mm-decode-coding-string contents coding-system))
5258             (setq buffer-file-coding-system
5259                   (if (boundp 'last-coding-system-used)
5260                       (symbol-value 'last-coding-system-used)
5261                     coding-system)))
5262         (mm-disable-multibyte)
5263         (insert contents)
5264         (setq buffer-file-coding-system mm-binary-coding-system))
5265       ;; We do it this way to make `normal-mode' set the appropriate mode.
5266       (unwind-protect
5267           (progn
5268             (setq buffer-file-name (expand-file-name filename))
5269             (normal-mode))
5270         (setq buffer-file-name nil))
5271       (goto-char (point-min)))))
5272
5273 (defun gnus-mime-print-part (&optional handle filename)
5274   "Print the MIME part under point."
5275   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5276   (gnus-article-check-buffer)
5277   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5278          (contents (and handle (mm-get-part handle)))
5279          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5280          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5281     (when contents
5282         (if printer
5283             (unwind-protect
5284                 (progn
5285                   (mm-save-part-to-file handle file)
5286                   (call-process shell-file-name nil
5287                                 (generate-new-buffer " *mm*")
5288                                 nil
5289                                 shell-command-switch
5290                                 (mm-mailcap-command
5291                                  printer file (mm-handle-type handle))))
5292               (delete-file file))
5293           (with-temp-buffer
5294             (insert contents)
5295             (gnus-print-buffer))
5296           (ps-despool filename)))))
5297
5298 (defun gnus-mime-inline-part (&optional handle arg)
5299   "Insert the MIME part under point into the current buffer.
5300 Compressed files like .gz and .bz2 are decompressed."
5301   (interactive (list nil current-prefix-arg))
5302   (gnus-article-check-buffer)
5303   (unless handle
5304     (setq handle (get-text-property (point) 'gnus-data)))
5305   (when handle
5306     (let ((b (point))
5307           (inhibit-read-only t)
5308           contents charset coding-system)
5309       (if (and (not arg) (mm-handle-undisplayer handle))
5310           (mm-remove-part handle)
5311         (mm-with-unibyte-buffer
5312           (mm-insert-part handle)
5313           (setq contents
5314                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5315                     (buffer-string))))
5316         (cond
5317          ((not arg)
5318           (unless (setq charset (mail-content-type-get
5319                                  (mm-handle-type handle) 'charset))
5320             (unless (setq coding-system
5321                           (mm-with-unibyte-buffer
5322                             (insert contents)
5323                             (mm-find-buffer-file-coding-system)))
5324               (setq charset gnus-newsgroup-charset))))
5325          ((numberp arg)
5326           (if (mm-handle-undisplayer handle)
5327               (mm-remove-part handle))
5328           (setq charset
5329                 (or (cdr (assq arg
5330                                gnus-summary-show-article-charset-alist))
5331                     (mm-read-coding-system "Charset: "))))
5332          ((mm-handle-undisplayer handle)
5333           (mm-remove-part handle)))
5334         (forward-line 2)
5335         (mm-display-inline handle)
5336         (goto-char b)))))
5337
5338 (defun gnus-mime-set-charset-parameters (handle charset)
5339   "Set CHARSET to parameters in HANDLE.
5340 CHARSET may either be a string or a symbol."
5341   (unless (stringp charset)
5342     (setq charset (symbol-name charset)))
5343   (if (stringp (car handle))
5344       (dolist (h (cdr handle))
5345         (gnus-mime-set-charset-parameters h charset))
5346     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5347                                             "message/external-body")
5348                                      (progn
5349                                        (unless (mm-handle-cache handle)
5350                                          (mm-extern-cache-contents handle))
5351                                        (mm-handle-cache handle))
5352                                    handle)))
5353            (param (assq 'charset (cdr type))))
5354       (if param
5355           (setcdr param charset)
5356         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5357
5358 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5359   "Insert the MIME part under point into the current buffer using the
5360 specified charset."
5361   (interactive (list nil current-prefix-arg))
5362   (gnus-article-check-buffer)
5363   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5364         (fun (get-text-property (point) 'gnus-callback))
5365         (gnus-newsgroup-ignored-charsets 'gnus-all)
5366         charset form preferred parts)
5367     (when handle
5368       (when (prog1
5369                 (and fun
5370                      (setq charset
5371                            (or (cdr (assq
5372                                      arg
5373                                      gnus-summary-show-article-charset-alist))
5374                                (mm-read-coding-system "Charset: "))))
5375               (if (mm-handle-undisplayer handle)
5376                   (mm-remove-part handle)))
5377         (gnus-mime-set-charset-parameters handle charset)
5378         (when (and (consp (setq form (cdr-safe fun)))
5379                    (setq form (ignore-errors
5380                                 (assq 'gnus-mime-display-alternative form)))
5381                    (setq preferred (caddr form))
5382                    (progn
5383                      (when (eq (car preferred) 'quote)
5384                        (setq preferred (cadr preferred)))
5385                      (not (equal preferred
5386                                  (get-text-property (point) 'gnus-data))))
5387                    (setq parts (get-text-property (point) 'gnus-part))
5388                    (setq parts (cdr (assq parts
5389                                           gnus-article-mime-handle-alist)))
5390                    (equal (mm-handle-media-type parts) "multipart/alternative")
5391                    (setq parts (reverse (cdr parts))))
5392           (setcar (cddr form)
5393                   (list 'quote (or (cadr (member preferred parts))
5394                                    (car parts)))))
5395         (funcall fun handle)))))
5396
5397 (defun gnus-mime-view-part-externally (&optional handle)
5398   "View the MIME part under point with an external viewer."
5399   (interactive)
5400   (gnus-article-check-buffer)
5401   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5402          (mm-user-display-methods nil)
5403          (mm-inlined-types nil)
5404          (mail-parse-charset gnus-newsgroup-charset)
5405          (mail-parse-ignored-charsets
5406           (with-current-buffer gnus-summary-buffer
5407             gnus-newsgroup-ignored-charsets))
5408          (type (mm-handle-media-type handle))
5409          (method (mailcap-mime-info type))
5410          (mm-enable-external t))
5411     (if (not (stringp method))
5412         (gnus-mime-view-part-as-type
5413          nil (lambda (type) (stringp (mailcap-mime-info type))))
5414       (when handle
5415         (mm-display-part handle nil t)))))
5416
5417 (defun gnus-mime-view-part-internally (&optional handle)
5418   "View the MIME part under point with an internal viewer.
5419 If no internal viewer is available, use an external viewer."
5420   (interactive)
5421   (gnus-article-check-buffer)
5422   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5423          (mm-inlined-types '(".*"))
5424          (mm-inline-large-images t)
5425          (mail-parse-charset gnus-newsgroup-charset)
5426          (mail-parse-ignored-charsets
5427           (with-current-buffer gnus-summary-buffer
5428             gnus-newsgroup-ignored-charsets))
5429          (inhibit-read-only t))
5430     (if (not (mm-inlinable-p handle))
5431         (gnus-mime-view-part-as-type
5432          nil (lambda (type) (mm-inlinable-p handle type)))
5433       (when handle
5434         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5435
5436 (defun gnus-mime-action-on-part (&optional action)
5437   "Do something with the MIME attachment at \(point\)."
5438   (interactive
5439    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5440   (gnus-article-check-buffer)
5441   (let ((action-pair (assoc action gnus-mime-action-alist)))
5442     (if action-pair
5443         (funcall (cdr action-pair)))))
5444
5445 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5446   "Call FUNCTION on MIME part N.
5447 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5448 If INTERACTIVE, call FUNCTION interactively."
5449   (let (window frame)
5450     ;; Check whether the article is displayed.
5451     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5452                  (setq window (get-buffer-window gnus-article-buffer t))
5453                  (frame-visible-p (setq frame (window-frame window))))
5454       (error "No article is displayed"))
5455     (with-current-buffer gnus-article-buffer
5456       ;; Check whether the article displays the right contents.
5457       (unless (with-current-buffer gnus-summary-buffer
5458                 (eq gnus-current-article (gnus-summary-article-number)))
5459         (error "You should select the right article first"))
5460       (if n
5461           (setq n (prefix-numeric-value n))
5462         (let ((pt (point)))
5463           (setq n (or (get-text-property pt 'gnus-part)
5464                       (and (not (bobp))
5465                            (get-text-property (1- pt) 'gnus-part))
5466                       (get-text-property (prog2
5467                                              (forward-line 1)
5468                                              (point)
5469                                            (goto-char pt))
5470                                          'gnus-part)
5471                       (get-text-property
5472                        (or (and (setq pt (previous-single-property-change
5473                                           pt 'gnus-part))
5474                                 (1- pt))
5475                            (next-single-property-change (point) 'gnus-part)
5476                            (point))
5477                        'gnus-part)
5478                       1))))
5479       ;; Check whether the specified part exists.
5480       (when (> n (length gnus-article-mime-handle-alist))
5481         (error "No such part")))
5482     (unless
5483         (progn
5484           ;; To select the window is needed so that the cursor
5485           ;; might be visible on the MIME button.
5486           (select-window (prog1
5487                              window
5488                            (setq window (selected-window))
5489                            ;; Article may be displayed in the other frame.
5490                            (gnus-select-frame-set-input-focus
5491                             (prog1
5492                                 frame
5493                               (setq frame (selected-frame))))))
5494           (when (gnus-article-goto-part n)
5495             ;; We point the cursor and the arrow at the MIME button
5496             ;; when the `function' prompt the user for something.
5497             (unless (and (pos-visible-in-window-p)
5498                          (> (count-lines (point) (window-end))
5499                             (/ (1- (window-height)) 3)))
5500               (recenter (/ (1- (window-height)) 3)))
5501             (let ((cursor-in-non-selected-windows t)
5502                   (overlay-arrow-string "=>")
5503                   (overlay-arrow-position (point-marker)))
5504               (unwind-protect
5505                   (cond
5506                    ((and no-handle interactive)
5507                     (call-interactively function))
5508                    (no-handle
5509                     (funcall function))
5510                    (interactive
5511                     (call-interactively
5512                      function (get-text-property (point) 'gnus-data)))
5513                    (t
5514                     (funcall function
5515                              (get-text-property (point) 'gnus-data))))
5516                 (set-marker overlay-arrow-position nil)
5517                 (unless gnus-auto-select-part
5518                   (gnus-select-frame-set-input-focus frame)
5519                   (select-window window))))
5520             t))
5521       (if gnus-inhibit-mime-unbuttonizing
5522           ;; This is the default though the program shouldn't reach here.
5523           (error "No such part")
5524         ;; The part which doesn't have the MIME button is selected.
5525         ;; So, we display all the buttons and redo it.
5526         (let ((gnus-inhibit-mime-unbuttonizing t))
5527           (gnus-summary-show-article)
5528           (gnus-article-part-wrapper n function no-handle))))))
5529
5530 (defun gnus-article-pipe-part (n)
5531   "Pipe MIME part N, which is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'mm-pipe-part))
5534
5535 (defun gnus-article-save-part (n)
5536   "Save MIME part N, which is the numerical prefix."
5537   (interactive "P")
5538   (gnus-article-part-wrapper n 'mm-save-part))
5539
5540 (defun gnus-article-interactively-view-part (n)
5541   "View MIME part N interactively, which is the numerical prefix."
5542   (interactive "P")
5543   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5544
5545 (defun gnus-article-copy-part (n)
5546   "Copy MIME part N, which is the numerical prefix."
5547   (interactive "P")
5548   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5549
5550 (defun gnus-article-view-part-as-charset (n)
5551   "View MIME part N using a specified charset.
5552 N is the numerical prefix."
5553   (interactive "P")
5554   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5555
5556 (defun gnus-article-view-part-externally (n)
5557   "View MIME part N externally, which is the numerical prefix."
5558   (interactive "P")
5559   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5560
5561 (defun gnus-article-inline-part (n)
5562   "Inline MIME part N, which is the numerical prefix."
5563   (interactive "P")
5564   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5565
5566 (defun gnus-article-save-part-and-strip (n)
5567   "Save MIME part N and replace it with an external body.
5568 N is the numerical prefix."
5569   (interactive "P")
5570   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5571
5572 (defun gnus-article-replace-part (n)
5573   "Replace MIME part N with an external body.
5574 N is the numerical prefix."
5575   (interactive "P")
5576   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5577
5578 (defun gnus-article-delete-part (n)
5579   "Delete MIME part N and add some information about the removed part.
5580 N is the numerical prefix."
5581   (interactive "P")
5582   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5583
5584 (defun gnus-article-view-part-as-type (n)
5585   "Choose a MIME media type, and view part N as such.
5586 N is the numerical prefix."
5587   (interactive "P")
5588   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5589
5590 (defun gnus-article-mime-match-handle-first (condition)
5591   (if condition
5592       (let (n)
5593         (dolist (ihandle gnus-article-mime-handle-alist)
5594           (if (and (cond
5595                     ((functionp condition)
5596                      (funcall condition (cdr ihandle)))
5597                     ((eq condition 'undisplayed)
5598                      (not (or (mm-handle-undisplayer (cdr ihandle))
5599                               (equal (mm-handle-media-type (cdr ihandle))
5600                                      "multipart/alternative"))))
5601                     ((eq condition 'undisplayed-alternative)
5602                      (not (mm-handle-undisplayer (cdr ihandle))))
5603                     (t t))
5604                    (gnus-article-goto-part (car ihandle))
5605                    (or (not n) (< (car ihandle) n)))
5606               (setq n (car ihandle))))
5607         (or n 1))
5608     1))
5609
5610 (defun gnus-article-view-part (&optional n)
5611   "View MIME part N, which is the numerical prefix.
5612 If the part is already shown, hide the part.  If N is nil, view
5613 all parts."
5614   (interactive "P")
5615   (with-current-buffer gnus-article-buffer
5616     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5617                              gnus-article-mime-match-handle-function)))
5618     (when (> n (length gnus-article-mime-handle-alist))
5619       (error "No such part"))
5620     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5621       (when (gnus-article-goto-part n)
5622         (if (equal (car handle) "multipart/alternative")
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 (shr-put-image data (propertize (or alt "*")
6207                                                'gnus-image-category 'shr)
6208                               flags)))
6209     (when image
6210       (gnus-add-image 'shr image))))
6211
6212 ;;; Article savers.
6213
6214 (defun gnus-output-to-file (file-name)
6215   "Append the current article to a file named FILE-NAME.
6216 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6217 text and used as the value of the coding cookie which is added to the
6218 top of a file.  Otherwise, this function saves a raw article without
6219 the coding cookie."
6220   (let* ((artbuf (current-buffer))
6221          (file-name-coding-system nnmail-pathname-coding-system)
6222          (coding gnus-article-save-coding-system)
6223          (coding-system-for-read (if coding
6224                                      nil ;; Rely on the coding cookie.
6225                                    mm-text-coding-system))
6226          (coding-system-for-write (or coding
6227                                       mm-text-coding-system-for-write
6228                                       mm-text-coding-system))
6229          (exists (file-exists-p file-name)))
6230     (with-temp-buffer
6231       (when exists
6232         (insert-file-contents file-name)
6233         (goto-char (point-min))
6234         ;; Remove the existing coding cookie.
6235         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6236           (delete-region (match-beginning 0) (match-end 0))))
6237       (goto-char (point-max))
6238       (insert-buffer-substring artbuf)
6239       ;; Append newline at end of the buffer as separator, and then
6240       ;; save it to file.
6241       (goto-char (point-max))
6242       (insert "\n")
6243       (when coding
6244         ;; If the coding system is not suitable to encode the text,
6245         ;; ask a user for a proper one.
6246         (when (fboundp 'select-safe-coding-system)
6247           (setq coding (coding-system-base
6248                         (save-window-excursion
6249                           (select-safe-coding-system (point-min) (point-max)
6250                                                      coding))))
6251           (setq coding-system-for-write
6252                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6253                     coding)))
6254         (goto-char (point-min))
6255         ;; Add the coding cookie.
6256         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6257                         coding-system-for-write)))
6258       (if exists
6259           (progn
6260             (write-region (point-min) (point-max) file-name nil 'no-message)
6261             (message "Appended to %s" file-name))
6262         (write-region (point-min) (point-max) file-name))))
6263   t)
6264
6265 (defun gnus-narrow-to-page (&optional arg)
6266   "Narrow the article buffer to a page.
6267 If given a numerical ARG, move forward ARG pages."
6268   (interactive "P")
6269   (setq arg (if arg (prefix-numeric-value arg) 0))
6270   (with-current-buffer gnus-article-buffer
6271     (widen)
6272     ;; Remove any old next/prev buttons.
6273     (when (gnus-visual-p 'page-marker)
6274       (let ((inhibit-read-only t))
6275         (gnus-remove-text-with-property 'gnus-prev)
6276         (gnus-remove-text-with-property 'gnus-next)))
6277     (let (st nd pt)
6278       (when (save-excursion
6279               (cond ((< arg 0)
6280                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6281                          (prog1
6282                              (setq nd (match-beginning 0)
6283                                    pt nd)
6284                            (when (re-search-backward page-delimiter nil t)
6285                              (setq st (match-end 0))))
6286                        (when (re-search-forward page-delimiter nil t)
6287                          (setq nd (match-beginning 0)
6288                                pt (point-min)))))
6289                     ((> arg 0)
6290                      (if (re-search-forward page-delimiter nil 'move arg)
6291                          (prog1
6292                              (setq st (match-end 0)
6293                                    pt st)
6294                            (when (re-search-forward page-delimiter nil t)
6295                              (setq nd (match-beginning 0))))
6296                        (when (re-search-backward page-delimiter nil t)
6297                          (setq st (match-end 0)
6298                                pt (point-max)))))
6299                     (t
6300                      (when (re-search-backward page-delimiter nil t)
6301                        (goto-char (setq st (match-end 0))))
6302                      (when (re-search-forward page-delimiter nil t)
6303                        (setq nd (match-beginning 0)))
6304                      (or st nd))))
6305         (setq gnus-page-broken t)
6306         (when pt (goto-char pt))
6307         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6308         (when (gnus-visual-p 'page-marker)
6309           (save-excursion
6310             (when nd
6311               (goto-char nd)
6312               (gnus-insert-next-page-button))
6313             (when st
6314               (goto-char st)
6315               (gnus-insert-prev-page-button))))))))
6316
6317 ;; Article mode commands
6318
6319 (defun gnus-article-goto-next-page ()
6320   "Show the next page of the article."
6321   (interactive)
6322   (when (gnus-article-next-page)
6323     (goto-char (point-min))
6324     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6325
6326
6327 (defun gnus-article-goto-prev-page ()
6328   "Show the previous page of the article."
6329   (interactive)
6330   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6331       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6332     (gnus-article-prev-page nil)))
6333
6334 ;; This is cleaner but currently breaks `gnus-pick-mode':
6335 ;;
6336 ;; (defun gnus-article-goto-next-page ()
6337 ;;   "Show the next page of the article."
6338 ;;   (interactive)
6339 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6340 ;;     (gnus-summary-next-page)))
6341 ;;
6342 ;; (defun gnus-article-goto-prev-page ()
6343 ;;   "Show the next page of the article."
6344 ;;   (interactive)
6345 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6346 ;;     (gnus-summary-prev-page)))
6347
6348 (defun gnus-article-next-page (&optional lines)
6349   "Show the next page of the current article.
6350 If end of article, return non-nil.  Otherwise return nil.
6351 Argument LINES specifies lines to be scrolled up."
6352   (interactive "p")
6353   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6354   (if (and (not (and gnus-article-over-scroll
6355                      (> (count-lines (window-start) (point-max))
6356                         (if (featurep 'xemacs)
6357                             (or lines (1- (window-height)))
6358                           (+ (or lines (1- (window-height))) scroll-margin)))))
6359            (save-excursion
6360              (end-of-line)
6361              (and (pos-visible-in-window-p)     ;Not continuation line.
6362                   (>= (point) (point-max)))))
6363       ;; Nothing in this page.
6364       (if (or (not gnus-page-broken)
6365               (save-excursion
6366                 (save-restriction
6367                   (widen)
6368                   (forward-line)
6369                   (eobp)))) ;Real end-of-buffer?
6370           (progn
6371             (when gnus-article-over-scroll
6372               (gnus-article-next-page-1 lines))
6373             t)                  ;Nothing more.
6374         (gnus-narrow-to-page 1)         ;Go to next page.
6375         nil)
6376     ;; More in this page.
6377     (gnus-article-next-page-1 lines)
6378     nil))
6379
6380 (defun gnus-article-beginning-of-window ()
6381   "Move point to the beginning of the window.
6382 In Emacs, the point is placed at the line number which `scroll-margin'
6383 specifies."
6384   (if (featurep 'xemacs)
6385       (move-to-window-line 0)
6386     ;; There is an obscure bug in Emacs that makes it impossible to
6387     ;; scroll past big pictures in the article buffer.  Try to fix
6388     ;; this by adding a sanity check by counting the lines visible.
6389     (when (> (count-lines (window-start) (window-end)) 30)
6390       (move-to-window-line
6391        (min (max 0 scroll-margin)
6392             (max 1 (- (window-height)
6393                       (if mode-line-format 1 0)
6394                       (if header-line-format 1 0)
6395                       2)))))))
6396
6397 (defun gnus-article-next-page-1 (lines)
6398   (condition-case ()
6399       (let ((scroll-in-place nil)
6400             (auto-window-vscroll nil))
6401         (scroll-up lines))
6402     (end-of-buffer
6403      ;; Long lines may cause an end-of-buffer error.
6404      (goto-char (point-max))))
6405   (gnus-article-beginning-of-window))
6406
6407 (defun gnus-article-prev-page (&optional lines)
6408   "Show previous page of current article.
6409 Argument LINES specifies lines to be scrolled down."
6410   (interactive "p")
6411   (move-to-window-line 0)
6412   (if (and gnus-page-broken
6413            (bobp)
6414            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6415       (progn
6416         (gnus-narrow-to-page -1)        ;Go to previous page.
6417         (goto-char (point-max))
6418         (recenter (if gnus-article-over-scroll
6419                       (if lines
6420                           (max (if (featurep 'xemacs)
6421                                    lines
6422                                  (+ lines scroll-margin))
6423                                3)
6424                         (- (window-height) 2))
6425                     -1)))
6426     (prog1
6427         (condition-case ()
6428             (let ((scroll-in-place nil))
6429               (scroll-down lines))
6430           (beginning-of-buffer
6431            (goto-char (point-min))))
6432       (gnus-article-beginning-of-window))))
6433
6434 (defun gnus-article-only-boring-p ()
6435   "Decide whether there is only boring text remaining in the article.
6436 Something \"interesting\" is a word of at least two letters that does
6437 not have a face in `gnus-article-boring-faces'."
6438   (when (and gnus-article-skip-boring
6439              (boundp 'gnus-article-boring-faces)
6440              (symbol-value 'gnus-article-boring-faces))
6441     (save-excursion
6442       (let ((inhibit-point-motion-hooks t))
6443         (catch 'only-boring
6444           (while (re-search-forward "\\b\\w\\w" nil t)
6445             (forward-char -1)
6446             (when (not (gnus-intersection
6447                         (gnus-faces-at (point))
6448                         (symbol-value 'gnus-article-boring-faces)))
6449               (throw 'only-boring nil)))
6450           (throw 'only-boring t))))))
6451
6452 (defun gnus-article-refer-article ()
6453   "Read article specified by message-id around point."
6454   (interactive)
6455   (save-excursion
6456     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6457     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6458     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6459         (let ((msg-id (concat "<" (match-string 0) ">")))
6460           (set-buffer gnus-summary-buffer)
6461           (gnus-summary-refer-article msg-id))
6462       (error "No references around point"))))
6463
6464 (defun gnus-article-show-summary ()
6465   "Reconfigure windows to show summary buffer."
6466   (interactive)
6467   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6468       (error "There is no summary buffer for this article buffer")
6469     (gnus-article-set-globals)
6470     (gnus-configure-windows 'article)
6471     (gnus-summary-goto-subject gnus-current-article)
6472     (gnus-summary-position-point)))
6473
6474 (defun gnus-article-describe-briefly ()
6475   "Describe article mode commands briefly."
6476   (interactive)
6477   (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")))
6478
6479 (defun gnus-article-check-buffer ()
6480   "Beep if not in an article buffer."
6481   (unless (equal major-mode 'gnus-article-mode)
6482     (error "Command invoked outside of a Gnus article buffer")))
6483
6484 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6485   "Read a summary buffer key sequence and execute it from the article buffer."
6486   (interactive "P")
6487   (gnus-article-check-buffer)
6488   (let ((nosaves
6489          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6490            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6491            "=" "^" "\M-^" "|"))
6492         (nosave-but-article
6493          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6494            "An" "Ap" [?A (meta return)] [?A delete]))
6495         (nosave-in-article
6496          '("AS" "\C-d"))
6497         (up-to-top
6498          '("n" "Gn" "p" "Gp"))
6499         keys new-sum-point)
6500     (with-current-buffer gnus-article-current-summary
6501       (let (gnus-pick-mode)
6502         (setq unread-command-events (nconc unread-command-events
6503                                            (list (or key last-command-event)))
6504               keys (if (featurep 'xemacs)
6505                        (events-to-keys (read-key-sequence nil t))
6506                      (read-key-sequence nil t)))))
6507
6508     (message "")
6509
6510     (cond
6511      ((eq (aref keys (1- (length keys))) ?\C-h)
6512       (gnus-article-describe-bindings (substring keys 0 -1)))
6513      ((or (member keys nosaves)
6514           (member keys nosave-but-article)
6515           (member keys nosave-in-article))
6516       (let (func)
6517         (save-window-excursion
6518           (pop-to-buffer gnus-article-current-summary)
6519           ;; We disable the pick minor mode commands.
6520           (let (gnus-pick-mode)
6521             (setq func (key-binding keys t))))
6522         (if (or (not func)
6523                 (numberp func))
6524             (ding)
6525           (unless (member keys nosave-in-article)
6526             (set-buffer gnus-article-current-summary))
6527           (when (get func 'disabled)
6528             (error "Function %s disabled" func))
6529           (call-interactively func)
6530           (setq new-sum-point (point)))
6531         (when (member keys nosave-but-article)
6532           (pop-to-buffer gnus-article-buffer))))
6533      (t
6534       ;; These commands should restore window configuration.
6535       (let ((obuf (current-buffer))
6536             (owin (current-window-configuration))
6537             win func in-buffer selected new-sum-start new-sum-hscroll err)
6538         (cond (not-restore-window
6539                (pop-to-buffer gnus-article-current-summary)
6540                (setq win (selected-window)))
6541               ((setq win (get-buffer-window gnus-article-current-summary))
6542                (select-window win))
6543               (t
6544                (let ((summary-buffer gnus-article-current-summary))
6545                  (gnus-configure-windows 'article)
6546                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6547                    (let ((gnus-buffer-configuration
6548                           '((article ((vertical 1.0
6549                                                 (summary 0.25 point)
6550                                                 (article 1.0)))))))
6551                      (gnus-configure-windows 'article))
6552                    (setq win (get-buffer-window summary-buffer 'visible)))
6553                  (gnus-select-frame-set-input-focus (window-frame win))
6554                  (select-window win))))
6555         (setq in-buffer (current-buffer))
6556         ;; We disable the pick minor mode commands.
6557         (setq func (let (gnus-pick-mode)
6558                      (key-binding keys t)))
6559         (when (get func 'disabled)
6560           (error "Function %s disabled" func))
6561         (if (and func
6562                  (functionp func)
6563                  (condition-case code
6564                      (progn
6565                        (call-interactively func)
6566                        t)
6567                    (error
6568                     (setq err code)
6569                     nil)))
6570             (progn
6571               (when (eq win (selected-window))
6572                 (setq new-sum-point (point)
6573                       new-sum-start (window-start win)
6574                       new-sum-hscroll (window-hscroll win)))
6575               (when (or (eq in-buffer (current-buffer))
6576                         (when (eq obuf (current-buffer))
6577                           (set-buffer in-buffer)
6578                           t))
6579                 (setq selected (gnus-summary-select-article))
6580                 (set-buffer obuf)
6581                 (unless not-restore-window
6582                   (set-window-configuration owin))
6583                 (when (and (eq selected 'old)
6584                            new-sum-point)
6585                   (set-window-start (get-buffer-window (current-buffer))
6586                                     1)
6587                   (set-window-point (get-buffer-window (current-buffer))
6588                                     (if (article-goto-body)
6589                                         (1- (point))
6590                                       (point))))
6591                 (when (and (not not-restore-window)
6592                            new-sum-point
6593                            (window-live-p win)
6594                            (with-current-buffer (window-buffer win)
6595                              (eq major-mode 'gnus-summary-mode)))
6596                   (set-window-point win new-sum-point)
6597                   (set-window-start win new-sum-start)
6598                   (set-window-hscroll win new-sum-hscroll))))
6599           (set-window-configuration owin)
6600           (if err
6601               (signal (car err) (cdr err))
6602             (ding))))))))
6603
6604 (defun gnus-article-read-summary-send-keys ()
6605   (interactive)
6606   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6607     (gnus-article-read-summary-keys)))
6608
6609 (defun gnus-article-describe-key (key)
6610   "Display documentation of the function invoked by KEY.
6611 KEY is a string or a vector."
6612   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6613                        (read-key-sequence "Describe key: "))))
6614   (gnus-article-check-buffer)
6615   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6616                                   gnus-article-read-summary-send-keys))
6617       (with-current-buffer gnus-article-current-summary
6618         (setq unread-command-events
6619               (if (featurep 'xemacs)
6620                   (append key nil)
6621                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6622                                         (list 'meta (- x 128))
6623                                       x))
6624                         key)))
6625         (let ((cursor-in-echo-area t)
6626               gnus-pick-mode)
6627           (describe-key (read-key-sequence nil t))))
6628     (describe-key key)))
6629
6630 (defun gnus-article-describe-key-briefly (key &optional insert)
6631   "Display documentation of the function invoked by KEY.
6632 KEY is a string or a vector."
6633   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6634                        (read-key-sequence "Describe key: "))
6635                      current-prefix-arg))
6636   (gnus-article-check-buffer)
6637   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6638                                   gnus-article-read-summary-send-keys))
6639       (with-current-buffer gnus-article-current-summary
6640         (setq unread-command-events
6641               (if (featurep 'xemacs)
6642                   (append key nil)
6643                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6644                                         (list 'meta (- x 128))
6645                                       x))
6646                         key)))
6647         (let ((cursor-in-echo-area t)
6648               gnus-pick-mode)
6649           (describe-key-briefly (read-key-sequence nil t) insert)))
6650     (describe-key-briefly key insert)))
6651
6652 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6653 (defvar gnus-agent-summary-mode)
6654 (defvar gnus-draft-mode)
6655 ;; Calling help-buffer will autoload help-mode.
6656 (defvar help-xref-stack-item)
6657 ;; Emacs 22 doesn't load it in the batch mode.
6658 (eval-when-compile
6659   (autoload 'help-buffer "help-mode"))
6660
6661 (defun gnus-article-describe-bindings (&optional prefix)
6662   "Show a list of all defined keys, and their definitions.
6663 The optional argument PREFIX, if non-nil, should be a key sequence;
6664 then we display only bindings that start with that prefix."
6665   (interactive)
6666   (gnus-article-check-buffer)
6667   (let ((keymap (copy-keymap gnus-article-mode-map))
6668         (map (copy-keymap gnus-article-send-map))
6669         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6670         parent agent draft)
6671     (define-key keymap "S" map)
6672     (define-key map [t] nil)
6673     (with-current-buffer gnus-article-current-summary
6674       (set-keymap-parent
6675        keymap
6676        (if (setq parent (keymap-parent gnus-article-mode-map))
6677            (prog1
6678                (setq parent (copy-keymap parent))
6679              (set-keymap-parent parent (current-local-map)))
6680          (current-local-map)))
6681       (set-keymap-parent map (key-binding "S"))
6682       (let (key def gnus-pick-mode)
6683         (while sumkeys
6684           (setq key (pop sumkeys))
6685           (cond ((and (vectorp key) (= (length key) 1)
6686                       (consp (setq def (aref key 0)))
6687                       (numberp (car def)) (numberp (cdr def)))
6688                  (when (< (max (car def) (cdr def)) 128)
6689                    (setq sumkeys
6690                          (append (mapcar
6691                                   #'vector
6692                                   (nreverse (gnus-uncompress-range def)))
6693                                  sumkeys))))
6694                 ((setq def (key-binding key))
6695                  (unless (eq def 'undefined)
6696                    (define-key keymap key def))))))
6697       (when (boundp 'gnus-agent-summary-mode)
6698         (setq agent gnus-agent-summary-mode))
6699       (when (boundp 'gnus-draft-mode)
6700         (setq draft gnus-draft-mode)))
6701     (with-temp-buffer
6702       (use-local-map keymap)
6703       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6704       (set (make-local-variable 'gnus-draft-mode) draft)
6705       (describe-bindings prefix))
6706     (let ((item `((lambda (prefix)
6707                     (with-current-buffer ,(current-buffer)
6708                       (gnus-article-describe-bindings prefix)))
6709                   ,prefix)))
6710       (with-current-buffer (let (help-xref-following) (help-buffer))
6711         (setq help-xref-stack-item item)))))
6712
6713 (defun gnus-article-reply-with-original (&optional wide)
6714   "Start composing a reply mail to the current message.
6715 The text in the region will be yanked.  If the region isn't active,
6716 the entire article will be yanked."
6717   (interactive)
6718   (let ((article (cdr gnus-article-current))
6719         contents)
6720     (if (not (gnus-region-active-p))
6721         (with-current-buffer gnus-summary-buffer
6722           (gnus-summary-reply (list (list article)) wide))
6723       (setq contents (buffer-substring (point) (mark t)))
6724       ;; Deactivate active regions.
6725       (when (and (boundp 'transient-mark-mode)
6726                  transient-mark-mode)
6727         (setq mark-active nil))
6728       (with-current-buffer gnus-summary-buffer
6729         (gnus-summary-reply
6730          (list (list article contents)) wide)))))
6731
6732 (defun gnus-article-wide-reply-with-original ()
6733   "Start composing a wide reply mail to the current message.
6734 The text in the region will be yanked.  If the region isn't active,
6735 the entire article will be yanked."
6736   (interactive)
6737   (gnus-article-reply-with-original t))
6738
6739 (defun gnus-article-followup-with-original ()
6740   "Compose a followup to the current article.
6741 The text in the region will be yanked.  If the region isn't active,
6742 the entire article will be yanked."
6743   (interactive)
6744   (let ((article (cdr gnus-article-current))
6745         contents)
6746       (if (not (gnus-region-active-p))
6747           (with-current-buffer gnus-summary-buffer
6748             (gnus-summary-followup (list (list article))))
6749         (setq contents (buffer-substring (point) (mark t)))
6750         ;; Deactivate active regions.
6751         (when (and (boundp 'transient-mark-mode)
6752                    transient-mark-mode)
6753           (setq mark-active nil))
6754         (with-current-buffer gnus-summary-buffer
6755           (gnus-summary-followup
6756            (list (list article contents)))))))
6757
6758 (defun gnus-article-hide (&optional arg force)
6759   "Hide all the gruft in the current article.
6760 This means that signatures, cited text and (some) headers will be
6761 hidden.
6762 If given a prefix, show the hidden text instead."
6763   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6764   (gnus-article-hide-headers arg)
6765   (gnus-article-hide-list-identifiers arg)
6766   (gnus-article-hide-citation-maybe arg force)
6767   (gnus-article-hide-signature arg))
6768
6769 (defun gnus-article-maybe-highlight ()
6770   "Do some article highlighting if article highlighting is requested."
6771   (when (gnus-visual-p 'article-highlight 'highlight)
6772     (gnus-article-highlight-some)))
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 (gnus-news-group-p group)
6962       ;; Block nothing in news groups.
6963       nil
6964     ;; Block everything anywhere else.
6965     "."))
6966
6967 (defun gnus-blocked-images ()
6968   (if (functionp gnus-blocked-images)
6969       (funcall gnus-blocked-images gnus-newsgroup-name)
6970     gnus-blocked-images))
6971
6972 ;;;
6973 ;;; Article editing
6974 ;;;
6975
6976 (defcustom gnus-article-edit-mode-hook nil
6977   "Hook run in article edit mode buffers."
6978   :group 'gnus-article-various
6979   :type 'hook)
6980
6981 (defvar gnus-article-edit-done-function nil)
6982
6983 (defvar gnus-article-edit-mode-map nil)
6984
6985 ;; Should we be using derived.el for this?
6986 (unless gnus-article-edit-mode-map
6987   (setq gnus-article-edit-mode-map (make-keymap))
6988   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6989
6990   (gnus-define-keys gnus-article-edit-mode-map
6991     "\C-c?"    describe-mode
6992     "\C-c\C-c" gnus-article-edit-done
6993     "\C-c\C-k" gnus-article-edit-exit
6994     "\C-c\C-f\C-t" message-goto-to
6995     "\C-c\C-f\C-o" message-goto-from
6996     "\C-c\C-f\C-b" message-goto-bcc
6997     ;;"\C-c\C-f\C-w" message-goto-fcc
6998     "\C-c\C-f\C-c" message-goto-cc
6999     "\C-c\C-f\C-s" message-goto-subject
7000     "\C-c\C-f\C-r" message-goto-reply-to
7001     "\C-c\C-f\C-n" message-goto-newsgroups
7002     "\C-c\C-f\C-d" message-goto-distribution
7003     "\C-c\C-f\C-f" message-goto-followup-to
7004     "\C-c\C-f\C-m" message-goto-mail-followup-to
7005     "\C-c\C-f\C-k" message-goto-keywords
7006     "\C-c\C-f\C-u" message-goto-summary
7007     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7008     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7009     "\C-c\C-b" message-goto-body
7010     "\C-c\C-i" message-goto-signature
7011
7012     "\C-c\C-t" message-insert-to
7013     "\C-c\C-n" message-insert-newsgroups
7014     "\C-c\C-o" message-sort-headers
7015     "\C-c\C-e" message-elide-region
7016     "\C-c\C-v" message-delete-not-region
7017     "\C-c\C-z" message-kill-to-signature
7018     "\M-\r" message-newline-and-reformat
7019     "\C-c\C-a" mml-attach-file
7020     "\C-a" message-beginning-of-line
7021     "\t" message-tab
7022     "\M-;" comment-region)
7023
7024   (gnus-define-keys (gnus-article-edit-wash-map
7025                      "\C-c\C-w" gnus-article-edit-mode-map)
7026     "f" gnus-article-edit-full-stops))
7027
7028 (easy-menu-define
7029   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7030   '("Field"
7031     ["Fetch To" message-insert-to t]
7032     ["Fetch Newsgroups" message-insert-newsgroups t]
7033     "----"
7034     ["To" message-goto-to t]
7035     ["From" message-goto-from t]
7036     ["Subject" message-goto-subject t]
7037     ["Cc" message-goto-cc t]
7038     ["Reply-To" message-goto-reply-to t]
7039     ["Summary" message-goto-summary t]
7040     ["Keywords" message-goto-keywords t]
7041     ["Newsgroups" message-goto-newsgroups t]
7042     ["Followup-To" message-goto-followup-to t]
7043     ["Mail-Followup-To" message-goto-mail-followup-to t]
7044     ["Distribution" message-goto-distribution t]
7045     ["Body" message-goto-body t]
7046     ["Signature" message-goto-signature t]))
7047
7048 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7049   "Major mode for editing articles.
7050 This is an extended text-mode.
7051
7052 \\{gnus-article-edit-mode-map}"
7053   (make-local-variable 'gnus-article-edit-done-function)
7054   (make-local-variable 'gnus-prev-winconf)
7055   (set (make-local-variable 'font-lock-defaults)
7056        '(message-font-lock-keywords t))
7057   (set (make-local-variable 'mail-header-separator) "")
7058   (set (make-local-variable 'gnus-article-edit-mode) t)
7059   (easy-menu-add message-mode-field-menu message-mode-map)
7060   (mml-mode)
7061   (setq buffer-read-only nil)
7062   (buffer-enable-undo)
7063   (widen))
7064
7065 (defun gnus-article-edit (&optional force)
7066   "Edit the current article.
7067 This will have permanent effect only in mail groups.
7068 If FORCE is non-nil, allow editing of articles even in read-only
7069 groups."
7070   (interactive "P")
7071   (when (and (not force)
7072              (gnus-group-read-only-p))
7073     (error "The current newsgroup does not support article editing"))
7074   (gnus-article-date-original)
7075   (gnus-article-edit-article
7076    'ignore
7077    `(lambda (no-highlight)
7078       'ignore
7079       (gnus-summary-edit-article-done
7080        ,(or (mail-header-references gnus-current-headers) "")
7081        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7082
7083 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7084   "Start editing the contents of the current article buffer."
7085   (let ((winconf (current-window-configuration)))
7086     (set-buffer gnus-article-buffer)
7087     (let ((message-auto-save-directory
7088            ;; Don't associate the article buffer with a draft file.
7089            nil))
7090       (gnus-article-edit-mode))
7091     (funcall start-func)
7092     (set-buffer-modified-p nil)
7093     (gnus-configure-windows 'edit-article)
7094     (setq gnus-article-edit-done-function exit-func)
7095     (setq gnus-prev-winconf winconf)
7096     (unless quiet
7097       (gnus-message 6 "C-c C-c to end edits"))))
7098
7099 (defun gnus-article-edit-done (&optional arg)
7100   "Update the article edits and exit."
7101   (interactive "P")
7102   (let ((func gnus-article-edit-done-function)
7103         (buf (current-buffer))
7104         (start (window-start))
7105         (p (point))
7106         (winconf gnus-prev-winconf))
7107     (widen) ;; Widen it in case that users narrowed the buffer.
7108     (funcall func arg)
7109     (set-buffer buf)
7110     ;; The cache and backlog have to be flushed somewhat.
7111     (when gnus-keep-backlog
7112       (gnus-backlog-remove-article
7113        (car gnus-article-current) (cdr gnus-article-current)))
7114     ;; Flush original article as well.
7115     (gnus-flush-original-article-buffer)
7116     (when gnus-use-cache
7117       (gnus-cache-update-article
7118        (car gnus-article-current) (cdr gnus-article-current)))
7119     ;; We remove all text props from the article buffer.
7120     (kill-all-local-variables)
7121     (set-text-properties (point-min) (point-max) nil)
7122     (gnus-article-mode)
7123     (set-window-configuration winconf)
7124     (set-buffer buf)
7125     (set-window-start (get-buffer-window buf) start)
7126     (set-window-point (get-buffer-window buf) (point)))
7127   (gnus-summary-show-article))
7128
7129 (defun gnus-flush-original-article-buffer ()
7130   (when (get-buffer gnus-original-article-buffer)
7131     (with-current-buffer gnus-original-article-buffer
7132       (setq gnus-original-article nil))))
7133
7134 (defun gnus-article-edit-exit ()
7135   "Exit the article editing without updating."
7136   (interactive)
7137   (when (or (not (buffer-modified-p))
7138             (yes-or-no-p "Article modified; kill anyway? "))
7139     (let ((curbuf (current-buffer))
7140           (p (point))
7141           (window-start (window-start)))
7142       (erase-buffer)
7143       (if (gnus-buffer-live-p gnus-original-article-buffer)
7144           (insert-buffer-substring gnus-original-article-buffer))
7145       (let ((winconf gnus-prev-winconf))
7146         (kill-all-local-variables)
7147         (gnus-article-mode)
7148         (set-window-configuration winconf)
7149         ;; Tippy-toe some to make sure that point remains where it was.
7150         (save-current-buffer
7151           (set-buffer curbuf)
7152           (set-window-start (get-buffer-window (current-buffer)) window-start)
7153           (goto-char p))))
7154     (gnus-summary-show-article)))
7155
7156 (defun gnus-article-edit-full-stops ()
7157   "Interactively repair spacing at end of sentences."
7158   (interactive)
7159   (save-excursion
7160     (goto-char (point-min))
7161     (search-forward-regexp "^$" nil t)
7162     (let ((case-fold-search nil))
7163       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7164
7165 ;;;
7166 ;;; Article highlights
7167 ;;;
7168
7169 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7170
7171 ;;; Internal Variables:
7172
7173 (defcustom gnus-button-url-regexp
7174   (concat
7175    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7176    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7177    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7178    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7179        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7180              (punct "!?:;.,"))
7181          (concat
7182           "\\(?:"
7183           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7184           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7185           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7186           "\\|"
7187           "[" chars punct     "]+" "[" chars "]"
7188           "\\)"))
7189      (concat ;; XEmacs 21.4 doesn't support POSIX.
7190       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7191       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7192    "\\)")
7193   "Regular expression that matches URLs."
7194   :group 'gnus-article-buttons
7195   :type 'regexp)
7196
7197 (defcustom gnus-button-valid-fqdn-regexp
7198   message-valid-fqdn-regexp
7199   "Regular expression that matches a valid FQDN."
7200   :version "22.1"
7201   :group 'gnus-article-buttons
7202   :type 'regexp)
7203
7204 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7205 (defcustom gnus-button-valid-localpart-regexp
7206   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7207   "Regular expression that matches a localpart of mail addresses or MIDs."
7208   :version "22.1"
7209   :group 'gnus-article-buttons
7210   :type 'regexp)
7211
7212 (defcustom gnus-button-man-handler 'manual-entry
7213   "Function to use for displaying man pages.
7214 The function must take at least one argument with a string naming the
7215 man page."
7216   :version "22.1"
7217   :type '(choice (function-item :tag "Man" manual-entry)
7218                  (function-item :tag "Woman" woman)
7219                  (function :tag "Other"))
7220   :group 'gnus-article-buttons)
7221
7222 (defcustom gnus-button-mid-or-mail-regexp
7223   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7224           gnus-button-valid-fqdn-regexp
7225           ">?\\)\\b")
7226   "Regular expression that matches a message ID or a mail address."
7227   :version "22.1"
7228   :group 'gnus-article-buttons
7229   :type 'regexp)
7230
7231 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7232   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7233 Strings like this can be either a message ID or a mail address.  If it is one
7234 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7235 message ID or a mail address, respectively.  If this variable is set to the
7236 symbol `ask', always query the user what do do.  If it is a function, this
7237 function will be called with the string as its only argument.  The function
7238 must return `mid', `mail', `invalid' or `ask'."
7239   :version "22.1"
7240   :group 'gnus-article-buttons
7241   :type '(choice (function-item :tag "Heuristic function"
7242                                 gnus-button-mid-or-mail-heuristic)
7243                  (const ask)
7244                  (const mid)
7245                  (const mail)))
7246
7247 (defcustom gnus-button-mid-or-mail-heuristic-alist
7248   '((-10.0 . ".+\\$.+@")
7249     (-10.0 . "#")
7250     (-10.0 . "\\*")
7251     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7252     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7253     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7254     (-1.0  . "^[^a-z]+@")
7255     ;;
7256     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7257     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7258     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7259     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7260     ;;
7261     (-2.0  . "^[0-9]")
7262     (-1.0  . "^[0-9][0-9]")
7263     ;;
7264     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7265     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7266     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7267     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7268     ;;
7269     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7270     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7271     ;;       "[0-9]{8,}.*\@"
7272     (-3.0
7273      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7274     ;; "[0-9]{12,}.*\@"
7275     ;; compensation for TDMA dated mail addresses:
7276     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7277     ;;
7278     (-20.0 . "\\.fsf@") ;; Gnus
7279     (-20.0 . "^slrn")
7280     (-20.0 . "^Pine")
7281     (-20.0 . "^alpine\\.")
7282     (-20.0 . "_-_") ;; Subject change in thread
7283     ;;
7284     (-20.0 . "\\.ln@") ;; leafnode
7285     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7286     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7287     ;;
7288     ;; (5.0 . "") ;; $local_part_len <= 7
7289     (10.0  . "^[^0-9]+@")
7290     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7291     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7292     (3.0   . "\@stud")
7293     ;;
7294     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7295     ;;
7296     (0.5   . "^[A-Z][a-z]")
7297     (0.5   . "^[A-Z][a-z][a-z]")
7298     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7299     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7300   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7301
7302 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7303 address.  The REGEXP is processed with `case-fold-search' set to nil."
7304   :version "22.1"
7305   :group 'gnus-article-buttons
7306   :type '(repeat (cons (number :tag "Rate")
7307                        (regexp :tag "Regexp"))))
7308
7309 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7310   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7311 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7312 address, `ask' if unsure and `invalid' if the string is invalid."
7313   (let ((case-fold-search nil)
7314         (list gnus-button-mid-or-mail-heuristic-alist)
7315         (result 0) rate regexp lpartlen elem)
7316     (setq lpartlen
7317           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7318     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7319     ;; Certain special cases...
7320     (when (string-match
7321            (concat
7322             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7323             "^[0-9]+\\.[0-9]+@compuserve\\|"
7324             "@public\\.gmane\\.org")
7325            mid-or-mail)
7326       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7327       (setq result 'mail))
7328     (when (string-match "@.*@\\| " mid-or-mail)
7329       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7330       (setq result 'invalid))
7331     ;; Nothing more to do, if result is not a number here...
7332     (when (numberp result)
7333       (while list
7334         (setq elem (car list)
7335               rate (car elem)
7336               regexp (cdr elem)
7337               list (cdr list))
7338         (when (string-match regexp mid-or-mail)
7339           (setq result (+ result rate))
7340           (gnus-message
7341            9 "`%s' matched `%s', rate `%s', result `%s'."
7342            mid-or-mail regexp rate result)))
7343       (when (<= lpartlen 7)
7344         (setq result (+ result 5.0))
7345         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7346                       mid-or-mail result))
7347       (when (>= lpartlen 12)
7348         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7349         (cond
7350          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7351           ;; Long local part should contain realname if e-mail address,
7352           ;; too many digits: message-id.
7353           ;; $score -= 5.0 + 0.1 * $local_part_len;
7354           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7355           (setq result (+ result rate))
7356           (gnus-message
7357            9 "Many digits in `%s', rate `%s', result `%s'."
7358            mid-or-mail rate result))
7359          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7360                         mid-or-mail)
7361           ;; Too few vowels [^aeiouy]{4,}.*\@
7362           (setq result (+ result -5.0))
7363           (gnus-message
7364            9 "Few vowels in `%s', rate `%s', result `%s'."
7365            mid-or-mail -5.0 result))
7366          (t
7367           (setq result (+ result 5.0))
7368           (gnus-message
7369            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7370     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7371     ;; Maybe we should make this a customizable alist: (condition . 'result)
7372     (cond
7373      ((symbolp result) result)
7374      ;; Now convert number into proper results:
7375      ((< result -10.0) 'mid)
7376      ((> result  10.0) 'mail)
7377      (t 'ask))))
7378
7379 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7380   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7381          (url-mid (concat "news" ":" mid-or-mail))
7382          (url-mailto (concat "mailto" ":" mid-or-mail)))
7383     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7384     (when (fboundp pref)
7385       (setq guessed
7386             ;; get rid of surrounding angles...
7387             (funcall pref
7388                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7389       (if (or (eq 'mid guessed) (eq 'mail guessed))
7390           (setq pref guessed)
7391         (setq pref 'ask)))
7392     (if (eq pref 'ask)
7393         (save-window-excursion
7394           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7395               (setq pref 'mail)
7396             (setq pref 'mid))))
7397     (cond ((eq pref 'mid)
7398            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7399            (gnus-button-handle-news url-mid))
7400           ((eq pref 'mail)
7401            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7402            (gnus-url-mailto url-mailto))
7403           (t (gnus-message 3 "Invalid string.")))))
7404
7405 (defun gnus-button-handle-custom (fun arg)
7406   "Call function FUN on argument ARG.
7407 Both FUN and ARG are supposed to be strings.  ARG will be passed
7408 as a symbol to FUN."
7409   (funcall (intern fun)
7410            (if (string-match "^customize-apropos" fun)
7411                arg
7412              (intern arg))))
7413
7414 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7415
7416 (defun gnus-button-handle-describe-function (url)
7417   "Call `describe-function' when pushing the corresponding URL button."
7418   (describe-function
7419    (intern
7420     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7421
7422 (defun gnus-button-handle-describe-variable (url)
7423   "Call `describe-variable' when pushing the corresponding URL button."
7424   (describe-variable
7425    (intern
7426     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7427
7428 (defun gnus-button-handle-symbol (url)
7429 "Display help on variable or function.
7430 Calls `describe-variable' or `describe-function'."
7431   (let ((sym (intern url)))
7432     (cond
7433      ((fboundp sym) (describe-function sym))
7434      ((boundp sym) (describe-variable sym))
7435      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7436
7437 (defun gnus-button-handle-describe-key (url)
7438   "Call `describe-key' when pushing the corresponding URL button."
7439   (let* ((key-string
7440           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7441          (keys (ignore-errors (eval `(kbd ,key-string)))))
7442     (if keys
7443         (describe-key keys)
7444       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7445
7446 (defun gnus-button-handle-apropos (url)
7447   "Call `apropos' when pushing the corresponding URL button."
7448   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7449
7450 (defun gnus-button-handle-apropos-command (url)
7451   "Call `apropos' when pushing the corresponding URL button."
7452   (apropos-command
7453    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7454
7455 (defun gnus-button-handle-apropos-variable (url)
7456   "Call `apropos' when pushing the corresponding URL button."
7457   (funcall
7458    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7459    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7460
7461 (defun gnus-button-handle-apropos-documentation (url)
7462   "Call `apropos' when pushing the corresponding URL button."
7463   (funcall
7464    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7465    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7466
7467 (defun gnus-button-handle-library (url)
7468   "Call `locate-library' when pushing the corresponding URL button."
7469   (gnus-message 9 "url=`%s'" url)
7470   (let* ((lib (locate-library url))
7471          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7472     (if (not lib)
7473         (gnus-message 1 "Cannot locale library `%s'." url)
7474       (find-file-read-only file))))
7475
7476 (defcustom gnus-button-man-level 5
7477   "*Integer that says how many man-related buttons Gnus will show.
7478 The higher the number, the more buttons will appear and the more false
7479 positives are possible.  Note that you can set this variable local to
7480 specific groups.  Setting it higher in Unix groups is probably a good idea.
7481 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7482 how to set variables in specific groups."
7483   :version "22.1"
7484   :group 'gnus-article-buttons
7485   :link '(custom-manual "(gnus)Group Parameters")
7486   :type 'integer)
7487
7488 (defcustom gnus-button-emacs-level 5
7489   "*Integer that says how many emacs-related buttons Gnus will show.
7490 The higher the number, the more buttons will appear and the more false
7491 positives are possible.  Note that you can set this variable local to
7492 specific groups.  Setting it higher in Emacs or Gnus related groups is
7493 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7494 `gnus-parameters' on how to set variables in specific groups."
7495   :version "22.1"
7496   :group 'gnus-article-buttons
7497   :link '(custom-manual "(gnus)Group Parameters")
7498   :type 'integer)
7499
7500 (defcustom gnus-button-message-level 5
7501   "*Integer that says how many buttons for news or mail messages will appear.
7502 The higher the number, the more buttons will appear and the more false
7503 positives are possible."
7504   ;; mail addresses, MIDs, URLs for news, ...
7505   :version "22.1"
7506   :group 'gnus-article-buttons
7507   :type 'integer)
7508
7509 (defcustom gnus-button-browse-level 5
7510   "*Integer that says how many buttons for browsing will appear.
7511 The higher the number, the more buttons will appear and the more false
7512 positives are possible."
7513   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7514   :version "22.1"
7515   :group 'gnus-article-buttons
7516   :type 'integer)
7517
7518 (defcustom gnus-button-alist
7519   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7520      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7521     ((concat "\\b\\(nntp\\|news\\):\\("
7522              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7523      0 t gnus-button-handle-news 2)
7524     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7525      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7526     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7527      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7528     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7529     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7530      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7531     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7532      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7533     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7534      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7535     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7536      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7537     ;; RFC 2368 (The mailto URL scheme)
7538     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7539      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7540     ("\\bmailto:\\([^ \n\t]+\\)"
7541      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7542     ;; Info Konqueror style <info:/foo/bar baz>.
7543     ;; Must come before " Gnus home-grown style".
7544     ("\\binfo://?\\([^'\">\n\t]+\\)"
7545      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7546    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7547     ("\\binfo://\\([^'\">\n\t ]+\\)"
7548      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7549     ;; Info GNOME style <info:foo#bar_baz>
7550     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7551      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7552     ;; Info KDE style <info:(foo)bar baz>
7553     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7554      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7555     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7556      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7557     ("\\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,]*\\)\\)?"
7558      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7559      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7560     ;; This is custom
7561     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7562      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7563     ;; Emacs help commands
7564     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7565      ;; regexp doesn't match arguments containing ` '.
7566      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7567     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7568      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7569     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7570      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7571     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7572      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7573     ;; The following entries may lead to many false positives so don't enable
7574     ;; them by default (use a high button level).
7575     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7576      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7577      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7578     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7579      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7580     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7581      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7582     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7583      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7584     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7585      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7586     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7587      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7588     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7589      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7590     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7591      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7592     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7593      ;; Unlike the other regexps we really have to require quoting
7594      ;; here to determine where it ends.
7595      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7596     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7597     ("<URL: *\\([^\n<>]*\\)>"
7598      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7599     ;; RFC 2396 (2.4.3., delims) ...
7600     ("\"URL: *\\([^\n\"]*\\)\""
7601      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7602     ;; Raw URLs.
7603     (gnus-button-url-regexp
7604      0 (>= gnus-button-browse-level 0) browse-url 0)
7605     ;; man pages
7606     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7607      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7608      gnus-button-handle-man 1)
7609     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7610     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7611      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7612      gnus-button-handle-man 1)
7613     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7614     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7615     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7616      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7617     ;; Recognizing patches to .el files.  This is somewhat obscure,
7618     ;; but considering the percentage of Gnus users who hack Emacs
7619     ;; Lisp files...
7620     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7621      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7622     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7623      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7624     ;; MID or mail: To avoid too many false positives we don't try to catch
7625     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7626     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7627     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7628     ;; so that non-ambiguous entries (see above) match first.
7629     (gnus-button-mid-or-mail-regexp
7630      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7631   "*Alist of regexps matching buttons in article bodies.
7632
7633 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7634 REGEXP: is the string (case insensitive) matching text around the button (can
7635 also be Lisp expression evaluating to a string),
7636 BUTTON: is the number of the regexp grouping actually matching the button,
7637 FORM: is a Lisp expression which must eval to true for the button to
7638 be added,
7639 CALLBACK: is the function to call when the user push this button, and each
7640 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7641
7642 CALLBACK can also be a variable, in that case the value of that
7643 variable it the real callback function."
7644   :group 'gnus-article-buttons
7645   :type '(repeat (list (choice regexp variable sexp)
7646                        (integer :tag "Button")
7647                        (sexp :tag "Form")
7648                        (function :tag "Callback")
7649                        (repeat :tag "Par"
7650                                :inline t
7651                                (integer :tag "Regexp group")))))
7652 (put 'gnus-button-alist 'risky-local-variable t)
7653
7654 (defcustom gnus-header-button-alist
7655   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7656      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7657     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7658      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7659     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7660      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7661     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7662      0 (>= gnus-button-browse-level 0) browse-url 0)
7663     ("^Subject:" gnus-button-url-regexp
7664      0 (>= gnus-button-browse-level 0) browse-url 0)
7665     ("^[^:]+:" gnus-button-url-regexp
7666      0 (>= gnus-button-browse-level 0) browse-url 0)
7667     ("^OpenPGP:.*url=" gnus-button-url-regexp
7668      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7669     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7670      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7671     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7672      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7673   "*Alist of headers and regexps to match buttons in article heads.
7674
7675 This alist is very similar to `gnus-button-alist', except that each
7676 alist has an additional HEADER element first in each entry:
7677
7678 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7679
7680 HEADER is a regexp to match a header.  For a fuller explanation, see
7681 `gnus-button-alist'."
7682   :group 'gnus-article-buttons
7683   :group 'gnus-article-headers
7684   :type '(repeat (list (regexp :tag "Header")
7685                        (choice regexp variable)
7686                        (integer :tag "Button")
7687                        (sexp :tag "Form")
7688                        (function :tag "Callback")
7689                        (repeat :tag "Par"
7690                                :inline t
7691                                (integer :tag "Regexp group")))))
7692 (put 'gnus-header-button-alist 'risky-local-variable t)
7693
7694 ;;; Commands:
7695
7696 (defun gnus-article-push-button (event)
7697   "Check text under the mouse pointer for a callback function.
7698 If the text under the mouse pointer has a `gnus-callback' property,
7699 call it with the value of the `gnus-data' text property."
7700   (interactive "e")
7701   (set-buffer (window-buffer (posn-window (event-start event))))
7702   (let* ((pos (posn-point (event-start event)))
7703          (data (get-text-property pos 'gnus-data))
7704          (fun (get-text-property pos 'gnus-callback)))
7705     (goto-char pos)
7706     (when fun
7707       (funcall fun data))))
7708
7709 (defun gnus-article-press-button ()
7710   "Check text at point for a callback function.
7711 If the text at point has a `gnus-callback' property,
7712 call it with the value of the `gnus-data' text property."
7713   (interactive)
7714   (let ((data (get-text-property (point) 'gnus-data))
7715         (fun (get-text-property (point) 'gnus-callback)))
7716     (when fun
7717       (funcall fun data))))
7718
7719 (defun gnus-article-highlight (&optional force)
7720   "Highlight current article.
7721 This function calls `gnus-article-highlight-headers',
7722 `gnus-article-highlight-citation',
7723 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7724 do the highlighting.  See the documentation for those functions."
7725   (interactive (list 'force))
7726   (gnus-article-highlight-headers)
7727   (gnus-article-highlight-citation force)
7728   (gnus-article-highlight-signature)
7729   (gnus-article-add-buttons)
7730   (gnus-article-add-buttons-to-head))
7731
7732 (defun gnus-article-highlight-some (&optional force)
7733   "Highlight current article.
7734 This function calls `gnus-article-highlight-headers',
7735 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7736 do the highlighting.  See the documentation for those functions."
7737   (interactive (list 'force))
7738   (gnus-article-highlight-headers)
7739   (gnus-article-highlight-signature)
7740   (gnus-article-add-buttons))
7741
7742 (defun gnus-article-highlight-headers ()
7743   "Highlight article headers as specified by `gnus-header-face-alist'."
7744   (interactive)
7745   (gnus-with-article-headers
7746     (let (regexp header-face field-face from hpoints fpoints)
7747       (dolist (entry gnus-header-face-alist)
7748         (goto-char (point-min))
7749         (setq regexp (concat "^\\("
7750                              (if (string-equal "" (nth 0 entry))
7751                                  "[^\t ]"
7752                                (nth 0 entry))
7753                              "\\)")
7754               header-face (nth 1 entry)
7755               field-face (nth 2 entry))
7756         (while (and (re-search-forward regexp nil t)
7757                     (not (eobp)))
7758           (beginning-of-line)
7759           (setq from (point))
7760           (unless (search-forward ":" nil t)
7761             (forward-char 1))
7762           (when (and header-face
7763                      (not (memq (point) hpoints)))
7764             (push (point) hpoints)
7765             (gnus-put-text-property from (point) 'face header-face))
7766           (when (and field-face
7767                      (not (memq (setq from (point)) fpoints)))
7768             (push from fpoints)
7769             (if (re-search-forward "^[^ \t]" nil t)
7770                 (forward-char -2)
7771               (goto-char (point-max)))
7772             (gnus-put-text-property from (point) 'face field-face)))))))
7773
7774 (defun gnus-article-highlight-signature ()
7775   "Highlight the signature in an article.
7776 It does this by highlighting everything after
7777 `gnus-signature-separator' using the face `gnus-signature'."
7778   (interactive)
7779   (gnus-with-article-buffer
7780     (let ((inhibit-point-motion-hooks t))
7781       (save-restriction
7782         (when (and gnus-signature-face
7783                    (gnus-article-narrow-to-signature))
7784           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7785                             'face gnus-signature-face)
7786           (widen)
7787           (gnus-article-search-signature)
7788           (let ((start (match-beginning 0))
7789                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7790             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7791                                      end)))))))
7792
7793 (defun gnus-button-in-region-p (b e prop)
7794   "Say whether PROP exists in the region."
7795   (text-property-not-all b e prop nil))
7796
7797 (defun gnus-article-add-buttons ()
7798   "Find external references in the article and make buttons of them.
7799 \"External references\" are things like Message-IDs and URLs, as
7800 specified by `gnus-button-alist'."
7801   (interactive)
7802   (gnus-with-article-buffer
7803     (let ((inhibit-point-motion-hooks t)
7804           (case-fold-search t)
7805           (alist gnus-button-alist)
7806           beg entry regexp)
7807       ;; We skip the headers.
7808       (article-goto-body)
7809       (setq beg (point))
7810       (while (setq entry (pop alist))
7811         (setq regexp (eval (car entry)))
7812         (goto-char beg)
7813         (while (re-search-forward regexp nil t)
7814           (let ((start (match-beginning (nth 1 entry)))
7815                 (end (match-end (nth 1 entry)))
7816                 (from (match-beginning 0)))
7817             (when (and (eval (nth 2 entry))
7818                        (not (gnus-button-in-region-p
7819                              start end 'gnus-callback)))
7820               ;; That optional form returned non-nil, so we add the
7821               ;; button.
7822               (setq from (set-marker (make-marker) from))
7823               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7824                            (gnus-article-extend-url-button from start end))
7825                 (gnus-article-add-button start end
7826                                          'gnus-button-push (list from entry))
7827                 (gnus-put-text-property
7828                  start end
7829                  'gnus-string (buffer-substring-no-properties
7830                                start end))))))))))
7831
7832 (defun gnus-article-extend-url-button (beg start end)
7833   "Extend url button if url is folded into two or more lines.
7834 Return non-nil if button is extended.  BEG is a marker that points to
7835 the beginning position of a text containing url.  START and END are
7836 the endpoints of a url button before it is extended.  The concatenated
7837 url is put as the `gnus-button-url' overlay property on the button."
7838   (let ((opoint (point))
7839         (points (list start end))
7840         url delim regexp)
7841     (prog1
7842         (when (and (progn
7843                      (goto-char end)
7844                      (not (looking-at "[\t ]*[\">]")))
7845                    (progn
7846                      (goto-char start)
7847                      (string-match
7848                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7849                       (buffer-substring (point-at-bol) start)))
7850                    (progn
7851                      (setq url (list (buffer-substring start end))
7852                            delim (if (match-beginning 1) ">" "\""))
7853                      (beginning-of-line)
7854                      (setq regexp (concat
7855                                    (when (and (looking-at
7856                                                message-cite-prefix-regexp)
7857                                               (< (match-end 0) start))
7858                                      (regexp-quote (match-string 0)))
7859                                    "\
7860 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7861                                    delim "\\)"))
7862                      (while (progn
7863                               (forward-line 1)
7864                               (and (looking-at regexp)
7865                                    (prog1
7866                                        (match-beginning 1)
7867                                      (push (or (match-string 2)
7868                                                (match-string 1))
7869                                            url)
7870                                      (push (setq end (or (match-end 2)
7871                                                          (match-end 1)))
7872                                            points)
7873                                      (push (or (match-beginning 2)
7874                                                (match-beginning 1))
7875                                            points)))))
7876                      (match-beginning 2)))
7877           (let (gnus-article-mouse-face widget-mouse-face)
7878             (while points
7879               (gnus-article-add-button (pop points) (pop points)
7880                                        'gnus-button-push beg)))
7881           (let ((overlay (gnus-make-overlay start end)))
7882             (gnus-overlay-put overlay 'evaporate t)
7883             (gnus-overlay-put overlay 'gnus-button-url
7884                               (list (mapconcat 'identity (nreverse url) "")))
7885             (when gnus-article-mouse-face
7886               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7887           t)
7888       (goto-char opoint))))
7889
7890 ;; Add buttons to the head of an article.
7891 (defun gnus-article-add-buttons-to-head ()
7892   "Add buttons to the head of the article."
7893   (interactive)
7894   (gnus-with-article-headers
7895     (let (beg end)
7896       (dolist (entry gnus-header-button-alist)
7897         ;; Each alist entry.
7898         (goto-char (point-min))
7899         (while (re-search-forward (car entry) nil t)
7900           ;; Each header matching the entry.
7901           (setq beg (match-beginning 0))
7902           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7903                              (match-beginning 0))
7904                         (point-max)))
7905           (goto-char beg)
7906           (while (re-search-forward (eval (nth 1 entry)) end t)
7907             ;; Each match within a header.
7908             (let* ((entry (cdr entry))
7909                    (start (match-beginning (nth 1 entry)))
7910                    (end (match-end (nth 1 entry)))
7911                    (form (nth 2 entry)))
7912               (goto-char (match-end 0))
7913               (when (eval form)
7914                 (gnus-article-add-button
7915                  start end (nth 3 entry)
7916                  (buffer-substring (match-beginning (nth 4 entry))
7917                                    (match-end (nth 4 entry)))))))
7918           (goto-char end))))))
7919
7920 ;;; External functions:
7921
7922 (defun gnus-article-add-button (from to fun &optional data text)
7923   "Create a button between FROM and TO with callback FUN and data DATA."
7924   (when gnus-article-button-face
7925     (gnus-overlay-put (gnus-make-overlay from to nil t)
7926                       'face gnus-article-button-face))
7927   (gnus-add-text-properties
7928    from to
7929    (nconc (and gnus-article-mouse-face
7930                (list gnus-mouse-face-prop gnus-article-mouse-face))
7931           (list 'gnus-callback fun)
7932           (and data (list 'gnus-data data))))
7933   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7934                          :help-echo (or text "Follow the link")
7935                          :keymap gnus-url-button-map
7936                          :button-keymap gnus-widget-button-keymap))
7937
7938 (defun gnus-article-copy-string ()
7939   "Copy the string in the button to the kill ring."
7940   (interactive)
7941   (gnus-article-check-buffer)
7942   (let ((data (get-text-property (point) 'gnus-string)))
7943     (when data
7944       (with-temp-buffer
7945         (insert data)
7946         (copy-region-as-kill (point-min) (point-max))
7947         (message "Copied %s" data)))))
7948
7949 ;;; Internal functions:
7950
7951 (defun gnus-article-set-globals ()
7952   (with-current-buffer gnus-summary-buffer
7953     (gnus-set-global-variables)))
7954
7955 (defun gnus-signature-toggle (end)
7956   (gnus-with-article-buffer
7957     (let ((inhibit-point-motion-hooks t))
7958       (if (text-property-any end (point-max) 'article-type 'signature)
7959           (progn
7960             (gnus-delete-wash-type 'signature)
7961             (gnus-remove-text-properties-when
7962              'article-type 'signature end (point-max)
7963              (cons 'article-type (cons 'signature
7964                                        gnus-hidden-properties))))
7965         (gnus-add-wash-type 'signature)
7966         (gnus-add-text-properties-when
7967          'article-type nil end (point-max)
7968          (cons 'article-type (cons 'signature
7969                                    gnus-hidden-properties)))))
7970     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7971       (gnus-set-mode-line 'article))))
7972
7973 (defun gnus-button-push (marker-and-entry)
7974   ;; Push button starting at MARKER.
7975   (save-excursion
7976     (let* ((marker (car marker-and-entry))
7977            (entry (cadr marker-and-entry))
7978            (regexp (car entry))
7979            (inhibit-point-motion-hooks t))
7980       (goto-char marker)
7981       ;; This is obviously true, or something bad is happening :)
7982       ;; But we need it to have the match-data
7983       (when (looking-at (or (if (symbolp regexp)
7984                                 (symbol-value regexp)
7985                               regexp)))
7986         (let ((fun (nth 3 entry))
7987               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7988                              (get-char-property marker 'gnus-button-url))
7989                         (mapcar (lambda (group)
7990                                   (let ((string (match-string group)))
7991                                     (set-text-properties
7992                                      0 (length string) nil string)
7993                                     string))
7994                                 (nthcdr 4 entry)))))
7995
7996           (cond
7997            ((fboundp fun)
7998             (apply fun args))
7999            ((and (boundp fun)
8000                  (fboundp (symbol-value fun)))
8001             (apply (symbol-value fun) args))
8002            (t
8003             (gnus-message 1 "You must define `%S' to use this button"
8004                           (cons fun args)))))))))
8005
8006 (defun gnus-parse-news-url (url)
8007   (let (scheme server port group message-id articles)
8008     (with-temp-buffer
8009       (insert url)
8010       (goto-char (point-min))
8011       (when (looking-at "\\([A-Za-z]+\\):")
8012         (setq scheme (match-string 1))
8013         (goto-char (match-end 0)))
8014       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8015         (setq server (match-string 1))
8016         (setq port (if (stringp (match-string 3))
8017                        (string-to-number (match-string 3))
8018                      (match-string 3)))
8019         (goto-char (match-end 0)))
8020
8021       (cond
8022        ((looking-at "\\(.*@.*\\)")
8023         (setq message-id (match-string 1)))
8024        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8025         (setq group (match-string 1)
8026               articles (split-string (match-string 2) "-")))
8027        ((looking-at "\\([^/]+\\)/?")
8028         (setq group (match-string 1)))
8029        (t
8030         (error "Unknown news URL syntax"))))
8031     (list scheme server port group message-id articles)))
8032
8033 (defun gnus-button-handle-news (url)
8034   "Fetch a news URL."
8035   (destructuring-bind (scheme server port group message-id articles)
8036       (gnus-parse-news-url url)
8037     (cond
8038      (message-id
8039       (with-current-buffer gnus-summary-buffer
8040         (if server
8041             (let ((gnus-refer-article-method
8042                    (nconc (list (list 'nntp server))
8043                           gnus-refer-article-method))
8044                   (nntp-port-number (or port "nntp")))
8045               (gnus-message 7 "Fetching %s with %s"
8046                             message-id gnus-refer-article-method)
8047               (gnus-summary-refer-article message-id))
8048           (gnus-summary-refer-article message-id))))
8049      (group
8050       (gnus-button-fetch-group url)))))
8051
8052 (defun gnus-button-patch (library line)
8053   "Visit an Emacs Lisp library LIBRARY on line LINE."
8054   (interactive)
8055   (let ((file (locate-library (file-name-nondirectory library))))
8056     (unless file
8057       (error "Couldn't find library %s" library))
8058     (find-file file)
8059     (goto-char (point-min))
8060     (forward-line (1- (string-to-number line)))))
8061
8062 (defun gnus-button-handle-man (url)
8063   "Fetch a man page."
8064   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8065   (when (eq gnus-button-man-handler 'woman)
8066     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8067   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8068   (funcall gnus-button-man-handler url))
8069
8070 (defun gnus-button-handle-info-url (url)
8071   "Fetch an info URL."
8072   (setq url (mm-subst-char-in-string ?+ ?\  url))
8073   (cond
8074    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8075     (gnus-info-find-node
8076      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8077                      "Gnus")
8078              ")" (gnus-url-unhex-string (match-string 2 url)))))
8079    ((string-match "([^)\"]+)[^\"]+" url)
8080     (setq url
8081           (gnus-replace-in-string
8082            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8083     (gnus-info-find-node url))
8084    (t (error "Can't parse %s" url))))
8085
8086 (defun gnus-button-handle-info-url-gnome (url)
8087   "Fetch GNOME style info URL."
8088   (setq url (mm-subst-char-in-string ?_ ?\  url))
8089   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8090       (gnus-info-find-node
8091        (concat "("
8092                (gnus-url-unhex-string
8093                  (match-string 1 url))
8094                ")"
8095                (or (gnus-url-unhex-string
8096                     (match-string 2 url))
8097                    "Top")))
8098     (error "Can't parse %s" url)))
8099
8100 (defun gnus-button-handle-info-url-kde (url)
8101   "Fetch KDE style info URL."
8102   (gnus-info-find-node (gnus-url-unhex-string url)))
8103
8104 ;; (info) will autoload info.el
8105 (declare-function Info-menu "info" (menu-item &optional fork))
8106 (declare-function Info-index-next "info" (num))
8107
8108 (defun gnus-button-handle-info-keystrokes (url)
8109   "Call `info' when pushing the corresponding URL button."
8110   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8111   (let (node indx comma)
8112     (if (string-match
8113          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8114                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8115                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8116                  "\\(?:[ \t\n,]*\\)\\)?")
8117          url)
8118         (setq node (match-string 2 url)
8119               indx (match-string 3 url))
8120       (error "Can't parse %s" url))
8121     (info)
8122     (Info-directory)
8123     (Info-menu node)
8124     (when (> (length indx) 0)
8125       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8126                             "\\([ \t\n,]*\\)")
8127                     indx)
8128       (setq comma (match-string 2 indx))
8129       (setq indx  (match-string 1 indx))
8130       (Info-index indx)
8131       (when comma
8132         (dotimes (i (with-temp-buffer
8133                       (insert comma)
8134                       ;; Note: the XEmacs version of `how-many' takes
8135                       ;; no optional argument.
8136                       (goto-char (point-min))
8137                       (how-many ",")))
8138           (Info-index-next 1)))
8139       nil)))
8140
8141 (autoload 'pgg-snarf-keys-region "pgg")
8142 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8143 (declare-function pgg-display-output-buffer "pgg" (start end status))
8144
8145 (defun gnus-button-openpgp (url)
8146   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8147   (with-temp-buffer
8148     (mm-url-insert-file-contents-external url)
8149     (pgg-snarf-keys-region (point-min) (point-max))
8150     (pgg-display-output-buffer nil nil nil)))
8151
8152 (defun gnus-button-message-id (message-id)
8153   "Fetch MESSAGE-ID."
8154   (with-current-buffer gnus-summary-buffer
8155     (gnus-summary-refer-article message-id)))
8156
8157 (defun gnus-button-fetch-group (address &rest ignore)
8158   "Fetch GROUP specified by ADDRESS."
8159   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8160                       address)
8161     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8162     ;; for nntp:// and news://
8163     (setq address (match-string 3 address)))
8164   (if (not (string-match "[:/]" address))
8165       ;; This is just a simple group url.
8166       (gnus-group-read-ephemeral-group address gnus-select-method)
8167     (if (not
8168          (string-match
8169           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8170           address))
8171         (error "Can't parse %s" address)
8172       (gnus-group-read-ephemeral-group
8173        (match-string 4 address)
8174        `(nntp ,(match-string 1 address)
8175               (nntp-address ,(match-string 1 address))
8176               (nntp-port-number ,(if (match-end 3)
8177                                      (match-string 3 address)
8178                                    "nntp")))
8179        nil nil nil
8180        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8181
8182 (defun gnus-url-parse-query-string (query &optional downcase)
8183   (let (retval pairs cur key val)
8184     (setq pairs (split-string query "&"))
8185     (while pairs
8186       (setq cur (car pairs)
8187             pairs (cdr pairs))
8188       (if (not (string-match "=" cur))
8189           nil                           ; Grace
8190         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8191               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8192         (if downcase
8193             (setq key (downcase key)))
8194         (setq cur (assoc key retval))
8195         (if cur
8196             (setcdr cur (cons val (cdr cur)))
8197           (setq retval (cons (list key val) retval)))))
8198     retval))
8199
8200 (defun gnus-url-mailto (url)
8201   ;; Send mail to someone
8202   (setq url (replace-regexp-in-string "\n" " " url))
8203   (when (string-match "mailto:/*\\(.*\\)" url)
8204     (setq url (substring url (match-beginning 1) nil)))
8205   (let (to args subject func)
8206     (setq args (gnus-url-parse-query-string
8207                 (if (string-match "^\\?" url)
8208                     (substring url 1)
8209                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8210                       (concat "to=" (match-string 1 url) "&"
8211                               (match-string 2 url))
8212                     (concat "to=" url))))
8213           subject (cdr-safe (assoc "subject" args)))
8214     (gnus-msg-mail)
8215     (while args
8216       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8217       (if (fboundp func)
8218           (funcall func)
8219         (message-position-on-field (caar args)))
8220       (insert (gnus-replace-in-string
8221                (mapconcat 'identity (reverse (cdar args)) ", ")
8222                "\r\n" "\n" t))
8223       (setq args (cdr args)))
8224     (if subject
8225         (message-goto-body)
8226       (message-goto-subject))))
8227
8228 (defun gnus-button-embedded-url (address)
8229   "Activate ADDRESS with `browse-url'."
8230   (browse-url (gnus-strip-whitespace address)))
8231
8232 ;;; Next/prev buttons in the article buffer.
8233
8234 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8235 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8236
8237 (defvar gnus-prev-page-map
8238   (let ((map (make-sparse-keymap)))
8239     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8240     (define-key map "\r" 'gnus-button-prev-page)
8241     map))
8242
8243 (defvar gnus-next-page-map
8244   (let ((map (make-sparse-keymap)))
8245     (define-key map gnus-mouse-2 'gnus-button-next-page)
8246     (define-key map "\r" 'gnus-button-next-page)
8247     map))
8248
8249 (defun gnus-insert-prev-page-button ()
8250   (let ((b (point)) e
8251         (inhibit-read-only t))
8252     (gnus-eval-format
8253      gnus-prev-page-line-format nil
8254      `(keymap ,gnus-prev-page-map
8255               gnus-prev t
8256               gnus-callback gnus-article-button-prev-page
8257               article-type annotation))
8258     (setq e (if (bolp)
8259                 ;; Exclude a newline.
8260                 (1- (point))
8261               (point)))
8262     (when gnus-article-button-face
8263       (gnus-overlay-put (gnus-make-overlay b e nil t)
8264                         'face gnus-article-button-face))
8265     (widget-convert-button
8266      'link b e
8267      :action 'gnus-button-prev-page
8268      :button-keymap gnus-prev-page-map)))
8269
8270 (defun gnus-button-next-page (&optional args more-args)
8271   "Go to the next page."
8272   (interactive)
8273   (let ((win (selected-window)))
8274     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8275     (gnus-article-next-page)
8276     (select-window win)))
8277
8278 (defun gnus-button-prev-page (&optional args more-args)
8279   "Go to the prev page."
8280   (interactive)
8281   (let ((win (selected-window)))
8282     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8283     (gnus-article-prev-page)
8284     (select-window win)))
8285
8286 (defun gnus-insert-next-page-button ()
8287   (let ((b (point)) e
8288         (inhibit-read-only t))
8289     (gnus-eval-format gnus-next-page-line-format nil
8290                       `(keymap ,gnus-next-page-map
8291                                gnus-next t
8292                                gnus-callback gnus-article-button-next-page
8293                                article-type annotation))
8294     (setq e (if (bolp)
8295                 ;; Exclude a newline.
8296                 (1- (point))
8297               (point)))
8298     (when gnus-article-button-face
8299       (gnus-overlay-put (gnus-make-overlay b e nil t)
8300                         'face gnus-article-button-face))
8301     (widget-convert-button
8302      'link b e
8303      :action 'gnus-button-next-page
8304      :button-keymap gnus-next-page-map)))
8305
8306 (defun gnus-article-button-next-page (arg)
8307   "Go to the next page."
8308   (interactive "P")
8309   (let ((win (selected-window)))
8310     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8311     (gnus-article-next-page)
8312     (select-window win)))
8313
8314 (defun gnus-article-button-prev-page (arg)
8315   "Go to the prev page."
8316   (interactive "P")
8317   (let ((win (selected-window)))
8318     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8319     (gnus-article-prev-page)
8320     (select-window win)))
8321
8322 (defvar gnus-decode-header-methods
8323   '(mail-decode-encoded-word-region)
8324   "List of methods used to decode headers.
8325
8326 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8327 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8328 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8329 whose names match REGEXP.
8330
8331 For example:
8332 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8333  mail-decode-encoded-word-region
8334  (\"chinese\" . rfc1843-decode-region))
8335 ")
8336
8337 (defvar gnus-decode-header-methods-cache nil)
8338
8339 (defun gnus-multi-decode-header (start end)
8340   "Apply the functions from `gnus-encoded-word-methods' that match."
8341   (unless (and gnus-decode-header-methods-cache
8342                (eq gnus-newsgroup-name
8343                    (car gnus-decode-header-methods-cache)))
8344     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8345     (dolist (x gnus-decode-header-methods)
8346       (if (symbolp x)
8347           (nconc gnus-decode-header-methods-cache (list x))
8348         (if (and gnus-newsgroup-name
8349                  (string-match (car x) gnus-newsgroup-name))
8350             (nconc gnus-decode-header-methods-cache
8351                    (list (cdr x)))))))
8352   (let ((xlist gnus-decode-header-methods-cache))
8353     (pop xlist)
8354     (save-restriction
8355       (narrow-to-region start end)
8356       (while xlist
8357         (funcall (pop xlist) (point-min) (point-max))))))
8358
8359 ;;;
8360 ;;; Treatment top-level handling.
8361 ;;;
8362
8363 (defvar gnus-inhibit-article-treatments nil)
8364
8365 (defun gnus-treat-article (gnus-treat-condition
8366                            &optional part-number total-parts gnus-treat-type)
8367   (let ((gnus-treat-length (- (point-max) (point-min)))
8368         (alist gnus-treatment-function-alist)
8369         (article-goto-body-goes-to-point-min-p t)
8370         (treated-type
8371          (or (not gnus-treat-type)
8372              (catch 'found
8373                (let ((list gnus-article-treat-types))
8374                  (while list
8375                    (when (string-match (pop list) gnus-treat-type)
8376                      (throw 'found t)))))))
8377         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8378         val elem)
8379     (gnus-run-hooks 'gnus-part-display-hook)
8380     (dolist (elem alist)
8381       (setq val
8382             (save-excursion
8383               (when (gnus-buffer-live-p gnus-summary-buffer)
8384                 (set-buffer gnus-summary-buffer))
8385               (symbol-value (car elem))))
8386       (when (and (or (consp val)
8387                      treated-type)
8388                  (or (not gnus-inhibit-article-treatments)
8389                      (eq gnus-treat-condition 'head))
8390                  (gnus-treat-predicate val)
8391                  (or (not (get (car elem) 'highlight))
8392                      highlightp))
8393         (save-restriction
8394           (funcall (cadr elem)))))))
8395
8396 ;; Dynamic variables.
8397 (defvar part-number)
8398 (defvar total-parts)
8399 (defvar gnus-treat-type)
8400 (defvar gnus-treat-condition)
8401 (defvar gnus-treat-length)
8402
8403 (defun gnus-treat-predicate (val)
8404   (cond
8405    ((null val)
8406     nil)
8407    (gnus-treat-condition
8408     (eq gnus-treat-condition val))
8409    ((and (listp val)
8410          (stringp (car val)))
8411     (apply 'gnus-or (mapcar `(lambda (s)
8412                                (string-match s ,(or gnus-newsgroup-name "")))
8413                             val)))
8414    ((listp val)
8415     (let ((pred (pop val)))
8416       (cond
8417        ((eq pred 'or)
8418         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8419        ((eq pred 'and)
8420         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8421        ((eq pred 'not)
8422         (not (gnus-treat-predicate (car val))))
8423        ((eq pred 'typep)
8424         (equal (car val) gnus-treat-type))
8425        (t
8426         (error "%S is not a valid predicate" pred)))))
8427    ((eq val t)
8428     t)
8429    ((eq val 'head)
8430     nil)
8431    ((eq val 'first)
8432     (eq part-number 1))
8433    ((eq val 'last)
8434     (eq part-number total-parts))
8435    ((numberp val)
8436     (< gnus-treat-length val))
8437    (t
8438     (error "%S is not a valid value" val))))
8439
8440 (defun gnus-article-encrypt-body (protocol &optional n)
8441   "Encrypt the article body."
8442   (interactive
8443    (list
8444     (or gnus-article-encrypt-protocol
8445         (gnus-completing-read "Encrypt protocol"
8446                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8447                               t))
8448     current-prefix-arg))
8449   ;; User might hit `K E' instead of `K e', so prompt once.
8450   (when (and gnus-article-encrypt-protocol
8451              gnus-novice-user)
8452     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8453       (error "Encrypt aborted")))
8454   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8455     (unless func
8456       (error "Can't find the encrypt protocol %s" protocol))
8457     (if (member gnus-newsgroup-name '("nndraft:delayed"
8458                                       "nndraft:drafts"
8459                                       "nndraft:queue"))
8460         (error "Can't encrypt the article in group %s"
8461                gnus-newsgroup-name))
8462     (gnus-summary-iterate n
8463       (with-current-buffer gnus-summary-buffer
8464         (let ((mail-parse-charset gnus-newsgroup-charset)
8465               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8466               (summary-buffer gnus-summary-buffer)
8467               references point)
8468           (gnus-set-global-variables)
8469           (when (gnus-group-read-only-p)
8470             (error "The current newsgroup does not support article encrypt"))
8471           (gnus-summary-show-article t)
8472           (setq references
8473               (or (mail-header-references gnus-current-headers) ""))
8474           (set-buffer gnus-article-buffer)
8475           (let* ((inhibit-read-only t)
8476                  (headers
8477                   (mapcar (lambda (field)
8478                             (and (save-restriction
8479                                    (message-narrow-to-head)
8480                                    (goto-char (point-min))
8481                                    (search-forward field nil t))
8482                                  (prog2
8483                                      (message-narrow-to-field)
8484                                      (buffer-string)
8485                                    (delete-region (point-min) (point-max))
8486                                    (widen))))
8487                           '("Content-Type:" "Content-Transfer-Encoding:"
8488                             "Content-Disposition:"))))
8489             (message-narrow-to-head)
8490             (message-remove-header "MIME-Version")
8491             (goto-char (point-max))
8492             (setq point (point))
8493             (insert (apply 'concat headers))
8494             (widen)
8495             (narrow-to-region point (point-max))
8496             (let ((message-options message-options))
8497               (message-options-set 'message-sender user-mail-address)
8498               (message-options-set 'message-recipients user-mail-address)
8499               (message-options-set 'message-sign-encrypt 'not)
8500               (funcall func))
8501             (goto-char (point-min))
8502             (insert "MIME-Version: 1.0\n")
8503             (widen)
8504             (gnus-summary-edit-article-done
8505              references nil summary-buffer t))
8506           (when gnus-keep-backlog
8507             (gnus-backlog-remove-article
8508              (car gnus-article-current) (cdr gnus-article-current)))
8509           (gnus-flush-original-article-buffer)
8510           (when gnus-use-cache
8511             (gnus-cache-update-article
8512              (car gnus-article-current) (cdr gnus-article-current))))))))
8513
8514 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8515   "The following specs can be used:
8516 %t  The security MIME type
8517 %i  Additional info
8518 %d  Details
8519 %D  Details if button is pressed")
8520
8521 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%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-line-format-alist
8529   '((?t gnus-tmp-type ?s)
8530     (?i gnus-tmp-info ?s)
8531     (?d gnus-tmp-details ?s)
8532     (?D gnus-tmp-pressed-details ?s)))
8533
8534 (defvar gnus-mime-security-button-commands
8535   '((gnus-article-press-button "\r" "Show Detail")
8536     (undefined "v")
8537     (undefined "t")
8538     (undefined "C")
8539     (gnus-mime-security-save-part "o" "Save...")
8540     (undefined "\C-o")
8541     (undefined "r")
8542     (undefined "d")
8543     (undefined "c")
8544     (undefined "i")
8545     (undefined "E")
8546     (undefined "e")
8547     (undefined "p")
8548     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8549     (undefined ".")))
8550
8551 (defvar gnus-mime-security-button-map
8552   (let ((map (make-sparse-keymap)))
8553     (define-key map gnus-mouse-2 'gnus-article-push-button)
8554     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8555     (dolist (c gnus-mime-security-button-commands)
8556       (define-key map (cadr c) (car c)))
8557     map))
8558
8559 (easy-menu-define
8560   gnus-mime-security-button-menu gnus-mime-security-button-map
8561   "Security button menu."
8562   `("Security Part"
8563     ,@(delq nil
8564             (mapcar (lambda (c)
8565                       (unless (eq (car c) 'undefined)
8566                         (vector (caddr c) (car c) :active t)))
8567                     gnus-mime-security-button-commands))))
8568
8569 (defun gnus-mime-security-button-menu (event prefix)
8570   "Construct a context-sensitive menu of security commands."
8571   (interactive "e\nP")
8572   (save-window-excursion
8573     (let ((pos (event-start event)))
8574       (select-window (posn-window pos))
8575       (goto-char (posn-point pos))
8576       (gnus-article-check-buffer)
8577       (popup-menu gnus-mime-security-button-menu nil prefix))))
8578
8579 (defvar gnus-mime-security-details-buffer nil)
8580
8581 (defvar gnus-mime-security-button-pressed nil)
8582
8583 (defvar gnus-mime-security-show-details-inline t
8584   "If non-nil, show details in the article buffer.")
8585
8586 (defun gnus-mime-security-verify-or-decrypt (handle)
8587   (mm-remove-parts (cdr handle))
8588   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8589         point (inhibit-read-only t))
8590     (if region
8591         (goto-char (car region)))
8592     (setq point (point))
8593     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8594       (let* ((mm-verify-option 'known)
8595              (mm-decrypt-option 'known)
8596              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8597         (unless (eq nparts (cdr handle))
8598           (mm-destroy-parts (cdr handle))
8599           (setcdr handle nparts))))
8600     (gnus-mime-display-security handle)
8601     (when region
8602       (delete-region (point) (cdr region))
8603       (set-marker (car region) nil)
8604       (set-marker (cdr region) nil))
8605     (goto-char point)))
8606
8607 (defun gnus-mime-security-show-details (handle)
8608   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8609     (if (not details)
8610         (gnus-message 5 "No details.")
8611       (if gnus-mime-security-show-details-inline
8612           (let ((gnus-mime-security-button-pressed
8613                  (not (get-text-property (point) 'gnus-mime-details)))
8614                 (gnus-mime-security-button-line-format
8615                  (get-text-property (point) 'gnus-line-format))
8616                 (inhibit-read-only t))
8617             (forward-char -1)
8618             (while (eq (get-text-property (point) 'gnus-line-format)
8619                        gnus-mime-security-button-line-format)
8620               (forward-char -1))
8621             (forward-char)
8622             (save-restriction
8623               (narrow-to-region (point) (point))
8624               (gnus-insert-mime-security-button handle))
8625             (delete-region (point)
8626                            (or (text-property-not-all
8627                                 (point) (point-max)
8628                                 'gnus-line-format
8629                                 gnus-mime-security-button-line-format)
8630                                (point-max))))
8631         ;; Not inlined.
8632         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8633             (with-current-buffer gnus-mime-security-details-buffer
8634               (erase-buffer)
8635               t)
8636           (setq gnus-mime-security-details-buffer
8637                 (gnus-get-buffer-create "*MIME Security Details*")))
8638         (with-current-buffer gnus-mime-security-details-buffer
8639           (insert details)
8640           (goto-char (point-min)))
8641         (pop-to-buffer gnus-mime-security-details-buffer)))))
8642
8643 (defun gnus-mime-security-press-button (handle)
8644   (save-excursion
8645     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8646         (gnus-mime-security-show-details handle)
8647       (gnus-mime-security-verify-or-decrypt handle))))
8648
8649 (defun gnus-insert-mime-security-button (handle &optional displayed)
8650   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8651          (gnus-tmp-type
8652           (concat
8653            (or (nth 2 (assoc protocol mm-verify-function-alist))
8654                (nth 2 (assoc protocol mm-decrypt-function-alist))
8655                "Unknown")
8656            (if (equal (car handle) "multipart/signed")
8657                " Signed" " Encrypted")
8658            " Part"))
8659          (gnus-tmp-info
8660           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8661               "Undecided"))
8662          (gnus-tmp-details
8663           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8664          gnus-tmp-pressed-details
8665          b e)
8666     (setq gnus-tmp-details
8667           (if gnus-tmp-details
8668               (concat "\n" gnus-tmp-details)
8669             ""))
8670     (setq gnus-tmp-pressed-details
8671           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8672     (unless (bolp)
8673       (insert "\n"))
8674     (setq b (point))
8675     (gnus-eval-format
8676      gnus-mime-security-button-line-format
8677      gnus-mime-security-button-line-format-alist
8678      `(keymap ,gnus-mime-security-button-map
8679          gnus-callback gnus-mime-security-press-button
8680          gnus-line-format ,gnus-mime-security-button-line-format
8681          gnus-mime-details ,gnus-mime-security-button-pressed
8682          article-type annotation
8683          gnus-data ,handle))
8684     (setq e (if (bolp)
8685                 ;; Exclude a newline.
8686                 (1- (point))
8687               (point)))
8688     (when gnus-article-button-face
8689       (gnus-overlay-put (gnus-make-overlay b e nil t)
8690                         'face gnus-article-button-face))
8691     (widget-convert-button
8692      'link b e
8693      :mime-handle handle
8694      :action 'gnus-widget-press-button
8695      :button-keymap gnus-mime-security-button-map
8696      :help-echo
8697      (lambda (widget)
8698        ;; Needed to properly clear the message due to a bug in
8699        ;; wid-edit (XEmacs only).
8700        (when (boundp 'help-echo-owns-message)
8701          (setq help-echo-owns-message t))
8702        (format
8703         "%S: show detail; %S: more options"
8704         (aref gnus-mouse-2 0)
8705         (aref gnus-down-mouse-3 0))))))
8706
8707 (defun gnus-mime-display-security (handle)
8708   (save-restriction
8709     (narrow-to-region (point) (point))
8710     (unless (gnus-unbuttonized-mime-type-p (car handle))
8711       (gnus-insert-mime-security-button handle))
8712     (gnus-mime-display-part (cadr handle))
8713     (unless (bolp)
8714       (insert "\n"))
8715     (unless (gnus-unbuttonized-mime-type-p (car handle))
8716       (let ((gnus-mime-security-button-line-format
8717              gnus-mime-security-button-end-line-format))
8718         (gnus-insert-mime-security-button handle)))
8719     (mm-set-handle-multipart-parameter
8720      handle 'gnus-region
8721      (cons (set-marker (make-marker) (point-min))
8722            (set-marker (make-marker) (point-max))))
8723     (goto-char (point-max))))
8724
8725 (defun gnus-mime-security-run-function (function)
8726   "Run FUNCTION with the security part under point."
8727   (gnus-article-check-buffer)
8728   (let ((data (get-text-property (point) 'gnus-data))
8729         buffer handle)
8730     (when (and (stringp (car-safe data))
8731                (setq buffer (mm-handle-multipart-original-buffer data))
8732                (setq handle (cadr data)))
8733       (if (bufferp (mm-handle-buffer handle))
8734           (progn
8735             (setq handle (cons buffer (copy-sequence (cdr handle))))
8736             (mm-handle-set-undisplayer handle nil))
8737         (setq handle (mm-make-handle
8738                       buffer
8739                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8740                       nil nil nil nil nil nil)))
8741       (funcall function handle))))
8742
8743 (defun gnus-mime-security-save-part ()
8744   "Save the security part under point."
8745   (interactive)
8746   (gnus-mime-security-run-function 'mm-save-part))
8747
8748 (defun gnus-mime-security-pipe-part ()
8749   "Pipe the security part under point to a process."
8750   (interactive)
8751   (gnus-mime-security-run-function 'mm-pipe-part))
8752
8753 (gnus-ems-redefine)
8754
8755 (provide 'gnus-art)
8756
8757 (run-hooks 'gnus-art-load-hook)
8758
8759 ;;; gnus-art.el ends here