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