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