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