bc6a977b4f2a1544694264896ec8ad52a74c8f3c
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'static))
33
34 (require 'path-util)
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'alist)
40 (require 'mime-view)
41 (require 'wid-edit)
42
43 ;; Avoid byte-compile warnings.
44 (eval-when-compile
45   (require 'mm-bodies)
46   (require 'mail-parse)
47   (require 'mm-decode)
48   (require 'mm-view)
49   (require 'mm-uu)
50   )
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)The Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
119     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
120     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
121     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
122     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
123     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
124     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
125     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
126     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
127     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
128     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
129     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
130     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
131     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
132     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
133     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
134     "^MBOX-Line" "^Priority:" "^X400-[-A-Za-z]+:"
135     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
136     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
137     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
138     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
139     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
140     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
141     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
142     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
143     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
144     "^X-Received:" "^Content-length:" "X-precedence:"
145     "^X-Authenticated-User:" "^X-Comment" "^X-Report:" "^X-Abuse-Info:"
146     "^X-HTTP-Proxy:" "^X-Mydeja-Info:" "^X-Copyright" "^X-No-Markup:"
147     "^X-Abuse-Info:" "^X-From_:" "^X-Accept-Language:" "^Errors-To:"
148     "^X-BeenThere:" "^X-Mailman-Version:" "^List-Help:" "^List-Post:"
149     "^List-Subscribe:" "^List-Id:" "^List-Unsubscribe:" "^List-Archive:"
150      "^X-Content-length:" "^X-Posting-Agent:" "^Original-Received:")
151   "*All headers that start with this regexp will be hidden.
152 This variable can also be a list of regexps of headers to be ignored.
153 If `gnus-visible-headers' is non-nil, this variable will be ignored."
154   :type '(choice :custom-show nil
155                  regexp
156                  (repeat regexp))
157   :group 'gnus-article-hiding)
158
159 (defcustom gnus-visible-headers
160   "^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:"
161   "*All headers that do not match this regexp will be hidden.
162 This variable can also be a list of regexp of headers to remain visible.
163 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
164   :type '(repeat :value-to-internal (lambda (widget value)
165                                       (custom-split-regexp-maybe value))
166                  :match (lambda (widget value)
167                           (or (stringp value)
168                               (widget-editable-list-match widget value)))
169                  regexp)
170   :group 'gnus-article-hiding)
171
172 (defcustom gnus-sorted-header-list
173   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
174     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
175   "*This variable is a list of regular expressions.
176 If it is non-nil, headers that match the regular expressions will
177 be placed first in the article buffer in the sequence specified by
178 this list."
179   :type '(repeat regexp)
180   :group 'gnus-article-hiding)
181
182 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
183   "Headers that are only to be displayed if they have interesting data.
184 Possible values in this list are:
185
186   'empty       Headers with no content.
187   'newsgroups  Newsgroup identical to Gnus group.
188   'to-address  To identical to To-address.
189   'followup-to Followup-to identical to Newsgroups.
190   'reply-to    Reply-to identical to From.
191   'date        Date less than four days old.
192   'long-to     To and/or Cc longer than 1024 characters.
193   'many-to     Multiple To and/or Cc."
194   :type '(set (const :tag "Headers with no content." empty)
195               (const :tag "Newsgroups identical to Gnus group." newsgroups)
196               (const :tag "To identical to To-address." to-address)
197               (const :tag "Followup-to identical to Newsgroups." followup-to)
198               (const :tag "Reply-to identical to From." reply-to)
199               (const :tag "Date less than four days old." date)
200               (const :tag "To and/or Cc longer than 1024 characters." long-to)
201               (const :tag "Multiple To and/or Cc headers." many-to))
202   :group 'gnus-article-hiding)
203
204 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
205   "Regexp matching signature separator.
206 This can also be a list of regexps.  In that case, it will be checked
207 from head to tail looking for a separator.  Searches will be done from
208 the end of the buffer."
209   :type '(repeat string)
210   :group 'gnus-article-signature)
211
212 (defcustom gnus-signature-limit nil
213   "Provide a limit to what is considered a signature.
214 If it is a number, no signature may not be longer (in characters) than
215 that number.  If it is a floating point number, no signature may be
216 longer (in lines) than that number.  If it is a function, the function
217 will be called without any parameters, and if it returns nil, there is
218 no signature in the buffer.  If it is a string, it will be used as a
219 regexp.  If it matches, the text in question is not a signature."
220   :type '(choice (integer :value 200)
221                  (number :value 4.0)
222                  (function :value fun)
223                  (regexp :value ".*"))
224   :group 'gnus-article-signature)
225
226 (defcustom gnus-hidden-properties '(invisible t intangible t)
227   "Property list to use for hiding text."
228   :type 'sexp
229   :group 'gnus-article-hiding)
230
231 ;; Fixme: This isn't the right thing for mixed graphical and and
232 ;; non-graphical frames in a session.
233 ;; gnus-xmas.el overrides this for XEmacs.
234 (defcustom gnus-article-x-face-command
235   (cond
236    ((and (fboundp 'image-type-available-p)
237          (module-installed-p 'x-face-e21))
238     'x-face-decode-message-header)
239    ((and (fboundp 'image-type-available-p)
240          (image-type-available-p 'xbm))
241     'gnus-article-display-xface)
242    ((and (not (featurep 'xemacs))
243          window-system
244          (module-installed-p 'x-face-mule))
245     'x-face-mule-gnus-article-display-x-face)
246    (gnus-article-compface-xbm
247     "{ echo '/* Width=48, Height=48 */'; uncompface; } | display -")
248    (t
249     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
250 display -"))
251   "*String or function to be executed to display an X-Face header.
252 If it is a string, the command will be executed in a sub-shell
253 asynchronously.  The compressed face will be piped to this command."
254   :type '(choice string
255                  (function-item
256                   :tag "x-face-decode-message-header (x-face-e21)"
257                   x-face-decode-message-header)
258                  (function-item gnus-article-display-xface)
259                  (function-item x-face-mule-gnus-article-display-x-face)
260                  function)
261   ;;:version "21.1"
262   :group 'gnus-article-washing)
263
264 (defcustom gnus-article-x-face-too-ugly nil
265   "Regexp matching posters whose face shouldn't be shown automatically."
266   :type '(choice regexp (const nil))
267   :group 'gnus-article-washing)
268
269 (defcustom gnus-article-banner-alist nil
270   "Banner alist for stripping.
271 For example,
272      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
273   :version "21.1"
274   :type '(repeat (cons symbol regexp))
275   :group 'gnus-article-washing)
276
277 (gnus-define-group-parameter
278  banner
279  :variable-document
280  "Alist of regexps (to match group names) and banner."
281  :variable-group gnus-article-washing
282  :parameter-type
283  '(choice :tag "Banner"
284           :value nil
285           (const :tag "Remove signature" signature)
286           (symbol :tag "Item in `gnus-article-banner-alist'" none)
287           regexp
288           (const :tag "None" nil))
289  :parameter-document
290  "If non-nil, specify how to remove `banners' from articles.
291
292 Symbol `signature' means to remove signatures delimited by
293 `gnus-signature-separator'.  Any other symbol is used to look up a
294 regular expression to match the banner in `gnus-article-banner-alist'.
295 A string is used as a regular expression to match the banner
296 directly.")
297
298 (defcustom gnus-emphasis-alist
299   (let ((format
300          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
301         (types
302          '(("_" "_" underline)
303            ("/" "/" italic)
304            ("\\*" "\\*" bold)
305            ("_/" "/_" underline-italic)
306            ("_\\*" "\\*_" underline-bold)
307            ("\\*/" "/\\*" bold-italic)
308            ("_\\*/" "/\\*_" underline-bold-italic))))
309     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
310        2 3 gnus-emphasis-underline)
311       ,@(mapcar
312          (lambda (spec)
313            (list
314             (format format (car spec) (car (cdr spec)))
315             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
316          types)))
317   "*Alist that says how to fontify certain phrases.
318 Each item looks like this:
319
320   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
321
322 The first element is a regular expression to be matched.  The second
323 is a number that says what regular expression grouping used to find
324 the entire emphasized word.  The third is a number that says what
325 regexp grouping should be displayed and highlighted.  The fourth
326 is the face used for highlighting."
327   :type '(repeat (list :value ("" 0 0 default)
328                        regexp
329                        (integer :tag "Match group")
330                        (integer :tag "Emphasize group")
331                        face))
332   :group 'gnus-article-emphasis)
333
334 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
335   "A regexp to describe whitespace which should not be emphasized.
336 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
337 The former avoids underlining of leading and trailing whitespace,
338 and the latter avoids underlining any whitespace at all."
339   :version "21.1"
340   :group 'gnus-article-emphasis
341   :type 'regexp)
342
343 (defface gnus-emphasis-bold '((t (:bold t)))
344   "Face used for displaying strong emphasized text (*word*)."
345   :group 'gnus-article-emphasis)
346
347 (defface gnus-emphasis-italic '((t (:italic t)))
348   "Face used for displaying italic emphasized text (/word/)."
349   :group 'gnus-article-emphasis)
350
351 (defface gnus-emphasis-underline '((t (:underline t)))
352   "Face used for displaying underlined emphasized text (_word_)."
353   :group 'gnus-article-emphasis)
354
355 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
356   "Face used for displaying underlined bold emphasized text (_*word*_)."
357   :group 'gnus-article-emphasis)
358
359 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
360   "Face used for displaying underlined italic emphasized text (_/word/_)."
361   :group 'gnus-article-emphasis)
362
363 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
364   "Face used for displaying bold italic emphasized text (/*word*/)."
365   :group 'gnus-article-emphasis)
366
367 (defface gnus-emphasis-underline-bold-italic
368   '((t (:bold t :italic t :underline t)))
369   "Face used for displaying underlined bold italic emphasized text.
370 Esample: (_/*word*/_)."
371   :group 'gnus-article-emphasis)
372
373 (defface gnus-emphasis-highlight-words
374   '((t (:background "black" :foreground "yellow")))
375   "Face used for displaying highlighted words."
376   :group 'gnus-article-emphasis)
377
378 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
379   "Format for display of Date headers in article bodies.
380 See `format-time-string' for the possible values.
381
382 The variable can also be function, which should return a complete Date
383 header.  The function is called with one argument, the time, which can
384 be fed to `format-time-string'."
385   :type '(choice string symbol)
386   :link '(custom-manual "(gnus)Article Date")
387   :group 'gnus-article-washing)
388
389 (eval-and-compile
390   (autoload 'mail-extract-address-components "mail-extr"))
391
392 (defcustom gnus-save-all-headers t
393   "*If non-nil, don't remove any headers before saving."
394   :group 'gnus-article-saving
395   :type 'boolean)
396
397 (defcustom gnus-prompt-before-saving 'always
398   "*This variable says how much prompting is to be done when saving articles.
399 If it is nil, no prompting will be done, and the articles will be
400 saved to the default files.  If this variable is `always', each and
401 every article that is saved will be preceded by a prompt, even when
402 saving large batches of articles.  If this variable is neither nil not
403 `always', there the user will be prompted once for a file name for
404 each invocation of the saving commands."
405   :group 'gnus-article-saving
406   :type '(choice (item always)
407                  (item :tag "never" nil)
408                  (sexp :tag "once" :format "%t\n" :value t)))
409
410 (defcustom gnus-saved-headers gnus-visible-headers
411   "Headers to keep if `gnus-save-all-headers' is nil.
412 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
413 If that variable is nil, however, all headers that match this regexp
414 will be kept while the rest will be deleted before saving."
415   :group 'gnus-article-saving
416   :type 'regexp)
417
418 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
419   "A function to save articles in your favourite format.
420 The function must be interactively callable (in other words, it must
421 be an Emacs command).
422
423 Gnus provides the following functions:
424
425 * gnus-summary-save-in-rmail (Rmail format)
426 * gnus-summary-save-in-mail (Unix mail format)
427 * gnus-summary-save-in-folder (MH folder)
428 * gnus-summary-save-in-file (article format)
429 * gnus-summary-save-in-vm (use VM's folder format)
430 * gnus-summary-write-to-file (article format -- overwrite)."
431   :group 'gnus-article-saving
432   :type '(radio (function-item gnus-summary-save-in-rmail)
433                 (function-item gnus-summary-save-in-mail)
434                 (function-item gnus-summary-save-in-folder)
435                 (function-item gnus-summary-save-in-file)
436                 (function-item gnus-summary-save-in-vm)
437                 (function-item gnus-summary-write-to-file)))
438
439 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
440   "A function generating a file name to save articles in Rmail format.
441 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
442   :group 'gnus-article-saving
443   :type 'function)
444
445 (defcustom gnus-mail-save-name 'gnus-plain-save-name
446   "A function generating a file name to save articles in Unix mail format.
447 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
448   :group 'gnus-article-saving
449   :type 'function)
450
451 (defcustom gnus-folder-save-name 'gnus-folder-save-name
452   "A function generating a file name to save articles in MH folder.
453 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
454   :group 'gnus-article-saving
455   :type 'function)
456
457 (defcustom gnus-file-save-name 'gnus-numeric-save-name
458   "A function generating a file name to save articles in article format.
459 The function is called with NEWSGROUP, HEADERS, and optional
460 LAST-FILE."
461   :group 'gnus-article-saving
462   :type 'function)
463
464 (defcustom gnus-split-methods
465   '((gnus-article-archive-name)
466     (gnus-article-nndoc-name))
467   "*Variable used to suggest where articles are to be saved.
468 For instance, if you would like to save articles related to Gnus in
469 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
470 you could set this variable to something like:
471
472  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
473    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
474
475 This variable is an alist where the where the key is the match and the
476 value is a list of possible files to save in if the match is non-nil.
477
478 If the match is a string, it is used as a regexp match on the
479 article.  If the match is a symbol, that symbol will be funcalled
480 from the buffer of the article to be saved with the newsgroup as the
481 parameter.  If it is a list, it will be evaled in the same buffer.
482
483 If this form or function returns a string, this string will be used as
484 a possible file name; and if it returns a non-nil list, that list will
485 be used as possible file names."
486   :group 'gnus-article-saving
487   :type '(repeat (choice (list :value (fun) function)
488                          (cons :value ("" "") regexp (repeat string))
489                          (sexp :value nil))))
490
491 (defcustom gnus-article-display-method-for-mime
492   'gnus-article-display-mime-message
493   "Function to display a MIME message.
494 The function is called from the article buffer."
495   :group 'gnus-article-mime
496   :type 'function)
497
498 (defcustom gnus-article-display-method-for-traditional
499   'gnus-article-display-traditional-message
500   "*Function to display a traditional message.
501 The function is called from the article buffer."
502   :group 'gnus-article-mime
503   :type 'function)
504
505 (defcustom gnus-page-delimiter "^\^L"
506   "*Regexp describing what to use as article page delimiters.
507 The default value is \"^\^L\", which is a form linefeed at the
508 beginning of a line."
509   :type 'regexp
510   :group 'gnus-article-various)
511
512 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
513   "*The format specification for the article mode line.
514 See `gnus-summary-mode-line-format' for a closer description.
515
516 The following additional specs are available:
517
518 %w  The article washing status.
519 %m  The number of MIME parts in the article."
520   :type 'string
521   :group 'gnus-article-various)
522
523 (defcustom gnus-article-mode-hook nil
524   "*A hook for Gnus article mode."
525   :type 'hook
526   :group 'gnus-article-various)
527
528 (when (featurep 'xemacs)
529   ;; Extracted from gnus-xmas-define in order to preserve user settings
530   (when (fboundp 'turn-off-scroll-in-place)
531     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
532   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
533   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
534
535 (defcustom gnus-article-menu-hook nil
536   "*Hook run after the creation of the article mode menu."
537   :type 'hook
538   :group 'gnus-article-various)
539
540 (defcustom gnus-article-prepare-hook nil
541   "*A hook called after an article has been prepared in the article buffer."
542   :type 'hook
543   :group 'gnus-article-various)
544
545 (defcustom gnus-article-hide-pgp-hook nil
546   "*A hook called after successfully hiding a PGP signature."
547   :type 'hook
548   :group 'gnus-article-various)
549
550 (defcustom gnus-article-button-face 'bold
551   "Face used for highlighting buttons in the article buffer.
552
553 An article button is a piece of text that you can activate by pressing
554 `RET' or `mouse-2' above it."
555   :type 'face
556   :group 'gnus-article-buttons)
557
558 (defcustom gnus-article-mouse-face 'highlight
559   "Face used for mouse highlighting in the article buffer.
560
561 Article buttons will be displayed in this face when the cursor is
562 above them."
563   :type 'face
564   :group 'gnus-article-buttons)
565
566 (defcustom gnus-signature-face 'gnus-signature-face
567   "Face used for highlighting a signature in the article buffer.
568 Obsolete; use the face `gnus-signature-face' for customizations instead."
569   :type 'face
570   :group 'gnus-article-highlight
571   :group 'gnus-article-signature)
572
573 (defface gnus-signature-face
574   '((t
575      (:italic t)))
576   "Face used for highlighting a signature in the article buffer."
577   :group 'gnus-article-highlight
578   :group 'gnus-article-signature)
579
580 (defface gnus-header-from-face
581   '((((class color)
582       (background dark))
583      (:foreground "spring green"))
584     (((class color)
585       (background light))
586      (:foreground "red3"))
587     (t
588      (:italic t)))
589   "Face used for displaying from headers."
590   :group 'gnus-article-headers
591   :group 'gnus-article-highlight)
592
593 (defface gnus-header-subject-face
594   '((((class color)
595       (background dark))
596      (:foreground "SeaGreen3"))
597     (((class color)
598       (background light))
599      (:foreground "red4"))
600     (t
601      (:bold t :italic t)))
602   "Face used for displaying subject headers."
603   :group 'gnus-article-headers
604   :group 'gnus-article-highlight)
605
606 (defface gnus-header-newsgroups-face
607   '((((class color)
608       (background dark))
609      (:foreground "yellow" :italic t))
610     (((class color)
611       (background light))
612      (:foreground "MidnightBlue" :italic t))
613     (t
614      (:italic t)))
615   "Face used for displaying newsgroups headers."
616   :group 'gnus-article-headers
617   :group 'gnus-article-highlight)
618
619 (defface gnus-header-name-face
620   '((((class color)
621       (background dark))
622      (:foreground "SeaGreen"))
623     (((class color)
624       (background light))
625      (:foreground "maroon"))
626     (t
627      (:bold t)))
628   "Face used for displaying header names."
629   :group 'gnus-article-headers
630   :group 'gnus-article-highlight)
631
632 (defface gnus-header-content-face
633   '((((class color)
634       (background dark))
635      (:foreground "forest green" :italic t))
636     (((class color)
637       (background light))
638      (:foreground "indianred4" :italic t))
639     (t
640      (:italic t)))
641   "Face used for displaying header content."
642   :group 'gnus-article-headers
643   :group 'gnus-article-highlight)
644
645 (defcustom gnus-header-face-alist
646   '(("From" nil gnus-header-from-face)
647     ("Subject" nil gnus-header-subject-face)
648     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
649     ("" gnus-header-name-face gnus-header-content-face))
650   "*Controls highlighting of article header.
651
652 An alist of the form (HEADER NAME CONTENT).
653
654 HEADER is a regular expression which should match the name of an
655 header header and NAME and CONTENT are either face names or nil.
656
657 The name of each header field will be displayed using the face
658 specified by the first element in the list where HEADER match the
659 header name and NAME is non-nil.  Similarly, the content will be
660 displayed by the first non-nil matching CONTENT face."
661   :group 'gnus-article-headers
662   :group 'gnus-article-highlight
663   :type '(repeat (list (regexp :tag "Header")
664                        (choice :tag "Name"
665                                (item :tag "skip" nil)
666                                (face :value default))
667                        (choice :tag "Content"
668                                (item :tag "skip" nil)
669                                (face :value default)))))
670
671 (defcustom gnus-article-decode-hook nil
672   "*Hook run to decode charsets in articles."
673   :group 'gnus-article-headers
674   :type 'hook)
675
676 (defcustom gnus-display-mime-function 'gnus-display-mime
677   "Function to display MIME articles."
678   :group 'gnus-article-mime
679   :type 'function)
680
681 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
682   "Function used to decode headers.")
683
684 (defvar gnus-article-dumbquotes-map
685   '(("\200" "EUR")
686     ("\202" ",")
687     ("\203" "f")
688     ("\204" ",,")
689     ("\205" "...")
690     ("\213" "<")
691     ("\214" "OE")
692     ("\221" "`")
693     ("\222" "'")
694     ("\223" "``")
695     ("\224" "\"")
696     ("\225" "*")
697     ("\226" "-")
698     ("\227" "--")
699     ("\231" "(TM)")
700     ("\233" ">")
701     ("\234" "oe")
702     ("\264" "'"))
703   "Table for MS-to-Latin1 translation.")
704
705 (defcustom gnus-ignored-mime-types nil
706   "List of MIME types that should be ignored by Gnus."
707   :version "21.1"
708   :group 'gnus-article-mime
709   :type '(repeat regexp))
710
711 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
712   "List of MIME types that should not be given buttons when rendered inline.
713 See also `gnus-buttonized-mime-types' which may override this variable."
714   :version "21.1"
715   :group 'gnus-article-mime
716   :type '(repeat regexp))
717
718 (defcustom gnus-buttonized-mime-types nil
719   "List of MIME types that should be given buttons when rendered inline.
720 If set, this variable overrides `gnus-unbuttonized-mime-types'.
721 To see e.g. security buttons you could set this to
722 `(\"multipart/signed\")'."
723   :version "21.1"
724   :group 'gnus-article-mime
725   :type '(repeat regexp))
726
727 (defcustom gnus-article-mime-part-function nil
728   "Function called with a MIME handle as the argument.
729 This is meant for people who want to do something automatic based
730 on parts -- for instance, adding Vcard info to a database."
731   :group 'gnus-article-mime
732   :type 'function)
733
734 (defcustom gnus-mime-multipart-functions nil
735   "An alist of MIME types to functions to display them."
736   :version "21.1"
737   :group 'gnus-article-mime
738   :type 'alist)
739
740 (defcustom gnus-article-date-lapsed-new-header nil
741   "Whether the X-Sent and Date headers can coexist.
742 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
743 either replace the old \"Date:\" header (if this variable is nil), or
744 be added below it (otherwise)."
745   :version "21.1"
746   :group 'gnus-article-headers
747   :type 'boolean)
748
749 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
750   "Function called with a MIME handle as the argument.
751 This is meant for people who want to view first matched part.
752 For `undisplayed-alternative' (default), the first undisplayed
753 part or alternative part is used.  For `undisplayed', the first
754 undisplayed part is used.  For a function, the first part which
755 the function return `t' is used.  For `nil', the first part is
756 used."
757   :version "21.1"
758   :group 'gnus-article-mime
759   :type '(choice
760           (item :tag "first" :value nil)
761           (item :tag "undisplayed" :value undisplayed)
762           (item :tag "undisplayed or alternative"
763                 :value undisplayed-alternative)
764           (function)))
765
766 (defcustom gnus-mime-action-alist
767   '(("save to file" . gnus-mime-save-part)
768     ("save and strip" . gnus-mime-save-part-and-strip)
769     ("display as text" . gnus-mime-inline-part)
770     ("view the part" . gnus-mime-view-part)
771     ("pipe to command" . gnus-mime-pipe-part)
772     ("toggle display" . gnus-article-press-button)
773     ("toggle display" . gnus-article-view-part-as-charset)
774     ("view as type" . gnus-mime-view-part-as-type)
775     ("internalize type" . gnus-mime-internalize-part)
776     ("externalize type" . gnus-mime-externalize-part))
777   "An alist of actions that run on the MIME attachment."
778   :group 'gnus-article-mime
779   :type '(repeat (cons (string :tag "name")
780                        (function))))
781
782 (defcustom gnus-mime-action-alist
783   '(("save to file" . gnus-mime-save-part)
784     ("display as text" . gnus-mime-inline-part)
785     ("view the part" . gnus-mime-view-part)
786     ("pipe to command" . gnus-mime-pipe-part)
787     ("toggle display" . gnus-article-press-button)
788     ("view as type" . gnus-mime-view-part-as-type)
789     ("internalize type" . gnus-mime-internalize-part)
790     ("externalize type" . gnus-mime-externalize-part))
791   "An alist of actions that run on the MIME attachment."
792   :version "21.1"
793   :group 'gnus-article-mime
794   :type '(repeat (cons (string :tag "name")
795                        (function))))
796
797 ;;;
798 ;;; The treatment variables
799 ;;;
800
801 (defvar gnus-part-display-hook nil
802   "Hook called on parts that are to receive treatment.")
803
804 (defvar gnus-article-treat-custom
805   '(choice (const :tag "Off" nil)
806            (const :tag "On" t)
807            (const :tag "Header" head)
808            (const :tag "Last" last)
809            (const :tag "Mime" mime)
810            (integer :tag "Less")
811            (repeat :tag "Groups" regexp)
812            (sexp :tag "Predicate")))
813
814 (defvar gnus-article-treat-head-custom
815   '(choice (const :tag "Off" nil)
816            (const :tag "Header" head)))
817
818 (defvar gnus-article-treat-types '("text/plain")
819   "Parts to treat.")
820
821 (defvar gnus-inhibit-treatment nil
822   "Whether to inhibit treatment.")
823
824 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
825   "Highlight the signature.
826 Valid values are nil, t, `head', `last', an integer or a predicate.
827 See the manual for details."
828   :group 'gnus-article-treat
829   :type gnus-article-treat-custom)
830 (put 'gnus-treat-highlight-signature 'highlight t)
831
832 (defcustom gnus-treat-buttonize 100000
833   "Add buttons.
834 Valid values are nil, t, `head', `last', an integer or a predicate.
835 See the manual for details."
836   :group 'gnus-article-treat
837   :type gnus-article-treat-custom)
838 (put 'gnus-treat-buttonize 'highlight t)
839
840 (defcustom gnus-treat-buttonize-head 'head
841   "Add buttons to the head.
842 Valid values are nil, t, `head', `last', an integer or a predicate.
843 See the manual for details."
844   :group 'gnus-article-treat
845   :type gnus-article-treat-head-custom)
846 (put 'gnus-treat-buttonize-head 'highlight t)
847
848 (defcustom gnus-treat-emphasize
849   (and (or window-system
850            (featurep 'xemacs)
851            (>= (string-to-number emacs-version) 21))
852        50000)
853   "Emphasize text.
854 Valid values are nil, t, `head', `last', an integer or a predicate.
855 See the manual for details."
856   :group 'gnus-article-treat
857   :type gnus-article-treat-custom)
858 (put 'gnus-treat-emphasize 'highlight t)
859
860 (defcustom gnus-treat-strip-cr nil
861   "Remove carriage returns.
862 Valid values are nil, t, `head', `last', an integer or a predicate.
863 See the manual for details."
864   :group 'gnus-article-treat
865   :type gnus-article-treat-custom)
866
867 (defcustom gnus-treat-leading-whitespace nil
868   "Remove leading whitespace in headers.
869 Valid values are nil, t, `head', `last', an integer or a predicate.
870 See the manual for details."
871   :group 'gnus-article-treat
872   :type gnus-article-treat-custom)
873
874 (defcustom gnus-treat-hide-headers 'head
875   "Hide headers.
876 Valid values are nil, t, `head', `last', an integer or a predicate.
877 See the manual for details."
878   :group 'gnus-article-treat
879   :type gnus-article-treat-head-custom)
880
881 (defcustom gnus-treat-hide-boring-headers nil
882   "Hide boring headers.
883 Valid values are nil, t, `head', `last', an integer or a predicate.
884 See the manual for details."
885   :group 'gnus-article-treat
886   :type gnus-article-treat-head-custom)
887
888 (defcustom gnus-treat-hide-signature nil
889   "Hide the signature.
890 Valid values are nil, t, `head', `last', an integer or a predicate.
891 See the manual for details."
892   :group 'gnus-article-treat
893   :type gnus-article-treat-custom)
894
895 (defcustom gnus-treat-fill-article nil
896   "Fill the article.
897 Valid values are nil, t, `head', `last', an integer or a predicate.
898 See the manual for details."
899   :group 'gnus-article-treat
900   :type gnus-article-treat-custom)
901
902 (defcustom gnus-treat-hide-citation nil
903   "Hide cited text.
904 Valid values are nil, t, `head', `last', an integer or a predicate.
905 See the manual for details."
906   :group 'gnus-article-treat
907   :type gnus-article-treat-custom)
908
909 (defcustom gnus-treat-hide-citation-maybe nil
910   "Hide cited text.
911 Valid values are nil, t, `head', `last', an integer or a predicate.
912 See the manual for details."
913   :group 'gnus-article-treat
914   :type gnus-article-treat-custom)
915
916 (defcustom gnus-treat-hide-citation-maybe nil
917   "Hide cited text.
918 Valid values are nil, t, `head', `last', an integer or a predicate.
919 See the manual for details."
920   :group 'gnus-article-treat
921   :type gnus-article-treat-custom)
922
923 (defcustom gnus-treat-strip-list-identifiers 'head
924   "Strip list identifiers from `gnus-list-identifiers`.
925 Valid values are nil, t, `head', `last', an integer or a predicate.
926 See the manual for details."
927   :version "21.1"
928   :group 'gnus-article-treat
929   :type gnus-article-treat-custom)
930
931 (defcustom gnus-treat-strip-pgp t
932   "Strip PGP signatures.
933 Valid values are nil, t, `head', `last', an integer or a predicate.
934 See the manual for details."
935   :group 'gnus-article-treat
936   :type gnus-article-treat-custom)
937
938 (defcustom gnus-treat-strip-pem nil
939   "Strip PEM signatures.
940 Valid values are nil, t, `head', `last', an integer or a predicate.
941 See the manual for details."
942   :group 'gnus-article-treat
943   :type gnus-article-treat-custom)
944
945 (defcustom gnus-treat-strip-banner t
946   "Strip banners from articles.
947 The banner to be stripped is specified in the `banner' group parameter.
948 Valid values are nil, t, `head', `last', an integer or a predicate.
949 See the manual for details."
950   :group 'gnus-article-treat
951   :type gnus-article-treat-custom)
952
953 (defcustom gnus-treat-highlight-headers 'head
954   "Highlight the headers.
955 Valid values are nil, t, `head', `last', an integer or a predicate.
956 See the manual for details."
957   :group 'gnus-article-treat
958   :type gnus-article-treat-head-custom)
959 (put 'gnus-treat-highlight-headers 'highlight t)
960
961 (defcustom gnus-treat-highlight-citation t
962   "Highlight cited text.
963 Valid values are nil, t, `head', `last', an integer or a predicate.
964 See the manual for details."
965   :group 'gnus-article-treat
966   :type gnus-article-treat-custom)
967 (put 'gnus-treat-highlight-citation 'highlight t)
968
969 (defcustom gnus-treat-date-ut nil
970   "Display the Date in UT (GMT).
971 Valid values are nil, t, `head', `last', an integer or a predicate.
972 See the manual for details."
973   :group 'gnus-article-treat
974   :type gnus-article-treat-head-custom)
975
976 (defcustom gnus-treat-date-local nil
977   "Display the Date in the local timezone.
978 Valid values are nil, t, `head', `last', an integer or a predicate.
979 See the manual for details."
980   :group 'gnus-article-treat
981   :type gnus-article-treat-head-custom)
982
983 (defcustom gnus-treat-date-english nil
984   "Display the Date in a format that can be read aloud in English.
985 Valid values are nil, t, `head', `last', an integer or a predicate.
986 See the manual for details."
987   :group 'gnus-article-treat
988   :type gnus-article-treat-head-custom)
989
990 (defcustom gnus-treat-date-lapsed nil
991   "Display the Date header in a way that says how much time has elapsed.
992 Valid values are nil, t, `head', `last', an integer or a predicate.
993 See the manual for details."
994   :group 'gnus-article-treat
995   :type gnus-article-treat-head-custom)
996
997 (defcustom gnus-treat-date-original nil
998   "Display the date in the original timezone.
999 Valid values are nil, t, `head', `last', an integer or a predicate.
1000 See the manual for details."
1001   :group 'gnus-article-treat
1002   :type gnus-article-treat-head-custom)
1003
1004 (defcustom gnus-treat-date-iso8601 nil
1005   "Display the date in the ISO8601 format.
1006 Valid values are nil, t, `head', `last', an integer or a predicate.
1007 See the manual for details."
1008   :version "21.1"
1009   :group 'gnus-article-treat
1010   :type gnus-article-treat-head-custom)
1011
1012 (defcustom gnus-treat-date-user-defined nil
1013   "Display the date in a user-defined format.
1014 The format is defined by the `gnus-article-time-format' variable.
1015 Valid values are nil, t, `head', `last', an integer or a predicate.
1016 See the manual for details."
1017   :group 'gnus-article-treat
1018   :type gnus-article-treat-head-custom)
1019
1020 (defcustom gnus-treat-strip-headers-in-body t
1021   "Strip the X-No-Archive header line from the beginning of the body.
1022 Valid values are nil, t, `head', `last', an integer or a predicate.
1023 See the manual for details."
1024   :version "21.1"
1025   :group 'gnus-article-treat
1026   :type gnus-article-treat-custom)
1027
1028 (defcustom gnus-treat-strip-trailing-blank-lines nil
1029   "Strip trailing blank lines.
1030 Valid values are nil, t, `head', `last', an integer or a predicate.
1031 See the manual for details."
1032   :group 'gnus-article-treat
1033   :type gnus-article-treat-custom)
1034
1035 (defcustom gnus-treat-strip-leading-blank-lines nil
1036   "Strip leading blank lines.
1037 Valid values are nil, t, `head', `last', an integer or a predicate.
1038 See the manual for details."
1039   :group 'gnus-article-treat
1040   :type gnus-article-treat-custom)
1041
1042 (defcustom gnus-treat-strip-multiple-blank-lines nil
1043   "Strip multiple blank lines.
1044 Valid values are nil, t, `head', `last', an integer or a predicate.
1045 See the manual for details."
1046   :group 'gnus-article-treat
1047   :type gnus-article-treat-custom)
1048
1049 (defcustom gnus-treat-overstrike t
1050   "Treat overstrike highlighting.
1051 Valid values are nil, t, `head', `last', an integer or a predicate.
1052 See the manual for details."
1053   :group 'gnus-article-treat
1054   :type gnus-article-treat-custom)
1055 (put 'gnus-treat-overstrike 'highlight t)
1056
1057 (defcustom gnus-treat-display-xface
1058   (and (or (and (fboundp 'image-type-available-p)
1059                 (image-type-available-p 'xbm)
1060                 (string-match "^0x" (shell-command-to-string "uncompface")))
1061            (and (featurep 'xemacs) (featurep 'xface))
1062            (eq 'x-face-mule-gnus-article-display-x-face
1063                gnus-article-x-face-command))
1064        'head)
1065   "Display X-Face headers.
1066 Valid values are nil, t, `head', `last', an integer or a predicate.
1067 See the manual for details."
1068   :group 'gnus-article-treat
1069   ;;:version "21.1"
1070   :type gnus-article-treat-head-custom)
1071 (put 'gnus-treat-display-xface 'highlight t)
1072
1073 (defcustom gnus-treat-display-smileys
1074   (if (or (and (featurep 'xemacs)
1075                (featurep 'xpm))
1076           (and (fboundp 'image-type-available-p)
1077                (image-type-available-p 'pbm))
1078           (and (not (featurep 'xemacs))
1079                window-system
1080                (module-installed-p 'smiley-mule)))
1081       t
1082     nil)
1083   "Display smileys.
1084 Valid values are nil, t, `head', `last', an integer or a predicate.
1085 See the manual for details."
1086   :group 'gnus-article-treat
1087   ;;:version "21.1"
1088   :type gnus-article-treat-custom)
1089 (put 'gnus-treat-display-smileys 'highlight t)
1090
1091 (defcustom gnus-treat-display-picons (if (featurep 'xemacs) 'head nil)
1092   "Display picons.
1093 Valid values are nil, t, `head', `last', an integer or a predicate.
1094 See the manual for details."
1095   :group 'gnus-article-treat
1096   :type gnus-article-treat-head-custom)
1097 (put 'gnus-treat-display-picons 'highlight t)
1098
1099 (defcustom gnus-treat-capitalize-sentences nil
1100   "Capitalize sentence-starting words.
1101 Valid values are nil, t, `head', `last', an integer or a predicate.
1102 See the manual for details."
1103   :version "21.1"
1104   :group 'gnus-article-treat
1105   :type gnus-article-treat-custom)
1106
1107 (defcustom gnus-treat-fill-long-lines nil
1108   "Fill long lines.
1109 Valid values are nil, t, `head', `last', an integer or a predicate.
1110 See the manual for details."
1111   :group 'gnus-article-treat
1112   :type gnus-article-treat-custom)
1113
1114 (defcustom gnus-treat-play-sounds nil
1115   "Play sounds.
1116 Valid values are nil, t, `head', `last', an integer or a predicate.
1117 See the manual for details."
1118   :version "21.1"
1119   :group 'gnus-article-treat
1120   :type gnus-article-treat-custom)
1121
1122 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1123   "Decode an article as `default-mime-charset'.  For instance, if you want to
1124 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1125 you could set this variable to something like: nil for don't decode, t for
1126 decode the body, '(or header t) for the whole article, etc."
1127   :group 'gnus-article-treat
1128   :type '(radio (const :tag "Off" nil)
1129                 (const :tag "Decode body" t)
1130                 (const :tag "Decode all" (or head t))))
1131
1132 (defcustom gnus-treat-translate nil
1133   "Translate articles from one language to another.
1134 Valid values are nil, t, `head', `last', an integer or a predicate.
1135 See the manual for details."
1136   :version "21.1"
1137   :group 'gnus-article-treat
1138   :type gnus-article-treat-custom)
1139
1140 (defcustom gnus-treat-x-pgp-sig nil
1141   "Verify X-PGP-Sig.
1142 To automatically treat X-PGP-Sig, set it to head.
1143 Valid values are nil, t, `head', `last', an integer or a predicate.
1144 See the manual for details."
1145   :group 'gnus-article-treat
1146   :group 'mime-security
1147   :type gnus-article-treat-custom)
1148
1149 (defvar gnus-article-encrypt-protocol-alist
1150   '(("PGP" . mml2015-self-encrypt)))
1151
1152 ;; Set to nil if more than one protocol added to
1153 ;; gnus-article-encrypt-protocol-alist.
1154 (defcustom gnus-article-encrypt-protocol "PGP"
1155   "The protocol used for encrypt articles.
1156 It is a string, such as \"PGP\". If nil, ask user."
1157   :type 'string
1158   :group 'mime-security)
1159
1160 ;;; Internal variables
1161
1162 (defvar gnus-english-month-names
1163   '("January" "February" "March" "April" "May" "June" "July" "August"
1164     "September" "October" "November" "December"))
1165
1166 (defvar article-goto-body-goes-to-point-min-p nil)
1167 (defvar gnus-article-wash-types nil)
1168 (defvar gnus-article-emphasis-alist nil)
1169
1170 (defvar gnus-article-mime-handle-alist-1 nil)
1171 (defvar gnus-treatment-function-alist
1172   `((gnus-treat-decode-article-as-default-mime-charset
1173      gnus-article-decode-article-as-default-mime-charset)
1174     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1175     (gnus-treat-strip-banner gnus-article-strip-banner)
1176     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1177     (gnus-treat-buttonize gnus-article-add-buttons)
1178     (gnus-treat-fill-article gnus-article-fill-cited-article)
1179     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1180     (gnus-treat-strip-cr gnus-article-remove-cr)
1181     (gnus-treat-display-xface gnus-article-display-x-face)
1182     (gnus-treat-date-ut gnus-article-date-ut)
1183     (gnus-treat-date-local gnus-article-date-local)
1184     (gnus-treat-date-english gnus-article-date-english)
1185     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1186     (gnus-treat-date-original gnus-article-date-original)
1187     (gnus-treat-date-user-defined gnus-article-date-user)
1188     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1189     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1190     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1191     (gnus-treat-hide-signature gnus-article-hide-signature)
1192     (gnus-treat-hide-citation gnus-article-hide-citation)
1193     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1194     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1195     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1196     (gnus-treat-strip-pgp gnus-article-hide-pgp)
1197     (gnus-treat-strip-pem gnus-article-hide-pem)
1198     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1199     (gnus-treat-emphasize gnus-article-emphasize)
1200     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1201     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1202     (gnus-treat-strip-trailing-blank-lines
1203      gnus-article-remove-trailing-blank-lines)
1204     (gnus-treat-strip-leading-blank-lines
1205      gnus-article-strip-leading-blank-lines)
1206     (gnus-treat-strip-multiple-blank-lines
1207      gnus-article-strip-multiple-blank-lines)
1208     (gnus-treat-overstrike gnus-article-treat-overstrike)
1209     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1210     (gnus-treat-display-smileys ,(if (or (featurep 'xemacs)
1211                                          (>= emacs-major-version 21))
1212                                      'gnus-smiley-display
1213                                    'gnus-article-smiley-display))
1214     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1215     (gnus-treat-display-picons gnus-article-display-picons)
1216     (gnus-treat-play-sounds gnus-earcon-display)))
1217
1218 (defvar gnus-article-mime-handle-alist nil)
1219 (defvar article-lapsed-timer nil)
1220 (defvar gnus-article-current-summary nil)
1221
1222 (defvar gnus-article-mode-syntax-table
1223   (let ((table (copy-syntax-table text-mode-syntax-table)))
1224     ;; This causes the citation match run O(2^n).
1225     ;; (modify-syntax-entry ?- "w" table)
1226     (modify-syntax-entry ?> ")" table)
1227     (modify-syntax-entry ?< "(" table)
1228     table)
1229   "Syntax table used in article mode buffers.
1230 Initialized from `text-mode-syntax-table.")
1231
1232 (defvar gnus-save-article-buffer nil)
1233
1234 (defvar gnus-article-mode-line-format-alist
1235   (nconc '((?w (gnus-article-wash-status) ?s)
1236            (?m (gnus-article-mime-part-status) ?s))
1237          gnus-summary-mode-line-format-alist))
1238
1239 (defvar gnus-number-of-articles-to-be-saved nil)
1240
1241 (defvar gnus-inhibit-hiding nil)
1242
1243 (defsubst gnus-article-hide-text (b e props)
1244   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1245   (gnus-add-text-properties-when 'article-type nil b e props)
1246   (when (memq 'intangible props)
1247     (put-text-property
1248      (max (1- b) (point-min))
1249      b 'intangible (cddr (memq 'intangible props)))))
1250
1251 (defsubst gnus-article-unhide-text (b e)
1252   "Remove hidden text properties from region between B and E."
1253   (remove-text-properties b e gnus-hidden-properties)
1254   (when (memq 'intangible gnus-hidden-properties)
1255     (put-text-property (max (1- b) (point-min))
1256                        b 'intangible nil)))
1257
1258 (defun gnus-article-hide-text-type (b e type)
1259   "Hide text of TYPE between B and E."
1260   (push type gnus-article-wash-types)
1261   (gnus-article-hide-text
1262    b e (cons 'article-type (cons type gnus-hidden-properties))))
1263
1264 (defun gnus-article-unhide-text-type (b e type)
1265   "Unhide text of TYPE between B and E."
1266   (setq gnus-article-wash-types
1267         (delq type gnus-article-wash-types))
1268   (remove-text-properties
1269    b e (cons 'article-type (cons type gnus-hidden-properties)))
1270   (when (memq 'intangible gnus-hidden-properties)
1271     (put-text-property (max (1- b) (point-min))
1272                        b 'intangible nil)))
1273
1274 (defun gnus-article-hide-text-of-type (type)
1275   "Hide text of TYPE in the current buffer."
1276   (save-excursion
1277     (let ((b (point-min))
1278           (e (point-max)))
1279       (while (setq b (text-property-any b e 'article-type type))
1280         (add-text-properties b (incf b) gnus-hidden-properties)))))
1281
1282 (defun gnus-article-delete-text-of-type (type)
1283   "Delete text of TYPE in the current buffer."
1284   (save-excursion
1285     (let ((b (point-min)))
1286       (while (setq b (text-property-any b (point-max) 'article-type type))
1287         (delete-region
1288          b (or (text-property-not-all b (point-max) 'article-type type)
1289                (point-max)))))))
1290
1291 (defun gnus-article-delete-invisible-text ()
1292   "Delete all invisible text in the current buffer."
1293   (save-excursion
1294     (let ((b (point-min)))
1295       (while (setq b (text-property-any b (point-max) 'invisible t))
1296         (delete-region
1297          b (or (text-property-not-all b (point-max) 'invisible t)
1298                (point-max)))))))
1299
1300 (defun gnus-article-text-type-exists-p (type)
1301   "Say whether any text of type TYPE exists in the buffer."
1302   (text-property-any (point-min) (point-max) 'article-type type))
1303
1304 (defsubst gnus-article-header-rank ()
1305   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1306   (let ((list gnus-sorted-header-list)
1307         (i 0))
1308     (while list
1309       (when (looking-at (car list))
1310         (setq list nil))
1311       (setq list (cdr list))
1312       (incf i))
1313     i))
1314
1315 (defun article-hide-headers (&optional arg delete)
1316   "Hide unwanted headers and possibly sort them as well."
1317   (interactive (gnus-article-hidden-arg))
1318   ;; Lars said that this function might be inhibited.
1319   (if (gnus-article-check-hidden-text 'headers arg)
1320       (progn
1321         ;; Show boring headers as well.
1322         (gnus-article-show-hidden-text 'boring-headers)
1323         (when (eq 1 (point-min))
1324           (set-window-start (get-buffer-window (current-buffer)) 1)))
1325     (unless gnus-inhibit-hiding
1326       (save-excursion
1327         (save-restriction
1328           (let ((inhibit-read-only t)
1329                 (case-fold-search t)
1330                 (max (1+ (length gnus-sorted-header-list)))
1331                 (ignored (when (not gnus-visible-headers)
1332                            (cond ((stringp gnus-ignored-headers)
1333                                   gnus-ignored-headers)
1334                                  ((listp gnus-ignored-headers)
1335                                   (mapconcat 'identity gnus-ignored-headers
1336                                              "\\|")))))
1337                 (visible
1338                  (cond ((stringp gnus-visible-headers)
1339                         gnus-visible-headers)
1340                        ((and gnus-visible-headers
1341                              (listp gnus-visible-headers))
1342                         (mapconcat 'identity gnus-visible-headers "\\|"))))
1343                 (inhibit-point-motion-hooks t)
1344                 beg)
1345             ;; First we narrow to just the headers.
1346             (article-narrow-to-head)
1347             ;; Hide any "From " lines at the beginning of (mail) articles.
1348             (while (looking-at "From ")
1349               (forward-line 1))
1350             (unless (bobp)
1351               (if delete
1352                   (delete-region (point-min) (point))
1353                 (gnus-article-hide-text (point-min) (point)
1354                                         (nconc (list 'article-type 'headers)
1355                                                gnus-hidden-properties))))
1356             ;; Then treat the rest of the header lines.
1357             ;; Then we use the two regular expressions
1358             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1359             ;; select which header lines is to remain visible in the
1360             ;; article buffer.
1361             (while (re-search-forward "^[^ \t:]*:" nil t)
1362               (beginning-of-line)
1363               ;; Mark the rank of the header.
1364               (put-text-property
1365                (point) (1+ (point)) 'message-rank
1366                (if (or (and visible (looking-at visible))
1367                        (and ignored
1368                             (not (looking-at ignored))))
1369                    (gnus-article-header-rank)
1370                  (+ 2 max)))
1371               (forward-line 1))
1372             (message-sort-headers-1)
1373             (when (setq beg (text-property-any
1374                              (point-min) (point-max) 'message-rank (+ 2 max)))
1375               ;; We delete or make invisible the unwanted headers.
1376               (push 'headers gnus-article-wash-types)
1377               (if delete
1378                   (progn
1379                     (add-text-properties
1380                      (point-min) (+ 5 (point-min))
1381                      '(article-type headers dummy-invisible t))
1382                     (delete-region beg (point-max)))
1383                 (gnus-article-hide-text-type beg (point-max) 'headers))))))))
1384   )
1385
1386 (defun article-hide-boring-headers (&optional arg)
1387   "Toggle hiding of headers that aren't very interesting.
1388 If given a negative prefix, always show; if given a positive prefix,
1389 always hide."
1390   (interactive (gnus-article-hidden-arg))
1391   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1392              (not gnus-show-all-headers))
1393     (save-excursion
1394       (save-restriction
1395         (let ((buffer-read-only nil)
1396               (list gnus-boring-article-headers)
1397               (inhibit-point-motion-hooks t)
1398               elem)
1399           (article-narrow-to-head)
1400           (while list
1401             (setq elem (pop list))
1402             (goto-char (point-min))
1403             (cond
1404              ;; Hide empty headers.
1405              ((eq elem 'empty)
1406               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1407                 (forward-line -1)
1408                 (gnus-article-hide-text-type
1409                  (progn (beginning-of-line) (point))
1410                  (progn
1411                    (end-of-line)
1412                    (if (re-search-forward "^[^ \t]" nil t)
1413                        (match-beginning 0)
1414                      (point-max)))
1415                  'boring-headers)))
1416              ;; Hide boring Newsgroups header.
1417              ((eq elem 'newsgroups)
1418               (when (gnus-string-equal
1419                      (gnus-fetch-field "newsgroups")
1420                      (gnus-group-real-name
1421                       (if (boundp 'gnus-newsgroup-name)
1422                           gnus-newsgroup-name
1423                         "")))
1424                 (gnus-article-hide-header "newsgroups")))
1425              ((eq elem 'to-address)
1426               (let ((to (message-fetch-field "to"))
1427                     (to-address
1428                      (gnus-parameter-to-address
1429                       (if (boundp 'gnus-newsgroup-name)
1430                           gnus-newsgroup-name ""))))
1431                 (when (and to to-address
1432                            (ignore-errors
1433                              (gnus-string-equal
1434                               ;; only one address in To
1435                               (nth 1 (mail-extract-address-components to))
1436                               to-address)))
1437                   (gnus-article-hide-header "to"))))
1438              ((eq elem 'followup-to)
1439               (when (gnus-string-equal
1440                      (message-fetch-field "followup-to")
1441                      (message-fetch-field "newsgroups"))
1442                 (gnus-article-hide-header "followup-to")))
1443              ((eq elem 'reply-to)
1444               (let ((from (message-fetch-field "from"))
1445                     (reply-to (message-fetch-field "reply-to")))
1446                 (when (and
1447                        from reply-to
1448                        (ignore-errors
1449                          (gnus-string-equal
1450                           (nth 1 (mail-extract-address-components from))
1451                           (nth 1 (mail-extract-address-components reply-to)))))
1452                   (gnus-article-hide-header "reply-to"))))
1453              ((eq elem 'date)
1454               (let ((date (message-fetch-field "date")))
1455                 (when (and date
1456                            (< (days-between (current-time-string) date)
1457                               4))
1458                   (gnus-article-hide-header "date"))))
1459              ((eq elem 'long-to)
1460               (let ((to (message-fetch-field "to"))
1461                     (cc (message-fetch-field "cc")))
1462                 (when (> (length to) 1024)
1463                   (gnus-article-hide-header "to"))
1464                 (when (> (length cc) 1024)
1465                   (gnus-article-hide-header "cc"))))
1466              ((eq elem 'many-to)
1467               (let ((to-count 0)
1468                     (cc-count 0))
1469                 (goto-char (point-min))
1470                 (while (re-search-forward "^to:" nil t)
1471                   (setq to-count (1+ to-count)))
1472                 (when (> to-count 1)
1473                   (while (> to-count 0)
1474                     (goto-char (point-min))
1475                     (save-restriction
1476                       (re-search-forward "^to:" nil nil to-count)
1477                       (forward-line -1)
1478                       (narrow-to-region (point) (point-max))
1479                       (gnus-article-hide-header "to"))
1480                     (setq to-count (1- to-count))))
1481                 (goto-char (point-min))
1482                 (while (re-search-forward "^cc:" nil t)
1483                   (setq cc-count (1+ cc-count)))
1484                 (when (> cc-count 1)
1485                   (while (> cc-count 0)
1486                     (goto-char (point-min))
1487                     (save-restriction
1488                       (re-search-forward "^cc:" nil nil cc-count)
1489                       (forward-line -1)
1490                       (narrow-to-region (point) (point-max))
1491                       (gnus-article-hide-header "cc"))
1492                     (setq cc-count (1- cc-count)))))))))))))
1493
1494 (defun gnus-article-hide-header (header)
1495   (save-excursion
1496     (goto-char (point-min))
1497     (when (re-search-forward (concat "^" header ":") nil t)
1498       (gnus-article-hide-text-type
1499        (progn (beginning-of-line) (point))
1500        (progn
1501          (end-of-line)
1502          (if (re-search-forward "^[^ \t]" nil t)
1503              (match-beginning 0)
1504            (point-max)))
1505        'boring-headers))))
1506
1507 (defun article-toggle-headers (&optional arg)
1508   "Toggle hiding of headers.  If given a negative prefix, always show;
1509 if given a positive prefix, always hide."
1510   (interactive (gnus-article-hidden-arg))
1511   (let ((force (when (numberp arg)
1512                  (cond ((> arg 0) 'always-hide)
1513                        ((< arg 0) 'always-show))))
1514         (window (get-buffer-window gnus-article-buffer))
1515         (header-end (point-min))
1516         header-start field-end field-start
1517         (inhibit-point-motion-hooks t)
1518         (inhibit-read-only t))
1519     (save-restriction
1520       (widen)
1521       (while (and (setq header-start
1522                         (text-property-any header-end (point-max)
1523                                            'article-treated-header t))
1524                   (setq header-end
1525                         (text-property-not-all header-start (point-max)
1526                                                'article-treated-header t)))
1527         (setq field-end header-start)
1528         (cond
1529          (;; Hide exposed invisible fields.
1530           (and (not (eq 'always-show force))
1531                (setq field-start
1532                      (text-property-any field-end header-end
1533                                         'exposed-invisible-field t)))
1534           (while (and field-start
1535                       (setq field-end (text-property-not-all
1536                                        field-start header-end
1537                                        'exposed-invisible-field t)))
1538             (add-text-properties field-start field-end gnus-hidden-properties)
1539             (setq field-start (text-property-any field-end header-end
1540                                                  'exposed-invisible-field t)))
1541           (put-text-property header-start header-end
1542                              'exposed-invisible-field nil))
1543          (;; Expose invisible fields.
1544           (and (not (eq 'always-hide force))
1545                (setq field-start
1546                      (text-property-any field-end header-end 'invisible t)))
1547           (while (and field-start
1548                       (setq field-end (text-property-not-all
1549                                        field-start header-end
1550                                        'invisible t)))
1551             ;; If the invisible text is not terminated with newline, we
1552             ;; won't expose it.  Because it may be created by x-face-mule.
1553             ;; BTW, XEmacs sometimes fail in putting a invisible text
1554             ;; property with `gnus-article-hide-text' (really?).  In that
1555             ;; case, the invisible text might be started from the middle of
1556             ;; a line so we will expose the sort of thing.
1557             (when (or (not (or (eq header-start field-start)
1558                                (eq ?\n (char-before field-start))))
1559                       (eq ?\n (char-before field-end)))
1560               (remove-text-properties field-start field-end
1561                                       gnus-hidden-properties)
1562               (put-text-property field-start field-end
1563                                  'exposed-invisible-field t))
1564             (setq field-start (text-property-any field-end header-end
1565                                                  'invisible t))))
1566          (;; Hide fields.
1567           (not (eq 'always-show force))
1568           (narrow-to-region header-start header-end)
1569           (article-hide-headers)
1570           ;; Re-display X-Face image under XEmacs.
1571           (when (and (featurep 'xemacs)
1572                      (gnus-functionp gnus-article-x-face-command))
1573             (let ((func (cadr (assq 'gnus-treat-display-xface
1574                                     gnus-treatment-function-alist)))
1575                   (condition 'head))
1576               (when (and (not gnus-inhibit-treatment)
1577                          func
1578                          (gnus-treat-predicate gnus-treat-display-xface))
1579                 (funcall func)
1580                 (put-text-property header-start header-end 'read-only nil))))
1581           (widen))
1582          ))
1583       (goto-char (point-min))
1584       (when window
1585         (set-window-start window (point-min))))))
1586
1587 (defvar gnus-article-normalized-header-length 40
1588   "Length of normalized headers.")
1589
1590 (defun article-normalize-headers ()
1591   "Make all header lines 40 characters long."
1592   (interactive)
1593   (let ((buffer-read-only nil)
1594         column)
1595     (save-excursion
1596       (save-restriction
1597         (article-narrow-to-head)
1598         (while (not (eobp))
1599           (cond
1600            ((< (setq column (- (gnus-point-at-eol) (point)))
1601                gnus-article-normalized-header-length)
1602             (end-of-line)
1603             (insert (make-string
1604                      (- gnus-article-normalized-header-length column)
1605                      ? )))
1606            ((> column gnus-article-normalized-header-length)
1607             (gnus-put-text-property
1608              (progn
1609                (forward-char gnus-article-normalized-header-length)
1610                (point))
1611              (gnus-point-at-eol)
1612              'invisible t))
1613            (t
1614             ;; Do nothing.
1615             ))
1616           (forward-line 1))))))
1617
1618 (defun article-treat-dumbquotes ()
1619   "Translate M****s*** sm*rtq**t*s into proper text.
1620 Note that this function guesses whether a character is a sm*rtq**t* or
1621 not, so it should only be used interactively.
1622
1623 Sm*rtq**t*s are M****s***'s unilateral extension to the character map
1624 in an attempt to provide more quoting characters.  If you see
1625 something like \\222 or \\264 where you're expecting some kind of
1626 apostrophe or quotation mark, then try this wash."
1627   (interactive)
1628   (article-translate-strings gnus-article-dumbquotes-map))
1629
1630 (defun article-translate-characters (from to)
1631   "Translate all characters in the body of the article according to FROM and TO.
1632 FROM is a string of characters to translate from; to is a string of
1633 characters to translate to."
1634   (save-excursion
1635     (when (article-goto-body)
1636       (let ((buffer-read-only nil)
1637             (x (make-string 225 ?x))
1638             (i -1))
1639         (while (< (incf i) (length x))
1640           (aset x i i))
1641         (setq i 0)
1642         (while (< i (length from))
1643           (aset x (aref from i) (aref to i))
1644           (incf i))
1645         (translate-region (point) (point-max) x)))))
1646
1647 (defun article-translate-strings (map)
1648   "Translate all string in the body of the article according to MAP.
1649 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1650   (save-excursion
1651     (when (article-goto-body)
1652       (let ((buffer-read-only nil)
1653             elem)
1654         (while (setq elem (pop map))
1655           (save-excursion
1656             (while (search-forward (car elem) nil t)
1657               (replace-match (cadr elem)))))))))
1658
1659 (defun article-treat-overstrike ()
1660   "Translate overstrikes into bold text."
1661   (interactive)
1662   (save-excursion
1663     (when (article-goto-body)
1664       (let ((buffer-read-only nil))
1665         (while (search-forward "\b" nil t)
1666           (let ((next (char-after))
1667                 start end previous)
1668             (backward-char 2)
1669             (setq start (point)
1670                   previous (char-after))
1671             (forward-char 3)
1672             (setq end (point))
1673             (backward-char)
1674             ;; We do the boldification/underlining by hiding the
1675             ;; overstrikes and putting the proper text property
1676             ;; on the letters.
1677             (cond
1678              ((eq next previous)
1679               (gnus-article-hide-text-type start (point) 'overstrike)
1680               (put-text-property (point) end 'face 'bold))
1681              ((eq next ?_)
1682               (gnus-article-hide-text-type
1683                (1- (point)) (1+ (point)) 'overstrike)
1684               (put-text-property
1685                start (1- (point)) 'face 'underline))
1686              ((eq previous ?_)
1687               (gnus-article-hide-text-type start (point) 'overstrike)
1688               (put-text-property
1689                (point) end 'face 'underline)))))))))
1690
1691 (defun article-fill-long-lines ()
1692   "Fill lines that are wider than the window width."
1693   (interactive)
1694   (save-excursion
1695     (let ((buffer-read-only nil)
1696           (width (window-width (get-buffer-window (current-buffer)))))
1697       (save-restriction
1698         (article-goto-body)
1699         (let ((adaptive-fill-mode nil)) ;Why?  -sm
1700           (while (not (eobp))
1701             (end-of-line)
1702             (when (>= (current-column) (min fill-column width))
1703               (narrow-to-region (point) (gnus-point-at-bol))
1704               (fill-paragraph nil)
1705               (goto-char (point-max))
1706               (widen))
1707             (forward-line 1)))))))
1708
1709 (defun article-capitalize-sentences ()
1710   "Capitalize the first word in each sentence."
1711   (interactive)
1712   (save-excursion
1713     (let ((buffer-read-only nil)
1714           (paragraph-start "^[\n\^L]"))
1715       (article-goto-body)
1716       (while (not (eobp))
1717         (capitalize-word 1)
1718         (forward-sentence)))))
1719
1720 (defun article-remove-cr ()
1721   "Remove trailing CRs and then translate remaining CRs into LFs."
1722   (interactive)
1723   (save-excursion
1724     (let ((buffer-read-only nil))
1725       (goto-char (point-min))
1726       (while (re-search-forward "\r+$" nil t)
1727         (replace-match "" t t))
1728       (goto-char (point-min))
1729       (while (search-forward "\r" nil t)
1730         (replace-match "\n" t t)))))
1731
1732 (defun article-remove-trailing-blank-lines ()
1733   "Remove all trailing blank lines from the article."
1734   (interactive)
1735   (save-excursion
1736     (let ((buffer-read-only nil))
1737       (goto-char (point-max))
1738       (delete-region
1739        (point)
1740        (progn
1741          (while (and (not (bobp))
1742                      (looking-at "^[ \t]*$")
1743                      (not (gnus-annotation-in-region-p
1744                            (point) (gnus-point-at-eol))))
1745            (forward-line -1))
1746          (forward-line 1)
1747          (point))))))
1748
1749 (defun article-display-x-face (&optional force)
1750   "Look for an X-Face header and display it if present."
1751   (interactive (list 'force))
1752   (save-excursion
1753     ;; Delete the old process, if any.
1754     (when (process-status "article-x-face")
1755       (delete-process "article-x-face"))
1756     (let ((inhibit-point-motion-hooks t)
1757           x-faces
1758           (case-fold-search t)
1759           from last)
1760       (save-restriction
1761         (article-narrow-to-head)
1762 ;;      (when (and buffer-read-only ;; When type `W f'
1763 ;;                 (progn
1764 ;;                   (goto-char (point-min))
1765 ;;                   (not (re-search-forward "^X-Face:[\t ]*" nil t)))
1766 ;;                 (gnus-buffer-live-p gnus-original-article-buffer))
1767 ;;        (with-current-buffer gnus-original-article-buffer
1768 ;;          (save-restriction
1769 ;;            (article-narrow-to-head)
1770 ;;            (while (re-search-forward "^X-Face:" nil t)
1771 ;;              (setq x-faces
1772 ;;                    (concat
1773 ;;                     (or x-faces "")
1774 ;;                     (buffer-substring
1775 ;;                      (match-beginning 0)
1776 ;;                      (1- (re-search-forward
1777 ;;                           "^\\($\\|[^ \t]\\)" nil t))))))))
1778 ;;        (if x-faces
1779 ;;            (let (point start bface eface buffer-read-only)
1780 ;;              (goto-char (point-max))
1781 ;;              (forward-line -1)
1782 ;;              (setq bface (get-text-property (gnus-point-at-bol) 'face)
1783 ;;                    eface (get-text-property (1- (gnus-point-at-eol)) 'face))
1784 ;;              (goto-char (point-max))
1785 ;;              (setq point (point))
1786 ;;              (insert x-faces)
1787 ;;              (goto-char point)
1788 ;;              (while (looking-at "\\([^:]+\\): *")
1789 ;;                (put-text-property (match-beginning 1) (1+ (match-end 1))
1790 ;;                                   'face bface)
1791 ;;                (setq start (match-end 0))
1792 ;;                (forward-line 1)
1793 ;;                (while (looking-at "[\t ]")
1794 ;;                  (forward-line 1))
1795 ;;                (put-text-property start (point)
1796 ;;                                   'face eface)))))
1797         (goto-char (point-min))
1798         (setq from (message-fetch-field "from"))
1799         (goto-char (point-min))
1800         (while (and gnus-article-x-face-command
1801                     (not last)
1802                     (or force
1803                         ;; Check whether this face is censored.
1804                         (not gnus-article-x-face-too-ugly)
1805                         (and gnus-article-x-face-too-ugly from
1806                              (not (string-match gnus-article-x-face-too-ugly
1807                                                 from))))
1808                     ;; Has to be present.
1809                     (re-search-forward "^X-Face:[\t ]*" nil t))
1810           ;; This used to try to do multiple faces (`while' instead of
1811           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1812           ;; work (b) it can crash some versions of Emacs (c) are
1813           ;; multiple faces really something to encourage?
1814           (when (stringp gnus-article-x-face-command)
1815             (setq last t))
1816           ;; We now have the area of the buffer where the X-Face is stored.
1817           (save-excursion
1818             (let ((beg (point))
1819                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1820               ;; We display the face.
1821               (if (symbolp gnus-article-x-face-command)
1822                   ;; The command is a lisp function, so we call it.
1823                   (if (gnus-functionp gnus-article-x-face-command)
1824                       (funcall gnus-article-x-face-command beg end)
1825                     (error "%s is not a function" gnus-article-x-face-command))
1826                 ;; The command is a string, so we interpret the command
1827                 ;; as a, well, command, and fork it off.
1828                 (let ((process-connection-type nil))
1829                   (process-kill-without-query
1830                    (start-process
1831                     "article-x-face" nil shell-file-name shell-command-switch
1832                     gnus-article-x-face-command))
1833                   (process-send-region "article-x-face" beg end)
1834                   (process-send-eof "article-x-face"))))))))))
1835
1836 (defun article-decode-mime-words ()
1837   "Decode all MIME-encoded words in the article."
1838   (interactive)
1839   (save-excursion
1840     (set-buffer gnus-article-buffer)
1841     (let ((inhibit-point-motion-hooks t)
1842           buffer-read-only
1843           (mail-parse-charset gnus-newsgroup-charset)
1844           (mail-parse-ignored-charsets
1845            (save-excursion (set-buffer gnus-summary-buffer)
1846                            gnus-newsgroup-ignored-charsets)))
1847       (mail-decode-encoded-word-region (point-min) (point-max)))))
1848
1849 (defun article-decode-charset (&optional prompt)
1850   "Decode charset-encoded text in the article.
1851 If PROMPT (the prefix), prompt for a coding system to use."
1852   (interactive "P")
1853   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1854         buffer-read-only
1855         (mail-parse-charset gnus-newsgroup-charset)
1856         (mail-parse-ignored-charsets
1857          (save-excursion (condition-case nil
1858                              (set-buffer gnus-summary-buffer)
1859                            (error))
1860                          gnus-newsgroup-ignored-charsets))
1861         ct cte ctl charset format)
1862     (save-excursion
1863       (save-restriction
1864         (article-narrow-to-head)
1865         (setq ct (message-fetch-field "Content-Type" t)
1866               cte (message-fetch-field "Content-Transfer-Encoding" t)
1867               ctl (and ct (ignore-errors
1868                             (mail-header-parse-content-type ct)))
1869               charset (cond
1870                        (prompt
1871                         (mm-read-coding-system "Charset to decode: "))
1872                        (ctl
1873                         (mail-content-type-get ctl 'charset)))
1874               format (and ctl (mail-content-type-get ctl 'format)))
1875         (when cte
1876           (setq cte (mail-header-strip cte)))
1877         (if (and ctl (not (string-match "/" (car ctl))))
1878             (setq ctl nil))
1879         (goto-char (point-max)))
1880       (forward-line 1)
1881       (save-restriction
1882         (narrow-to-region (point) (point-max))
1883         (when (and (eq mail-parse-charset 'gnus-decoded)
1884                    (eq (mm-body-7-or-8) '8bit))
1885           ;; The text code could have been decoded.
1886           (setq charset mail-parse-charset))
1887         (when (and (or (not ctl)
1888                        (equal (car ctl) "text/plain"))
1889                    (not format)) ;; article with format will decode later.
1890           (mm-decode-body
1891            charset (and cte (intern (downcase
1892                                      (gnus-strip-whitespace cte))))
1893            (car ctl)))))))
1894
1895 (defun article-decode-encoded-words ()
1896   "Remove encoded-word encoding from headers."
1897   (let (buffer-read-only)
1898     (let ((charset (save-excursion
1899                      (set-buffer gnus-summary-buffer)
1900                      default-mime-charset)))
1901       (mime-decode-header-in-buffer charset)
1902       )))
1903
1904 (defun article-de-quoted-unreadable (&optional force read-charset)
1905   "Translate a quoted-printable-encoded article.
1906 If FORCE, decode the article whether it is marked as quoted-printable
1907 or not.
1908 If READ-CHARSET, ask for a coding system."
1909   (interactive (list 'force current-prefix-arg))
1910   (save-excursion
1911     (let ((buffer-read-only nil) type charset)
1912       (if (gnus-buffer-live-p gnus-original-article-buffer)
1913           (with-current-buffer gnus-original-article-buffer
1914             (setq type
1915                   (gnus-fetch-field "content-transfer-encoding"))
1916             (let* ((ct (gnus-fetch-field "content-type"))
1917                    (ctl (and ct
1918                              (ignore-errors
1919                                (mail-header-parse-content-type ct)))))
1920               (setq charset (and ctl
1921                                  (mail-content-type-get ctl 'charset)))
1922               (if (stringp charset)
1923                   (setq charset (intern (downcase charset)))))))
1924       (if read-charset
1925           (setq charset (mm-read-coding-system "Charset: " charset)))
1926       (unless charset
1927         (setq charset gnus-newsgroup-charset))
1928       (when (or force
1929                 (and type (let ((case-fold-search t))
1930                             (string-match "quoted-printable" type))))
1931         (article-goto-body)
1932         (quoted-printable-decode-region
1933          (point) (point-max) (mm-charset-to-coding-system charset))))))
1934
1935 (defun article-de-base64-unreadable (&optional force read-charset)
1936   "Translate a base64 article.
1937 If FORCE, decode the article whether it is marked as base64 not.
1938 If READ-CHARSET, ask for a coding system."
1939   (interactive (list 'force current-prefix-arg))
1940   (save-excursion
1941     (let ((buffer-read-only nil) type charset)
1942       (if (gnus-buffer-live-p gnus-original-article-buffer)
1943           (with-current-buffer gnus-original-article-buffer
1944             (setq type
1945                   (gnus-fetch-field "content-transfer-encoding"))
1946             (let* ((ct (gnus-fetch-field "content-type"))
1947                    (ctl (and ct
1948                              (ignore-errors
1949                                (mail-header-parse-content-type ct)))))
1950               (setq charset (and ctl
1951                                  (mail-content-type-get ctl 'charset)))
1952               (if (stringp charset)
1953                   (setq charset (intern (downcase charset)))))))
1954       (if read-charset
1955           (setq charset (mm-read-coding-system "Charset: " charset)))
1956       (unless charset
1957         (setq charset gnus-newsgroup-charset))
1958       (when (or force
1959                 (and type (let ((case-fold-search t))
1960                             (string-match "base64" type))))
1961         (article-goto-body)
1962         (save-restriction
1963           (narrow-to-region (point) (point-max))
1964           (base64-decode-region (point-min) (point-max))
1965           (mm-decode-coding-region
1966            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1967
1968 (eval-when-compile
1969   (require 'rfc1843))
1970
1971 (defun article-decode-HZ ()
1972   "Translate a HZ-encoded article."
1973   (interactive)
1974   (require 'rfc1843)
1975   (save-excursion
1976     (let ((buffer-read-only nil))
1977       (rfc1843-decode-region (point-min) (point-max)))))
1978
1979 (defun article-wash-html (&optional read-charset)
1980   "Format an html article.
1981 If READ-CHARSET, ask for a coding system."
1982   (interactive "P")
1983   (save-excursion
1984     (let ((buffer-read-only nil)
1985           charset)
1986       (if (gnus-buffer-live-p gnus-original-article-buffer)
1987           (with-current-buffer gnus-original-article-buffer
1988             (let* ((ct (gnus-fetch-field "content-type"))
1989                    (ctl (and ct
1990                              (ignore-errors
1991                                (mail-header-parse-content-type ct)))))
1992               (setq charset (and ctl
1993                                  (mail-content-type-get ctl 'charset)))
1994               (if (stringp charset)
1995                   (setq charset (intern (downcase charset)))))))
1996       (if read-charset
1997           (setq charset (mm-read-coding-system "Charset: " charset)))
1998       (unless charset
1999         (setq charset gnus-newsgroup-charset))
2000       (article-goto-body)
2001       (save-window-excursion
2002         (save-restriction
2003           (narrow-to-region (point) (point-max))
2004           (mm-setup-w3)
2005           (let ((w3-strict-width (window-width))
2006                 (url-standalone-mode t))
2007             (condition-case var
2008                 (w3-region (point-min) (point-max))
2009               (error))))))))
2010
2011 (defun article-hide-list-identifiers ()
2012   "Remove list identifies from the Subject header.
2013 The `gnus-list-identifiers' variable specifies what to do."
2014   (interactive)
2015   (let ((inhibit-point-motion-hooks t)
2016         (regexp (if (consp gnus-list-identifiers)
2017                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2018                   gnus-list-identifiers))
2019         buffer-read-only)
2020     (when regexp
2021       (save-excursion
2022         (save-restriction
2023           (article-narrow-to-head)
2024           (goto-char (point-min))
2025           (while (re-search-forward
2026                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2027                   nil t)
2028             (delete-region (match-beginning 2) (match-end 0))
2029             (beginning-of-line))
2030           (when (re-search-forward
2031                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2032             (delete-region (match-beginning 1) (match-end 1))))))))
2033
2034 (defun article-hide-pgp ()
2035   "Remove any PGP headers and signatures in the current article."
2036   (interactive)
2037   (save-excursion
2038     (save-restriction
2039       (let ((inhibit-point-motion-hooks t)
2040             buffer-read-only beg end)
2041         (article-goto-body)
2042         ;; Hide the "header".
2043         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
2044           (push 'pgp gnus-article-wash-types)
2045           (delete-region (match-beginning 0) (match-end 0))
2046           ;; Remove armor headers (rfc2440 6.2)
2047           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
2048                                      (point)))
2049           (setq beg (point))
2050           ;; Hide the actual signature.
2051           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
2052                (setq end (1+ (match-beginning 0)))
2053                (delete-region
2054                 end
2055                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
2056                     (match-end 0)
2057                   ;; Perhaps we shouldn't hide to the end of the buffer
2058                   ;; if there is no end to the signature?
2059                   (point-max))))
2060           ;; Hide "- " PGP quotation markers.
2061           (when (and beg end)
2062             (narrow-to-region beg end)
2063             (goto-char (point-min))
2064             (while (re-search-forward "^- " nil t)
2065               (delete-region
2066                (match-beginning 0) (match-end 0)))
2067             (widen))
2068           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
2069
2070 (defun article-hide-pem (&optional arg)
2071   "Toggle hiding of any PEM headers and signatures in the current article.
2072 If given a negative prefix, always show; if given a positive prefix,
2073 always hide."
2074   (interactive (gnus-article-hidden-arg))
2075   (unless (gnus-article-check-hidden-text 'pem arg)
2076     (save-excursion
2077       (let (buffer-read-only end)
2078         (goto-char (point-min))
2079         ;; Hide the horrendously ugly "header".
2080         (when (and (search-forward
2081                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2082                     nil t)
2083                    (setq end (1+ (match-beginning 0))))
2084           (push 'pem gnus-article-wash-types)
2085           (gnus-article-hide-text-type
2086            end
2087            (if (search-forward "\n\n" nil t)
2088                (match-end 0)
2089              (point-max))
2090            'pem)
2091           ;; Hide the trailer as well
2092           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2093                                 nil t)
2094             (gnus-article-hide-text-type
2095              (match-beginning 0) (match-end 0) 'pem)))))))
2096
2097 (defun article-strip-banner ()
2098   "Strip the banner specified by the `banner' group parameter."
2099   (interactive)
2100   (save-excursion
2101     (save-restriction
2102       (let ((inhibit-point-motion-hooks t)
2103             (banner (gnus-parameter-banner gnus-newsgroup-name))
2104             (gnus-signature-limit nil)
2105             buffer-read-only beg end)
2106         (when banner
2107           (article-goto-body)
2108           (cond
2109            ((eq banner 'signature)
2110             (when (gnus-article-narrow-to-signature)
2111               (widen)
2112               (forward-line -1)
2113               (delete-region (point) (point-max))))
2114            ((symbolp banner)
2115             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2116                 (while (re-search-forward banner nil t)
2117                   (delete-region (match-beginning 0) (match-end 0)))))
2118            ((stringp banner)
2119             (while (re-search-forward banner nil t)
2120               (delete-region (match-beginning 0) (match-end 0))))))))))
2121
2122 (defun article-babel ()
2123   "Translate article using an online translation service."
2124   (interactive)
2125   (require 'babel)
2126   (save-excursion
2127     (set-buffer gnus-article-buffer)
2128     (when (article-goto-body)
2129       (let* ((buffer-read-only nil)
2130              (start (point))
2131              (end (point-max))
2132              (orig (buffer-substring start end))
2133              (trans (babel-as-string orig)))
2134         (save-restriction
2135           (narrow-to-region start end)
2136           (delete-region start end)
2137           (insert trans))))))
2138
2139 (defun article-hide-signature (&optional arg)
2140   "Hide the signature in the current article.
2141 If given a negative prefix, always show; if given a positive prefix,
2142 always hide."
2143   (interactive (gnus-article-hidden-arg))
2144   (save-excursion
2145     (save-restriction
2146       (if (interactive-p)
2147           (progn
2148             (widen)
2149             (article-goto-body))
2150         (goto-char (point-min)))
2151       (unless (gnus-article-check-hidden-text 'signature arg)
2152         (let ((buffer-read-only nil)
2153               (button (point)))
2154           (while (setq button (text-property-any button (point-max)
2155                                                  'gnus-callback
2156                                                  'gnus-signature-toggle))
2157             (setq button (text-property-not-all button (point-max)
2158                                                 'gnus-callback
2159                                                 'gnus-signature-toggle))
2160             (when (and button (not (eobp)))
2161               (gnus-article-hide-text-type
2162                (1+ button)
2163                (next-single-property-change (1+ button) 'mime-view-entity
2164                                             nil (point-max))
2165                'signature))))))))
2166
2167 (defun article-strip-headers-in-body ()
2168   "Strip offensive headers from bodies."
2169   (interactive)
2170   (save-excursion
2171     (article-goto-body)
2172     (let ((case-fold-search t))
2173       (when (looking-at "x-no-archive:")
2174         (gnus-delete-line)))))
2175
2176 (defun article-strip-leading-blank-lines ()
2177   "Remove all blank lines from the beginning of the article."
2178   (interactive)
2179   (save-excursion
2180     (let ((inhibit-point-motion-hooks t)
2181           buffer-read-only)
2182       (when (article-goto-body)
2183         (while (and (not (eobp))
2184                     (looking-at "[ \t]*$"))
2185           (gnus-delete-line))))))
2186
2187 (defun article-narrow-to-head ()
2188   "Narrow the buffer to the head of the message.
2189 Point is left at the beginning of the narrowed-to region."
2190   (narrow-to-region
2191    (goto-char (point-min))
2192    (if (search-forward "\n\n" nil 1)
2193        (1- (point))
2194      (point-max)))
2195   (goto-char (point-min)))
2196
2197 (defun article-goto-body ()
2198   "Place point at the start of the body."
2199   (goto-char (point-min))
2200   (cond
2201    ;; This variable is only bound when dealing with separate
2202    ;; MIME body parts.
2203    (article-goto-body-goes-to-point-min-p
2204     t)
2205    ((search-forward "\n\n" nil t)
2206     t)
2207    (t
2208     (goto-char (point-max))
2209     nil)))
2210
2211 (defun article-strip-multiple-blank-lines ()
2212   "Replace consecutive blank lines with one empty line."
2213   (interactive)
2214   (save-excursion
2215     (let ((inhibit-point-motion-hooks t)
2216           buffer-read-only)
2217       ;; First make all blank lines empty.
2218       (article-goto-body)
2219       (while (re-search-forward "^[ \t]+$" nil t)
2220         (unless (gnus-annotation-in-region-p
2221                  (match-beginning 0) (match-end 0))
2222           (replace-match "" nil t)))
2223       ;; Then replace multiple empty lines with a single empty line.
2224       (article-goto-body)
2225       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2226         (unless (gnus-annotation-in-region-p
2227                  (match-beginning 0) (match-end 0))
2228           (delete-region (match-beginning 1) (match-end 1)))))))
2229
2230 (defun article-strip-leading-space ()
2231   "Remove all white space from the beginning of the lines in the article."
2232   (interactive)
2233   (save-excursion
2234     (let ((inhibit-point-motion-hooks t)
2235           buffer-read-only)
2236       (article-goto-body)
2237       (while (re-search-forward "^[ \t]+" nil t)
2238         (replace-match "" t t)))))
2239
2240 (defun article-strip-trailing-space ()
2241   "Remove all white space from the end of the lines in the article."
2242   (interactive)
2243   (save-excursion
2244     (let ((inhibit-point-motion-hooks t)
2245           buffer-read-only)
2246       (article-goto-body)
2247       (while (re-search-forward "[ \t]+$" nil t)
2248         (replace-match "" t t)))))
2249
2250 (defun article-strip-blank-lines ()
2251   "Strip leading, trailing and multiple blank lines."
2252   (interactive)
2253   (article-strip-leading-blank-lines)
2254   (article-remove-trailing-blank-lines)
2255   (article-strip-multiple-blank-lines))
2256
2257 (defun article-strip-all-blank-lines ()
2258   "Strip all blank lines."
2259   (interactive)
2260   (save-excursion
2261     (let ((inhibit-point-motion-hooks t)
2262           buffer-read-only)
2263       (article-goto-body)
2264       (while (re-search-forward "^[ \t]*\n" nil t)
2265         (replace-match "" t t)))))
2266
2267 (defun gnus-article-narrow-to-signature ()
2268   "Narrow to the signature; return t if a signature is found, else nil."
2269   (let ((inhibit-point-motion-hooks t))
2270     (when (gnus-article-search-signature)
2271       (forward-line 1)
2272       ;; Check whether we have some limits to what we consider
2273       ;; to be a signature.
2274       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2275                       (list gnus-signature-limit)))
2276             limit limited)
2277         (while (setq limit (pop limits))
2278           (if (or (and (integerp limit)
2279                        (< (- (point-max) (point)) limit))
2280                   (and (floatp limit)
2281                        (< (count-lines (point) (point-max)) limit))
2282                   (and (gnus-functionp limit)
2283                        (funcall limit))
2284                   (and (stringp limit)
2285                        (not (re-search-forward limit nil t))))
2286               ()                        ; This limit did not succeed.
2287             (setq limited t
2288                   limits nil)))
2289         (unless limited
2290           (narrow-to-region (point) (point-max))
2291           t)))))
2292
2293 (defun gnus-article-search-signature ()
2294   "Search the current buffer for the signature separator.
2295 Put point at the beginning of the signature separator."
2296   (let ((cur (point)))
2297     (goto-char (point-max))
2298     (if (if (stringp gnus-signature-separator)
2299             (re-search-backward gnus-signature-separator nil t)
2300           (let ((seps gnus-signature-separator))
2301             (while (and seps
2302                         (not (re-search-backward (car seps) nil t)))
2303               (pop seps))
2304             seps))
2305         t
2306       (goto-char cur)
2307       nil)))
2308
2309 (defun gnus-article-hidden-arg ()
2310   "Return the current prefix arg as a number, or 0 if no prefix."
2311   (list (if current-prefix-arg
2312             (prefix-numeric-value current-prefix-arg)
2313           0)))
2314
2315 (defun gnus-article-check-hidden-text (type arg)
2316   "Return nil if hiding is necessary.
2317 Arg can be nil or a number.  Nil and positive means hide, negative
2318 means show, 0 means toggle."
2319   (save-excursion
2320     (save-restriction
2321       (let ((hide (gnus-article-hidden-text-p type)))
2322         (cond
2323          ((or (null arg)
2324               (> arg 0))
2325           nil)
2326          ((< arg 0)
2327           (gnus-article-show-hidden-text type)
2328           t)
2329          (t
2330           (if (eq hide 'hidden)
2331               (progn
2332                 (gnus-article-show-hidden-text type)
2333                 t)
2334             nil)))))))
2335
2336 (defun gnus-article-hidden-text-p (type)
2337   "Say whether the current buffer contains hidden text of type TYPE."
2338   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2339     (while (and pos
2340                 (not (get-text-property pos 'invisible))
2341                 (not (get-text-property pos 'dummy-invisible)))
2342       (setq pos
2343             (text-property-any (1+ pos) (point-max) 'article-type type)))
2344     (if pos
2345         'hidden
2346       nil)))
2347
2348 (defun gnus-article-show-hidden-text (type &optional dummy)
2349   "Show all hidden text of type TYPE.
2350 Originally it is hide instead of DUMMY."
2351   (let ((buffer-read-only nil)
2352         (inhibit-point-motion-hooks t))
2353     (gnus-remove-text-properties-when
2354      'article-type type
2355      (point-min) (point-max)
2356      (cons 'article-type (cons type
2357                                gnus-hidden-properties)))))
2358
2359 (defconst article-time-units
2360   `((year . ,(* 365.25 24 60 60))
2361     (week . ,(* 7 24 60 60))
2362     (day . ,(* 24 60 60))
2363     (hour . ,(* 60 60))
2364     (minute . 60)
2365     (second . 1))
2366   "Mapping from time units to seconds.")
2367
2368 (defun article-date-ut (&optional type highlight header)
2369   "Convert DATE date to universal time in the current article.
2370 If TYPE is `local', convert to local time; if it is `lapsed', output
2371 how much time has lapsed since DATE.  For `lapsed', the value of
2372 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2373 should replace the \"Date:\" one, or should be added below it."
2374   (interactive (list 'ut t))
2375   (let* ((header (or header
2376                      (and (eq 1 (point-min))
2377                           (mail-header-date (save-excursion
2378                                               (set-buffer gnus-summary-buffer)
2379                                               gnus-current-headers)))
2380                      (message-fetch-field "date")
2381                      ""))
2382          (date (if (vectorp header) (mail-header-date header)
2383                  header))
2384          (inhibit-point-motion-hooks t)
2385          bface eface date-pos)
2386     (when (and date (not (string= date "")))
2387       (save-excursion
2388         (save-restriction
2389           (article-narrow-to-head)
2390           (when (or (and (eq type 'lapsed)
2391                          gnus-article-date-lapsed-new-header
2392                          ;; Attempt to get the face of X-Sent first.
2393                          (re-search-forward "^X-Sent:[ \t]" nil t))
2394                     (re-search-forward "^Date:[ \t]" nil t)
2395                     ;; If Date is missing, try again for X-Sent.
2396                     (re-search-forward "^X-Sent:[ \t]" nil t))
2397             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2398                   date (or (get-text-property (gnus-point-at-bol)
2399                                               'original-date)
2400                            date)
2401                   eface (get-text-property (1- (gnus-point-at-eol))
2402                                            'face)))
2403           (let ((buffer-read-only nil))
2404             ;; Delete any old X-Sent headers.
2405             (when (setq date-pos
2406                         (text-property-any (point-min) (point-max)
2407                                            'article-date-lapsed t))
2408               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2409               (delete-region (match-beginning 0)
2410                              (progn (forward-line 1) (point))))
2411             (goto-char (point-min))
2412             ;; Delete any old Date headers.
2413             (while (re-search-forward "^Date:[ \t]" nil t)
2414               (unless date-pos
2415                 (setq date-pos (match-beginning 0)))
2416               (unless (and (eq type 'lapsed)
2417                            gnus-article-date-lapsed-new-header)
2418                 (delete-region (match-beginning 0)
2419                                (progn (message-next-header) (point)))))
2420             (if date-pos
2421                 (progn
2422                   (goto-char date-pos)
2423                   (unless (bolp)
2424                     ;; Possibly, Date has been deleted.
2425                     (insert "\n"))
2426                   (when (and (eq type 'lapsed)
2427                              gnus-article-date-lapsed-new-header
2428                              (looking-at "Date:"))
2429                     (forward-line 1)))
2430               (goto-char (point-min)))
2431             (insert (article-make-date-line date type))
2432             (when (eq type 'lapsed)
2433               (put-text-property (gnus-point-at-bol) (point)
2434                                  'article-date-lapsed t))
2435             (insert "\n")
2436             (forward-line -1)
2437             ;; Do highlighting.
2438             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2439               (put-text-property (match-beginning 1) (1+ (match-end 1))
2440                                  'original-date date)
2441               (put-text-property (match-beginning 1) (1+ (match-end 1))
2442                                  'face bface)
2443               (put-text-property (match-beginning 2) (match-end 2)
2444                                  'face eface))))))))
2445
2446 (defun article-make-date-line (date type)
2447   "Return a DATE line of TYPE."
2448   (unless (memq type '(local ut original user iso8601 lapsed english))
2449     (error "Unknown conversion type: %s" type))
2450   (condition-case ()
2451       (let ((time (date-to-time date)))
2452         (cond
2453          ;; Convert to the local timezone.
2454          ((eq type 'local)
2455           (let ((tz (car (current-time-zone time))))
2456             (format "Date: %s %s%02d%02d" (current-time-string time)
2457                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2458                     (/ (% (abs tz) 3600) 60))))
2459          ;; Convert to Universal Time.
2460          ((eq type 'ut)
2461           (concat "Date: "
2462                   (current-time-string
2463                    (let* ((e (parse-time-string date))
2464                           (tm (apply 'encode-time e))
2465                           (ms (car tm))
2466                           (ls (- (cadr tm) (car (current-time-zone time)))))
2467                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2468                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2469                            (t (list ms ls)))))
2470                   " UT"))
2471          ;; Get the original date from the article.
2472          ((eq type 'original)
2473           (concat "Date: " (if (string-match "\n+$" date)
2474                                (substring date 0 (match-beginning 0))
2475                              date)))
2476          ;; Let the user define the format.
2477          ((eq type 'user)
2478           (if (gnus-functionp gnus-article-time-format)
2479               (funcall gnus-article-time-format time)
2480             (concat
2481              "Date: "
2482              (format-time-string gnus-article-time-format time))))
2483          ;; ISO 8601.
2484          ((eq type 'iso8601)
2485           (let ((tz (car (current-time-zone time))))
2486             (concat
2487              "Date: "
2488              (format-time-string "%Y%m%dT%H%M%S" time)
2489              (format "%s%02d%02d"
2490                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2491                      (/ (% (abs tz) 3600) 60)))))
2492          ;; Do an X-Sent lapsed format.
2493          ((eq type 'lapsed)
2494           ;; If the date is seriously mangled, the timezone functions are
2495           ;; liable to bug out, so we ignore all errors.
2496           (let* ((now (current-time))
2497                  (real-time (subtract-time now time))
2498                  (real-sec (and real-time
2499                                 (+ (* (float (car real-time)) 65536)
2500                                    (cadr real-time))))
2501                  (sec (and real-time (abs real-sec)))
2502                  num prev)
2503             (cond
2504              ((null real-time)
2505               "X-Sent: Unknown")
2506              ((zerop sec)
2507               "X-Sent: Now")
2508              (t
2509               (concat
2510                "X-Sent: "
2511                ;; This is a bit convoluted, but basically we go
2512                ;; through the time units for years, weeks, etc,
2513                ;; and divide things to see whether that results
2514                ;; in positive answers.
2515                (mapconcat
2516                 (lambda (unit)
2517                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2518                       ;; The (remaining) seconds are too few to
2519                       ;; be divided into this time unit.
2520                       ""
2521                     ;; It's big enough, so we output it.
2522                     (setq sec (- sec (* num (cdr unit))))
2523                     (prog1
2524                         (concat (if prev ", " "") (int-to-string
2525                                                    (floor num))
2526                                 " " (symbol-name (car unit))
2527                                 (if (> num 1) "s" ""))
2528                       (setq prev t))))
2529                 article-time-units "")
2530                ;; If dates are odd, then it might appear like the
2531                ;; article was sent in the future.
2532                (if (> real-sec 0)
2533                    " ago"
2534                  " in the future"))))))
2535          ;; Display the date in proper English
2536          ((eq type 'english)
2537           (let ((dtime (decode-time time)))
2538             (concat
2539              "Date: the "
2540              (number-to-string (nth 3 dtime))
2541              (let ((digit (% (nth 3 dtime) 10)))
2542                (cond
2543                 ((memq (nth 3 dtime) '(11 12 13)) "th")
2544                 ((= digit 1) "st")
2545                 ((= digit 2) "nd")
2546                 ((= digit 3) "rd")
2547                 (t "th")))
2548              " of "
2549              (nth (1- (nth 4 dtime)) gnus-english-month-names)
2550              " "
2551              (number-to-string (nth 5 dtime))
2552              " at "
2553              (format "%02d" (nth 2 dtime))
2554              ":"
2555              (format "%02d" (nth 1 dtime)))))))
2556     (error
2557      (format "Date: %s (from Oort)" date))))
2558
2559 (defun article-date-local (&optional highlight)
2560   "Convert the current article date to the local timezone."
2561   (interactive (list t))
2562   (article-date-ut 'local highlight))
2563
2564 (defun article-date-english (&optional highlight)
2565   "Convert the current article date to something that is proper English."
2566   (interactive (list t))
2567   (article-date-ut 'english highlight))
2568
2569 (defun article-date-original (&optional highlight)
2570   "Convert the current article date to what it was originally.
2571 This is only useful if you have used some other date conversion
2572 function and want to see what the date was before converting."
2573   (interactive (list t))
2574   (article-date-ut 'original highlight))
2575
2576 (defun article-date-lapsed (&optional highlight)
2577   "Convert the current article date to time lapsed since it was sent."
2578   (interactive (list t))
2579   (article-date-ut 'lapsed highlight))
2580
2581 (defun article-update-date-lapsed ()
2582   "Function to be run from a timer to update the lapsed time line."
2583   (let (deactivate-mark)
2584     (save-excursion
2585       (ignore-errors
2586         (walk-windows
2587          (lambda (w)
2588            (set-buffer (window-buffer w))
2589            (when (eq major-mode 'gnus-article-mode)
2590              (goto-char (point-min))
2591              (when (re-search-forward "^X-Sent:" nil t)
2592                (article-date-lapsed t))))
2593          nil 'visible)))))
2594
2595 (defun gnus-start-date-timer (&optional n)
2596   "Start a timer to update the X-Sent header in the article buffers.
2597 The numerical prefix says how frequently (in seconds) the function
2598 is to run."
2599   (interactive "p")
2600   (unless n
2601     (setq n 1))
2602   (gnus-stop-date-timer)
2603   (setq article-lapsed-timer
2604         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2605
2606 (defun gnus-stop-date-timer ()
2607   "Stop the X-Sent timer."
2608   (interactive)
2609   (when article-lapsed-timer
2610     (nnheader-cancel-timer article-lapsed-timer)
2611     (setq article-lapsed-timer nil)))
2612
2613 (defun article-date-user (&optional highlight)
2614   "Convert the current article date to the user-defined format.
2615 This format is defined by the `gnus-article-time-format' variable."
2616   (interactive (list t))
2617   (article-date-ut 'user highlight))
2618
2619 (defun article-date-iso8601 (&optional highlight)
2620   "Convert the current article date to ISO8601."
2621   (interactive (list t))
2622   (article-date-ut 'iso8601 highlight))
2623
2624 (defun article-show-all ()
2625   "Show all hidden text in the article buffer."
2626   (interactive)
2627   (save-excursion
2628     (widen)
2629     (let ((buffer-read-only nil))
2630       (gnus-article-unhide-text (point-min) (point-max))
2631       (gnus-remove-text-with-property 'gnus-prev)
2632       (gnus-remove-text-with-property 'gnus-next))))
2633
2634 (defun article-show-all-headers ()
2635   "Show all hidden headers in the article buffer."
2636   (interactive)
2637   (save-excursion
2638     (save-restriction
2639       (widen)
2640       (article-narrow-to-head)
2641       (let ((buffer-read-only nil))
2642         (gnus-article-unhide-text (point-min) (point-max))))))
2643
2644 (defun article-remove-leading-whitespace ()
2645   "Remove excessive whitespace from all headers."
2646   (interactive)
2647   (save-excursion
2648     (save-restriction
2649       (let ((buffer-read-only nil))
2650         (article-narrow-to-head)
2651         (goto-char (point-min))
2652         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
2653           (delete-region (match-beginning 1) (match-end 1)))))))
2654
2655 (defun article-emphasize (&optional arg)
2656   "Emphasize text according to `gnus-emphasis-alist'."
2657   (interactive (gnus-article-hidden-arg))
2658   (unless (gnus-article-check-hidden-text 'emphasis arg)
2659     (save-excursion
2660       (let ((alist (or
2661                     (condition-case nil
2662                         (with-current-buffer gnus-summary-buffer
2663                           gnus-article-emphasis-alist)
2664                       (error))
2665                     gnus-emphasis-alist))
2666             (buffer-read-only nil)
2667             (props (append '(article-type emphasis)
2668                            gnus-hidden-properties))
2669             regexp elem beg invisible visible face)
2670         (article-goto-body)
2671         (setq beg (point))
2672         (while (setq elem (pop alist))
2673           (goto-char beg)
2674           (setq regexp (car elem)
2675                 invisible (nth 1 elem)
2676                 visible (nth 2 elem)
2677                 face (nth 3 elem))
2678           (while (re-search-forward regexp nil t)
2679             (when (and (match-beginning visible) (match-beginning invisible))
2680               (push 'emphasis gnus-article-wash-types)
2681               (gnus-article-hide-text
2682                (match-beginning invisible) (match-end invisible) props)
2683               (gnus-article-unhide-text-type
2684                (match-beginning visible) (match-end visible) 'emphasis)
2685               (gnus-put-text-property-excluding-newlines
2686                (match-beginning visible) (match-end visible) 'face face)
2687               (goto-char (match-end invisible)))))))))
2688
2689 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2690   "Setup newsgroup emphasis alist."
2691   (unless gnus-article-emphasis-alist
2692     (let ((name (and gnus-newsgroup-name
2693                      (gnus-group-real-name gnus-newsgroup-name))))
2694       (make-local-variable 'gnus-article-emphasis-alist)
2695       (setq gnus-article-emphasis-alist
2696             (nconc
2697              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2698                (while (setq elem (pop alist))
2699                  (when (and name (string-match (car elem) name))
2700                    (setq alist nil
2701                          highlight (copy-sequence (cdr elem)))))
2702                highlight)
2703              (copy-sequence highlight-words)
2704              (if gnus-newsgroup-name
2705                  (copy-sequence (gnus-group-find-parameter
2706                                  gnus-newsgroup-name 'highlight-words t)))
2707              gnus-emphasis-alist)))))
2708
2709 (eval-when-compile
2710   (defvar gnus-summary-article-menu)
2711   (defvar gnus-summary-post-menu))
2712
2713 ;;; Saving functions.
2714
2715 (defun gnus-article-save (save-buffer file &optional num)
2716   "Save the currently selected article."
2717   (unless gnus-save-all-headers
2718     ;; Remove headers according to `gnus-saved-headers'.
2719     (let ((gnus-visible-headers
2720            (or gnus-saved-headers gnus-visible-headers))
2721           (gnus-article-buffer save-buffer))
2722       (save-excursion
2723         (set-buffer save-buffer)
2724         (article-hide-headers 1 t))))
2725   (save-window-excursion
2726     (if (not gnus-default-article-saver)
2727         (error "No default saver is defined")
2728       ;; !!! Magic!  The saving functions all save
2729       ;; `gnus-save-article-buffer' (or so they think), but we
2730       ;; bind that variable to our save-buffer.
2731       (set-buffer gnus-article-buffer)
2732       (let* ((gnus-save-article-buffer save-buffer)
2733              (filename
2734               (cond
2735                ((not gnus-prompt-before-saving) 'default)
2736                ((eq gnus-prompt-before-saving 'always) nil)
2737                (t file)))
2738              (gnus-number-of-articles-to-be-saved
2739               (when (eq gnus-prompt-before-saving t)
2740                 num)))                  ; Magic
2741         (set-buffer gnus-article-current-summary)
2742         (funcall gnus-default-article-saver filename)))))
2743
2744 (defun gnus-read-save-file-name (prompt &optional filename
2745                                         function group headers variable)
2746   (let ((default-name
2747           (funcall function group headers (symbol-value variable)))
2748         result)
2749     (setq result
2750           (expand-file-name
2751            (cond
2752             ((eq filename 'default)
2753              default-name)
2754             ((eq filename t)
2755              default-name)
2756             (filename filename)
2757             (t
2758              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2759                     (prompt
2760                      (format prompt
2761                              (if (and gnus-number-of-articles-to-be-saved
2762                                       (> gnus-number-of-articles-to-be-saved 1))
2763                                  (format "these %d articles"
2764                                          gnus-number-of-articles-to-be-saved)
2765                                "this article")))
2766                     (file
2767                      ;; Let the split methods have their say.
2768                      (cond
2769                       ;; No split name was found.
2770                       ((null split-name)
2771                        (read-file-name
2772                         (concat prompt " (default "
2773                                 (file-name-nondirectory default-name) ") ")
2774                         (file-name-directory default-name)
2775                         default-name))
2776                       ;; A single group name is returned.
2777                       ((stringp split-name)
2778                        (setq default-name
2779                              (funcall function split-name headers
2780                                       (symbol-value variable)))
2781                        (read-file-name
2782                         (concat prompt " (default "
2783                                 (file-name-nondirectory default-name) ") ")
2784                         (file-name-directory default-name)
2785                         default-name))
2786                       ;; A single split name was found
2787                       ((= 1 (length split-name))
2788                        (let* ((name (expand-file-name
2789                                      (car split-name)
2790                                      gnus-article-save-directory))
2791                               (dir (cond ((file-directory-p name)
2792                                           (file-name-as-directory name))
2793                                          ((file-exists-p name) name)
2794                                          (t gnus-article-save-directory))))
2795                          (read-file-name
2796                           (concat prompt " (default " name ") ")
2797                           dir name)))
2798                       ;; A list of splits was found.
2799                       (t
2800                        (setq split-name (nreverse split-name))
2801                        (let (result)
2802                          (let ((file-name-history
2803                                 (nconc split-name file-name-history)))
2804                            (setq result
2805                                  (expand-file-name
2806                                   (read-file-name
2807                                    (concat prompt " (`M-p' for defaults) ")
2808                                    gnus-article-save-directory
2809                                    (car split-name))
2810                                   gnus-article-save-directory)))
2811                          (car (push result file-name-history)))))))
2812                ;; Create the directory.
2813                (gnus-make-directory (file-name-directory file))
2814                ;; If we have read a directory, we append the default file name.
2815                (when (file-directory-p file)
2816                  (setq file (expand-file-name (file-name-nondirectory
2817                                                default-name)
2818                                               (file-name-as-directory file))))
2819                ;; Possibly translate some characters.
2820                (nnheader-translate-file-chars file))))))
2821     (gnus-make-directory (file-name-directory result))
2822     (set variable result)))
2823
2824 (defun gnus-article-archive-name (group)
2825   "Return the first instance of an \"Archive-name\" in the current buffer."
2826   (let ((case-fold-search t))
2827     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2828       (nnheader-concat gnus-article-save-directory
2829                        (match-string 1)))))
2830
2831 (defun gnus-article-nndoc-name (group)
2832   "If GROUP is an nndoc group, return the name of the parent group."
2833   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2834     (gnus-group-get-parameter group 'save-article-group)))
2835
2836 (defun gnus-summary-save-in-rmail (&optional filename)
2837   "Append this article to Rmail file.
2838 Optional argument FILENAME specifies file name.
2839 Directory to save to is default to `gnus-article-save-directory'."
2840   (setq filename (gnus-read-save-file-name
2841                   "Save %s in rmail file:" filename
2842                   gnus-rmail-save-name gnus-newsgroup-name
2843                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2844   (gnus-eval-in-buffer-window gnus-save-article-buffer
2845     (save-excursion
2846       (save-restriction
2847         (widen)
2848         (gnus-output-to-rmail filename))))
2849   filename)
2850
2851 (defun gnus-summary-save-in-mail (&optional filename)
2852   "Append this article to Unix mail file.
2853 Optional argument FILENAME specifies file name.
2854 Directory to save to is default to `gnus-article-save-directory'."
2855   (setq filename (gnus-read-save-file-name
2856                   "Save %s in Unix mail file:" filename
2857                   gnus-mail-save-name gnus-newsgroup-name
2858                   gnus-current-headers 'gnus-newsgroup-last-mail))
2859   (gnus-eval-in-buffer-window gnus-save-article-buffer
2860     (save-excursion
2861       (save-restriction
2862         (widen)
2863         (if (and (file-readable-p filename)
2864                  (mail-file-babyl-p filename))
2865             (rmail-output-to-rmail-file filename t)
2866           (gnus-output-to-mail filename)))))
2867   filename)
2868
2869 (defun gnus-summary-save-in-file (&optional filename overwrite)
2870   "Append this article to file.
2871 Optional argument FILENAME specifies file name.
2872 Directory to save to is default to `gnus-article-save-directory'."
2873   (setq filename (gnus-read-save-file-name
2874                   "Save %s in file:" filename
2875                   gnus-file-save-name gnus-newsgroup-name
2876                   gnus-current-headers 'gnus-newsgroup-last-file))
2877   (gnus-eval-in-buffer-window gnus-save-article-buffer
2878     (save-excursion
2879       (save-restriction
2880         (widen)
2881         (when (and overwrite
2882                    (file-exists-p filename))
2883           (delete-file filename))
2884         (gnus-output-to-file filename))))
2885   filename)
2886
2887 (defun gnus-summary-write-to-file (&optional filename)
2888   "Write this article to a file.
2889 Optional argument FILENAME specifies file name.
2890 The directory to save in defaults to `gnus-article-save-directory'."
2891   (gnus-summary-save-in-file nil t))
2892
2893 (defun gnus-summary-save-body-in-file (&optional filename)
2894   "Append this article body to a file.
2895 Optional argument FILENAME specifies file name.
2896 The directory to save in defaults to `gnus-article-save-directory'."
2897   (setq filename (gnus-read-save-file-name
2898                   "Save %s body in file:" filename
2899                   gnus-file-save-name gnus-newsgroup-name
2900                   gnus-current-headers 'gnus-newsgroup-last-file))
2901   (gnus-eval-in-buffer-window gnus-save-article-buffer
2902     (save-excursion
2903       (save-restriction
2904         (widen)
2905         (when (article-goto-body)
2906           (narrow-to-region (point) (point-max)))
2907         (gnus-output-to-file filename))))
2908   filename)
2909
2910 (defun gnus-summary-save-in-pipe (&optional command)
2911   "Pipe this article to subprocess."
2912   (setq command
2913         (cond ((and (eq command 'default)
2914                     gnus-last-shell-command)
2915                gnus-last-shell-command)
2916               ((stringp command)
2917                command)
2918               (t (read-string
2919                   (format
2920                    "Shell command on %s: "
2921                    (if (and gnus-number-of-articles-to-be-saved
2922                             (> gnus-number-of-articles-to-be-saved 1))
2923                        (format "these %d articles"
2924                                gnus-number-of-articles-to-be-saved)
2925                      "this article"))
2926                   gnus-last-shell-command))))
2927   (when (string-equal command "")
2928     (if gnus-last-shell-command
2929         (setq command gnus-last-shell-command)
2930       (error "A command is required")))
2931   (gnus-eval-in-buffer-window gnus-article-buffer
2932     (save-restriction
2933       (widen)
2934       (shell-command-on-region (point-min) (point-max) command nil)))
2935   (setq gnus-last-shell-command command))
2936
2937 ;;; Article file names when saving.
2938
2939 (defun gnus-capitalize-newsgroup (newsgroup)
2940   "Capitalize NEWSGROUP name."
2941   (when (not (zerop (length newsgroup)))
2942     (concat (char-to-string (upcase (aref newsgroup 0)))
2943             (substring newsgroup 1))))
2944
2945 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2946   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2947 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2948 Otherwise, it is like ~/News/news/group/num."
2949   (let ((default
2950           (expand-file-name
2951            (concat (if (gnus-use-long-file-name 'not-save)
2952                        (gnus-capitalize-newsgroup newsgroup)
2953                      (gnus-newsgroup-directory-form newsgroup))
2954                    "/" (int-to-string (mail-header-number headers)))
2955            gnus-article-save-directory)))
2956     (if (and last-file
2957              (string-equal (file-name-directory default)
2958                            (file-name-directory last-file))
2959              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2960         default
2961       (or last-file default))))
2962
2963 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2964   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2965 If variable `gnus-use-long-file-name' is non-nil, it is
2966 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2967   (let ((default
2968           (expand-file-name
2969            (concat (if (gnus-use-long-file-name 'not-save)
2970                        newsgroup
2971                      (gnus-newsgroup-directory-form newsgroup))
2972                    "/" (int-to-string (mail-header-number headers)))
2973            gnus-article-save-directory)))
2974     (if (and last-file
2975              (string-equal (file-name-directory default)
2976                            (file-name-directory last-file))
2977              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2978         default
2979       (or last-file default))))
2980
2981 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2982   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2983 If variable `gnus-use-long-file-name' is non-nil, it is
2984 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2985   (or last-file
2986       (expand-file-name
2987        (if (gnus-use-long-file-name 'not-save)
2988            newsgroup
2989          (file-relative-name
2990           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
2991           default-directory))
2992        gnus-article-save-directory)))
2993
2994 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
2995   "Generate file name from sender."
2996   (let ((from (mail-header-from headers)))
2997     (expand-file-name
2998      (if (and from (string-match "\\([^ <]+\\)@" from))
2999          (match-string 1 from)
3000        "nobody")
3001      gnus-article-save-directory)))
3002
3003 (defun article-verify-x-pgp-sig ()
3004   "Verify X-PGP-Sig."
3005   (interactive)
3006   (if (gnus-buffer-live-p gnus-original-article-buffer)
3007       (let ((sig (with-current-buffer gnus-original-article-buffer
3008                    (gnus-fetch-field "X-PGP-Sig")))
3009             items info headers)
3010         (when (and sig
3011                    mml2015-use
3012                    (mml2015-clear-verify-function))
3013           (with-temp-buffer
3014             (insert-buffer gnus-original-article-buffer)
3015             (setq items (split-string sig))
3016             (message-narrow-to-head)
3017             (let ((inhibit-point-motion-hooks t)
3018                   (case-fold-search t))
3019               ;; Don't verify multiple headers.
3020               (setq headers (mapconcat (lambda (header)
3021                                          (concat header ": "
3022                                                  (mail-fetch-field header) "\n"))
3023                                        (split-string (nth 1 items) ",") "")))
3024             (delete-region (point-min) (point-max))
3025             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3026             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3027             (insert headers)
3028             (widen)
3029             (forward-line)
3030             (while (not (eobp))
3031               (if (looking-at "^-")
3032                   (insert "- "))
3033               (forward-line))
3034             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3035             (insert "Version: " (car items) "\n\n")
3036             (insert (mapconcat 'identity (cddr items) "\n"))
3037             (insert "\n-----END PGP SIGNATURE-----\n")
3038             (let ((mm-security-handle (list (format "multipart/signed"))))
3039               (mml2015-clean-buffer)
3040               (let ((coding-system-for-write (or gnus-newsgroup-charset
3041                                                  'iso-8859-1)))
3042                 (funcall (mml2015-clear-verify-function)))
3043               (setq info
3044                     (or (mm-handle-multipart-ctl-parameter
3045                          mm-security-handle 'gnus-details)
3046                         (mm-handle-multipart-ctl-parameter
3047                          mm-security-handle 'gnus-info)))))
3048           (when info
3049             (let (buffer-read-only bface eface)
3050               (save-restriction
3051                 (message-narrow-to-head)
3052                 (goto-char (point-max))
3053                 (forward-line -1)
3054                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3055                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3056                 (message-remove-header "X-Gnus-PGP-Verify")
3057                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3058                     (forward-line)
3059                   (goto-char (point-max)))
3060                 (narrow-to-region (point) (point))
3061                 (insert "X-Gnus-PGP-Verify: " info "\n")
3062                 (goto-char (point-min))
3063                 (forward-line)
3064                 (while (not (eobp))
3065                   (if (not (looking-at "^[ \t]"))
3066                       (insert " "))
3067                   (forward-line))
3068                 ;; Do highlighting.
3069                 (goto-char (point-min))
3070                 (when (looking-at "\\([^:]+\\): *")
3071                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3072                                      'face bface)
3073                   (put-text-property (match-end 0) (point-max)
3074                                      'face eface)))))))))
3075
3076 (eval-and-compile
3077   (mapcar
3078    (lambda (func)
3079      (let (afunc gfunc)
3080        (if (consp func)
3081            (setq afunc (car func)
3082                  gfunc (cdr func))
3083          (setq afunc func
3084                gfunc (intern (format "gnus-%s" func))))
3085        (defalias gfunc
3086          (if (fboundp afunc)
3087              `(lambda (&optional interactive &rest args)
3088                 ,(documentation afunc t)
3089                 (interactive (list t))
3090                 (save-excursion
3091                   (set-buffer gnus-article-buffer)
3092                   (if interactive
3093                       (call-interactively ',afunc)
3094                     (apply ',afunc args))))))))
3095    '(article-hide-headers
3096      article-verify-x-pgp-sig
3097      article-hide-boring-headers
3098      article-toggle-headers
3099      article-treat-overstrike
3100      article-fill-long-lines
3101      article-capitalize-sentences
3102      article-remove-cr
3103      article-remove-leading-whitespace
3104      article-display-x-face
3105      article-de-quoted-unreadable
3106      article-de-base64-unreadable
3107      article-decode-HZ
3108      article-wash-html
3109      article-hide-list-identifiers
3110      article-hide-pgp
3111      article-strip-banner
3112      article-babel
3113      article-hide-pem
3114      article-hide-signature
3115      article-strip-headers-in-body
3116      article-remove-trailing-blank-lines
3117      article-strip-leading-blank-lines
3118      article-strip-multiple-blank-lines
3119      article-strip-leading-space
3120      article-strip-trailing-space
3121      article-strip-blank-lines
3122      article-strip-all-blank-lines
3123      article-date-local
3124      article-date-english
3125      article-date-iso8601
3126      article-date-original
3127      article-date-ut
3128      article-decode-mime-words
3129      article-decode-charset
3130      article-decode-encoded-words
3131      article-date-user
3132      article-date-lapsed
3133      article-emphasize
3134      article-treat-dumbquotes
3135      article-normalize-headers
3136      (article-show-all-headers . gnus-article-show-all-headers)
3137      (article-show-all . gnus-article-show-all))))
3138 \f
3139 ;;;
3140 ;;; Gnus article mode
3141 ;;;
3142
3143 (put 'gnus-article-mode 'mode-class 'special)
3144
3145 (gnus-define-keys gnus-article-mode-map
3146   " " gnus-article-goto-next-page
3147   "\177" gnus-article-goto-prev-page
3148   [delete] gnus-article-goto-prev-page
3149   [backspace] gnus-article-goto-prev-page
3150   "\C-c^" gnus-article-refer-article
3151   "h" gnus-article-show-summary
3152   "s" gnus-article-show-summary
3153   "\C-c\C-m" gnus-article-mail
3154   "?" gnus-article-describe-briefly
3155   "e" gnus-summary-edit-article
3156   "<" beginning-of-buffer
3157   ">" end-of-buffer
3158   "\C-c\C-i" gnus-info-find-node
3159   "\C-c\C-b" gnus-bug
3160   "\C-hk" gnus-article-describe-key
3161   "\C-hc" gnus-article-describe-key-briefly
3162
3163   "\C-d" gnus-article-read-summary-keys
3164   "\M-*" gnus-article-read-summary-keys
3165   "\M-#" gnus-article-read-summary-keys
3166   "\M-^" gnus-article-read-summary-keys
3167   "\M-g" gnus-article-read-summary-keys)
3168
3169 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3170 (let (keys)
3171   (let ((key 32))
3172     (while (<= key 127)
3173       (push (char-to-string key) keys)
3174       (incf key))
3175     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3176       (setq key (car elem))
3177       (when (and (> (length key) 0)
3178                  (not (eq 'menu-bar (aref key 0)))
3179                  (symbolp (lookup-key gnus-summary-mode-map key)))
3180         (push key keys))))
3181   (dolist (key keys)
3182     (unless (lookup-key gnus-article-mode-map key)
3183       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3184
3185 (defun gnus-article-make-menu-bar ()
3186   (unless (boundp 'gnus-article-commands-menu)
3187     (gnus-summary-make-menu-bar))
3188   (gnus-turn-off-edit-menu 'article)
3189   (unless (boundp 'gnus-article-article-menu)
3190     (easy-menu-define
3191      gnus-article-article-menu gnus-article-mode-map ""
3192      '("Article"
3193        ["Scroll forwards" gnus-article-goto-next-page t]
3194        ["Scroll backwards" gnus-article-goto-prev-page t]
3195        ["Show summary" gnus-article-show-summary t]
3196        ["Fetch Message-ID at point" gnus-article-refer-article t]
3197        ["Mail to address at point" gnus-article-mail t]
3198        ["Send a bug report" gnus-bug t]))
3199
3200     (easy-menu-define
3201      gnus-article-treatment-menu gnus-article-mode-map ""
3202      ;; Fixme: this should use :active (and maybe :visible).
3203      '("Treatment"
3204        ["Hide headers" gnus-article-toggle-headers t]
3205        ["Hide signature" gnus-article-hide-signature t]
3206        ["Hide citation" gnus-article-hide-citation t]
3207        ["Treat overstrike" gnus-article-treat-overstrike t]
3208        ["Remove carriage return" gnus-article-remove-cr t]
3209        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3210        ["Decode HZ" gnus-article-decode-HZ t]))
3211
3212     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3213
3214     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3215
3216     (gnus-run-hooks 'gnus-article-menu-hook)))
3217
3218 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3219 ;; Summary.
3220
3221 (defun gnus-article-mode ()
3222   "Major mode for displaying an article.
3223
3224 All normal editing commands are switched off.
3225
3226 The following commands are available in addition to all summary mode
3227 commands:
3228 \\<gnus-article-mode-map>
3229 \\[gnus-article-next-page]\t Scroll the article one page forwards
3230 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3231 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3232 \\[gnus-article-show-summary]\t Display the summary buffer
3233 \\[gnus-article-mail]\t Send a reply to the address near point
3234 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3235 \\[gnus-info-find-node]\t Go to the Gnus info node"
3236   (interactive)
3237   (gnus-simplify-mode-line)
3238   (setq mode-name "Article")
3239   (setq major-mode 'gnus-article-mode)
3240   (make-local-variable 'minor-mode-alist)
3241   (unless (assq 'gnus-show-mime minor-mode-alist)
3242     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3243   (use-local-map gnus-article-mode-map)
3244   (when (gnus-visual-p 'article-menu 'menu)
3245     (gnus-article-make-menu-bar))
3246   (gnus-update-format-specifications nil 'article-mode)
3247   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3248   (make-local-variable 'gnus-page-broken)
3249   (make-local-variable 'gnus-button-marker-list)
3250   (make-local-variable 'gnus-article-current-summary)
3251   (make-local-variable 'gnus-article-mime-handles)
3252   (make-local-variable 'gnus-article-decoded-p)
3253   (make-local-variable 'gnus-article-mime-handle-alist)
3254   (make-local-variable 'gnus-article-wash-types)
3255   (make-local-variable 'gnus-article-charset)
3256   (make-local-variable 'gnus-article-ignored-charsets)
3257   (gnus-set-default-directory)
3258   (buffer-disable-undo)
3259   (setq buffer-read-only t)
3260   (set-syntax-table gnus-article-mode-syntax-table)
3261   (gnus-run-hooks 'gnus-article-mode-hook))
3262
3263 (defun gnus-article-setup-buffer ()
3264   "Initialize the article buffer."
3265   (let* ((name (if gnus-single-article-buffer "*Article*"
3266                  (concat "*Article " gnus-newsgroup-name "*")))
3267          (original
3268           (progn (string-match "\\*Article" name)
3269                  (concat " *Original Article"
3270                          (substring name (match-end 0))))))
3271     (setq gnus-article-buffer name)
3272     (setq gnus-original-article-buffer original)
3273     (setq gnus-article-mime-handle-alist nil)
3274     ;; This might be a variable local to the summary buffer.
3275     (unless gnus-single-article-buffer
3276       (save-excursion
3277         (set-buffer gnus-summary-buffer)
3278         (setq gnus-article-buffer name)
3279         (setq gnus-original-article-buffer original)
3280         (gnus-set-global-variables)))
3281     (gnus-article-setup-highlight-words)
3282     ;; Init original article buffer.
3283     (save-excursion
3284       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3285       (set-buffer-multibyte nil)
3286       (setq major-mode 'gnus-original-article-mode)
3287       (make-local-variable 'gnus-original-article))
3288     (if (get-buffer name)
3289         (save-excursion
3290           (set-buffer name)
3291           (buffer-disable-undo)
3292           (setq buffer-read-only t)
3293           (unless (eq major-mode 'gnus-article-mode)
3294             (gnus-article-mode))
3295           (current-buffer))
3296       (save-excursion
3297         (set-buffer (gnus-get-buffer-create name))
3298         (gnus-article-mode)
3299         (make-local-variable 'gnus-summary-buffer)
3300         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3301         (current-buffer)))))
3302
3303 ;; Set article window start at LINE, where LINE is the number of lines
3304 ;; from the head of the article.
3305 (defun gnus-article-set-window-start (&optional line)
3306   (set-window-start
3307    (get-buffer-window gnus-article-buffer t)
3308    (save-excursion
3309      (set-buffer gnus-article-buffer)
3310      (goto-char (point-min))
3311      (if (not line)
3312          (point-min)
3313        (gnus-message 6 "Moved to bookmark")
3314        (search-forward "\n\n" nil t)
3315        (forward-line line)
3316        (point)))))
3317
3318 ;;; @@ article filters
3319 ;;;
3320
3321 (defun gnus-article-display-mime-message ()
3322   "Article display method for MIME message."
3323   ;; called from `gnus-original-article-buffer'.
3324   (let (charset all-headers)
3325     (with-current-buffer gnus-summary-buffer
3326       (setq charset default-mime-charset
3327             all-headers gnus-have-all-headers))
3328     (make-local-variable 'default-mime-charset)
3329     (setq default-mime-charset charset)
3330     (with-current-buffer (get-buffer-create gnus-article-buffer)
3331       (make-local-variable 'default-mime-charset)
3332       (setq default-mime-charset charset))
3333     (mime-display-message mime-message-structure
3334                           gnus-article-buffer nil gnus-article-mode-map)
3335     (when all-headers
3336       (gnus-article-hide-headers nil -1)))
3337   (run-hooks 'gnus-mime-article-prepare-hook))
3338
3339 (defun gnus-article-display-traditional-message ()
3340   "Article display method for traditional message."
3341   (set-buffer gnus-article-buffer)
3342   (let (buffer-read-only)
3343     (erase-buffer)
3344     (insert-buffer-substring gnus-original-article-buffer)))
3345
3346 (defun gnus-article-make-full-mail-header (&optional number charset)
3347   "Create a new mail header structure in a raw article buffer."
3348   (unless (and number charset)
3349     (save-current-buffer
3350       (set-buffer gnus-summary-buffer)
3351       (unless number
3352         (setq number (or (cdr gnus-article-current) 0)))
3353       (unless charset
3354         (setq charset (or default-mime-charset 'x-ctext)))))
3355   (goto-char (point-min))
3356   (let ((header-end (if (search-forward "\n\n" nil t)
3357                         (1- (point))
3358                       (goto-char (point-max))))
3359         (chars (- (point-max) (point)))
3360         (lines (count-lines (point) (point-max)))
3361         (default-mime-charset charset)
3362         xref)
3363     (narrow-to-region (point-min) header-end)
3364     (setq xref (std11-fetch-field "xref"))
3365     (prog1
3366         (make-full-mail-header
3367          number
3368          (std11-fetch-field "subject")
3369          (std11-fetch-field "from")
3370          (std11-fetch-field "date")
3371          (std11-fetch-field "message-id")
3372          (std11-fetch-field "references")
3373          chars
3374          lines
3375          (when xref (concat "Xref: " xref)))
3376       (widen))))
3377
3378 (defun gnus-article-prepare (article &optional all-headers header)
3379   "Prepare ARTICLE in article mode buffer.
3380 ARTICLE should either be an article number or a Message-ID.
3381 If ARTICLE is an id, HEADER should be the article headers.
3382 If ALL-HEADERS is non-nil, no headers are hidden."
3383   (save-excursion
3384     ;; Make sure we start in a summary buffer.
3385     (unless (eq major-mode 'gnus-summary-mode)
3386       (set-buffer gnus-summary-buffer))
3387     (setq gnus-summary-buffer (current-buffer))
3388     (let* ((gnus-article (if header (mail-header-number header) article))
3389            (summary-buffer (current-buffer))
3390            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3391            (group gnus-newsgroup-name)
3392            result)
3393       (save-excursion
3394         (gnus-article-setup-buffer)
3395         (set-buffer gnus-article-buffer)
3396         ;; Deactivate active regions.
3397         (when (and (boundp 'transient-mark-mode)
3398                    transient-mark-mode)
3399           (setq mark-active nil))
3400         (if (not (setq result (let ((buffer-read-only nil))
3401                                 (gnus-request-article-this-buffer
3402                                  article group))))
3403             ;; There is no such article.
3404             (save-excursion
3405               (when (and (numberp article)
3406                          (not (memq article gnus-newsgroup-sparse)))
3407                 (setq gnus-article-current
3408                       (cons gnus-newsgroup-name article))
3409                 (set-buffer gnus-summary-buffer)
3410                 (setq gnus-current-article article)
3411                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3412                     (progn
3413                       (gnus-summary-set-agent-mark article)
3414                       (message "Message marked for downloading"))
3415                   (gnus-summary-mark-article article gnus-canceled-mark)
3416                   (unless (memq article gnus-newsgroup-sparse)
3417                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3418           (if (or (eq result 'pseudo)
3419                   (eq result 'nneething))
3420               (progn
3421                 (save-excursion
3422                   (set-buffer summary-buffer)
3423                   (push article gnus-newsgroup-history)
3424                   (setq gnus-last-article gnus-current-article
3425                         gnus-current-article 0
3426                         gnus-current-headers nil
3427                         gnus-article-current nil)
3428                   (if (eq result 'nneething)
3429                       (gnus-configure-windows 'summary)
3430                     (gnus-configure-windows 'article))
3431                   (gnus-set-global-variables))
3432                 (let ((gnus-article-mime-handle-alist-1
3433                        gnus-article-mime-handle-alist))
3434                   (gnus-set-mode-line 'article)))
3435             ;; The result from the `request' was an actual article -
3436             ;; or at least some text that is now displayed in the
3437             ;; article buffer.
3438             (when (and (numberp article)
3439                        (not (eq article gnus-current-article)))
3440               ;; Seems like a new article has been selected.
3441               ;; `gnus-current-article' must be an article number.
3442               (save-excursion
3443                 (set-buffer summary-buffer)
3444                 (push article gnus-newsgroup-history)
3445                 (setq gnus-last-article gnus-current-article
3446                       gnus-current-article article
3447                       gnus-current-headers
3448                       (gnus-summary-article-header gnus-current-article)
3449                       gnus-article-current
3450                       (cons gnus-newsgroup-name gnus-current-article))
3451                 (unless (vectorp gnus-current-headers)
3452                   (setq gnus-current-headers nil))
3453                 (gnus-summary-goto-subject gnus-current-article)
3454                 (when (gnus-summary-show-thread)
3455                   ;; If the summary buffer really was folded, the
3456                   ;; previous goto may not actually have gone to
3457                   ;; the right article, but the thread root instead.
3458                   ;; So we go again.
3459                   (gnus-summary-goto-subject gnus-current-article))
3460                 (gnus-run-hooks 'gnus-mark-article-hook)
3461                 (gnus-set-mode-line 'summary)
3462                 (when (gnus-visual-p 'article-highlight 'highlight)
3463                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3464                 ;; Set the global newsgroup variables here.
3465                 (gnus-set-global-variables)
3466                 (setq gnus-have-all-headers
3467                       (or all-headers gnus-show-all-headers))))
3468             (save-excursion
3469               (gnus-configure-windows 'article))
3470             (when (or (numberp article)
3471                       (stringp article))
3472               (gnus-article-prepare-display)
3473               ;; Do page break.
3474               (goto-char (point-min))
3475               (setq gnus-page-broken
3476                     (when gnus-break-pages
3477                       (gnus-narrow-to-page)
3478                       t)))
3479             (let ((gnus-article-mime-handle-alist-1
3480                    gnus-article-mime-handle-alist))
3481               (gnus-set-mode-line 'article))
3482             (article-goto-body)
3483             (set-window-point (get-buffer-window (current-buffer)) (point))
3484             (gnus-configure-windows 'article)
3485             t))))))
3486
3487 (defun gnus-article-prepare-mime-display (&optional number)
3488   (goto-char (point-min))
3489   (when (re-search-forward "^[^\t ]+:" nil t)
3490     (goto-char (match-beginning 0)))
3491   (let ((entity (if (eq 1 (point-min))
3492                     (get-text-property 1 'mime-view-entity)
3493                   (get-text-property (point) 'mime-view-entity)))
3494         last-entity child-entity next type)
3495     (setq child-entity (mime-entity-children entity))
3496     (if child-entity
3497         (setq last-entity (nth (1- (length child-entity))
3498                                child-entity))
3499       (setq last-entity entity))
3500     (save-restriction
3501       (narrow-to-region (point)
3502                         (if (search-forward "\n\n" nil t)
3503                             (point)
3504                           (point-max)))
3505       (gnus-treat-article 'head)
3506       (put-text-property (point-min) (point-max) 'article-treated-header t)
3507       (goto-char (point-max)))
3508     (while (and (not (eobp)) entity)
3509       (setq next (set-marker
3510                   (make-marker)
3511                   (next-single-property-change (point) 'mime-view-entity
3512                                                nil (point-max))))
3513       (let ((types (mime-entity-content-type entity)))
3514         (while (eq 'multipart (mime-content-type-primary-type types))
3515           (setq entity (car (mime-entity-children entity))
3516                 types (mime-entity-content-type entity)))
3517         (when types
3518           (setq type (format "%s/%s"
3519                              (mime-content-type-primary-type types)
3520                              (mime-content-type-subtype types)))))
3521       (if (string-equal type "message/rfc822")
3522           (progn
3523             (setq next (point))
3524             (let ((children (mime-entity-children entity))
3525                   last-children)
3526               (when children
3527                 (setq last-children (nth (1- (length children)) children))
3528                 (while
3529                     (and
3530                      (not (eq last-children
3531                               (get-text-property next 'mime-view-entity)))
3532                      (setq next
3533                            (next-single-property-change next
3534                                                         'mime-view-entity
3535                                                         nil (point-max)))))))
3536             (setq next (next-single-property-change next 'mime-view-entity
3537                                                     nil (point-max)))
3538             (save-restriction
3539               (narrow-to-region (point) next)
3540               (gnus-article-prepare-mime-display)
3541               (goto-char (point-max)))
3542             (setq entity (get-text-property (point) 'mime-view-entity)))
3543         (save-restriction
3544           (narrow-to-region (point) next)
3545           ;; Kludge. We have to count true number, but for now,
3546           ;; part number is here only to achieve `last'.
3547           (gnus-treat-article nil 1
3548                               (if (eq entity last-entity)
3549                                   1 2)
3550                               type)
3551           (goto-char (point-max)))
3552         (setq entity (get-text-property next 'mime-view-entity))))))
3553
3554 ;;;###autoload
3555 (defun gnus-article-prepare-display ()
3556   "Make the current buffer look like a nice article."
3557   (let ((gnus-article-buffer (current-buffer))
3558         buffer-read-only)
3559     (unless (eq major-mode 'gnus-article-mode)
3560       (gnus-article-mode))
3561     (setq buffer-read-only nil
3562           gnus-button-marker-list nil
3563           gnus-article-wash-types nil)
3564     (save-restriction
3565       (widen)
3566       (static-if (featurep 'xemacs)
3567           (map-extents (lambda (extent maparg) (delete-extent extent)))
3568         (let ((lists (overlay-lists)))
3569           (dolist (overlay (nconc (car lists) (cdr lists)))
3570             (delete-overlay overlay)))))
3571     (gnus-run-hooks 'gnus-tmp-internal-hook))
3572   (set-buffer gnus-original-article-buffer)
3573   ;; Display message.
3574   (setq mime-message-structure gnus-current-headers)
3575   (mime-buffer-entity-set-buffer-internal mime-message-structure
3576                                           gnus-original-article-buffer)
3577   (mime-entity-set-representation-type-internal mime-message-structure
3578                                                 'mime-buffer-entity)
3579   (luna-send mime-message-structure 'initialize-instance
3580              mime-message-structure)
3581   (if gnus-show-mime
3582       (let (mime-display-header-hook mime-display-text/plain-hook)
3583         (funcall gnus-article-display-method-for-mime))
3584     (funcall gnus-article-display-method-for-traditional))
3585   ;; Call the treatment functions.
3586   (let ((inhibit-read-only t))
3587     (save-restriction
3588       (widen)
3589       (if gnus-show-mime
3590           (gnus-article-prepare-mime-display)
3591         (narrow-to-region (goto-char (point-min))
3592                           (if (search-forward "\n\n" nil t)
3593                               (point)
3594                             (point-max)))
3595         (gnus-treat-article 'head)
3596         (put-text-property (point-min) (point-max) 'article-treated-header t)
3597         (goto-char (point-max))
3598         (widen)
3599         (narrow-to-region (point) (point-max))
3600         (gnus-treat-article nil))
3601       (put-text-property (point-min) (point-max) 'read-only nil)))
3602   (gnus-run-hooks 'gnus-article-prepare-hook))
3603
3604 (defun gnus-article-decode-article-as-default-mime-charset ()
3605   "Decode an article as `default-mime-charset'.  It won't work if the
3606 value of the variable `gnus-show-mime' is non-nil."
3607   (unless gnus-show-mime
3608     (set (make-local-variable 'default-mime-charset)
3609          (with-current-buffer gnus-summary-buffer
3610            default-mime-charset))
3611     (decode-mime-charset-region (point-min) (point-max)
3612                                 default-mime-charset)))
3613
3614 ;;;
3615 ;;; Gnus MIME viewing functions
3616 ;;;
3617
3618 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3619   "The following specs can be used:
3620 %t  The MIME type
3621 %T  MIME type, along with additional info
3622 %n  The `name' parameter
3623 %d  The description, if any
3624 %l  The length of the encoded part
3625 %p  The part identifier number
3626 %e  Dots if the part isn't displayed")
3627
3628 (defvar gnus-mime-button-line-format-alist
3629   '((?t gnus-tmp-type ?s)
3630     (?T gnus-tmp-type-long ?s)
3631     (?n gnus-tmp-name ?s)
3632     (?d gnus-tmp-description ?s)
3633     (?p gnus-tmp-id ?s)
3634     (?l gnus-tmp-length ?d)
3635     (?e gnus-tmp-dots ?s)))
3636
3637 (defvar gnus-mime-button-commands
3638   '((gnus-article-press-button "\r" "Toggle Display")
3639     (gnus-mime-view-part "v" "View Interactively...")
3640     (gnus-mime-view-part-as-type "t" "View As Type...")
3641     (gnus-mime-view-part-as-charset "C" "View As charset...")
3642     (gnus-mime-save-part "o" "Save...")
3643     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3644     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3645     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3646     (gnus-mime-internalize-part "E" "View Internally")
3647     (gnus-mime-externalize-part "e" "View Externally")
3648     (gnus-mime-pipe-part "|" "Pipe To Command...")
3649     (gnus-mime-action-on-part "." "Take action on the part")))
3650
3651 (defun gnus-article-mime-part-status ()
3652   (with-current-buffer gnus-article-buffer
3653     (let ((entity (get-text-property (point-min) 'mime-view-entity))
3654           children)
3655       (if (and entity
3656                (setq children (mime-entity-children entity))
3657                (setq children (length children)))
3658           (if (eq 1 children)
3659               " (1 part)"
3660             (format " (%d parts)" children))
3661         ""))))
3662
3663 (defvar gnus-mime-button-map
3664   (let ((map (make-sparse-keymap)))
3665     (unless (>= (string-to-number emacs-version) 21)
3666       ;; XEmacs doesn't care.
3667       (set-keymap-parent map gnus-article-mode-map))
3668     (define-key map gnus-mouse-2 'gnus-article-push-button)
3669     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3670     (dolist (c gnus-mime-button-commands)
3671       (define-key map (cadr c) (car c)))
3672     map))
3673
3674 (defun gnus-mime-button-menu (event)
3675   "Construct a context-sensitive menu of MIME commands."
3676   (interactive "e")
3677   (save-window-excursion
3678     (let ((pos (event-start event)))
3679       (select-window (posn-window pos))
3680       (goto-char (posn-point pos))
3681       (gnus-article-check-buffer)
3682       (let ((response (x-popup-menu
3683                        t `("MIME Part"
3684                            ("" ,@(mapcar (lambda (c)
3685                                            (cons (caddr c) (car c)))
3686                                          gnus-mime-button-commands))))))
3687         (if response
3688             (call-interactively response))))))
3689
3690 (defun gnus-mime-view-all-parts (&optional handles)
3691   "View all the MIME parts."
3692   (interactive)
3693   (save-current-buffer
3694     (set-buffer gnus-article-buffer)
3695     (let ((handles (or handles gnus-article-mime-handles))
3696           (mail-parse-charset gnus-newsgroup-charset)
3697           (mail-parse-ignored-charsets
3698            (with-current-buffer gnus-summary-buffer
3699              gnus-newsgroup-ignored-charsets)))
3700       (when handles
3701         (mm-remove-parts handles)
3702         (goto-char (point-min))
3703         (or (search-forward "\n\n") (goto-char (point-max)))
3704         (let (buffer-read-only)
3705           (delete-region (point) (point-max))
3706           (mm-display-parts handles))))))
3707
3708 (defun gnus-mime-save-part-and-strip ()
3709   "Save the MIME part under point then replace it with an external body."
3710   (interactive)
3711   (gnus-article-check-buffer)
3712   (let* ((data (get-text-property (point) 'gnus-data))
3713          file param
3714          (handles gnus-article-mime-handles))
3715     (if (mm-multiple-handles gnus-article-mime-handles)
3716         (error "This function is not implemented"))
3717     (setq file (and data (mm-save-part data)))
3718     (when file
3719       (with-current-buffer (mm-handle-buffer data)
3720         (erase-buffer)
3721         (insert "Content-Type: " (mm-handle-media-type data))
3722         (mml-insert-parameter-string (cdr (mm-handle-type data))
3723                                      '(charset))
3724         (insert "\n")
3725         (insert "Content-ID: " (message-make-message-id) "\n")
3726         (insert "Content-Transfer-Encoding: binary\n")
3727         (insert "\n"))
3728       (setcdr data
3729               (cdr (mm-make-handle nil
3730                                    `("message/external-body"
3731                                      (access-type . "LOCAL-FILE")
3732                                      (name . ,file)))))
3733       (set-buffer gnus-summary-buffer)
3734       (gnus-article-edit-article
3735        `(lambda ()
3736           (erase-buffer)
3737           (let ((mail-parse-charset (or gnus-article-charset
3738                                         ',gnus-newsgroup-charset))
3739                 (mail-parse-ignored-charsets
3740                  (or gnus-article-ignored-charsets
3741                      ',gnus-newsgroup-ignored-charsets))
3742                 (mbl mml-buffer-list))
3743             (setq mml-buffer-list nil)
3744             (insert-buffer gnus-original-article-buffer)
3745             (mime-to-mml ',handles)
3746             (setq gnus-article-mime-handles nil)
3747             (let ((mbl1 mml-buffer-list))
3748               (setq mml-buffer-list mbl)
3749               (set (make-local-variable 'mml-buffer-list) mbl1))
3750             ;; LOCAL argument of add-hook differs between GNU Emacs
3751             ;; and XEmacs. make-local-hook makes sure they are local.
3752             (make-local-hook 'kill-buffer-hook)
3753             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3754        `(lambda (no-highlight)
3755           (let ((mail-parse-charset (or gnus-article-charset
3756                                         ',gnus-newsgroup-charset))
3757                 (message-options message-options)
3758                 (message-options-set-recipient)
3759                 (mail-parse-ignored-charsets
3760                  (or gnus-article-ignored-charsets
3761                      ',gnus-newsgroup-ignored-charsets)))
3762             (mml-to-mime)
3763             (mml-destroy-buffers)
3764             (remove-hook 'kill-buffer-hook
3765                          'mml-destroy-buffers t)
3766             (kill-local-variable 'mml-buffer-list))
3767           (gnus-summary-edit-article-done
3768            ,(or (mail-header-references gnus-current-headers) "")
3769            ,(gnus-group-read-only-p)
3770            ,gnus-summary-buffer no-highlight))))))
3771
3772 (defun gnus-mime-save-part ()
3773   "Save the MIME part under point."
3774   (interactive)
3775   (gnus-article-check-buffer)
3776   (let ((data (get-text-property (point) 'gnus-data)))
3777     (when data
3778       (mm-save-part data))))
3779
3780 (defun gnus-mime-pipe-part ()
3781   "Pipe the MIME part under point to a process."
3782   (interactive)
3783   (gnus-article-check-buffer)
3784   (let ((data (get-text-property (point) 'gnus-data)))
3785     (when data
3786       (mm-pipe-part data))))
3787
3788 (defun gnus-mime-view-part ()
3789   "Interactively choose a viewing method for the MIME part under point."
3790   (interactive)
3791   (gnus-article-check-buffer)
3792   (let ((data (get-text-property (point) 'gnus-data)))
3793     (when data
3794       (setq gnus-article-mime-handles
3795             (mm-merge-handles
3796              gnus-article-mime-handles (setq data (copy-sequence data))))
3797       (mm-interactively-view-part data))))
3798
3799 (defun gnus-mime-view-part-as-type-internal ()
3800   (gnus-article-check-buffer)
3801   (let* ((name (mail-content-type-get
3802                 (mm-handle-type (get-text-property (point) 'gnus-data))
3803                 'name))
3804          (def-type (and name (mm-default-file-encoding name))))
3805     (and def-type (cons def-type 0))))
3806
3807 (defun gnus-mime-view-part-as-type (&optional mime-type)
3808   "Choose a MIME media type, and view the part as such."
3809   (interactive)
3810   (unless mime-type
3811     (setq mime-type (completing-read
3812                      "View as MIME type: "
3813                      (mapcar #'list (mailcap-mime-types))
3814                      nil nil
3815                      (gnus-mime-view-part-as-type-internal))))
3816   (gnus-article-check-buffer)
3817   (let ((handle (get-text-property (point) 'gnus-data)))
3818     (when handle
3819       (setq handle
3820             (mm-make-handle (mm-handle-buffer handle)
3821                             (cons mime-type (cdr (mm-handle-type handle)))
3822                             (mm-handle-encoding handle)
3823                             (mm-handle-undisplayer handle)
3824                             (mm-handle-disposition handle)
3825                             (mm-handle-description handle)
3826                             nil
3827                             (mm-handle-id handle)))
3828       (setq gnus-article-mime-handles
3829             (mm-merge-handles gnus-article-mime-handles handle))
3830       (gnus-mm-display-part handle))))
3831
3832 (defun gnus-mime-copy-part (&optional handle)
3833   "Put the the MIME part under point into a new buffer."
3834   (interactive)
3835   (gnus-article-check-buffer)
3836   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3837          (contents (and handle (mm-get-part handle)))
3838          (base (and handle
3839                     (file-name-nondirectory
3840                      (or
3841                       (mail-content-type-get (mm-handle-type handle) 'name)
3842                       (mail-content-type-get (mm-handle-type handle)
3843                                              'filename)
3844                       "*decoded*"))))
3845          (buffer (and base (generate-new-buffer base))))
3846     (when contents
3847       (switch-to-buffer buffer)
3848       (insert contents)
3849       ;; We do it this way to make `normal-mode' set the appropriate mode.
3850       (unwind-protect
3851           (progn
3852             (setq buffer-file-name (expand-file-name base))
3853             (normal-mode))
3854         (setq buffer-file-name nil))
3855       (goto-char (point-min)))))
3856
3857 (defun gnus-mime-inline-part (&optional handle arg)
3858   "Insert the MIME part under point into the current buffer."
3859   (interactive (list nil current-prefix-arg))
3860   (gnus-article-check-buffer)
3861   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3862          contents charset
3863          (b (point))
3864          buffer-read-only)
3865     (when handle
3866       (if (and (not arg) (mm-handle-undisplayer handle))
3867           (mm-remove-part handle)
3868         (setq contents (mm-get-part handle))
3869         (cond
3870          ((not arg)
3871           (setq charset (or (mail-content-type-get
3872                              (mm-handle-type handle) 'charset)
3873                             gnus-newsgroup-charset)))
3874          ((numberp arg)
3875           (if (mm-handle-undisplayer handle)
3876               (mm-remove-part handle))
3877           (setq charset
3878                 (or (cdr (assq arg
3879                                gnus-summary-show-article-charset-alist))
3880                     (mm-read-coding-system "Charset: ")))))
3881         (forward-line 2)
3882         (mm-insert-inline handle
3883                           (if (and charset
3884                                    (setq charset (mm-charset-to-coding-system
3885                                                   charset))
3886                                    (not (eq charset 'ascii)))
3887                               (mm-decode-coding-string contents charset)
3888                             contents))
3889         (goto-char b)))))
3890
3891 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3892   "Insert the MIME part under point into the current buffer using the
3893 specified charset."
3894   (interactive (list nil current-prefix-arg))
3895   (gnus-article-check-buffer)
3896   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3897          contents charset
3898          (b (point))
3899          buffer-read-only)
3900     (when handle
3901       (if (mm-handle-undisplayer handle)
3902           (mm-remove-part handle))
3903       (let ((gnus-newsgroup-charset
3904              (or (cdr (assq arg
3905                             gnus-summary-show-article-charset-alist))
3906                  (mm-read-coding-system "Charset: ")))
3907             (gnus-newsgroup-ignored-charsets 'gnus-all))
3908         (gnus-article-press-button)))))
3909
3910 (defun gnus-mime-externalize-part (&optional handle)
3911   "View the MIME part under point with an external viewer."
3912   (interactive)
3913   (gnus-article-check-buffer)
3914   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3915          (mm-user-display-methods nil)
3916          (mm-inlined-types nil)
3917          (mail-parse-charset gnus-newsgroup-charset)
3918          (mail-parse-ignored-charsets
3919           (save-excursion (set-buffer gnus-summary-buffer)
3920                           gnus-newsgroup-ignored-charsets)))
3921     (when handle
3922       (if (mm-handle-undisplayer handle)
3923           (mm-remove-part handle)
3924         (mm-display-part handle)))))
3925
3926 (defun gnus-mime-internalize-part (&optional handle)
3927   "View the MIME part under point with an internal viewer.
3928 If no internal viewer is available, use an external viewer."
3929   (interactive)
3930   (gnus-article-check-buffer)
3931   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3932          (mm-inlined-types '(".*"))
3933          (mm-inline-large-images t)
3934          (mail-parse-charset gnus-newsgroup-charset)
3935          (mail-parse-ignored-charsets
3936           (save-excursion (set-buffer gnus-summary-buffer)
3937                           gnus-newsgroup-ignored-charsets)))
3938     (when handle
3939       (if (mm-handle-undisplayer handle)
3940           (mm-remove-part handle)
3941         (mm-display-part handle)))))
3942
3943 (defun gnus-mime-action-on-part (&optional action)
3944   "Do something with the MIME attachment at \(point\)."
3945   (interactive
3946    (list (completing-read "Action: " gnus-mime-action-alist)))
3947   (gnus-article-check-buffer)
3948   (let ((action-pair (assoc action gnus-mime-action-alist)))
3949     (if action-pair
3950         (funcall (cdr action-pair)))))
3951
3952 (defun gnus-article-part-wrapper (n function)
3953   (save-current-buffer
3954     (set-buffer gnus-article-buffer)
3955     (when (> n (length gnus-article-mime-handle-alist))
3956       (error "No such part"))
3957     (gnus-article-goto-part n)
3958     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3959       (funcall function handle))))
3960
3961 (defun gnus-article-pipe-part (n)
3962   "Pipe MIME part N, which is the numerical prefix."
3963   (interactive "p")
3964   (gnus-article-part-wrapper n 'mm-pipe-part))
3965
3966 (defun gnus-article-save-part (n)
3967   "Save MIME part N, which is the numerical prefix."
3968   (interactive "p")
3969   (gnus-article-part-wrapper n 'mm-save-part))
3970
3971 (defun gnus-article-interactively-view-part (n)
3972   "View MIME part N interactively, which is the numerical prefix."
3973   (interactive "p")
3974   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3975
3976 (defun gnus-article-copy-part (n)
3977   "Copy MIME part N, which is the numerical prefix."
3978   (interactive "p")
3979   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3980
3981 (defun gnus-article-view-part-as-charset (n)
3982   "Copy MIME part N, which is the numerical prefix."
3983   (interactive "p")
3984   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3985
3986 (defun gnus-article-externalize-part (n)
3987   "View MIME part N externally, which is the numerical prefix."
3988   (interactive "p")
3989   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3990
3991 (defun gnus-article-inline-part (n)
3992   "Inline MIME part N, which is the numerical prefix."
3993   (interactive "p")
3994   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3995
3996 (defun gnus-article-mime-match-handle-first (condition)
3997   (if condition
3998       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3999         (while (setq ihandle (pop alist))
4000           (if (and (cond
4001                     ((functionp condition)
4002                      (funcall condition (cdr ihandle)))
4003                     ((eq condition 'undisplayed)
4004                      (not (or (mm-handle-undisplayer (cdr ihandle))
4005                               (equal (mm-handle-media-type (cdr ihandle))
4006                                      "multipart/alternative"))))
4007                     ((eq condition 'undisplayed-alternative)
4008                      (not (mm-handle-undisplayer (cdr ihandle))))
4009                     (t t))
4010                    (gnus-article-goto-part (car ihandle))
4011                    (or (not n) (< (car ihandle) n)))
4012               (setq n (car ihandle))))
4013         (or n 1))
4014     1))
4015
4016 (defun gnus-article-view-part (&optional n)
4017   "View MIME part N, which is the numerical prefix."
4018   (interactive "P")
4019   (save-current-buffer
4020     (set-buffer gnus-article-buffer)
4021     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4022                              gnus-article-mime-match-handle-function)))
4023     (when (> n (length gnus-article-mime-handle-alist))
4024       (error "No such part"))
4025     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4026       (when (gnus-article-goto-part n)
4027         (if (equal (car handle) "multipart/alternative")
4028             (gnus-article-press-button)
4029           (when (eq (gnus-mm-display-part handle) 'internal)
4030             (gnus-set-window-start)))))))
4031
4032 (defsubst gnus-article-mime-total-parts ()
4033   (if (bufferp (car gnus-article-mime-handles))
4034       1 ;; single part
4035     (1- (length gnus-article-mime-handles))))
4036
4037 (defun gnus-mm-display-part (handle)
4038   "Display HANDLE and fix MIME button."
4039   (let ((id (get-text-property (point) 'gnus-part))
4040         (point (point))
4041         buffer-read-only)
4042     (forward-line 1)
4043     (prog1
4044         (let ((window (selected-window))
4045               (mail-parse-charset gnus-newsgroup-charset)
4046               (mail-parse-ignored-charsets
4047                (save-excursion (set-buffer gnus-summary-buffer)
4048                                gnus-newsgroup-ignored-charsets)))
4049           (save-excursion
4050             (unwind-protect
4051                 (let ((win (get-buffer-window (current-buffer) t))
4052                       (beg (point)))
4053                   (when win
4054                     (select-window win))
4055                   (goto-char point)
4056                   (forward-line)
4057                   (if (mm-handle-displayed-p handle)
4058                       ;; This will remove the part.
4059                       (mm-display-part handle)
4060                     (save-restriction
4061                       (narrow-to-region (point)
4062                                         (if (eobp) (point) (1+ (point))))
4063                       (mm-display-part handle)
4064                       ;; We narrow to the part itself and
4065                       ;; then call the treatment functions.
4066                       (goto-char (point-min))
4067                       (forward-line 1)
4068                       (narrow-to-region (point) (point-max))
4069                       (gnus-treat-article
4070                        nil id
4071                        (gnus-article-mime-total-parts)
4072                        (mm-handle-media-type handle)))))
4073               (if (window-live-p window)
4074                   (select-window window)))))
4075       (goto-char point)
4076       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4077       (gnus-insert-mime-button
4078        handle id (list (mm-handle-displayed-p handle)))
4079       (goto-char point))))
4080
4081 (defun gnus-article-goto-part (n)
4082   "Go to MIME part N."
4083   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4084     (when point
4085       (goto-char point))))
4086
4087 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4088   (let ((gnus-tmp-name
4089          (or (mail-content-type-get (mm-handle-type handle) 'name)
4090              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4091              (mail-content-type-get (mm-handle-type handle) 'url)
4092              ""))
4093         (gnus-tmp-type (mm-handle-media-type handle))
4094         (gnus-tmp-description
4095          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4096                                               "")))
4097         (gnus-tmp-dots
4098          (if (if displayed (car displayed)
4099                (mm-handle-displayed-p handle))
4100              "" "..."))
4101         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4102                            (buffer-size)))
4103         gnus-tmp-type-long b e)
4104     (when (string-match ".*/" gnus-tmp-name)
4105       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4106     (setq gnus-tmp-type-long (concat gnus-tmp-type
4107                                      (and (not (equal gnus-tmp-name ""))
4108                                           (concat "; " gnus-tmp-name))))
4109     (unless (equal gnus-tmp-description "")
4110       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4111     (unless (bolp)
4112       (insert "\n"))
4113     (setq b (point))
4114     (gnus-eval-format
4115      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4116      `(keymap ,gnus-mime-button-map
4117               ,@(if (>= (string-to-number emacs-version) 21)
4118                     nil
4119                   (list 'local-map gnus-mime-button-map))
4120               gnus-callback gnus-mm-display-part
4121               gnus-part ,gnus-tmp-id
4122               article-type annotation
4123               gnus-data ,handle))
4124     (setq e (point))
4125     (widget-convert-button
4126      'link b e
4127      :mime-handle handle
4128      :action 'gnus-widget-press-button
4129      :button-keymap gnus-mime-button-map
4130      :help-echo
4131      (lambda (widget/window &optional overlay pos)
4132        ;; Needed to properly clear the message due to a bug in
4133        ;; wid-edit (XEmacs only).
4134        (if (boundp 'help-echo-owns-message)
4135            (setq help-echo-owns-message t))
4136        (format
4137         "%S: %s the MIME part; %S: more options"
4138         (aref gnus-mouse-2 0)
4139         ;; XEmacs will get a single widget arg; Emacs 21 will get
4140         ;; window, overlay, position.
4141         (if (mm-handle-displayed-p
4142              (if overlay
4143                  (with-current-buffer (gnus-overlay-buffer overlay)
4144                    (widget-get (widget-at (gnus-overlay-start overlay))
4145                                :mime-handle))
4146                (widget-get widget/window :mime-handle)))
4147             "hide" "show")
4148         (aref gnus-down-mouse-3 0))))))
4149
4150 (defun gnus-widget-press-button (elems el)
4151   (goto-char (widget-get elems :from))
4152   (gnus-article-press-button))
4153
4154 (defvar gnus-displaying-mime nil)
4155
4156 (defun gnus-display-mime (&optional ihandles)
4157   "Display the MIME parts."
4158   (save-excursion
4159     (save-selected-window
4160       (let ((window (get-buffer-window gnus-article-buffer))
4161             (point (point)))
4162         (when window
4163           (select-window window)
4164           ;; We have to do this since selecting the window
4165           ;; may change the point.  So we set the window point.
4166           (set-window-point window point)))
4167       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
4168              buffer-read-only handle name type b e display)
4169         (when (and (not ihandles)
4170                    (not gnus-displaying-mime))
4171           ;; Top-level call; we clean up.
4172           (when gnus-article-mime-handles
4173             (mm-destroy-parts gnus-article-mime-handles)
4174             (setq gnus-article-mime-handle-alist nil));; A trick.
4175           (setq gnus-article-mime-handles handles)
4176           ;; We allow users to glean info from the handles.
4177           (when gnus-article-mime-part-function
4178             (gnus-mime-part-function handles)))
4179         (if (and handles
4180                  (or (not (stringp (car handles)))
4181                      (cdr handles)))
4182             (progn
4183               (when (and (not ihandles)
4184                          (not gnus-displaying-mime))
4185                 ;; Clean up for mime parts.
4186                 (article-goto-body)
4187                 (delete-region (point) (point-max)))
4188               (let ((gnus-displaying-mime t))
4189                 (gnus-mime-display-part handles)))
4190           (save-restriction
4191             (article-goto-body)
4192             (narrow-to-region (point) (point-max))
4193             (gnus-treat-article nil 1 1)
4194             (widen)))
4195         (unless ihandles
4196           ;; Highlight the headers.
4197           (save-excursion
4198             (save-restriction
4199               (article-goto-body)
4200               (narrow-to-region (point-min) (point))
4201               (gnus-treat-article 'head))))))))
4202
4203 (defvar gnus-mime-display-multipart-as-mixed nil)
4204 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4205 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4206
4207 (defun gnus-mime-display-part (handle)
4208   (cond
4209    ;; Single part.
4210    ((not (stringp (car handle)))
4211     (gnus-mime-display-single handle))
4212    ;; User-defined multipart
4213    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4214     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4215              handle))
4216    ;; multipart/alternative
4217    ((and (equal (car handle) "multipart/alternative")
4218          (not (or gnus-mime-display-multipart-as-mixed
4219                   gnus-mime-display-multipart-alternative-as-mixed)))
4220     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4221       (push (cons id handle) gnus-article-mime-handle-alist)
4222       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4223    ;; multipart/related
4224    ((and (equal (car handle) "multipart/related")
4225          (not (or gnus-mime-display-multipart-as-mixed
4226                   gnus-mime-display-multipart-related-as-mixed)))
4227     ;;;!!!We should find the start part, but we just default
4228     ;;;!!!to the first part.
4229     ;;(gnus-mime-display-part (cadr handle))
4230     ;;;!!! Most multipart/related is an HTML message plus images.
4231     ;;;!!! Unfortunately we are unable to let W3 display those
4232     ;;;!!! included images, so we just display it as a mixed multipart.
4233     ;;(gnus-mime-display-mixed (cdr handle))
4234     ;;;!!! No, w3 can display everything just fine.
4235     (gnus-mime-display-part (cadr handle)))
4236    ((equal (car handle) "multipart/signed")
4237     (or (memq 'signed gnus-article-wash-types)
4238         (push 'signed gnus-article-wash-types))
4239     (gnus-mime-display-security handle))
4240    ((equal (car handle) "multipart/encrypted")
4241     (or (memq 'encrypted gnus-article-wash-types)
4242         (push 'encrypted gnus-article-wash-types))
4243     (gnus-mime-display-security handle))
4244    ;; Other multiparts are handled like multipart/mixed.
4245    (t
4246     (gnus-mime-display-mixed (cdr handle)))))
4247
4248 (defun gnus-mime-part-function (handles)
4249   (if (stringp (car handles))
4250       (mapcar 'gnus-mime-part-function (cdr handles))
4251     (funcall gnus-article-mime-part-function handles)))
4252
4253 (defun gnus-mime-display-mixed (handles)
4254   (mapcar 'gnus-mime-display-part handles))
4255
4256 (defun gnus-mime-display-single (handle)
4257   (let ((type (mm-handle-media-type handle))
4258         (ignored gnus-ignored-mime-types)
4259         (not-attachment t)
4260         (move nil)
4261         display text)
4262     (catch 'ignored
4263       (progn
4264         (while ignored
4265           (when (string-match (pop ignored) type)
4266             (throw 'ignored nil)))
4267         (if (and (setq not-attachment
4268                        (and (not (mm-inline-override-p handle))
4269                             (or (not (mm-handle-disposition handle))
4270                                 (equal (car (mm-handle-disposition handle))
4271                                        "inline")
4272                                 (mm-attachment-override-p handle))))
4273                  (mm-automatic-display-p handle)
4274                  (or (and
4275                       (mm-inlinable-p handle)
4276                       (mm-inlined-p handle))
4277                      (mm-automatic-external-display-p type)))
4278             (setq display t)
4279           (when (equal (mm-handle-media-supertype handle) "text")
4280             (setq text t)))
4281         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4282               beg)
4283           (push (cons id handle) gnus-article-mime-handle-alist)
4284           (when (or (not display)
4285                     (not (gnus-unbuttonized-mime-type-p type)))
4286             ;(gnus-article-insert-newline)
4287             (gnus-insert-mime-button
4288              handle id (list (or display (and not-attachment text))))
4289             (gnus-article-insert-newline)
4290             ;(gnus-article-insert-newline)
4291             ;; Remember modify the number of forward lines.
4292             (setq move t))
4293           (setq beg (point))
4294           (cond
4295            (display
4296             (when move
4297               (forward-line -1)
4298               (setq beg (point)))
4299             (let ((mail-parse-charset gnus-newsgroup-charset)
4300                   (mail-parse-ignored-charsets
4301                    (save-excursion (condition-case ()
4302                                        (set-buffer gnus-summary-buffer)
4303                                      (error))
4304                                    gnus-newsgroup-ignored-charsets)))
4305               (mm-display-part handle t))
4306             (goto-char (point-max)))
4307            ((and text not-attachment)
4308             (when move
4309               (forward-line -1)
4310               (setq beg (point)))
4311             (gnus-article-insert-newline)
4312             (mm-insert-inline handle (mm-get-part handle))
4313             (goto-char (point-max))))
4314           ;; Do highlighting.
4315           (save-excursion
4316             (save-restriction
4317               (narrow-to-region beg (point))
4318               (gnus-treat-article
4319                nil id
4320                (gnus-article-mime-total-parts)
4321                (mm-handle-media-type handle)))))))))
4322
4323 (defun gnus-unbuttonized-mime-type-p (type)
4324   "Say whether TYPE is to be unbuttonized."
4325   (unless gnus-inhibit-mime-unbuttonizing
4326     (when (catch 'found
4327             (let ((types gnus-unbuttonized-mime-types))
4328               (while types
4329                 (when (string-match (pop types) type)
4330                   (throw 'found t)))))
4331       (not (catch 'found
4332              (let ((types gnus-buttonized-mime-types))
4333                (while types
4334                  (when (string-match (pop types) type)
4335                    (throw 'found t)))))))))
4336
4337 (defun gnus-article-insert-newline ()
4338   "Insert a newline, but mark it as undeletable."
4339   (gnus-put-text-property
4340    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4341
4342 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4343   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4344          (ihandles handles)
4345          (point (point))
4346          handle buffer-read-only from props begend not-pref)
4347     (save-window-excursion
4348       (save-restriction
4349         (when ibegend
4350           (narrow-to-region (car ibegend)
4351                             (or (cdr ibegend)
4352                                 (progn
4353                                   (goto-char (car ibegend))
4354                                   (forward-line 2)
4355                                   (point))))
4356           (delete-region (point-min) (point-max))
4357           (mm-remove-parts handles))
4358         (setq begend (list (point-marker)))
4359         ;; Do the toggle.
4360         (unless (setq not-pref (cadr (member preferred ihandles)))
4361           (setq not-pref (car ihandles)))
4362         (when (or ibegend
4363                   (not preferred)
4364                   (not (gnus-unbuttonized-mime-type-p
4365                         "multipart/alternative")))
4366           (gnus-add-text-properties
4367            (setq from (point))
4368            (progn
4369              (insert (format "%d.  " id))
4370              (point))
4371            `(gnus-callback
4372              (lambda (handles)
4373                (unless ,(not ibegend)
4374                  (setq gnus-article-mime-handle-alist
4375                        ',gnus-article-mime-handle-alist))
4376                (gnus-mime-display-alternative
4377                 ',ihandles ',not-pref ',begend ,id))
4378              ,@(if (>= (string-to-number emacs-version) 21)
4379                    nil ;; XEmacs doesn't care
4380                  (list 'local-map gnus-mime-button-map))
4381              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4382              face ,gnus-article-button-face
4383              keymap ,gnus-mime-button-map
4384              gnus-part ,id
4385              gnus-data ,handle))
4386           (widget-convert-button 'link from (point)
4387                                  :action 'gnus-widget-press-button
4388                                  :button-keymap gnus-widget-button-keymap)
4389           ;; Do the handles
4390           (while (setq handle (pop handles))
4391             (gnus-add-text-properties
4392              (setq from (point))
4393              (progn
4394                (insert (format "(%c) %-18s"
4395                                (if (equal handle preferred) ?* ? )
4396                                (mm-handle-media-type handle)))
4397                (point))
4398              `(gnus-callback
4399                (lambda (handles)
4400                  (unless ,(not ibegend)
4401                    (setq gnus-article-mime-handle-alist
4402                          ',gnus-article-mime-handle-alist))
4403                  (gnus-mime-display-alternative
4404                   ',ihandles ',handle ',begend ,id))
4405                ,@(if (>= (string-to-number emacs-version) 21)
4406                      nil ;; XEmacs doesn't care
4407                    (list 'local-map gnus-mime-button-map))
4408                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4409                face ,gnus-article-button-face
4410                keymap ,gnus-mime-button-map
4411                gnus-part ,id
4412                gnus-data ,handle))
4413             (widget-convert-button 'link from (point)
4414                                    :action 'gnus-widget-press-button
4415                                    :button-keymap gnus-widget-button-keymap)
4416             (insert "  "))
4417           (insert "\n\n"))
4418         (when preferred
4419           (if (stringp (car preferred))
4420               (gnus-display-mime preferred)
4421             (let ((mail-parse-charset gnus-newsgroup-charset)
4422                   (mail-parse-ignored-charsets
4423                    (save-excursion (set-buffer gnus-summary-buffer)
4424                                    gnus-newsgroup-ignored-charsets)))
4425               (mm-display-part preferred)
4426               ;; Do highlighting.
4427               (save-excursion
4428                 (save-restriction
4429                   (narrow-to-region (car begend) (point-max))
4430                   (gnus-treat-article
4431                    nil (length gnus-article-mime-handle-alist)
4432                    (gnus-article-mime-total-parts)
4433                    (mm-handle-media-type handle))))))
4434           (goto-char (point-max))
4435           (setcdr begend (point-marker)))))
4436     (when ibegend
4437       (goto-char point))))
4438
4439 (defun gnus-article-wash-status ()
4440   "Return a string which display status of article washing."
4441   (save-excursion
4442     (set-buffer gnus-article-buffer)
4443     (let ((cite (memq 'cite gnus-article-wash-types))
4444           (headers (memq 'headers gnus-article-wash-types))
4445           (boring (memq 'boring-headers gnus-article-wash-types))
4446           (pgp (memq 'pgp gnus-article-wash-types))
4447           (pem (memq 'pem gnus-article-wash-types))
4448           (signed (memq 'signed gnus-article-wash-types))
4449           (encrypted (memq 'encrypted gnus-article-wash-types))
4450           (signature (memq 'signature gnus-article-wash-types))
4451           (overstrike (memq 'overstrike gnus-article-wash-types))
4452           (emphasis (memq 'emphasis gnus-article-wash-types)))
4453       (format "%c%c%c%c%c%c%c"
4454               (if cite ?c ? )
4455               (if (or headers boring) ?h ? )
4456               (if (or pgp pem signed encrypted) ?p ? )
4457               (if signature ?s ? )
4458               (if overstrike ?o ? )
4459               (if gnus-show-mime ?m ? )
4460               (if emphasis ?e ? )))))
4461
4462 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4463
4464 (defun gnus-article-maybe-hide-headers ()
4465   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4466 Provided for backwards compatibility."
4467   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4468                  (not (save-excursion (set-buffer gnus-summary-buffer)
4469                                       gnus-have-all-headers)))
4470              (not gnus-inhibit-hiding))
4471     (gnus-article-hide-headers)))
4472
4473 ;;; Article savers.
4474
4475 (defun gnus-output-to-file (file-name)
4476   "Append the current article to a file named FILE-NAME."
4477   (let ((artbuf (current-buffer)))
4478     (with-temp-buffer
4479       (insert-buffer-substring artbuf)
4480       ;; Append newline at end of the buffer as separator, and then
4481       ;; save it to file.
4482       (goto-char (point-max))
4483       (insert "\n")
4484       (let ((file-name-coding-system nnmail-pathname-coding-system)
4485             (pathname-coding-system nnmail-pathname-coding-system))
4486         (write-region-as-binary (point-min) (point-max) file-name 'append))
4487       t)))
4488
4489 (defun gnus-narrow-to-page (&optional arg)
4490   "Narrow the article buffer to a page.
4491 If given a numerical ARG, move forward ARG pages."
4492   (interactive "P")
4493   (setq arg (if arg (prefix-numeric-value arg) 0))
4494   (save-excursion
4495     (set-buffer gnus-article-buffer)
4496     (goto-char (point-min))
4497     (widen)
4498     ;; Remove any old next/prev buttons.
4499     (when (gnus-visual-p 'page-marker)
4500       (let ((buffer-read-only nil))
4501         (gnus-remove-text-with-property 'gnus-prev)
4502         (gnus-remove-text-with-property 'gnus-next)))
4503     (when
4504         (cond ((< arg 0)
4505                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4506               ((> arg 0)
4507                (re-search-forward page-delimiter nil 'move arg)))
4508       (goto-char (match-end 0)))
4509     (narrow-to-region
4510      (point)
4511      (if (re-search-forward page-delimiter nil 'move)
4512          (match-beginning 0)
4513        (point)))
4514     (when (and (gnus-visual-p 'page-marker)
4515                (not (= (point-min) 1)))
4516       (save-excursion
4517         (goto-char (point-min))
4518         (gnus-insert-prev-page-button)))
4519     (when (and (gnus-visual-p 'page-marker)
4520                (< (+ (point-max) 2) (buffer-size)))
4521       (save-excursion
4522         (goto-char (point-max))
4523         (gnus-insert-next-page-button)))))
4524
4525 ;; Article mode commands
4526
4527 (defun gnus-article-goto-next-page ()
4528   "Show the next page of the article."
4529   (interactive)
4530   (when (gnus-article-next-page)
4531     (goto-char (point-min))
4532     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4533
4534 (defun gnus-article-goto-prev-page ()
4535   "Show the next page of the article."
4536   (interactive)
4537   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4538     (gnus-article-prev-page nil)))
4539
4540 (defun gnus-article-next-page (&optional lines)
4541   "Show the next page of the current article.
4542 If end of article, return non-nil.  Otherwise return nil.
4543 Argument LINES specifies lines to be scrolled up."
4544   (interactive "p")
4545   (let ((start (window-start))
4546         end-of-buffer end-of-page)
4547     (save-excursion
4548       (move-to-window-line -1)
4549       (if (<= (point) start)
4550           (progn
4551             (forward-line 2)
4552             (setq start (point)))
4553         (forward-line 1)
4554         (setq start nil))
4555       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4556                          (and (pos-visible-in-window-p)
4557                               (setq end-of-buffer t)))
4558                         ((eobp)
4559                          (setq end-of-page t)))
4560                   (not lines))
4561         (move-to-window-line lines)
4562         (unless (search-backward "\n\n" nil t)
4563           (setq start (point)))))
4564     (cond (end-of-buffer t)
4565           (end-of-page
4566            (gnus-narrow-to-page 1)
4567            nil)
4568           (t
4569            (if start
4570                (set-window-start (selected-window) start)
4571              (let (window-pixel-scroll-increment)
4572                (scroll-up lines)))
4573            nil))))
4574
4575 (defun gnus-article-prev-page (&optional lines)
4576   "Show previous page of current article.
4577 Argument LINES specifies lines to be scrolled down."
4578   (interactive "p")
4579   (let (beginning-of-buffer beginning-of-page)
4580     (save-excursion
4581       (move-to-window-line 0)
4582       (cond ((eq 1 (point))
4583              (setq beginning-of-buffer t))
4584             ((bobp)
4585              (setq beginning-of-page t))))
4586     (cond (beginning-of-buffer)
4587           (beginning-of-page
4588            (gnus-narrow-to-page -1))
4589           (t
4590            (condition-case nil
4591                (let (window-pixel-scroll-increment)
4592                  (scroll-down lines))
4593              (beginning-of-buffer
4594               (goto-char (point-min))))))))
4595
4596 (defun gnus-article-refer-article ()
4597   "Read article specified by message-id around point."
4598   (interactive)
4599   (let ((point (point)))
4600     (search-forward ">" nil t)          ;Move point to end of "<....>".
4601     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4602         (let ((message-id (match-string 1)))
4603           (goto-char point)
4604           (set-buffer gnus-summary-buffer)
4605           (gnus-summary-refer-article message-id))
4606       (goto-char (point))
4607       (error "No references around point"))))
4608
4609 (defun gnus-article-show-summary ()
4610   "Reconfigure windows to show summary buffer."
4611   (interactive)
4612   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4613       (error "There is no summary buffer for this article buffer")
4614     (gnus-article-set-globals)
4615     (gnus-configure-windows 'article)
4616     (gnus-summary-goto-subject gnus-current-article)
4617     (gnus-summary-position-point)))
4618
4619 (defun gnus-article-describe-briefly ()
4620   "Describe article mode commands briefly."
4621   (interactive)
4622   (gnus-message 6 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page   \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
4623
4624 (defun gnus-article-summary-command ()
4625   "Execute the last keystroke in the summary buffer."
4626   (interactive)
4627   (let ((obuf (current-buffer))
4628         (owin (current-window-configuration))
4629         func)
4630     (switch-to-buffer gnus-article-current-summary 'norecord)
4631     (setq func (lookup-key (current-local-map) (this-command-keys)))
4632     (call-interactively func)
4633     (set-buffer obuf)
4634     (set-window-configuration owin)
4635     (set-window-point (get-buffer-window (current-buffer)) (point))))
4636
4637 (defun gnus-article-summary-command-nosave ()
4638   "Execute the last keystroke in the summary buffer."
4639   (interactive)
4640   (let (func)
4641     (pop-to-buffer gnus-article-current-summary 'norecord)
4642     (setq func (lookup-key (current-local-map) (this-command-keys)))
4643     (call-interactively func)))
4644
4645 (defun gnus-article-check-buffer ()
4646   "Beep if not in an article buffer."
4647   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4648     (error "Command invoked outside of a Gnus article buffer")))
4649
4650 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4651   "Read a summary buffer key sequence and execute it from the article buffer."
4652   (interactive "P")
4653   (gnus-article-check-buffer)
4654   (let ((nosaves
4655          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4656            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4657            "=" "^" "\M-^" "|"))
4658         (nosave-but-article
4659          '("A\r"))
4660         (nosave-in-article
4661          '("\C-d"))
4662         (up-to-top
4663          '("n" "Gn" "p" "Gp"))
4664         keys new-sum-point)
4665     (save-excursion
4666       (set-buffer gnus-article-current-summary)
4667       (let (gnus-pick-mode)
4668         (push (or key last-command-event) unread-command-events)
4669         (setq keys (static-if (featurep 'xemacs)
4670                        (events-to-keys (read-key-sequence nil))
4671                      (read-key-sequence nil)))))
4672     (message "")
4673
4674     (if (or (member keys nosaves)
4675             (member keys nosave-but-article)
4676             (member keys nosave-in-article))
4677         (let (func)
4678           (save-window-excursion
4679             (pop-to-buffer gnus-article-current-summary 'norecord)
4680             ;; We disable the pick minor mode commands.
4681             (let (gnus-pick-mode)
4682               (setq func (lookup-key (current-local-map) keys))))
4683           (if (or (not func)
4684                   (numberp func))
4685               (ding)
4686             (unless (member keys nosave-in-article)
4687               (set-buffer gnus-article-current-summary))
4688             (call-interactively func)
4689             (setq new-sum-point (point)))
4690           (when (member keys nosave-but-article)
4691             (pop-to-buffer gnus-article-buffer 'norecord)))
4692       ;; These commands should restore window configuration.
4693       (let ((obuf (current-buffer))
4694             (owin (current-window-configuration))
4695             (opoint (point))
4696             (summary gnus-article-current-summary)
4697             func in-buffer selected)
4698         (if not-restore-window
4699             (pop-to-buffer summary 'norecord)
4700           (switch-to-buffer summary 'norecord))
4701         (setq in-buffer (current-buffer))
4702         ;; We disable the pick minor mode commands.
4703         (if (and (setq func (let (gnus-pick-mode)
4704                               (lookup-key (current-local-map) keys)))
4705                  (functionp func))
4706             (progn
4707               (call-interactively func)
4708               (setq new-sum-point (point))
4709               (when (eq in-buffer (current-buffer))
4710                 (setq selected (gnus-summary-select-article))
4711                 (set-buffer obuf)
4712                 (unless not-restore-window
4713                   (set-window-configuration owin))
4714                 (when (eq selected 'old)
4715                   (article-goto-body)
4716                   (set-window-start (get-buffer-window (current-buffer))
4717                                     1)
4718                   (set-window-point (get-buffer-window (current-buffer))
4719                                     (point)))
4720                 (let ((win (get-buffer-window gnus-article-current-summary)))
4721                   (when win
4722                     (set-window-point win new-sum-point))))    )
4723           (switch-to-buffer gnus-article-buffer)
4724           (ding))))))
4725
4726 (defun gnus-article-describe-key (key)
4727   "Display documentation of the function invoked by KEY.  KEY is a string."
4728   (interactive "kDescribe key: ")
4729   (gnus-article-check-buffer)
4730   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4731       (save-excursion
4732         (set-buffer gnus-article-current-summary)
4733         (let (gnus-pick-mode)
4734           (if (featurep 'xemacs)
4735               (progn
4736                 (push (elt key 0) unread-command-events)
4737                 (setq key (events-to-keys
4738                            (read-key-sequence "Describe key: "))))
4739             (setq unread-command-events
4740                   (mapcar
4741                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4742                    key))
4743             (setq key (read-key-sequence "Describe key: "))))
4744         (describe-key key))
4745     (describe-key key)))
4746
4747 (defun gnus-article-describe-key-briefly (key &optional insert)
4748   "Display documentation of the function invoked by KEY.  KEY is a string."
4749   (interactive "kDescribe key: \nP")
4750   (gnus-article-check-buffer)
4751   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4752       (save-excursion
4753         (set-buffer gnus-article-current-summary)
4754         (let (gnus-pick-mode)
4755           (if (featurep 'xemacs)
4756               (progn
4757                 (push (elt key 0) unread-command-events)
4758                 (setq key (events-to-keys
4759                            (read-key-sequence "Describe key: "))))
4760             (setq unread-command-events
4761                   (mapcar
4762                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4763                    key))
4764             (setq key (read-key-sequence "Describe key: "))))
4765         (describe-key-briefly key insert))
4766     (describe-key-briefly key insert)))
4767
4768 (defun gnus-article-hide (&optional arg force)
4769   "Hide all the gruft in the current article.
4770 This means that PGP stuff, signatures, cited text and (some)
4771 headers will be hidden.
4772 If given a prefix, show the hidden text instead."
4773   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4774   (gnus-article-hide-headers arg)
4775   (gnus-article-hide-list-identifiers arg)
4776   (gnus-article-hide-pgp arg)
4777   (gnus-article-hide-citation-maybe arg force)
4778   (gnus-article-hide-signature arg))
4779
4780 (defun gnus-article-maybe-highlight ()
4781   "Do some article highlighting if article highlighting is requested."
4782   (when (gnus-visual-p 'article-highlight 'highlight)
4783     (gnus-article-highlight-some)))
4784
4785 (defun gnus-check-group-server ()
4786   ;; Make sure the connection to the server is alive.
4787   (unless (gnus-server-opened
4788            (gnus-find-method-for-group gnus-newsgroup-name))
4789     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4790     (gnus-request-group gnus-newsgroup-name t)))
4791
4792 (defun gnus-request-article-this-buffer (article group)
4793   "Get an article and insert it into this buffer.
4794 T-gnus change: Insert an article into `gnus-original-article-buffer'."
4795   (let (do-update-line sparse-header)
4796     ;; The current buffer is `gnus-article-buffer'.
4797     (prog1
4798         (save-excursion
4799           (erase-buffer)
4800           (gnus-kill-all-overlays)
4801           (setq group (or group gnus-newsgroup-name))
4802
4803           ;; Using `gnus-request-article' directly will insert the article into
4804           ;; `nntp-server-buffer' - so we'll save some time by not having to
4805           ;; copy it from the server buffer into the article buffer.
4806
4807           ;; We only request an article by message-id when we do not have the
4808           ;; headers for it, so we'll have to get those.
4809           (when (stringp article)
4810             (gnus-read-header article))
4811
4812           ;; If the article number is negative, that means that this article
4813           ;; doesn't belong in this newsgroup (possibly), so we find its
4814           ;; message-id and request it by id instead of number.
4815           (when (and (numberp article)
4816                      gnus-summary-buffer
4817                      (get-buffer gnus-summary-buffer)
4818                      (gnus-buffer-exists-p gnus-summary-buffer))
4819             (save-excursion
4820               (set-buffer gnus-summary-buffer)
4821               (let ((header (gnus-summary-article-header article)))
4822                 (when (< article 0)
4823                   (cond
4824                    ((memq article gnus-newsgroup-sparse)
4825                     ;; This is a sparse gap article.
4826                     (setq do-update-line article)
4827                     (setq article (mail-header-id header))
4828                     (setq sparse-header (gnus-read-header article))
4829                     (setq gnus-newsgroup-sparse
4830                           (delq article gnus-newsgroup-sparse)))
4831                    ((vectorp header)
4832                     ;; It's a real article.
4833                     (setq article (mail-header-id header)))
4834                    (t
4835                     ;; It is an extracted pseudo-article.
4836                     (setq article 'pseudo)
4837                     (gnus-request-pseudo-article header))))
4838
4839                 (let ((method (gnus-find-method-for-group
4840                                gnus-newsgroup-name)))
4841                   (when (and (eq (car method) 'nneething)
4842                              (vectorp header))
4843                     (let ((dir (expand-file-name
4844                                 (mail-header-subject header)
4845                                 (file-name-as-directory
4846                                  (or (cadr (assq 'nneething-address method))
4847                                      (nth 1 method))))))
4848                       (when (file-directory-p dir)
4849                         (setq article 'nneething)
4850                         (gnus-group-enter-directory dir))))))))
4851           (setq gnus-original-article (cons group article))
4852
4853           ;; The current buffer is `gnus-original-article-buffer'. 
4854           (if (get-buffer gnus-original-article-buffer)
4855               (set-buffer gnus-original-article-buffer)
4856             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4857             (set-buffer-multibyte nil)
4858             (buffer-disable-undo)
4859             (setq major-mode 'gnus-original-article-mode)
4860             (setq buffer-read-only nil))
4861
4862           (cond
4863            ;; Refuse to select canceled articles.
4864            ((and (numberp article)
4865                  gnus-summary-buffer
4866                  (get-buffer gnus-summary-buffer)
4867                  (gnus-buffer-exists-p gnus-summary-buffer)
4868                  (eq (cdr (save-excursion
4869                             (set-buffer gnus-summary-buffer)
4870                             (assq article gnus-newsgroup-reads)))
4871                      gnus-canceled-mark))
4872             nil)
4873            ;; Check the backlog.
4874            ((and gnus-keep-backlog
4875                  (gnus-backlog-request-article group article (current-buffer)))
4876             'article)
4877            ;; Check asynchronous pre-fetch.
4878            ((gnus-async-request-fetched-article group article (current-buffer))
4879             (gnus-async-prefetch-next group article gnus-summary-buffer)
4880             (when (and (numberp article) gnus-keep-backlog)
4881               (gnus-backlog-enter-article group article (current-buffer)))
4882             'article)
4883            ;; Check the cache.
4884            ((and gnus-use-cache
4885                  (numberp article)
4886                  (gnus-cache-request-article article group))
4887             'article)
4888            ;; Get the article and put into the article buffer.
4889            ((or (stringp article)
4890                 (numberp article))
4891             (let ((gnus-override-method gnus-override-method)
4892                   (methods (and (stringp article)
4893                                 gnus-refer-article-method))
4894                   result
4895                   (buffer-read-only nil))
4896               (if (or (not (listp methods))
4897                       (and (symbolp (car methods))
4898                            (assq (car methods) nnoo-definition-alist)))
4899                   (setq methods (list methods)))
4900               (when (and (null gnus-override-method)
4901                          methods)
4902                 (setq gnus-override-method (pop methods)))
4903               (while (not result)
4904                 (when (eq gnus-override-method 'current)
4905                   (setq gnus-override-method
4906                         (with-current-buffer gnus-summary-buffer
4907                           gnus-current-select-method)))
4908                 (erase-buffer)
4909                 (gnus-kill-all-overlays)
4910                 (let ((gnus-newsgroup-name group))
4911                   (gnus-check-group-server))
4912                 (when (gnus-request-article article group (current-buffer))
4913                   (when (numberp article)
4914                     (gnus-async-prefetch-next group article
4915                                               gnus-summary-buffer)
4916                     (when gnus-keep-backlog
4917                       (gnus-backlog-enter-article
4918                        group article (current-buffer))))
4919                   (setq result 'article))
4920                 (if (not result)
4921                     (if methods
4922                         (setq gnus-override-method (pop methods))
4923                       (setq result 'done))))
4924               (and (eq result 'article) 'article)))
4925            ;; It was a pseudo.
4926            (t article)))
4927
4928       ;; Associate this article with the current summary buffer.
4929       (setq gnus-article-current-summary gnus-summary-buffer)
4930
4931       ;; T-gnus change: Copy the requested article from
4932       ;; `gnus-original-article-buffer' to the current buffer when
4933       ;; this function is called.
4934       (insert-buffer gnus-original-article-buffer)
4935
4936       ;; Decode charsets.
4937       (run-hooks 'gnus-article-decode-hook)
4938       ;; Mark article as decoded or not.
4939       (setq gnus-article-decoded-p gnus-article-decode-hook)
4940
4941       ;; Update sparse articles.
4942       (when (and do-update-line
4943                  (or (numberp article)
4944                      (stringp article)))
4945         (let ((buf (current-buffer)))
4946           (set-buffer gnus-summary-buffer)
4947           (gnus-summary-update-article do-update-line sparse-header)
4948           (gnus-summary-goto-subject do-update-line nil t)
4949           (set-window-point (get-buffer-window (current-buffer) t)
4950                             (point))
4951           (set-buffer buf))))))
4952
4953 ;;;
4954 ;;; Article editing
4955 ;;;
4956
4957 (defcustom gnus-article-edit-mode-hook nil
4958   "Hook run in article edit mode buffers."
4959   :group 'gnus-article-various
4960   :type 'hook)
4961
4962 (defcustom gnus-article-edit-article-setup-function
4963   'gnus-article-mime-edit-article-setup
4964   "Function called to setup an editing article buffer."
4965   :group 'gnus-article-various
4966   :type 'function)
4967
4968 (defvar gnus-article-edit-done-function nil)
4969
4970 (defvar gnus-article-edit-mode-map nil)
4971
4972 ;; Should we be using derived.el for this?
4973 (unless gnus-article-edit-mode-map
4974   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4975   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4976
4977   (gnus-define-keys gnus-article-edit-mode-map
4978     "\C-c\C-c" gnus-article-edit-done
4979     "\C-c\C-k" gnus-article-edit-exit)
4980
4981   (gnus-define-keys (gnus-article-edit-wash-map
4982                      "\C-c\C-w" gnus-article-edit-mode-map)
4983     "f" gnus-article-edit-full-stops))
4984
4985 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
4986   "Major mode for editing articles.
4987 This is an extended text-mode.
4988
4989 \\{gnus-article-edit-mode-map}"
4990   (make-local-variable 'gnus-article-edit-done-function)
4991   (make-local-variable 'gnus-prev-winconf)
4992   (set (make-local-variable 'font-lock-defaults)
4993        '(message-font-lock-keywords t))
4994   (setq buffer-read-only nil)
4995   (buffer-enable-undo)
4996   (widen))
4997
4998 (defun gnus-article-edit (&optional force)
4999   "Edit the current article.
5000 This will have permanent effect only in mail groups.
5001 If FORCE is non-nil, allow editing of articles even in read-only
5002 groups."
5003   (interactive "P")
5004   (when (and (not force)
5005              (gnus-group-read-only-p))
5006     (error "The current newsgroup does not support article editing"))
5007   (gnus-article-date-original)
5008   (gnus-article-edit-article
5009    'ignore
5010    `(lambda (no-highlight)
5011       'ignore
5012       (gnus-summary-edit-article-done
5013        ,(or (mail-header-references gnus-current-headers) "")
5014        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5015
5016 (defun gnus-article-edit-article (start-func exit-func)
5017   "Start editing the contents of the current article buffer."
5018   (let ((winconf (current-window-configuration)))
5019     (set-buffer gnus-article-buffer)
5020     (gnus-article-edit-mode)
5021     (funcall start-func)
5022     (set-buffer-modified-p nil)
5023     (gnus-configure-windows 'edit-article)
5024     (setq gnus-article-edit-done-function exit-func)
5025     (setq gnus-prev-winconf winconf)
5026     (when gnus-article-edit-article-setup-function
5027       (funcall gnus-article-edit-article-setup-function))
5028     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5029
5030 (defun gnus-article-edit-done (&optional arg)
5031   "Update the article edits and exit."
5032   (interactive "P")
5033   (let ((func gnus-article-edit-done-function)
5034         (buf (current-buffer))
5035         (start (window-start)))
5036     (remove-hook 'gnus-article-mode-hook
5037                  'gnus-article-mime-edit-article-unwind)
5038     ;; We remove all text props from the article buffer.
5039     (let ((content
5040            (buffer-substring-no-properties (point-min) (point-max)))
5041           (p (point)))
5042       (erase-buffer)
5043       (insert content)
5044       (let ((winconf gnus-prev-winconf))
5045         (gnus-article-mode)
5046         (set-window-configuration winconf)
5047         ;; Tippy-toe some to make sure that point remains where it was.
5048         (save-current-buffer
5049           (set-buffer buf)
5050           (set-window-start (get-buffer-window (current-buffer)) start)
5051           (goto-char p))))
5052     (save-excursion
5053       (set-buffer buf)
5054       (let ((buffer-read-only nil))
5055         (funcall func arg))
5056       ;; The cache and backlog have to be flushed somewhat.
5057       (when gnus-keep-backlog
5058         (gnus-backlog-remove-article
5059          (car gnus-article-current) (cdr gnus-article-current)))
5060       ;; Flush original article as well.
5061       (save-excursion
5062         (when (get-buffer gnus-original-article-buffer)
5063           (set-buffer gnus-original-article-buffer)
5064           (setq gnus-original-article nil)))
5065       (when gnus-use-cache
5066         (gnus-cache-update-article
5067          (car gnus-article-current) (cdr gnus-article-current))))
5068     (set-buffer buf)
5069     (set-window-start (get-buffer-window buf) start)
5070     (set-window-point (get-buffer-window buf) (point))))
5071
5072 (defun gnus-article-edit-exit ()
5073   "Exit the article editing without updating."
5074   (interactive)
5075   (when (or (not (buffer-modified-p))
5076             (yes-or-no-p "Article modified; kill anyway? "))
5077     (let ((curbuf (current-buffer))
5078           (p (point))
5079           (window-start (window-start)))
5080       (erase-buffer)
5081       (if (gnus-buffer-live-p gnus-original-article-buffer)
5082           (insert-buffer gnus-original-article-buffer))
5083       (let ((winconf gnus-prev-winconf))
5084         (gnus-article-mode)
5085         (set-window-configuration winconf)
5086         ;; Tippy-toe some to make sure that point remains where it was.
5087         (save-current-buffer
5088           (set-buffer curbuf)
5089           (set-window-start (get-buffer-window (current-buffer)) window-start)
5090           (goto-char p))))))
5091
5092 (defun gnus-article-edit-full-stops ()
5093   "Interactively repair spacing at end of sentences."
5094   (interactive)
5095   (save-excursion
5096     (goto-char (point-min))
5097     (search-forward-regexp "^$" nil t)
5098     (let ((case-fold-search nil))
5099       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5100
5101 ;;;
5102 ;;; Article editing with MIME-Edit
5103 ;;;
5104
5105 (defcustom gnus-article-mime-edit-article-setup-hook nil
5106   "Hook run after setting up a MIME editing article buffer."
5107   :group 'gnus-article-various
5108   :type 'hook)
5109
5110 (defun gnus-article-mime-edit-article-unwind ()
5111   "Unwind `gnus-article-buffer' if article editing was given up."
5112   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5113   (when (featurep 'font-lock)
5114     (setq font-lock-defaults nil)
5115     (font-lock-mode -1))
5116   (when mime-edit-mode-flag
5117     (mime-edit-exit 'nomime 'no-error)
5118     (message "")))
5119
5120 (defun gnus-article-mime-edit-article-setup ()
5121   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5122 after replacing with the original article."
5123   (setq gnus-show-mime t)
5124   (setq gnus-article-edit-done-function
5125         `(lambda (&rest args)
5126            (when mime-edit-mode-flag
5127              (let (mime-edit-insert-user-agent-field)
5128                (mime-edit-exit))
5129              (message ""))
5130            (goto-char (point-min))
5131            (let (case-fold-search)
5132              (when (re-search-forward
5133                     (format "^%s$" (regexp-quote mail-header-separator))
5134                     nil t)
5135                (replace-match "")))
5136            (apply ,gnus-article-edit-done-function args)
5137            (insert
5138             (prog1
5139                 (buffer-substring-no-properties (point-min) (point-max))
5140               (set-buffer (get-buffer-create gnus-original-article-buffer))
5141               (erase-buffer)))
5142            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5143            (set-buffer gnus-article-buffer)
5144            (gnus-article-prepare-display)))
5145   (substitute-key-definition 'gnus-article-edit-done
5146                              'gnus-article-mime-edit-done
5147                              gnus-article-edit-mode-map)
5148   (substitute-key-definition 'gnus-article-edit-exit
5149                              'gnus-article-mime-edit-exit
5150                              gnus-article-edit-mode-map)
5151   (erase-buffer)
5152   (insert-buffer gnus-original-article-buffer)
5153   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5154     (fset 'mime-edit-decode-single-part-in-buffer
5155           (lambda (&rest args)
5156             (if (let ((content-type (car args)))
5157                   (and (eq 'message (mime-content-type-primary-type
5158                                      content-type))
5159                        (eq 'rfc822 (mime-content-type-subtype content-type))))
5160                 (setcar (cdr args) 'not-decode-text))
5161             (apply ofn args)))
5162     (unwind-protect
5163         (mime-edit-again)
5164       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
5165   (when (featurep 'font-lock)
5166     (set (make-local-variable 'font-lock-defaults)
5167          '(message-font-lock-keywords t))
5168     (font-lock-set-defaults)
5169     (turn-on-font-lock))
5170   (set-buffer-modified-p nil)
5171   (delete-other-windows)
5172   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5173   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5174
5175 (defun gnus-article-mime-edit-done (&optional arg)
5176   "Update the article MIME edits and exit."
5177   (interactive "P")
5178   (when (featurep 'font-lock)
5179     (setq font-lock-defaults nil)
5180     (font-lock-mode -1))
5181   (gnus-article-edit-done arg))
5182
5183 (defun gnus-article-mime-edit-exit ()
5184   "Exit the article MIME editing without updating."
5185   (interactive)
5186   (when (or (not (buffer-modified-p))
5187             (yes-or-no-p "Article modified; kill anyway? "))
5188     (when (featurep 'font-lock)
5189       (setq font-lock-defaults nil)
5190       (font-lock-mode -1))
5191     (when mime-edit-mode-flag
5192       (let (mime-edit-insert-user-agent-field)
5193         (mime-edit-exit))
5194       (message ""))
5195     (goto-char (point-min))
5196     (let (case-fold-search)
5197       (when (re-search-forward
5198              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5199         (replace-match "")))
5200     (let ((winconf gnus-prev-winconf))
5201       (insert (prog1
5202                   (buffer-substring-no-properties (point-min) (point-max))
5203                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5204                 (erase-buffer)))
5205       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5206       (set-buffer gnus-article-buffer)
5207       (gnus-article-prepare-display)
5208       (set-window-configuration winconf))))
5209
5210 ;;;
5211 ;;; Article highlights
5212 ;;;
5213
5214 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5215
5216 ;;; Internal Variables:
5217
5218 (defcustom gnus-button-url-regexp "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)\\)"
5219   "Regular expression that matches URLs."
5220   :group 'gnus-article-buttons
5221   :type 'regexp)
5222
5223 (defcustom gnus-button-alist
5224   `(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5225      0 t gnus-button-handle-news 3)
5226     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
5227      gnus-button-handle-news 2)
5228     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5229      1 t
5230      gnus-button-fetch-group 4)
5231     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5232     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5233      t gnus-button-message-id 3)
5234     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5235     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5236     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5237     ;; This is info
5238     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t
5239      gnus-button-handle-info 2)
5240     ;; This is how URLs _should_ be embedded in text...
5241     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
5242     ;; Raw URLs.
5243     (,gnus-button-url-regexp 0 t browse-url 0))
5244   "*Alist of regexps matching buttons in article bodies.
5245
5246 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5247 REGEXP: is the string matching text around the button,
5248 BUTTON: is the number of the regexp grouping actually matching the button,
5249 FORM: is a lisp expression which must eval to true for the button to
5250 be added,
5251 CALLBACK: is the function to call when the user push this button, and each
5252 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5253
5254 CALLBACK can also be a variable, in that case the value of that
5255 variable it the real callback function."
5256   :group 'gnus-article-buttons
5257   :type '(repeat (list regexp
5258                        (integer :tag "Button")
5259                        (sexp :tag "Form")
5260                        (function :tag "Callback")
5261                        (repeat :tag "Par"
5262                                :inline t
5263                                (integer :tag "Regexp group")))))
5264
5265 (defcustom gnus-header-button-alist
5266   `(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
5267      0 t gnus-button-message-id 0)
5268     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5269     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5270      0 t gnus-button-mailto 0)
5271     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5272     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5273     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5274     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5275      gnus-button-message-id 3))
5276   "*Alist of headers and regexps to match buttons in article heads.
5277
5278 This alist is very similar to `gnus-button-alist', except that each
5279 alist has an additional HEADER element first in each entry:
5280
5281 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5282
5283 HEADER is a regexp to match a header.  For a fuller explanation, see
5284 `gnus-button-alist'."
5285   :group 'gnus-article-buttons
5286   :group 'gnus-article-headers
5287   :type '(repeat (list (regexp :tag "Header")
5288                        regexp
5289                        (integer :tag "Button")
5290                        (sexp :tag "Form")
5291                        (function :tag "Callback")
5292                        (repeat :tag "Par"
5293                                :inline t
5294                                (integer :tag "Regexp group")))))
5295
5296 (defvar gnus-button-regexp nil)
5297 (defvar gnus-button-marker-list nil)
5298 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5299
5300 (defvar gnus-button-last nil)
5301 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5302
5303 ;;; Commands:
5304
5305 (defun gnus-article-push-button (event)
5306   "Check text under the mouse pointer for a callback function.
5307 If the text under the mouse pointer has a `gnus-callback' property,
5308 call it with the value of the `gnus-data' text property."
5309   (interactive "e")
5310   (set-buffer (window-buffer (posn-window (event-start event))))
5311   (let* ((pos (posn-point (event-start event)))
5312          (data (get-text-property pos 'gnus-data))
5313          (fun (get-text-property pos 'gnus-callback)))
5314     (goto-char pos)
5315     (when fun
5316       (funcall fun data))))
5317
5318 (defun gnus-article-press-button ()
5319   "Check text at point for a callback function.
5320 If the text at point has a `gnus-callback' property,
5321 call it with the value of the `gnus-data' text property."
5322   (interactive)
5323   (let* ((data (get-text-property (point) 'gnus-data))
5324          (fun (get-text-property (point) 'gnus-callback)))
5325     (when fun
5326       (funcall fun data))))
5327
5328 (defun gnus-article-prev-button (n)
5329   "Move point to N buttons backward.
5330 If N is negative, move forward instead."
5331   (interactive "p")
5332   (gnus-article-next-button (- n)))
5333
5334 (defun gnus-article-next-button (n)
5335   "Move point to N buttons forward.
5336 If N is negative, move backward instead."
5337   (interactive "p")
5338   (let ((function (if (< n 0) 'previous-single-property-change
5339                     'next-single-property-change))
5340         (inhibit-point-motion-hooks t)
5341         (backward (< n 0))
5342         (limit (if (< n 0) (point-min) (point-max))))
5343     (setq n (abs n))
5344     (while (and (not (= limit (point)))
5345                 (> n 0))
5346       ;; Skip past the current button.
5347       (when (get-text-property (point) 'gnus-callback)
5348         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5349       ;; Go to the next (or previous) button.
5350       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5351       ;; Put point at the start of the button.
5352       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5353         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5354       ;; Skip past intangible buttons.
5355       (when (get-text-property (point) 'intangible)
5356         (incf n))
5357       (decf n))
5358     (unless (zerop n)
5359       (gnus-message 5 "No more buttons"))
5360     n))
5361
5362 (defun gnus-article-highlight (&optional force)
5363   "Highlight current article.
5364 This function calls `gnus-article-highlight-headers',
5365 `gnus-article-highlight-citation',
5366 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5367 do the highlighting.  See the documentation for those functions."
5368   (interactive (list 'force))
5369   (gnus-article-highlight-headers)
5370   (gnus-article-highlight-citation force)
5371   (gnus-article-highlight-signature)
5372   (gnus-article-add-buttons force)
5373   (gnus-article-add-buttons-to-head))
5374
5375 (defun gnus-article-highlight-some (&optional force)
5376   "Highlight current article.
5377 This function calls `gnus-article-highlight-headers',
5378 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5379 do the highlighting.  See the documentation for those functions."
5380   (interactive (list 'force))
5381   (gnus-article-highlight-headers)
5382   (gnus-article-highlight-signature)
5383   (gnus-article-add-buttons))
5384
5385 (defun gnus-article-highlight-headers ()
5386   "Highlight article headers as specified by `gnus-header-face-alist'."
5387   (interactive)
5388   (save-excursion
5389     (set-buffer gnus-article-buffer)
5390     (save-restriction
5391       (let ((alist gnus-header-face-alist)
5392             (buffer-read-only nil)
5393             (case-fold-search t)
5394             (inhibit-point-motion-hooks t)
5395             entry regexp header-face field-face from hpoints fpoints)
5396         (article-narrow-to-head)
5397         (while (setq entry (pop alist))
5398           (goto-char (point-min))
5399           (setq regexp (concat "^\\("
5400                                (if (string-equal "" (nth 0 entry))
5401                                    "[^\t ]"
5402                                  (nth 0 entry))
5403                                "\\)")
5404                 header-face (nth 1 entry)
5405                 field-face (nth 2 entry))
5406           (while (and (re-search-forward regexp nil t)
5407                       (not (eobp)))
5408             (beginning-of-line)
5409             (setq from (point))
5410             (unless (search-forward ":" nil t)
5411               (forward-char 1))
5412             (when (and header-face
5413                        (not (memq (point) hpoints)))
5414               (push (point) hpoints)
5415               (gnus-put-text-property from (point) 'face header-face))
5416             (when (and field-face
5417                        (not (memq (setq from (point)) fpoints)))
5418               (push from fpoints)
5419               (if (re-search-forward "^[^ \t]" nil t)
5420                   (forward-char -2)
5421                 (goto-char (point-max)))
5422               (gnus-put-text-property from (point) 'face field-face))))))))
5423
5424 (defun gnus-article-highlight-signature ()
5425   "Highlight the signature in an article.
5426 It does this by highlighting everything after
5427 `gnus-signature-separator' using `gnus-signature-face'."
5428   (interactive)
5429   (when gnus-signature-face
5430     (save-excursion
5431       (set-buffer gnus-article-buffer)
5432       (let ((buffer-read-only nil)
5433             (inhibit-point-motion-hooks t))
5434         (save-restriction
5435           (when (gnus-article-narrow-to-signature)
5436             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5437                               'face gnus-signature-face)))))))
5438
5439 (defun gnus-article-buttonize-signature ()
5440   "Add button to the signature."
5441   (interactive)
5442   (save-excursion
5443     (set-buffer gnus-article-buffer)
5444     (let ((buffer-read-only nil)
5445           (inhibit-point-motion-hooks t))
5446       (when (gnus-article-search-signature)
5447         (gnus-article-add-button (match-beginning 0) (match-end 0)
5448                                  'gnus-signature-toggle
5449                                  (set-marker (make-marker)
5450                                              (1+ (match-end 0))))))))
5451
5452 (defun gnus-button-in-region-p (b e prop)
5453   "Say whether PROP exists in the region."
5454   (text-property-not-all b e prop nil))
5455
5456 (defun gnus-article-add-buttons (&optional force)
5457   "Find external references in the article and make buttons of them.
5458 \"External references\" are things like Message-IDs and URLs, as
5459 specified by `gnus-button-alist'."
5460   (interactive (list 'force))
5461   (save-excursion
5462     (set-buffer gnus-article-buffer)
5463     (let ((buffer-read-only nil)
5464           (inhibit-point-motion-hooks t)
5465           (case-fold-search t)
5466           (alist gnus-button-alist)
5467           beg entry regexp)
5468       ;; Remove all old markers.
5469       (let (marker entry new-list)
5470         (while (setq marker (pop gnus-button-marker-list))
5471           (if (or (< marker (point-min)) (>= marker (point-max)))
5472               (push marker new-list)
5473             (goto-char marker)
5474             (when (setq entry (gnus-button-entry))
5475               (put-text-property (match-beginning (nth 1 entry))
5476                                  (match-end (nth 1 entry))
5477                                  'gnus-callback nil))
5478             (set-marker marker nil)))
5479         (setq gnus-button-marker-list new-list))
5480       ;; We skip the headers.
5481       (article-goto-body)
5482       (setq beg (point))
5483       (while (setq entry (pop alist))
5484         (setq regexp (car entry))
5485         (goto-char beg)
5486         (while (re-search-forward regexp nil t)
5487           (let* ((start (and entry (match-beginning (nth 1 entry))))
5488                  (end (and entry (match-end (nth 1 entry))))
5489                  (from (match-beginning 0)))
5490             (when (and (or (eq t (nth 2 entry))
5491                            (eval (nth 2 entry)))
5492                        (not (gnus-button-in-region-p
5493                              start end 'gnus-callback)))
5494               ;; That optional form returned non-nil, so we add the
5495               ;; button.
5496               (gnus-article-add-button
5497                start end 'gnus-button-push
5498                (car (push (set-marker (make-marker) from)
5499                           gnus-button-marker-list))))))))))
5500
5501 ;; Add buttons to the head of an article.
5502 (defun gnus-article-add-buttons-to-head ()
5503   "Add buttons to the head of the article."
5504   (interactive)
5505   (save-excursion
5506     (set-buffer gnus-article-buffer)
5507     (save-restriction
5508       (let ((buffer-read-only nil)
5509             (inhibit-point-motion-hooks t)
5510             (case-fold-search t)
5511             (alist gnus-header-button-alist)
5512             entry beg end)
5513         (article-narrow-to-head)
5514         (while alist
5515           ;; Each alist entry.
5516           (setq entry (car alist)
5517                 alist (cdr alist))
5518           (goto-char (point-min))
5519           (while (re-search-forward (car entry) nil t)
5520             ;; Each header matching the entry.
5521             (setq beg (match-beginning 0))
5522             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5523                                (match-beginning 0))
5524                           (point-max)))
5525             (goto-char beg)
5526             (while (re-search-forward (nth 1 entry) end t)
5527               ;; Each match within a header.
5528               (let* ((entry (cdr entry))
5529                      (start (match-beginning (nth 1 entry)))
5530                      (end (match-end (nth 1 entry)))
5531                      (form (nth 2 entry)))
5532                 (goto-char (match-end 0))
5533                 (when (eval form)
5534                   (gnus-article-add-button
5535                    start end (nth 3 entry)
5536                    (buffer-substring (match-beginning (nth 4 entry))
5537                                      (match-end (nth 4 entry)))))))
5538             (goto-char end)))))))
5539
5540 ;;; External functions:
5541
5542 (defun gnus-article-add-button (from to fun &optional data)
5543   "Create a button between FROM and TO with callback FUN and data DATA."
5544   (when gnus-article-button-face
5545     (gnus-overlay-put (gnus-make-overlay from to)
5546                       'face gnus-article-button-face))
5547   (gnus-add-text-properties
5548    from to
5549    (nconc (and gnus-article-mouse-face
5550                (list gnus-mouse-face-prop gnus-article-mouse-face))
5551           (list 'gnus-callback fun)
5552           (and data (list 'gnus-data data))))
5553   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5554                          ;; Quote `:button-keymap' for Mule 2.3
5555                          ;; but it won't work.
5556                          ':button-keymap gnus-widget-button-keymap))
5557
5558 ;;; Internal functions:
5559
5560 (defun gnus-article-set-globals ()
5561   (save-excursion
5562     (set-buffer gnus-summary-buffer)
5563     (gnus-set-global-variables)))
5564
5565 (defun gnus-signature-toggle (end)
5566   (save-excursion
5567     (set-buffer gnus-article-buffer)
5568     (let ((buffer-read-only nil)
5569           (inhibit-point-motion-hooks t)
5570           (limit (next-single-property-change end 'mime-view-entity
5571                                               nil (point-max))))
5572       (if (text-property-any end limit 'article-type 'signature)
5573           (gnus-remove-text-properties-when
5574            'article-type 'signature end limit
5575            (cons 'article-type (cons 'signature
5576                                      gnus-hidden-properties)))
5577         (gnus-add-text-properties-when
5578          'article-type nil end limit
5579          (cons 'article-type (cons 'signature
5580                                    gnus-hidden-properties)))))))
5581
5582 (defun gnus-button-entry ()
5583   ;; Return the first entry in `gnus-button-alist' matching this place.
5584   (let ((alist gnus-button-alist)
5585         (entry nil))
5586     (while alist
5587       (setq entry (pop alist))
5588       (if (looking-at (car entry))
5589           (setq alist nil)
5590         (setq entry nil)))
5591     entry))
5592
5593 (defun gnus-button-push (marker)
5594   ;; Push button starting at MARKER.
5595   (save-excursion
5596     (goto-char marker)
5597     (let* ((entry (gnus-button-entry))
5598            (inhibit-point-motion-hooks t)
5599            (fun (nth 3 entry))
5600            (args (mapcar (lambda (group)
5601                            (let ((string (match-string group)))
5602                              (gnus-set-text-properties
5603                               0 (length string) nil string)
5604                              string))
5605                          (nthcdr 4 entry))))
5606       (cond
5607        ((fboundp fun)
5608         (apply fun args))
5609        ((and (boundp fun)
5610              (fboundp (symbol-value fun)))
5611         (apply (symbol-value fun) args))
5612        (t
5613         (gnus-message 1 "You must define `%S' to use this button"
5614                       (cons fun args)))))))
5615
5616 (defun gnus-parse-news-url (url)
5617   (let (scheme server group message-id articles)
5618     (with-temp-buffer
5619       (insert url)
5620       (goto-char (point-min))
5621       (when (looking-at "\\([A-Za-z]+\\):")
5622         (setq scheme (match-string 1))
5623         (goto-char (match-end 0)))
5624       (when (looking-at "//\\([^/]+\\)/")
5625         (setq server (match-string 1))
5626         (goto-char (match-end 0)))
5627
5628       (cond
5629        ((looking-at "\\(.*@.*\\)")
5630         (setq message-id (match-string 1)))
5631        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
5632         (setq group (match-string 1)
5633               articles (split-string (match-string 2) "-")))
5634        ((looking-at "\\([^/]+\\)/?")
5635         (setq group (match-string 1)))
5636        (t
5637         (error "Unknown news URL syntax"))))
5638     (list scheme server group message-id articles)))
5639
5640 (defun gnus-button-handle-news (url)
5641   "Fetch a news URL."
5642   (destructuring-bind (scheme server group message-id articles)
5643       (gnus-parse-news-url url)
5644     (cond
5645      (message-id
5646       (save-excursion
5647         (set-buffer gnus-summary-buffer)
5648         (if server
5649             (let ((gnus-refer-article-method (list (list 'nntp server))))
5650               (gnus-summary-refer-article message-id))
5651           (gnus-summary-refer-article message-id))))
5652      (group
5653       (gnus-button-fetch-group url)))))
5654
5655 (defun gnus-button-handle-info (url)
5656   "Fetch an info URL."
5657   (if (string-match 
5658        "^\\([^:/]+\\)?/\\(.*\\)"
5659        url)
5660       (gnus-info-find-node
5661        (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
5662                        "Gnus") 
5663                ")" 
5664                (gnus-url-unhex-string (match-string 2 url))))
5665     (error "Can't parse %s" url)))
5666
5667 (defun gnus-button-message-id (message-id)
5668   "Fetch MESSAGE-ID."
5669   (save-excursion
5670     (set-buffer gnus-summary-buffer)
5671     (gnus-summary-refer-article message-id)))
5672
5673 (defun gnus-button-fetch-group (address)
5674   "Fetch GROUP specified by ADDRESS."
5675   (if (not (string-match "[:/]" address))
5676       ;; This is just a simple group url.
5677       (gnus-group-read-ephemeral-group address gnus-select-method)
5678     (if (not
5679          (string-match
5680           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
5681           address))
5682         (error "Can't parse %s" address)
5683       (gnus-group-read-ephemeral-group
5684        (match-string 4 address)
5685        `(nntp ,(match-string 1 address)
5686               (nntp-address ,(match-string 1 address))
5687               (nntp-port-number ,(if (match-end 3)
5688                                      (match-string 3 address)
5689                                    "nntp")))
5690        nil nil nil
5691        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
5692
5693 (defun gnus-url-parse-query-string (query &optional downcase)
5694   (let (retval pairs cur key val)
5695     (setq pairs (split-string query "&"))
5696     (while pairs
5697       (setq cur (car pairs)
5698             pairs (cdr pairs))
5699       (if (not (string-match "=" cur))
5700           nil                           ; Grace
5701         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5702               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5703         (if downcase
5704             (setq key (downcase key)))
5705         (setq cur (assoc key retval))
5706         (if cur
5707             (setcdr cur (cons val (cdr cur)))
5708           (setq retval (cons (list key val) retval)))))
5709     retval))
5710
5711 (defun gnus-url-unhex (x)
5712   (if (> x ?9)
5713       (if (>= x ?a)
5714           (+ 10 (- x ?a))
5715         (+ 10 (- x ?A)))
5716     (- x ?0)))
5717
5718 (defun gnus-url-unhex-string (str &optional allow-newlines)
5719   "Remove %XXX embedded spaces, etc in a url.
5720 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5721 decoding of carriage returns and line feeds in the string, which is normally
5722 forbidden in URL encoding."
5723   (setq str (or (nnheader-replace-chars-in-string str ?+ ? ) ""))
5724   (let ((tmp "")
5725         (case-fold-search t))
5726     (while (string-match "%[0-9a-f][0-9a-f]" str)
5727       (let* ((start (match-beginning 0))
5728              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5729              (code (+ (* 16 ch1)
5730                       (gnus-url-unhex (elt str (+ start 2))))))
5731         (setq tmp (concat
5732                    tmp (substring str 0 start)
5733                    (cond
5734                     (allow-newlines
5735                      (char-to-string code))
5736                     ((or (= code ?\n) (= code ?\r))
5737                      " ")
5738                     (t (char-to-string code))))
5739               str (substring str (match-end 0)))))
5740     (setq tmp (concat tmp str))
5741     tmp))
5742
5743 (defun gnus-url-mailto (url)
5744   ;; Send mail to someone
5745   (when (string-match "mailto:/*\\(.*\\)" url)
5746     (setq url (substring url (match-beginning 1) nil)))
5747   (let (to args subject func)
5748     (if (string-match (regexp-quote "?") url)
5749         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5750               args (gnus-url-parse-query-string
5751                     (substring url (match-end 0) nil) t))
5752       (setq to (gnus-url-unhex-string url)))
5753     (setq args (cons (list "to" to) args)
5754           subject (cdr-safe (assoc "subject" args)))
5755     (gnus-msg-mail)
5756     (while args
5757       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5758       (if (fboundp func)
5759           (funcall func)
5760         (message-position-on-field (caar args)))
5761       (insert (mapconcat 'identity (cdar args) ", "))
5762       (setq args (cdr args)))
5763     (if subject
5764         (message-goto-body)
5765       (message-goto-subject))))
5766
5767 (defun gnus-button-embedded-url (address)
5768   "Activate ADDRESS with `browse-url'."
5769   (browse-url (gnus-strip-whitespace address)))
5770
5771 (eval-when-compile
5772   ;; Silence the byte-compiler.
5773   (autoload 'smiley-toggle-buffer "gnus-bitmap"))
5774 (defun gnus-article-smiley-display ()
5775   "Display \"smileys\" as small graphical icons."
5776   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5777
5778 ;;; Next/prev buttons in the article buffer.
5779
5780 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5781 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5782
5783 (defvar gnus-prev-page-map nil)
5784 (unless gnus-prev-page-map
5785   (setq gnus-prev-page-map (make-sparse-keymap))
5786   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5787   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5788
5789 (static-if (featurep 'xemacs)
5790     (defun gnus-insert-prev-page-button ()
5791       (let ((buffer-read-only nil))
5792         (gnus-eval-format
5793          gnus-prev-page-line-format nil
5794          `(gnus-prev t local-map ,gnus-prev-page-map
5795                      gnus-callback gnus-article-button-prev-page
5796                      article-type annotation))))
5797   (defun gnus-insert-prev-page-button ()
5798     (let ((buffer-read-only nil)
5799           (situation (get-text-property (point-min) 'mime-view-situation)))
5800       (set-keymap-parent gnus-prev-page-map (current-local-map))
5801       (gnus-eval-format
5802        gnus-prev-page-line-format nil
5803        `(gnus-prev t local-map ,gnus-prev-page-map
5804                    gnus-callback gnus-article-button-prev-page
5805                    article-type annotation
5806                    mime-view-situation ,situation))))
5807   )
5808
5809 (defvar gnus-next-page-map nil)
5810 (unless gnus-next-page-map
5811   (setq gnus-next-page-map (make-sparse-keymap))
5812   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5813   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5814
5815 (defun gnus-button-next-page ()
5816   "Go to the next page."
5817   (interactive)
5818   (let ((win (selected-window)))
5819     (select-window (get-buffer-window gnus-article-buffer t))
5820     (gnus-article-next-page)
5821     (select-window win)))
5822
5823 (defun gnus-button-prev-page ()
5824   "Go to the prev page."
5825   (interactive)
5826   (let ((win (selected-window)))
5827     (select-window (get-buffer-window gnus-article-buffer t))
5828     (gnus-article-prev-page)
5829     (select-window win)))
5830
5831 (static-if (featurep 'xemacs)
5832     (defun gnus-insert-next-page-button ()
5833       (let ((buffer-read-only nil))
5834         (gnus-eval-format gnus-next-page-line-format nil
5835                           `(gnus-next
5836                             t local-map ,gnus-next-page-map
5837                             gnus-callback gnus-article-button-next-page
5838                             article-type annotation))))
5839   (defun gnus-insert-next-page-button ()
5840     (let ((buffer-read-only nil)
5841           (situation (get-text-property (point-min) 'mime-view-situation)))
5842       (set-keymap-parent gnus-next-page-map (current-local-map))
5843       (gnus-eval-format gnus-next-page-line-format nil
5844                         `(gnus-next
5845                           t local-map ,gnus-next-page-map
5846                           gnus-callback gnus-article-button-next-page
5847                           article-type annotation
5848                           mime-view-situation ,situation))))
5849   )
5850
5851 (defun gnus-article-button-next-page (arg)
5852   "Go to the next page."
5853   (interactive "P")
5854   (let ((win (selected-window)))
5855     (select-window (get-buffer-window gnus-article-buffer t))
5856     (gnus-article-next-page)
5857     (select-window win)))
5858
5859 (defun gnus-article-button-prev-page (arg)
5860   "Go to the prev page."
5861   (interactive "P")
5862   (let ((win (selected-window)))
5863     (select-window (get-buffer-window gnus-article-buffer t))
5864     (gnus-article-prev-page)
5865     (select-window win)))
5866
5867 (defvar gnus-decode-header-methods
5868   '(mail-decode-encoded-word-region)
5869   "List of methods used to decode headers.
5870
5871 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5872 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5873 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5874 whose names match REGEXP.
5875
5876 For example:
5877 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5878  mail-decode-encoded-word-region
5879  (\"chinese\" . rfc1843-decode-region))
5880 ")
5881
5882 (defvar gnus-decode-header-methods-cache nil)
5883
5884 (defun gnus-multi-decode-header (start end)
5885   "Apply the functions from `gnus-encoded-word-methods' that match."
5886   (unless (and gnus-decode-header-methods-cache
5887                (eq gnus-newsgroup-name
5888                    (car gnus-decode-header-methods-cache)))
5889     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5890     (mapcar (lambda (x)
5891               (if (symbolp x)
5892                   (nconc gnus-decode-header-methods-cache (list x))
5893                 (if (and gnus-newsgroup-name
5894                          (string-match (car x) gnus-newsgroup-name))
5895                     (nconc gnus-decode-header-methods-cache
5896                            (list (cdr x))))))
5897             gnus-decode-header-methods))
5898   (let ((xlist gnus-decode-header-methods-cache))
5899     (pop xlist)
5900     (save-restriction
5901       (narrow-to-region start end)
5902       (while xlist
5903         (funcall (pop xlist) (point-min) (point-max))))))
5904
5905 ;;;
5906 ;;; Treatment top-level handling.
5907 ;;;
5908
5909 (defun gnus-treat-article (condition &optional part-number total-parts type)
5910   (let ((length (- (point-max) (point-min)))
5911         (alist gnus-treatment-function-alist)
5912         (article-goto-body-goes-to-point-min-p t)
5913         (treated-type
5914          (or (not type)
5915              (catch 'found
5916                (let ((list gnus-article-treat-types))
5917                  (while list
5918                    (when (string-match (pop list) type)
5919                      (throw 'found t)))))))
5920         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5921         (entity (static-unless (featurep 'xemacs)
5922                   (when (eq 'head condition)
5923                     (get-text-property (point-min) 'mime-view-entity))))
5924         val elem buttonized)
5925     (gnus-run-hooks 'gnus-part-display-hook)
5926     (unless gnus-inhibit-treatment
5927       (while (setq elem (pop alist))
5928         (setq val
5929               (save-excursion
5930                 (if (gnus-buffer-live-p gnus-summary-buffer)
5931                     (set-buffer gnus-summary-buffer))
5932                 (symbol-value (car elem))))
5933         (when (and (or (consp val)
5934                        treated-type)
5935                    (gnus-treat-predicate val)
5936                    (or (not (get (car elem) 'highlight))
5937                        highlightp))
5938           (when (and (not buttonized)
5939                      (memq (car elem)
5940                            '(gnus-treat-hide-signature
5941                              gnus-treat-highlight-signature)))
5942             (gnus-article-buttonize-signature)
5943             (setq buttonized t))
5944           (save-restriction
5945             (funcall (cadr elem)))))
5946       ;; FSF Emacsen does not inherit the existing text properties
5947       ;; in the new text, so we should do it for `mime-view-entity'.
5948       (static-unless (featurep 'xemacs)
5949         (when entity
5950           (put-text-property (point-min) (point-max)
5951                              'mime-view-entity entity))))))
5952
5953 ;; Dynamic variables.
5954 (eval-when-compile
5955   (defvar part-number)
5956   (defvar total-parts)
5957   (defvar type)
5958   (defvar condition)
5959   (defvar length))
5960
5961 (defun gnus-treat-predicate (val)
5962   (cond
5963    ((null val)
5964     nil)
5965    ((and (listp val)
5966          (stringp (car val)))
5967     (apply 'gnus-or (mapcar `(lambda (s)
5968                                (string-match s ,(or gnus-newsgroup-name "")))
5969                             val)))
5970    ((listp val)
5971     (let ((pred (pop val)))
5972       (cond
5973        ((eq pred 'or)
5974         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5975        ((eq pred 'and)
5976         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5977        ((eq pred 'not)
5978         (not (gnus-treat-predicate (car val))))
5979        ((eq pred 'typep)
5980         (equal (car val) type))
5981        (t
5982         (error "%S is not a valid predicate" pred)))))
5983    ((eq val 'mime)
5984     gnus-show-mime)
5985    (condition
5986     (eq condition val))
5987    ((eq val t)
5988     t)
5989    ((eq val 'head)
5990     nil)
5991    ((eq val 'last)
5992     (eq part-number total-parts))
5993    ((numberp val)
5994     (< length val))
5995    (t
5996     (error "%S is not a valid value" val))))
5997
5998 (defun gnus-article-encrypt-body (protocol &optional n)
5999   "Encrypt the article body."
6000   (interactive
6001    (list
6002     (or gnus-article-encrypt-protocol
6003         (completing-read "Encrypt protocol: "
6004                          gnus-article-encrypt-protocol-alist
6005                          nil t))
6006     current-prefix-arg))
6007   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
6008     (unless func
6009       (error (format "Can't find the encrypt protocol %s" protocol)))
6010     (if (equal gnus-newsgroup-name "nndraft:drafts")
6011         (error "Can't encrypt the article in group nndraft:drafts"))
6012     (if (equal gnus-newsgroup-name "nndraft:queue")
6013         (error "Don't encrypt the article in group nndraft:queue"))
6014     (gnus-summary-iterate n
6015       (save-excursion
6016         (set-buffer gnus-summary-buffer)
6017         (let ((mail-parse-charset gnus-newsgroup-charset)
6018               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6019               (summary-buffer gnus-summary-buffer)
6020               references point)
6021           (gnus-set-global-variables)
6022           (when (gnus-group-read-only-p)
6023             (error "The current newsgroup does not support article encrypt"))
6024           (gnus-summary-show-article t)
6025           (setq references
6026                 (or (mail-header-references gnus-current-headers) ""))
6027           (set-buffer gnus-article-buffer)
6028           (let* ((buffer-read-only nil)
6029                  (headers
6030                   (mapcar (lambda (field)
6031                             (and (save-restriction
6032                                    (message-narrow-to-head)
6033                                    (goto-char (point-min))
6034                                    (search-forward field nil t))
6035                                  (prog2
6036                                      (message-narrow-to-field)
6037                                      (buffer-substring (point-min) (point-max))
6038                                    (delete-region (point-min) (point-max))
6039                                    (widen))))
6040                           '("Content-Type:" "Content-Transfer-Encoding:"
6041                             "Content-Disposition:"))))
6042             (message-narrow-to-head)
6043             (message-remove-header "MIME-Version")
6044             (goto-char (point-max))
6045             (setq point (point))
6046             (insert (apply 'concat headers))
6047             (widen)
6048             (narrow-to-region point (point-max))
6049             (let ((message-options message-options))
6050               (message-options-set 'message-sender user-mail-address)
6051               (message-options-set 'message-recipients user-mail-address)
6052               (message-options-set 'message-sign-encrypt 'not)
6053               (funcall func))
6054             (goto-char (point-min))
6055             (insert "MIME-Version: 1.0\n")
6056             (widen)
6057             (gnus-summary-edit-article-done
6058              references nil summary-buffer t))
6059           (when gnus-keep-backlog
6060             (gnus-backlog-remove-article
6061              (car gnus-article-current) (cdr gnus-article-current)))
6062           (save-excursion
6063             (when (get-buffer gnus-original-article-buffer)
6064               (set-buffer gnus-original-article-buffer)
6065               (setq gnus-original-article nil)))
6066           (when gnus-use-cache
6067             (gnus-cache-update-article
6068              (car gnus-article-current) (cdr gnus-article-current))))))))
6069
6070 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6071   "The following specs can be used:
6072 %t  The security MIME type
6073 %i  Additional info
6074 %d  Details
6075 %D  Details if button is pressed")
6076
6077 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6078   "The following specs can be used:
6079 %t  The security MIME type
6080 %i  Additional info
6081 %d  Details
6082 %D  Details if button is pressed")
6083
6084 (defvar gnus-mime-security-button-line-format-alist
6085   '((?t gnus-tmp-type ?s)
6086     (?i gnus-tmp-info ?s)
6087     (?d gnus-tmp-details ?s)
6088     (?D gnus-tmp-pressed-details ?s)))
6089
6090 (defvar gnus-mime-security-button-map
6091   (let ((map (make-sparse-keymap)))
6092     (unless (>= (string-to-number emacs-version) 21)
6093       (set-keymap-parent map gnus-article-mode-map))
6094     (define-key map gnus-mouse-2 'gnus-article-push-button)
6095     (define-key map "\r" 'gnus-article-press-button)
6096     map))
6097
6098 (defvar gnus-mime-security-details-buffer nil)
6099
6100 (defvar gnus-mime-security-button-pressed nil)
6101
6102 (defvar gnus-mime-security-show-details-inline t
6103   "If non-nil, show details in the article buffer.")
6104
6105 (defun gnus-mime-security-verify-or-decrypt (handle)
6106   (mm-remove-parts (cdr handle))
6107   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6108         point buffer-read-only)
6109     (if region
6110         (goto-char (car region)))
6111     (save-restriction
6112       (narrow-to-region (point) (point))
6113       (with-current-buffer (mm-handle-multipart-original-buffer handle)
6114         (let* ((mm-verify-option 'known)
6115                (mm-decrypt-option 'known)
6116                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6117           (unless (eq nparts (cdr handle))
6118             (mm-destroy-parts (cdr handle))
6119             (setcdr handle nparts))))
6120       (setq point (point))
6121       (gnus-mime-display-security handle)
6122       (goto-char (point-max)))
6123     (when region
6124       (delete-region (point) (cdr region))
6125       (set-marker (car region) nil)
6126       (set-marker (cdr region) nil))
6127     (goto-char point)))
6128
6129 (defun gnus-mime-security-show-details (handle)
6130   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6131     (if details
6132         (if gnus-mime-security-show-details-inline
6133             (let ((gnus-mime-security-button-pressed t)
6134                   (gnus-mime-security-button-line-format
6135                    (get-text-property (point) 'gnus-line-format))
6136                   buffer-read-only)
6137               (forward-char -1)
6138               (while (eq (get-text-property (point) 'gnus-line-format)
6139                          gnus-mime-security-button-line-format)
6140                 (forward-char -1))
6141               (forward-char)
6142               (save-restriction
6143                 (narrow-to-region (point) (point))
6144                 (gnus-insert-mime-security-button handle))
6145               (delete-region (point)
6146                              (or (text-property-not-all
6147                                   (point) (point-max)
6148                                   'gnus-line-format
6149                                   gnus-mime-security-button-line-format)
6150                                  (point-max))))
6151           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6152               (with-current-buffer gnus-mime-security-details-buffer
6153                 (erase-buffer)
6154                 t)
6155             (setq gnus-mime-security-details-buffer
6156                   (gnus-get-buffer-create "*MIME Security Details*")))
6157           (with-current-buffer gnus-mime-security-details-buffer
6158             (insert details)
6159             (goto-char (point-min)))
6160           (pop-to-buffer gnus-mime-security-details-buffer))
6161       (gnus-message 5 "No details."))))
6162
6163 (defun gnus-mime-security-press-button (handle)
6164   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6165       (gnus-mime-security-show-details handle)
6166     (gnus-mime-security-verify-or-decrypt handle)))
6167
6168 (defun gnus-insert-mime-security-button (handle &optional displayed)
6169   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6170          (gnus-tmp-type
6171           (concat
6172            (or (nth 2 (assoc protocol mm-verify-function-alist))
6173                (nth 2 (assoc protocol mm-decrypt-function-alist))
6174                "Unknown")
6175            (if (equal (car handle) "multipart/signed")
6176                " Signed" " Encrypted")
6177            " Part"))
6178          (gnus-tmp-info
6179           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6180               "Undecided"))
6181          (gnus-tmp-details
6182           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6183          gnus-tmp-pressed-details
6184          b e)
6185     (setq gnus-tmp-details
6186           (if gnus-tmp-details
6187               (concat "\n" gnus-tmp-details) ""))
6188     (setq gnus-tmp-pressed-details
6189           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6190     (unless (bolp)
6191       (insert "\n"))
6192     (setq b (point))
6193     (gnus-eval-format
6194      gnus-mime-security-button-line-format
6195      gnus-mime-security-button-line-format-alist
6196      `(keymap ,gnus-mime-security-button-map
6197               ,@(if (>= (string-to-number emacs-version) 21)
6198                     nil ;; XEmacs doesn't care
6199                   (list 'local-map gnus-mime-security-button-map))
6200               gnus-callback gnus-mime-security-press-button
6201               gnus-line-format ,gnus-mime-security-button-line-format
6202               article-type annotation
6203               gnus-data ,handle))
6204     (setq e (point))
6205     (widget-convert-button
6206      'link b e
6207      :mime-handle handle
6208      :action 'gnus-widget-press-button
6209      :button-keymap gnus-mime-security-button-map
6210      :help-echo
6211      (lambda (widget/window &optional overlay pos)
6212        ;; Needed to properly clear the message due to a bug in
6213        ;; wid-edit (XEmacs only).
6214        (if (boundp 'help-echo-owns-message)
6215            (setq help-echo-owns-message t))
6216        (format
6217         "%S: show detail"
6218         (aref gnus-mouse-2 0))))))
6219
6220 (defun gnus-mime-display-security (handle)
6221   (save-restriction
6222     (narrow-to-region (point) (point))
6223     (unless (gnus-unbuttonized-mime-type-p (car handle))
6224       (gnus-insert-mime-security-button handle))
6225     (gnus-mime-display-mixed (cdr handle))
6226     (unless (bolp)
6227       (insert "\n"))
6228     (unless (gnus-unbuttonized-mime-type-p (car handle))
6229       (let ((gnus-mime-security-button-line-format
6230              gnus-mime-security-button-end-line-format))
6231         (gnus-insert-mime-security-button handle)))
6232     (mm-set-handle-multipart-parameter
6233      handle 'gnus-region
6234      (cons (set-marker (make-marker) (point-min))
6235            (set-marker (make-marker) (point-max))))))
6236
6237
6238 ;;; @ for mime-view
6239 ;;;
6240
6241 (defun gnus-article-header-presentation-method (entity situation)
6242   (mime-insert-header entity)
6243   )
6244
6245 (set-alist 'mime-header-presentation-method-alist
6246            'gnus-original-article-mode
6247            #'gnus-article-header-presentation-method)
6248
6249 (defun gnus-mime-preview-quitting-method ()
6250   (mime-preview-kill-buffer)
6251   (delete-other-windows)
6252   (gnus-article-show-summary)
6253   (gnus-summary-select-article gnus-show-all-headers t))
6254
6255 (set-alist 'mime-preview-quitting-method-alist
6256            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6257
6258 (set-alist 'mime-preview-following-method-alist
6259            'gnus-original-article-mode #'gnus-following-method)
6260
6261 (set-alist 'mime-preview-over-to-previous-method-alist
6262            'gnus-original-article-mode
6263            (lambda ()
6264              (if (> (point-min) 1)
6265                  (gnus-article-prev-page)
6266                (gnus-article-read-summary-keys
6267                 nil (gnus-character-to-event ?P)))))
6268
6269 (set-alist 'mime-preview-over-to-next-method-alist
6270            'gnus-original-article-mode'
6271            (lambda ()
6272              (if (< (point-max) (buffer-size))
6273                  (gnus-article-next-page)
6274                (gnus-article-read-summary-keys
6275                 nil (gnus-character-to-event ?N)))))
6276
6277
6278 ;;; @ end
6279 ;;;
6280
6281 (gnus-ems-redefine)
6282
6283 (provide 'gnus-art)
6284
6285 (run-hooks 'gnus-art-load-hook)
6286
6287 ;;; gnus-art.el ends here