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