gnus-art.el (gnus-mime-delete-part): Fix Lisp type of byte(s).
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
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      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^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:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-fetch-partial-articles nil
261   "If non-nil, Gnus will fetch partial articles.
262 If t, nnimap will fetch only the first part.  If a string, it
263 will fetch all parts that have types that match that string.  A
264 likely value would be \"text/\" to automatically fetch all
265 textual parts.
266
267 Currently only the nnimap backend actually supports partial
268 article fetching.  If the backend doesn't support it, it has no
269 effect."
270   :version "24.1"
271   :type '(choice (const nil)
272                  (const t)
273                  (regexp))
274   :group 'gnus-article)
275
276 (defcustom gnus-hidden-properties '(invisible t intangible t)
277   "Property list to use for hiding text."
278   :type 'sexp
279   :group 'gnus-article-hiding)
280
281 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
282 ;; frames in a session.
283 (defcustom gnus-article-x-face-command
284   (if (featurep 'xemacs)
285       (if (or (gnus-image-type-available-p 'xface)
286               (gnus-image-type-available-p 'pbm))
287           'gnus-display-x-face-in-from
288         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
289     (if (gnus-image-type-available-p 'pbm)
290         'gnus-display-x-face-in-from
291       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
292 display -"))
293   "*String or function to be executed to display an X-Face header.
294 If it is a string, the command will be executed in a sub-shell
295 asynchronously.  The compressed face will be piped to this command."
296   :type `(choice string
297                  (function-item gnus-display-x-face-in-from)
298                  function)
299   :version "21.1"
300   :group 'gnus-picon
301   :group 'gnus-article-washing)
302
303 (defcustom gnus-article-x-face-too-ugly nil
304   "Regexp matching posters whose face shouldn't be shown automatically."
305   :type '(choice regexp (const nil))
306   :group 'gnus-article-washing)
307
308 (defcustom gnus-article-banner-alist nil
309   "Banner alist for stripping.
310 For example,
311      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
312   :version "21.1"
313   :type '(repeat (cons symbol regexp))
314   :group 'gnus-article-washing)
315
316 (gnus-define-group-parameter
317  banner
318  :variable-document
319  "Alist of regexps (to match group names) and banner."
320  :variable-group gnus-article-washing
321  :parameter-type
322  '(choice :tag "Banner"
323           :value nil
324           (const :tag "Remove signature" signature)
325           (symbol :tag "Item in `gnus-article-banner-alist'" none)
326           regexp
327           (const :tag "None" nil))
328  :parameter-document
329  "If non-nil, specify how to remove `banners' from articles.
330
331 Symbol `signature' means to remove signatures delimited by
332 `gnus-signature-separator'.  Any other symbol is used to look up a
333 regular expression to match the banner in `gnus-article-banner-alist'.
334 A string is used as a regular expression to match the banner
335 directly.")
336
337 (defcustom gnus-article-address-banner-alist nil
338   "Alist of mail addresses and banners.
339 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
340 to match a mail address in the From: header, BANNER is one of a symbol
341 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
342 If ADDRESS matches author's mail address, it will remove things like
343 advertisements.  For example:
344
345 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
346 "
347   :type '(repeat
348           (cons
349            (regexp :tag "Address")
350            (choice :tag "Banner" :value nil
351                    (const :tag "Remove signature" signature)
352                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
353                    regexp
354                    (const :tag "None" nil))))
355   :version "22.1"
356   :group 'gnus-article-washing)
357
358 (defmacro gnus-emphasis-custom-with-format (&rest body)
359   `(let ((format "\
360 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
361 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
362      ,@body))
363
364 (defun gnus-emphasis-custom-value-to-external (value)
365   (gnus-emphasis-custom-with-format
366    (if (consp (car value))
367        (list (format format (car (car value)) (cdr (car value)))
368              2
369              (if (nth 1 value) 2 3)
370              (nth 2 value))
371      value)))
372
373 (defun gnus-emphasis-custom-value-to-internal (value)
374   (gnus-emphasis-custom-with-format
375    (let ((regexp (concat "\\`"
376                          (format (regexp-quote format)
377                                  "\\([^()]+\\)" "\\([^()]+\\)")
378                          "\\'"))
379          pattern)
380      (if (string-match regexp (setq pattern (car value)))
381          (list (cons (match-string 1 pattern) (match-string 2 pattern))
382                (= (nth 2 value) 2)
383                (nth 3 value))
384        value))))
385
386 (defcustom gnus-emphasis-alist
387   (let ((types
388          '(("\\*" "\\*" bold nil 2)
389            ("_" "_" underline)
390            ("/" "/" italic)
391            ("_/" "/_" underline-italic)
392            ("_\\*" "\\*_" underline-bold)
393            ("\\*/" "/\\*" bold-italic)
394            ("_\\*/" "/\\*_" underline-bold-italic))))
395     (nconc
396      (gnus-emphasis-custom-with-format
397       (mapcar (lambda (spec)
398                 (list (format format (car spec) (cadr spec))
399                       (or (nth 3 spec) 2)
400                       (or (nth 4 spec) 3)
401                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
402               types))
403      '(;; I've never seen anyone use this strikethru convention whereas I've
404        ;; several times seen it triggered by normal text.  --Stef
405        ;; Miles suggests that this form is sometimes used but for italics,
406        ;; so maybe we should map it to `italic'.
407        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
408        ;; 2 3 gnus-emphasis-strikethru)
409        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
410         2 3 gnus-emphasis-underline))))
411   "*Alist that says how to fontify certain phrases.
412 Each item looks like this:
413
414   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
415
416 The first element is a regular expression to be matched.  The second
417 is a number that says what regular expression grouping used to find
418 the entire emphasized word.  The third is a number that says what
419 regexp grouping should be displayed and highlighted.  The fourth
420 is the face used for highlighting."
421   :type
422   '(repeat
423     (menu-choice
424      :format "%[Customizing Style%]\n%v"
425      :indent 2
426      (group :tag "Default"
427             :value ("" 0 0 default)
428             :value-create
429             (lambda (widget)
430               (let ((value (widget-get
431                             (cadr (widget-get (widget-get widget :parent)
432                                               :args))
433                             :value)))
434                 (if (not (eq (nth 2 value) 'default))
435                     (widget-put
436                      widget
437                      :value
438                      (gnus-emphasis-custom-value-to-external value))))
439               (widget-group-value-create widget))
440             regexp
441             (integer :format "Match group: %v")
442             (integer :format "Emphasize group: %v")
443             face)
444      (group :tag "Simple"
445             :value (("_" . "_") nil default)
446             (cons :format "%v"
447                   (regexp :format "Start regexp: %v")
448                   (regexp :format "End regexp: %v"))
449             (boolean :format "Show start and end patterns: %[%v%]\n"
450                      :on " On " :off " Off ")
451             face)))
452   :get (lambda (symbol)
453          (mapcar 'gnus-emphasis-custom-value-to-internal
454                  (default-value symbol)))
455   :set (lambda (symbol value)
456          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
457                                      value)))
458   :group 'gnus-article-emphasis)
459
460 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
461   "A regexp to describe whitespace which should not be emphasized.
462 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
463 The former avoids underlining of leading and trailing whitespace,
464 and the latter avoids underlining any whitespace at all."
465   :version "21.1"
466   :group 'gnus-article-emphasis
467   :type 'regexp)
468
469 (defface gnus-emphasis-bold '((t (:bold t)))
470   "Face used for displaying strong emphasized text (*word*)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-italic '((t (:italic t)))
474   "Face used for displaying italic emphasized text (/word/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline '((t (:underline t)))
478   "Face used for displaying underlined emphasized text (_word_)."
479   :group 'gnus-article-emphasis)
480
481 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
482   "Face used for displaying underlined bold emphasized text (_*word*_)."
483   :group 'gnus-article-emphasis)
484
485 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
486   "Face used for displaying underlined italic emphasized text (_/word/_)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
490   "Face used for displaying bold italic emphasized text (/*word*/)."
491   :group 'gnus-article-emphasis)
492
493 (defface gnus-emphasis-underline-bold-italic
494   '((t (:bold t :italic t :underline t)))
495   "Face used for displaying underlined bold italic emphasized text.
496 Example: (_/*word*/_)."
497   :group 'gnus-article-emphasis)
498
499 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
500                                       '((t (:strikethru t)))
501                                     '((t (:strike-through t))))
502   "Face used for displaying strike-through text (-word-)."
503   :group 'gnus-article-emphasis)
504
505 (defface gnus-emphasis-highlight-words
506   '((t (:background "black" :foreground "yellow")))
507   "Face used for displaying highlighted words."
508   :group 'gnus-article-emphasis)
509
510 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
511   "Format for display of Date headers in article bodies.
512 See `format-time-string' for the possible values.
513
514 The variable can also be function, which should return a complete Date
515 header.  The function is called with one argument, the time, which can
516 be fed to `format-time-string'."
517   :type '(choice string function)
518   :link '(custom-manual "(gnus)Article Date")
519   :group 'gnus-article-washing)
520
521 (defcustom gnus-save-all-headers t
522   "*If non-nil, don't remove any headers before saving.
523 This will be overridden by the `:headers' property that the symbol of
524 the saver function, which is specified by `gnus-default-article-saver',
525 might have."
526   :group 'gnus-article-saving
527   :type 'boolean)
528
529 (defcustom gnus-prompt-before-saving 'always
530   "*This variable says how much prompting is to be done when saving articles.
531 If it is nil, no prompting will be done, and the articles will be
532 saved to the default files.  If this variable is `always', each and
533 every article that is saved will be preceded by a prompt, even when
534 saving large batches of articles.  If this variable is neither nil not
535 `always', there the user will be prompted once for a file name for
536 each invocation of the saving commands."
537   :group 'gnus-article-saving
538   :type '(choice (item always)
539                  (item :tag "never" nil)
540                  (sexp :tag "once" :format "%t\n" :value t)))
541
542 (defcustom gnus-saved-headers gnus-visible-headers
543   "Headers to keep if `gnus-save-all-headers' is nil.
544 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
545 If that variable is nil, however, all headers that match this regexp
546 will be kept while the rest will be deleted before saving.  This and
547 `gnus-save-all-headers' will be overridden by the `:headers' property
548 that the symbol of the saver function, which is specified by
549 `gnus-default-article-saver', might have."
550   :group 'gnus-article-saving
551   :type 'regexp)
552
553 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
554 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
555   "A function to save articles in your favourite format.
556 The function will be called by way of the `gnus-summary-save-article'
557 command, and friends such as `gnus-summary-save-article-rmail'.
558
559 Gnus provides the following functions:
560
561 * gnus-summary-save-in-rmail (Rmail format)
562 * gnus-summary-save-in-mail (Unix mail format)
563 * gnus-summary-save-in-folder (MH folder)
564 * gnus-summary-save-in-file (article format)
565 * gnus-summary-save-body-in-file (article body)
566 * gnus-summary-save-in-vm (use VM's folder format)
567 * gnus-summary-write-to-file (article format -- overwrite)
568 * gnus-summary-write-body-to-file (article body -- overwrite)
569 * gnus-summary-save-in-pipe (article format)
570
571 The symbol of each function may have the following properties:
572
573 * :decode
574 The value non-nil means save decoded articles.  This is meaningful
575 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
576 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
577 `gnus-summary-save-in-pipe'.
578
579 * :function
580 The value specifies an alternative function which appends, not
581 overwrites, articles to a file.  This implies that when saving many
582 articles at a time, `gnus-prompt-before-saving' is bound to t and all
583 articles are saved in a single file.  This is meaningful only with
584 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
585
586 * :headers
587 The value specifies the symbol of a variable of which the value
588 specifies headers to be saved.  If it is omitted,
589 `gnus-save-all-headers' and `gnus-saved-headers' control what
590 headers should be saved."
591   :group 'gnus-article-saving
592   :type '(radio (function-item gnus-summary-save-in-rmail)
593                 (function-item gnus-summary-save-in-mail)
594                 (function-item gnus-summary-save-in-folder)
595                 (function-item gnus-summary-save-in-file)
596                 (function-item gnus-summary-save-body-in-file)
597                 (function-item gnus-summary-save-in-vm)
598                 (function-item gnus-summary-write-to-file)
599                 (function-item gnus-summary-write-body-to-file)
600                 (function-item gnus-summary-save-in-pipe)
601                 (function)))
602
603 (defcustom gnus-article-save-coding-system
604   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
605       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
606       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
607       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
608   "Coding system used to save decoded articles to a file.
609
610 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
611 which can safely encode any characters in text.  This is used by the
612 commands including:
613
614 * gnus-summary-save-article-file
615 * gnus-summary-save-article-body-file
616 * gnus-summary-write-article-file
617 * gnus-summary-write-article-body-file
618
619 and the functions to which you may set `gnus-default-article-saver':
620
621 * gnus-summary-save-in-file
622 * gnus-summary-save-body-in-file
623 * gnus-summary-write-to-file
624 * gnus-summary-write-body-to-file
625
626 Those commands and functions save just text displayed in the article
627 buffer to a file if the value of this variable is non-nil.  Note that
628 buttonized MIME parts will be lost in a saved file in that case.
629 Otherwise, raw articles will be saved."
630   :group 'gnus-article-saving
631   :type `(choice
632           :format "%{%t%}:\n %[Value Menu%] %v"
633           (const :tag "Save raw articles" nil)
634           ,@(delq nil
635                   (mapcar
636                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
637                    '((const :tag "UTF-8" utf-8)
638                      (const :tag "iso-2022-7bit" iso-2022-7bit)
639                      (const :tag "Emacs internal" emacs-mule)
640                      (const :tag "escape-quoted" escape-quoted))))
641           (symbol :tag "Coding system")))
642
643 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
644   "A function generating a file name to save articles in Rmail format.
645 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
646   :group 'gnus-article-saving
647   :type 'function)
648
649 (defcustom gnus-mail-save-name 'gnus-plain-save-name
650   "A function generating a file name to save articles in Unix mail format.
651 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-folder-save-name 'gnus-folder-save-name
656   "A function generating a file name to save articles in MH folder.
657 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
658   :group 'gnus-article-saving
659   :type 'function)
660
661 (defcustom gnus-file-save-name 'gnus-numeric-save-name
662   "A function generating a file name to save articles in article format.
663 The function is called with NEWSGROUP, HEADERS, and optional
664 LAST-FILE."
665   :group 'gnus-article-saving
666   :type 'function)
667
668 (defcustom gnus-split-methods
669   '((gnus-article-archive-name)
670     (gnus-article-nndoc-name))
671   "*Variable used to suggest where articles are to be saved.
672 For instance, if you would like to save articles related to Gnus in
673 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
674 you could set this variable to something like:
675
676  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
677    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
678
679 This variable is an alist where the key is the match and the
680 value is a list of possible files to save in if the match is
681 non-nil.
682
683 If the match is a string, it is used as a regexp match on the
684 article.  If the match is a symbol, that symbol will be funcalled
685 from the buffer of the article to be saved with the newsgroup as the
686 parameter.  If it is a list, it will be evaled in the same buffer.
687
688 If this form or function returns a string, this string will be used as a
689 possible file name; and if it returns a non-nil list, that list will be
690 used as possible file names."
691   :group 'gnus-article-saving
692   :type '(repeat (choice (list :value (fun) function)
693                          (cons :value ("" "") regexp (repeat string))
694                          (sexp :value nil))))
695
696 (defcustom gnus-page-delimiter "^\^L"
697   "*Regexp describing what to use as article page delimiters.
698 The default value is \"^\^L\", which is a form linefeed at the
699 beginning of a line."
700   :type 'regexp
701   :group 'gnus-article-various)
702
703 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
704   "*The format specification for the article mode line.
705 See `gnus-summary-mode-line-format' for a closer description.
706
707 The following additional specs are available:
708
709 %w  The article washing status.
710 %m  The number of MIME parts in the article."
711   :type 'string
712   :group 'gnus-article-various)
713
714 (defcustom gnus-article-mode-hook nil
715   "*A hook for Gnus article mode."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (when (featurep 'xemacs)
720   ;; Extracted from gnus-xmas-define in order to preserve user settings
721   (when (fboundp 'turn-off-scroll-in-place)
722     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
723   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
724   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
725
726 (defcustom gnus-article-menu-hook nil
727   "*Hook run after the creation of the article mode menu."
728   :type 'hook
729   :group 'gnus-article-various)
730
731 (defcustom gnus-article-prepare-hook nil
732   "*A hook called after an article has been prepared in the article buffer."
733   :type 'hook
734   :group 'gnus-article-various)
735
736 (defcustom gnus-copy-article-ignored-headers nil
737   "List of headers to be removed when copying an article.
738 Each element is a regular expression."
739   :version "23.1" ;; No Gnus
740   :type '(repeat regexp)
741   :group 'gnus-article-various)
742
743 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
744                         "Gnus 5.10 (Emacs-22.1)")
745
746 (defface gnus-button
747   '((t (:weight bold)))
748   "Face used for highlighting a button in the article buffer."
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-article-button-face 'gnus-button
752   "Face used for highlighting buttons in the article buffer.
753
754 An article button is a piece of text that you can activate by pressing
755 `RET' or `mouse-2' above it."
756   :type 'face
757   :group 'gnus-article-buttons)
758
759 (defcustom gnus-article-mouse-face 'highlight
760   "Face used for mouse highlighting in the article buffer.
761
762 Article buttons will be displayed in this face when the cursor is
763 above them."
764   :type 'face
765   :group 'gnus-article-buttons)
766
767 (defcustom gnus-signature-face 'gnus-signature
768   "Face used for highlighting a signature in the article buffer.
769 Obsolete; use the face `gnus-signature' for customizations instead."
770   :type 'face
771   :group 'gnus-article-highlight
772   :group 'gnus-article-signature)
773
774 (defface gnus-signature
775   '((t
776      (:italic t)))
777   "Face used for highlighting a signature in the article buffer."
778   :group 'gnus-article-highlight
779   :group 'gnus-article-signature)
780 ;; backward-compatibility alias
781 (put 'gnus-signature-face 'face-alias 'gnus-signature)
782 (put 'gnus-signature-face 'obsolete-face "22.1")
783
784 (defface gnus-header-from
785   '((((class color)
786       (background dark))
787      (:foreground "PaleGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red3"))
791     (t
792      (:italic t)))
793   "Face used for displaying from headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
798 (put 'gnus-header-from-face 'obsolete-face "22.1")
799
800 (defface gnus-header-subject
801   '((((class color)
802       (background dark))
803      (:foreground "SeaGreen1"))
804     (((class color)
805       (background light))
806      (:foreground "red4"))
807     (t
808      (:bold t :italic t)))
809   "Face used for displaying subject headers."
810   :group 'gnus-article-headers
811   :group 'gnus-article-highlight)
812 ;; backward-compatibility alias
813 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
814 (put 'gnus-header-subject-face 'obsolete-face "22.1")
815
816 (defface gnus-header-newsgroups
817   '((((class color)
818       (background dark))
819      (:foreground "yellow" :italic t))
820     (((class color)
821       (background light))
822      (:foreground "MidnightBlue" :italic t))
823     (t
824      (:italic t)))
825   "Face used for displaying newsgroups headers.
826 In the default setup this face is only used for crossposted
827 articles."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
832 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
833
834 (defface gnus-header-name
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen2"))
838     (((class color)
839       (background light))
840      (:foreground "maroon"))
841     (t
842      (:bold t)))
843   "Face used for displaying header names."
844   :group 'gnus-article-headers
845   :group 'gnus-article-highlight)
846 ;; backward-compatibility alias
847 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
848 (put 'gnus-header-name-face 'obsolete-face "22.1")
849
850 (defface gnus-header-content
851   '((((class color)
852       (background dark))
853      (:foreground "SpringGreen1" :italic t))
854     (((class color)
855       (background light))
856      (:foreground "indianred4" :italic t))
857     (t
858      (:italic t)))  "Face used for displaying header content."
859   :group 'gnus-article-headers
860   :group 'gnus-article-highlight)
861 ;; backward-compatibility alias
862 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
863 (put 'gnus-header-content-face 'obsolete-face "22.1")
864
865 (defcustom gnus-header-face-alist
866   '(("From" nil gnus-header-from)
867     ("Subject" nil gnus-header-subject)
868     ("Newsgroups:.*," nil gnus-header-newsgroups)
869     ("" gnus-header-name gnus-header-content))
870   "*Controls highlighting of article headers.
871
872 An alist of the form (HEADER NAME CONTENT).
873
874 HEADER is a regular expression which should match the name of a
875 header and NAME and CONTENT are either face names or nil.
876
877 The name of each header field will be displayed using the face
878 specified by the first element in the list where HEADER matches
879 the header name and NAME is non-nil.  Similarly, the content will
880 be displayed by the first non-nil matching CONTENT face."
881   :group 'gnus-article-headers
882   :group 'gnus-article-highlight
883   :type '(repeat (list (regexp :tag "Header")
884                        (choice :tag "Name"
885                                (item :tag "skip" nil)
886                                (face :value default))
887                        (choice :tag "Content"
888                                (item :tag "skip" nil)
889                                (face :value default)))))
890
891 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
892                                           '((xface . (:face gnus-x-face)))
893                                         '((pbm . (:face gnus-x-face))
894                                           (png . nil)))
895   "Alist of image types and properties applied to Face and X-Face images.
896 Here are examples:
897
898 ;; Specify the altitude of Face images in the From header.
899 \(setq gnus-face-properties-alist
900       '((pbm . (:face gnus-x-face :ascent 80))
901         (png . (:ascent 80))))
902
903 ;; Show Face images as pressed buttons.
904 \(setq gnus-face-properties-alist
905       '((pbm . (:face gnus-x-face :relief -2))
906         (png . (:relief -2))))
907
908 See the manual for the valid properties for various image types.
909 Currently, `pbm' is used for X-Face images and `png' is used for Face
910 images in Emacs.  Only the `:face' property is effective on the `xface'
911 image type in XEmacs if it is built with the libcompface library."
912   :version "23.1" ;; No Gnus
913   :group 'gnus-article-headers
914   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
915
916 (defcustom gnus-article-decode-hook
917   '(article-decode-charset article-decode-encoded-words
918                            article-decode-group-name article-decode-idna-rhs)
919   "*Hook run to decode charsets in articles."
920   :group 'gnus-article-headers
921   :type 'hook)
922
923 (defcustom gnus-display-mime-function 'gnus-display-mime
924   "Function to display MIME articles."
925   :group 'gnus-article-mime
926   :type 'function)
927
928 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
929   "Function used to decode headers.")
930
931 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
932   "Function used to decode addresses.")
933
934 (defvar gnus-article-dumbquotes-map
935   '(("\200" "EUR")
936     ("\202" ",")
937     ("\203" "f")
938     ("\204" ",,")
939     ("\205" "...")
940     ("\213" "<")
941     ("\214" "OE")
942     ("\221" "`")
943     ("\222" "'")
944     ("\223" "``")
945     ("\224" "\"")
946     ("\225" "*")
947     ("\226" "-")
948     ("\227" "--")
949     ("\230" "~")
950     ("\231" "(TM)")
951     ("\233" ">")
952     ("\234" "oe")
953     ("\264" "'"))
954   "Table for MS-to-Latin1 translation.")
955
956 (defcustom gnus-ignored-mime-types nil
957   "List of MIME types that should be ignored by Gnus."
958   :version "21.1"
959   :group 'gnus-article-mime
960   :type '(repeat regexp))
961
962 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
963   "List of MIME types that should not be given buttons when rendered inline.
964 See also `gnus-buttonized-mime-types' which may override this variable.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "21.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-buttonized-mime-types nil
971   "List of MIME types that should be given buttons when rendered inline.
972 If set, this variable overrides `gnus-unbuttonized-mime-types'.
973 To see e.g. security buttons you could set this to
974 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
975 this list to display radio buttons that allow you to choose one of two
976 media types those mails include.  See also `mm-discouraged-alternatives'.
977 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
978   :version "22.1"
979   :group 'gnus-article-mime
980   :type '(repeat regexp))
981
982 (defcustom gnus-inhibit-mime-unbuttonizing nil
983   "If non-nil, all MIME parts get buttons.
984 When nil (the default value), then some MIME parts do not get buttons,
985 as described by the variables `gnus-buttonized-mime-types' and
986 `gnus-unbuttonized-mime-types'."
987   :version "22.1"
988   :group 'gnus-article-mime
989   :type 'boolean)
990
991 (defcustom gnus-body-boundary-delimiter "_"
992   "String used to delimit header and body.
993 This variable is used by `gnus-article-treat-body-boundary' which can
994 be controlled by `gnus-treat-body-boundary'."
995   :version "22.1"
996   :group 'gnus-article-various
997   :type '(choice (item :tag "None" :value nil)
998                  string))
999
1000 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
1001                                   "/usr/share/picons")
1002   "Defines the location of the faces database.
1003 For information on obtaining this database of pretty pictures, please
1004 see http://www.cs.indiana.edu/picons/ftp/index.html"
1005   :version "22.1"
1006   :type '(repeat directory)
1007   :link '(url-link :tag "download"
1008                    "http://www.cs.indiana.edu/picons/ftp/index.html")
1009   :link '(custom-manual "(gnus)Picons")
1010   :group 'gnus-picon)
1011
1012 (defun gnus-picons-installed-p ()
1013   "Say whether picons are installed on your machine."
1014   (let ((installed nil))
1015     (dolist (database gnus-picon-databases)
1016       (when (file-exists-p database)
1017         (setq installed t)))
1018     installed))
1019
1020 (defcustom gnus-article-mime-part-function nil
1021   "Function called with a MIME handle as the argument.
1022 This is meant for people who want to do something automatic based
1023 on parts -- for instance, adding Vcard info to a database."
1024   :group 'gnus-article-mime
1025   :type '(choice (const nil)
1026                  function))
1027
1028 (defcustom gnus-mime-multipart-functions nil
1029   "An alist of MIME types to functions to display them."
1030   :version "21.1"
1031   :group 'gnus-article-mime
1032   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1033
1034 (defcustom gnus-article-date-lapsed-new-header nil
1035   "Whether the X-Sent and Date headers can coexist.
1036 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1037 either replace the old \"Date:\" header (if this variable is nil), or
1038 be added below it (otherwise)."
1039   :version "21.1"
1040   :group 'gnus-article-headers
1041   :type 'boolean)
1042
1043 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1044   "Function called with a MIME handle as the argument.
1045 This is meant for people who want to view first matched part.
1046 For `undisplayed-alternative' (default), the first undisplayed
1047 part or alternative part is used.  For `undisplayed', the first
1048 undisplayed part is used.  For a function, the first part which
1049 the function return t is used.  For nil, the first part is
1050 used."
1051   :version "21.1"
1052   :group 'gnus-article-mime
1053   :type '(choice
1054           (item :tag "first" :value nil)
1055           (item :tag "undisplayed" :value undisplayed)
1056           (item :tag "undisplayed or alternative"
1057                 :value undisplayed-alternative)
1058           (function)))
1059
1060 (defcustom gnus-mime-action-alist
1061   '(("save to file" . gnus-mime-save-part)
1062     ("save and strip" . gnus-mime-save-part-and-strip)
1063     ("replace with file" . gnus-mime-replace-part)
1064     ("delete part" . gnus-mime-delete-part)
1065     ("display as text" . gnus-mime-inline-part)
1066     ("view the part" . gnus-mime-view-part)
1067     ("pipe to command" . gnus-mime-pipe-part)
1068     ("toggle display" . gnus-article-press-button)
1069     ("toggle display" . gnus-article-view-part-as-charset)
1070     ("view as type" . gnus-mime-view-part-as-type)
1071     ("view internally" . gnus-mime-view-part-internally)
1072     ("view externally" . gnus-mime-view-part-externally))
1073   "An alist of actions that run on the MIME attachment."
1074   :group 'gnus-article-mime
1075   :type '(repeat (cons (string :tag "name")
1076                        (function))))
1077
1078 (defcustom gnus-auto-select-part 1
1079   "Advance to next MIME part when deleting or stripping parts.
1080
1081 When 0, point will be placed on the same part as before.  When
1082 positive (negative), move point forward (backwards) this many
1083 parts.  When nil, redisplay article."
1084   :version "23.1" ;; No Gnus
1085   :group 'gnus-article-mime
1086   :type '(choice (const nil :tag "Redisplay article.")
1087                  (const 1 :tag "Next part.")
1088                  (const 0 :tag "Current part.")
1089                  integer))
1090
1091 ;;;
1092 ;;; The treatment variables
1093 ;;;
1094
1095 (defvar gnus-part-display-hook nil
1096   "Hook called on parts that are to receive treatment.")
1097
1098 (defvar gnus-article-treat-custom
1099   '(choice (const :tag "Off" nil)
1100            (const :tag "On" t)
1101            (const :tag "Header" head)
1102            (const :tag "First" first)
1103            (const :tag "Last" last)
1104            (integer :tag "Less")
1105            (repeat :tag "Groups" regexp)
1106            (sexp :tag "Predicate")))
1107
1108 (defvar gnus-article-treat-head-custom
1109   '(choice (const :tag "Off" nil)
1110            (const :tag "Header" head)))
1111
1112 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1113                                    "text/x-patch")
1114   "Parts to treat.")
1115
1116 (defvar gnus-inhibit-treatment nil
1117   "Whether to inhibit treatment.")
1118
1119 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1120   "Highlight the signature.
1121 Valid values are nil, t, `head', `first', `last', an integer or a
1122 predicate.  See Info node `(gnus)Customizing Articles'."
1123   :group 'gnus-article-treat
1124   :link '(custom-manual "(gnus)Customizing Articles")
1125   :type gnus-article-treat-custom)
1126 (put 'gnus-treat-highlight-signature 'highlight t)
1127
1128 (defcustom gnus-treat-buttonize 100000
1129   "Add buttons.
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-custom)
1135 (put 'gnus-treat-buttonize 'highlight t)
1136
1137 (defcustom gnus-treat-buttonize-head 'head
1138   "Add buttons to the head.
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-head-custom)
1144 (put 'gnus-treat-buttonize-head 'highlight t)
1145
1146 (defcustom gnus-treat-emphasize 50000
1147   "Emphasize text.
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-custom)
1153 (put 'gnus-treat-emphasize 'highlight t)
1154
1155 (defcustom gnus-treat-strip-cr nil
1156   "Remove carriage returns.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :version "22.1"
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-custom)
1163
1164 (defcustom gnus-treat-unsplit-urls nil
1165   "Remove newlines from within URLs.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :version "22.1"
1169   :group 'gnus-article-treat
1170   :link '(custom-manual "(gnus)Customizing Articles")
1171   :type gnus-article-treat-custom)
1172
1173 (defcustom gnus-treat-leading-whitespace nil
1174   "Remove leading whitespace in headers.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :version "22.1"
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-hide-headers 'head
1183   "Hide headers.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-head-custom)
1189
1190 (defcustom gnus-treat-hide-boring-headers nil
1191   "Hide boring headers.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-head-custom)
1197
1198 (defcustom gnus-treat-hide-signature nil
1199   "Hide the signature.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-fill-article nil
1207   "Fill the article.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-custom)
1213
1214 (defcustom gnus-treat-hide-citation nil
1215   "Hide cited text.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-custom)
1221
1222 (defcustom gnus-treat-hide-citation-maybe nil
1223   "Hide cited text.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-strip-list-identifiers 'head
1231   "Strip list identifiers from `gnus-list-identifiers`.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :version "21.1"
1235   :group 'gnus-article-treat
1236   :link '(custom-manual "(gnus)Customizing Articles")
1237   :type gnus-article-treat-custom)
1238
1239 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1240                         "Gnus 5.10 (Emacs 22.1)")
1241
1242 (defcustom gnus-treat-strip-pem nil
1243   "Strip PEM signatures.
1244 Valid values are nil, t, `head', `first', `last', an integer or a
1245 predicate.  See Info node `(gnus)Customizing Articles'."
1246   :group 'gnus-article-treat
1247   :link '(custom-manual "(gnus)Customizing Articles")
1248   :type gnus-article-treat-custom)
1249
1250 (defcustom gnus-treat-strip-banner t
1251   "Strip banners from articles.
1252 The banner to be stripped is specified in the `banner' group parameter.
1253 Valid values are nil, t, `head', `first', `last', an integer or a
1254 predicate.  See Info node `(gnus)Customizing Articles'."
1255   :group 'gnus-article-treat
1256   :link '(custom-manual "(gnus)Customizing Articles")
1257   :type gnus-article-treat-custom)
1258
1259 (defcustom gnus-treat-highlight-headers 'head
1260   "Highlight the headers.
1261 Valid values are nil, t, `head', `first', `last', an integer or a
1262 predicate.  See Info node `(gnus)Customizing Articles'."
1263   :group 'gnus-article-treat
1264   :link '(custom-manual "(gnus)Customizing Articles")
1265   :type gnus-article-treat-head-custom)
1266 (put 'gnus-treat-highlight-headers 'highlight t)
1267
1268 (defcustom gnus-treat-highlight-citation t
1269   "Highlight cited text.
1270 Valid values are nil, t, `head', `first', `last', an integer or a
1271 predicate.  See Info node `(gnus)Customizing Articles'."
1272   :group 'gnus-article-treat
1273   :link '(custom-manual "(gnus)Customizing Articles")
1274   :type gnus-article-treat-custom)
1275 (put 'gnus-treat-highlight-citation 'highlight t)
1276
1277 (defcustom gnus-treat-date-ut nil
1278   "Display the Date in UT (GMT).
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :group 'gnus-article-treat
1282   :link '(custom-manual "(gnus)Customizing Articles")
1283   :type gnus-article-treat-head-custom)
1284
1285 (defcustom gnus-treat-date-local nil
1286   "Display the Date in the local timezone.
1287 Valid values are nil, t, `head', `first', `last', an integer or a
1288 predicate.  See Info node `(gnus)Customizing Articles'."
1289   :group 'gnus-article-treat
1290   :link '(custom-manual "(gnus)Customizing Articles")
1291   :type gnus-article-treat-head-custom)
1292
1293 (defcustom gnus-treat-date-english nil
1294   "Display the Date in a format that can be read aloud in English.
1295 Valid values are nil, t, `head', `first', `last', an integer or a
1296 predicate.  See Info node `(gnus)Customizing Articles'."
1297   :version "22.1"
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301
1302 (defcustom gnus-treat-date-lapsed nil
1303   "Display the Date header in a way that says how much time has elapsed.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :group 'gnus-article-treat
1307   :link '(custom-manual "(gnus)Customizing Articles")
1308   :type gnus-article-treat-head-custom)
1309
1310 (defcustom gnus-treat-date-original nil
1311   "Display the date in the original timezone.
1312 Valid values are nil, t, `head', `first', `last', an integer or a
1313 predicate.  See Info node `(gnus)Customizing Articles'."
1314   :group 'gnus-article-treat
1315   :link '(custom-manual "(gnus)Customizing Articles")
1316   :type gnus-article-treat-head-custom)
1317
1318 (defcustom gnus-treat-date-iso8601 nil
1319   "Display the date in the ISO8601 format.
1320 Valid values are nil, t, `head', `first', `last', an integer or a
1321 predicate.  See Info node `(gnus)Customizing Articles'."
1322   :version "21.1"
1323   :group 'gnus-article-treat
1324   :link '(custom-manual "(gnus)Customizing Articles")
1325   :type gnus-article-treat-head-custom)
1326
1327 (defcustom gnus-treat-date-user-defined nil
1328   "Display the date in a user-defined format.
1329 The format is defined by the `gnus-article-time-format' variable.
1330 Valid values are nil, t, `head', `first', `last', an integer or a
1331 predicate.  See Info node `(gnus)Customizing Articles'."
1332   :group 'gnus-article-treat
1333   :link '(custom-manual "(gnus)Customizing Articles")
1334   :type gnus-article-treat-head-custom)
1335
1336 (defcustom gnus-treat-strip-headers-in-body t
1337   "Strip the X-No-Archive header line from the beginning of the body.
1338 Valid values are nil, t, `head', `first', `last', an integer or a
1339 predicate.  See Info node `(gnus)Customizing Articles'."
1340   :version "21.1"
1341   :group 'gnus-article-treat
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :type gnus-article-treat-custom)
1344
1345 (defcustom gnus-treat-strip-trailing-blank-lines nil
1346   "Strip trailing blank lines.
1347 Valid values are nil, t, `head', `first', `last', an integer or a
1348 predicate.  See Info node `(gnus)Customizing Articles'.
1349
1350 When set to t, it also strips trailing blanks in all MIME parts.
1351 Consider to use `last' instead."
1352   :group 'gnus-article-treat
1353   :link '(custom-manual "(gnus)Customizing Articles")
1354   :type gnus-article-treat-custom)
1355
1356 (defcustom gnus-treat-strip-leading-blank-lines nil
1357   "Strip leading blank lines.
1358 Valid values are nil, t, `head', `first', `last', an integer or a
1359 predicate.  See Info node `(gnus)Customizing Articles'.
1360
1361 When set to t, it also strips trailing blanks in all MIME parts."
1362   :group 'gnus-article-treat
1363   :link '(custom-manual "(gnus)Customizing Articles")
1364   :type gnus-article-treat-custom)
1365
1366 (defcustom gnus-treat-strip-multiple-blank-lines nil
1367   "Strip multiple blank lines.
1368 Valid values are nil, t, `head', `first', `last', an integer or a
1369 predicate.  See Info node `(gnus)Customizing Articles'."
1370   :group 'gnus-article-treat
1371   :link '(custom-manual "(gnus)Customizing Articles")
1372   :type gnus-article-treat-custom)
1373
1374 (defcustom gnus-treat-unfold-headers 'head
1375   "Unfold folded header lines.
1376 Valid values are nil, t, `head', `first', `last', an integer or a
1377 predicate.  See Info node `(gnus)Customizing Articles'."
1378   :version "22.1"
1379   :group 'gnus-article-treat
1380   :link '(custom-manual "(gnus)Customizing Articles")
1381   :type gnus-article-treat-custom)
1382
1383 (defcustom gnus-article-unfold-long-headers nil
1384   "If non-nil, allow unfolding headers even if the header is long.
1385 If it is a regexp, only long headers matching this regexp are unfolded.
1386 If it is t, all long headers are unfolded.
1387
1388 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1389   :version "23.1" ;; No Gnus
1390   :group 'gnus-article-treat
1391   :type '(choice (const nil)
1392                  (const :tag "all" t)
1393                  (regexp)))
1394
1395 (defcustom gnus-treat-fold-headers nil
1396   "Fold headers.
1397 Valid values are nil, t, `head', `first', `last', an integer or a
1398 predicate.  See Info node `(gnus)Customizing Articles'."
1399   :version "22.1"
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403
1404 (defcustom gnus-treat-fold-newsgroups 'head
1405   "Fold the Newsgroups and Followup-To headers.
1406 Valid values are nil, t, `head', `first', `last', an integer or a
1407 predicate.  See Info node `(gnus)Customizing Articles'."
1408   :version "22.1"
1409   :group 'gnus-article-treat
1410   :link '(custom-manual "(gnus)Customizing Articles")
1411   :type gnus-article-treat-custom)
1412
1413 (defcustom gnus-treat-overstrike t
1414   "Treat overstrike highlighting.
1415 Valid values are nil, t, `head', `first', `last', an integer or a
1416 predicate.  See Info node `(gnus)Customizing Articles'."
1417   :group 'gnus-article-treat
1418   :link '(custom-manual "(gnus)Customizing Articles")
1419   :type gnus-article-treat-custom)
1420 (put 'gnus-treat-overstrike 'highlight t)
1421
1422 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1423   "Treat ANSI SGR control sequences.
1424 Valid values are nil, t, `head', `first', `last', an integer or a
1425 predicate.  See Info node `(gnus)Customizing Articles'."
1426   :group 'gnus-article-treat
1427   :link '(custom-manual "(gnus)Customizing Articles")
1428   :type gnus-article-treat-custom)
1429
1430 (make-obsolete-variable 'gnus-treat-display-xface
1431                         'gnus-treat-display-x-face "22.1")
1432
1433 (defcustom gnus-treat-display-x-face
1434   (and (not noninteractive)
1435        (gnus-image-type-available-p 'xbm)
1436        (if (featurep 'xemacs)
1437            (featurep 'xface)
1438          (condition-case nil
1439              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1440                   (executable-find "icontopbm"))
1441            ;; shell-command-to-string may signal an error, e.g. if
1442            ;; shell-file-name is not found.
1443            (error nil)))
1444        'head)
1445   "Display X-Face headers.
1446 Valid values are nil and `head'.
1447 See Info node `(gnus)Customizing Articles' and Info node
1448 `(gnus)X-Face' for details."
1449   :group 'gnus-article-treat
1450   :version "21.1"
1451   :link '(custom-manual "(gnus)Customizing Articles")
1452   :link '(custom-manual "(gnus)X-Face")
1453   :type gnus-article-treat-head-custom
1454   :set (lambda (symbol value)
1455          (set-default
1456           symbol
1457           (cond ((or (boundp symbol) (get symbol 'saved-value))
1458                  value)
1459                 ((boundp 'gnus-treat-display-xface)
1460                  (message "\
1461 ** gnus-treat-display-xface is an obsolete variable;\
1462  use gnus-treat-display-x-face instead")
1463                  (default-value 'gnus-treat-display-xface))
1464                 ((get 'gnus-treat-display-xface 'saved-value)
1465                  (message "\
1466 ** gnus-treat-display-xface is an obsolete variable;\
1467  use gnus-treat-display-x-face instead")
1468                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1469                 (t
1470                  value)))))
1471 (put 'gnus-treat-display-x-face 'highlight t)
1472
1473 (defcustom gnus-treat-display-face
1474   (and (not noninteractive)
1475        (gnus-image-type-available-p 'png)
1476        'head)
1477   "Display Face headers.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles' and Info
1480 node `(gnus)Face' for details."
1481   :group 'gnus-article-treat
1482   :version "22.1"
1483   :link '(custom-manual "(gnus)Customizing Articles")
1484   :link '(custom-manual "(gnus)X-Face")
1485   :type gnus-article-treat-head-custom)
1486 (put 'gnus-treat-display-face 'highlight t)
1487
1488 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1489   "Display smileys.
1490 Valid values are nil, t, `head', `first', `last', an integer or a
1491 predicate.  See Info node `(gnus)Customizing Articles' and Info
1492 node `(gnus)Smileys' for details."
1493   :group 'gnus-article-treat
1494   :version "21.1"
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Smileys")
1497   :type gnus-article-treat-custom)
1498 (put 'gnus-treat-display-smileys 'highlight t)
1499
1500 (defcustom gnus-treat-from-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in the From header.
1505 Valid values are nil, t, `head', `first', `last', an integer or a
1506 predicate.  See Info node `(gnus)Customizing Articles' and Info
1507 node `(gnus)Picons' for details."
1508   :version "22.1"
1509   :group 'gnus-article-treat
1510   :group 'gnus-picon
1511   :link '(custom-manual "(gnus)Customizing Articles")
1512   :link '(custom-manual "(gnus)Picons")
1513   :type gnus-article-treat-head-custom)
1514 (put 'gnus-treat-from-picon 'highlight t)
1515
1516 (defcustom gnus-treat-mail-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in To and Cc headers.
1521 Valid values are nil, t, `head', `first', `last', an integer or a
1522 predicate.  See Info node `(gnus)Customizing Articles' and Info
1523 node `(gnus)Picons' for details."
1524   :version "22.1"
1525   :group 'gnus-article-treat
1526   :group 'gnus-picon
1527   :link '(custom-manual "(gnus)Customizing Articles")
1528   :link '(custom-manual "(gnus)Picons")
1529   :type gnus-article-treat-head-custom)
1530 (put 'gnus-treat-mail-picon 'highlight t)
1531
1532 (defcustom gnus-treat-newsgroups-picon
1533   (if (and (gnus-image-type-available-p 'xpm)
1534            (gnus-picons-installed-p))
1535       'head nil)
1536   "Display picons in the Newsgroups and Followup-To headers.
1537 Valid values are nil, t, `head', `first', `last', an integer or a
1538 predicate.  See Info node `(gnus)Customizing Articles' and Info
1539 node `(gnus)Picons' for details."
1540   :version "22.1"
1541   :group 'gnus-article-treat
1542   :group 'gnus-picon
1543   :link '(custom-manual "(gnus)Customizing Articles")
1544   :link '(custom-manual "(gnus)Picons")
1545   :type gnus-article-treat-head-custom)
1546 (put 'gnus-treat-newsgroups-picon 'highlight t)
1547
1548 (defcustom gnus-treat-from-gravatar nil
1549   "Display gravatars in the From header.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles' and Info
1552 node `(gnus)Gravatars' for details."
1553   :version "24.1"
1554   :group 'gnus-article-treat
1555   :group 'gnus-gravatar
1556   :link '(custom-manual "(gnus)Customizing Articles")
1557   :link '(custom-manual "(gnus)Gravatars")
1558   :type gnus-article-treat-head-custom)
1559 (put 'gnus-treat-from-gravatar 'highlight t)
1560
1561 (defcustom gnus-treat-mail-gravatar nil
1562   "Display gravatars in To and Cc headers.
1563 Valid values are nil, t, `head', `first', `last', an integer or a
1564 predicate.  See Info node `(gnus)Customizing Articles' and Info
1565 node `(gnus)Gravatars' for details."
1566   :version "24.1"
1567   :group 'gnus-article-treat
1568   :group 'gnus-gravatar
1569   :link '(custom-manual "(gnus)Customizing Articles")
1570   :link '(custom-manual "(gnus)Gravatars")
1571   :type gnus-article-treat-head-custom)
1572 (put 'gnus-treat-mail-gravatar 'highlight t)
1573
1574 (defcustom gnus-treat-body-boundary
1575   (if (or gnus-treat-newsgroups-picon
1576           gnus-treat-mail-picon
1577           gnus-treat-from-picon
1578           gnus-treat-from-gravatar
1579           gnus-treat-mail-gravatar)
1580       ;; If there's much decoration, the user might prefer a boundery.
1581       'head
1582     nil)
1583   "Draw a boundary at the end of the headers.
1584 Valid values are nil and `head'.
1585 See Info node `(gnus)Customizing Articles' for details."
1586   :version "22.1"
1587   :group 'gnus-article-treat
1588   :link '(custom-manual "(gnus)Customizing Articles")
1589   :type gnus-article-treat-head-custom)
1590
1591 (defcustom gnus-treat-capitalize-sentences nil
1592   "Capitalize sentence-starting words.
1593 Valid values are nil, t, `head', `first', `last', an integer or a
1594 predicate.  See Info node `(gnus)Customizing Articles'."
1595   :version "21.1"
1596   :group 'gnus-article-treat
1597   :link '(custom-manual "(gnus)Customizing Articles")
1598   :type gnus-article-treat-custom)
1599
1600 (defcustom gnus-treat-wash-html nil
1601   "Format as HTML.
1602 Valid values are nil, t, `head', `first', `last', an integer or a
1603 predicate.  See Info node `(gnus)Customizing Articles'."
1604   :version "22.1"
1605   :group 'gnus-article-treat
1606   :link '(custom-manual "(gnus)Customizing Articles")
1607   :type gnus-article-treat-custom)
1608
1609 (defcustom gnus-treat-fill-long-lines nil
1610   "Fill long lines.
1611 Valid values are nil, t, `head', `first', `last', an integer or a
1612 predicate.  See Info node `(gnus)Customizing Articles'."
1613   :group 'gnus-article-treat
1614   :link '(custom-manual "(gnus)Customizing Articles")
1615   :type gnus-article-treat-custom)
1616
1617 (defcustom gnus-treat-x-pgp-sig nil
1618   "Verify X-PGP-Sig.
1619 To automatically treat X-PGP-Sig, set it to head.
1620 Valid values are nil, t, `head', `first', `last', an integer or a
1621 predicate.  See Info node `(gnus)Customizing Articles'."
1622   :version "22.1"
1623   :group 'gnus-article-treat
1624   :group 'mime-security
1625   :link '(custom-manual "(gnus)Customizing Articles")
1626   :type gnus-article-treat-custom)
1627
1628 (defvar gnus-article-encrypt-protocol-alist
1629   '(("PGP" . mml2015-self-encrypt)))
1630
1631 ;; Set to nil if more than one protocol added to
1632 ;; gnus-article-encrypt-protocol-alist.
1633 (defcustom gnus-article-encrypt-protocol "PGP"
1634   "The protocol used for encrypt articles.
1635 It is a string, such as \"PGP\". If nil, ask user."
1636   :version "22.1"
1637   :type 'string
1638   :group 'mime-security)
1639
1640 (defvar gnus-article-wash-function nil
1641   "Function used for converting HTML into text.")
1642
1643 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1644                               (mm-coding-system-p 'utf-8)
1645                               (executable-find idna-program))
1646   "Whether IDNA decoding of headers is used when viewing messages.
1647 This requires GNU Libidn, and by default only enabled if it is found."
1648   :version "22.1"
1649   :group 'gnus-article-headers
1650   :type 'boolean)
1651
1652 (defcustom gnus-article-over-scroll nil
1653   "If non-nil, allow scrolling the article buffer even when there no more text."
1654   :version "22.1"
1655   :group 'gnus-article
1656   :type 'boolean)
1657
1658 ;;; Internal variables
1659
1660 (defvar gnus-english-month-names
1661   '("January" "February" "March" "April" "May" "June" "July" "August"
1662     "September" "October" "November" "December"))
1663
1664 (defvar article-goto-body-goes-to-point-min-p nil)
1665 (defvar gnus-article-wash-types nil)
1666 (defvar gnus-article-emphasis-alist nil)
1667 (defvar gnus-article-image-alist nil)
1668
1669 (defvar gnus-article-mime-handle-alist-1 nil)
1670 (defvar gnus-treatment-function-alist
1671   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1672     (gnus-treat-strip-banner gnus-article-strip-banner)
1673     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1674     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1675     (gnus-treat-buttonize gnus-article-add-buttons)
1676     (gnus-treat-fill-article gnus-article-fill-cited-article)
1677     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1678     (gnus-treat-strip-cr gnus-article-remove-cr)
1679     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1680     (gnus-treat-date-ut gnus-article-date-ut)
1681     (gnus-treat-date-local gnus-article-date-local)
1682     (gnus-treat-date-english gnus-article-date-english)
1683     (gnus-treat-date-original gnus-article-date-original)
1684     (gnus-treat-date-user-defined gnus-article-date-user)
1685     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1686     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1687     (gnus-treat-display-x-face gnus-article-display-x-face)
1688     (gnus-treat-display-face gnus-article-display-face)
1689     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1690     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1691     (gnus-treat-hide-signature gnus-article-hide-signature)
1692     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1693     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1694     (gnus-treat-from-picon gnus-treat-from-picon)
1695     (gnus-treat-mail-picon gnus-treat-mail-picon)
1696     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1697     (gnus-treat-strip-pem gnus-article-hide-pem)
1698     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1699     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1700     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1701     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1702     (gnus-treat-strip-trailing-blank-lines
1703      gnus-article-remove-trailing-blank-lines)
1704     (gnus-treat-strip-leading-blank-lines
1705      gnus-article-strip-leading-blank-lines)
1706     (gnus-treat-strip-multiple-blank-lines
1707      gnus-article-strip-multiple-blank-lines)
1708     (gnus-treat-overstrike gnus-article-treat-overstrike)
1709     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1710     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1711     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1712     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1713     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1714     (gnus-treat-display-smileys gnus-treat-smiley)
1715     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1716     (gnus-treat-wash-html gnus-article-wash-html)
1717     (gnus-treat-emphasize gnus-article-emphasize)
1718     (gnus-treat-hide-citation gnus-article-hide-citation)
1719     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1720     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1721     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1722
1723 (defvar gnus-article-mime-handle-alist nil)
1724 (defvar article-lapsed-timer nil)
1725 (defvar gnus-article-current-summary nil)
1726
1727 (defvar gnus-article-mode-syntax-table
1728   (let ((table (copy-syntax-table text-mode-syntax-table)))
1729     ;; This causes the citation match run O(2^n).
1730     ;; (modify-syntax-entry ?- "w" table)
1731     (modify-syntax-entry ?> ")<" table)
1732     (modify-syntax-entry ?< "(>" table)
1733     ;; make M-. in article buffers work for `foo' strings
1734     (modify-syntax-entry ?' " " table)
1735     (modify-syntax-entry ?` " " table)
1736     table)
1737   "Syntax table used in article mode buffers.
1738 Initialized from `text-mode-syntax-table.")
1739
1740 (defvar gnus-save-article-buffer nil)
1741
1742 (defvar gnus-number-of-articles-to-be-saved nil)
1743
1744 (defvar gnus-inhibit-hiding nil)
1745
1746 (defvar gnus-article-edit-mode nil)
1747
1748 ;;; Macros for dealing with the article buffer.
1749
1750 (defmacro gnus-with-article-headers (&rest forms)
1751   `(with-current-buffer gnus-article-buffer
1752      (save-restriction
1753        (let ((inhibit-read-only t)
1754              (inhibit-point-motion-hooks t)
1755              (case-fold-search t))
1756          (article-narrow-to-head)
1757          ,@forms))))
1758
1759 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1760 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1761
1762 (defmacro gnus-with-article-buffer (&rest forms)
1763   `(with-current-buffer gnus-article-buffer
1764      (let ((inhibit-read-only t))
1765        ,@forms)))
1766
1767 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1768 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1769
1770 (defun gnus-article-goto-header (header)
1771   "Go to HEADER, which is a regular expression."
1772   (re-search-forward (concat "^\\(" header "\\):") nil t))
1773
1774 (defsubst gnus-article-hide-text (b e props)
1775   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1776   (gnus-add-text-properties-when 'article-type nil b e props)
1777   (when (memq 'intangible props)
1778     (put-text-property
1779      (max (1- b) (point-min))
1780      b 'intangible (cddr (memq 'intangible props)))))
1781
1782 (defsubst gnus-article-unhide-text (b e)
1783   "Remove hidden text properties from region between B and E."
1784   (remove-text-properties b e gnus-hidden-properties)
1785   (when (memq 'intangible gnus-hidden-properties)
1786     (put-text-property (max (1- b) (point-min))
1787                        b 'intangible nil)))
1788
1789 (defun gnus-article-hide-text-type (b e type)
1790   "Hide text of TYPE between B and E."
1791   (gnus-add-wash-type type)
1792   (gnus-article-hide-text
1793    b e (cons 'article-type (cons type gnus-hidden-properties))))
1794
1795 (defun gnus-article-unhide-text-type (b e type)
1796   "Unhide text of TYPE between B and E."
1797   (gnus-delete-wash-type type)
1798   (remove-text-properties
1799    b e (cons 'article-type (cons type gnus-hidden-properties)))
1800   (when (memq 'intangible gnus-hidden-properties)
1801     (put-text-property (max (1- b) (point-min))
1802                        b 'intangible nil)))
1803
1804 (defun gnus-article-hide-text-of-type (type)
1805   "Hide text of TYPE in the current buffer."
1806   (save-excursion
1807     (let ((b (point-min))
1808           (e (point-max)))
1809       (while (setq b (text-property-any b e 'article-type type))
1810         (add-text-properties b (incf b) gnus-hidden-properties)))))
1811
1812 (defun gnus-article-delete-text-of-type (type)
1813   "Delete text of TYPE in the current buffer."
1814   (save-excursion
1815     (let ((b (point-min)))
1816       (if (eq type 'multipart)
1817           ;; Remove MIME buttons associated with multipart/alternative parts.
1818           (progn
1819             (goto-char b)
1820             (while (if (get-text-property (point) 'gnus-part)
1821                        (setq b (point))
1822                      (when (setq b (next-single-property-change (point)
1823                                                                 'gnus-part))
1824                        (goto-char b)
1825                        t))
1826               (end-of-line)
1827               (skip-chars-forward "\n")
1828               (when (eq (get-text-property b 'article-type) 'multipart)
1829                 (delete-region b (point)))))
1830         (while (setq b (text-property-any b (point-max) 'article-type type))
1831           (delete-region
1832            b (or (text-property-not-all b (point-max) 'article-type type)
1833                  (point-max))))))))
1834
1835 (defun gnus-article-delete-invisible-text ()
1836   "Delete all invisible text in the current buffer."
1837   (save-excursion
1838     (let ((b (point-min)))
1839       (while (setq b (text-property-any b (point-max) 'invisible t))
1840         (delete-region
1841          b (or (text-property-not-all b (point-max) 'invisible t)
1842                (point-max)))))))
1843
1844 (defun gnus-article-text-type-exists-p (type)
1845   "Say whether any text of type TYPE exists in the buffer."
1846   (text-property-any (point-min) (point-max) 'article-type type))
1847
1848 (defsubst gnus-article-header-rank ()
1849   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1850   (let ((list gnus-sorted-header-list)
1851         (i 1))
1852     (while list
1853       (if (looking-at (car list))
1854           (setq list nil)
1855         (setq list (cdr list))
1856         (incf i)))
1857       i))
1858
1859 (defun article-hide-headers (&optional arg delete)
1860   "Hide unwanted headers and possibly sort them as well."
1861   (interactive)
1862   ;; This function might be inhibited.
1863   (unless gnus-inhibit-hiding
1864     (let ((inhibit-read-only t)
1865           (case-fold-search t)
1866           (max (1+ (length gnus-sorted-header-list)))
1867           (inhibit-point-motion-hooks t)
1868           (cur (current-buffer))
1869           ignored visible beg)
1870       (save-excursion
1871         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1872         ;; group parameters, so we should go to the summary buffer.
1873         (when (prog1
1874                   (condition-case nil
1875                       (progn (set-buffer gnus-summary-buffer) t)
1876                     (error nil))
1877                 (setq ignored (when (not gnus-visible-headers)
1878                                 (cond ((stringp gnus-ignored-headers)
1879                                        gnus-ignored-headers)
1880                                       ((listp gnus-ignored-headers)
1881                                        (mapconcat 'identity
1882                                                   gnus-ignored-headers
1883                                                   "\\|"))))
1884                       visible (cond ((stringp gnus-visible-headers)
1885                                      gnus-visible-headers)
1886                                     ((and gnus-visible-headers
1887                                           (listp gnus-visible-headers))
1888                                      (mapconcat 'identity
1889                                                 gnus-visible-headers
1890                                                 "\\|")))))
1891           (set-buffer cur))
1892         (save-restriction
1893           ;; First we narrow to just the headers.
1894           (article-narrow-to-head)
1895           ;; Hide any "From " lines at the beginning of (mail) articles.
1896           (while (looking-at "From ")
1897             (forward-line 1))
1898           (unless (bobp)
1899             (delete-region (point-min) (point)))
1900           ;; Then treat the rest of the header lines.
1901           ;; Then we use the two regular expressions
1902           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1903           ;; select which header lines is to remain visible in the
1904           ;; article buffer.
1905           (while (re-search-forward "^[^ \t:]*:" nil t)
1906             (beginning-of-line)
1907             ;; Mark the rank of the header.
1908             (put-text-property
1909              (point) (1+ (point)) 'message-rank
1910              (if (or (and visible (looking-at visible))
1911                      (and ignored
1912                           (not (looking-at ignored))))
1913                  (gnus-article-header-rank)
1914                (+ 2 max)))
1915             (forward-line 1))
1916           (message-sort-headers-1)
1917           (when (setq beg (text-property-any
1918                            (point-min) (point-max) 'message-rank (+ 2 max)))
1919             ;; We delete the unwanted headers.
1920             (gnus-add-wash-type 'headers)
1921             (add-text-properties (point-min) (+ 5 (point-min))
1922                                  '(article-type headers dummy-invisible t))
1923             (delete-region beg (point-max))))))))
1924
1925 (defun article-hide-boring-headers (&optional arg)
1926   "Toggle hiding of headers that aren't very interesting.
1927 If given a negative prefix, always show; if given a positive prefix,
1928 always hide."
1929   (interactive (gnus-article-hidden-arg))
1930   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1931              (not gnus-show-all-headers))
1932     (save-excursion
1933       (save-restriction
1934         (let ((inhibit-read-only t)
1935               (inhibit-point-motion-hooks t))
1936           (article-narrow-to-head)
1937           (dolist (elem gnus-boring-article-headers)
1938             (goto-char (point-min))
1939             (cond
1940              ;; Hide empty headers.
1941              ((eq elem 'empty)
1942               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1943                 (forward-line -1)
1944                 (gnus-article-hide-text-type
1945                  (point-at-bol)
1946                  (progn
1947                    (end-of-line)
1948                    (if (re-search-forward "^[^ \t]" nil t)
1949                        (match-beginning 0)
1950                      (point-max)))
1951                  'boring-headers)))
1952              ;; Hide boring Newsgroups header.
1953              ((eq elem 'newsgroups)
1954               (when (gnus-string-equal
1955                      (gnus-fetch-field "newsgroups")
1956                      (gnus-group-real-name
1957                       (if (boundp 'gnus-newsgroup-name)
1958                           gnus-newsgroup-name
1959                         "")))
1960                 (gnus-article-hide-header "newsgroups")))
1961              ((eq elem 'to-address)
1962               (let ((to (message-fetch-field "to"))
1963                     (to-address
1964                      (gnus-parameter-to-address
1965                       (if (boundp 'gnus-newsgroup-name)
1966                           gnus-newsgroup-name ""))))
1967                 (when (and to to-address
1968                            (ignore-errors
1969                              (gnus-string-equal
1970                               ;; only one address in To
1971                               (nth 1 (mail-extract-address-components to))
1972                               to-address)))
1973                   (gnus-article-hide-header "to"))))
1974              ((eq elem 'to-list)
1975               (let ((to (message-fetch-field "to"))
1976                     (to-list
1977                      (gnus-parameter-to-list
1978                       (if (boundp 'gnus-newsgroup-name)
1979                           gnus-newsgroup-name ""))))
1980                 (when (and to to-list
1981                            (ignore-errors
1982                              (gnus-string-equal
1983                               ;; only one address in To
1984                               (nth 1 (mail-extract-address-components to))
1985                               to-list)))
1986                   (gnus-article-hide-header "to"))))
1987              ((eq elem 'cc-list)
1988               (let ((cc (message-fetch-field "cc"))
1989                     (to-list
1990                      (gnus-parameter-to-list
1991                       (if (boundp 'gnus-newsgroup-name)
1992                           gnus-newsgroup-name ""))))
1993                 (when (and cc to-list
1994                            (ignore-errors
1995                              (gnus-string-equal
1996                               ;; only one address in CC
1997                               (nth 1 (mail-extract-address-components cc))
1998                               to-list)))
1999                   (gnus-article-hide-header "cc"))))
2000              ((eq elem 'followup-to)
2001               (when (gnus-string-equal
2002                      (message-fetch-field "followup-to")
2003                      (message-fetch-field "newsgroups"))
2004                 (gnus-article-hide-header "followup-to")))
2005              ((eq elem 'reply-to)
2006               (if (gnus-group-find-parameter
2007                    gnus-newsgroup-name 'broken-reply-to)
2008                   (gnus-article-hide-header "reply-to")
2009                 (let ((from (message-fetch-field "from"))
2010                       (reply-to (message-fetch-field "reply-to")))
2011                   (when
2012                       (and
2013                        from reply-to
2014                        (ignore-errors
2015                          (equal
2016                           (sort (mapcar
2017                                  (lambda (x) (downcase (cadr x)))
2018                                  (mail-extract-address-components from t))
2019                                 'string<)
2020                           (sort (mapcar
2021                                  (lambda (x) (downcase (cadr x)))
2022                                  (mail-extract-address-components reply-to t))
2023                                 'string<))))
2024                     (gnus-article-hide-header "reply-to")))))
2025              ((eq elem 'date)
2026               (let ((date (with-current-buffer gnus-original-article-buffer
2027                             ;; If date in `gnus-article-buffer' is localized
2028                             ;; (`gnus-treat-date-user-defined'),
2029                             ;; `days-between' might fail.
2030                             (message-fetch-field "date"))))
2031                 (when (and date
2032                            (< (days-between (current-time-string) date)
2033                               4))
2034                   (gnus-article-hide-header "date"))))
2035              ((eq elem 'long-to)
2036               (let ((to (message-fetch-field "to"))
2037                     (cc (message-fetch-field "cc")))
2038                 (when (> (length to) 1024)
2039                   (gnus-article-hide-header "to"))
2040                 (when (> (length cc) 1024)
2041                   (gnus-article-hide-header "cc"))))
2042              ((eq elem 'many-to)
2043               (let ((to-count 0)
2044                     (cc-count 0))
2045                 (goto-char (point-min))
2046                 (while (re-search-forward "^to:" nil t)
2047                   (setq to-count (1+ to-count)))
2048                 (when (> to-count 1)
2049                   (while (> to-count 0)
2050                     (goto-char (point-min))
2051                     (save-restriction
2052                       (re-search-forward "^to:" nil nil to-count)
2053                       (forward-line -1)
2054                       (narrow-to-region (point) (point-max))
2055                       (gnus-article-hide-header "to"))
2056                     (setq to-count (1- to-count))))
2057                 (goto-char (point-min))
2058                 (while (re-search-forward "^cc:" nil t)
2059                   (setq cc-count (1+ cc-count)))
2060                 (when (> cc-count 1)
2061                   (while (> cc-count 0)
2062                     (goto-char (point-min))
2063                     (save-restriction
2064                       (re-search-forward "^cc:" nil nil cc-count)
2065                       (forward-line -1)
2066                       (narrow-to-region (point) (point-max))
2067                       (gnus-article-hide-header "cc"))
2068                     (setq cc-count (1- cc-count)))))))))))))
2069
2070 (defun gnus-article-hide-header (header)
2071   (save-excursion
2072     (goto-char (point-min))
2073     (when (re-search-forward (concat "^" header ":") nil t)
2074       (gnus-article-hide-text-type
2075        (point-at-bol)
2076        (progn
2077          (end-of-line)
2078          (if (re-search-forward "^[^ \t]" nil t)
2079              (match-beginning 0)
2080            (point-max)))
2081        'boring-headers))))
2082
2083 (defvar gnus-article-normalized-header-length 40
2084   "Length of normalized headers.")
2085
2086 (defun article-normalize-headers ()
2087   "Make all header lines 40 characters long."
2088   (interactive)
2089   (let ((inhibit-read-only t)
2090         column)
2091     (save-excursion
2092       (save-restriction
2093         (article-narrow-to-head)
2094         (while (not (eobp))
2095           (cond
2096            ((< (setq column (- (point-at-eol) (point)))
2097                gnus-article-normalized-header-length)
2098             (end-of-line)
2099             (insert (make-string
2100                      (- gnus-article-normalized-header-length column)
2101                      ? )))
2102            ((> column gnus-article-normalized-header-length)
2103             (gnus-put-text-property
2104              (progn
2105                (forward-char gnus-article-normalized-header-length)
2106                (point))
2107              (point-at-eol)
2108              'invisible t))
2109            (t
2110             ;; Do nothing.
2111             ))
2112           (forward-line 1))))))
2113
2114 (defun article-treat-dumbquotes ()
2115   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2116 Note that this function guesses whether a character is a sm*rtq**t* or
2117 not, so it should only be used interactively.
2118
2119 Sm*rtq**t*s are M****s***'s unilateral extension to the
2120 iso-8859-1 character map in an attempt to provide more quoting
2121 characters.  If you see something like \\222 or \\264 where
2122 you're expecting some kind of apostrophe or quotation mark, then
2123 try this wash."
2124   (interactive)
2125   (article-translate-strings gnus-article-dumbquotes-map))
2126
2127 (defun article-translate-characters (from to)
2128   "Translate all characters in the body of the article according to FROM and TO.
2129 FROM is a string of characters to translate from; to is a string of
2130 characters to translate to."
2131   (save-excursion
2132     (when (article-goto-body)
2133       (let ((inhibit-read-only t)
2134             (x (make-string 225 ?x))
2135             (i -1))
2136         (while (< (incf i) (length x))
2137           (aset x i i))
2138         (setq i 0)
2139         (while (< i (length from))
2140           (aset x (aref from i) (aref to i))
2141           (incf i))
2142         (translate-region (point) (point-max) x)))))
2143
2144 (defun article-translate-strings (map)
2145   "Translate all string in the body of the article according to MAP.
2146 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2147   (save-excursion
2148     (when (article-goto-body)
2149       (let ((inhibit-read-only t))
2150         (dolist (elem map)
2151           (save-excursion
2152             (while (search-forward (car elem) nil t)
2153               (replace-match (cadr elem)))))))))
2154
2155 (defun article-treat-overstrike ()
2156   "Translate overstrikes into bold text."
2157   (interactive)
2158   (save-excursion
2159     (when (article-goto-body)
2160       (let ((inhibit-read-only t))
2161         (while (search-forward "\b" nil t)
2162           (let ((next (char-after))
2163                 (previous (char-after (- (point) 2))))
2164             ;; We do the boldification/underlining by hiding the
2165             ;; overstrikes and putting the proper text property
2166             ;; on the letters.
2167             (cond
2168              ((eq next previous)
2169               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2170               (put-text-property (point) (1+ (point)) 'face 'bold))
2171              ((eq next ?_)
2172               (gnus-article-hide-text-type
2173                (1- (point)) (1+ (point)) 'overstrike)
2174               (put-text-property
2175                (- (point) 2) (1- (point)) 'face 'underline))
2176              ((eq previous ?_)
2177               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2178               (put-text-property
2179                (point) (1+ (point)) 'face 'underline)))))))))
2180
2181 (defun article-treat-ansi-sequences ()
2182   "Translate ANSI SGR control sequences into overlays or extents."
2183   (interactive)
2184   (save-excursion
2185     (when (article-goto-body)
2186       (let ((inhibit-read-only t))
2187         (ansi-color-apply-on-region (point) (point-max))))))
2188
2189 (defun gnus-article-treat-unfold-headers ()
2190   "Unfold folded message headers.
2191 Only the headers that fit into the current window width will be
2192 unfolded."
2193   (interactive)
2194   (gnus-with-article-headers
2195     (let (length)
2196       (while (not (eobp))
2197         (save-restriction
2198           (mail-header-narrow-to-field)
2199           (let* ((header (buffer-string))
2200                  (unfoldable
2201                   (or (equal gnus-article-unfold-long-headers t)
2202                       (and (stringp gnus-article-unfold-long-headers)
2203                            (string-match gnus-article-unfold-long-headers header)))))
2204             (with-temp-buffer
2205               (insert header)
2206               (goto-char (point-min))
2207               (while (re-search-forward "\n[\t ]" nil t)
2208                 (replace-match " " t t)))
2209             (setq length (- (point-max) (point-min) 1))
2210             (when (or unfoldable
2211                       (< length (window-width)))
2212               (while (re-search-forward "\n[\t ]" nil t)
2213                 (replace-match " " t t))))
2214           (goto-char (point-max)))))))
2215
2216 (defun gnus-article-treat-fold-headers ()
2217   "Fold message headers."
2218   (interactive)
2219   (gnus-with-article-headers
2220     (while (not (eobp))
2221       (save-restriction
2222         (mail-header-narrow-to-field)
2223         (mail-header-fold-field)
2224         (goto-char (point-max))))))
2225
2226 (defun gnus-treat-smiley ()
2227   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2228   (interactive)
2229   (gnus-with-article-buffer
2230     (if (memq 'smiley gnus-article-wash-types)
2231         (gnus-delete-images 'smiley)
2232       (article-goto-body)
2233       (let ((images (smiley-region (point) (point-max))))
2234         (when images
2235           (gnus-add-wash-type 'smiley)
2236           (dolist (image images)
2237             (gnus-add-image 'smiley image)))))))
2238
2239 (defun gnus-article-remove-images ()
2240   "Remove all images from the article buffer."
2241   (interactive)
2242   (gnus-with-article-buffer
2243     (dolist (elem gnus-article-image-alist)
2244       (gnus-delete-images (car elem)))))
2245
2246 (defun gnus-article-treat-fold-newsgroups ()
2247   "Unfold folded message headers.
2248 Only the headers that fit into the current window width will be
2249 unfolded."
2250   (interactive)
2251   (gnus-with-article-headers
2252     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2253       (save-restriction
2254         (mail-header-narrow-to-field)
2255         (while (re-search-forward ", *" nil t)
2256           (replace-match ", " t t))
2257         (mail-header-fold-field)
2258         (goto-char (point-max))))))
2259
2260 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2261   "Value of `truncate-lines' in Gnus Article buffer.
2262 Valid values are nil, t, `head', `first', `last', an integer or a
2263 predicate.  See Info node `(gnus)Customizing Articles'."
2264   :version "23.1" ;; No Gnus
2265   :group 'gnus-article
2266   ;; :link '(custom-manual "(gnus)Customizing Articles")
2267   :type 'boolean)
2268
2269 (defun gnus-article-toggle-truncate-lines (&optional arg)
2270   "Toggle whether to fold or truncate long lines in article the buffer.
2271 If ARG is non-nil and not a number, toggle
2272 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2273 long lines if and only if arg is positive."
2274   (interactive "P")
2275   (cond
2276    ((and (numberp arg) (> arg 0))
2277     (setq gnus-article-truncate-lines t))
2278    ((numberp arg)
2279     (setq gnus-article-truncate-lines nil))
2280    (arg
2281     (setq gnus-article-truncate-lines
2282           (not gnus-article-truncate-lines))))
2283   (gnus-with-article-buffer
2284     (cond
2285      ((and (numberp arg) (> arg 0))
2286       (setq truncate-lines nil))
2287      ((numberp arg)
2288       (setq truncate-lines t)))
2289     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2290     ;; `toggle-truncate-lines' needs an argument.
2291     (toggle-truncate-lines)))
2292
2293 (defun gnus-article-treat-body-boundary ()
2294   "Place a boundary line at the end of the headers."
2295   (interactive)
2296   (when (and gnus-body-boundary-delimiter
2297              (> (length gnus-body-boundary-delimiter) 0))
2298     (gnus-with-article-headers
2299       (goto-char (point-max))
2300       (let ((start (point)))
2301         (insert "X-Boundary: ")
2302         (gnus-add-text-properties start (point) '(invisible t intangible t))
2303         (insert (let (str)
2304                   (while (>= (window-width) (length str))
2305                     (setq str (concat str gnus-body-boundary-delimiter)))
2306                   (substring str 0 (window-width)))
2307                 "\n")
2308         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2309
2310 (defun article-fill-long-lines ()
2311   "Fill lines that are wider than the window width."
2312   (interactive)
2313   (save-excursion
2314     (let ((inhibit-read-only t)
2315           (width (window-width (get-buffer-window (current-buffer)))))
2316       (save-restriction
2317         (article-goto-body)
2318         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2319           (while (not (eobp))
2320             (end-of-line)
2321             (when (>= (current-column) (min fill-column width))
2322               (narrow-to-region (min (1+ (point)) (point-max))
2323                                 (point-at-bol))
2324               (let ((goback (point-marker)))
2325                 (fill-paragraph nil)
2326                 (goto-char (marker-position goback)))
2327               (widen))
2328             (forward-line 1)))))))
2329
2330 (defun article-capitalize-sentences ()
2331   "Capitalize the first word in each sentence."
2332   (interactive)
2333   (save-excursion
2334     (let ((inhibit-read-only t)
2335           (paragraph-start "^[\n\^L]"))
2336       (article-goto-body)
2337       (while (not (eobp))
2338         (capitalize-word 1)
2339         (forward-sentence)))))
2340
2341 (defun article-remove-cr ()
2342   "Remove trailing CRs and then translate remaining CRs into LFs."
2343   (interactive)
2344   (save-excursion
2345     (let ((inhibit-read-only t))
2346       (goto-char (point-min))
2347       (while (re-search-forward "\r+$" nil t)
2348         (replace-match "" t t))
2349       (goto-char (point-min))
2350       (while (search-forward "\r" nil t)
2351         (replace-match "\n" t t)))))
2352
2353 (defun article-remove-trailing-blank-lines ()
2354   "Remove all trailing blank lines from the article."
2355   (interactive)
2356   (save-excursion
2357     (let ((inhibit-read-only t))
2358       (goto-char (point-max))
2359       (delete-region
2360        (point)
2361        (progn
2362          (while (and (not (bobp))
2363                      (looking-at "^[ \t]*$")
2364                      (not (gnus-annotation-in-region-p
2365                            (point) (point-at-eol))))
2366            (forward-line -1))
2367          (forward-line 1)
2368          (point))))))
2369
2370 (defvar gnus-face-properties-alist)
2371
2372 (defun article-display-face (&optional force)
2373   "Display any Face headers in the header."
2374   (interactive (list 'force))
2375   (let ((wash-face-p buffer-read-only))
2376     (gnus-with-article-headers
2377       ;; When displaying parts, this function can be called several times on
2378       ;; the same article, without any intended toggle semantic (as typing `W
2379       ;; D d' would have). So face deletion must occur only when we come from
2380       ;; an interactive command, that is when the *Article* buffer is
2381       ;; read-only.
2382       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2383           (gnus-delete-images 'face)
2384         (let ((from (message-fetch-field "from"))
2385               face faces)
2386           (save-current-buffer
2387             (when (and wash-face-p
2388                        (gnus-buffer-live-p gnus-original-article-buffer)
2389                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2390               (set-buffer gnus-original-article-buffer))
2391             (save-restriction
2392               (mail-narrow-to-head)
2393               (when (or force
2394                         ;; Check whether this face is censored.
2395                         (not (and gnus-article-x-face-too-ugly
2396                                   (or from
2397                                       (setq from (message-fetch-field "from")))
2398                                   (string-match gnus-article-x-face-too-ugly
2399                                                 from))))
2400                 (while (gnus-article-goto-header "Face")
2401                   (push (mail-header-field-value) faces)))))
2402           (when faces
2403             (goto-char (point-min))
2404             (let (png image)
2405               (unless (setq from (gnus-article-goto-header "from"))
2406                 (insert "From:")
2407                 (setq from (point))
2408                 (insert " [no `from' set]\n"))
2409               (while faces
2410                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2411                   (setq image
2412                         (apply 'gnus-create-image png 'png t
2413                                (cdr (assq 'png gnus-face-properties-alist))))
2414                   (goto-char from)
2415                   (gnus-add-wash-type 'face)
2416                   (gnus-add-image 'face image)
2417                   (gnus-put-image image nil 'face))))))))))
2418
2419 (defun article-display-x-face (&optional force)
2420   "Look for an X-Face header and display it if present."
2421   (interactive (list 'force))
2422   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2423     (gnus-with-article-headers
2424       ;; Delete the old process, if any.
2425       (when (process-status "article-x-face")
2426         (delete-process "article-x-face"))
2427       ;; See the comment in `article-display-face'.
2428       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2429           ;; We have already displayed X-Faces, so we remove them
2430           ;; instead.
2431           (gnus-delete-images 'xface)
2432         ;; Display X-Faces.
2433         (let ((from (message-fetch-field "from"))
2434               x-faces face)
2435           (save-current-buffer
2436             (when (and wash-face-p
2437                        (gnus-buffer-live-p gnus-original-article-buffer)
2438                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2439               ;; If type `W f', use gnus-original-article-buffer,
2440               ;; otherwise use the current buffer because displaying
2441               ;; RFC822 parts calls this function too.
2442               (set-buffer gnus-original-article-buffer))
2443             (save-restriction
2444               (mail-narrow-to-head)
2445               (and gnus-article-x-face-command
2446                    (or force
2447                        ;; Check whether this face is censored.
2448                        (not (and gnus-article-x-face-too-ugly
2449                                  (or from
2450                                      (setq from (message-fetch-field "from")))
2451                                  (string-match gnus-article-x-face-too-ugly
2452                                                from))))
2453                    (while (gnus-article-goto-header "X-Face")
2454                      (push (mail-header-field-value) x-faces)))))
2455           (when x-faces
2456             ;; We display the face.
2457             (cond ((functionp gnus-article-x-face-command)
2458                    ;; The command is a lisp function, so we call it.
2459                    (mapc gnus-article-x-face-command x-faces))
2460                   ((stringp gnus-article-x-face-command)
2461                    ;; The command is a string, so we interpret the command
2462                    ;; as a, well, command, and fork it off.
2463                    (let ((process-connection-type nil))
2464                      (gnus-set-process-query-on-exit-flag
2465                       (start-process
2466                        "article-x-face" nil shell-file-name
2467                        shell-command-switch gnus-article-x-face-command)
2468                       nil)
2469                      ;; Sending multiple EOFs to xv doesn't work,
2470                      ;; so we only do a single external face.
2471                      (with-temp-buffer
2472                        (insert (car x-faces))
2473                        (process-send-region "article-x-face"
2474                                             (point-min) (point-max)))
2475                      (process-send-eof "article-x-face")))
2476                   (t
2477                    (error "`%s' set to `%s' is not a function"
2478                           gnus-article-x-face-command
2479                           'gnus-article-x-face-command)))))))))
2480
2481 (defun article-decode-mime-words ()
2482   "Decode all MIME-encoded words in the article."
2483   (interactive)
2484   (gnus-with-article-buffer
2485     (let ((inhibit-point-motion-hooks t)
2486           (mail-parse-charset gnus-newsgroup-charset)
2487           (mail-parse-ignored-charsets
2488            (with-current-buffer gnus-summary-buffer
2489              gnus-newsgroup-ignored-charsets)))
2490       (mail-decode-encoded-word-region (point-min) (point-max)))))
2491
2492 (defun article-decode-charset (&optional prompt)
2493   "Decode charset-encoded text in the article.
2494 If PROMPT (the prefix), prompt for a coding system to use."
2495   (interactive "P")
2496   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2497         (inhibit-read-only t)
2498         (mail-parse-charset gnus-newsgroup-charset)
2499         (mail-parse-ignored-charsets
2500          (save-excursion (condition-case nil
2501                              (set-buffer gnus-summary-buffer)
2502                            (error))
2503                          gnus-newsgroup-ignored-charsets))
2504         ct cte ctl charset format)
2505     (save-excursion
2506       (save-restriction
2507         (article-narrow-to-head)
2508         (setq ct (message-fetch-field "Content-Type" t)
2509               cte (message-fetch-field "Content-Transfer-Encoding" t)
2510               ctl (and ct (mail-header-parse-content-type ct))
2511               charset (cond
2512                        (prompt
2513                         (mm-read-coding-system "Charset to decode: "))
2514                        (ctl
2515                         (mail-content-type-get ctl 'charset)))
2516               format (and ctl (mail-content-type-get ctl 'format)))
2517         (when cte
2518           (setq cte (mail-header-strip cte)))
2519         (if (and ctl (not (string-match "/" (car ctl))))
2520             (setq ctl nil))
2521         (goto-char (point-max)))
2522       (forward-line 1)
2523       (save-restriction
2524         (narrow-to-region (point) (point-max))
2525         (when (and (eq mail-parse-charset 'gnus-decoded)
2526                    (eq (mm-body-7-or-8) '8bit))
2527           ;; The text code could have been decoded.
2528           (setq charset mail-parse-charset))
2529         (when (and (or (not ctl)
2530                        (equal (car ctl) "text/plain"))
2531                    (not format)) ;; article with format will decode later.
2532           (mm-decode-body
2533            charset (and cte (intern (downcase
2534                                      (gnus-strip-whitespace cte))))
2535            (car ctl)))))))
2536
2537 (defun article-decode-encoded-words ()
2538   "Remove encoded-word encoding from headers."
2539   (let ((inhibit-point-motion-hooks t)
2540         (mail-parse-charset gnus-newsgroup-charset)
2541         (mail-parse-ignored-charsets
2542          (save-excursion (condition-case nil
2543                              (set-buffer gnus-summary-buffer)
2544                            (error))
2545                          gnus-newsgroup-ignored-charsets))
2546         (inhibit-read-only t)
2547         end start)
2548     (goto-char (point-min))
2549     (when (search-forward "\n\n" nil 'move)
2550       (forward-line -1))
2551     (setq end (point))
2552     (while (not (bobp))
2553       (while (progn
2554                (forward-line -1)
2555                (and (not (bobp))
2556                     (memq (char-after) '(?\t ? )))))
2557       (setq start (point))
2558       (if (looking-at "\
2559 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2560 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2561           (funcall gnus-decode-address-function start end)
2562         (funcall gnus-decode-header-function start end))
2563       (goto-char (setq end start)))))
2564
2565 (defun article-decode-group-name ()
2566   "Decode group names in Newsgroups, Followup-To and Xref headers."
2567   (let ((inhibit-point-motion-hooks t)
2568         (inhibit-read-only t)
2569         (method (gnus-find-method-for-group gnus-newsgroup-name))
2570         regexp)
2571     (when (and (or gnus-group-name-charset-method-alist
2572                    gnus-group-name-charset-group-alist)
2573                (gnus-buffer-live-p gnus-original-article-buffer))
2574       (save-restriction
2575         (article-narrow-to-head)
2576         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2577           (with-current-buffer gnus-original-article-buffer
2578             (goto-char (point-min)))
2579           (setq regexp (concat "^" header
2580                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2581           (while (re-search-forward regexp nil t)
2582             (replace-match (save-match-data
2583                              (gnus-decode-newsgroups
2584                               ;; XXX how to use data in article buffer?
2585                               (with-current-buffer gnus-original-article-buffer
2586                                 (re-search-forward regexp nil t)
2587                                 (match-string 1))
2588                               gnus-newsgroup-name method))
2589                            t t nil 1))
2590           (goto-char (point-min)))))))
2591
2592 (autoload 'idna-to-unicode "idna")
2593
2594 (defun article-decode-idna-rhs ()
2595   "Decode IDNA strings in RHS in various headers in current buffer.
2596 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2597 Mail-Reply-To: and Mail-Followup-To:."
2598   (when gnus-use-idna
2599     (save-restriction
2600       (let ((inhibit-point-motion-hooks t)
2601             (inhibit-read-only t))
2602         (article-narrow-to-head)
2603         (goto-char (point-min))
2604         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2605           (let (ace unicode)
2606             (when (save-match-data
2607                     (and (setq ace (match-string 1))
2608                          (save-excursion
2609                            (and (re-search-backward "^[^ \t]" nil t)
2610                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2611                          (setq unicode (idna-to-unicode ace))))
2612               (unless (string= ace unicode)
2613                 (replace-match unicode nil nil nil 1)))))))))
2614
2615 (defun article-de-quoted-unreadable (&optional force read-charset)
2616   "Translate a quoted-printable-encoded article.
2617 If FORCE, decode the article whether it is marked as quoted-printable
2618 or not.
2619 If READ-CHARSET, ask for a coding system."
2620   (interactive (list 'force current-prefix-arg))
2621   (save-excursion
2622     (let ((inhibit-read-only t) type charset)
2623       (if (gnus-buffer-live-p gnus-original-article-buffer)
2624           (with-current-buffer gnus-original-article-buffer
2625             (setq type
2626                   (gnus-fetch-field "content-transfer-encoding"))
2627             (let* ((ct (gnus-fetch-field "content-type"))
2628                    (ctl (and ct (mail-header-parse-content-type ct))))
2629               (setq charset (and ctl
2630                                  (mail-content-type-get ctl 'charset)))
2631               (if (stringp charset)
2632                   (setq charset (intern (downcase charset)))))))
2633       (if read-charset
2634           (setq charset (mm-read-coding-system "Charset: " charset)))
2635       (unless charset
2636         (setq charset gnus-newsgroup-charset))
2637       (when (or force
2638                 (and type (let ((case-fold-search t))
2639                             (string-match "quoted-printable" type))))
2640         (article-goto-body)
2641         (quoted-printable-decode-region
2642          (point) (point-max) (mm-charset-to-coding-system charset))))))
2643
2644 (defun article-de-base64-unreadable (&optional force read-charset)
2645   "Translate a base64 article.
2646 If FORCE, decode the article whether it is marked as base64 not.
2647 If READ-CHARSET, ask for a coding system."
2648   (interactive (list 'force current-prefix-arg))
2649   (save-excursion
2650     (let ((inhibit-read-only t) type charset)
2651       (if (gnus-buffer-live-p gnus-original-article-buffer)
2652           (with-current-buffer gnus-original-article-buffer
2653             (setq type
2654                   (gnus-fetch-field "content-transfer-encoding"))
2655             (let* ((ct (gnus-fetch-field "content-type"))
2656                    (ctl (and ct (mail-header-parse-content-type ct))))
2657               (setq charset (and ctl
2658                                  (mail-content-type-get ctl 'charset)))
2659               (if (stringp charset)
2660                   (setq charset (intern (downcase charset)))))))
2661       (if read-charset
2662           (setq charset (mm-read-coding-system "Charset: " charset)))
2663       (unless charset
2664         (setq charset gnus-newsgroup-charset))
2665       (when (or force
2666                 (and type (let ((case-fold-search t))
2667                             (string-match "base64" type))))
2668         (article-goto-body)
2669         (save-restriction
2670           (narrow-to-region (point) (point-max))
2671           (base64-decode-region (point-min) (point-max))
2672           (mm-decode-coding-region
2673            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2674
2675 (eval-when-compile
2676   (require 'rfc1843))
2677
2678 (defun article-decode-HZ ()
2679   "Translate a HZ-encoded article."
2680   (interactive)
2681   (require 'rfc1843)
2682   (save-excursion
2683     (let ((inhibit-read-only t))
2684       (rfc1843-decode-region (point-min) (point-max)))))
2685
2686 (defun article-unsplit-urls ()
2687   "Remove the newlines that some other mailers insert into URLs."
2688   (interactive)
2689   (save-excursion
2690     (let ((inhibit-read-only t))
2691       (goto-char (point-min))
2692       (while (re-search-forward
2693               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2694         (replace-match "\\1\\3" t)))
2695     (when (interactive-p)
2696       (gnus-treat-article nil))))
2697
2698
2699 (defun article-wash-html (&optional read-charset)
2700   "Format an HTML article.
2701 If READ-CHARSET, ask for a coding system.  If it is a number, the
2702 charset defined in `gnus-summary-show-article-charset-alist' is used."
2703   (interactive "P")
2704   (save-excursion
2705     (let ((inhibit-read-only t)
2706           charset)
2707       (if read-charset
2708           (if (or (and (numberp read-charset)
2709                        (setq charset
2710                              (cdr
2711                               (assq read-charset
2712                                     gnus-summary-show-article-charset-alist))))
2713                   (setq charset (mm-read-coding-system "Charset: ")))
2714               (let ((gnus-summary-show-article-charset-alist
2715                      (list (cons 1 charset))))
2716                 (with-current-buffer gnus-summary-buffer
2717                   (gnus-summary-show-article 1)))
2718             (error "No charset is given"))
2719         (when (gnus-buffer-live-p gnus-original-article-buffer)
2720           (with-current-buffer gnus-original-article-buffer
2721             (let* ((ct (gnus-fetch-field "content-type"))
2722                    (ctl (and ct (mail-header-parse-content-type ct))))
2723               (setq charset (and ctl
2724                                  (mail-content-type-get ctl 'charset)))
2725               (when (stringp charset)
2726                 (setq charset (intern (downcase charset)))))))
2727         (unless charset
2728           (setq charset gnus-newsgroup-charset)))
2729       (article-goto-body)
2730       (save-window-excursion
2731         (save-restriction
2732           (narrow-to-region (point) (point-max))
2733           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2734                  (entry (assq func mm-text-html-washer-alist)))
2735             (when entry
2736               (setq func (cdr entry)))
2737             (cond
2738              ((functionp func)
2739               (funcall func))
2740              (t
2741               (apply (car func) (cdr func))))))))))
2742
2743 ;; External.
2744 (declare-function w3-region "ext:w3-display" (st nd))
2745
2746 (defun gnus-article-wash-html-with-w3 ()
2747   "Wash the current buffer with w3."
2748   (mm-setup-w3)
2749   (let ((w3-strict-width (window-width))
2750         (url-standalone-mode t)
2751         (url-gateway-unplugged t)
2752         (w3-honor-stylesheets nil))
2753     (condition-case ()
2754         (w3-region (point-min) (point-max))
2755       (error))))
2756
2757 ;; External.
2758 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2759
2760 (defun gnus-article-wash-html-with-w3m ()
2761   "Wash the current buffer with emacs-w3m."
2762   (mm-setup-w3m)
2763   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2764         w3m-force-redisplay)
2765     (w3m-region (point-min) (point-max)))
2766   ;; Put the mark meaning this part was rendered by emacs-w3m.
2767   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2768   (when (and mm-inline-text-html-with-w3m-keymap
2769              (boundp 'w3m-minor-mode-map)
2770              w3m-minor-mode-map)
2771     (if (and (boundp 'w3m-link-map)
2772              w3m-link-map)
2773         (let* ((start (point-min))
2774                (end (point-max))
2775                (on (get-text-property start 'w3m-href-anchor))
2776                (map (copy-keymap w3m-link-map))
2777                next)
2778           (set-keymap-parent map w3m-minor-mode-map)
2779           (while (< start end)
2780             (if on
2781                 (progn
2782                   (setq next (or (text-property-any start end
2783                                                     'w3m-href-anchor nil)
2784                                  end))
2785                   (put-text-property start next 'keymap map))
2786               (setq next (or (text-property-not-all start end
2787                                                     'w3m-href-anchor nil)
2788                              end))
2789               (put-text-property start next 'keymap w3m-minor-mode-map))
2790             (setq start next
2791                   on (not on))))
2792       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2793
2794 (defvar charset) ;; Bound by `article-wash-html'.
2795
2796 (defun gnus-article-wash-html-with-w3m-standalone ()
2797   "Wash the current buffer with w3m."
2798   (if (mm-w3m-standalone-supports-m17n-p)
2799       (progn
2800         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2801           ;; The default.
2802           (setq charset 'iso-8859-1))
2803         (let ((coding-system-for-write charset)
2804               (coding-system-for-read charset))
2805           (call-process-region
2806            (point-min) (point-max)
2807            "w3m" t t nil "-dump" "-T" "text/html"
2808            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2809     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2810
2811 (defvar gnus-article-browse-html-temp-list nil
2812   "List of temporary files created by `gnus-article-browse-html-parts'.
2813 Internal variable.")
2814
2815 (defcustom gnus-article-browse-delete-temp 'ask
2816   "What to do with temporary files from `gnus-article-browse-html-parts'.
2817 If nil, don't delete temporary files.  If it is t, delete them on
2818 exit from the summary buffer.  If it is the symbol `file', query
2819 on each file, if it is `ask' ask once when exiting from the
2820 summary buffer."
2821   :group 'gnus-article
2822   :version "23.1" ;; No Gnus
2823   :type '(choice (const :tag "Don't delete" nil)
2824                  (const :tag "Don't ask" t)
2825                  (const :tag "Ask" ask)
2826                  (const :tag "Ask for each file" file)))
2827
2828 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2829
2830 (defun gnus-article-browse-delete-temp-files (&optional how)
2831   "Delete temp-files created by `gnus-article-browse-html-parts'."
2832   (when (and gnus-article-browse-html-temp-list
2833              (progn
2834                (or how (setq how gnus-article-browse-delete-temp))
2835                (if (eq how 'ask)
2836                    (let ((files (length gnus-article-browse-html-temp-list)))
2837                      (gnus-y-or-n-p (format
2838                                      "Delete all %s temporary HTML file%s? "
2839                                      files
2840                                      (if (> files 1) "s" ""))))
2841                  how)))
2842     (dolist (file gnus-article-browse-html-temp-list)
2843       (cond ((file-directory-p file)
2844              (when (or (not (eq how 'file))
2845                        (gnus-y-or-n-p
2846                         (format
2847                          "Delete temporary HTML file(s) in directory `%s'? "
2848                          (file-name-as-directory file))))
2849                (gnus-delete-directory file)))
2850             ((file-exists-p file)
2851              (when (or (not (eq how 'file))
2852                        (gnus-y-or-n-p
2853                         (format "Delete temporary HTML file `%s'? " file)))
2854                (delete-file file)))))
2855     ;; Also remove file from the list when not deleted or if file doesn't
2856     ;; exist anymore.
2857     (setq gnus-article-browse-html-temp-list nil))
2858   gnus-article-browse-html-temp-list)
2859
2860 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2861   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2862 Return file name."
2863   (save-match-data
2864     (let (file type)
2865       (catch 'found
2866         (dolist (handle handles)
2867           (cond
2868            ((not (listp handle)))
2869            ((equal (mm-handle-media-supertype handle) "multipart")
2870             (when (setq file (gnus-article-browse-html-save-cid-content
2871                               cid handle directory))
2872               (throw 'found file)))
2873            ((equal (concat "<" cid ">") (mm-handle-id handle))
2874             (setq file
2875                   (expand-file-name
2876                    (or (mail-content-type-get
2877                         (mm-handle-disposition handle) 'filename)
2878                        (mail-content-type-get
2879                         (setq type (mm-handle-type handle)) 'name)
2880                        (concat
2881                         (make-temp-name "cid")
2882                         (car (rassoc (car type) mailcap-mime-extensions))))
2883                    directory))
2884             (mm-save-part-to-file handle file)
2885             (throw 'found file))))))))
2886
2887 (defun gnus-article-browse-html-parts (list &optional header)
2888   "View all \"text/html\" parts from LIST.
2889 Recurse into multiparts.  The optional HEADER that should be a decoded
2890 message header will be added to the bodies of the \"text/html\" parts."
2891   ;; Internal function used by `gnus-article-browse-html-article'.
2892   (let (type file charset content cid-dir tmp-file showed)
2893     ;; Find and show the html-parts.
2894     (dolist (handle list)
2895       ;; If HTML, show it:
2896       (cond ((not (listp handle)))
2897             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2898                  (and (equal (car type) "message/external-body")
2899                       (or header
2900                           (setq file (or (mail-content-type-get type 'name)
2901                                          (mail-content-type-get
2902                                           (mm-handle-disposition handle)
2903                                           'filename))))
2904                       (or (mm-handle-cache handle)
2905                           (condition-case code
2906                               (progn (mm-extern-cache-contents handle) t)
2907                             (error
2908                              (gnus-message 3 "%s" (error-message-string code))
2909                              (when (>= gnus-verbose 3) (sit-for 2))
2910                              nil)))
2911                       (progn
2912                         (setq handle (mm-handle-cache handle)
2913                               type (mm-handle-type handle))
2914                         (equal (car type) "text/html"))))
2915              (setq charset (mail-content-type-get type 'charset)
2916                    content (mm-get-part handle))
2917              (with-temp-buffer
2918                (if (eq charset 'gnus-decoded)
2919                    (mm-enable-multibyte)
2920                  (mm-disable-multibyte))
2921                (insert content)
2922                ;; resolve cid contents
2923                (let ((case-fold-search t)
2924                      cid-file)
2925                  (goto-char (point-min))
2926                  (while (re-search-forward "\
2927 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2928                                            nil t)
2929                    (unless cid-dir
2930                      (setq cid-dir (mm-make-temp-file "cid" t))
2931                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2932                    (setq file nil
2933                          content nil)
2934                    (when (setq cid-file
2935                                (gnus-article-browse-html-save-cid-content
2936                                 (match-string 2)
2937                                 (with-current-buffer gnus-article-buffer
2938                                   gnus-article-mime-handles)
2939                                 cid-dir))
2940                      (replace-match (concat "file://" cid-file)
2941                                     nil nil nil 1))))
2942                (unless content (setq content (buffer-string))))
2943              (when (or charset header (not file))
2944                (setq tmp-file (mm-make-temp-file
2945                                ;; Do we need to care for 8.3 filenames?
2946                                "mm-" nil ".html")))
2947              ;; Add a meta html tag to specify charset and a header.
2948              (cond
2949               (header
2950                (let (title eheader body hcharset coding force-charset)
2951                  (with-temp-buffer
2952                    (mm-enable-multibyte)
2953                    (setq case-fold-search t)
2954                    (insert header "\n")
2955                    (setq title (message-fetch-field "subject"))
2956                    (goto-char (point-min))
2957                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2958                      (replace-match (cond ((match-beginning 1) "&lt;")
2959                                           ((match-beginning 2) "&gt;")
2960                                           (t "&amp;"))))
2961                    (goto-char (point-min))
2962                    (insert "<pre>\n")
2963                    (goto-char (point-max))
2964                    (insert "</pre>\n<hr>\n")
2965                    ;; We have to examine charset one by one since
2966                    ;; charset specified in parts might be different.
2967                    (if (eq charset 'gnus-decoded)
2968                        (setq charset 'utf-8
2969                              eheader (mm-encode-coding-string (buffer-string)
2970                                                               charset)
2971                              title (when title
2972                                      (mm-encode-coding-string title charset))
2973                              body (mm-encode-coding-string content charset)
2974                              force-charset t)
2975                      (setq hcharset (mm-find-mime-charset-region (point-min)
2976                                                                  (point-max)))
2977                      (cond ((= (length hcharset) 1)
2978                             (setq hcharset (car hcharset)
2979                                   coding (mm-charset-to-coding-system
2980                                           hcharset)))
2981                            ((> (length hcharset) 1)
2982                             (setq hcharset 'utf-8
2983                                   coding hcharset)))
2984                      (if coding
2985                          (if charset
2986                              (progn
2987                                (setq body
2988                                      (mm-charset-to-coding-system charset))
2989                                (if (eq coding body)
2990                                    (setq eheader (mm-encode-coding-string
2991                                                   (buffer-string) coding)
2992                                          title (when title
2993                                                  (mm-encode-coding-string
2994                                                   title coding))
2995                                          body content)
2996                                  (setq charset 'utf-8
2997                                        eheader (mm-encode-coding-string
2998                                                 (buffer-string) charset)
2999                                        title (when title
3000                                                (mm-encode-coding-string
3001                                                 title charset))
3002                                        body (mm-encode-coding-string
3003                                              (mm-decode-coding-string
3004                                               content body)
3005                                              charset)
3006                                        force-charset t)))
3007                            (setq charset hcharset
3008                                  eheader (mm-encode-coding-string
3009                                           (buffer-string) coding)
3010                                  title (when title
3011                                          (mm-encode-coding-string
3012                                           title coding))
3013                                  body content))
3014                        (setq eheader (mm-string-as-unibyte (buffer-string))
3015                              body content)))
3016                    (erase-buffer)
3017                    (mm-disable-multibyte)
3018                    (insert body)
3019                    (when charset
3020                      (mm-add-meta-html-tag handle charset force-charset))
3021                    (when title
3022                      (goto-char (point-min))
3023                      (unless (search-forward "<title>" nil t)
3024                        (re-search-forward "<head>\\s-*" nil t)
3025                        (insert "<title>" title "</title>\n")))
3026                    (goto-char (point-min))
3027                    (or (re-search-forward
3028                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3029                        (re-search-forward
3030                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3031                    (insert eheader)
3032                    (mm-write-region (point-min) (point-max)
3033                                     tmp-file nil nil nil 'binary t))))
3034               (charset
3035                (mm-with-unibyte-buffer
3036                  (insert (if (eq charset 'gnus-decoded)
3037                              (mm-encode-coding-string content
3038                                                       (setq charset 'utf-8))
3039                            content))
3040                  (if (or (mm-add-meta-html-tag handle charset)
3041                          (not file))
3042                      (mm-write-region (point-min) (point-max)
3043                                       tmp-file nil nil nil 'binary t)
3044                    (setq tmp-file nil))))
3045               (tmp-file
3046                (mm-save-part-to-file handle tmp-file)))
3047              (when tmp-file
3048                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3049              (add-hook 'gnus-summary-prepare-exit-hook
3050                        'gnus-article-browse-delete-temp-files)
3051              (add-hook 'gnus-exit-gnus-hook
3052                        (lambda  ()
3053                          (gnus-article-browse-delete-temp-files t)))
3054              ;; FIXME: Warn if there's an <img> tag?
3055              (browse-url-of-file (or tmp-file (expand-file-name file)))
3056              (setq showed t))
3057             ;; If multipart, recurse
3058             ((equal (mm-handle-media-supertype handle) "multipart")
3059              (when (gnus-article-browse-html-parts handle header)
3060                (setq showed t)))
3061             ((equal (mm-handle-media-type handle) "message/rfc822")
3062              (mm-with-multibyte-buffer
3063                (mm-insert-part handle)
3064                (setq handle (mm-dissect-buffer t t))
3065                (when (and (bufferp (car handle))
3066                           (stringp (car (mm-handle-type handle))))
3067                  (setq handle (list handle)))
3068                (when header
3069                  (article-decode-encoded-words)
3070                  (let ((gnus-visible-headers
3071                         (or (get 'gnus-visible-headers 'standard-value)
3072                             gnus-visible-headers)))
3073                    (article-hide-headers))
3074                  (goto-char (point-min))
3075                  (search-forward "\n\n" nil 'move)
3076                  (skip-chars-backward "\t\n ")
3077                  (setq header (buffer-substring (point-min) (point)))))
3078              (when (prog1
3079                        (gnus-article-browse-html-parts handle header)
3080                      (mm-destroy-parts handle))
3081                (setq showed t)))))
3082     showed))
3083
3084 (defun gnus-article-browse-html-article (&optional arg)
3085   "View \"text/html\" parts of the current article with a WWW browser.
3086 Inline images embedded in a message using the cid scheme, as they are
3087 generally considered to be safe, will be processed properly.
3088 The message header is added to the beginning of every html part unless
3089 the prefix argument ARG is given.
3090
3091 Warning: Spammers use links to images (using the http scheme) in HTML
3092 articles to verify whether you have read the message.  As
3093 `gnus-article-browse-html-article' passes the HTML content to the
3094 browser without eliminating these \"web bugs\" you should only
3095 use it for mails from trusted senders.
3096
3097 If you always want to display HTML parts in the browser, set
3098 `mm-text-html-renderer' to nil.
3099
3100 This command creates temporary files to pass HTML contents including
3101 images if any to the browser, and deletes them when exiting the group
3102 \(if you want)."
3103   ;; Cf. `mm-w3m-safe-url-regexp'
3104   (interactive "P")
3105   (if arg
3106       (gnus-summary-show-article)
3107     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3108                                     gnus-visible-headers))
3109           ;; As we insert a <hr>, there's no need for the body boundary.
3110           (gnus-treat-body-boundary nil))
3111       (gnus-summary-show-article)))
3112   (with-current-buffer gnus-article-buffer
3113     (let ((header (unless arg
3114                     (save-restriction
3115                       (widen)
3116                       (buffer-substring-no-properties
3117                        (goto-char (point-min))
3118                        (if (search-forward "\n\n" nil t)
3119                            (match-beginning 0)
3120                          (goto-char (point-max))
3121                          (skip-chars-backward "\t\n ")
3122                          (point))))))
3123           parts)
3124       (set-buffer gnus-original-article-buffer)
3125       (setq parts (mm-dissect-buffer t t))
3126       ;; If singlepart, enforce a list.
3127       (when (and (bufferp (car parts))
3128                  (stringp (car (mm-handle-type parts))))
3129         (setq parts (list parts)))
3130       ;; Process the list
3131       (unless (gnus-article-browse-html-parts parts header)
3132         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3133       (mm-destroy-parts parts)
3134       (unless arg
3135         (gnus-summary-show-article)))))
3136
3137 (defun article-hide-list-identifiers ()
3138   "Remove list identifies from the Subject header.
3139 The `gnus-list-identifiers' variable specifies what to do."
3140   (interactive)
3141   (let ((inhibit-point-motion-hooks t)
3142         (regexp (if (consp gnus-list-identifiers)
3143                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3144                   gnus-list-identifiers))
3145         (inhibit-read-only t))
3146     (when regexp
3147       (save-excursion
3148         (save-restriction
3149           (article-narrow-to-head)
3150           (goto-char (point-min))
3151           (while (re-search-forward
3152                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3153                   nil t)
3154             (delete-region (match-beginning 2) (match-end 0))
3155             (beginning-of-line))
3156           (when (re-search-forward
3157                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3158             (delete-region (match-beginning 1) (match-end 1))))))))
3159
3160 (defun article-hide-pem (&optional arg)
3161   "Toggle hiding of any PEM headers and signatures in the current article.
3162 If given a negative prefix, always show; if given a positive prefix,
3163 always hide."
3164   (interactive (gnus-article-hidden-arg))
3165   (unless (gnus-article-check-hidden-text 'pem arg)
3166     (save-excursion
3167       (let ((inhibit-read-only t) end)
3168         (goto-char (point-min))
3169         ;; Hide the horrendously ugly "header".
3170         (when (and (search-forward
3171                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3172                     nil t)
3173                    (setq end (1+ (match-beginning 0))))
3174           (gnus-add-wash-type 'pem)
3175           (gnus-article-hide-text-type
3176            end
3177            (if (search-forward "\n\n" nil t)
3178                (match-end 0)
3179              (point-max))
3180            'pem)
3181           ;; Hide the trailer as well
3182           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3183                                 nil t)
3184             (gnus-article-hide-text-type
3185              (match-beginning 0) (match-end 0) 'pem)))))))
3186
3187 (defun article-strip-banner ()
3188   "Strip the banners specified by the `banner' group parameter and by
3189 `gnus-article-address-banner-alist'."
3190   (interactive)
3191   (save-excursion
3192     (save-restriction
3193       (let ((inhibit-point-motion-hooks t))
3194         (when (gnus-parameter-banner gnus-newsgroup-name)
3195           (article-really-strip-banner
3196            (gnus-parameter-banner gnus-newsgroup-name)))
3197         (when gnus-article-address-banner-alist
3198           ;; Note that the From header is decoded here, so it is
3199           ;; required that the *-extract-address-components function
3200           ;; supports non-ASCII text.
3201           (let ((from (save-restriction
3202                         (widen)
3203                         (article-narrow-to-head)
3204                         (mail-fetch-field "from"))))
3205             (when (and from
3206                        (setq from
3207                              (cadr (funcall gnus-extract-address-components
3208                                             from))))
3209               (catch 'found
3210                 (dolist (pair gnus-article-address-banner-alist)
3211                   (when (string-match (car pair) from)
3212                     (throw 'found
3213                            (article-really-strip-banner (cdr pair)))))))))))))
3214
3215 (defun article-really-strip-banner (banner)
3216   "Strip the banner specified by the argument."
3217   (save-excursion
3218     (save-restriction
3219       (let ((inhibit-point-motion-hooks t)
3220             (gnus-signature-limit nil)
3221             (inhibit-read-only t))
3222         (article-goto-body)
3223         (cond
3224          ((eq banner 'signature)
3225           (when (gnus-article-narrow-to-signature)
3226             (widen)
3227             (forward-line -1)
3228             (delete-region (point) (point-max))))
3229          ((symbolp banner)
3230           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3231               (while (re-search-forward banner nil t)
3232                 (delete-region (match-beginning 0) (match-end 0)))))
3233          ((stringp banner)
3234           (while (re-search-forward banner nil t)
3235             (delete-region (match-beginning 0) (match-end 0)))))))))
3236
3237 (defun article-babel ()
3238   "Translate article using an online translation service."
3239   (interactive)
3240   (require 'babel)
3241   (gnus-with-article-buffer
3242     (when (article-goto-body)
3243       (let* ((start (point))
3244              (end (point-max))
3245              (orig (buffer-substring start end))
3246              (trans (babel-as-string orig)))
3247         (save-restriction
3248           (narrow-to-region start end)
3249           (delete-region start end)
3250           (insert trans))))))
3251
3252 (defun article-hide-signature (&optional arg)
3253   "Hide the signature in the current article.
3254 If given a negative prefix, always show; if given a positive prefix,
3255 always hide."
3256   (interactive (gnus-article-hidden-arg))
3257   (unless (gnus-article-check-hidden-text 'signature arg)
3258     (save-excursion
3259       (save-restriction
3260         (let ((inhibit-read-only t))
3261           (when (gnus-article-narrow-to-signature)
3262             (gnus-article-hide-text-type
3263              (point-min) (point-max) 'signature))))))
3264   (gnus-set-mode-line 'article))
3265
3266 (defun article-strip-headers-in-body ()
3267   "Strip offensive headers from bodies."
3268   (interactive)
3269   (save-excursion
3270     (article-goto-body)
3271     (let ((case-fold-search t))
3272       (when (looking-at "x-no-archive:")
3273         (gnus-delete-line)))))
3274
3275 (defun article-strip-leading-blank-lines ()
3276   "Remove all blank lines from the beginning of the article."
3277   (interactive)
3278   (save-excursion
3279     (let ((inhibit-point-motion-hooks t)
3280           (inhibit-read-only t))
3281       (when (article-goto-body)
3282         (while (and (not (eobp))
3283                     (looking-at "[ \t]*$"))
3284           (gnus-delete-line))))))
3285
3286 (defun article-narrow-to-head ()
3287   "Narrow the buffer to the head of the message.
3288 Point is left at the beginning of the narrowed-to region."
3289   (narrow-to-region
3290    (goto-char (point-min))
3291    (if (search-forward "\n\n" nil 1)
3292        (1- (point))
3293      (point-max)))
3294   (goto-char (point-min)))
3295
3296 (defun article-goto-body ()
3297   "Place point at the start of the body."
3298   (goto-char (point-min))
3299   (cond
3300    ;; This variable is only bound when dealing with separate
3301    ;; MIME body parts.
3302    (article-goto-body-goes-to-point-min-p
3303     t)
3304    ((search-forward "\n\n" nil t)
3305     t)
3306    (t
3307     (goto-char (point-max))
3308     nil)))
3309
3310 (defun article-strip-multiple-blank-lines ()
3311   "Replace consecutive blank lines with one empty line."
3312   (interactive)
3313   (save-excursion
3314     (let ((inhibit-point-motion-hooks t)
3315           (inhibit-read-only t))
3316       ;; First make all blank lines empty.
3317       (article-goto-body)
3318       (while (re-search-forward "^[ \t]+$" nil t)
3319         (unless (gnus-annotation-in-region-p
3320                  (match-beginning 0) (match-end 0))
3321           (replace-match "" nil t)))
3322       ;; Then replace multiple empty lines with a single empty line.
3323       (article-goto-body)
3324       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3325         (unless (gnus-annotation-in-region-p
3326                  (match-beginning 0) (match-end 0))
3327           (delete-region (match-beginning 1) (match-end 1)))))))
3328
3329 (defun article-strip-leading-space ()
3330   "Remove all white space from the beginning of the lines in the article."
3331   (interactive)
3332   (save-excursion
3333     (let ((inhibit-point-motion-hooks t)
3334           (inhibit-read-only t))
3335       (article-goto-body)
3336       (while (re-search-forward "^[ \t]+" nil t)
3337         (replace-match "" t t)))))
3338
3339 (defun article-strip-trailing-space ()
3340   "Remove all white space from the end of the lines in the article."
3341   (interactive)
3342   (save-excursion
3343     (let ((inhibit-point-motion-hooks t)
3344           (inhibit-read-only t))
3345       (article-goto-body)
3346       (while (re-search-forward "[ \t]+$" nil t)
3347         (replace-match "" t t)))))
3348
3349 (defun article-strip-blank-lines ()
3350   "Strip leading, trailing and multiple blank lines."
3351   (interactive)
3352   (article-strip-leading-blank-lines)
3353   (article-remove-trailing-blank-lines)
3354   (article-strip-multiple-blank-lines))
3355
3356 (defun article-strip-all-blank-lines ()
3357   "Strip all blank lines."
3358   (interactive)
3359   (save-excursion
3360     (let ((inhibit-point-motion-hooks t)
3361           (inhibit-read-only t))
3362       (article-goto-body)
3363       (while (re-search-forward "^[ \t]*\n" nil t)
3364         (replace-match "" t t)))))
3365
3366 (defun gnus-article-narrow-to-signature ()
3367   "Narrow to the signature; return t if a signature is found, else nil."
3368   (let ((inhibit-point-motion-hooks t))
3369     (when (gnus-article-search-signature)
3370       (forward-line 1)
3371       ;; Check whether we have some limits to what we consider
3372       ;; to be a signature.
3373       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3374                       (list gnus-signature-limit)))
3375             limit limited)
3376         (while (setq limit (pop limits))
3377           (if (or (and (integerp limit)
3378                        (< (- (point-max) (point)) limit))
3379                   (and (floatp limit)
3380                        (< (count-lines (point) (point-max)) limit))
3381                   (and (functionp limit)
3382                        (funcall limit))
3383                   (and (stringp limit)
3384                        (not (re-search-forward limit nil t))))
3385               ()                        ; This limit did not succeed.
3386             (setq limited t
3387                   limits nil)))
3388         (unless limited
3389           (narrow-to-region (point) (point-max))
3390           t)))))
3391
3392 (defun gnus-article-search-signature ()
3393   "Search the current buffer for the signature separator.
3394 Put point at the beginning of the signature separator."
3395   (let ((cur (point)))
3396     (goto-char (point-max))
3397     (if (if (stringp gnus-signature-separator)
3398             (re-search-backward gnus-signature-separator nil t)
3399           (let ((seps gnus-signature-separator))
3400             (while (and seps
3401                         (not (re-search-backward (car seps) nil t)))
3402               (pop seps))
3403             seps))
3404         t
3405       (goto-char cur)
3406       nil)))
3407
3408 (defun gnus-article-hidden-arg ()
3409   "Return the current prefix arg as a number, or 0 if no prefix."
3410   (list (if current-prefix-arg
3411             (prefix-numeric-value current-prefix-arg)
3412           0)))
3413
3414 (defun gnus-article-check-hidden-text (type arg)
3415   "Return nil if hiding is necessary.
3416 Arg can be nil or a number.  nil and positive means hide, negative
3417 means show, 0 means toggle."
3418   (save-excursion
3419     (save-restriction
3420       (let ((hide (gnus-article-hidden-text-p type)))
3421         (cond
3422          ((or (null arg)
3423               (> arg 0))
3424           nil)
3425          ((< arg 0)
3426           (gnus-article-show-hidden-text type)
3427           t)
3428          (t
3429           (if (eq hide 'hidden)
3430               (progn
3431                 (gnus-article-show-hidden-text type)
3432                 t)
3433             nil)))))))
3434
3435 (defun gnus-article-hidden-text-p (type)
3436   "Say whether the current buffer contains hidden text of type TYPE."
3437   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3438     (while (and pos
3439                 (not (get-text-property pos 'invisible))
3440                 (not (get-text-property pos 'dummy-invisible)))
3441       (setq pos
3442             (text-property-any (1+ pos) (point-max) 'article-type type)))
3443     (if pos
3444         'hidden
3445       nil)))
3446
3447 (defun gnus-article-show-hidden-text (type &optional dummy)
3448   "Show all hidden text of type TYPE.
3449 Originally it is hide instead of DUMMY."
3450   (let ((inhibit-read-only t)
3451         (inhibit-point-motion-hooks t))
3452     (gnus-remove-text-properties-when
3453      'article-type type
3454      (point-min) (point-max)
3455      (cons 'article-type (cons type
3456                                gnus-hidden-properties)))
3457     (gnus-delete-wash-type type)))
3458
3459 (defconst article-time-units
3460   `((year . ,(* 365.25 24 60 60))
3461     (week . ,(* 7 24 60 60))
3462     (day . ,(* 24 60 60))
3463     (hour . ,(* 60 60))
3464     (minute . 60)
3465     (second . 1))
3466   "Mapping from time units to seconds.")
3467
3468 (defun gnus-article-forward-header ()
3469   "Move point to the start of the next header.
3470 If the current header is a continuation header, this can be several
3471 lines forward."
3472   (let ((ended nil))
3473     (while (not ended)
3474       (forward-line 1)
3475       (if (looking-at "[ \t]+[^ \t]")
3476           (forward-line 1)
3477         (setq ended t)))))
3478
3479 (defun article-date-ut (&optional type highlight)
3480   "Convert DATE date to universal time in the current article.
3481 If TYPE is `local', convert to local time; if it is `lapsed', output
3482 how much time has lapsed since DATE.  For `lapsed', the value of
3483 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3484 should replace the \"Date:\" one, or should be added below it."
3485   (interactive (list 'ut t))
3486   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3487          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3488                              tdate-regexp)
3489                             ((eq type 'lapsed)
3490                              "^X-Sent:[ \t]")
3491                             (article-lapsed-timer
3492                              "^Date:[ \t]")
3493                             (t
3494                              tdate-regexp)))
3495          (case-fold-search t)
3496          (inhibit-read-only t)
3497          (inhibit-point-motion-hooks t)
3498          pos date bface eface)
3499     (save-excursion
3500       (save-restriction
3501         (widen)
3502         (goto-char (point-min))
3503         (while (or (setq date (get-text-property (setq pos (point))
3504                                                  'original-date))
3505                    (when (setq pos (next-single-property-change
3506                                     (point) 'original-date))
3507                      (setq date (get-text-property pos 'original-date))
3508                      t))
3509           (narrow-to-region
3510            pos (if (setq pos (text-property-any pos (point-max)
3511                                                 'original-date nil))
3512                    (progn
3513                      (goto-char pos)
3514                      (if (or (bolp) (eobp))
3515                          (point)
3516                        (1+ (point))))
3517                  (point-max)))
3518           (goto-char (point-min))
3519           (when (re-search-forward tdate-regexp nil t)
3520             (setq bface (get-text-property (point-at-bol) 'face)
3521                   eface (get-text-property (1- (point-at-eol)) 'face)))
3522           (goto-char (point-min))
3523           (setq pos nil)
3524           ;; Delete any old Date headers.
3525           (while (re-search-forward date-regexp nil t)
3526             (if pos
3527                 (delete-region (point-at-bol) (progn
3528                                                 (gnus-article-forward-header)
3529                                                 (point)))
3530               (delete-region (point-at-bol) (progn
3531                                               (gnus-article-forward-header)
3532                                               (forward-char -1)
3533                                               (point)))
3534               (setq pos (point))))
3535           (when (and (not pos)
3536                      (re-search-forward tdate-regexp nil t))
3537             (forward-line 1))
3538           (gnus-goto-char pos)
3539           (insert (article-make-date-line date (or type 'ut)))
3540           (unless pos
3541             (insert "\n")
3542             (forward-line -1))
3543           ;; Do highlighting.
3544           (beginning-of-line)
3545           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3546             (put-text-property (match-beginning 1) (1+ (match-end 1))
3547                                'face bface)
3548             (put-text-property (match-beginning 2) (match-end 2)
3549                                'face eface))
3550           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3551           (goto-char (point-max))
3552           (widen))))))
3553
3554 (defun article-make-date-line (date type)
3555   "Return a DATE line of TYPE."
3556   (unless (memq type '(local ut original user iso8601 lapsed english))
3557     (error "Unknown conversion type: %s" type))
3558   (condition-case ()
3559       (let ((time (date-to-time date)))
3560         (cond
3561          ;; Convert to the local timezone.
3562          ((eq type 'local)
3563           (concat "Date: " (message-make-date time)))
3564          ;; Convert to Universal Time.
3565          ((eq type 'ut)
3566           (concat "Date: "
3567                   (substring
3568                    (message-make-date
3569                     (let* ((e (parse-time-string date))
3570                            (tm (apply 'encode-time e))
3571                            (ms (car tm))
3572                            (ls (- (cadr tm) (car (current-time-zone time)))))
3573                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3574                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3575                             (t (list ms ls)))))
3576                    0 -5)
3577                   "UT"))
3578          ;; Get the original date from the article.
3579          ((eq type 'original)
3580           (concat "Date: " (if (string-match "\n+$" date)
3581                                (substring date 0 (match-beginning 0))
3582                              date)))
3583          ;; Let the user define the format.
3584          ((eq type 'user)
3585           (let ((format (or (condition-case nil
3586                                 (with-current-buffer gnus-summary-buffer
3587                                   gnus-article-time-format)
3588                               (error nil))
3589                             gnus-article-time-format)))
3590             (if (functionp format)
3591                 (funcall format time)
3592               (concat "Date: " (format-time-string format time)))))
3593          ;; ISO 8601.
3594          ((eq type 'iso8601)
3595           (let ((tz (car (current-time-zone time))))
3596             (concat
3597              "Date: "
3598              (format-time-string "%Y%m%dT%H%M%S" time)
3599              (format "%s%02d%02d"
3600                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3601                      (/ (% (abs tz) 3600) 60)))))
3602          ;; Do an X-Sent lapsed format.
3603          ((eq type 'lapsed)
3604           ;; If the date is seriously mangled, the timezone functions are
3605           ;; liable to bug out, so we ignore all errors.
3606           (let* ((now (current-time))
3607                  (real-time (subtract-time now time))
3608                  (real-sec (and real-time
3609                                 (+ (* (float (car real-time)) 65536)
3610                                    (cadr real-time))))
3611                  (sec (and real-time (abs real-sec)))
3612                  num prev)
3613             (cond
3614              ((null real-time)
3615               "X-Sent: Unknown")
3616              ((zerop sec)
3617               "X-Sent: Now")
3618              (t
3619               (concat
3620                "X-Sent: "
3621                ;; This is a bit convoluted, but basically we go
3622                ;; through the time units for years, weeks, etc,
3623                ;; and divide things to see whether that results
3624                ;; in positive answers.
3625                (mapconcat
3626                 (lambda (unit)
3627                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3628                       ;; The (remaining) seconds are too few to
3629                       ;; be divided into this time unit.
3630                       ""
3631                     ;; It's big enough, so we output it.
3632                     (setq sec (- sec (* num (cdr unit))))
3633                     (prog1
3634                         (concat (if prev ", " "") (int-to-string
3635                                                    (floor num))
3636                                 " " (symbol-name (car unit))
3637                                 (if (> num 1) "s" ""))
3638                       (setq prev t))))
3639                 article-time-units "")
3640                ;; If dates are odd, then it might appear like the
3641                ;; article was sent in the future.
3642                (if (> real-sec 0)
3643                    " ago"
3644                  " in the future"))))))
3645          ;; Display the date in proper English
3646          ((eq type 'english)
3647           (let ((dtime (decode-time time)))
3648             (concat
3649              "Date: the "
3650              (number-to-string (nth 3 dtime))
3651              (let ((digit (% (nth 3 dtime) 10)))
3652                (cond
3653                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3654                 ((= digit 1) "st")
3655                 ((= digit 2) "nd")
3656                 ((= digit 3) "rd")
3657                 (t "th")))
3658              " of "
3659              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3660              " "
3661              (number-to-string (nth 5 dtime))
3662              " at "
3663              (format "%02d" (nth 2 dtime))
3664              ":"
3665              (format "%02d" (nth 1 dtime)))))))
3666     (error
3667      (format "Date: %s (from Gnus)" date))))
3668
3669 (defun article-date-local (&optional highlight)
3670   "Convert the current article date to the local timezone."
3671   (interactive (list t))
3672   (article-date-ut 'local highlight))
3673
3674 (defun article-date-english (&optional highlight)
3675   "Convert the current article date to something that is proper English."
3676   (interactive (list t))
3677   (article-date-ut 'english highlight))
3678
3679 (defun article-date-original (&optional highlight)
3680   "Convert the current article date to what it was originally.
3681 This is only useful if you have used some other date conversion
3682 function and want to see what the date was before converting."
3683   (interactive (list t))
3684   (article-date-ut 'original highlight))
3685
3686 (defun article-date-lapsed (&optional highlight)
3687   "Convert the current article date to time lapsed since it was sent."
3688   (interactive (list t))
3689   (article-date-ut 'lapsed highlight))
3690
3691 (defun article-update-date-lapsed ()
3692   "Function to be run from a timer to update the lapsed time line."
3693   (save-match-data
3694     (let (deactivate-mark)
3695       (save-excursion
3696         (ignore-errors
3697          (walk-windows
3698           (lambda (w)
3699             (set-buffer (window-buffer w))
3700             (when (eq major-mode 'gnus-article-mode)
3701               (let ((mark (point-marker)))
3702                 (goto-char (point-min))
3703                 (when (re-search-forward "^X-Sent:" nil t)
3704                   (article-date-lapsed t))
3705                 (goto-char (marker-position mark))
3706                 (move-marker mark nil))))
3707           nil 'visible))))))
3708
3709 (defun gnus-start-date-timer (&optional n)
3710   "Start a timer to update the X-Sent header in the article buffers.
3711 The numerical prefix says how frequently (in seconds) the function
3712 is to run."
3713   (interactive "p")
3714   (unless n
3715     (setq n 1))
3716   (gnus-stop-date-timer)
3717   (setq article-lapsed-timer
3718         (run-at-time 1 n 'article-update-date-lapsed)))
3719
3720 (defun gnus-stop-date-timer ()
3721   "Stop the X-Sent timer."
3722   (interactive)
3723   (when article-lapsed-timer
3724     (nnheader-cancel-timer article-lapsed-timer)
3725     (setq article-lapsed-timer nil)))
3726
3727 (defun article-date-user (&optional highlight)
3728   "Convert the current article date to the user-defined format.
3729 This format is defined by the `gnus-article-time-format' variable."
3730   (interactive (list t))
3731   (article-date-ut 'user highlight))
3732
3733 (defun article-date-iso8601 (&optional highlight)
3734   "Convert the current article date to ISO8601."
3735   (interactive (list t))
3736   (article-date-ut 'iso8601 highlight))
3737
3738 (defmacro gnus-article-save-original-date (&rest forms)
3739   "Save the original date as a text property and evaluate FORMS."
3740   `(let* ((case-fold-search t)
3741           (start (progn
3742                    (goto-char (point-min))
3743                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3744                               (not (bolp)))
3745                      (match-end 0))))
3746           (date (when (and start
3747                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3748                                               nil t))
3749                   (buffer-substring-no-properties start
3750                                                   (match-beginning 0)))))
3751      (goto-char (point-max))
3752      (skip-chars-backward "\n")
3753      (put-text-property (point-min) (point) 'original-date date)
3754      ,@forms
3755      (goto-char (point-max))
3756      (skip-chars-backward "\n")
3757      (put-text-property (point-min) (point) 'original-date date)))
3758
3759 ;; (defun article-show-all ()
3760 ;;   "Show all hidden text in the article buffer."
3761 ;;   (interactive)
3762 ;;   (save-excursion
3763 ;;     (let ((inhibit-read-only t))
3764 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3765
3766 (defun article-remove-leading-whitespace ()
3767   "Remove excessive whitespace from all headers."
3768   (interactive)
3769   (save-excursion
3770     (save-restriction
3771       (let ((inhibit-read-only t))
3772         (article-narrow-to-head)
3773         (goto-char (point-min))
3774         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3775           (delete-region (match-beginning 1) (match-end 1)))))))
3776
3777 (defun article-emphasize (&optional arg)
3778   "Emphasize text according to `gnus-emphasis-alist'."
3779   (interactive (gnus-article-hidden-arg))
3780   (unless (gnus-article-check-hidden-text 'emphasis arg)
3781     (save-excursion
3782       (let ((alist (or
3783                     (condition-case nil
3784                         (with-current-buffer gnus-summary-buffer
3785                           gnus-article-emphasis-alist)
3786                       (error))
3787                     gnus-emphasis-alist))
3788             (inhibit-read-only t)
3789             (props (append '(article-type emphasis)
3790                            gnus-hidden-properties))
3791             regexp elem beg invisible visible face)
3792         (article-goto-body)
3793         (setq beg (point))
3794         (while (setq elem (pop alist))
3795           (goto-char beg)
3796           (setq regexp (car elem)
3797                 invisible (nth 1 elem)
3798                 visible (nth 2 elem)
3799                 face (nth 3 elem))
3800           (while (re-search-forward regexp nil t)
3801             (when (and (match-beginning visible) (match-beginning invisible))
3802               (gnus-article-hide-text
3803                (match-beginning invisible) (match-end invisible) props)
3804               (gnus-article-unhide-text-type
3805                (match-beginning visible) (match-end visible) 'emphasis)
3806               (gnus-put-overlay-excluding-newlines
3807                (match-beginning visible) (match-end visible) 'face face)
3808               (gnus-add-wash-type 'emphasis)
3809               (goto-char (match-end invisible)))))))))
3810
3811 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3812   "Setup newsgroup emphasis alist."
3813   (unless gnus-article-emphasis-alist
3814     (let ((name (and gnus-newsgroup-name
3815                      (gnus-group-real-name gnus-newsgroup-name))))
3816       (make-local-variable 'gnus-article-emphasis-alist)
3817       (setq gnus-article-emphasis-alist
3818             (nconc
3819              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3820                (while (setq elem (pop alist))
3821                  (when (and name (string-match (car elem) name))
3822                    (setq alist nil
3823                          highlight (copy-sequence (cdr elem)))))
3824                highlight)
3825              (copy-sequence highlight-words)
3826              (if gnus-newsgroup-name
3827                  (copy-sequence (gnus-group-find-parameter
3828                                  gnus-newsgroup-name 'highlight-words t)))
3829              gnus-emphasis-alist)))))
3830
3831 (defvar gnus-summary-article-menu)
3832 (defvar gnus-summary-post-menu)
3833
3834 ;;; Saving functions.
3835
3836 (defun gnus-article-save (save-buffer file &optional num)
3837   "Save the currently selected article."
3838   (when (or (get gnus-default-article-saver :headers)
3839             (not gnus-save-all-headers))
3840     ;; Remove headers according to `gnus-saved-headers' or the value
3841     ;; of the `:headers' property that the saver function might have.
3842     (let ((gnus-visible-headers
3843            (or (symbol-value (get gnus-default-article-saver :headers))
3844                gnus-saved-headers gnus-visible-headers))
3845           ;; Ignore group parameter.  See `article-hide-headers'.
3846           (gnus-summary-buffer nil))
3847       (with-current-buffer save-buffer
3848         (article-hide-headers 1 t))))
3849   (save-window-excursion
3850     (if (not gnus-default-article-saver)
3851         (error "No default saver is defined")
3852       ;; !!! Magic!  The saving functions all save
3853       ;; `gnus-save-article-buffer' (or so they think), but we
3854       ;; bind that variable to our save-buffer.
3855       (set-buffer gnus-article-buffer)
3856       (let* ((gnus-save-article-buffer save-buffer)
3857              (filename
3858               (cond
3859                ((not gnus-prompt-before-saving) 'default)
3860                ((eq gnus-prompt-before-saving 'always) nil)
3861                (t file)))
3862              (gnus-number-of-articles-to-be-saved
3863               (when (eq gnus-prompt-before-saving t)
3864                 num)))                  ; Magic
3865         (set-buffer gnus-article-current-summary)
3866         (funcall gnus-default-article-saver filename)))))
3867
3868 (defun gnus-read-save-file-name (prompt &optional filename
3869                                         function group headers variable
3870                                         dir-var)
3871   (let ((default-name
3872           (funcall function group headers (symbol-value variable)))
3873         result)
3874     (setq result
3875           (expand-file-name
3876            (cond
3877             ((eq filename 'default)
3878              default-name)
3879             ((eq filename t)
3880              default-name)
3881             (filename filename)
3882             (t
3883              (when (symbol-value dir-var)
3884                (setq default-name (expand-file-name
3885                                    (file-name-nondirectory default-name)
3886                                    (symbol-value dir-var))))
3887              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3888                     (prompt
3889                      (format prompt
3890                              (if (and gnus-number-of-articles-to-be-saved
3891                                       (> gnus-number-of-articles-to-be-saved 1))
3892                                  (format "these %d articles"
3893                                          gnus-number-of-articles-to-be-saved)
3894                                "this article")))
3895                     (file
3896                      ;; Let the split methods have their say.
3897                      (cond
3898                       ;; No split name was found.
3899                       ((null split-name)
3900                        (read-file-name
3901                         (concat prompt " (default "
3902                                 (file-name-nondirectory default-name) "): ")
3903                         (file-name-directory default-name)
3904                         default-name))
3905                       ;; A single group name is returned.
3906                       ((stringp split-name)
3907                        (setq default-name
3908                              (funcall function split-name headers
3909                                       (symbol-value variable)))
3910                        (read-file-name
3911                         (concat prompt " (default "
3912                                 (file-name-nondirectory default-name) "): ")
3913                         (file-name-directory default-name)
3914                         default-name))
3915                       ;; A single split name was found
3916                       ((= 1 (length split-name))
3917                        (let* ((name (expand-file-name
3918                                      (car split-name)
3919                                      gnus-article-save-directory))
3920                               (dir (cond ((file-directory-p name)
3921                                           (file-name-as-directory name))
3922                                          ((file-exists-p name) name)
3923                                          (t gnus-article-save-directory))))
3924                          (read-file-name
3925                           (concat prompt " (default " name "): ")
3926                           dir name)))
3927                       ;; A list of splits was found.
3928                       (t
3929                        (setq split-name (nreverse split-name))
3930                        (let (result)
3931                          (let ((file-name-history
3932                                 (nconc split-name file-name-history)))
3933                            (setq result
3934                                  (expand-file-name
3935                                   (read-file-name
3936                                    (concat prompt " (`M-p' for defaults): ")
3937                                    gnus-article-save-directory
3938                                    (car split-name))
3939                                   gnus-article-save-directory)))
3940                          (car (push result file-name-history)))))))
3941                ;; Create the directory.
3942                (gnus-make-directory (file-name-directory file))
3943                ;; If we have read a directory, we append the default file name.
3944                (when (file-directory-p file)
3945                  (setq file (expand-file-name (file-name-nondirectory
3946                                                default-name)
3947                                               (file-name-as-directory file))))
3948                ;; Possibly translate some characters.
3949                (nnheader-translate-file-chars file))))))
3950     (gnus-make-directory (file-name-directory result))
3951     (when variable
3952       (set variable result))
3953     (when dir-var
3954       (set dir-var (file-name-directory result)))
3955     result))
3956
3957 (defun gnus-article-archive-name (group)
3958   "Return the first instance of an \"Archive-name\" in the current buffer."
3959   (let ((case-fold-search t))
3960     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3961       (nnheader-concat gnus-article-save-directory
3962                        (match-string 1)))))
3963
3964 (defun gnus-article-nndoc-name (group)
3965   "If GROUP is an nndoc group, return the name of the parent group."
3966   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3967     (gnus-group-get-parameter group 'save-article-group)))
3968
3969 (defun gnus-summary-save-in-rmail (&optional filename)
3970   "Append this article to Rmail file.
3971 Optional argument FILENAME specifies file name.
3972 Directory to save to is default to `gnus-article-save-directory'."
3973   (setq filename (gnus-read-save-file-name
3974                   "Save %s in rmail file" filename
3975                   gnus-rmail-save-name gnus-newsgroup-name
3976                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3977   (gnus-eval-in-buffer-window gnus-save-article-buffer
3978     (save-excursion
3979       (save-restriction
3980         (widen)
3981         ;; Note that unlike gnus-summary-save-in-mail, there is no
3982         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3983         ;; not use Babyl.
3984         (gnus-output-to-rmail filename))))
3985   filename)
3986
3987 (defun gnus-summary-save-in-mail (&optional filename)
3988   "Append this article to Unix mail file.
3989 Optional argument FILENAME specifies file name.
3990 Directory to save to is default to `gnus-article-save-directory'."
3991   (setq filename (gnus-read-save-file-name
3992                   "Save %s in Unix mail file" filename
3993                   gnus-mail-save-name gnus-newsgroup-name
3994                   gnus-current-headers 'gnus-newsgroup-last-mail))
3995   (gnus-eval-in-buffer-window gnus-save-article-buffer
3996     (save-excursion
3997       (save-restriction
3998         (widen)
3999         (if (and (file-readable-p filename)
4000                  (file-regular-p filename)
4001                  (mail-file-babyl-p filename))
4002             (gnus-output-to-rmail filename)
4003           (gnus-output-to-mail filename)))))
4004   filename)
4005
4006 (put 'gnus-summary-save-in-file :decode t)
4007 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4008 (defun gnus-summary-save-in-file (&optional filename overwrite)
4009   "Append this article to file.
4010 Optional argument FILENAME specifies file name.
4011 Directory to save to is default to `gnus-article-save-directory'."
4012   (setq filename (gnus-read-save-file-name
4013                   "Save %s in file" filename
4014                   gnus-file-save-name gnus-newsgroup-name
4015                   gnus-current-headers 'gnus-newsgroup-last-file))
4016   (gnus-eval-in-buffer-window gnus-save-article-buffer
4017     (save-excursion
4018       (save-restriction
4019         (widen)
4020         (when (and overwrite
4021                    (file-exists-p filename))
4022           (delete-file filename))
4023         (gnus-output-to-file filename))))
4024   filename)
4025
4026 (put 'gnus-summary-write-to-file :decode t)
4027 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4028 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4029 (defun gnus-summary-write-to-file (&optional filename)
4030   "Write this article to a file, overwriting it if the file exists.
4031 Optional argument FILENAME specifies file name.
4032 The directory to save in defaults to `gnus-article-save-directory'."
4033   (setq filename (gnus-read-save-file-name
4034                   "Save %s in file" filename
4035                   gnus-file-save-name gnus-newsgroup-name
4036                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4037   (gnus-summary-save-in-file filename t))
4038
4039 (put 'gnus-summary-save-body-in-file :decode t)
4040 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4041   "Append this article body to a file.
4042 Optional argument FILENAME specifies file name.
4043 The directory to save in defaults to `gnus-article-save-directory'."
4044   (setq filename (gnus-read-save-file-name
4045                   "Save %s body in file" filename
4046                   gnus-file-save-name gnus-newsgroup-name
4047                   gnus-current-headers 'gnus-newsgroup-last-file))
4048   (gnus-eval-in-buffer-window gnus-save-article-buffer
4049     (save-excursion
4050       (save-restriction
4051         (widen)
4052         (when (article-goto-body)
4053           (narrow-to-region (point) (point-max)))
4054         (when (and overwrite
4055                    (file-exists-p filename))
4056           (delete-file filename))
4057         (gnus-output-to-file filename))))
4058   filename)
4059
4060 (put 'gnus-summary-write-body-to-file :decode t)
4061 (put 'gnus-summary-write-body-to-file
4062      :function 'gnus-summary-save-body-in-file)
4063 (defun gnus-summary-write-body-to-file (&optional filename)
4064   "Write this article body to a file, overwriting it if the file exists.
4065 Optional argument FILENAME specifies file name.
4066 The directory to save in defaults to `gnus-article-save-directory'."
4067   (setq filename (gnus-read-save-file-name
4068                   "Save %s body in file" filename
4069                   gnus-file-save-name gnus-newsgroup-name
4070                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4071   (gnus-summary-save-body-in-file filename t))
4072
4073 (put 'gnus-summary-save-in-pipe :decode t)
4074 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4075 (defun gnus-summary-save-in-pipe (&optional command raw)
4076   "Pipe this article to subprocess COMMAND.
4077 Valid values for COMMAND include:
4078   a string
4079     The executable command name and possibly arguments.
4080   nil
4081     You will be prompted for the command in the minibuffer.
4082   the symbol `default'
4083     It will be replaced with the command which the variable
4084     `gnus-summary-pipe-output-default-command' holds or the command
4085     last used for saving.
4086 Non-nil value for RAW overrides `:decode' and `:headers' properties
4087 and the raw article including all headers will be piped."
4088   (let ((article (gnus-summary-article-number))
4089         (decode (unless raw
4090                   (get 'gnus-summary-save-in-pipe :decode)))
4091         save-buffer default)
4092     (if article
4093         (if (vectorp (gnus-summary-article-header article))
4094             (save-current-buffer
4095               (gnus-summary-select-article decode decode nil article)
4096               (insert-buffer-substring
4097                (prog1
4098                    (if decode
4099                        gnus-article-buffer
4100                      gnus-original-article-buffer)
4101                  (setq save-buffer
4102                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4103               ;; Remove unwanted headers.
4104               (when (and (not raw)
4105                          (or (get 'gnus-summary-save-in-pipe :headers)
4106                              (not gnus-save-all-headers)))
4107                 (let ((gnus-visible-headers
4108                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4109                                               :headers))
4110                            gnus-saved-headers gnus-visible-headers))
4111                       (gnus-summary-buffer nil))
4112                   (article-hide-headers 1 t))))
4113           (error "%d is not a real article" article))
4114       (error "No article to pipe"))
4115     (setq default (or gnus-summary-pipe-output-default-command
4116                       gnus-last-shell-command))
4117     (unless (stringp command)
4118       (setq command
4119             (if (and (eq command 'default) default)
4120                 default
4121               (gnus-read-shell-command "Shell command on this article: "
4122                                        default))))
4123     (when (string-equal command "")
4124       (if default
4125           (setq command default)
4126         (error "A command is required")))
4127     (gnus-eval-in-buffer-window save-buffer
4128       (save-restriction
4129         (widen)
4130         (shell-command-on-region (point-min) (point-max) command nil)))
4131     (gnus-kill-buffer save-buffer))
4132   (setq gnus-summary-pipe-output-default-command command))
4133
4134 (defun gnus-summary-pipe-to-muttprint (&optional command)
4135   "Pipe this article to muttprint."
4136   (unless (stringp command)
4137     (setq command (read-string
4138                    "Print using command: " gnus-summary-muttprint-program
4139                    nil gnus-summary-muttprint-program)))
4140   (let ((gnus-summary-pipe-output-default-command
4141          gnus-summary-pipe-output-default-command))
4142     (gnus-summary-save-in-pipe command))
4143   (setq gnus-summary-muttprint-program command))
4144
4145 ;;; Article file names when saving.
4146
4147 (defun gnus-capitalize-newsgroup (newsgroup)
4148   "Capitalize NEWSGROUP name."
4149   (when (not (zerop (length newsgroup)))
4150     (concat (char-to-string (upcase (aref newsgroup 0)))
4151             (substring newsgroup 1))))
4152
4153 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4154   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4155 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4156 Otherwise, it is like ~/News/news/group/num."
4157   (let ((default
4158           (expand-file-name
4159            (concat (if (gnus-use-long-file-name 'not-save)
4160                        (gnus-capitalize-newsgroup newsgroup)
4161                      (gnus-newsgroup-directory-form newsgroup))
4162                    "/" (int-to-string (mail-header-number headers)))
4163            gnus-article-save-directory)))
4164     (if (and last-file
4165              (string-equal (file-name-directory default)
4166                            (file-name-directory last-file))
4167              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4168         default
4169       (or last-file default))))
4170
4171 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4172   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4173 If variable `gnus-use-long-file-name' is non-nil, it is
4174 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4175   (let ((default
4176           (expand-file-name
4177            (concat (if (gnus-use-long-file-name 'not-save)
4178                        newsgroup
4179                      (gnus-newsgroup-directory-form newsgroup))
4180                    "/" (int-to-string (mail-header-number headers)))
4181            gnus-article-save-directory)))
4182     (if (and last-file
4183              (string-equal (file-name-directory default)
4184                            (file-name-directory last-file))
4185              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4186         default
4187       (or last-file default))))
4188
4189 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4190   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4191 If variable `gnus-use-long-file-name' is non-nil, it is
4192 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4193   (or last-file
4194       (expand-file-name
4195        (if (gnus-use-long-file-name 'not-save)
4196            newsgroup
4197          (file-relative-name
4198           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4199           default-directory))
4200        gnus-article-save-directory)))
4201
4202 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4203   "Generate file name from sender."
4204   (let ((from (mail-header-from headers)))
4205     (expand-file-name
4206      (if (and from (string-match "\\([^ <]+\\)@" from))
4207          (match-string 1 from)
4208        "nobody")
4209      gnus-article-save-directory)))
4210
4211 (defun article-verify-x-pgp-sig ()
4212   "Verify X-PGP-Sig."
4213   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4214   (interactive)
4215   (if (gnus-buffer-live-p gnus-original-article-buffer)
4216       (let ((sig (with-current-buffer gnus-original-article-buffer
4217                    (gnus-fetch-field "X-PGP-Sig")))
4218             items info headers)
4219         (when (and sig
4220                    mml2015-use
4221                    (mml2015-clear-verify-function))
4222           (with-temp-buffer
4223             (insert-buffer-substring gnus-original-article-buffer)
4224             (setq items (split-string sig))
4225             (message-narrow-to-head)
4226             (let ((inhibit-point-motion-hooks t)
4227                   (case-fold-search t))
4228               ;; Don't verify multiple headers.
4229               (setq headers (mapconcat (lambda (header)
4230                                          (concat header ": "
4231                                                  (mail-fetch-field header)
4232                                                  "\n"))
4233                                        (split-string (nth 1 items) ",") "")))
4234             (delete-region (point-min) (point-max))
4235             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4236             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4237             (insert headers)
4238             (widen)
4239             (forward-line)
4240             (while (not (eobp))
4241               (if (looking-at "^-")
4242                   (insert "- "))
4243               (forward-line))
4244             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4245             (insert "Version: " (car items) "\n\n")
4246             (insert (mapconcat 'identity (cddr items) "\n"))
4247             (insert "\n-----END PGP SIGNATURE-----\n")
4248             (let ((mm-security-handle (list (format "multipart/signed"))))
4249               (mml2015-clean-buffer)
4250               (let ((coding-system-for-write (or gnus-newsgroup-charset
4251                                                  'iso-8859-1)))
4252                 (funcall (mml2015-clear-verify-function)))
4253               (setq info
4254                     (or (mm-handle-multipart-ctl-parameter
4255                          mm-security-handle 'gnus-details)
4256                         (mm-handle-multipart-ctl-parameter
4257                          mm-security-handle 'gnus-info)))))
4258           (when info
4259             (let ((inhibit-read-only t) bface eface)
4260               (save-restriction
4261                 (message-narrow-to-head)
4262                 (goto-char (point-max))
4263                 (forward-line -1)
4264                 (setq bface (get-text-property (point-at-bol) 'face)
4265                       eface (get-text-property (1- (point-at-eol)) 'face))
4266                 (message-remove-header "X-Gnus-PGP-Verify")
4267                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4268                     (forward-line)
4269                   (goto-char (point-max)))
4270                 (narrow-to-region (point) (point))
4271                 (insert "X-Gnus-PGP-Verify: " info "\n")
4272                 (goto-char (point-min))
4273                 (forward-line)
4274                 (while (not (eobp))
4275                   (if (not (looking-at "^[ \t]"))
4276                       (insert " "))
4277                   (forward-line))
4278                 ;; Do highlighting.
4279                 (goto-char (point-min))
4280                 (when (looking-at "\\([^:]+\\): *")
4281                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4282                                      'face bface)
4283                   (put-text-property (match-end 0) (point-max)
4284                                      'face eface)))))))))
4285
4286 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4287
4288 (defun article-verify-cancel-lock ()
4289   "Verify Cancel-Lock header."
4290   (interactive)
4291   (if (gnus-buffer-live-p gnus-original-article-buffer)
4292       (canlock-verify gnus-original-article-buffer)))
4293
4294 (eval-and-compile
4295   (mapc
4296    (lambda (func)
4297      (let (afunc gfunc)
4298        (if (consp func)
4299            (setq afunc (car func)
4300                  gfunc (cdr func))
4301          (setq afunc func
4302                gfunc (intern (format "gnus-%s" func))))
4303        (defalias gfunc
4304          (when (fboundp afunc)
4305            `(lambda (&optional interactive &rest args)
4306               ,(documentation afunc t)
4307               (interactive (list t))
4308               (with-current-buffer gnus-article-buffer
4309                 (if interactive
4310                     (call-interactively ',afunc)
4311                   (apply ',afunc args))))))))
4312    '(article-hide-headers
4313      article-verify-x-pgp-sig
4314      article-verify-cancel-lock
4315      article-hide-boring-headers
4316      article-treat-overstrike
4317      article-treat-ansi-sequences
4318      article-fill-long-lines
4319      article-capitalize-sentences
4320      article-remove-cr
4321      article-remove-leading-whitespace
4322      article-display-x-face
4323      article-display-face
4324      article-de-quoted-unreadable
4325      article-de-base64-unreadable
4326      article-decode-HZ
4327      article-wash-html
4328      article-unsplit-urls
4329      article-hide-list-identifiers
4330      article-strip-banner
4331      article-babel
4332      article-hide-pem
4333      article-hide-signature
4334      article-strip-headers-in-body
4335      article-remove-trailing-blank-lines
4336      article-strip-leading-blank-lines
4337      article-strip-multiple-blank-lines
4338      article-strip-leading-space
4339      article-strip-trailing-space
4340      article-strip-blank-lines
4341      article-strip-all-blank-lines
4342      article-date-local
4343      article-date-english
4344      article-date-iso8601
4345      article-date-original
4346      article-date-ut
4347      article-decode-mime-words
4348      article-decode-charset
4349      article-decode-encoded-words
4350      article-date-user
4351      article-date-lapsed
4352      article-emphasize
4353      article-treat-dumbquotes
4354      article-normalize-headers
4355      ;;(article-show-all . gnus-article-show-all-headers)
4356      )))
4357 \f
4358 ;;;
4359 ;;; Gnus article mode
4360 ;;;
4361
4362 (put 'gnus-article-mode 'mode-class 'special)
4363
4364 (set-keymap-parent gnus-article-mode-map widget-keymap)
4365
4366 (gnus-define-keys gnus-article-mode-map
4367   " " gnus-article-goto-next-page
4368   "\177" gnus-article-goto-prev-page
4369   [delete] gnus-article-goto-prev-page
4370   [backspace] gnus-article-goto-prev-page
4371   "\C-c^" gnus-article-refer-article
4372   "h" gnus-article-show-summary
4373   "s" gnus-article-show-summary
4374   "\C-c\C-m" gnus-article-mail
4375   "?" gnus-article-describe-briefly
4376   "<" beginning-of-buffer
4377   ">" end-of-buffer
4378   "\C-c\C-i" gnus-info-find-node
4379   "\C-c\C-b" gnus-bug
4380   "R" gnus-article-reply-with-original
4381   "F" gnus-article-followup-with-original
4382   "\C-hk" gnus-article-describe-key
4383   "\C-hc" gnus-article-describe-key-briefly
4384   "\C-hb" gnus-article-describe-bindings
4385
4386   "e" gnus-article-read-summary-keys
4387   "\C-d" gnus-article-read-summary-keys
4388   "\M-*" gnus-article-read-summary-keys
4389   "\M-#" gnus-article-read-summary-keys
4390   "\M-^" gnus-article-read-summary-keys
4391   "\M-g" gnus-article-read-summary-keys)
4392
4393 (substitute-key-definition
4394  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4395
4396 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4397   "W" gnus-article-wide-reply-with-original)
4398 (if (featurep 'xemacs)
4399     (set-keymap-default-binding gnus-article-send-map
4400                                 'gnus-article-read-summary-send-keys)
4401   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4402
4403 (defun gnus-article-make-menu-bar ()
4404   (unless (boundp 'gnus-article-commands-menu)
4405     (gnus-summary-make-menu-bar))
4406   (gnus-turn-off-edit-menu 'article)
4407   (unless (boundp 'gnus-article-article-menu)
4408     (easy-menu-define
4409      gnus-article-article-menu gnus-article-mode-map ""
4410      '("Article"
4411        ["Scroll forwards" gnus-article-goto-next-page t]
4412        ["Scroll backwards" gnus-article-goto-prev-page t]
4413        ["Show summary" gnus-article-show-summary t]
4414        ["Fetch Message-ID at point" gnus-article-refer-article t]
4415        ["Mail to address at point" gnus-article-mail t]
4416        ["Send a bug report" gnus-bug t]))
4417
4418     (easy-menu-define
4419      gnus-article-treatment-menu gnus-article-mode-map ""
4420      ;; Fixme: this should use :active (and maybe :visible).
4421      '("Treatment"
4422        ["Hide headers" gnus-article-hide-headers t]
4423        ["Hide signature" gnus-article-hide-signature t]
4424        ["Hide citation" gnus-article-hide-citation t]
4425        ["Treat overstrike" gnus-article-treat-overstrike t]
4426        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4427        ["Remove carriage return" gnus-article-remove-cr t]
4428        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4429        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4430        ["Remove base64" gnus-article-de-base64-unreadable t]
4431        ["Treat html" gnus-article-wash-html t]
4432        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4433        ["Decode HZ" gnus-article-decode-HZ t]))
4434
4435     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4436
4437     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4438
4439     (gnus-run-hooks 'gnus-article-menu-hook)))
4440
4441 (defvar bookmark-make-record-function)
4442
4443 (defun gnus-article-mode ()
4444   "Major mode for displaying an article.
4445
4446 All normal editing commands are switched off.
4447
4448 The following commands are available in addition to all summary mode
4449 commands:
4450 \\<gnus-article-mode-map>
4451 \\[gnus-article-next-page]\t Scroll the article one page forwards
4452 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4453 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4454 \\[gnus-article-show-summary]\t Display the summary buffer
4455 \\[gnus-article-mail]\t Send a reply to the address near point
4456 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4457 \\[gnus-info-find-node]\t Go to the Gnus info node"
4458   (interactive)
4459   (kill-all-local-variables)
4460   (gnus-simplify-mode-line)
4461   (setq mode-name "Article")
4462   (setq major-mode 'gnus-article-mode)
4463   (make-local-variable 'minor-mode-alist)
4464   (use-local-map gnus-article-mode-map)
4465   (when (gnus-visual-p 'article-menu 'menu)
4466     (gnus-article-make-menu-bar)
4467     (when gnus-summary-tool-bar-map
4468       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4469   (gnus-update-format-specifications nil 'article-mode)
4470   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4471   (set (make-local-variable 'gnus-page-broken) nil)
4472   (make-local-variable 'gnus-button-marker-list)
4473   (make-local-variable 'gnus-article-current-summary)
4474   (make-local-variable 'gnus-article-mime-handles)
4475   (make-local-variable 'gnus-article-decoded-p)
4476   (make-local-variable 'gnus-article-mime-handle-alist)
4477   (make-local-variable 'gnus-article-wash-types)
4478   (make-local-variable 'gnus-article-image-alist)
4479   (make-local-variable 'gnus-article-charset)
4480   (make-local-variable 'gnus-article-ignored-charsets)
4481   (set (make-local-variable 'bookmark-make-record-function)
4482        'gnus-summary-bookmark-make-record)
4483   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4484   ;; face.
4485   (set (make-local-variable 'nobreak-char-display) nil)
4486   (setq cursor-in-non-selected-windows nil)
4487   (setq truncate-lines gnus-article-truncate-lines)
4488   (gnus-set-default-directory)
4489   (buffer-disable-undo)
4490   (setq buffer-read-only t
4491         show-trailing-whitespace nil)
4492   (set-syntax-table gnus-article-mode-syntax-table)
4493   (mm-enable-multibyte)
4494   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4495
4496 (defvar gnus-button-marker-list nil
4497   "Regexp matching any of the regexps from `gnus-button-alist'.
4498 Internal variable.")
4499
4500 (defun gnus-article-setup-buffer ()
4501   "Initialize the article buffer."
4502   (let* ((name (if gnus-single-article-buffer "*Article*"
4503                  (concat "*Article " gnus-newsgroup-name "*")))
4504          (original
4505           (progn (string-match "\\*Article" name)
4506                  (concat " *Original Article"
4507                          (substring name (match-end 0))))))
4508     (setq gnus-article-buffer name)
4509     (setq gnus-original-article-buffer original)
4510     (setq gnus-article-mime-handle-alist nil)
4511     (with-current-buffer gnus-summary-buffer
4512       ;; This might be a variable local to the summary buffer.
4513       (unless gnus-single-article-buffer
4514         (setq gnus-article-buffer name)
4515         (setq gnus-original-article-buffer original)
4516         (gnus-set-global-variables)))
4517     (gnus-article-setup-highlight-words)
4518     ;; Init original article buffer.
4519     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4520       (mm-enable-multibyte)
4521       (setq major-mode 'gnus-original-article-mode)
4522       (make-local-variable 'gnus-original-article))
4523     (if (and (get-buffer name)
4524              (with-current-buffer name
4525                (if gnus-article-edit-mode
4526                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4527                        (progn
4528                          (set-buffer-modified-p nil)
4529                          (gnus-kill-buffer name)
4530                          (message "")
4531                          nil)
4532                      (error "Action aborted"))
4533                  t)))
4534         (with-current-buffer name
4535           (set (make-local-variable 'gnus-article-edit-mode) nil)
4536           (when gnus-article-mime-handles
4537             (mm-destroy-parts gnus-article-mime-handles)
4538             (setq gnus-article-mime-handles nil))
4539           ;; Set it to nil in article-buffer!
4540           (setq gnus-article-mime-handle-alist nil)
4541           (buffer-disable-undo)
4542           (setq buffer-read-only t)
4543           ;; This list just keeps growing if we don't reset it.
4544           (setq gnus-button-marker-list nil)
4545           (unless (eq major-mode 'gnus-article-mode)
4546             (gnus-article-mode))
4547           (current-buffer))
4548       (with-current-buffer (gnus-get-buffer-create name)
4549         (gnus-article-mode)
4550         (make-local-variable 'gnus-summary-buffer)
4551         (setq gnus-summary-buffer
4552               (gnus-summary-buffer-name gnus-newsgroup-name))
4553         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4554         (current-buffer)))))
4555
4556 ;; Set article window start at LINE, where LINE is the number of lines
4557 ;; from the head of the article.
4558 (defun gnus-article-set-window-start (&optional line)
4559   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4560     (when article-window
4561       (set-window-start
4562        article-window
4563        (with-current-buffer gnus-article-buffer
4564          (goto-char (point-min))
4565          (if (not line)
4566              (point-min)
4567            (gnus-message 6 "Moved to bookmark")
4568            (search-forward "\n\n" nil t)
4569            (forward-line line)
4570            (point)))))))
4571
4572 (defun gnus-article-prepare (article &optional all-headers header)
4573   "Prepare ARTICLE in article mode buffer.
4574 ARTICLE should either be an article number or a Message-ID.
4575 If ARTICLE is an id, HEADER should be the article headers.
4576 If ALL-HEADERS is non-nil, no headers are hidden."
4577   (save-excursion
4578     ;; Make sure we start in a summary buffer.
4579     (unless (eq major-mode 'gnus-summary-mode)
4580       (set-buffer gnus-summary-buffer))
4581     (setq gnus-summary-buffer (current-buffer))
4582     (let* ((gnus-article (if header (mail-header-number header) article))
4583            (summary-buffer (current-buffer))
4584            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4585            (group gnus-newsgroup-name)
4586            result)
4587       (save-excursion
4588         (gnus-article-setup-buffer)
4589         (set-buffer gnus-article-buffer)
4590         ;; Deactivate active regions.
4591         (when (and (boundp 'transient-mark-mode)
4592                    transient-mark-mode)
4593           (setq mark-active nil))
4594         (if (not (setq result (let ((inhibit-read-only t))
4595                                 (gnus-request-article-this-buffer
4596                                  article group))))
4597             ;; There is no such article.
4598             (save-excursion
4599               (when (and (numberp article)
4600                          (not (memq article gnus-newsgroup-sparse)))
4601                 (setq gnus-article-current
4602                       (cons gnus-newsgroup-name article))
4603                 (set-buffer gnus-summary-buffer)
4604                 (setq gnus-current-article article)
4605                 (if (and (memq article gnus-newsgroup-undownloaded)
4606                          (not (gnus-online (gnus-find-method-for-group
4607                                             gnus-newsgroup-name))))
4608                     (progn
4609                       (gnus-summary-set-agent-mark article)
4610                       (message "Message marked for downloading"))
4611                   (gnus-summary-mark-article article gnus-canceled-mark)
4612                   (unless (memq article gnus-newsgroup-sparse)
4613                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4614           (if (or (eq result 'pseudo)
4615                   (eq result 'nneething))
4616               (progn
4617                 (with-current-buffer summary-buffer
4618                   (push article gnus-newsgroup-history)
4619                   (setq gnus-last-article gnus-current-article
4620                         gnus-current-article 0
4621                         gnus-current-headers nil
4622                         gnus-article-current nil)
4623                   (if (eq result 'nneething)
4624                       (gnus-configure-windows 'summary)
4625                     (gnus-configure-windows 'article))
4626                   (gnus-set-global-variables))
4627                 (let ((gnus-article-mime-handle-alist-1
4628                        gnus-article-mime-handle-alist))
4629                   (gnus-set-mode-line 'article)))
4630             ;; The result from the `request' was an actual article -
4631             ;; or at least some text that is now displayed in the
4632             ;; article buffer.
4633             (when (and (numberp article)
4634                        (not (eq article gnus-current-article)))
4635               ;; Seems like a new article has been selected.
4636               ;; `gnus-current-article' must be an article number.
4637               (with-current-buffer summary-buffer
4638                 (push article gnus-newsgroup-history)
4639                 (setq gnus-last-article gnus-current-article
4640                       gnus-current-article article
4641                       gnus-current-headers
4642                       (gnus-summary-article-header gnus-current-article)
4643                       gnus-article-current
4644                       (cons gnus-newsgroup-name gnus-current-article))
4645                 (unless (vectorp gnus-current-headers)
4646                   (setq gnus-current-headers nil))
4647                 (gnus-summary-goto-subject gnus-current-article)
4648                 (when (gnus-summary-show-thread)
4649                   ;; If the summary buffer really was folded, the
4650                   ;; previous goto may not actually have gone to
4651                   ;; the right article, but the thread root instead.
4652                   ;; So we go again.
4653                   (gnus-summary-goto-subject gnus-current-article))
4654                 (gnus-run-hooks 'gnus-mark-article-hook)
4655                 (gnus-set-mode-line 'summary)
4656                 (when (gnus-visual-p 'article-highlight 'highlight)
4657                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4658                 ;; Set the global newsgroup variables here.
4659                 (gnus-set-global-variables)
4660                 (setq gnus-have-all-headers
4661                       (or all-headers gnus-show-all-headers))))
4662             (save-excursion
4663               (gnus-configure-windows 'article))
4664             (when (or (numberp article)
4665                       (stringp article))
4666               (gnus-article-prepare-display)
4667               ;; Do page break.
4668               (goto-char (point-min))
4669               (when gnus-break-pages
4670                 (gnus-narrow-to-page)))
4671             (let ((gnus-article-mime-handle-alist-1
4672                    gnus-article-mime-handle-alist))
4673               (gnus-set-mode-line 'article))
4674             (article-goto-body)
4675             (unless (bobp)
4676               (forward-line -1))
4677             (set-window-point (get-buffer-window (current-buffer)) (point))
4678             (gnus-configure-windows 'article)
4679             t))))))
4680
4681 ;;;###autoload
4682 (defun gnus-article-prepare-display ()
4683   "Make the current buffer look like a nice article."
4684   ;; Hooks for getting information from the article.
4685   ;; This hook must be called before being narrowed.
4686   (let ((gnus-article-buffer (current-buffer))
4687         buffer-read-only
4688         (inhibit-read-only t))
4689     (unless (eq major-mode 'gnus-article-mode)
4690       (gnus-article-mode))
4691     (setq buffer-read-only nil
4692           gnus-article-wash-types nil
4693           gnus-article-image-alist nil)
4694     (gnus-run-hooks 'gnus-tmp-internal-hook)
4695     (when gnus-display-mime-function
4696       (funcall gnus-display-mime-function))
4697     (gnus-run-hooks 'gnus-article-prepare-hook)))
4698
4699 ;;;
4700 ;;; Gnus Sticky Article Mode
4701 ;;;
4702
4703 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4704   "Mode for sticky articles."
4705   ;; Release bindings that won't work.
4706   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4707                              gnus-sticky-article-mode-map)
4708   (substitute-key-definition 'gnus-article-refer-article 'undefined
4709                              gnus-sticky-article-mode-map)
4710   (dolist (k '("e" "h" "s" "F" "R"))
4711     (define-key gnus-sticky-article-mode-map k nil))
4712   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4713   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4714   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4715   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4716
4717 (defun gnus-sticky-article (arg)
4718   "Make the current article sticky.
4719 If a prefix ARG is given, ask for a name for this sticky article buffer."
4720   (interactive "P")
4721   (gnus-summary-show-thread)
4722   (gnus-summary-select-article nil nil 'pseudo)
4723   (let (new-art-buf-name)
4724     (gnus-eval-in-buffer-window gnus-article-buffer
4725       (setq new-art-buf-name
4726             (concat
4727              "*Sticky Article: "
4728              (if arg
4729                  (read-from-minibuffer "Sticky article buffer name: ")
4730                (gnus-with-article-headers
4731                  (gnus-article-goto-header "subject")
4732                  (setq new-art-buf-name
4733                        (buffer-substring-no-properties
4734                         (line-beginning-position) (line-end-position)))
4735                  (goto-char (point-min))
4736                  (gnus-article-goto-header "from")
4737                  (setq new-art-buf-name
4738                        (concat
4739                         new-art-buf-name ", "
4740                         (buffer-substring-no-properties
4741                          (line-beginning-position) (line-end-position))))
4742                  (goto-char (point-min))
4743                  (gnus-article-goto-header "date")
4744                  (setq new-art-buf-name
4745                        (concat
4746                         new-art-buf-name ", "
4747                         (buffer-substring-no-properties
4748                          (line-beginning-position) (line-end-position))))))
4749              "*"))
4750       (if (and (gnus-buffer-live-p new-art-buf-name)
4751                (with-current-buffer new-art-buf-name
4752                  (eq major-mode 'gnus-sticky-article-mode)))
4753           (switch-to-buffer new-art-buf-name)
4754         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4755       (gnus-sticky-article-mode))
4756     (setq gnus-article-buffer new-art-buf-name))
4757   (gnus-summary-recenter)
4758   (gnus-summary-position-point))
4759
4760 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4761   "Kill the given sticky article BUFFER.
4762 If none is given, assume the current buffer and kill it if it has
4763 `gnus-sticky-article-mode'."
4764   (interactive)
4765   (unless buffer
4766     (setq buffer (current-buffer)))
4767   (with-current-buffer buffer
4768     (when (eq major-mode 'gnus-sticky-article-mode)
4769       (gnus-kill-buffer buffer))))
4770
4771 (defun gnus-kill-sticky-article-buffers (arg)
4772   "Kill all sticky article buffers.
4773 If a prefix ARG is given, ask for confirmation."
4774   (interactive "P")
4775   (dolist (buf (gnus-buffers))
4776     (with-current-buffer buf
4777       (when (eq major-mode 'gnus-sticky-article-mode)
4778         (if (not arg)
4779             (gnus-kill-buffer buf)
4780           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4781             (gnus-kill-buffer buf)))))))
4782
4783 ;;;
4784 ;;; Gnus MIME viewing functions
4785 ;;;
4786
4787 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4788   "Format of the MIME buttons.
4789
4790 Valid specifiers include:
4791 %t  The MIME type
4792 %T  MIME type, along with additional info
4793 %n  The `name' parameter
4794 %d  The description, if any
4795 %l  The length of the encoded part
4796 %p  The part identifier number
4797 %e  Dots if the part isn't displayed
4798
4799 General format specifiers can also be used.  See Info node
4800 `(gnus)Formatting Variables'.")
4801
4802 (defvar gnus-mime-button-line-format-alist
4803   '((?t gnus-tmp-type ?s)
4804     (?T gnus-tmp-type-long ?s)
4805     (?n gnus-tmp-name ?s)
4806     (?d gnus-tmp-description ?s)
4807     (?p gnus-tmp-id ?s)
4808     (?l gnus-tmp-length ?d)
4809     (?e gnus-tmp-dots ?s)))
4810
4811 (defvar gnus-mime-button-commands
4812   '((gnus-article-press-button "\r" "Toggle Display")
4813     (gnus-mime-view-part "v" "View Interactively...")
4814     (gnus-mime-view-part-as-type "t" "View As Type...")
4815     (gnus-mime-view-part-as-charset "C" "View As charset...")
4816     (gnus-mime-save-part "o" "Save...")
4817     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4818     (gnus-mime-replace-part "r" "Replace part")
4819     (gnus-mime-delete-part "d" "Delete part")
4820     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4821     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4822     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4823     (gnus-mime-view-part-externally "e" "View Externally")
4824     (gnus-mime-print-part "p" "Print")
4825     (gnus-mime-pipe-part "|" "Pipe To Command...")
4826     (gnus-mime-action-on-part "." "Take action on the part...")))
4827
4828 (defun gnus-article-mime-part-status ()
4829   (if gnus-article-mime-handle-alist-1
4830       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4831           " (1 part)"
4832         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4833     ""))
4834
4835 (defvar gnus-mime-button-map
4836   (let ((map (make-sparse-keymap)))
4837     (define-key map gnus-mouse-2 'gnus-article-push-button)
4838     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4839     (dolist (c gnus-mime-button-commands)
4840       (define-key map (cadr c) (car c)))
4841     map))
4842
4843 (easy-menu-define
4844   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4845   `("MIME Part"
4846     ,@(mapcar (lambda (c)
4847                 (vector (caddr c) (car c) :active t))
4848               gnus-mime-button-commands)))
4849
4850 (defvar gnus-url-button-commands
4851   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4852
4853 (defvar gnus-url-button-map
4854   (let ((map (make-sparse-keymap)))
4855     (dolist (c gnus-url-button-commands)
4856       (define-key map (cadr c) (car c)))
4857     map))
4858
4859 (easy-menu-define
4860   gnus-url-button-menu gnus-url-button-map "URL button menu."
4861   `("Url Button"
4862     ,@(mapcar (lambda (c)
4863                 (vector (caddr c) (car c) :active t))
4864               gnus-url-button-commands)))
4865
4866 (defmacro gnus-bind-safe-url-regexp (&rest body)
4867   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4868   `(let ((mm-w3m-safe-url-regexp
4869           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4870                                 (gnus-buffer-live-p
4871                                  gnus-article-current-summary))
4872                            (with-current-buffer gnus-article-current-summary
4873                              gnus-newsgroup-name)
4874                          gnus-newsgroup-name)))
4875             (if (cond ((not group)
4876                        ;; Maybe we're in a mml-preview buffer
4877                        ;; and no group is selected.
4878                        t)
4879                       ((stringp gnus-safe-html-newsgroups)
4880                        (string-match gnus-safe-html-newsgroups group))
4881                       ((consp gnus-safe-html-newsgroups)
4882                        (member group gnus-safe-html-newsgroups)))
4883                 nil
4884               mm-w3m-safe-url-regexp))))
4885      ,@body))
4886
4887 (defun gnus-mime-button-menu (event prefix)
4888  "Construct a context-sensitive menu of MIME commands."
4889  (interactive "e\nP")
4890  (save-window-excursion
4891    (let ((pos (event-start event)))
4892      (select-window (posn-window pos))
4893      (goto-char (posn-point pos))
4894      (gnus-article-check-buffer)
4895      (popup-menu gnus-mime-button-menu nil prefix))))
4896
4897 (defun gnus-mime-view-all-parts (&optional handles)
4898   "View all the MIME parts."
4899   (interactive)
4900   (with-current-buffer gnus-article-buffer
4901     (let ((handles (or handles gnus-article-mime-handles))
4902           (mail-parse-charset gnus-newsgroup-charset)
4903           (mail-parse-ignored-charsets
4904            (with-current-buffer gnus-summary-buffer
4905              gnus-newsgroup-ignored-charsets)))
4906       (when handles
4907         (mm-remove-parts handles)
4908         (goto-char (point-min))
4909         (or (search-forward "\n\n") (goto-char (point-max)))
4910         (let ((inhibit-read-only t))
4911           (delete-region (point) (point-max))
4912           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4913
4914 (defun gnus-article-jump-to-part (n)
4915   "Jump to MIME part N."
4916   (interactive "P")
4917   (pop-to-buffer gnus-article-buffer)
4918   ;; FIXME: why is it necessary?
4919   (sit-for 0)
4920   (let ((parts (length gnus-article-mime-handle-alist)))
4921     (or n (setq n (read-number (format "Jump to part (2..%s): " parts))))
4922     (unless (and (integerp n) (<= n parts) (>= n 1))
4923       (setq n
4924             (progn
4925               (gnus-message 7 "Invalid part `%s', using %s instead."
4926                             n parts)
4927               parts)))
4928     (gnus-message 9 "Jumping to part %s." n)
4929     (cond ((>= gnus-auto-select-part 1)
4930            (while (and (<= n parts)
4931                        (not (gnus-article-goto-part n)))
4932              (setq n (1+ n))))
4933           ((< gnus-auto-select-part 0)
4934            (while (and (>= n 1)
4935                        (not (gnus-article-goto-part n)))
4936              (setq n (1- n))))
4937           (t
4938            (gnus-article-goto-part n)))))
4939
4940 (defvar gnus-mime-buttonized-part-id nil
4941   "ID of a mime part that should be buttonized.
4942 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4943
4944 (eval-when-compile
4945   (defsubst gnus-article-edit-part (handles &optional current-id)
4946     "Edit an article in order to delete a mime part.
4947 This function is exclusively used by `gnus-mime-save-part-and-strip'
4948 and `gnus-mime-delete-part', and not provided at run-time normally."
4949     (gnus-article-edit-article
4950      `(lambda ()
4951         (buffer-disable-undo)
4952         (erase-buffer)
4953         (let ((mail-parse-charset (or gnus-article-charset
4954                                       ',gnus-newsgroup-charset))
4955               (mail-parse-ignored-charsets
4956                (or gnus-article-ignored-charsets
4957                    ',gnus-newsgroup-ignored-charsets))
4958               (mbl mml-buffer-list))
4959           (setq mml-buffer-list nil)
4960           (insert-buffer-substring gnus-original-article-buffer)
4961           (mime-to-mml ',handles)
4962           (setq gnus-article-mime-handles nil)
4963           (let ((mbl1 mml-buffer-list))
4964             (setq mml-buffer-list mbl)
4965             (set (make-local-variable 'mml-buffer-list) mbl1))
4966           (gnus-make-local-hook 'kill-buffer-hook)
4967           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4968      `(lambda (no-highlight)
4969         (let ((mail-parse-charset (or gnus-article-charset
4970                                       ',gnus-newsgroup-charset))
4971               (message-options message-options)
4972               (message-options-set-recipient)
4973               (mail-parse-ignored-charsets
4974                (or gnus-article-ignored-charsets
4975                    ',gnus-newsgroup-ignored-charsets)))
4976           (mml-to-mime)
4977           (mml-destroy-buffers)
4978           (remove-hook 'kill-buffer-hook
4979                        'mml-destroy-buffers t)
4980           (kill-local-variable 'mml-buffer-list))
4981         (gnus-summary-edit-article-done
4982          ,(or (mail-header-references gnus-current-headers) "")
4983          ,(gnus-group-read-only-p)
4984          ,gnus-summary-buffer no-highlight))
4985      t)
4986     ;; Force buttonizing this part.
4987     (let ((gnus-mime-buttonized-part-id current-id))
4988       (gnus-article-edit-done))
4989     (gnus-configure-windows 'article)
4990     (when (and current-id (integerp gnus-auto-select-part))
4991       (gnus-article-jump-to-part
4992        (min (max (+ current-id gnus-auto-select-part) 1)
4993             (with-current-buffer gnus-article-buffer
4994               (length gnus-article-mime-handle-alist)))))))
4995
4996 (defun gnus-mime-replace-part (file)
4997   "Replace MIME part under point with an external body."
4998   ;; Useful if file has already been saved to disk
4999   (interactive
5000    (list
5001     (read-file-name "Replace MIME part with file: "
5002                     (or mm-default-directory default-directory)
5003                     nil nil)))
5004   (gnus-mime-save-part-and-strip file))
5005
5006 (defun gnus-mime-save-part-and-strip (&optional file)
5007   "Save the MIME part under point then replace it with an external body.
5008 If FILE is given, use it for the external part."
5009   (interactive)
5010   (gnus-article-check-buffer)
5011   (when (gnus-group-read-only-p)
5012     (error "The current group does not support deleting of parts"))
5013   (when (mm-complicated-handles gnus-article-mime-handles)
5014     (error "\
5015 The current article has a complicated MIME structure, giving up..."))
5016   (let* ((data (get-text-property (point) 'gnus-data))
5017          (id (get-text-property (point) 'gnus-part))
5018          param
5019          (handles gnus-article-mime-handles))
5020     (unless file
5021       (setq file
5022             (and data (mm-save-part data "Delete MIME part and save to: "))))
5023     (when file
5024       (with-current-buffer (mm-handle-buffer data)
5025         (erase-buffer)
5026         (insert "Content-Type: " (mm-handle-media-type data))
5027         (mml-insert-parameter-string (cdr (mm-handle-type data))
5028                                      '(charset))
5029         ;; Add a filename for the sake of saving the part again.
5030         (mml-insert-parameter
5031          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5032         (insert "\n")
5033         (insert "Content-ID: " (message-make-message-id) "\n")
5034         (insert "Content-Transfer-Encoding: binary\n")
5035         (insert "\n"))
5036       (setcdr data
5037               (cdr (mm-make-handle nil
5038                                    `("message/external-body"
5039                                      (access-type . "LOCAL-FILE")
5040                                      (name . ,file)))))
5041       ;; (set-buffer gnus-summary-buffer)
5042       (gnus-article-edit-part handles id))))
5043
5044 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5045 ;; parts...>') but with stripping would be nice.
5046
5047 (defun gnus-mime-delete-part ()
5048   "Delete the MIME part under point.
5049 Replace it with some information about the removed part."
5050   (interactive)
5051   (gnus-article-check-buffer)
5052   (when (gnus-group-read-only-p)
5053     (error "The current group does not support deleting of parts"))
5054   (when (mm-complicated-handles gnus-article-mime-handles)
5055     (error "\
5056 The current article has a complicated MIME structure, giving up..."))
5057   (when (or gnus-expert-user
5058             (gnus-yes-or-no-p "\
5059 Deleting parts may malfunction or destroy the article; continue? "))
5060     (let* ((data (get-text-property (point) 'gnus-data))
5061            (id (get-text-property (point) 'gnus-part))
5062            (handles gnus-article-mime-handles)
5063            (none "(none)")
5064            (description
5065             (let ((desc (mm-handle-description data)))
5066               (when desc
5067                 (mail-decode-encoded-word-string desc))))
5068            (filename
5069             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5070                 none))
5071            (type (mm-handle-media-type data)))
5072       (unless data
5073         (error "No MIME part under point"))
5074       (with-current-buffer (mm-handle-buffer data)
5075         (let ((bsize (buffer-size)))
5076           (erase-buffer)
5077           (insert
5078            (concat
5079             ",----\n"
5080             "| The following attachment has been deleted:\n"
5081             "|\n"
5082             "| Type:           " type "\n"
5083             "| Filename:       " filename "\n"
5084             "| Size (encoded): " (format "%s byte%s\n"
5085                                          bsize (if (= bsize 1)
5086                                                    ""
5087                                                  "s"))
5088             (when description
5089               (concat    "| Description:    " description "\n"))
5090             "`----\n"))
5091           (setcdr data
5092                   (cdr (mm-make-handle
5093                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5094                         (list "attachment")
5095                         (format "Deleted attachment (%s bytes)" bsize))))))
5096       ;; (set-buffer gnus-summary-buffer)
5097       (gnus-article-edit-part handles id))))
5098
5099 (defun gnus-mime-save-part ()
5100   "Save the MIME part under point."
5101   (interactive)
5102   (gnus-article-check-buffer)
5103   (let ((data (get-text-property (point) 'gnus-data)))
5104     (when data
5105       (mm-save-part data))))
5106
5107 (defun gnus-mime-pipe-part (&optional cmd)
5108   "Pipe the MIME part under point to a process.
5109 Use CMD as the process."
5110   (interactive)
5111   (gnus-article-check-buffer)
5112   (let ((data (get-text-property (point) 'gnus-data)))
5113     (when data
5114       (mm-pipe-part data cmd))))
5115
5116 (defun gnus-mime-view-part ()
5117   "Interactively choose a viewing method for the MIME part under point."
5118   (interactive)
5119   (gnus-article-check-buffer)
5120   (let ((data (get-text-property (point) 'gnus-data)))
5121     (when data
5122       (setq gnus-article-mime-handles
5123             (mm-merge-handles
5124              gnus-article-mime-handles (setq data (copy-sequence data))))
5125       (mm-interactively-view-part data))))
5126
5127 (defun gnus-mime-view-part-as-type-internal ()
5128   (gnus-article-check-buffer)
5129   (let* ((handle (get-text-property (point) 'gnus-data))
5130          (name (or
5131                 ;; Content-Type: foo/bar; name=...
5132                 (mail-content-type-get (mm-handle-type handle) 'name)
5133                 ;; Content-Disposition: attachment; filename=...
5134                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5135          (def-type (and name (mm-default-file-encoding name))))
5136     (or (and def-type (cons def-type 0))
5137         (and handle
5138              (equal (mm-handle-media-supertype handle) "text")
5139              '("text/plain" . 0))
5140         '("application/octet-stream" . 0))))
5141
5142 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5143   "Choose a MIME media type, and view the part as such.
5144 If non-nil, PRED is a predicate to use during completion to limit the
5145 available media-types."
5146   (interactive)
5147   (unless mime-type
5148     (setq mime-type
5149           (let ((default (gnus-mime-view-part-as-type-internal)))
5150             (completing-read
5151              (format "View as MIME type (default %s): "
5152                      (car default))
5153              (mapcar #'list (mailcap-mime-types))
5154              pred nil nil nil
5155              (car default)))))
5156   (gnus-article-check-buffer)
5157   (let ((handle (get-text-property (point) 'gnus-data)))
5158     (when handle
5159       (when (equal (mm-handle-media-type handle) "message/external-body")
5160         (unless (mm-handle-cache handle)
5161           (mm-extern-cache-contents handle))
5162         (setq handle (mm-handle-cache handle)))
5163       (setq handle
5164             (mm-make-handle (mm-handle-buffer handle)
5165                             (cons mime-type (cdr (mm-handle-type handle)))
5166                             (mm-handle-encoding handle)
5167                             (mm-handle-undisplayer handle)
5168                             (mm-handle-disposition handle)
5169                             (mm-handle-description handle)
5170                             nil
5171                             (mm-handle-id handle)))
5172       (setq gnus-article-mime-handles
5173             (mm-merge-handles gnus-article-mime-handles handle))
5174       (when (mm-handle-displayed-p handle)
5175         (mm-remove-part handle))
5176       (gnus-mm-display-part handle))))
5177
5178 (defun gnus-mime-copy-part (&optional handle arg)
5179   "Put the MIME part under point into a new buffer.
5180 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5181 are decompressed."
5182   (interactive (list nil current-prefix-arg))
5183   (gnus-article-check-buffer)
5184   (unless handle
5185     (setq handle (get-text-property (point) 'gnus-data)))
5186   (when handle
5187     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5188                                                'name)
5189                         (mail-content-type-get (mm-handle-disposition handle)
5190                                                'filename)))
5191           contents dont-decode charset coding-system)
5192       (mm-with-unibyte-buffer
5193         (mm-insert-part handle)
5194         (setq contents (or (condition-case nil
5195                                (mm-decompress-buffer filename nil 'sig)
5196                              (error
5197                               (setq dont-decode t)
5198                               nil))
5199                            (buffer-string))))
5200       (setq filename (cond (filename (file-name-nondirectory filename))
5201                            (dont-decode "*raw data*")
5202                            (t "*decoded*")))
5203       (cond
5204        (dont-decode)
5205        ((not arg)
5206         (unless (setq charset (mail-content-type-get
5207                                (mm-handle-type handle) 'charset))
5208           (unless (setq coding-system (mm-with-unibyte-buffer
5209                                         (insert contents)
5210                                         (mm-find-buffer-file-coding-system)))
5211             (setq charset gnus-newsgroup-charset))))
5212        ((numberp arg)
5213         (setq charset (or (cdr (assq arg
5214                                      gnus-summary-show-article-charset-alist))
5215                           (mm-read-coding-system "Charset: ")))))
5216       (switch-to-buffer (generate-new-buffer filename))
5217       (if (or coding-system
5218               (and charset
5219                    (setq coding-system (mm-charset-to-coding-system charset))
5220                    (not (eq charset 'ascii))))
5221           (progn
5222             (mm-enable-multibyte)
5223             (insert (mm-decode-coding-string contents coding-system))
5224             (setq buffer-file-coding-system
5225                   (if (boundp 'last-coding-system-used)
5226                       (symbol-value 'last-coding-system-used)
5227                     coding-system)))
5228         (mm-disable-multibyte)
5229         (insert contents)
5230         (setq buffer-file-coding-system mm-binary-coding-system))
5231       ;; We do it this way to make `normal-mode' set the appropriate mode.
5232       (unwind-protect
5233           (progn
5234             (setq buffer-file-name (expand-file-name filename))
5235             (normal-mode))
5236         (setq buffer-file-name nil))
5237       (goto-char (point-min)))))
5238
5239 (defun gnus-mime-print-part (&optional handle filename)
5240   "Print the MIME part under point."
5241   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5242   (gnus-article-check-buffer)
5243   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5244          (contents (and handle (mm-get-part handle)))
5245          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5246          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5247     (when contents
5248         (if printer
5249             (unwind-protect
5250                 (progn
5251                   (mm-save-part-to-file handle file)
5252                   (call-process shell-file-name nil
5253                                 (generate-new-buffer " *mm*")
5254                                 nil
5255                                 shell-command-switch
5256                                 (mm-mailcap-command
5257                                  printer file (mm-handle-type handle))))
5258               (delete-file file))
5259           (with-temp-buffer
5260             (insert contents)
5261             (gnus-print-buffer))
5262           (ps-despool filename)))))
5263
5264 (defun gnus-mime-inline-part (&optional handle arg)
5265   "Insert the MIME part under point into the current buffer.
5266 Compressed files like .gz and .bz2 are decompressed."
5267   (interactive (list nil current-prefix-arg))
5268   (gnus-article-check-buffer)
5269   (unless handle
5270     (setq handle (get-text-property (point) 'gnus-data)))
5271   (when handle
5272     (let ((b (point))
5273           (inhibit-read-only t)
5274           contents charset coding-system)
5275       (if (and (not arg) (mm-handle-undisplayer handle))
5276           (mm-remove-part handle)
5277         (mm-with-unibyte-buffer
5278           (mm-insert-part handle)
5279           (setq contents
5280                 (or (mm-decompress-buffer
5281                      (or (mail-content-type-get (mm-handle-type handle)
5282                                                 'name)
5283                          (mail-content-type-get (mm-handle-disposition handle)
5284                                                 'filename))
5285                      nil t)
5286                     (buffer-string))))
5287         (cond
5288          ((not arg)
5289           (unless (setq charset (mail-content-type-get
5290                                  (mm-handle-type handle) 'charset))
5291             (unless (setq coding-system
5292                           (mm-with-unibyte-buffer
5293                             (insert contents)
5294                             (mm-find-buffer-file-coding-system)))
5295               (setq charset gnus-newsgroup-charset))))
5296          ((numberp arg)
5297           (if (mm-handle-undisplayer handle)
5298               (mm-remove-part handle))
5299           (setq charset
5300                 (or (cdr (assq arg
5301                                gnus-summary-show-article-charset-alist))
5302                     (mm-read-coding-system "Charset: "))))
5303          (t
5304           (if (mm-handle-undisplayer handle)
5305               (mm-remove-part handle))))
5306         (forward-line 2)
5307         (mm-insert-inline
5308          handle
5309          (if (or coding-system
5310                  (and charset
5311                       (setq coding-system
5312                             (mm-charset-to-coding-system charset))
5313                       (not (eq coding-system 'ascii))))
5314              (mm-decode-coding-string contents coding-system)
5315            (mm-string-to-multibyte contents)))
5316         (goto-char b)))))
5317
5318 (defun gnus-mime-set-charset-parameters (handle charset)
5319   "Set CHARSET to parameters in HANDLE.
5320 CHARSET may either be a string or a symbol."
5321   (unless (stringp charset)
5322     (setq charset (symbol-name charset)))
5323   (if (stringp (car handle))
5324       (dolist (h (cdr handle))
5325         (gnus-mime-set-charset-parameters h charset))
5326     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5327                                             "message/external-body")
5328                                      (progn
5329                                        (unless (mm-handle-cache handle)
5330                                          (mm-extern-cache-contents handle))
5331                                        (mm-handle-cache handle))
5332                                    handle)))
5333            (param (assq 'charset (cdr type))))
5334       (if param
5335           (setcdr param charset)
5336         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5337
5338 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5339   "Insert the MIME part under point into the current buffer using the
5340 specified charset."
5341   (interactive (list nil current-prefix-arg))
5342   (gnus-article-check-buffer)
5343   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5344         (fun (get-text-property (point) 'gnus-callback))
5345         (gnus-newsgroup-ignored-charsets 'gnus-all)
5346         charset form preferred parts)
5347     (when handle
5348       (when (prog1
5349                 (and fun
5350                      (setq charset
5351                            (or (cdr (assq
5352                                      arg
5353                                      gnus-summary-show-article-charset-alist))
5354                                (mm-read-coding-system "Charset: "))))
5355               (if (mm-handle-undisplayer handle)
5356                   (mm-remove-part handle)))
5357         (gnus-mime-set-charset-parameters handle charset)
5358         (when (and (consp (setq form (cdr-safe fun)))
5359                    (setq form (ignore-errors
5360                                 (assq 'gnus-mime-display-alternative form)))
5361                    (setq preferred (caddr form))
5362                    (progn
5363                      (when (eq (car preferred) 'quote)
5364                        (setq preferred (cadr preferred)))
5365                      (not (equal preferred
5366                                  (get-text-property (point) 'gnus-data))))
5367                    (setq parts (get-text-property (point) 'gnus-part))
5368                    (setq parts (cdr (assq parts
5369                                           gnus-article-mime-handle-alist)))
5370                    (equal (mm-handle-media-type parts) "multipart/alternative")
5371                    (setq parts (reverse (cdr parts))))
5372           (setcar (cddr form)
5373                   (list 'quote (or (cadr (member preferred parts))
5374                                    (car parts)))))
5375         (funcall fun handle)))))
5376
5377 (defun gnus-mime-view-part-externally (&optional handle)
5378   "View the MIME part under point with an external viewer."
5379   (interactive)
5380   (gnus-article-check-buffer)
5381   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5382          (mm-user-display-methods nil)
5383          (mm-inlined-types nil)
5384          (mail-parse-charset gnus-newsgroup-charset)
5385          (mail-parse-ignored-charsets
5386           (with-current-buffer gnus-summary-buffer
5387             gnus-newsgroup-ignored-charsets))
5388          (type (mm-handle-media-type handle))
5389          (method (mailcap-mime-info type))
5390          (mm-enable-external t))
5391     (if (not (stringp method))
5392         (gnus-mime-view-part-as-type
5393          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5394       (when handle
5395         (if (mm-handle-undisplayer handle)
5396             (mm-remove-part handle)
5397           (mm-display-part handle))))))
5398
5399 (defun gnus-mime-view-part-internally (&optional handle)
5400   "View the MIME part under point with an internal viewer.
5401 If no internal viewer is available, use an external viewer."
5402   (interactive)
5403   (gnus-article-check-buffer)
5404   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5405          (mm-inlined-types '(".*"))
5406          (mm-inline-large-images t)
5407          (mail-parse-charset gnus-newsgroup-charset)
5408          (mail-parse-ignored-charsets
5409           (with-current-buffer gnus-summary-buffer
5410             gnus-newsgroup-ignored-charsets))
5411          (inhibit-read-only t))
5412     (if (not (mm-inlinable-p handle))
5413         (gnus-mime-view-part-as-type
5414          nil (lambda (types) (mm-inlinable-p handle (car types))))
5415       (when handle
5416         (if (mm-handle-undisplayer handle)
5417             (mm-remove-part handle)
5418           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5419
5420 (defun gnus-mime-action-on-part (&optional action)
5421   "Do something with the MIME attachment at \(point\)."
5422   (interactive
5423    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5424   (gnus-article-check-buffer)
5425   (let ((action-pair (assoc action gnus-mime-action-alist)))
5426     (if action-pair
5427         (funcall (cdr action-pair)))))
5428
5429 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5430   "Call FUNCTION on MIME part N.
5431 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5432 If INTERACTIVE, call FUNCTION interactivly."
5433   (let (window frame)
5434     ;; Check whether the article is displayed.
5435     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5436                  (setq window (get-buffer-window gnus-article-buffer t))
5437                  (frame-visible-p (setq frame (window-frame window))))
5438       (error "No article is displayed"))
5439     (with-current-buffer gnus-article-buffer
5440       ;; Check whether the article displays the right contents.
5441       (unless (with-current-buffer gnus-summary-buffer
5442                 (eq gnus-current-article (gnus-summary-article-number)))
5443         (error "You should select the right article first"))
5444       (if n
5445           (setq n (prefix-numeric-value n))
5446         (let ((pt (point)))
5447           (setq n (or (get-text-property pt 'gnus-part)
5448                       (and (not (bobp))
5449                            (get-text-property (1- pt) 'gnus-part))
5450                       (get-text-property (prog2
5451                                              (forward-line 1)
5452                                              (point)
5453                                            (goto-char pt))
5454                                          'gnus-part)
5455                       (get-text-property
5456                        (or (and (setq pt (previous-single-property-change
5457                                           pt 'gnus-part))
5458                                 (1- pt))
5459                            (next-single-property-change (point) 'gnus-part)
5460                            (point))
5461                        'gnus-part)
5462                       1))))
5463       ;; Check whether the specified part exists.
5464       (when (> n (length gnus-article-mime-handle-alist))
5465         (error "No such part")))
5466     (unless
5467         (progn
5468           ;; To select the window is needed so that the cursor
5469           ;; might be visible on the MIME button.
5470           (select-window (prog1
5471                              window
5472                            (setq window (selected-window))
5473                            ;; Article may be displayed in the other frame.
5474                            (gnus-select-frame-set-input-focus
5475                             (prog1
5476                                 frame
5477                               (setq frame (selected-frame))))))
5478           (when (gnus-article-goto-part n)
5479             ;; We point the cursor and the arrow at the MIME button
5480             ;; when the `function' prompt the user for something.
5481             (let ((cursor-in-non-selected-windows t)
5482                   (overlay-arrow-string "=>")
5483                   (overlay-arrow-position (point-marker)))
5484               (unwind-protect
5485                   (cond
5486                    ((and no-handle interactive)
5487                     (call-interactively function))
5488                    (no-handle
5489                     (funcall function))
5490                    (interactive
5491                     (call-interactively
5492                      function
5493                      (cdr (assq n gnus-article-mime-handle-alist))))
5494                    (t
5495                     (funcall function
5496                              (cdr (assq n gnus-article-mime-handle-alist)))))
5497                 (set-marker overlay-arrow-position nil)
5498                 (unless gnus-auto-select-part
5499                   (gnus-select-frame-set-input-focus frame)
5500                   (select-window window))))
5501             t))
5502       (if gnus-inhibit-mime-unbuttonizing
5503           ;; This is the default though the program shouldn't reach here.
5504           (error "No such part")
5505         ;; The part which doesn't have the MIME button is selected.
5506         ;; So, we display all the buttons and redo it.
5507         (let ((gnus-inhibit-mime-unbuttonizing t))
5508           (gnus-summary-show-article)
5509           (gnus-article-part-wrapper n function no-handle))))))
5510
5511 (defun gnus-article-pipe-part (n)
5512   "Pipe MIME part N, which is the numerical prefix."
5513   (interactive "P")
5514   (gnus-article-part-wrapper n 'mm-pipe-part))
5515
5516 (defun gnus-article-save-part (n)
5517   "Save MIME part N, which is the numerical prefix."
5518   (interactive "P")
5519   (gnus-article-part-wrapper n 'mm-save-part))
5520
5521 (defun gnus-article-interactively-view-part (n)
5522   "View MIME part N interactively, which is the numerical prefix."
5523   (interactive "P")
5524   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5525
5526 (defun gnus-article-copy-part (n)
5527   "Copy MIME part N, which is the numerical prefix."
5528   (interactive "P")
5529   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5530
5531 (defun gnus-article-view-part-as-charset (n)
5532   "View MIME part N using a specified charset.
5533 N is the numerical prefix."
5534   (interactive "P")
5535   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5536
5537 (defun gnus-article-view-part-externally (n)
5538   "View MIME part N externally, which is the numerical prefix."
5539   (interactive "P")
5540   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5541
5542 (defun gnus-article-inline-part (n)
5543   "Inline MIME part N, which is the numerical prefix."
5544   (interactive "P")
5545   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5546
5547 (defun gnus-article-save-part-and-strip (n)
5548   "Save MIME part N and replace it with an external body.
5549 N is the numerical prefix."
5550   (interactive "P")
5551   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5552
5553 (defun gnus-article-replace-part (n)
5554   "Replace MIME part N with an external body.
5555 N is the numerical prefix."
5556   (interactive "P")
5557   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5558
5559 (defun gnus-article-delete-part (n)
5560   "Delete MIME part N and add some information about the removed part.
5561 N is the numerical prefix."
5562   (interactive "P")
5563   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5564
5565 (defun gnus-article-view-part-as-type (n)
5566   "Choose a MIME media type, and view part N as such.
5567 N is the numerical prefix."
5568   (interactive "P")
5569   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5570
5571 (defun gnus-article-mime-match-handle-first (condition)
5572   (if condition
5573       (let (n)
5574         (dolist (ihandle gnus-article-mime-handle-alist)
5575           (if (and (cond
5576                     ((functionp condition)
5577                      (funcall condition (cdr ihandle)))
5578                     ((eq condition 'undisplayed)
5579                      (not (or (mm-handle-undisplayer (cdr ihandle))
5580                               (equal (mm-handle-media-type (cdr ihandle))
5581                                      "multipart/alternative"))))
5582                     ((eq condition 'undisplayed-alternative)
5583                      (not (mm-handle-undisplayer (cdr ihandle))))
5584                     (t t))
5585                    (gnus-article-goto-part (car ihandle))
5586                    (or (not n) (< (car ihandle) n)))
5587               (setq n (car ihandle))))
5588         (or n 1))
5589     1))
5590
5591 (defun gnus-article-view-part (&optional n)
5592   "View MIME part N, which is the numerical prefix.
5593 If the part is already shown, hide the part.  If N is nil, view
5594 all parts."
5595   (interactive "P")
5596   (with-current-buffer gnus-article-buffer
5597     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5598                              gnus-article-mime-match-handle-function)))
5599     (when (> n (length gnus-article-mime-handle-alist))
5600       (error "No such part"))
5601     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5602       (when (gnus-article-goto-part n)
5603         (if (equal (car handle) "multipart/alternative")
5604             (gnus-article-press-button)
5605           (when (eq (gnus-mm-display-part handle) 'internal)
5606             (gnus-set-window-start)))))))
5607
5608 (defsubst gnus-article-mime-total-parts ()
5609   (if (bufferp (car gnus-article-mime-handles))
5610       1 ;; single part
5611     (1- (length gnus-article-mime-handles))))
5612
5613 (defun gnus-mm-display-part (handle)
5614   "Display HANDLE and fix MIME button."
5615   (let ((id (get-text-property (point) 'gnus-part))
5616         (point (point))
5617         (inhibit-read-only t))
5618     (forward-line 1)
5619     (prog1
5620         (let ((window (selected-window))
5621               (mail-parse-charset gnus-newsgroup-charset)
5622               (mail-parse-ignored-charsets
5623                (if (gnus-buffer-live-p gnus-summary-buffer)
5624                    (with-current-buffer gnus-summary-buffer
5625                      gnus-newsgroup-ignored-charsets)
5626                  nil)))
5627           (save-excursion
5628             (unwind-protect
5629                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5630                       (beg (point)))
5631                   (when win
5632                     (select-window win))
5633                   (goto-char point)
5634                   (forward-line)
5635                   (if (mm-handle-displayed-p handle)
5636                       ;; This will remove the part.
5637                       (mm-display-part handle)
5638                     (save-restriction
5639                       (narrow-to-region (point)
5640                                         (if (eobp) (point) (1+ (point))))
5641                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5642                       ;; We narrow to the part itself and
5643                       ;; then call the treatment functions.
5644                       (goto-char (point-min))
5645                       (forward-line 1)
5646                       (narrow-to-region (point) (point-max))
5647                       (gnus-treat-article
5648                        nil id
5649                        (gnus-article-mime-total-parts)
5650                        (mm-handle-media-type handle)))))
5651               (if (window-live-p window)
5652                   (select-window window)))))
5653       (goto-char point)
5654       (gnus-delete-line)
5655       (gnus-insert-mime-button
5656        handle id (list (mm-handle-displayed-p handle)))
5657       (goto-char point))))
5658
5659 (defun gnus-article-goto-part (n)
5660   "Go to MIME part N."
5661   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5662
5663 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5664   (let ((gnus-tmp-name
5665          (or (mail-content-type-get (mm-handle-type handle) 'name)
5666              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5667              (mail-content-type-get (mm-handle-type handle) 'url)
5668              ""))
5669         (gnus-tmp-type (mm-handle-media-type handle))
5670         (gnus-tmp-description (or (mm-handle-description handle) ""))
5671         (gnus-tmp-dots
5672          (if (if displayed (car displayed)
5673                (mm-handle-displayed-p handle))
5674              "" "..."))
5675         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5676                            (buffer-size)))
5677         gnus-tmp-type-long b e)
5678     (when (string-match ".*/" gnus-tmp-name)
5679       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5680     (setq gnus-tmp-type-long (concat gnus-tmp-type
5681                                      (and (not (equal gnus-tmp-name ""))
5682                                           (concat "; " gnus-tmp-name))))
5683     (unless (equal gnus-tmp-description "")
5684       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5685     (unless (bolp)
5686       (insert "\n"))
5687     (setq b (point))
5688     (gnus-eval-format
5689      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5690      `(keymap ,gnus-mime-button-map
5691               gnus-callback gnus-mm-display-part
5692               gnus-part ,gnus-tmp-id
5693               article-type annotation
5694               gnus-data ,handle))
5695     (setq e (if (bolp)
5696                 ;; Exclude a newline.
5697                 (1- (point))
5698               (point)))
5699     (when gnus-article-button-face
5700       (gnus-overlay-put (gnus-make-overlay b e nil t)
5701                         'face gnus-article-button-face))
5702     (widget-convert-button
5703      'link b e
5704      :mime-handle handle
5705      :action 'gnus-widget-press-button
5706      :button-keymap gnus-mime-button-map
5707      :help-echo
5708      (lambda (widget)
5709        ;; Needed to properly clear the message due to a bug in
5710        ;; wid-edit (XEmacs only).
5711        (if (boundp 'help-echo-owns-message)
5712            (setq help-echo-owns-message t))
5713        (format
5714         "%S: %s the MIME part; %S: more options"
5715         (aref gnus-mouse-2 0)
5716         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5717             "hide" "show")
5718         (aref gnus-down-mouse-3 0))))))
5719
5720 (defun gnus-widget-press-button (elems el)
5721   (goto-char (widget-get elems :from))
5722   (gnus-article-press-button))
5723
5724 (defvar gnus-displaying-mime nil)
5725
5726 (defun gnus-display-mime (&optional ihandles)
5727   "Display the MIME parts."
5728   (save-excursion
5729     (save-selected-window
5730       (let ((window (get-buffer-window gnus-article-buffer))
5731             (point (point)))
5732         (when window
5733           (select-window window)
5734           ;; We have to do this since selecting the window
5735           ;; may change the point.  So we set the window point.
5736           (set-window-point window point)))
5737       (let ((handles ihandles)
5738             (inhibit-read-only t)
5739             handle)
5740         (cond (handles)
5741               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5742                (when gnus-article-emulate-mime
5743                  (mm-uu-dissect-text-parts handles)))
5744               (gnus-article-emulate-mime
5745                (setq handles (mm-uu-dissect))))
5746         (when (and (not ihandles)
5747                    (not gnus-displaying-mime))
5748           ;; Top-level call; we clean up.
5749           (when gnus-article-mime-handles
5750             (mm-destroy-parts gnus-article-mime-handles)
5751             (setq gnus-article-mime-handle-alist nil));; A trick.
5752           (setq gnus-article-mime-handles handles)
5753           ;; We allow users to glean info from the handles.
5754           (when gnus-article-mime-part-function
5755             (gnus-mime-part-function handles)))
5756         (if (and handles
5757                  (or (not (stringp (car handles)))
5758                      (cdr handles)))
5759             (progn
5760               (when (and (not ihandles)
5761                          (not gnus-displaying-mime))
5762                 ;; Clean up for mime parts.
5763                 (article-goto-body)
5764                 (delete-region (point) (point-max)))
5765               (let ((gnus-displaying-mime t))
5766                 (gnus-mime-display-part handles)))
5767           (save-restriction
5768             (article-goto-body)
5769             (narrow-to-region (point) (point-max))
5770             (gnus-treat-article nil 1 1)
5771             (widen)))
5772         (unless ihandles
5773           ;; Highlight the headers.
5774           (save-excursion
5775             (save-restriction
5776               (article-goto-body)
5777               (narrow-to-region (point-min) (point))
5778               (gnus-article-save-original-date
5779                (gnus-treat-article 'head)))))))
5780     ;; Cope with broken MIME messages.
5781     (goto-char (point-max))
5782     (unless (bolp)
5783       (insert "\n"))))
5784
5785 (defcustom gnus-mime-display-multipart-as-mixed nil
5786   "Display \"multipart\" parts as  \"multipart/mixed\".
5787
5788 If t, it overrides nil values of
5789 `gnus-mime-display-multipart-alternative-as-mixed' and
5790 `gnus-mime-display-multipart-related-as-mixed'."
5791   :group 'gnus-article-mime
5792   :type 'boolean)
5793
5794 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5795   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5796   :version "22.1"
5797   :group 'gnus-article-mime
5798   :type 'boolean)
5799
5800 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5801   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5802
5803 If displaying \"text/html\" is discouraged \(see
5804 `mm-discouraged-alternatives'\) images or other material inside a
5805 \"multipart/related\" part might be overlooked when this variable is nil."
5806   :version "22.1"
5807   :group 'gnus-article-mime
5808   :type 'boolean)
5809
5810 (defun gnus-mime-display-part (handle)
5811   (cond
5812    ;; Maybe a broken MIME message.
5813    ((null handle))
5814    ;; Single part.
5815    ((not (stringp (car handle)))
5816     (gnus-mime-display-single handle))
5817    ;; User-defined multipart
5818    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5819     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5820              handle))
5821    ;; multipart/alternative
5822    ((and (equal (car handle) "multipart/alternative")
5823          (not (or gnus-mime-display-multipart-as-mixed
5824                   gnus-mime-display-multipart-alternative-as-mixed)))
5825     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5826       (push (cons id handle) gnus-article-mime-handle-alist)
5827       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5828    ;; multipart/related
5829    ((and (equal (car handle) "multipart/related")
5830          (not (or gnus-mime-display-multipart-as-mixed
5831                   gnus-mime-display-multipart-related-as-mixed)))
5832     ;;;!!!We should find the start part, but we just default
5833     ;;;!!!to the first part.
5834     ;;(gnus-mime-display-part (cadr handle))
5835     ;;;!!! Most multipart/related is an HTML message plus images.
5836     ;;;!!! Unfortunately we are unable to let W3 display those
5837     ;;;!!! included images, so we just display it as a mixed multipart.
5838     ;;(gnus-mime-display-mixed (cdr handle))
5839     ;;;!!! No, w3 can display everything just fine.
5840     (gnus-mime-display-part (cadr handle)))
5841    ((equal (car handle) "multipart/signed")
5842     (gnus-add-wash-type 'signed)
5843     (gnus-mime-display-security handle))
5844    ((equal (car handle) "multipart/encrypted")
5845     (gnus-add-wash-type 'encrypted)
5846     (gnus-mime-display-security handle))
5847    ;; Other multiparts are handled like multipart/mixed.
5848    (t
5849     (gnus-mime-display-mixed (cdr handle)))))
5850
5851 (defun gnus-mime-part-function (handles)
5852   (if (stringp (car handles))
5853       (mapcar 'gnus-mime-part-function (cdr handles))
5854     (funcall gnus-article-mime-part-function handles)))
5855
5856 (defun gnus-mime-display-mixed (handles)
5857   (mapcar 'gnus-mime-display-part handles))
5858
5859 (defun gnus-mime-display-single (handle)
5860   (let ((type (mm-handle-media-type handle))
5861         (ignored gnus-ignored-mime-types)
5862         (not-attachment t)
5863         (move nil)
5864         display text)
5865     (catch 'ignored
5866       (progn
5867         (while ignored
5868           (when (string-match (pop ignored) type)
5869             (throw 'ignored nil)))
5870         (if (and (setq not-attachment
5871                        (and (not (mm-inline-override-p handle))
5872                             (or (not (mm-handle-disposition handle))
5873                                 (equal (car (mm-handle-disposition handle))
5874                                        "inline")
5875                                 (mm-attachment-override-p handle))))
5876                  (mm-automatic-display-p handle)
5877                  (or (and
5878                       (mm-inlinable-p handle)
5879                       (mm-inlined-p handle))
5880                      (mm-automatic-external-display-p type)))
5881             (setq display t)
5882           (when (equal (mm-handle-media-supertype handle) "text")
5883             (setq text t)))
5884         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5885               beg)
5886           (push (cons id handle) gnus-article-mime-handle-alist)
5887           (when (and display
5888                      (equal (mm-handle-media-supertype handle) "message"))
5889             (insert-char
5890              ?\n
5891              (cond ((not (bolp)) 2)
5892                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5893                    (t 1))))
5894           (when (or (not display)
5895                     (not (gnus-unbuttonized-mime-type-p type))
5896                     (eq id gnus-mime-buttonized-part-id))
5897             (gnus-insert-mime-button
5898              handle id (list (or display (and not-attachment text))))
5899             (gnus-article-insert-newline)
5900             ;; Remember modify the number of forward lines.
5901             (setq move t))
5902           (setq beg (point))
5903           (cond
5904            (display
5905             (when move
5906               (forward-line -1)
5907               (setq beg (point)))
5908             (let ((mail-parse-charset gnus-newsgroup-charset)
5909                   (mail-parse-ignored-charsets
5910                    (save-excursion (condition-case ()
5911                                        (set-buffer gnus-summary-buffer)
5912                                      (error))
5913                                    gnus-newsgroup-ignored-charsets)))
5914               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5915             (goto-char (point-max)))
5916            ((and text not-attachment)
5917             (when move
5918               (forward-line -1)
5919               (setq beg (point)))
5920             (gnus-article-insert-newline)
5921             (mm-insert-inline
5922              handle
5923              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5924                                                        'charset)
5925                                 (and (equal type "text/calendar") 'utf-8))))
5926                (cond ((not charset)
5927                       (mm-string-as-multibyte (mm-get-part handle)))
5928                      ((eq charset 'gnus-decoded)
5929                       (with-current-buffer (mm-handle-buffer handle)
5930                         (buffer-string)))
5931                      (t
5932                       (mm-decode-string (mm-get-part handle) charset)))))
5933             (goto-char (point-max))))
5934           ;; Do highlighting.
5935           (save-excursion
5936             (save-restriction
5937               (narrow-to-region beg (point))
5938               (if (eq handle gnus-article-mime-handles)
5939                   ;; The format=flowed case.
5940                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5941                 ;; Don't count signature parts that are never displayed.
5942                 ;; The part number should be re-calculated supposing this
5943                 ;; might be a message/rfc822 part.
5944                 (let (handles)
5945                   (dolist (part gnus-article-mime-handles)
5946                     (unless (or (stringp part)
5947                                 (equal (car (mm-handle-type part))
5948                                        "application/pgp-signature"))
5949                       (push part handles)))
5950                   (gnus-treat-article
5951                    nil (length (memq handle handles)) (length handles)
5952                    (mm-handle-media-type handle)))))))))))
5953
5954 (defun gnus-unbuttonized-mime-type-p (type)
5955   "Say whether TYPE is to be unbuttonized."
5956   (unless gnus-inhibit-mime-unbuttonizing
5957     (when (catch 'found
5958             (let ((types gnus-unbuttonized-mime-types))
5959               (while types
5960                 (when (string-match (pop types) type)
5961                   (throw 'found t)))))
5962       (not (catch 'found
5963              (let ((types gnus-buttonized-mime-types))
5964                (while types
5965                  (when (string-match (pop types) type)
5966                    (throw 'found t)))))))))
5967
5968 (defun gnus-article-insert-newline ()
5969   "Insert a newline, but mark it as undeletable."
5970   (gnus-put-text-property
5971    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5972
5973 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5974   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5975          (ihandles handles)
5976          (point (point))
5977          handle (inhibit-read-only t) from props begend not-pref)
5978     (save-window-excursion
5979       (save-restriction
5980         (when ibegend
5981           (narrow-to-region (car ibegend)
5982                             (or (cdr ibegend)
5983                                 (progn
5984                                   (goto-char (car ibegend))
5985                                   (forward-line 2)
5986                                   (point))))
5987           (delete-region (point-min) (point-max))
5988           (mm-remove-parts handles))
5989         (setq begend (list (point-marker)))
5990         ;; Do the toggle.
5991         (unless (setq not-pref (cadr (member preferred ihandles)))
5992           (setq not-pref (car ihandles)))
5993         (when (or ibegend
5994                   (not preferred)
5995                   (not (gnus-unbuttonized-mime-type-p
5996                         "multipart/alternative")))
5997           (gnus-add-text-properties
5998            (setq from (point))
5999            (progn
6000              (insert (format "%d.  " id))
6001              (point))
6002            `(gnus-callback
6003              (lambda (handles)
6004                (unless ,(not ibegend)
6005                  (setq gnus-article-mime-handle-alist
6006                        ',gnus-article-mime-handle-alist))
6007                (gnus-mime-display-alternative
6008                 ',ihandles ',not-pref ',begend ,id))
6009              keymap ,gnus-mime-button-map
6010              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6011              face ,gnus-article-button-face
6012              gnus-part ,id
6013              article-type multipart))
6014           (widget-convert-button 'link from (point)
6015                                  :action 'gnus-widget-press-button
6016                                  :button-keymap gnus-widget-button-keymap)
6017           ;; Do the handles
6018           (while (setq handle (pop handles))
6019             (gnus-add-text-properties
6020              (setq from (point))
6021              (progn
6022                (insert (format "(%c) %-18s"
6023                                (if (equal handle preferred) ?* ? )
6024                                (mm-handle-media-type handle)))
6025                (point))
6026              `(gnus-callback
6027                (lambda (handles)
6028                  (unless ,(not ibegend)
6029                    (setq gnus-article-mime-handle-alist
6030                          ',gnus-article-mime-handle-alist))
6031                  (gnus-mime-display-alternative
6032                   ',ihandles ',handle ',begend ,id))
6033                keymap ,gnus-mime-button-map
6034                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6035                face ,gnus-article-button-face
6036                gnus-part ,id
6037                gnus-data ,handle))
6038             (widget-convert-button 'link from (point)
6039                                    :action 'gnus-widget-press-button
6040                                    :button-keymap gnus-widget-button-keymap)
6041             (insert "  "))
6042           (insert "\n\n"))
6043         (when preferred
6044           (if (stringp (car preferred))
6045               (gnus-display-mime preferred)
6046             (let ((mail-parse-charset gnus-newsgroup-charset)
6047                   (mail-parse-ignored-charsets
6048                    (with-current-buffer gnus-summary-buffer
6049                      gnus-newsgroup-ignored-charsets)))
6050               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6051               ;; Do highlighting.
6052               (save-excursion
6053                 (save-restriction
6054                   (narrow-to-region (car begend) (point-max))
6055                   (gnus-treat-article
6056                    nil (length gnus-article-mime-handle-alist)
6057                    (gnus-article-mime-total-parts)
6058                    (mm-handle-media-type handle))))))
6059           (goto-char (point-max))
6060           (setcdr begend (point-marker)))))
6061     (when ibegend
6062       (goto-char point))))
6063
6064 (defconst gnus-article-wash-status-strings
6065   (let ((alist '((cite "c" "Possible hidden citation text"
6066                        " " "All citation text visible")
6067                  (headers "h" "Hidden headers"
6068                           " " "All headers visible.")
6069                  (pgp "p" "Encrypted or signed message status hidden"
6070                       " " "No hidden encryption nor digital signature status")
6071                  (signature "s" "Signature has been hidden"
6072                             " " "Signature is visible")
6073                  (overstrike "o" "Overstrike (^H) characters applied"
6074                              " " "No overstrike characters applied")
6075                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6076                            " " "No /*_emphasis_*/ characters applied")))
6077         result)
6078     (dolist (entry alist result)
6079       (let ((key (nth 0 entry))
6080             (on (copy-sequence (nth 1 entry)))
6081             (on-help (nth 2 entry))
6082             (off (copy-sequence (nth 3 entry)))
6083             (off-help (nth 4 entry)))
6084         (put-text-property 0 1 'help-echo on-help on)
6085         (put-text-property 0 1 'help-echo off-help off)
6086         (push (list key on off) result))))
6087   "Alist of strings describing wash status in the mode line.
6088 Each entry has the form (KEY ON OF), where the KEY is a symbol
6089 representing the particular washing function, ON is the string to use
6090 in the article mode line when the washing function is active, and OFF
6091 is the string to use when it is inactive.")
6092
6093 (defun gnus-article-wash-status-entry (key value)
6094   (let ((entry (assoc key gnus-article-wash-status-strings)))
6095     (if value (nth 1 entry) (nth 2 entry))))
6096
6097 (defun gnus-article-wash-status ()
6098   "Return a string which display status of article washing."
6099   (with-current-buffer gnus-article-buffer
6100     (let ((cite (memq 'cite gnus-article-wash-types))
6101           (headers (memq 'headers gnus-article-wash-types))
6102           (boring (memq 'boring-headers gnus-article-wash-types))
6103           (pgp (memq 'pgp gnus-article-wash-types))
6104           (pem (memq 'pem gnus-article-wash-types))
6105           (signed (memq 'signed gnus-article-wash-types))
6106           (encrypted (memq 'encrypted gnus-article-wash-types))
6107           (signature (memq 'signature gnus-article-wash-types))
6108           (overstrike (memq 'overstrike gnus-article-wash-types))
6109           (emphasis (memq 'emphasis gnus-article-wash-types)))
6110       (concat
6111        (gnus-article-wash-status-entry 'cite cite)
6112        (gnus-article-wash-status-entry 'headers (or headers boring))
6113        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6114        (gnus-article-wash-status-entry 'signature signature)
6115        (gnus-article-wash-status-entry 'overstrike overstrike)
6116        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6117
6118 (defun gnus-add-wash-type (type)
6119   "Add a washing of TYPE to the current status."
6120   (add-to-list 'gnus-article-wash-types type))
6121
6122 (defun gnus-delete-wash-type (type)
6123   "Add a washing of TYPE to the current status."
6124   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6125
6126 (defun gnus-add-image (category image)
6127   "Add IMAGE of CATEGORY to the list of displayed images."
6128   (let ((entry (assq category gnus-article-image-alist)))
6129     (unless entry
6130       (setq entry (list category))
6131       (push entry gnus-article-image-alist))
6132     (nconc entry (list image))))
6133
6134 (defun gnus-delete-images (category)
6135   "Delete all images in CATEGORY."
6136   (let ((entry (assq category gnus-article-image-alist)))
6137     (dolist (image (cdr entry))
6138       (gnus-remove-image image category))
6139     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6140     (gnus-delete-wash-type category)))
6141
6142 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6143
6144 (defun gnus-article-maybe-hide-headers ()
6145   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6146 Provided for backwards compatibility."
6147   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6148                  (not (with-current-buffer gnus-summary-buffer
6149                         gnus-have-all-headers)))
6150              (not gnus-inhibit-hiding))
6151     (gnus-article-hide-headers)))
6152
6153 ;;; Article savers.
6154
6155 (defun gnus-output-to-file (file-name)
6156   "Append the current article to a file named FILE-NAME.
6157 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6158 text and used as the value of the coding cookie which is added to the
6159 top of a file.  Otherwise, this function saves a raw article without
6160 the coding cookie."
6161   (let* ((artbuf (current-buffer))
6162          (file-name-coding-system nnmail-pathname-coding-system)
6163          (coding gnus-article-save-coding-system)
6164          (coding-system-for-read (if coding
6165                                      nil ;; Rely on the coding cookie.
6166                                    mm-text-coding-system))
6167          (coding-system-for-write (or coding
6168                                       mm-text-coding-system-for-write
6169                                       mm-text-coding-system))
6170          (exists (file-exists-p file-name)))
6171     (with-temp-buffer
6172       (when exists
6173         (insert-file-contents file-name)
6174         (goto-char (point-min))
6175         ;; Remove the existing coding cookie.
6176         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6177           (delete-region (match-beginning 0) (match-end 0))))
6178       (goto-char (point-max))
6179       (insert-buffer-substring artbuf)
6180       ;; Append newline at end of the buffer as separator, and then
6181       ;; save it to file.
6182       (goto-char (point-max))
6183       (insert "\n")
6184       (when coding
6185         ;; If the coding system is not suitable to encode the text,
6186         ;; ask a user for a proper one.
6187         (when (fboundp 'select-safe-coding-system)
6188           (setq coding (coding-system-base
6189                         (save-window-excursion
6190                           (select-safe-coding-system (point-min) (point-max)
6191                                                      coding))))
6192           (setq coding-system-for-write
6193                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6194                     coding)))
6195         (goto-char (point-min))
6196         ;; Add the coding cookie.
6197         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6198                         coding-system-for-write)))
6199       (if exists
6200           (progn
6201             (write-region (point-min) (point-max) file-name nil 'no-message)
6202             (message "Appended to %s" file-name))
6203         (write-region (point-min) (point-max) file-name))))
6204   t)
6205
6206 (defun gnus-narrow-to-page (&optional arg)
6207   "Narrow the article buffer to a page.
6208 If given a numerical ARG, move forward ARG pages."
6209   (interactive "P")
6210   (setq arg (if arg (prefix-numeric-value arg) 0))
6211   (with-current-buffer gnus-article-buffer
6212     (widen)
6213     ;; Remove any old next/prev buttons.
6214     (when (gnus-visual-p 'page-marker)
6215       (let ((inhibit-read-only t))
6216         (gnus-remove-text-with-property 'gnus-prev)
6217         (gnus-remove-text-with-property 'gnus-next)))
6218     (let (st nd pt)
6219       (when (save-excursion
6220               (cond ((< arg 0)
6221                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6222                          (prog1
6223                              (setq nd (match-beginning 0)
6224                                    pt nd)
6225                            (when (re-search-backward page-delimiter nil t)
6226                              (setq st (match-end 0))))
6227                        (when (re-search-forward page-delimiter nil t)
6228                          (setq nd (match-beginning 0)
6229                                pt (point-min)))))
6230                     ((> arg 0)
6231                      (if (re-search-forward page-delimiter nil 'move arg)
6232                          (prog1
6233                              (setq st (match-end 0)
6234                                    pt st)
6235                            (when (re-search-forward page-delimiter nil t)
6236                              (setq nd (match-beginning 0))))
6237                        (when (re-search-backward page-delimiter nil t)
6238                          (setq st (match-end 0)
6239                                pt (point-max)))))
6240                     (t
6241                      (when (re-search-backward page-delimiter nil t)
6242                        (goto-char (setq st (match-end 0))))
6243                      (when (re-search-forward page-delimiter nil t)
6244                        (setq nd (match-beginning 0)))
6245                      (or st nd))))
6246         (setq gnus-page-broken t)
6247         (when pt (goto-char pt))
6248         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6249         (when (gnus-visual-p 'page-marker)
6250           (save-excursion
6251             (when nd
6252               (goto-char nd)
6253               (gnus-insert-next-page-button))
6254             (when st
6255               (goto-char st)
6256               (gnus-insert-prev-page-button))))))))
6257
6258 ;; Article mode commands
6259
6260 (defun gnus-article-goto-next-page ()
6261   "Show the next page of the article."
6262   (interactive)
6263   (when (gnus-article-next-page)
6264     (goto-char (point-min))
6265     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6266
6267
6268 (defun gnus-article-goto-prev-page ()
6269   "Show the previous page of the article."
6270   (interactive)
6271   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6272       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6273     (gnus-article-prev-page nil)))
6274
6275 ;; This is cleaner but currently breaks `gnus-pick-mode':
6276 ;;
6277 ;; (defun gnus-article-goto-next-page ()
6278 ;;   "Show the next page of the article."
6279 ;;   (interactive)
6280 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6281 ;;     (gnus-summary-next-page)))
6282 ;;
6283 ;; (defun gnus-article-goto-prev-page ()
6284 ;;   "Show the next page of the article."
6285 ;;   (interactive)
6286 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6287 ;;     (gnus-summary-prev-page)))
6288
6289 (defun gnus-article-next-page (&optional lines)
6290   "Show the next page of the current article.
6291 If end of article, return non-nil.  Otherwise return nil.
6292 Argument LINES specifies lines to be scrolled up."
6293   (interactive "p")
6294   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6295   (if (and (not (and gnus-article-over-scroll
6296                      (> (count-lines (window-start) (point-max))
6297                         (if (featurep 'xemacs)
6298                             (or lines (1- (window-height)))
6299                           (+ (or lines (1- (window-height))) scroll-margin)))))
6300            (save-excursion
6301              (end-of-line)
6302              (and (pos-visible-in-window-p)     ;Not continuation line.
6303                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6304       ;; Nothing in this page.
6305       (if (or (not gnus-page-broken)
6306               (save-excursion
6307                 (save-restriction
6308                   (widen)
6309                   (forward-line)
6310                   (eobp)))) ;Real end-of-buffer?
6311           (progn
6312             (when gnus-article-over-scroll
6313               (gnus-article-next-page-1 lines))
6314             t)                  ;Nothing more.
6315         (gnus-narrow-to-page 1)         ;Go to next page.
6316         nil)
6317     ;; More in this page.
6318     (gnus-article-next-page-1 lines)
6319     nil))
6320
6321 (defun gnus-article-beginning-of-window ()
6322   "Move point to the beginning of the window.
6323 In Emacs, the point is placed at the line number which `scroll-margin'
6324 specifies."
6325   (if (featurep 'xemacs)
6326       (move-to-window-line 0)
6327     ;; There is an obscure bug in Emacs that makes it impossible to
6328     ;; scroll past big pictures in the article buffer.  Try to fix
6329     ;; this by adding a sanity check by counting the lines visible.
6330     (when (> (count-lines (window-start) (window-end)) 30)
6331       (move-to-window-line
6332        (min (max 0 scroll-margin)
6333             (max 1 (- (window-height)
6334                       (if mode-line-format 1 0)
6335                       (if header-line-format 1 0)
6336                       2)))))))
6337
6338 (defun gnus-article-next-page-1 (lines)
6339   (condition-case ()
6340       (let ((scroll-in-place nil))
6341         (scroll-up lines))
6342     (end-of-buffer
6343      ;; Long lines may cause an end-of-buffer error.
6344      (goto-char (point-max))))
6345   (gnus-article-beginning-of-window))
6346
6347 (defun gnus-article-prev-page (&optional lines)
6348   "Show previous page of current article.
6349 Argument LINES specifies lines to be scrolled down."
6350   (interactive "p")
6351   (move-to-window-line 0)
6352   (if (and gnus-page-broken
6353            (bobp)
6354            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6355       (progn
6356         (gnus-narrow-to-page -1)        ;Go to previous page.
6357         (goto-char (point-max))
6358         (recenter (if gnus-article-over-scroll
6359                       (if lines
6360                           (max (if (featurep 'xemacs)
6361                                    lines
6362                                  (+ lines scroll-margin))
6363                                3)
6364                         (- (window-height) 2))
6365                     -1)))
6366     (prog1
6367         (condition-case ()
6368             (let ((scroll-in-place nil))
6369               (scroll-down lines))
6370           (beginning-of-buffer
6371            (goto-char (point-min))))
6372       (gnus-article-beginning-of-window))))
6373
6374 (defun gnus-article-only-boring-p ()
6375   "Decide whether there is only boring text remaining in the article.
6376 Something \"interesting\" is a word of at least two letters that does
6377 not have a face in `gnus-article-boring-faces'."
6378   (when (and gnus-article-skip-boring
6379              (boundp 'gnus-article-boring-faces)
6380              (symbol-value 'gnus-article-boring-faces))
6381     (save-excursion
6382       (let ((inhibit-point-motion-hooks t))
6383         (catch 'only-boring
6384           (while (re-search-forward "\\b\\w\\w" nil t)
6385             (forward-char -1)
6386             (when (not (gnus-intersection
6387                         (gnus-faces-at (point))
6388                         (symbol-value 'gnus-article-boring-faces)))
6389               (throw 'only-boring nil)))
6390           (throw 'only-boring t))))))
6391
6392 (defun gnus-article-refer-article ()
6393   "Read article specified by message-id around point."
6394   (interactive)
6395   (save-excursion
6396     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6397     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6398     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6399         (let ((msg-id (concat "<" (match-string 0) ">")))
6400           (set-buffer gnus-summary-buffer)
6401           (gnus-summary-refer-article msg-id))
6402       (error "No references around point"))))
6403
6404 (defun gnus-article-show-summary ()
6405   "Reconfigure windows to show summary buffer."
6406   (interactive)
6407   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6408       (error "There is no summary buffer for this article buffer")
6409     (gnus-article-set-globals)
6410     (gnus-configure-windows 'article)
6411     (gnus-summary-goto-subject gnus-current-article)
6412     (gnus-summary-position-point)))
6413
6414 (defun gnus-article-describe-briefly ()
6415   "Describe article mode commands briefly."
6416   (interactive)
6417   (gnus-message 6 "%s" (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")))
6418
6419 (defun gnus-article-check-buffer ()
6420   "Beep if not in an article buffer."
6421   (unless (equal major-mode 'gnus-article-mode)
6422     (error "Command invoked outside of a Gnus article buffer")))
6423
6424 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6425   "Read a summary buffer key sequence and execute it from the article buffer."
6426   (interactive "P")
6427   (gnus-article-check-buffer)
6428   (let ((nosaves
6429          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6430            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6431            "=" "^" "\M-^" "|"))
6432         (nosave-but-article
6433          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6434            "An" "Ap" [?A (meta return)] [?A delete]))
6435         (nosave-in-article
6436          '("AS" "\C-d"))
6437         (up-to-top
6438          '("n" "Gn" "p" "Gp"))
6439         keys new-sum-point)
6440     (with-current-buffer gnus-article-current-summary
6441       (let (gnus-pick-mode)
6442         (setq unread-command-events (nconc unread-command-events
6443                                            (list (or key last-command-event)))
6444               keys (if (featurep 'xemacs)
6445                        (events-to-keys (read-key-sequence nil t))
6446                      (read-key-sequence nil t)))))
6447
6448     (message "")
6449
6450     (cond
6451      ((eq (aref keys (1- (length keys))) ?\C-h)
6452       (gnus-article-describe-bindings (substring keys 0 -1)))
6453      ((or (member keys nosaves)
6454           (member keys nosave-but-article)
6455           (member keys nosave-in-article))
6456       (let (func)
6457         (save-window-excursion
6458           (pop-to-buffer gnus-article-current-summary)
6459           ;; We disable the pick minor mode commands.
6460           (let (gnus-pick-mode)
6461             (setq func (key-binding keys t))))
6462         (if (or (not func)
6463                 (numberp func))
6464             (ding)
6465           (unless (member keys nosave-in-article)
6466             (set-buffer gnus-article-current-summary))
6467           (call-interactively func)
6468           (setq new-sum-point (point)))
6469         (when (member keys nosave-but-article)
6470           (pop-to-buffer gnus-article-buffer))))
6471      (t
6472       ;; These commands should restore window configuration.
6473       (let ((obuf (current-buffer))
6474             (owin (current-window-configuration))
6475             win func in-buffer selected new-sum-start new-sum-hscroll err)
6476         (cond (not-restore-window
6477                (pop-to-buffer gnus-article-current-summary)
6478                (setq win (selected-window)))
6479               ((setq win (get-buffer-window gnus-article-current-summary))
6480                (select-window win))
6481               (t
6482                (let ((summary-buffer gnus-article-current-summary))
6483                  (gnus-configure-windows 'article)
6484                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6485                    (let ((gnus-buffer-configuration
6486                           '((article ((vertical 1.0
6487                                                 (summary 0.25 point)
6488                                                 (article 1.0)))))))
6489                      (gnus-configure-windows 'article))
6490                    (setq win (get-buffer-window summary-buffer 'visible)))
6491                  (gnus-select-frame-set-input-focus (window-frame win))
6492                  (select-window win))))
6493         (setq in-buffer (current-buffer))
6494         ;; We disable the pick minor mode commands.
6495         (if (and (setq func (let (gnus-pick-mode)
6496                               (key-binding keys t)))
6497                  (functionp func)
6498                  (condition-case code
6499                      (progn
6500                        (call-interactively func)
6501                        t)
6502                    (error
6503                     (setq err code)
6504                     nil)))
6505             (progn
6506               (when (eq win (selected-window))
6507                 (setq new-sum-point (point)
6508                       new-sum-start (window-start win)
6509                       new-sum-hscroll (window-hscroll win)))
6510               (when (or (eq in-buffer (current-buffer))
6511                         (when (eq obuf (current-buffer))
6512                           (set-buffer in-buffer)
6513                           t))
6514                 (setq selected (gnus-summary-select-article))
6515                 (set-buffer obuf)
6516                 (unless not-restore-window
6517                   (set-window-configuration owin))
6518                 (when (and (eq selected 'old)
6519                            new-sum-point)
6520                   (set-window-start (get-buffer-window (current-buffer))
6521                                     1)
6522                   (set-window-point (get-buffer-window (current-buffer))
6523                                     (if (article-goto-body)
6524                                         (1- (point))
6525                                       (point))))
6526                 (when (and (not not-restore-window)
6527                            new-sum-point
6528                            (window-live-p win)
6529                            (with-current-buffer (window-buffer win)
6530                              (eq major-mode 'gnus-summary-mode)))
6531                   (set-window-point win new-sum-point)
6532                   (set-window-start win new-sum-start)
6533                   (set-window-hscroll win new-sum-hscroll))))
6534           (set-window-configuration owin)
6535           (if err
6536               (signal (car err) (cdr err))
6537             (ding))))))))
6538
6539 (defun gnus-article-read-summary-send-keys ()
6540   (interactive)
6541   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6542     (gnus-article-read-summary-keys)))
6543
6544 (defun gnus-article-describe-key (key)
6545   "Display documentation of the function invoked by KEY.
6546 KEY is a string or a vector."
6547   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6548                        (read-key-sequence "Describe key: "))))
6549   (gnus-article-check-buffer)
6550   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6551                                   gnus-article-read-summary-send-keys))
6552       (with-current-buffer gnus-article-current-summary
6553         (setq unread-command-events
6554               (if (featurep 'xemacs)
6555                   (append key nil)
6556                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6557                                         (list 'meta (- x 128))
6558                                       x))
6559                         key)))
6560         (let ((cursor-in-echo-area t)
6561               gnus-pick-mode)
6562           (describe-key (read-key-sequence nil t))))
6563     (describe-key key)))
6564
6565 (defun gnus-article-describe-key-briefly (key &optional insert)
6566   "Display documentation of the function invoked by KEY.
6567 KEY is a string or a vector."
6568   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6569                        (read-key-sequence "Describe key: "))
6570                      current-prefix-arg))
6571   (gnus-article-check-buffer)
6572   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6573                                   gnus-article-read-summary-send-keys))
6574       (with-current-buffer gnus-article-current-summary
6575         (setq unread-command-events
6576               (if (featurep 'xemacs)
6577                   (append key nil)
6578                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6579                                         (list 'meta (- x 128))
6580                                       x))
6581                         key)))
6582         (let ((cursor-in-echo-area t)
6583               gnus-pick-mode)
6584           (describe-key-briefly (read-key-sequence nil t) insert)))
6585     (describe-key-briefly key insert)))
6586
6587 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6588 (defvar gnus-agent-summary-mode)
6589 (defvar gnus-draft-mode)
6590 ;; Calling help-buffer will autoload help-mode.
6591 (defvar help-xref-stack-item)
6592 ;; Emacs 22 doesn't load it in the batch mode.
6593 (eval-when-compile
6594   (autoload 'help-buffer "help-mode"))
6595
6596 (defun gnus-article-describe-bindings (&optional prefix)
6597   "Show a list of all defined keys, and their definitions.
6598 The optional argument PREFIX, if non-nil, should be a key sequence;
6599 then we display only bindings that start with that prefix."
6600   (interactive)
6601   (gnus-article-check-buffer)
6602   (let ((keymap (copy-keymap gnus-article-mode-map))
6603         (map (copy-keymap gnus-article-send-map))
6604         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6605         parent agent draft)
6606     (define-key keymap "S" map)
6607     (define-key map [t] nil)
6608     (with-current-buffer gnus-article-current-summary
6609       (set-keymap-parent
6610        keymap
6611        (if (setq parent (keymap-parent gnus-article-mode-map))
6612            (prog1
6613                (setq parent (copy-keymap parent))
6614              (set-keymap-parent parent (current-local-map)))
6615          (current-local-map)))
6616       (set-keymap-parent map (key-binding "S"))
6617       (let (key def gnus-pick-mode)
6618         (while sumkeys
6619           (setq key (pop sumkeys))
6620           (cond ((and (vectorp key) (= (length key) 1)
6621                       (consp (setq def (aref key 0)))
6622                       (numberp (car def)) (numberp (cdr def)))
6623                  (when (< (max (car def) (cdr def)) 128)
6624                    (setq sumkeys
6625                          (append (mapcar
6626                                   #'vector
6627                                   (nreverse (gnus-uncompress-range def)))
6628                                  sumkeys))))
6629                 ((setq def (key-binding key))
6630                  (unless (eq def 'undefined)
6631                    (define-key keymap key def))))))
6632       (when (boundp 'gnus-agent-summary-mode)
6633         (setq agent gnus-agent-summary-mode))
6634       (when (boundp 'gnus-draft-mode)
6635         (setq draft gnus-draft-mode)))
6636     (with-temp-buffer
6637       (use-local-map keymap)
6638       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6639       (set (make-local-variable 'gnus-draft-mode) draft)
6640       (describe-bindings prefix))
6641     (let ((item `((lambda (prefix)
6642                     (with-current-buffer ,(current-buffer)
6643                       (gnus-article-describe-bindings prefix)))
6644                   ,prefix)))
6645       (with-current-buffer (let (help-xref-following) (help-buffer))
6646         (setq help-xref-stack-item item)))))
6647
6648 (defun gnus-article-reply-with-original (&optional wide)
6649   "Start composing a reply mail to the current message.
6650 The text in the region will be yanked.  If the region isn't active,
6651 the entire article will be yanked."
6652   (interactive)
6653   (let ((article (cdr gnus-article-current))
6654         contents)
6655     (if (not (gnus-region-active-p))
6656         (with-current-buffer gnus-summary-buffer
6657           (gnus-summary-reply (list (list article)) wide))
6658       (setq contents (buffer-substring (point) (mark t)))
6659       ;; Deactivate active regions.
6660       (when (and (boundp 'transient-mark-mode)
6661                  transient-mark-mode)
6662         (setq mark-active nil))
6663       (with-current-buffer gnus-summary-buffer
6664         (gnus-summary-reply
6665          (list (list article contents)) wide)))))
6666
6667 (defun gnus-article-wide-reply-with-original ()
6668   "Start composing a wide reply mail to the current message.
6669 The text in the region will be yanked.  If the region isn't active,
6670 the entire article will be yanked."
6671   (interactive)
6672   (gnus-article-reply-with-original t))
6673
6674 (defun gnus-article-followup-with-original ()
6675   "Compose a followup to the current article.
6676 The text in the region will be yanked.  If the region isn't active,
6677 the entire article will be yanked."
6678   (interactive)
6679   (let ((article (cdr gnus-article-current))
6680         contents)
6681       (if (not (gnus-region-active-p))
6682           (with-current-buffer gnus-summary-buffer
6683             (gnus-summary-followup (list (list article))))
6684         (setq contents (buffer-substring (point) (mark t)))
6685         ;; Deactivate active regions.
6686         (when (and (boundp 'transient-mark-mode)
6687                    transient-mark-mode)
6688           (setq mark-active nil))
6689         (with-current-buffer gnus-summary-buffer
6690           (gnus-summary-followup
6691            (list (list article contents)))))))
6692
6693 (defun gnus-article-hide (&optional arg force)
6694   "Hide all the gruft in the current article.
6695 This means that signatures, cited text and (some) headers will be
6696 hidden.
6697 If given a prefix, show the hidden text instead."
6698   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6699   (gnus-article-hide-headers arg)
6700   (gnus-article-hide-list-identifiers arg)
6701   (gnus-article-hide-citation-maybe arg force)
6702   (gnus-article-hide-signature arg))
6703
6704 (defun gnus-article-maybe-highlight ()
6705   "Do some article highlighting if article highlighting is requested."
6706   (when (gnus-visual-p 'article-highlight 'highlight)
6707     (gnus-article-highlight-some)))
6708
6709 (defun gnus-check-group-server ()
6710   ;; Make sure the connection to the server is alive.
6711   (unless (gnus-server-opened
6712            (gnus-find-method-for-group gnus-newsgroup-name))
6713     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6714     (gnus-request-group gnus-newsgroup-name t)))
6715
6716 (eval-when-compile
6717   (autoload 'nneething-get-file-name "nneething"))
6718
6719 (defun gnus-request-article-this-buffer (article group)
6720   "Get an article and insert it into this buffer."
6721   (let (do-update-line sparse-header)
6722     (prog1
6723         (save-excursion
6724           (erase-buffer)
6725           (gnus-kill-all-overlays)
6726           (setq group (or group gnus-newsgroup-name))
6727
6728           ;; Using `gnus-request-article' directly will insert the article into
6729           ;; `nntp-server-buffer' - so we'll save some time by not having to
6730           ;; copy it from the server buffer into the article buffer.
6731
6732           ;; We only request an article by message-id when we do not have the
6733           ;; headers for it, so we'll have to get those.
6734           (when (stringp article)
6735             (gnus-read-header article))
6736
6737           ;; If the article number is negative, that means that this article
6738           ;; doesn't belong in this newsgroup (possibly), so we find its
6739           ;; message-id and request it by id instead of number.
6740           (when (and (numberp article)
6741                      gnus-summary-buffer
6742                      (get-buffer gnus-summary-buffer)
6743                      (gnus-buffer-exists-p gnus-summary-buffer))
6744             (with-current-buffer gnus-summary-buffer
6745               (let ((header (gnus-summary-article-header article)))
6746                 (when (< article 0)
6747                   (cond
6748                    ((memq article gnus-newsgroup-sparse)
6749                     ;; This is a sparse gap article.
6750                     (setq do-update-line article)
6751                     (setq article (mail-header-id header))
6752                     (setq sparse-header (gnus-read-header article))
6753                     (setq gnus-newsgroup-sparse
6754                           (delq article gnus-newsgroup-sparse)))
6755                    ((vectorp header)
6756                     ;; It's a real article.
6757                     (setq article (mail-header-id header)))
6758                    (t
6759                     ;; It is an extracted pseudo-article.
6760                     (setq article 'pseudo)
6761                     (gnus-request-pseudo-article header))))
6762
6763                 (let ((method (gnus-find-method-for-group
6764                                gnus-newsgroup-name)))
6765                   (when (and (eq (car method) 'nneething)
6766                              (vectorp header))
6767                     (let ((dir (nneething-get-file-name
6768                                 (mail-header-id header))))
6769                       (when (and (stringp dir)
6770                                  (file-directory-p dir))
6771                         (setq article 'nneething)
6772                         (gnus-group-enter-directory dir))))))))
6773
6774           (cond
6775            ;; Refuse to select canceled articles.
6776            ((and (numberp article)
6777                  gnus-summary-buffer
6778                  (get-buffer gnus-summary-buffer)
6779                  (gnus-buffer-exists-p gnus-summary-buffer)
6780                  (eq (cdr (with-current-buffer gnus-summary-buffer
6781                             (assq article gnus-newsgroup-reads)))
6782                      gnus-canceled-mark))
6783             nil)
6784            ;; We first check `gnus-original-article-buffer'.
6785            ((and (get-buffer gnus-original-article-buffer)
6786                  (numberp article)
6787                  (with-current-buffer gnus-original-article-buffer
6788                    (and (equal (car gnus-original-article) group)
6789                         (eq (cdr gnus-original-article) article))))
6790             ;; `insert-buffer-substring' would incorrectly use the
6791             ;; equivalent of string-make-multibyte which amount to decoding
6792             ;; with locale-coding-system, causing failure of
6793             ;; subsequent decoding.
6794             (insert (mm-string-to-multibyte
6795                      (with-current-buffer gnus-original-article-buffer
6796                        (buffer-substring (point-min) (point-max)))))
6797             'article)
6798            ;; Check the backlog.
6799            ((and gnus-keep-backlog
6800                  (gnus-backlog-request-article group article (current-buffer)))
6801             'article)
6802            ;; Check asynchronous pre-fetch.
6803            ((gnus-async-request-fetched-article group article (current-buffer))
6804             (gnus-async-prefetch-next group article gnus-summary-buffer)
6805             (when (and (numberp article) gnus-keep-backlog)
6806               (gnus-backlog-enter-article group article (current-buffer)))
6807             'article)
6808            ;; Check the cache.
6809            ((and gnus-use-cache
6810                  (numberp article)
6811                  (gnus-cache-request-article article group))
6812             'article)
6813            ;; Check the agent cache.
6814            ((gnus-agent-request-article article group)
6815             'article)
6816            ;; Get the article and put into the article buffer.
6817            ((or (stringp article)
6818                 (numberp article))
6819             (let ((gnus-override-method gnus-override-method)
6820                   (methods (and (stringp article)
6821                                 gnus-refer-article-method))
6822                   (backend (car (gnus-find-method-for-group
6823                                  gnus-newsgroup-name)))
6824                   result
6825                   (inhibit-read-only t))
6826               (if (or (not (listp methods))
6827                       (and (symbolp (car methods))
6828                            (assq (car methods) nnoo-definition-alist)))
6829                   (setq methods (list methods)))
6830               (when (and (null gnus-override-method)
6831                          methods)
6832                 (setq gnus-override-method (pop methods)))
6833               (while (not result)
6834                 (when (eq gnus-override-method 'current)
6835                   (setq gnus-override-method
6836                         (with-current-buffer gnus-summary-buffer
6837                           gnus-current-select-method)))
6838                 (erase-buffer)
6839                 (gnus-kill-all-overlays)
6840                 (let ((gnus-newsgroup-name group))
6841                   (gnus-check-group-server))
6842                 (cond
6843                  ((gnus-request-article article group (current-buffer))
6844                   (when (numberp article)
6845                     (gnus-async-prefetch-next group article
6846                                               gnus-summary-buffer)
6847                     (when gnus-keep-backlog
6848                       (gnus-backlog-enter-article
6849                        group article (current-buffer))))
6850                   (setq result 'article))
6851                  (methods
6852                   (setq gnus-override-method (pop methods)))
6853                  ((not (string-match "^400 "
6854                                      (nnheader-get-report backend)))
6855                   ;; If we get 400 server disconnect, reconnect and
6856                   ;; retry; otherwise, assume the article has expired.
6857                   (setq result 'done))))
6858               (and (eq result 'article) 'article)))
6859            ;; It was a pseudo.
6860            (t article)))
6861
6862       ;; Associate this article with the current summary buffer.
6863       (setq gnus-article-current-summary gnus-summary-buffer)
6864
6865       ;; Take the article from the original article buffer
6866       ;; and place it in the buffer it's supposed to be in.
6867       (when (and (get-buffer gnus-article-buffer)
6868                  (equal (buffer-name (current-buffer))
6869                         (buffer-name (get-buffer gnus-article-buffer))))
6870         (save-excursion
6871           (if (get-buffer gnus-original-article-buffer)
6872               (set-buffer gnus-original-article-buffer)
6873             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6874             (buffer-disable-undo)
6875             (setq major-mode 'gnus-original-article-mode)
6876             (setq buffer-read-only t))
6877           (let ((inhibit-read-only t))
6878             (erase-buffer)
6879             (insert-buffer-substring gnus-article-buffer))
6880           (setq gnus-original-article (cons group article)))
6881
6882         ;; Decode charsets.
6883         (run-hooks 'gnus-article-decode-hook)
6884         ;; Mark article as decoded or not.
6885         (setq gnus-article-decoded-p gnus-article-decode-hook))
6886
6887       ;; Update sparse articles.
6888       (when (and do-update-line
6889                  (or (numberp article)
6890                      (stringp article)))
6891         (let ((buf (current-buffer)))
6892           (set-buffer gnus-summary-buffer)
6893           (gnus-summary-update-article do-update-line sparse-header)
6894           (gnus-summary-goto-subject do-update-line nil t)
6895           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6896                             (point))
6897           (set-buffer buf))))))
6898
6899 ;;;
6900 ;;; Article editing
6901 ;;;
6902
6903 (defcustom gnus-article-edit-mode-hook nil
6904   "Hook run in article edit mode buffers."
6905   :group 'gnus-article-various
6906   :type 'hook)
6907
6908 (defvar gnus-article-edit-done-function nil)
6909
6910 (defvar gnus-article-edit-mode-map nil)
6911
6912 ;; Should we be using derived.el for this?
6913 (unless gnus-article-edit-mode-map
6914   (setq gnus-article-edit-mode-map (make-keymap))
6915   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6916
6917   (gnus-define-keys gnus-article-edit-mode-map
6918     "\C-c?"    describe-mode
6919     "\C-c\C-c" gnus-article-edit-done
6920     "\C-c\C-k" gnus-article-edit-exit
6921     "\C-c\C-f\C-t" message-goto-to
6922     "\C-c\C-f\C-o" message-goto-from
6923     "\C-c\C-f\C-b" message-goto-bcc
6924     ;;"\C-c\C-f\C-w" message-goto-fcc
6925     "\C-c\C-f\C-c" message-goto-cc
6926     "\C-c\C-f\C-s" message-goto-subject
6927     "\C-c\C-f\C-r" message-goto-reply-to
6928     "\C-c\C-f\C-n" message-goto-newsgroups
6929     "\C-c\C-f\C-d" message-goto-distribution
6930     "\C-c\C-f\C-f" message-goto-followup-to
6931     "\C-c\C-f\C-m" message-goto-mail-followup-to
6932     "\C-c\C-f\C-k" message-goto-keywords
6933     "\C-c\C-f\C-u" message-goto-summary
6934     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6935     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6936     "\C-c\C-b" message-goto-body
6937     "\C-c\C-i" message-goto-signature
6938
6939     "\C-c\C-t" message-insert-to
6940     "\C-c\C-n" message-insert-newsgroups
6941     "\C-c\C-o" message-sort-headers
6942     "\C-c\C-e" message-elide-region
6943     "\C-c\C-v" message-delete-not-region
6944     "\C-c\C-z" message-kill-to-signature
6945     "\M-\r" message-newline-and-reformat
6946     "\C-c\C-a" mml-attach-file
6947     "\C-a" message-beginning-of-line
6948     "\t" message-tab
6949     "\M-;" comment-region)
6950
6951   (gnus-define-keys (gnus-article-edit-wash-map
6952                      "\C-c\C-w" gnus-article-edit-mode-map)
6953     "f" gnus-article-edit-full-stops))
6954
6955 (easy-menu-define
6956   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6957   '("Field"
6958     ["Fetch To" message-insert-to t]
6959     ["Fetch Newsgroups" message-insert-newsgroups t]
6960     "----"
6961     ["To" message-goto-to t]
6962     ["From" message-goto-from t]
6963     ["Subject" message-goto-subject t]
6964     ["Cc" message-goto-cc t]
6965     ["Reply-To" message-goto-reply-to t]
6966     ["Summary" message-goto-summary t]
6967     ["Keywords" message-goto-keywords t]
6968     ["Newsgroups" message-goto-newsgroups t]
6969     ["Followup-To" message-goto-followup-to t]
6970     ["Mail-Followup-To" message-goto-mail-followup-to t]
6971     ["Distribution" message-goto-distribution t]
6972     ["Body" message-goto-body t]
6973     ["Signature" message-goto-signature t]))
6974
6975 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6976   "Major mode for editing articles.
6977 This is an extended text-mode.
6978
6979 \\{gnus-article-edit-mode-map}"
6980   (make-local-variable 'gnus-article-edit-done-function)
6981   (make-local-variable 'gnus-prev-winconf)
6982   (set (make-local-variable 'font-lock-defaults)
6983        '(message-font-lock-keywords t))
6984   (set (make-local-variable 'mail-header-separator) "")
6985   (set (make-local-variable 'gnus-article-edit-mode) t)
6986   (easy-menu-add message-mode-field-menu message-mode-map)
6987   (mml-mode)
6988   (setq buffer-read-only nil)
6989   (buffer-enable-undo)
6990   (widen))
6991
6992 (defun gnus-article-edit (&optional force)
6993   "Edit the current article.
6994 This will have permanent effect only in mail groups.
6995 If FORCE is non-nil, allow editing of articles even in read-only
6996 groups."
6997   (interactive "P")
6998   (when (and (not force)
6999              (gnus-group-read-only-p))
7000     (error "The current newsgroup does not support article editing"))
7001   (gnus-article-date-original)
7002   (gnus-article-edit-article
7003    'ignore
7004    `(lambda (no-highlight)
7005       'ignore
7006       (gnus-summary-edit-article-done
7007        ,(or (mail-header-references gnus-current-headers) "")
7008        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7009
7010 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7011   "Start editing the contents of the current article buffer."
7012   (let ((winconf (current-window-configuration)))
7013     (set-buffer gnus-article-buffer)
7014     (let ((message-auto-save-directory
7015            ;; Don't associate the article buffer with a draft file.
7016            nil))
7017       (gnus-article-edit-mode))
7018     (funcall start-func)
7019     (set-buffer-modified-p nil)
7020     (gnus-configure-windows 'edit-article)
7021     (setq gnus-article-edit-done-function exit-func)
7022     (setq gnus-prev-winconf winconf)
7023     (unless quiet
7024       (gnus-message 6 "C-c C-c to end edits"))))
7025
7026 (defun gnus-article-edit-done (&optional arg)
7027   "Update the article edits and exit."
7028   (interactive "P")
7029   (let ((func gnus-article-edit-done-function)
7030         (buf (current-buffer))
7031         (start (window-start))
7032         (p (point))
7033         (winconf gnus-prev-winconf))
7034     (widen) ;; Widen it in case that users narrowed the buffer.
7035     (funcall func arg)
7036     (set-buffer buf)
7037     ;; The cache and backlog have to be flushed somewhat.
7038     (when gnus-keep-backlog
7039       (gnus-backlog-remove-article
7040        (car gnus-article-current) (cdr gnus-article-current)))
7041     ;; Flush original article as well.
7042     (gnus-flush-original-article-buffer)
7043     (when gnus-use-cache
7044       (gnus-cache-update-article
7045        (car gnus-article-current) (cdr gnus-article-current)))
7046     ;; We remove all text props from the article buffer.
7047     (kill-all-local-variables)
7048     (set-text-properties (point-min) (point-max) nil)
7049     (gnus-article-mode)
7050     (set-window-configuration winconf)
7051     (set-buffer buf)
7052     (set-window-start (get-buffer-window buf) start)
7053     (set-window-point (get-buffer-window buf) (point)))
7054   (gnus-summary-show-article))
7055
7056 (defun gnus-flush-original-article-buffer ()
7057   (when (get-buffer gnus-original-article-buffer)
7058     (with-current-buffer gnus-original-article-buffer
7059       (setq gnus-original-article nil))))
7060
7061 (defun gnus-article-edit-exit ()
7062   "Exit the article editing without updating."
7063   (interactive)
7064   (when (or (not (buffer-modified-p))
7065             (yes-or-no-p "Article modified; kill anyway? "))
7066     (let ((curbuf (current-buffer))
7067           (p (point))
7068           (window-start (window-start)))
7069       (erase-buffer)
7070       (if (gnus-buffer-live-p gnus-original-article-buffer)
7071           (insert-buffer-substring gnus-original-article-buffer))
7072       (let ((winconf gnus-prev-winconf))
7073         (kill-all-local-variables)
7074         (gnus-article-mode)
7075         (set-window-configuration winconf)
7076         ;; Tippy-toe some to make sure that point remains where it was.
7077         (save-current-buffer
7078           (set-buffer curbuf)
7079           (set-window-start (get-buffer-window (current-buffer)) window-start)
7080           (goto-char p))))
7081     (gnus-summary-show-article)))
7082
7083 (defun gnus-article-edit-full-stops ()
7084   "Interactively repair spacing at end of sentences."
7085   (interactive)
7086   (save-excursion
7087     (goto-char (point-min))
7088     (search-forward-regexp "^$" nil t)
7089     (let ((case-fold-search nil))
7090       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7091
7092 ;;;
7093 ;;; Article highlights
7094 ;;;
7095
7096 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7097
7098 ;;; Internal Variables:
7099
7100 (defcustom gnus-button-url-regexp
7101   (concat
7102    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7103    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7104    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7105    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7106        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7107              (punct "!?:;.,"))
7108          (concat
7109           "\\(?:"
7110           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7111           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7112           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7113           "\\|"
7114           "[" chars punct     "]+" "[" chars "]"
7115           "\\)"))
7116      (concat ;; XEmacs 21.4 doesn't support POSIX.
7117       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7118       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7119    "\\)")
7120   "Regular expression that matches URLs."
7121   :group 'gnus-article-buttons
7122   :type 'regexp)
7123
7124 (defcustom gnus-button-valid-fqdn-regexp
7125   message-valid-fqdn-regexp
7126   "Regular expression that matches a valid FQDN."
7127   :version "22.1"
7128   :group 'gnus-article-buttons
7129   :type 'regexp)
7130
7131 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7132 (defcustom gnus-button-valid-localpart-regexp
7133   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7134   "Regular expression that matches a localpart of mail addresses or MIDs."
7135   :version "22.1"
7136   :group 'gnus-article-buttons
7137   :type 'regexp)
7138
7139 (defcustom gnus-button-man-handler 'manual-entry
7140   "Function to use for displaying man pages.
7141 The function must take at least one argument with a string naming the
7142 man page."
7143   :version "22.1"
7144   :type '(choice (function-item :tag "Man" manual-entry)
7145                  (function-item :tag "Woman" woman)
7146                  (function :tag "Other"))
7147   :group 'gnus-article-buttons)
7148
7149 (defcustom gnus-button-mid-or-mail-regexp
7150   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7151           gnus-button-valid-fqdn-regexp
7152           ">?\\)\\b")
7153   "Regular expression that matches a message ID or a mail address."
7154   :version "22.1"
7155   :group 'gnus-article-buttons
7156   :type 'regexp)
7157
7158 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7159   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7160 Strings like this can be either a message ID or a mail address.  If it is one
7161 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7162 message ID or a mail address, respectively.  If this variable is set to the
7163 symbol `ask', always query the user what do do.  If it is a function, this
7164 function will be called with the string as its only argument.  The function
7165 must return `mid', `mail', `invalid' or `ask'."
7166   :version "22.1"
7167   :group 'gnus-article-buttons
7168   :type '(choice (function-item :tag "Heuristic function"
7169                                 gnus-button-mid-or-mail-heuristic)
7170                  (const ask)
7171                  (const mid)
7172                  (const mail)))
7173
7174 (defcustom gnus-button-mid-or-mail-heuristic-alist
7175   '((-10.0 . ".+\\$.+@")
7176     (-10.0 . "#")
7177     (-10.0 . "\\*")
7178     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7179     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7180     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7181     (-1.0  . "^[^a-z]+@")
7182     ;;
7183     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7184     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7185     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7186     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7187     ;;
7188     (-2.0  . "^[0-9]")
7189     (-1.0  . "^[0-9][0-9]")
7190     ;;
7191     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7192     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7193     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7194     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7195     ;;
7196     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7197     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7198     ;;       "[0-9]{8,}.*\@"
7199     (-3.0
7200      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7201     ;; "[0-9]{12,}.*\@"
7202     ;; compensation for TDMA dated mail addresses:
7203     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7204     ;;
7205     (-20.0 . "\\.fsf@") ;; Gnus
7206     (-20.0 . "^slrn")
7207     (-20.0 . "^Pine")
7208     (-20.0 . "^alpine\\.")
7209     (-20.0 . "_-_") ;; Subject change in thread
7210     ;;
7211     (-20.0 . "\\.ln@") ;; leafnode
7212     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7213     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7214     ;;
7215     ;; (5.0 . "") ;; $local_part_len <= 7
7216     (10.0  . "^[^0-9]+@")
7217     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7218     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7219     (3.0   . "\@stud")
7220     ;;
7221     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7222     ;;
7223     (0.5   . "^[A-Z][a-z]")
7224     (0.5   . "^[A-Z][a-z][a-z]")
7225     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7226     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7227   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7228
7229 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7230 address.  The REGEXP is processed with `case-fold-search' set to nil."
7231   :version "22.1"
7232   :group 'gnus-article-buttons
7233   :type '(repeat (cons (number :tag "Rate")
7234                        (regexp :tag "Regexp"))))
7235
7236 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7237   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7238 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7239 address, `ask' if unsure and `invalid' if the string is invalid."
7240   (let ((case-fold-search nil)
7241         (list gnus-button-mid-or-mail-heuristic-alist)
7242         (result 0) rate regexp lpartlen elem)
7243     (setq lpartlen
7244           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7245     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7246     ;; Certain special cases...
7247     (when (string-match
7248            (concat
7249             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7250             "^[0-9]+\\.[0-9]+@compuserve\\|"
7251             "@public\\.gmane\\.org")
7252            mid-or-mail)
7253       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7254       (setq result 'mail))
7255     (when (string-match "@.*@\\| " mid-or-mail)
7256       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7257       (setq result 'invalid))
7258     ;; Nothing more to do, if result is not a number here...
7259     (when (numberp result)
7260       (while list
7261         (setq elem (car list)
7262               rate (car elem)
7263               regexp (cdr elem)
7264               list (cdr list))
7265         (when (string-match regexp mid-or-mail)
7266           (setq result (+ result rate))
7267           (gnus-message
7268            9 "`%s' matched `%s', rate `%s', result `%s'."
7269            mid-or-mail regexp rate result)))
7270       (when (<= lpartlen 7)
7271         (setq result (+ result 5.0))
7272         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7273                       mid-or-mail result))
7274       (when (>= lpartlen 12)
7275         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7276         (cond
7277          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7278           ;; Long local part should contain realname if e-mail address,
7279           ;; too many digits: message-id.
7280           ;; $score -= 5.0 + 0.1 * $local_part_len;
7281           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7282           (setq result (+ result rate))
7283           (gnus-message
7284            9 "Many digits in `%s', rate `%s', result `%s'."
7285            mid-or-mail rate result))
7286          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7287                         mid-or-mail)
7288           ;; Too few vowels [^aeiouy]{4,}.*\@
7289           (setq result (+ result -5.0))
7290           (gnus-message
7291            9 "Few vowels in `%s', rate `%s', result `%s'."
7292            mid-or-mail -5.0 result))
7293          (t
7294           (setq result (+ result 5.0))
7295           (gnus-message
7296            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7297     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7298     ;; Maybe we should make this a customizable alist: (condition . 'result)
7299     (cond
7300      ((symbolp result) result)
7301      ;; Now convert number into proper results:
7302      ((< result -10.0) 'mid)
7303      ((> result  10.0) 'mail)
7304      (t 'ask))))
7305
7306 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7307   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7308          (url-mid (concat "news" ":" mid-or-mail))
7309          (url-mailto (concat "mailto" ":" mid-or-mail)))
7310     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7311     (when (fboundp pref)
7312       (setq guessed
7313             ;; get rid of surrounding angles...
7314             (funcall pref
7315                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7316       (if (or (eq 'mid guessed) (eq 'mail guessed))
7317           (setq pref guessed)
7318         (setq pref 'ask)))
7319     (if (eq pref 'ask)
7320         (save-window-excursion
7321           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7322               (setq pref 'mail)
7323             (setq pref 'mid))))
7324     (cond ((eq pref 'mid)
7325            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7326            (gnus-button-handle-news url-mid))
7327           ((eq pref 'mail)
7328            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7329            (gnus-url-mailto url-mailto))
7330           (t (gnus-message 3 "Invalid string.")))))
7331
7332 (defun gnus-button-handle-custom (fun arg)
7333   "Call function FUN on argument ARG.
7334 Both FUN and ARG are supposed to be strings.  ARG will be passed
7335 as a symbol to FUN."
7336   (funcall (intern fun)
7337            (if (string-match "^customize-apropos" fun)
7338                arg
7339              (intern arg))))
7340
7341 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7342
7343 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7344 ;; stuff?
7345
7346 (defun gnus-button-handle-describe-function (url)
7347   "Call `describe-function' when pushing the corresponding URL button."
7348   (describe-function
7349    (intern
7350     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7351
7352 (defun gnus-button-handle-describe-variable (url)
7353   "Call `describe-variable' when pushing the corresponding URL button."
7354   (describe-variable
7355    (intern
7356     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7357
7358 (defun gnus-button-handle-symbol (url)
7359 "Display help on variable or function.
7360 Calls `describe-variable' or `describe-function'."
7361   (let ((sym (intern url)))
7362     (cond
7363      ((fboundp sym) (describe-function sym))
7364      ((boundp sym) (describe-variable sym))
7365      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7366
7367 (defun gnus-button-handle-describe-key (url)
7368   "Call `describe-key' when pushing the corresponding URL button."
7369   (let* ((key-string
7370           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7371          (keys (ignore-errors (eval `(kbd ,key-string)))))
7372     (if keys
7373         (describe-key keys)
7374       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7375
7376 (defun gnus-button-handle-apropos (url)
7377   "Call `apropos' when pushing the corresponding URL button."
7378   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7379
7380 (defun gnus-button-handle-apropos-command (url)
7381   "Call `apropos' when pushing the corresponding URL button."
7382   (apropos-command
7383    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7384
7385 (defun gnus-button-handle-apropos-variable (url)
7386   "Call `apropos' when pushing the corresponding URL button."
7387   (funcall
7388    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7389    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7390
7391 (defun gnus-button-handle-apropos-documentation (url)
7392   "Call `apropos' when pushing the corresponding URL button."
7393   (funcall
7394    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7395    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7396
7397 (defun gnus-button-handle-library (url)
7398   "Call `locate-library' when pushing the corresponding URL button."
7399   (gnus-message 9 "url=`%s'" url)
7400   (let* ((lib (locate-library url))
7401          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7402     (if (not lib)
7403         (gnus-message 1 "Cannot locale library `%s'." url)
7404       (find-file-read-only file))))
7405
7406 (defcustom gnus-button-man-level 5
7407   "*Integer that says how many man-related buttons Gnus will show.
7408 The higher the number, the more buttons will appear and the more false
7409 positives are possible.  Note that you can set this variable local to
7410 specific groups.  Setting it higher in Unix groups is probably a good idea.
7411 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7412 how to set variables in specific groups."
7413   :version "22.1"
7414   :group 'gnus-article-buttons
7415   :link '(custom-manual "(gnus)Group Parameters")
7416   :type 'integer)
7417
7418 (defcustom gnus-button-emacs-level 5
7419   "*Integer that says how many emacs-related buttons Gnus will show.
7420 The higher the number, the more buttons will appear and the more false
7421 positives are possible.  Note that you can set this variable local to
7422 specific groups.  Setting it higher in Emacs or Gnus related groups is
7423 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7424 `gnus-parameters' on how to set variables in specific groups."
7425   :version "22.1"
7426   :group 'gnus-article-buttons
7427   :link '(custom-manual "(gnus)Group Parameters")
7428   :type 'integer)
7429
7430 (defcustom gnus-button-message-level 5
7431   "*Integer that says how many buttons for news or mail messages will appear.
7432 The higher the number, the more buttons will appear and the more false
7433 positives are possible."
7434   ;; mail addresses, MIDs, URLs for news, ...
7435   :version "22.1"
7436   :group 'gnus-article-buttons
7437   :type 'integer)
7438
7439 (defcustom gnus-button-browse-level 5
7440   "*Integer that says how many buttons for browsing will appear.
7441 The higher the number, the more buttons will appear and the more false
7442 positives are possible."
7443   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7444   :version "22.1"
7445   :group 'gnus-article-buttons
7446   :type 'integer)
7447
7448 (defcustom gnus-button-alist
7449   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7450      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7451     ((concat "\\b\\(nntp\\|news\\):\\("
7452              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7453      0 t gnus-button-handle-news 2)
7454     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7455      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7456     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7457      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7458     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7459     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7460      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7461     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7462      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7463     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7464      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7465     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7466      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7467     ;; RFC 2368 (The mailto URL scheme)
7468     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7469      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7470     ("\\bmailto:\\([^ \n\t]+\\)"
7471      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7472     ;; Info Konqueror style <info:/foo/bar baz>.
7473     ;; Must come before " Gnus home-grown style".
7474     ("\\binfo://?\\([^'\">\n\t]+\\)"
7475      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7476    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7477     ("\\binfo://\\([^'\">\n\t ]+\\)"
7478      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7479     ;; Info GNOME style <info:foo#bar_baz>
7480     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7481      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7482     ;; Info KDE style <info:(foo)bar baz>
7483     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7484      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7485     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7486      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7487     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7488      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7489      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7490     ;; This is custom
7491     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7492      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7493     ;; Emacs help commands
7494     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7495      ;; regexp doesn't match arguments containing ` '.
7496      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7497     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7498      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7499     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7500      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7501     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7502      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7503     ;; The following entries may lead to many false positives so don't enable
7504     ;; them by default (use a high button level).
7505     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7506      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7507      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7508     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7509      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7510     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7511      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7512     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7513      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7514     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7515      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7516     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7517      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7518     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7519      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7520     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7521      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7522     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7523      ;; Unlike the other regexps we really have to require quoting
7524      ;; here to determine where it ends.
7525      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7526     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7527     ("<URL: *\\([^\n<>]*\\)>"
7528      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7529     ;; RFC 2396 (2.4.3., delims) ...
7530     ("\"URL: *\\([^\n\"]*\\)\""
7531      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7532     ;; Raw URLs.
7533     (gnus-button-url-regexp
7534      0 (>= gnus-button-browse-level 0) browse-url 0)
7535     ;; man pages
7536     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7537      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7538      gnus-button-handle-man 1)
7539     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7540     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7541      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7542      gnus-button-handle-man 1)
7543     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7544     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7545     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7546      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7547     ;; Recognizing patches to .el files.  This is somewhat obscure,
7548     ;; but considering the percentage of Gnus users who hack Emacs
7549     ;; Lisp files...
7550     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7551      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7552     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7553      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7554     ;; MID or mail: To avoid too many false positives we don't try to catch
7555     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7556     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7557     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7558     ;; so that non-ambiguous entries (see above) match first.
7559     (gnus-button-mid-or-mail-regexp
7560      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7561   "*Alist of regexps matching buttons in article bodies.
7562
7563 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7564 REGEXP: is the string (case insensitive) matching text around the button (can
7565 also be Lisp expression evaluating to a string),
7566 BUTTON: is the number of the regexp grouping actually matching the button,
7567 FORM: is a Lisp expression which must eval to true for the button to
7568 be added,
7569 CALLBACK: is the function to call when the user push this button, and each
7570 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7571
7572 CALLBACK can also be a variable, in that case the value of that
7573 variable it the real callback function."
7574   :group 'gnus-article-buttons
7575   :type '(repeat (list (choice regexp variable sexp)
7576                        (integer :tag "Button")
7577                        (sexp :tag "Form")
7578                        (function :tag "Callback")
7579                        (repeat :tag "Par"
7580                                :inline t
7581                                (integer :tag "Regexp group")))))
7582 (put 'gnus-button-alist 'risky-local-variable t)
7583
7584 (defcustom gnus-header-button-alist
7585   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7586      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7587     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7588      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7589     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7590      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7591     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7592      0 (>= gnus-button-browse-level 0) browse-url 0)
7593     ("^Subject:" gnus-button-url-regexp
7594      0 (>= gnus-button-browse-level 0) browse-url 0)
7595     ("^[^:]+:" gnus-button-url-regexp
7596      0 (>= gnus-button-browse-level 0) browse-url 0)
7597     ("^OpenPGP:.*url=" gnus-button-url-regexp
7598      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7599     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7600      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7601     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7602      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7603   "*Alist of headers and regexps to match buttons in article heads.
7604
7605 This alist is very similar to `gnus-button-alist', except that each
7606 alist has an additional HEADER element first in each entry:
7607
7608 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7609
7610 HEADER is a regexp to match a header.  For a fuller explanation, see
7611 `gnus-button-alist'."
7612   :group 'gnus-article-buttons
7613   :group 'gnus-article-headers
7614   :type '(repeat (list (regexp :tag "Header")
7615                        (choice regexp variable)
7616                        (integer :tag "Button")
7617                        (sexp :tag "Form")
7618                        (function :tag "Callback")
7619                        (repeat :tag "Par"
7620                                :inline t
7621                                (integer :tag "Regexp group")))))
7622 (put 'gnus-header-button-alist 'risky-local-variable t)
7623
7624 ;;; Commands:
7625
7626 (defun gnus-article-push-button (event)
7627   "Check text under the mouse pointer for a callback function.
7628 If the text under the mouse pointer has a `gnus-callback' property,
7629 call it with the value of the `gnus-data' text property."
7630   (interactive "e")
7631   (set-buffer (window-buffer (posn-window (event-start event))))
7632   (let* ((pos (posn-point (event-start event)))
7633          (data (get-text-property pos 'gnus-data))
7634          (fun (get-text-property pos 'gnus-callback)))
7635     (goto-char pos)
7636     (when fun
7637       (funcall fun data))))
7638
7639 (defun gnus-article-press-button ()
7640   "Check text at point for a callback function.
7641 If the text at point has a `gnus-callback' property,
7642 call it with the value of the `gnus-data' text property."
7643   (interactive)
7644   (let ((data (get-text-property (point) 'gnus-data))
7645         (fun (get-text-property (point) 'gnus-callback)))
7646     (when fun
7647       (funcall fun data))))
7648
7649 (defun gnus-article-highlight (&optional force)
7650   "Highlight current article.
7651 This function calls `gnus-article-highlight-headers',
7652 `gnus-article-highlight-citation',
7653 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7654 do the highlighting.  See the documentation for those functions."
7655   (interactive (list 'force))
7656   (gnus-article-highlight-headers)
7657   (gnus-article-highlight-citation force)
7658   (gnus-article-highlight-signature)
7659   (gnus-article-add-buttons force)
7660   (gnus-article-add-buttons-to-head))
7661
7662 (defun gnus-article-highlight-some (&optional force)
7663   "Highlight current article.
7664 This function calls `gnus-article-highlight-headers',
7665 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7666 do the highlighting.  See the documentation for those functions."
7667   (interactive (list 'force))
7668   (gnus-article-highlight-headers)
7669   (gnus-article-highlight-signature)
7670   (gnus-article-add-buttons))
7671
7672 (defun gnus-article-highlight-headers ()
7673   "Highlight article headers as specified by `gnus-header-face-alist'."
7674   (interactive)
7675   (gnus-with-article-headers
7676     (let (regexp header-face field-face from hpoints fpoints)
7677       (dolist (entry gnus-header-face-alist)
7678         (goto-char (point-min))
7679         (setq regexp (concat "^\\("
7680                              (if (string-equal "" (nth 0 entry))
7681                                  "[^\t ]"
7682                                (nth 0 entry))
7683                              "\\)")
7684               header-face (nth 1 entry)
7685               field-face (nth 2 entry))
7686         (while (and (re-search-forward regexp nil t)
7687                     (not (eobp)))
7688           (beginning-of-line)
7689           (setq from (point))
7690           (unless (search-forward ":" nil t)
7691             (forward-char 1))
7692           (when (and header-face
7693                      (not (memq (point) hpoints)))
7694             (push (point) hpoints)
7695             (gnus-put-text-property from (point) 'face header-face))
7696           (when (and field-face
7697                      (not (memq (setq from (point)) fpoints)))
7698             (push from fpoints)
7699             (if (re-search-forward "^[^ \t]" nil t)
7700                 (forward-char -2)
7701               (goto-char (point-max)))
7702             (gnus-put-text-property from (point) 'face field-face)))))))
7703
7704 (defun gnus-article-highlight-signature ()
7705   "Highlight the signature in an article.
7706 It does this by highlighting everything after
7707 `gnus-signature-separator' using the face `gnus-signature'."
7708   (interactive)
7709   (gnus-with-article-buffer
7710     (let ((inhibit-point-motion-hooks t))
7711       (save-restriction
7712         (when (and gnus-signature-face
7713                    (gnus-article-narrow-to-signature))
7714           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7715                             'face gnus-signature-face)
7716           (widen)
7717           (gnus-article-search-signature)
7718           (let ((start (match-beginning 0))
7719                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7720             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7721                                      end)))))))
7722
7723 (defun gnus-button-in-region-p (b e prop)
7724   "Say whether PROP exists in the region."
7725   (text-property-not-all b e prop nil))
7726
7727 (defun gnus-article-add-buttons (&optional force)
7728   "Find external references in the article and make buttons of them.
7729 \"External references\" are things like Message-IDs and URLs, as
7730 specified by `gnus-button-alist'."
7731   (interactive (list 'force))
7732   (gnus-with-article-buffer
7733     (let ((inhibit-point-motion-hooks t)
7734           (case-fold-search t)
7735           (alist gnus-button-alist)
7736           beg entry regexp)
7737       ;; Remove all old markers.
7738       (let (marker entry new-list)
7739         (while (setq marker (pop gnus-button-marker-list))
7740           (if (or (< marker (point-min)) (>= marker (point-max)))
7741               (push marker new-list)
7742             (goto-char marker)
7743             (when (setq entry (gnus-button-entry))
7744               (put-text-property (match-beginning (nth 1 entry))
7745                                  (match-end (nth 1 entry))
7746                                  'gnus-callback nil))
7747             (set-marker marker nil)))
7748         (setq gnus-button-marker-list new-list))
7749       ;; We skip the headers.
7750       (article-goto-body)
7751       (setq beg (point))
7752       (while (setq entry (pop alist))
7753         (setq regexp (eval (car entry)))
7754         (goto-char beg)
7755         (while (re-search-forward regexp nil t)
7756           (let ((start (match-beginning (nth 1 entry)))
7757                 (end (match-end (nth 1 entry)))
7758                 (from (match-beginning 0)))
7759             (when (and (or (eq t (nth 2 entry))
7760                            (eval (nth 2 entry)))
7761                        (not (gnus-button-in-region-p
7762                              start end 'gnus-callback)))
7763               ;; That optional form returned non-nil, so we add the
7764               ;; button.
7765               (setq from (set-marker (make-marker) from))
7766               (push from gnus-button-marker-list)
7767               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7768                            (gnus-article-extend-url-button from start end))
7769                 (gnus-article-add-button start end
7770                                          'gnus-button-push from)
7771                 (gnus-put-text-property
7772                  start end
7773                  'gnus-string (buffer-substring-no-properties
7774                                start end))))))))))
7775
7776 (defun gnus-article-extend-url-button (beg start end)
7777   "Extend url button if url is folded into two or more lines.
7778 Return non-nil if button is extended.  BEG is a marker that points to
7779 the beginning position of a text containing url.  START and END are
7780 the endpoints of a url button before it is extended.  The concatenated
7781 url is put as the `gnus-button-url' overlay property on the button."
7782   (let ((opoint (point))
7783         (points (list start end))
7784         url delim regexp)
7785     (prog1
7786         (when (and (progn
7787                      (goto-char end)
7788                      (not (looking-at "[\t ]*[\">]")))
7789                    (progn
7790                      (goto-char start)
7791                      (string-match
7792                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7793                       (buffer-substring (point-at-bol) start)))
7794                    (progn
7795                      (setq url (list (buffer-substring start end))
7796                            delim (if (match-beginning 1) ">" "\""))
7797                      (beginning-of-line)
7798                      (setq regexp (concat
7799                                    (when (and (looking-at
7800                                                message-cite-prefix-regexp)
7801                                               (< (match-end 0) start))
7802                                      (regexp-quote (match-string 0)))
7803                                    "\
7804 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7805                                    delim "\\)"))
7806                      (while (progn
7807                               (forward-line 1)
7808                               (and (looking-at regexp)
7809                                    (prog1
7810                                        (match-beginning 1)
7811                                      (push (or (match-string 2)
7812                                                (match-string 1))
7813                                            url)
7814                                      (push (setq end (or (match-end 2)
7815                                                          (match-end 1)))
7816                                            points)
7817                                      (push (or (match-beginning 2)
7818                                                (match-beginning 1))
7819                                            points)))))
7820                      (match-beginning 2)))
7821           (let (gnus-article-mouse-face widget-mouse-face)
7822             (while points
7823               (gnus-article-add-button (pop points) (pop points)
7824                                        'gnus-button-push beg)))
7825           (let ((overlay (gnus-make-overlay start end)))
7826             (gnus-overlay-put overlay 'evaporate t)
7827             (gnus-overlay-put overlay 'gnus-button-url
7828                               (list (mapconcat 'identity (nreverse url) "")))
7829             (when gnus-article-mouse-face
7830               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7831           t)
7832       (goto-char opoint))))
7833
7834 ;; Add buttons to the head of an article.
7835 (defun gnus-article-add-buttons-to-head ()
7836   "Add buttons to the head of the article."
7837   (interactive)
7838   (gnus-with-article-headers
7839     (let (beg end)
7840       (dolist (entry gnus-header-button-alist)
7841         ;; Each alist entry.
7842         (goto-char (point-min))
7843         (while (re-search-forward (car entry) nil t)
7844           ;; Each header matching the entry.
7845           (setq beg (match-beginning 0))
7846           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7847                              (match-beginning 0))
7848                         (point-max)))
7849           (goto-char beg)
7850           (while (re-search-forward (eval (nth 1 entry)) end t)
7851             ;; Each match within a header.
7852             (let* ((entry (cdr entry))
7853                    (start (match-beginning (nth 1 entry)))
7854                    (end (match-end (nth 1 entry)))
7855                    (form (nth 2 entry)))
7856               (goto-char (match-end 0))
7857               (when (eval form)
7858                 (gnus-article-add-button
7859                  start end (nth 3 entry)
7860                  (buffer-substring (match-beginning (nth 4 entry))
7861                                    (match-end (nth 4 entry)))))))
7862           (goto-char end))))))
7863
7864 ;;; External functions:
7865
7866 (defun gnus-article-add-button (from to fun &optional data text)
7867   "Create a button between FROM and TO with callback FUN and data DATA."
7868   (when gnus-article-button-face
7869     (gnus-overlay-put (gnus-make-overlay from to nil t)
7870                       'face gnus-article-button-face))
7871   (gnus-add-text-properties
7872    from to
7873    (nconc (and gnus-article-mouse-face
7874                (list gnus-mouse-face-prop gnus-article-mouse-face))
7875           (list 'gnus-callback fun)
7876           (and data (list 'gnus-data data))))
7877   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7878                          :help-echo (or text "Follow the link")
7879                          :keymap gnus-url-button-map
7880                          :button-keymap gnus-widget-button-keymap))
7881
7882 (defun gnus-article-copy-string ()
7883   "Copy the string in the button to the kill ring."
7884   (interactive)
7885   (gnus-article-check-buffer)
7886   (let ((data (get-text-property (point) 'gnus-string)))
7887     (when data
7888       (with-temp-buffer
7889         (insert data)
7890         (copy-region-as-kill (point-min) (point-max))
7891         (message "Copied %s" data)))))
7892
7893 ;;; Internal functions:
7894
7895 (defun gnus-article-set-globals ()
7896   (with-current-buffer gnus-summary-buffer
7897     (gnus-set-global-variables)))
7898
7899 (defun gnus-signature-toggle (end)
7900   (gnus-with-article-buffer
7901     (let ((inhibit-point-motion-hooks t))
7902       (if (text-property-any end (point-max) 'article-type 'signature)
7903           (progn
7904             (gnus-delete-wash-type 'signature)
7905             (gnus-remove-text-properties-when
7906              'article-type 'signature end (point-max)
7907              (cons 'article-type (cons 'signature
7908                                        gnus-hidden-properties))))
7909         (gnus-add-wash-type 'signature)
7910         (gnus-add-text-properties-when
7911          'article-type nil end (point-max)
7912          (cons 'article-type (cons 'signature
7913                                    gnus-hidden-properties)))))
7914     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7915       (gnus-set-mode-line 'article))))
7916
7917 (defun gnus-button-entry ()
7918   ;; Return the first entry in `gnus-button-alist' matching this place.
7919   (let ((alist gnus-button-alist)
7920         (entry nil))
7921     (while alist
7922       (setq entry (pop alist))
7923       (if (looking-at (eval (car entry)))
7924           (setq alist nil)
7925         (setq entry nil)))
7926     entry))
7927
7928 (defun gnus-button-push (marker)
7929   ;; Push button starting at MARKER.
7930   (save-excursion
7931     (goto-char marker)
7932     (let* ((entry (gnus-button-entry))
7933            (inhibit-point-motion-hooks t)
7934            (fun (nth 3 entry))
7935            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7936                           (get-char-property marker 'gnus-button-url))
7937                      (mapcar (lambda (group)
7938                                (let ((string (match-string group)))
7939                                  (set-text-properties
7940                                   0 (length string) nil string)
7941                                  string))
7942                              (nthcdr 4 entry)))))
7943       (cond
7944        ((fboundp fun)
7945         (apply fun args))
7946        ((and (boundp fun)
7947              (fboundp (symbol-value fun)))
7948         (apply (symbol-value fun) args))
7949        (t
7950         (gnus-message 1 "You must define `%S' to use this button"
7951                       (cons fun args)))))))
7952
7953 (defun gnus-parse-news-url (url)
7954   (let (scheme server port group message-id articles)
7955     (with-temp-buffer
7956       (insert url)
7957       (goto-char (point-min))
7958       (when (looking-at "\\([A-Za-z]+\\):")
7959         (setq scheme (match-string 1))
7960         (goto-char (match-end 0)))
7961       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7962         (setq server (match-string 1))
7963         (setq port (if (stringp (match-string 3))
7964                        (string-to-number (match-string 3))
7965                      (match-string 3)))
7966         (goto-char (match-end 0)))
7967
7968       (cond
7969        ((looking-at "\\(.*@.*\\)")
7970         (setq message-id (match-string 1)))
7971        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7972         (setq group (match-string 1)
7973               articles (split-string (match-string 2) "-")))
7974        ((looking-at "\\([^/]+\\)/?")
7975         (setq group (match-string 1)))
7976        (t
7977         (error "Unknown news URL syntax"))))
7978     (list scheme server port group message-id articles)))
7979
7980 (defun gnus-button-handle-news (url)
7981   "Fetch a news URL."
7982   (destructuring-bind (scheme server port group message-id articles)
7983       (gnus-parse-news-url url)
7984     (cond
7985      (message-id
7986       (with-current-buffer gnus-summary-buffer
7987         (if server
7988             (let ((gnus-refer-article-method
7989                    (nconc (list (list 'nntp server))
7990                           gnus-refer-article-method))
7991                   (nntp-port-number (or port "nntp")))
7992               (gnus-message 7 "Fetching %s with %s"
7993                             message-id gnus-refer-article-method)
7994               (gnus-summary-refer-article message-id))
7995           (gnus-summary-refer-article message-id))))
7996      (group
7997       (gnus-button-fetch-group url)))))
7998
7999 (defun gnus-button-patch (library line)
8000   "Visit an Emacs Lisp library LIBRARY on line LINE."
8001   (interactive)
8002   (let ((file (locate-library (file-name-nondirectory library))))
8003     (unless file
8004       (error "Couldn't find library %s" library))
8005     (find-file file)
8006     (goto-char (point-min))
8007     (forward-line (1- (string-to-number line)))))
8008
8009 (defun gnus-button-handle-man (url)
8010   "Fetch a man page."
8011   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8012   (when (eq gnus-button-man-handler 'woman)
8013     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8014   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8015   (funcall gnus-button-man-handler url))
8016
8017 (defun gnus-button-handle-info-url (url)
8018   "Fetch an info URL."
8019   (setq url (mm-subst-char-in-string ?+ ?\  url))
8020   (cond
8021    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8022     (gnus-info-find-node
8023      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8024                      "Gnus")
8025              ")" (gnus-url-unhex-string (match-string 2 url)))))
8026    ((string-match "([^)\"]+)[^\"]+" url)
8027     (setq url
8028           (gnus-replace-in-string
8029            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8030     (gnus-info-find-node url))
8031    (t (error "Can't parse %s" url))))
8032
8033 (defun gnus-button-handle-info-url-gnome (url)
8034   "Fetch GNOME style info URL."
8035   (setq url (mm-subst-char-in-string ?_ ?\  url))
8036   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8037       (gnus-info-find-node
8038        (concat "("
8039                (gnus-url-unhex-string
8040                  (match-string 1 url))
8041                ")"
8042                (or (gnus-url-unhex-string
8043                     (match-string 2 url))
8044                    "Top")))
8045     (error "Can't parse %s" url)))
8046
8047 (defun gnus-button-handle-info-url-kde (url)
8048   "Fetch KDE style info URL."
8049   (gnus-info-find-node (gnus-url-unhex-string url)))
8050
8051 ;; (info) will autoload info.el
8052 (declare-function Info-menu "info" (menu-item &optional fork))
8053 (declare-function Info-index-next "info" (num))
8054
8055 (defun gnus-button-handle-info-keystrokes (url)
8056   "Call `info' when pushing the corresponding URL button."
8057   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8058   (let (node indx comma)
8059     (if (string-match
8060          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8061                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8062                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8063                  "\\(?:[ \t\n,]*\\)\\)?")
8064          url)
8065         (setq node (match-string 2 url)
8066               indx (match-string 3 url))
8067       (error "Can't parse %s" url))
8068     (info)
8069     (Info-directory)
8070     (Info-menu node)
8071     (when (> (length indx) 0)
8072       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8073                             "\\([ \t\n,]*\\)")
8074                     indx)
8075       (setq comma (match-string 2 indx))
8076       (setq indx  (match-string 1 indx))
8077       (Info-index indx)
8078       (when comma
8079         (dotimes (i (with-temp-buffer
8080                       (insert comma)
8081                       ;; Note: the XEmacs version of `how-many' takes
8082                       ;; no optional argument.
8083                       (goto-char (point-min))
8084                       (how-many ",")))
8085           (Info-index-next 1)))
8086       nil)))
8087
8088 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8089 (declare-function pgg-display-output-buffer "pgg" (start end status))
8090
8091 (defun gnus-button-openpgp (url)
8092   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8093   (with-temp-buffer
8094     (mm-url-insert-file-contents-external url)
8095     (pgg-snarf-keys-region (point-min) (point-max))
8096     (pgg-display-output-buffer nil nil nil)))
8097
8098 (defun gnus-button-message-id (message-id)
8099   "Fetch MESSAGE-ID."
8100   (with-current-buffer gnus-summary-buffer
8101     (gnus-summary-refer-article message-id)))
8102
8103 (defun gnus-button-fetch-group (address &rest ignore)
8104   "Fetch GROUP specified by ADDRESS."
8105   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8106                       address)
8107     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8108     ;; for nntp:// and news://
8109     (setq address (match-string 3 address)))
8110   (if (not (string-match "[:/]" address))
8111       ;; This is just a simple group url.
8112       (gnus-group-read-ephemeral-group address gnus-select-method)
8113     (if (not
8114          (string-match
8115           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8116           address))
8117         (error "Can't parse %s" address)
8118       (gnus-group-read-ephemeral-group
8119        (match-string 4 address)
8120        `(nntp ,(match-string 1 address)
8121               (nntp-address ,(match-string 1 address))
8122               (nntp-port-number ,(if (match-end 3)
8123                                      (match-string 3 address)
8124                                    "nntp")))
8125        nil nil nil
8126        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8127
8128 (defun gnus-url-parse-query-string (query &optional downcase)
8129   (let (retval pairs cur key val)
8130     (setq pairs (split-string query "&"))
8131     (while pairs
8132       (setq cur (car pairs)
8133             pairs (cdr pairs))
8134       (if (not (string-match "=" cur))
8135           nil                           ; Grace
8136         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8137               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8138         (if downcase
8139             (setq key (downcase key)))
8140         (setq cur (assoc key retval))
8141         (if cur
8142             (setcdr cur (cons val (cdr cur)))
8143           (setq retval (cons (list key val) retval)))))
8144     retval))
8145
8146 (defun gnus-url-mailto (url)
8147   ;; Send mail to someone
8148   (when (string-match "mailto:/*\\(.*\\)" url)
8149     (setq url (substring url (match-beginning 1) nil)))
8150   (let (to args subject func)
8151     (setq args (gnus-url-parse-query-string
8152                 (if (string-match "^\\?" url)
8153                     (substring url 1)
8154                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8155                       (concat "to=" (match-string 1 url) "&"
8156                               (match-string 2 url))
8157                     (concat "to=" url)))
8158                 t)
8159           subject (cdr-safe (assoc "subject" args)))
8160     (gnus-msg-mail)
8161     (while args
8162       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8163       (if (fboundp func)
8164           (funcall func)
8165         (message-position-on-field (caar args)))
8166       (insert (gnus-replace-in-string
8167                (mapconcat 'identity (reverse (cdar args)) ", ")
8168                "\r\n" "\n" t))
8169       (setq args (cdr args)))
8170     (if subject
8171         (message-goto-body)
8172       (message-goto-subject))))
8173
8174 (defun gnus-button-embedded-url (address)
8175   "Activate ADDRESS with `browse-url'."
8176   (browse-url (gnus-strip-whitespace address)))
8177
8178 ;;; Next/prev buttons in the article buffer.
8179
8180 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8181 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8182
8183 (defvar gnus-prev-page-map
8184   (let ((map (make-sparse-keymap)))
8185     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8186     (define-key map "\r" 'gnus-button-prev-page)
8187     map))
8188
8189 (defvar gnus-next-page-map
8190   (let ((map (make-sparse-keymap)))
8191     (unless (>= emacs-major-version 21)
8192       ;; XEmacs doesn't care.
8193       (set-keymap-parent map gnus-article-mode-map))
8194     (define-key map gnus-mouse-2 'gnus-button-next-page)
8195     (define-key map "\r" 'gnus-button-next-page)
8196     map))
8197
8198 (defun gnus-insert-prev-page-button ()
8199   (let ((b (point)) e
8200         (inhibit-read-only t))
8201     (gnus-eval-format
8202      gnus-prev-page-line-format nil
8203      `(keymap ,gnus-prev-page-map
8204               gnus-prev t
8205               gnus-callback gnus-article-button-prev-page
8206               article-type annotation))
8207     (setq e (if (bolp)
8208                 ;; Exclude a newline.
8209                 (1- (point))
8210               (point)))
8211     (when gnus-article-button-face
8212       (gnus-overlay-put (gnus-make-overlay b e nil t)
8213                         'face gnus-article-button-face))
8214     (widget-convert-button
8215      'link b e
8216      :action 'gnus-button-prev-page
8217      :button-keymap gnus-prev-page-map)))
8218
8219 (defun gnus-button-next-page (&optional args more-args)
8220   "Go to the next page."
8221   (interactive)
8222   (let ((win (selected-window)))
8223     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8224     (gnus-article-next-page)
8225     (select-window win)))
8226
8227 (defun gnus-button-prev-page (&optional args more-args)
8228   "Go to the prev page."
8229   (interactive)
8230   (let ((win (selected-window)))
8231     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8232     (gnus-article-prev-page)
8233     (select-window win)))
8234
8235 (defun gnus-insert-next-page-button ()
8236   (let ((b (point)) e
8237         (inhibit-read-only t))
8238     (gnus-eval-format gnus-next-page-line-format nil
8239                       `(keymap ,gnus-next-page-map
8240                                gnus-next t
8241                                gnus-callback gnus-article-button-next-page
8242                                article-type annotation))
8243     (setq e (if (bolp)
8244                 ;; Exclude a newline.
8245                 (1- (point))
8246               (point)))
8247     (when gnus-article-button-face
8248       (gnus-overlay-put (gnus-make-overlay b e nil t)
8249                         'face gnus-article-button-face))
8250     (widget-convert-button
8251      'link b e
8252      :action 'gnus-button-next-page
8253      :button-keymap gnus-next-page-map)))
8254
8255 (defun gnus-article-button-next-page (arg)
8256   "Go to the next page."
8257   (interactive "P")
8258   (let ((win (selected-window)))
8259     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8260     (gnus-article-next-page)
8261     (select-window win)))
8262
8263 (defun gnus-article-button-prev-page (arg)
8264   "Go to the prev page."
8265   (interactive "P")
8266   (let ((win (selected-window)))
8267     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8268     (gnus-article-prev-page)
8269     (select-window win)))
8270
8271 (defvar gnus-decode-header-methods
8272   '(mail-decode-encoded-word-region)
8273   "List of methods used to decode headers.
8274
8275 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8276 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8277 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8278 whose names match REGEXP.
8279
8280 For example:
8281 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8282  mail-decode-encoded-word-region
8283  (\"chinese\" . rfc1843-decode-region))
8284 ")
8285
8286 (defvar gnus-decode-header-methods-cache nil)
8287
8288 (defun gnus-multi-decode-header (start end)
8289   "Apply the functions from `gnus-encoded-word-methods' that match."
8290   (unless (and gnus-decode-header-methods-cache
8291                (eq gnus-newsgroup-name
8292                    (car gnus-decode-header-methods-cache)))
8293     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8294     (dolist (x gnus-decode-header-methods)
8295       (if (symbolp x)
8296           (nconc gnus-decode-header-methods-cache (list x))
8297         (if (and gnus-newsgroup-name
8298                  (string-match (car x) gnus-newsgroup-name))
8299             (nconc gnus-decode-header-methods-cache
8300                    (list (cdr x)))))))
8301   (let ((xlist gnus-decode-header-methods-cache))
8302     (pop xlist)
8303     (save-restriction
8304       (narrow-to-region start end)
8305       (while xlist
8306         (funcall (pop xlist) (point-min) (point-max))))))
8307
8308 ;;;
8309 ;;; Treatment top-level handling.
8310 ;;;
8311
8312 (defun gnus-treat-article (condition &optional part-number total-parts type)
8313   (let ((length (- (point-max) (point-min)))
8314         (alist gnus-treatment-function-alist)
8315         (article-goto-body-goes-to-point-min-p t)
8316         (treated-type
8317          (or (not type)
8318              (catch 'found
8319                (let ((list gnus-article-treat-types))
8320                  (while list
8321                    (when (string-match (pop list) type)
8322                      (throw 'found t)))))))
8323         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8324         val elem)
8325     (gnus-run-hooks 'gnus-part-display-hook)
8326     (dolist (elem alist)
8327       (setq val
8328             (save-excursion
8329               (when (gnus-buffer-live-p gnus-summary-buffer)
8330                 (set-buffer gnus-summary-buffer))
8331               (symbol-value (car elem))))
8332       (when (and (or (consp val)
8333                      treated-type)
8334                  (gnus-treat-predicate val)
8335                  (or (not (get (car elem) 'highlight))
8336                      highlightp))
8337         (save-restriction
8338           (funcall (cadr elem)))))))
8339
8340 ;; Dynamic variables.
8341 (defvar part-number)
8342 (defvar total-parts)
8343 (defvar type)
8344 (defvar condition)
8345 (defvar length)
8346
8347 (defun gnus-treat-predicate (val)
8348   (cond
8349    ((null val)
8350     nil)
8351    (condition
8352     (eq condition val))
8353    ((and (listp val)
8354          (stringp (car val)))
8355     (apply 'gnus-or (mapcar `(lambda (s)
8356                                (string-match s ,(or gnus-newsgroup-name "")))
8357                             val)))
8358    ((listp val)
8359     (let ((pred (pop val)))
8360       (cond
8361        ((eq pred 'or)
8362         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8363        ((eq pred 'and)
8364         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8365        ((eq pred 'not)
8366         (not (gnus-treat-predicate (car val))))
8367        ((eq pred 'typep)
8368         (equal (car val) type))
8369        (t
8370         (error "%S is not a valid predicate" pred)))))
8371    ((eq val t)
8372     t)
8373    ((eq val 'head)
8374     nil)
8375    ((eq val 'first)
8376     (eq part-number 1))
8377    ((eq val 'last)
8378     (eq part-number total-parts))
8379    ((numberp val)
8380     (< length val))
8381    (t
8382     (error "%S is not a valid value" val))))
8383
8384 (defun gnus-article-encrypt-body (protocol &optional n)
8385   "Encrypt the article body."
8386   (interactive
8387    (list
8388     (or gnus-article-encrypt-protocol
8389         (completing-read "Encrypt protocol: "
8390                          gnus-article-encrypt-protocol-alist
8391                          nil t))
8392     current-prefix-arg))
8393   ;; User might hit `K E' instead of `K e', so prompt once.
8394   (when (and gnus-article-encrypt-protocol
8395              gnus-novice-user)
8396     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8397       (error "Encrypt aborted")))
8398   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8399     (unless func
8400       (error "Can't find the encrypt protocol %s" protocol))
8401     (if (member gnus-newsgroup-name '("nndraft:delayed"
8402                                       "nndraft:drafts"
8403                                       "nndraft:queue"))
8404         (error "Can't encrypt the article in group %s"
8405                gnus-newsgroup-name))
8406     (gnus-summary-iterate n
8407       (with-current-buffer gnus-summary-buffer
8408         (let ((mail-parse-charset gnus-newsgroup-charset)
8409               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8410               (summary-buffer gnus-summary-buffer)
8411               references point)
8412           (gnus-set-global-variables)
8413           (when (gnus-group-read-only-p)
8414             (error "The current newsgroup does not support article encrypt"))
8415           (gnus-summary-show-article t)
8416           (setq references
8417               (or (mail-header-references gnus-current-headers) ""))
8418           (set-buffer gnus-article-buffer)
8419           (let* ((inhibit-read-only t)
8420                  (headers
8421                   (mapcar (lambda (field)
8422                             (and (save-restriction
8423                                    (message-narrow-to-head)
8424                                    (goto-char (point-min))
8425                                    (search-forward field nil t))
8426                                  (prog2
8427                                      (message-narrow-to-field)
8428                                      (buffer-string)
8429                                    (delete-region (point-min) (point-max))
8430                                    (widen))))
8431                           '("Content-Type:" "Content-Transfer-Encoding:"
8432                             "Content-Disposition:"))))
8433             (message-narrow-to-head)
8434             (message-remove-header "MIME-Version")
8435             (goto-char (point-max))
8436             (setq point (point))
8437             (insert (apply 'concat headers))
8438             (widen)
8439             (narrow-to-region point (point-max))
8440             (let ((message-options message-options))
8441               (message-options-set 'message-sender user-mail-address)
8442               (message-options-set 'message-recipients user-mail-address)
8443               (message-options-set 'message-sign-encrypt 'not)
8444               (funcall func))
8445             (goto-char (point-min))
8446             (insert "MIME-Version: 1.0\n")
8447             (widen)
8448             (gnus-summary-edit-article-done
8449              references nil summary-buffer t))
8450           (when gnus-keep-backlog
8451             (gnus-backlog-remove-article
8452              (car gnus-article-current) (cdr gnus-article-current)))
8453           (gnus-flush-original-article-buffer)
8454           (when gnus-use-cache
8455             (gnus-cache-update-article
8456              (car gnus-article-current) (cdr gnus-article-current))))))))
8457
8458 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8459   "The following specs can be used:
8460 %t  The security MIME type
8461 %i  Additional info
8462 %d  Details
8463 %D  Details if button is pressed")
8464
8465 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8466   "The following specs can be used:
8467 %t  The security MIME type
8468 %i  Additional info
8469 %d  Details
8470 %D  Details if button is pressed")
8471
8472 (defvar gnus-mime-security-button-line-format-alist
8473   '((?t gnus-tmp-type ?s)
8474     (?i gnus-tmp-info ?s)
8475     (?d gnus-tmp-details ?s)
8476     (?D gnus-tmp-pressed-details ?s)))
8477
8478 (defvar gnus-mime-security-button-commands
8479   '((gnus-article-press-button "\r" "Show Detail")
8480     (undefined "v")
8481     (undefined "t")
8482     (undefined "C")
8483     (gnus-mime-security-save-part "o" "Save...")
8484     (undefined "\C-o")
8485     (undefined "r")
8486     (undefined "d")
8487     (undefined "c")
8488     (undefined "i")
8489     (undefined "E")
8490     (undefined "e")
8491     (undefined "p")
8492     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8493     (undefined ".")))
8494
8495 (defvar gnus-mime-security-button-map
8496   (let ((map (make-sparse-keymap)))
8497     (define-key map gnus-mouse-2 'gnus-article-push-button)
8498     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8499     (dolist (c gnus-mime-security-button-commands)
8500       (define-key map (cadr c) (car c)))
8501     map))
8502
8503 (easy-menu-define
8504   gnus-mime-security-button-menu gnus-mime-security-button-map
8505   "Security button menu."
8506   `("Security Part"
8507     ,@(delq nil
8508             (mapcar (lambda (c)
8509                       (unless (eq (car c) 'undefined)
8510                         (vector (caddr c) (car c) :active t)))
8511                     gnus-mime-security-button-commands))))
8512
8513 (defun gnus-mime-security-button-menu (event prefix)
8514   "Construct a context-sensitive menu of security commands."
8515   (interactive "e\nP")
8516   (save-window-excursion
8517     (let ((pos (event-start event)))
8518       (select-window (posn-window pos))
8519       (goto-char (posn-point pos))
8520       (gnus-article-check-buffer)
8521       (popup-menu gnus-mime-security-button-menu nil prefix))))
8522
8523 (defvar gnus-mime-security-details-buffer nil)
8524
8525 (defvar gnus-mime-security-button-pressed nil)
8526
8527 (defvar gnus-mime-security-show-details-inline t
8528   "If non-nil, show details in the article buffer.")
8529
8530 (defun gnus-mime-security-verify-or-decrypt (handle)
8531   (mm-remove-parts (cdr handle))
8532   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8533         point (inhibit-read-only t))
8534     (if region
8535         (goto-char (car region)))
8536     (setq point (point))
8537     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8538       (let* ((mm-verify-option 'known)
8539              (mm-decrypt-option 'known)
8540              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8541         (unless (eq nparts (cdr handle))
8542           (mm-destroy-parts (cdr handle))
8543           (setcdr handle nparts))))
8544     (gnus-mime-display-security handle)
8545     (when region
8546       (delete-region (point) (cdr region))
8547       (set-marker (car region) nil)
8548       (set-marker (cdr region) nil))
8549     (goto-char point)))
8550
8551 (defun gnus-mime-security-show-details (handle)
8552   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8553     (if (not details)
8554         (gnus-message 5 "No details.")
8555       (if gnus-mime-security-show-details-inline
8556           (let ((gnus-mime-security-button-pressed
8557                  (not (get-text-property (point) 'gnus-mime-details)))
8558                 (gnus-mime-security-button-line-format
8559                  (get-text-property (point) 'gnus-line-format))
8560                 (inhibit-read-only t))
8561             (forward-char -1)
8562             (while (eq (get-text-property (point) 'gnus-line-format)
8563                        gnus-mime-security-button-line-format)
8564               (forward-char -1))
8565             (forward-char)
8566             (save-restriction
8567               (narrow-to-region (point) (point))
8568               (gnus-insert-mime-security-button handle))
8569             (delete-region (point)
8570                            (or (text-property-not-all
8571                                 (point) (point-max)
8572                                 'gnus-line-format
8573                                 gnus-mime-security-button-line-format)
8574                                (point-max))))
8575         ;; Not inlined.
8576         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8577             (with-current-buffer gnus-mime-security-details-buffer
8578               (erase-buffer)
8579               t)
8580           (setq gnus-mime-security-details-buffer
8581                 (gnus-get-buffer-create "*MIME Security Details*")))
8582         (with-current-buffer gnus-mime-security-details-buffer
8583           (insert details)
8584           (goto-char (point-min)))
8585         (pop-to-buffer gnus-mime-security-details-buffer)))))
8586
8587 (defun gnus-mime-security-press-button (handle)
8588   (save-excursion
8589     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8590         (gnus-mime-security-show-details handle)
8591       (gnus-mime-security-verify-or-decrypt handle))))
8592
8593 (defun gnus-insert-mime-security-button (handle &optional displayed)
8594   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8595          (gnus-tmp-type
8596           (concat
8597            (or (nth 2 (assoc protocol mm-verify-function-alist))
8598                (nth 2 (assoc protocol mm-decrypt-function-alist))
8599                "Unknown")
8600            (if (equal (car handle) "multipart/signed")
8601                " Signed" " Encrypted")
8602            " Part"))
8603          (gnus-tmp-info
8604           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8605               "Undecided"))
8606          (gnus-tmp-details
8607           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8608          gnus-tmp-pressed-details
8609          b e)
8610     (setq gnus-tmp-details
8611           (if gnus-tmp-details
8612               (concat "\n" gnus-tmp-details)
8613             ""))
8614     (setq gnus-tmp-pressed-details
8615           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8616     (unless (bolp)
8617       (insert "\n"))
8618     (setq b (point))
8619     (gnus-eval-format
8620      gnus-mime-security-button-line-format
8621      gnus-mime-security-button-line-format-alist
8622      `(keymap ,gnus-mime-security-button-map
8623          gnus-callback gnus-mime-security-press-button
8624          gnus-line-format ,gnus-mime-security-button-line-format
8625          gnus-mime-details ,gnus-mime-security-button-pressed
8626          article-type annotation
8627          gnus-data ,handle))
8628     (setq e (if (bolp)
8629                 ;; Exclude a newline.
8630                 (1- (point))
8631               (point)))
8632     (when gnus-article-button-face
8633       (gnus-overlay-put (gnus-make-overlay b e nil t)
8634                         'face gnus-article-button-face))
8635     (widget-convert-button
8636      'link b e
8637      :mime-handle handle
8638      :action 'gnus-widget-press-button
8639      :button-keymap gnus-mime-security-button-map
8640      :help-echo
8641      (lambda (widget)
8642        ;; Needed to properly clear the message due to a bug in
8643        ;; wid-edit (XEmacs only).
8644        (when (boundp 'help-echo-owns-message)
8645          (setq help-echo-owns-message t))
8646        (format
8647         "%S: show detail; %S: more options"
8648         (aref gnus-mouse-2 0)
8649         (aref gnus-down-mouse-3 0))))))
8650
8651 (defun gnus-mime-display-security (handle)
8652   (save-restriction
8653     (narrow-to-region (point) (point))
8654     (unless (gnus-unbuttonized-mime-type-p (car handle))
8655       (gnus-insert-mime-security-button handle))
8656     (gnus-mime-display-part (cadr handle))
8657     (unless (bolp)
8658       (insert "\n"))
8659     (unless (gnus-unbuttonized-mime-type-p (car handle))
8660       (let ((gnus-mime-security-button-line-format
8661              gnus-mime-security-button-end-line-format))
8662         (gnus-insert-mime-security-button handle)))
8663     (mm-set-handle-multipart-parameter
8664      handle 'gnus-region
8665      (cons (set-marker (make-marker) (point-min))
8666            (set-marker (make-marker) (point-max))))
8667     (goto-char (point-max))))
8668
8669 (defun gnus-mime-security-run-function (function)
8670   "Run FUNCTION with the security part under point."
8671   (gnus-article-check-buffer)
8672   (let ((data (get-text-property (point) 'gnus-data))
8673         buffer handle)
8674     (when (and (stringp (car-safe data))
8675                (setq buffer (mm-handle-multipart-original-buffer data))
8676                (setq handle (cadr data)))
8677       (if (bufferp (mm-handle-buffer handle))
8678           (progn
8679             (setq handle (cons buffer (copy-sequence (cdr handle))))
8680             (mm-handle-set-undisplayer handle nil))
8681         (setq handle (mm-make-handle
8682                       buffer
8683                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8684                       nil nil nil nil nil nil)))
8685       (funcall function handle))))
8686
8687 (defun gnus-mime-security-save-part ()
8688   "Save the security part under point."
8689   (interactive)
8690   (gnus-mime-security-run-function 'mm-save-part))
8691
8692 (defun gnus-mime-security-pipe-part ()
8693   "Pipe the security part under point to a process."
8694   (interactive)
8695   (gnus-mime-security-run-function 'mm-pipe-part))
8696
8697 (gnus-ems-redefine)
8698
8699 (provide 'gnus-art)
8700
8701 (run-hooks 'gnus-art-load-hook)
8702
8703 ;;; gnus-art.el ends here