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