Importing Pterodactyl Gnus v0.93.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'custom)
31 (require 'gnus)
32 (require 'gnus-sum)
33 (require 'gnus-spec)
34 (require 'gnus-int)
35 (require 'browse-url)
36 (require 'mm-bodies)
37 (require 'mail-parse)
38 (require 'mm-decode)
39 (require 'mm-view)
40 (require 'wid-edit)
41 (require 'mm-uu)
42
43 (defgroup gnus-article nil
44   "Article display."
45   :link '(custom-manual "(gnus)The Article Buffer")
46   :group 'gnus)
47
48 (defgroup gnus-article-treat nil
49   "Treating article parts."
50   :link '(custom-manual "(gnus)Article Hiding")
51   :group 'gnus-article)
52
53 (defgroup gnus-article-hiding nil
54   "Hiding article parts."
55   :link '(custom-manual "(gnus)Article Hiding")
56   :group 'gnus-article)
57
58 (defgroup gnus-article-highlight nil
59   "Article highlighting."
60   :link '(custom-manual "(gnus)Article Highlighting")
61   :group 'gnus-article
62   :group 'gnus-visual)
63
64 (defgroup gnus-article-signature nil
65   "Article signatures."
66   :link '(custom-manual "(gnus)Article Signature")
67   :group 'gnus-article)
68
69 (defgroup gnus-article-headers nil
70   "Article headers."
71   :link '(custom-manual "(gnus)Hiding Headers")
72   :group 'gnus-article)
73
74 (defgroup gnus-article-washing nil
75   "Special commands on articles."
76   :link '(custom-manual "(gnus)Article Washing")
77   :group 'gnus-article)
78
79 (defgroup gnus-article-emphasis nil
80   "Fontisizing articles."
81   :link '(custom-manual "(gnus)Article Fontisizing")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-saving nil
85   "Saving articles."
86   :link '(custom-manual "(gnus)Saving Articles")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-mime nil
90   "Worshiping the MIME wonder."
91   :link '(custom-manual "(gnus)Using MIME")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-buttons nil
95   "Pushable buttons in the article buffer."
96   :link '(custom-manual "(gnus)Article Buttons")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-various nil
100   "Other article options."
101   :link '(custom-manual "(gnus)Misc Article")
102   :group 'gnus-article)
103
104 (defcustom gnus-ignored-headers
105   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
106     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
107     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
108     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
109     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
110     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
111     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
112     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
113     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
114     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
115     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
116     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
117     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
118     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
119     "^Old-Received:" "^X-Pgp-Fingerprint:" "^X-Pgp-Key-Id:"
120     "^X-Pgp-Public-Key-Url:" "^X-Auth:" "^X-From-Line:"
121     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
122     "^X-Mailing-List:" "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
123     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:")
124   "*All headers that start with this regexp will be hidden.
125 This variable can also be a list of regexps of headers to be ignored.
126 If `gnus-visible-headers' is non-nil, this variable will be ignored."
127   :type '(choice :custom-show nil
128                  regexp
129                  (repeat regexp))
130   :group 'gnus-article-hiding)
131
132 (defcustom gnus-visible-headers
133   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
134   "*All headers that do not match this regexp will be hidden.
135 This variable can also be a list of regexp of headers to remain visible.
136 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
137   :type '(repeat :value-to-internal (lambda (widget value)
138                                       (custom-split-regexp-maybe value))
139                  :match (lambda (widget value)
140                           (or (stringp value)
141                               (widget-editable-list-match widget value)))
142                  regexp)
143   :group 'gnus-article-hiding)
144
145 (defcustom gnus-sorted-header-list
146   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
147     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
148   "*This variable is a list of regular expressions.
149 If it is non-nil, headers that match the regular expressions will
150 be placed first in the article buffer in the sequence specified by
151 this list."
152   :type '(repeat regexp)
153   :group 'gnus-article-hiding)
154
155 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
156   "Headers that are only to be displayed if they have interesting data.
157 Possible values in this list are `empty', `newsgroups', `followup-to',
158 `reply-to', `date', `long-to', and `many-to'."
159   :type '(set (const :tag "Headers with no content." empty)
160               (const :tag "Newsgroups with only one group." newsgroups)
161               (const :tag "Followup-to identical to newsgroups." followup-to)
162               (const :tag "Reply-to identical to from." reply-to)
163               (const :tag "Date less than four days old." date)
164               (const :tag "Very long To header." long-to)
165               (const :tag "Multiple To headers." many-to))
166   :group 'gnus-article-hiding)
167
168 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
169   "Regexp matching signature separator.
170 This can also be a list of regexps.  In that case, it will be checked
171 from head to tail looking for a separator.  Searches will be done from
172 the end of the buffer."
173   :type '(repeat string)
174   :group 'gnus-article-signature)
175
176 (defcustom gnus-signature-limit nil
177    "Provide a limit to what is considered a signature.
178 If it is a number, no signature may not be longer (in characters) than
179 that number.  If it is a floating point number, no signature may be
180 longer (in lines) than that number.  If it is a function, the function
181 will be called without any parameters, and if it returns nil, there is
182 no signature in the buffer.  If it is a string, it will be used as a
183 regexp.  If it matches, the text in question is not a signature."
184   :type '(choice (integer :value 200)
185                  (number :value 4.0)
186                  (function :value fun)
187                  (regexp :value ".*"))
188   :group 'gnus-article-signature)
189
190 (defcustom gnus-hidden-properties '(invisible t intangible t)
191   "Property list to use for hiding text."
192   :type 'sexp
193   :group 'gnus-article-hiding)
194
195 (defcustom gnus-article-x-face-command
196   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | display -"
197   "*String or function to be executed to display an X-Face header.
198 If it is a string, the command will be executed in a sub-shell
199 asynchronously.  The compressed face will be piped to this command."
200   :type 'string                         ;Leave function case to Lisp.
201   :group 'gnus-article-washing)
202
203 (defcustom gnus-article-x-face-too-ugly nil
204   "Regexp matching posters whose face shouldn't be shown automatically."
205   :type '(choice regexp (const nil))
206   :group 'gnus-article-washing)
207
208 (defcustom gnus-emphasis-alist
209   (let ((format
210          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\|\\s)\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-?!.,;:\"]\\|\\s(\\|\\s)\\)")
211         (types
212          '(("_" "_" underline)
213            ("/" "/" italic)
214            ("\\*" "\\*" bold)
215            ("_/" "/_" underline-italic)
216            ("_\\*" "\\*_" underline-bold)
217            ("\\*/" "/\\*" bold-italic)
218            ("_\\*/" "/\\*_" underline-bold-italic))))
219     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
220        2 3 gnus-emphasis-underline)
221       ,@(mapcar
222          (lambda (spec)
223            (list
224             (format format (car spec) (cadr spec))
225             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
226          types)))
227   "*Alist that says how to fontify certain phrases.
228 Each item looks like this:
229
230   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
231
232 The first element is a regular expression to be matched.  The second
233 is a number that says what regular expression grouping used to find
234 the entire emphasized word.  The third is a number that says what
235 regexp grouping should be displayed and highlighted.  The fourth
236 is the face used for highlighting."
237   :type '(repeat (list :value ("" 0 0 default)
238                        regexp
239                        (integer :tag "Match group")
240                        (integer :tag "Emphasize group")
241                        face))
242   :group 'gnus-article-emphasis)
243
244 (defface gnus-emphasis-bold '((t (:bold t)))
245   "Face used for displaying strong emphasized text (*word*)."
246   :group 'gnus-article-emphasis)
247
248 (defface gnus-emphasis-italic '((t (:italic t)))
249   "Face used for displaying italic emphasized text (/word/)."
250   :group 'gnus-article-emphasis)
251
252 (defface gnus-emphasis-underline '((t (:underline t)))
253   "Face used for displaying underlined emphasized text (_word_)."
254   :group 'gnus-article-emphasis)
255
256 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
257   "Face used for displaying underlined bold emphasized text (_*word*_)."
258   :group 'gnus-article-emphasis)
259
260 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
261   "Face used for displaying underlined italic emphasized text (_*word*_)."
262   :group 'gnus-article-emphasis)
263
264 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
265   "Face used for displaying bold italic emphasized text (/*word*/)."
266   :group 'gnus-article-emphasis)
267
268 (defface gnus-emphasis-underline-bold-italic
269   '((t (:bold t :italic t :underline t)))
270   "Face used for displaying underlined bold italic emphasized text.
271 Esample: (_/*word*/_)."
272   :group 'gnus-article-emphasis)
273
274 (defface gnus-emphasis-highlight-words
275   '((t (:background "black" :foreground "yellow")))
276   "Face used for displaying highlighted words."
277   :group 'gnus-article-emphasis)
278
279 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
280   "Format for display of Date headers in article bodies.
281 See `format-time-string' for the possible values.
282
283 The variable can also be function, which should return a complete Date
284 header.  The function is called with one argument, the time, which can
285 be fed to `format-time-string'."
286   :type '(choice string symbol)
287   :link '(custom-manual "(gnus)Article Date")
288   :group 'gnus-article-washing)
289
290 (eval-and-compile
291   (autoload 'mail-extract-address-components "mail-extr"))
292
293 (defcustom gnus-save-all-headers t
294   "*If non-nil, don't remove any headers before saving."
295   :group 'gnus-article-saving
296   :type 'boolean)
297
298 (defcustom gnus-prompt-before-saving 'always
299   "*This variable says how much prompting is to be done when saving articles.
300 If it is nil, no prompting will be done, and the articles will be
301 saved to the default files.  If this variable is `always', each and
302 every article that is saved will be preceded by a prompt, even when
303 saving large batches of articles.  If this variable is neither nil not
304 `always', there the user will be prompted once for a file name for
305 each invocation of the saving commands."
306   :group 'gnus-article-saving
307   :type '(choice (item always)
308                  (item :tag "never" nil)
309                  (sexp :tag "once" :format "%t\n" :value t)))
310
311 (defcustom gnus-saved-headers gnus-visible-headers
312   "Headers to keep if `gnus-save-all-headers' is nil.
313 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
314 If that variable is nil, however, all headers that match this regexp
315 will be kept while the rest will be deleted before saving."
316   :group 'gnus-article-saving
317   :type 'regexp)
318
319 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
320   "A function to save articles in your favourite format.
321 The function must be interactively callable (in other words, it must
322 be an Emacs command).
323
324 Gnus provides the following functions:
325
326 * gnus-summary-save-in-rmail (Rmail format)
327 * gnus-summary-save-in-mail (Unix mail format)
328 * gnus-summary-save-in-folder (MH folder)
329 * gnus-summary-save-in-file (article format)
330 * gnus-summary-save-in-vm (use VM's folder format)
331 * gnus-summary-write-to-file (article format -- overwrite)."
332   :group 'gnus-article-saving
333   :type '(radio (function-item gnus-summary-save-in-rmail)
334                 (function-item gnus-summary-save-in-mail)
335                 (function-item gnus-summary-save-in-folder)
336                 (function-item gnus-summary-save-in-file)
337                 (function-item gnus-summary-save-in-vm)
338                 (function-item gnus-summary-write-to-file)))
339
340 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
341   "A function generating a file name to save articles in Rmail format.
342 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
343   :group 'gnus-article-saving
344   :type 'function)
345
346 (defcustom gnus-mail-save-name 'gnus-plain-save-name
347   "A function generating a file name to save articles in Unix mail format.
348 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
349   :group 'gnus-article-saving
350   :type 'function)
351
352 (defcustom gnus-folder-save-name 'gnus-folder-save-name
353   "A function generating a file name to save articles in MH folder.
354 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
355   :group 'gnus-article-saving
356   :type 'function)
357
358 (defcustom gnus-file-save-name 'gnus-numeric-save-name
359   "A function generating a file name to save articles in article format.
360 The function is called with NEWSGROUP, HEADERS, and optional
361 LAST-FILE."
362   :group 'gnus-article-saving
363   :type 'function)
364
365 (defcustom gnus-split-methods
366   '((gnus-article-archive-name)
367     (gnus-article-nndoc-name))
368   "*Variable used to suggest where articles are to be saved.
369 For instance, if you would like to save articles related to Gnus in
370 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
371 you could set this variable to something like:
372
373  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
374    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
375
376 This variable is an alist where the where the key is the match and the
377 value is a list of possible files to save in if the match is non-nil.
378
379 If the match is a string, it is used as a regexp match on the
380 article.  If the match is a symbol, that symbol will be funcalled
381 from the buffer of the article to be saved with the newsgroup as the
382 parameter.  If it is a list, it will be evaled in the same buffer.
383
384 If this form or function returns a string, this string will be used as
385 a possible file name; and if it returns a non-nil list, that list will
386 be used as possible file names."
387   :group 'gnus-article-saving
388   :type '(repeat (choice (list :value (fun) function)
389                          (cons :value ("" "") regexp (repeat string))
390                          (sexp :value nil))))
391
392 (defcustom gnus-page-delimiter "^\^L"
393   "*Regexp describing what to use as article page delimiters.
394 The default value is \"^\^L\", which is a form linefeed at the
395 beginning of a line."
396   :type 'regexp
397   :group 'gnus-article-various)
398
399 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
400   "*The format specification for the article mode line.
401 See `gnus-summary-mode-line-format' for a closer description.
402
403 The following additional specs are available:
404
405 %w  The article washing status.
406 %m  The number of MIME parts in the article."
407   :type 'string
408   :group 'gnus-article-various)
409
410 (defcustom gnus-article-mode-hook nil
411   "*A hook for Gnus article mode."
412   :type 'hook
413   :group 'gnus-article-various)
414
415 (defcustom gnus-article-menu-hook nil
416   "*Hook run after the creation of the article mode menu."
417   :type 'hook
418   :group 'gnus-article-various)
419
420 (defcustom gnus-article-prepare-hook nil
421   "*A hook called after an article has been prepared in the article buffer."
422   :type 'hook
423   :group 'gnus-article-various)
424
425 (defcustom gnus-article-hide-pgp-hook nil
426   "*A hook called after successfully hiding a PGP signature."
427   :type 'hook
428   :group 'gnus-article-various)
429
430 (defcustom gnus-article-button-face 'bold
431   "Face used for highlighting buttons in the article buffer.
432
433 An article button is a piece of text that you can activate by pressing
434 `RET' or `mouse-2' above it."
435   :type 'face
436   :group 'gnus-article-buttons)
437
438 (defcustom gnus-article-mouse-face 'highlight
439   "Face used for mouse highlighting in the article buffer.
440
441 Article buttons will be displayed in this face when the cursor is
442 above them."
443   :type 'face
444   :group 'gnus-article-buttons)
445
446 (defcustom gnus-signature-face 'gnus-signature-face
447   "Face used for highlighting a signature in the article buffer.
448 Obsolete; use the face `gnus-signature-face' for customizations instead."
449   :type 'face
450   :group 'gnus-article-highlight
451   :group 'gnus-article-signature)
452
453 (defface gnus-signature-face
454   '((((type x))
455      (:italic t)))
456   "Face used for highlighting a signature in the article buffer."
457   :group 'gnus-article-highlight
458   :group 'gnus-article-signature)
459
460 (defface gnus-header-from-face
461   '((((class color)
462       (background dark))
463      (:foreground "spring green"))
464     (((class color)
465       (background light))
466      (:foreground "red3"))
467     (t
468      (:italic t)))
469   "Face used for displaying from headers."
470   :group 'gnus-article-headers
471   :group 'gnus-article-highlight)
472
473 (defface gnus-header-subject-face
474   '((((class color)
475       (background dark))
476      (:foreground "SeaGreen3"))
477     (((class color)
478       (background light))
479      (:foreground "red4"))
480     (t
481      (:bold t :italic t)))
482   "Face used for displaying subject headers."
483   :group 'gnus-article-headers
484   :group 'gnus-article-highlight)
485
486 (defface gnus-header-newsgroups-face
487   '((((class color)
488       (background dark))
489      (:foreground "yellow" :italic t))
490     (((class color)
491       (background light))
492      (:foreground "MidnightBlue" :italic t))
493     (t
494      (:italic t)))
495   "Face used for displaying newsgroups headers."
496   :group 'gnus-article-headers
497   :group 'gnus-article-highlight)
498
499 (defface gnus-header-name-face
500   '((((class color)
501       (background dark))
502      (:foreground "SeaGreen"))
503     (((class color)
504       (background light))
505      (:foreground "maroon"))
506     (t
507      (:bold t)))
508   "Face used for displaying header names."
509   :group 'gnus-article-headers
510   :group 'gnus-article-highlight)
511
512 (defface gnus-header-content-face
513   '((((class color)
514       (background dark))
515      (:foreground "forest green" :italic t))
516     (((class color)
517       (background light))
518      (:foreground "indianred4" :italic t))
519     (t
520      (:italic t)))  "Face used for displaying header content."
521   :group 'gnus-article-headers
522   :group 'gnus-article-highlight)
523
524 (defcustom gnus-header-face-alist
525   '(("From" nil gnus-header-from-face)
526     ("Subject" nil gnus-header-subject-face)
527     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
528     ("" gnus-header-name-face gnus-header-content-face))
529   "*Controls highlighting of article header.
530
531 An alist of the form (HEADER NAME CONTENT).
532
533 HEADER is a regular expression which should match the name of an
534 header header and NAME and CONTENT are either face names or nil.
535
536 The name of each header field will be displayed using the face
537 specified by the first element in the list where HEADER match the
538 header name and NAME is non-nil.  Similarly, the content will be
539 displayed by the first non-nil matching CONTENT face."
540   :group 'gnus-article-headers
541   :group 'gnus-article-highlight
542   :type '(repeat (list (regexp :tag "Header")
543                        (choice :tag "Name"
544                                (item :tag "skip" nil)
545                                (face :value default))
546                        (choice :tag "Content"
547                                (item :tag "skip" nil)
548                                (face :value default)))))
549
550 (defcustom gnus-article-decode-hook
551   '(article-decode-charset article-decode-encoded-words)
552   "*Hook run to decode charsets in articles."
553   :group 'gnus-article-headers
554   :type 'hook)
555
556 (defcustom gnus-display-mime-function 'gnus-display-mime
557   "Function to display MIME articles."
558   :group 'gnus-article-mime
559   :type 'function)
560
561 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
562   "Function used to decode headers.")
563
564 (defvar gnus-article-dumbquotes-map
565   '(("\202" ",")
566     ("\203" "f")
567     ("\204" ",,")
568     ("\205" "...")
569     ("\213" "<")
570     ("\214" "OE")
571     ("\221" "`")
572     ("\222" "'")
573     ("\223" "``")
574     ("\224" "\"")
575     ("\225" "*")
576     ("\226" "---")
577     ("\227" "-")
578     ("\231" "(TM)")
579     ("\233" ">")
580     ("\234" "oe")
581     ("\264" "'"))
582   "Table for MS-to-Latin1 translation.")
583
584 (defcustom gnus-ignored-mime-types nil
585   "List of MIME types that should be ignored by Gnus."
586   :group 'gnus-article-mime
587   :type '(repeat regexp))
588
589 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
590   "List of MIME types that should not be given buttons when rendered."
591   :group 'gnus-article-mime
592   :type '(repeat regexp))
593
594 (defcustom gnus-article-mime-part-function nil
595   "Function called with a MIME handle as the argument.
596 This is meant for people who want to do something automatic based
597 on parts -- for instance, adding Vcard info to a database."
598   :group 'gnus-article-mime
599   :type 'function)
600
601 (defcustom gnus-mime-multipart-functions nil
602   "An alist of MIME types to functions to display them.")
603
604 (defcustom gnus-article-date-lapsed-new-header nil
605   "Whether the X-Sent and Date headers can coexist.
606 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
607 either replace the old \"Date:\" header (if this variable is nil), or
608 be added below it (otherwise)."
609   :group 'gnus-article-headers
610   :type 'boolean)
611
612 ;;;
613 ;;; The treatment variables
614 ;;;
615
616 (defvar gnus-part-display-hook nil
617   "Hook called on parts that are to receive treatment.")
618
619 (defvar gnus-article-treat-custom
620   '(choice (const :tag "Off" nil)
621            (const :tag "On" t)
622            (const :tag "Header" head)
623            (const :tag "Last" last)
624            (integer :tag "Less")
625            (repeat :tag "Groups" regexp)
626            (sexp :tag "Predicate")))
627
628 (defvar gnus-article-treat-head-custom
629   '(choice (const :tag "Off" nil)
630            (const :tag "Header" head)))
631
632 (defvar gnus-article-treat-types '("text/plain")
633   "Parts to treat.")
634
635 (defvar gnus-inhibit-treatment nil
636   "Whether to inhibit treatment.")
637
638 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
639   "Highlight the signature.
640 Valid values are nil, t, `head', `last', an integer or a predicate.
641 See the manual for details."
642   :group 'gnus-article-treat
643   :type gnus-article-treat-custom)
644 (put 'gnus-treat-highlight-signature 'highlight t)
645
646 (defcustom gnus-treat-buttonize t
647   "Add buttons.
648 Valid values are nil, t, `head', `last', an integer or a predicate.
649 See the manual for details."
650   :group 'gnus-article-treat
651   :type gnus-article-treat-custom)
652 (put 'gnus-treat-buttonize 'highlight t)
653
654 (defcustom gnus-treat-buttonize-head 'head
655   "Add buttons to the head.
656 Valid values are nil, t, `head', `last', an integer or a predicate.
657 See the manual for details."
658   :group 'gnus-article-treat
659   :type gnus-article-treat-head-custom)
660 (put 'gnus-treat-buttonize-head 'highlight t)
661
662 (defcustom gnus-treat-emphasize t
663   "Emphasize text.
664 Valid values are nil, t, `head', `last', an integer or a predicate.
665 See the manual for details."
666   :group 'gnus-article-treat
667   :type gnus-article-treat-custom)
668 (put 'gnus-treat-emphasize 'highlight t)
669
670 (defcustom gnus-treat-strip-cr nil
671   "Remove carriage returns.
672 Valid values are nil, t, `head', `last', an integer or a predicate.
673 See the manual for details."
674   :group 'gnus-article-treat
675   :type gnus-article-treat-custom)
676
677 (defcustom gnus-treat-hide-headers 'head
678   "Hide headers.
679 Valid values are nil, t, `head', `last', an integer or a predicate.
680 See the manual for details."
681   :group 'gnus-article-treat
682   :type gnus-article-treat-head-custom)
683
684 (defcustom gnus-treat-hide-boring-headers nil
685   "Hide boring headers.
686 Valid values are nil, t, `head', `last', an integer or a predicate.
687 See the manual for details."
688   :group 'gnus-article-treat
689   :type gnus-article-treat-head-custom)
690
691 (defcustom gnus-treat-hide-signature nil
692   "Hide the signature.
693 Valid values are nil, t, `head', `last', an integer or a predicate.
694 See the manual for details."
695   :group 'gnus-article-treat
696   :type gnus-article-treat-custom)
697
698 (defcustom gnus-treat-fill-article nil
699   "Fill the article.
700 Valid values are nil, t, `head', `last', an integer or a predicate.
701 See the manual for details."
702   :group 'gnus-article-treat
703   :type gnus-article-treat-custom)
704
705 (defcustom gnus-treat-hide-citation nil
706   "Hide cited text.
707 Valid values are nil, t, `head', `last', an integer or a predicate.
708 See the manual for details."
709   :group 'gnus-article-treat
710   :type gnus-article-treat-custom)
711
712 (defcustom gnus-treat-strip-pgp t
713   "Strip PGP signatures.
714 Valid values are nil, t, `head', `last', an integer or a predicate.
715 See the manual for details."
716   :group 'gnus-article-treat
717   :type gnus-article-treat-custom)
718
719 (defcustom gnus-treat-strip-pem nil
720   "Strip PEM signatures.
721 Valid values are nil, t, `head', `last', an integer or a predicate.
722 See the manual for details."
723   :group 'gnus-article-treat
724   :type gnus-article-treat-custom)
725
726 (defcustom gnus-treat-strip-banner t
727   "Strip banners from articles.
728 The banner to be stripped is specified in the `banner' group parameter.
729 Valid values are nil, t, `head', `last', an integer or a predicate.
730 See the manual for details."
731   :group 'gnus-article-treat
732   :type gnus-article-treat-custom)
733
734 (defcustom gnus-treat-highlight-headers 'head
735   "Highlight the headers.
736 Valid values are nil, t, `head', `last', an integer or a predicate.
737 See the manual for details."
738   :group 'gnus-article-treat
739   :type gnus-article-treat-head-custom)
740 (put 'gnus-treat-highlight-headers 'highlight t)
741
742 (defcustom gnus-treat-highlight-citation t
743   "Highlight cited text.
744 Valid values are nil, t, `head', `last', an integer or a predicate.
745 See the manual for details."
746   :group 'gnus-article-treat
747   :type gnus-article-treat-custom)
748 (put 'gnus-treat-highlight-citation 'highlight t)
749
750 (defcustom gnus-treat-date-ut nil
751   "Display the Date in UT (GMT).
752 Valid values are nil, t, `head', `last', an integer or a predicate.
753 See the manual for details."
754   :group 'gnus-article-treat
755   :type gnus-article-treat-head-custom)
756
757 (defcustom gnus-treat-date-local nil
758   "Display the Date in the local timezone.
759 Valid values are nil, t, `head', `last', an integer or a predicate.
760 See the manual for details."
761   :group 'gnus-article-treat
762   :type gnus-article-treat-head-custom)
763
764 (defcustom gnus-treat-date-lapsed nil
765   "Display the Date header in a way that says how much time has elapsed.
766 Valid values are nil, t, `head', `last', an integer or a predicate.
767 See the manual for details."
768   :group 'gnus-article-treat
769   :type gnus-article-treat-head-custom)
770
771 (defcustom gnus-treat-date-original nil
772   "Display the date in the original timezone.
773 Valid values are nil, t, `head', `last', an integer or a predicate.
774 See the manual for details."
775   :group 'gnus-article-treat
776   :type gnus-article-treat-head-custom)
777
778 (defcustom gnus-treat-date-iso8601 nil
779   "Display the date in the ISO8601 format.
780 Valid values are nil, t, `head', `last', an integer or a predicate.
781 See the manual for details."
782   :group 'gnus-article-treat
783   :type gnus-article-treat-head-custom)
784
785 (defcustom gnus-treat-date-user-defined nil
786   "Display the date in a user-defined format.
787 The format is defined by the `gnus-article-time-format' variable.
788 Valid values are nil, t, `head', `last', an integer or a predicate.
789 See the manual for details."
790   :group 'gnus-article-treat
791   :type gnus-article-treat-head-custom)
792
793 (defcustom gnus-treat-strip-headers-in-body t
794   "Strip the X-No-Archive header line from the beginning of the body.
795 Valid values are nil, t, `head', `last', an integer or a predicate.
796 See the manual for details."
797   :group 'gnus-article-treat
798   :type gnus-article-treat-custom)
799
800 (defcustom gnus-treat-strip-trailing-blank-lines nil
801   "Strip trailing blank lines.
802 Valid values are nil, t, `head', `last', an integer or a predicate.
803 See the manual for details."
804   :group 'gnus-article-treat
805   :type gnus-article-treat-custom)
806
807 (defcustom gnus-treat-strip-leading-blank-lines nil
808   "Strip leading blank lines.
809 Valid values are nil, t, `head', `last', an integer or a predicate.
810 See the manual for details."
811   :group 'gnus-article-treat
812   :type gnus-article-treat-custom)
813
814 (defcustom gnus-treat-strip-multiple-blank-lines nil
815   "Strip multiple blank lines.
816 Valid values are nil, t, `head', `last', an integer or a predicate.
817 See the manual for details."
818   :group 'gnus-article-treat
819   :type gnus-article-treat-custom)
820
821 (defcustom gnus-treat-overstrike t
822   "Treat overstrike highlighting.
823 Valid values are nil, t, `head', `last', an integer or a predicate.
824 See the manual for details."
825   :group 'gnus-article-treat
826   :type gnus-article-treat-custom)
827 (put 'gnus-treat-overstrike 'highlight t)
828
829 (defcustom gnus-treat-display-xface (if (and gnus-xemacs (featurep 'xface))
830                                         'head nil)
831   "Display X-Face headers.
832 Valid values are nil, t, `head', `last', an integer or a predicate.
833 See the manual for details."
834   :group 'gnus-article-treat
835   :type gnus-article-treat-head-custom)
836 (put 'gnus-treat-display-xface 'highlight t)
837
838 (defcustom gnus-treat-display-smileys (if (and gnus-xemacs
839                                                (featurep 'xpm))
840                                           t nil)
841   "Display smileys.
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-custom)
846 (put 'gnus-treat-display-smileys 'highlight t)
847
848 (defcustom gnus-treat-display-picons (if gnus-xemacs 'head nil)
849   "Display picons.
850 Valid values are nil, t, `head', `last', an integer or a predicate.
851 See the manual for details."
852   :group 'gnus-article-treat
853   :type gnus-article-treat-head-custom)
854 (put 'gnus-treat-display-picons 'highlight t)
855
856 (defcustom gnus-treat-capitalize-sentences nil
857   "Capitalize sentence-starting words.
858 Valid values are nil, t, `head', `last', an integer or a predicate.
859 See the manual for details."
860   :group 'gnus-article-treat
861   :type gnus-article-treat-custom)
862
863 (defcustom gnus-treat-fill-long-lines nil
864   "Fill long lines.
865 Valid values are nil, t, `head', `last', an integer or a predicate.
866 See the manual for details."
867   :group 'gnus-article-treat
868   :type gnus-article-treat-custom)
869
870 (defcustom gnus-treat-play-sounds nil
871   "Play sounds.
872 Valid values are nil, t, `head', `last', an integer or a predicate.
873 See the manual for details."
874   :group 'gnus-article-treat
875   :type gnus-article-treat-custom)
876
877 (defcustom gnus-treat-translate nil
878   "Translate articles from one language to another.
879 Valid values are nil, t, `head', `last', an integer or a predicate.
880 See the manual for details."
881   :group 'gnus-article-treat
882   :type gnus-article-treat-custom)
883
884 ;;; Internal variables
885
886 (defvar article-goto-body-goes-to-point-min-p nil)
887 (defvar gnus-article-wash-types nil)
888 (defvar gnus-article-emphasis-alist nil)
889
890 (defvar gnus-article-mime-handle-alist-1 nil)
891 (defvar gnus-treatment-function-alist
892   '((gnus-treat-strip-banner gnus-article-strip-banner)
893     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
894     (gnus-treat-highlight-signature gnus-article-highlight-signature)
895     (gnus-treat-buttonize gnus-article-add-buttons)
896     (gnus-treat-fill-article gnus-article-fill-cited-article)
897     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
898     (gnus-treat-strip-cr gnus-article-remove-cr)
899     (gnus-treat-emphasize gnus-article-emphasize)
900     (gnus-treat-display-xface gnus-article-display-x-face)
901     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
902     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
903     (gnus-treat-hide-signature gnus-article-hide-signature)
904     (gnus-treat-hide-citation gnus-article-hide-citation)
905     (gnus-treat-strip-pgp gnus-article-hide-pgp)
906     (gnus-treat-strip-pem gnus-article-hide-pem)
907     (gnus-treat-highlight-headers gnus-article-highlight-headers)
908     (gnus-treat-highlight-citation gnus-article-highlight-citation)
909     (gnus-treat-highlight-signature gnus-article-highlight-signature)
910     (gnus-treat-date-ut gnus-article-date-ut)
911     (gnus-treat-date-local gnus-article-date-local)
912     (gnus-treat-date-lapsed gnus-article-date-lapsed)
913     (gnus-treat-date-original gnus-article-date-original)
914     (gnus-treat-date-user-defined gnus-article-date-user)
915     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
916     (gnus-treat-strip-trailing-blank-lines
917      gnus-article-remove-trailing-blank-lines)
918     (gnus-treat-strip-leading-blank-lines
919      gnus-article-strip-leading-blank-lines)
920     (gnus-treat-strip-multiple-blank-lines
921      gnus-article-strip-multiple-blank-lines)
922     (gnus-treat-overstrike gnus-article-treat-overstrike)
923     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
924     (gnus-treat-display-smileys gnus-smiley-display)
925     (gnus-treat-display-picons gnus-article-display-picons)
926     (gnus-treat-play-sounds gnus-earcon-display)))
927
928 (defvar gnus-article-mime-handle-alist nil)
929 (defvar article-lapsed-timer nil)
930 (defvar gnus-article-current-summary nil)
931
932 (defvar gnus-article-mode-syntax-table
933   (let ((table (copy-syntax-table text-mode-syntax-table)))
934     (modify-syntax-entry ?- "w" table)
935     (modify-syntax-entry ?> ")" table)
936     (modify-syntax-entry ?< "(" table)
937     table)
938   "Syntax table used in article mode buffers.
939 Initialized from `text-mode-syntax-table.")
940
941 (defvar gnus-save-article-buffer nil)
942
943 (defvar gnus-article-mode-line-format-alist
944   (nconc '((?w (gnus-article-wash-status) ?s)
945            (?m (gnus-article-mime-part-status) ?s))
946          gnus-summary-mode-line-format-alist))
947
948 (defvar gnus-number-of-articles-to-be-saved nil)
949
950 (defvar gnus-inhibit-hiding nil)
951
952 (defsubst gnus-article-hide-text (b e props)
953   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
954   (add-text-properties b e props)
955   (when (memq 'intangible props)
956     (put-text-property
957      (max (1- b) (point-min))
958      b 'intangible (cddr (memq 'intangible props)))))
959 (defsubst gnus-article-unhide-text (b e)
960   "Remove hidden text properties from region between B and E."
961   (remove-text-properties b e gnus-hidden-properties)
962   (when (memq 'intangible gnus-hidden-properties)
963     (put-text-property (max (1- b) (point-min))
964                        b 'intangible nil)))
965
966 (defun gnus-article-hide-text-type (b e type)
967   "Hide text of TYPE between B and E."
968   (push type gnus-article-wash-types)
969   (gnus-article-hide-text
970    b e (cons 'article-type (cons type gnus-hidden-properties))))
971
972 (defun gnus-article-unhide-text-type (b e type)
973   "Unhide text of TYPE between B and E."
974   (setq gnus-article-wash-types
975         (delq type gnus-article-wash-types))
976   (remove-text-properties
977    b e (cons 'article-type (cons type gnus-hidden-properties)))
978   (when (memq 'intangible gnus-hidden-properties)
979     (put-text-property (max (1- b) (point-min))
980                        b 'intangible nil)))
981
982 (defun gnus-article-hide-text-of-type (type)
983   "Hide text of TYPE in the current buffer."
984   (save-excursion
985     (let ((b (point-min))
986           (e (point-max)))
987       (while (setq b (text-property-any b e 'article-type type))
988         (add-text-properties b (incf b) gnus-hidden-properties)))))
989
990 (defun gnus-article-delete-text-of-type (type)
991   "Delete text of TYPE in the current buffer."
992   (save-excursion
993     (let ((b (point-min)))
994       (while (setq b (text-property-any b (point-max) 'article-type type))
995         (delete-region
996          b (or (text-property-not-all b (point-max) 'article-type type)
997                (point-max)))))))
998
999 (defun gnus-article-delete-invisible-text ()
1000   "Delete all invisible text in the current buffer."
1001   (save-excursion
1002     (let ((b (point-min)))
1003       (while (setq b (text-property-any b (point-max) 'invisible t))
1004         (delete-region
1005          b (or (text-property-not-all b (point-max) 'invisible t)
1006                (point-max)))))))
1007
1008 (defun gnus-article-text-type-exists-p (type)
1009   "Say whether any text of type TYPE exists in the buffer."
1010   (text-property-any (point-min) (point-max) 'article-type type))
1011
1012 (defsubst gnus-article-header-rank ()
1013   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1014   (let ((list gnus-sorted-header-list)
1015         (i 0))
1016     (while list
1017       (when (looking-at (car list))
1018         (setq list nil))
1019       (setq list (cdr list))
1020       (incf i))
1021     i))
1022
1023 (defun article-hide-headers (&optional arg delete)
1024   "Hide unwanted headers and possibly sort them as well."
1025   (interactive)
1026   ;; This function might be inhibited.
1027   (unless gnus-inhibit-hiding
1028     (save-excursion
1029       (save-restriction
1030         (let ((buffer-read-only nil)
1031               (case-fold-search t)
1032               (max (1+ (length gnus-sorted-header-list)))
1033               (ignored (when (not gnus-visible-headers)
1034                          (cond ((stringp gnus-ignored-headers)
1035                                 gnus-ignored-headers)
1036                                ((listp gnus-ignored-headers)
1037                                 (mapconcat 'identity gnus-ignored-headers
1038                                            "\\|")))))
1039               (visible
1040                (cond ((stringp gnus-visible-headers)
1041                       gnus-visible-headers)
1042                      ((and gnus-visible-headers
1043                            (listp gnus-visible-headers))
1044                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1045               (inhibit-point-motion-hooks t)
1046               beg)
1047           ;; First we narrow to just the headers.
1048           (article-narrow-to-head)
1049           ;; Hide any "From " lines at the beginning of (mail) articles.
1050           (while (looking-at "From ")
1051             (forward-line 1))
1052           (unless (bobp)
1053             (delete-region (point-min) (point)))
1054           ;; Then treat the rest of the header lines.
1055           ;; Then we use the two regular expressions
1056           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1057           ;; select which header lines is to remain visible in the
1058           ;; article buffer.
1059           (while (re-search-forward "^[^ \t]*:" nil t)
1060             (beginning-of-line)
1061             ;; Mark the rank of the header.
1062             (put-text-property
1063              (point) (1+ (point)) 'message-rank
1064              (if (or (and visible (looking-at visible))
1065                      (and ignored
1066                           (not (looking-at ignored))))
1067                  (gnus-article-header-rank)
1068                (+ 2 max)))
1069             (forward-line 1))
1070           (message-sort-headers-1)
1071           (when (setq beg (text-property-any
1072                            (point-min) (point-max) 'message-rank (+ 2 max)))
1073             ;; We delete the unwanted headers.
1074             (add-text-properties (point-min) (+ 5 (point-min))
1075                                  '(article-type headers dummy-invisible t))
1076             (delete-region beg (point-max))))))))
1077
1078 (defun article-hide-boring-headers (&optional arg)
1079   "Toggle hiding of headers that aren't very interesting.
1080 If given a negative prefix, always show; if given a positive prefix,
1081 always hide."
1082   (interactive (gnus-article-hidden-arg))
1083   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1084              (not gnus-show-all-headers))
1085     (save-excursion
1086       (save-restriction
1087         (let ((buffer-read-only nil)
1088               (list gnus-boring-article-headers)
1089               (inhibit-point-motion-hooks t)
1090               elem)
1091           (article-narrow-to-head)
1092           (while list
1093             (setq elem (pop list))
1094             (goto-char (point-min))
1095             (cond
1096              ;; Hide empty headers.
1097              ((eq elem 'empty)
1098               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1099                 (forward-line -1)
1100                 (gnus-article-hide-text-type
1101                  (progn (beginning-of-line) (point))
1102                  (progn
1103                    (end-of-line)
1104                    (if (re-search-forward "^[^ \t]" nil t)
1105                        (match-beginning 0)
1106                      (point-max)))
1107                  'boring-headers)))
1108              ;; Hide boring Newsgroups header.
1109              ((eq elem 'newsgroups)
1110               (when (equal (gnus-fetch-field "newsgroups")
1111                            (gnus-group-real-name
1112                             (if (boundp 'gnus-newsgroup-name)
1113                                 gnus-newsgroup-name
1114                               "")))
1115                 (gnus-article-hide-header "newsgroups")))
1116              ((eq elem 'followup-to)
1117               (when (equal (message-fetch-field "followup-to")
1118                            (message-fetch-field "newsgroups"))
1119                 (gnus-article-hide-header "followup-to")))
1120              ((eq elem 'reply-to)
1121               (let ((from (message-fetch-field "from"))
1122                     (reply-to (message-fetch-field "reply-to")))
1123                 (when (and
1124                        from reply-to
1125                        (ignore-errors
1126                          (equal
1127                           (nth 1 (mail-extract-address-components from))
1128                           (nth 1 (mail-extract-address-components reply-to)))))
1129                   (gnus-article-hide-header "reply-to"))))
1130              ((eq elem 'date)
1131               (let ((date (message-fetch-field "date")))
1132                 (when (and date
1133                            (< (days-between (current-time-string) date)
1134                               4))
1135                   (gnus-article-hide-header "date"))))
1136              ((eq elem 'long-to)
1137               (let ((to (message-fetch-field "to")))
1138                 (when (> (length to) 1024)
1139                   (gnus-article-hide-header "to"))))
1140              ((eq elem 'many-to)
1141               (let ((to-count 0))
1142                 (goto-char (point-min))
1143                 (while (re-search-forward "^to:" nil t)
1144                   (setq to-count (1+ to-count)))
1145                 (when (> to-count 1)
1146                   (while (> to-count 0)
1147                     (goto-char (point-min))
1148                     (save-restriction
1149                       (re-search-forward "^to:" nil nil to-count)
1150                       (forward-line -1)
1151                       (narrow-to-region (point) (point-max))
1152                       (gnus-article-hide-header "to"))
1153                     (setq to-count (1- to-count)))))))))))))
1154
1155 (defun gnus-article-hide-header (header)
1156   (save-excursion
1157     (goto-char (point-min))
1158     (when (re-search-forward (concat "^" header ":") nil t)
1159       (gnus-article-hide-text-type
1160        (progn (beginning-of-line) (point))
1161        (progn
1162          (end-of-line)
1163          (if (re-search-forward "^[^ \t]" nil t)
1164              (match-beginning 0)
1165            (point-max)))
1166        'boring-headers))))
1167
1168 (defvar gnus-article-normalized-header-length 40
1169   "Length of normalized headers.")
1170
1171 (defun article-normalize-headers ()
1172   "Make all header lines 40 characters long."
1173   (interactive)
1174   (let ((buffer-read-only nil)
1175         column)
1176     (save-excursion
1177       (save-restriction
1178         (article-narrow-to-head)
1179         (while (not (eobp))
1180           (cond
1181            ((< (setq column (- (gnus-point-at-eol) (point)))
1182                gnus-article-normalized-header-length)
1183             (end-of-line)
1184             (insert (make-string
1185                      (- gnus-article-normalized-header-length column)
1186                      ? )))
1187            ((> column gnus-article-normalized-header-length)
1188             (gnus-put-text-property
1189              (progn
1190                (forward-char gnus-article-normalized-header-length)
1191                (point))
1192              (gnus-point-at-eol)
1193              'invisible t))
1194            (t
1195             ;; Do nothing.
1196             ))
1197           (forward-line 1))))))
1198
1199 (defun article-treat-dumbquotes ()
1200   "Translate M******** sm*rtq**t*s into proper text.
1201 Note that this function guesses whether a character is a sm*rtq**t* or
1202 not, so it should only be used interactively."
1203   (interactive)
1204   (article-translate-strings gnus-article-dumbquotes-map))
1205
1206 (defun article-translate-characters (from to)
1207   "Translate all characters in the body of the article according to FROM and TO.
1208 FROM is a string of characters to translate from; to is a string of
1209 characters to translate to."
1210   (save-excursion
1211     (when (article-goto-body)
1212       (let ((buffer-read-only nil)
1213             (x (make-string 225 ?x))
1214             (i -1))
1215         (while (< (incf i) (length x))
1216           (aset x i i))
1217         (setq i 0)
1218         (while (< i (length from))
1219           (aset x (aref from i) (aref to i))
1220           (incf i))
1221         (translate-region (point) (point-max) x)))))
1222
1223 (defun article-translate-strings (map)
1224   "Translate all string in the body of the article according to MAP.
1225 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1226   (save-excursion
1227     (when (article-goto-body)
1228       (let ((buffer-read-only nil)
1229             elem)
1230         (while (setq elem (pop map))
1231           (save-excursion
1232             (while (search-forward (car elem) nil t)
1233               (replace-match (cadr elem)))))))))
1234
1235 (defun article-treat-overstrike ()
1236   "Translate overstrikes into bold text."
1237   (interactive)
1238   (save-excursion
1239     (when (article-goto-body)
1240       (let ((buffer-read-only nil))
1241         (while (search-forward "\b" nil t)
1242           (let ((next (char-after))
1243                 (previous (char-after (- (point) 2))))
1244             ;; We do the boldification/underlining by hiding the
1245             ;; overstrikes and putting the proper text property
1246             ;; on the letters.
1247             (cond
1248              ((eq next previous)
1249               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1250               (put-text-property (point) (1+ (point)) 'face 'bold))
1251              ((eq next ?_)
1252               (gnus-article-hide-text-type
1253                (1- (point)) (1+ (point)) 'overstrike)
1254               (put-text-property
1255                (- (point) 2) (1- (point)) 'face 'underline))
1256              ((eq previous ?_)
1257               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1258               (put-text-property
1259                (point) (1+ (point)) 'face 'underline)))))))))
1260
1261 (defun article-fill-long-lines ()
1262   "Fill lines that are wider than the window width."
1263   (interactive)
1264   (save-excursion
1265     (let ((buffer-read-only nil)
1266           (width (window-width (get-buffer-window (current-buffer)))))
1267       (save-restriction
1268         (article-goto-body)
1269         (let ((adaptive-fill-mode nil))
1270           (while (not (eobp))
1271             (end-of-line)
1272             (when (>= (current-column) (min fill-column width))
1273               (narrow-to-region (point) (gnus-point-at-bol))
1274               (fill-paragraph nil)
1275               (goto-char (point-max))
1276               (widen))
1277             (forward-line 1)))))))
1278
1279 (defun article-capitalize-sentences ()
1280   "Capitalize the first word in each sentence."
1281   (interactive)
1282   (save-excursion
1283     (let ((buffer-read-only nil)
1284           (paragraph-start "^[\n\^L]"))
1285       (article-goto-body)
1286       (while (not (eobp))
1287         (capitalize-word 1)
1288         (forward-sentence)))))
1289
1290 (defun article-remove-cr ()
1291   "Remove trailing CRs and then translate remaining CRs into LFs."
1292   (interactive)
1293   (save-excursion
1294     (let ((buffer-read-only nil))
1295       (goto-char (point-min))
1296       (while (re-search-forward "\r+$" nil t)
1297         (replace-match "" t t))
1298       (goto-char (point-min))
1299       (while (search-forward "\r" nil t)
1300         (replace-match "\n" t t)))))
1301
1302 (defun article-remove-trailing-blank-lines ()
1303   "Remove all trailing blank lines from the article."
1304   (interactive)
1305   (save-excursion
1306     (let ((buffer-read-only nil))
1307       (goto-char (point-max))
1308       (delete-region
1309        (point)
1310        (progn
1311          (while (and (not (bobp))
1312                      (looking-at "^[ \t]*$")
1313                      (not (gnus-annotation-in-region-p
1314                            (point) (gnus-point-at-eol))))
1315            (forward-line -1))
1316          (forward-line 1)
1317          (point))))))
1318
1319 (defun article-display-x-face (&optional force)
1320   "Look for an X-Face header and display it if present."
1321   (interactive (list 'force))
1322   (save-excursion
1323     ;; Delete the old process, if any.
1324     (when (process-status "article-x-face")
1325       (delete-process "article-x-face"))
1326     (let ((inhibit-point-motion-hooks t)
1327           (case-fold-search t)
1328           from last)
1329       (save-restriction
1330         (article-narrow-to-head)
1331         (goto-char (point-min))
1332         (setq from (message-fetch-field "from"))
1333         (goto-char (point-min))
1334         (while (and gnus-article-x-face-command
1335                     (not last)
1336                     (or force
1337                         ;; Check whether this face is censored.
1338                         (not gnus-article-x-face-too-ugly)
1339                         (and gnus-article-x-face-too-ugly from
1340                              (not (string-match gnus-article-x-face-too-ugly
1341                                                 from))))
1342                     ;; Has to be present.
1343                     (re-search-forward "^X-Face: " nil t))
1344           ;; This used to try to do multiple faces (`while' instead of
1345           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1346           ;; work (b) it can crash some versions of Emacs (c) are
1347           ;; multiple faces really something to encourage?
1348           (when (stringp gnus-article-x-face-command)
1349             (setq last t))
1350           ;; We now have the area of the buffer where the X-Face is stored.
1351           (save-excursion
1352             (let ((beg (point))
1353                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1354               ;; We display the face.
1355               (if (symbolp gnus-article-x-face-command)
1356                   ;; The command is a lisp function, so we call it.
1357                   (if (gnus-functionp gnus-article-x-face-command)
1358                       (funcall gnus-article-x-face-command beg end)
1359                     (error "%s is not a function" gnus-article-x-face-command))
1360                 ;; The command is a string, so we interpret the command
1361                 ;; as a, well, command, and fork it off.
1362                 (let ((process-connection-type nil))
1363                   (process-kill-without-query
1364                    (start-process
1365                     "article-x-face" nil shell-file-name shell-command-switch
1366                     gnus-article-x-face-command))
1367                   (process-send-region "article-x-face" beg end)
1368                   (process-send-eof "article-x-face"))))))))))
1369
1370 (defun article-decode-mime-words ()
1371   "Decode all MIME-encoded words in the article."
1372   (interactive)
1373   (save-excursion
1374     (set-buffer gnus-article-buffer)
1375     (let ((inhibit-point-motion-hooks t)
1376           buffer-read-only
1377           (mail-parse-charset gnus-newsgroup-charset)
1378           (mail-parse-ignored-charsets 
1379            (save-excursion (set-buffer gnus-summary-buffer)
1380                            gnus-newsgroup-ignored-charsets)))
1381       (mail-decode-encoded-word-region (point-min) (point-max)))))
1382
1383 (defun article-decode-charset (&optional prompt)
1384   "Decode charset-encoded text in the article.
1385 If PROMPT (the prefix), prompt for a coding system to use."
1386   (interactive "P")
1387   (save-excursion
1388     (save-restriction
1389       (article-narrow-to-head)
1390       (let* ((inhibit-point-motion-hooks t)
1391              (case-fold-search t)
1392              (ct (message-fetch-field "Content-Type" t))
1393              (cte (message-fetch-field "Content-Transfer-Encoding" t))
1394              (ctl (and ct (ignore-errors
1395                             (mail-header-parse-content-type ct))))
1396              (charset (cond
1397                        (prompt
1398                         (mm-read-coding-system "Charset to decode: "))
1399                        (ctl
1400                         (mail-content-type-get ctl 'charset))))
1401              (mail-parse-charset gnus-newsgroup-charset)
1402              (mail-parse-ignored-charsets 
1403               (save-excursion (set-buffer gnus-summary-buffer)
1404                               gnus-newsgroup-ignored-charsets))
1405              buffer-read-only)
1406         (goto-char (point-max))
1407         (widen)
1408         (forward-line 1)
1409         (narrow-to-region (point) (point-max))
1410         (when (and (or (not ctl)
1411                        (equal (car ctl) "text/plain"))
1412                    (not (mm-uu-test)))
1413           (mm-decode-body
1414            charset (and cte (intern (downcase
1415                                      (gnus-strip-whitespace cte))))
1416            (car ctl)))))))
1417
1418 (defun article-decode-encoded-words ()
1419   "Remove encoded-word encoding from headers."
1420   (let ((inhibit-point-motion-hooks t)
1421         (mail-parse-charset gnus-newsgroup-charset)
1422         (mail-parse-ignored-charsets 
1423          (save-excursion (set-buffer gnus-summary-buffer)
1424                          gnus-newsgroup-ignored-charsets))
1425         buffer-read-only)
1426     (save-restriction
1427       (article-narrow-to-head)
1428       (funcall gnus-decode-header-function (point-min) (point-max)))))
1429
1430 (defun article-de-quoted-unreadable (&optional force)
1431   "Translate a quoted-printable-encoded article.
1432 If FORCE, decode the article whether it is marked as quoted-printable
1433 or not."
1434   (interactive (list 'force))
1435   (save-excursion
1436     (let ((buffer-read-only nil)
1437           (type (gnus-fetch-field "content-transfer-encoding"))
1438           (charset gnus-newsgroup-charset))
1439       (when (or force
1440                 (and type (string-match "quoted-printable" (downcase type))))
1441         (article-goto-body)
1442         (save-restriction
1443           (narrow-to-region (point) (point-max))
1444           (quoted-printable-decode-region (point-min) (point-max))
1445           (when charset
1446             (mm-decode-body charset)))))))
1447
1448 (defun article-hide-pgp ()
1449   "Remove any PGP headers and signatures in the current article."
1450   (interactive)
1451   (save-excursion
1452     (save-restriction
1453       (let ((inhibit-point-motion-hooks t)
1454             buffer-read-only beg end)
1455         (article-goto-body)
1456         ;; Hide the "header".
1457         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1458           (push 'pgp gnus-article-wash-types)
1459           (delete-region (match-beginning 0) (match-end 0))
1460           ;; PGP 5 and GNU PG add a `Hash: <>' comment, hide that too
1461           (when (looking-at "Hash:.*$")
1462             (delete-region (point) (1+ (gnus-point-at-eol))))
1463           (setq beg (point))
1464           ;; Hide the actual signature.
1465           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1466                (setq end (1+ (match-beginning 0)))
1467                (delete-region
1468                 end
1469                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1470                     (match-end 0)
1471                   ;; Perhaps we shouldn't hide to the end of the buffer
1472                   ;; if there is no end to the signature?
1473                   (point-max))))
1474           ;; Hide "- " PGP quotation markers.
1475           (when (and beg end)
1476             (narrow-to-region beg end)
1477             (goto-char (point-min))
1478             (while (re-search-forward "^- " nil t)
1479               (delete-region
1480                (match-beginning 0) (match-end 0)))
1481             (widen))
1482           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1483
1484 (defun article-hide-pem (&optional arg)
1485   "Toggle hiding of any PEM headers and signatures in the current article.
1486 If given a negative prefix, always show; if given a positive prefix,
1487 always hide."
1488   (interactive (gnus-article-hidden-arg))
1489   (unless (gnus-article-check-hidden-text 'pem arg)
1490     (save-excursion
1491       (let (buffer-read-only end)
1492         (goto-char (point-min))
1493         ;; Hide the horrendously ugly "header".
1494         (when (and (search-forward
1495                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1496                     nil t)
1497                    (setq end (1+ (match-beginning 0))))
1498           (push 'pem gnus-article-wash-types)
1499           (gnus-article-hide-text-type
1500            end
1501            (if (search-forward "\n\n" nil t)
1502                (match-end 0)
1503              (point-max))
1504            'pem)
1505           ;; Hide the trailer as well
1506           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1507                                 nil t)
1508             (gnus-article-hide-text-type
1509              (match-beginning 0) (match-end 0) 'pem)))))))
1510
1511 (defun article-strip-banner ()
1512   "Strip the banner specified by the `banner' group parameter."
1513   (interactive)
1514   (save-excursion
1515     (save-restriction
1516       (let ((inhibit-point-motion-hooks t)
1517             (banner (gnus-group-get-parameter gnus-newsgroup-name 'banner))
1518             (gnus-signature-limit nil)
1519             buffer-read-only beg end)
1520         (when banner
1521           (article-goto-body)
1522           (cond
1523            ((eq banner 'signature)
1524             (when (gnus-article-narrow-to-signature)
1525               (widen)
1526               (forward-line -1)
1527               (delete-region (point) (point-max))))
1528            ((stringp banner)
1529             (while (re-search-forward banner nil t)
1530               (delete-region (match-beginning 0) (match-end 0))))))))))
1531
1532 (defun article-babel-prompt ()
1533   "Prompt for a babel translation."
1534   (require 'babel)
1535   (completing-read "Translate from: "
1536                    babel-translations nil t
1537                    (car (car babel-translations))
1538                    babel-history))
1539
1540 (defun article-babel (translation)
1541   "Translate article according to TRANSLATION using babelfish."
1542   (interactive (list (article-babel-prompt)))
1543   (require 'babel)
1544   (save-excursion
1545     (set-buffer gnus-article-buffer)
1546     (when (article-goto-body)
1547       (let* ((buffer-read-only nil)
1548              (start (point))
1549              (end (point-max))
1550              (msg (buffer-substring start end)))
1551         (save-restriction
1552           (narrow-to-region start end)
1553           (delete-region start end)
1554           (babel-fetch msg (cdr (assoc translation babel-translations)))
1555           (save-restriction
1556             (narrow-to-region start (point-max))
1557             (babel-wash)))))))
1558
1559 (defun article-hide-signature (&optional arg)
1560   "Hide the signature in the current article.
1561 If given a negative prefix, always show; if given a positive prefix,
1562 always hide."
1563   (interactive (gnus-article-hidden-arg))
1564   (unless (gnus-article-check-hidden-text 'signature arg)
1565     (save-excursion
1566       (save-restriction
1567         (let ((buffer-read-only nil))
1568           (when (gnus-article-narrow-to-signature)
1569             (gnus-article-hide-text-type
1570              (point-min) (point-max) 'signature)))))))
1571
1572 (defun article-strip-headers-in-body ()
1573   "Strip offensive headers from bodies."
1574   (interactive)
1575   (save-excursion
1576     (article-goto-body)
1577     (let ((case-fold-search t))
1578       (when (looking-at "x-no-archive:")
1579         (gnus-delete-line)))))
1580
1581 (defun article-strip-leading-blank-lines ()
1582   "Remove all blank lines from the beginning of the article."
1583   (interactive)
1584   (save-excursion
1585     (let ((inhibit-point-motion-hooks t)
1586           buffer-read-only)
1587       (when (article-goto-body)
1588         (while (and (not (eobp))
1589                     (looking-at "[ \t]*$"))
1590           (gnus-delete-line))))))
1591
1592 (defun article-narrow-to-head ()
1593   "Narrow the buffer to the head of the message.
1594 Point is left at the beginning of the narrowed-to region."
1595   (narrow-to-region
1596    (goto-char (point-min))
1597    (if (search-forward "\n\n" nil 1)
1598        (1- (point))
1599      (point-max)))
1600   (goto-char (point-min)))
1601
1602 (defun article-goto-body ()
1603   "Place point at the start of the body."
1604   (goto-char (point-min))
1605   (cond
1606    ;; This variable is only bound when dealing with separate
1607    ;; MIME body parts.
1608    (article-goto-body-goes-to-point-min-p
1609     t)
1610    ((search-forward "\n\n" nil t)
1611     t)
1612    (t
1613     (goto-char (point-max))
1614     nil)))
1615
1616 (defun article-strip-multiple-blank-lines ()
1617   "Replace consecutive blank lines with one empty line."
1618   (interactive)
1619   (save-excursion
1620     (let ((inhibit-point-motion-hooks t)
1621           buffer-read-only)
1622       ;; First make all blank lines empty.
1623       (article-goto-body)
1624       (while (re-search-forward "^[ \t]+$" nil t)
1625         (unless (gnus-annotation-in-region-p
1626                  (match-beginning 0) (match-end 0))
1627           (replace-match "" nil t)))
1628       ;; Then replace multiple empty lines with a single empty line.
1629       (article-goto-body)
1630       (while (re-search-forward "\n\n\n+" nil t)
1631         (unless (gnus-annotation-in-region-p
1632                  (match-beginning 0) (match-end 0))
1633           (replace-match "\n\n" t t))))))
1634
1635 (defun article-strip-leading-space ()
1636   "Remove all white space from the beginning of the lines in the article."
1637   (interactive)
1638   (save-excursion
1639     (let ((inhibit-point-motion-hooks t)
1640           buffer-read-only)
1641       (article-goto-body)
1642       (while (re-search-forward "^[ \t]+" nil t)
1643         (replace-match "" t t)))))
1644
1645 (defun article-strip-trailing-space ()
1646   "Remove all white space from the end of the lines in the article."
1647   (interactive)
1648   (save-excursion
1649     (let ((inhibit-point-motion-hooks t)
1650           buffer-read-only)
1651       (article-goto-body)
1652       (while (re-search-forward "[ \t]+$" nil t)
1653         (replace-match "" t t)))))
1654
1655 (defun article-strip-blank-lines ()
1656   "Strip leading, trailing and multiple blank lines."
1657   (interactive)
1658   (article-strip-leading-blank-lines)
1659   (article-remove-trailing-blank-lines)
1660   (article-strip-multiple-blank-lines))
1661
1662 (defun article-strip-all-blank-lines ()
1663   "Strip all blank lines."
1664   (interactive)
1665   (save-excursion
1666     (let ((inhibit-point-motion-hooks t)
1667           buffer-read-only)
1668       (article-goto-body)
1669       (while (re-search-forward "^[ \t]*\n" nil t)
1670         (replace-match "" t t)))))
1671
1672 (defun gnus-article-narrow-to-signature ()
1673   "Narrow to the signature; return t if a signature is found, else nil."
1674   (let ((inhibit-point-motion-hooks t))
1675     (when (gnus-article-search-signature)
1676       (forward-line 1)
1677       ;; Check whether we have some limits to what we consider
1678       ;; to be a signature.
1679       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1680                       (list gnus-signature-limit)))
1681             limit limited)
1682         (while (setq limit (pop limits))
1683           (if (or (and (integerp limit)
1684                        (< (- (point-max) (point)) limit))
1685                   (and (floatp limit)
1686                        (< (count-lines (point) (point-max)) limit))
1687                   (and (gnus-functionp limit)
1688                        (funcall limit))
1689                   (and (stringp limit)
1690                        (not (re-search-forward limit nil t))))
1691               ()                        ; This limit did not succeed.
1692             (setq limited t
1693                   limits nil)))
1694         (unless limited
1695           (narrow-to-region (point) (point-max))
1696           t)))))
1697
1698 (defun gnus-article-search-signature ()
1699   "Search the current buffer for the signature separator.
1700 Put point at the beginning of the signature separator."
1701   (let ((cur (point)))
1702     (goto-char (point-max))
1703     (if (if (stringp gnus-signature-separator)
1704             (re-search-backward gnus-signature-separator nil t)
1705           (let ((seps gnus-signature-separator))
1706             (while (and seps
1707                         (not (re-search-backward (car seps) nil t)))
1708               (pop seps))
1709             seps))
1710         t
1711       (goto-char cur)
1712       nil)))
1713
1714 (defun gnus-article-hidden-arg ()
1715   "Return the current prefix arg as a number, or 0 if no prefix."
1716   (list (if current-prefix-arg
1717             (prefix-numeric-value current-prefix-arg)
1718           0)))
1719
1720 (defun gnus-article-check-hidden-text (type arg)
1721   "Return nil if hiding is necessary.
1722 Arg can be nil or a number.  Nil and positive means hide, negative
1723 means show, 0 means toggle."
1724   (save-excursion
1725     (save-restriction
1726       (let ((hide (gnus-article-hidden-text-p type)))
1727         (cond
1728          ((or (null arg)
1729               (> arg 0))
1730           nil)
1731          ((< arg 0)
1732           (gnus-article-show-hidden-text type))
1733          (t
1734           (if (eq hide 'hidden)
1735               (gnus-article-show-hidden-text type)
1736             nil)))))))
1737
1738 (defun gnus-article-hidden-text-p (type)
1739   "Say whether the current buffer contains hidden text of type TYPE."
1740   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1741     (while (and pos
1742                 (not (get-text-property pos 'invisible))
1743                 (not (get-text-property pos 'dummy-invisible)))
1744       (setq pos
1745             (text-property-any (1+ pos) (point-max) 'article-type type)))
1746     (if pos
1747         'hidden
1748       nil)))
1749
1750 (defun gnus-article-show-hidden-text (type &optional hide)
1751   "Show all hidden text of type TYPE.
1752 If HIDE, hide the text instead."
1753   (save-excursion
1754     (let ((buffer-read-only nil)
1755           (inhibit-point-motion-hooks t)
1756           (end (point-min))
1757           beg)
1758       (while (setq beg (text-property-any end (point-max) 'article-type type))
1759         (goto-char beg)
1760         (setq end (or
1761                    (text-property-not-all beg (point-max) 'article-type type)
1762                    (point-max)))
1763         (if hide
1764             (gnus-article-hide-text beg end gnus-hidden-properties)
1765           (gnus-article-unhide-text beg end))
1766         (goto-char end))
1767       t)))
1768
1769 (defconst article-time-units
1770   `((year . ,(* 365.25 24 60 60))
1771     (week . ,(* 7 24 60 60))
1772     (day . ,(* 24 60 60))
1773     (hour . ,(* 60 60))
1774     (minute . 60)
1775     (second . 1))
1776   "Mapping from time units to seconds.")
1777
1778 (defun article-date-ut (&optional type highlight header)
1779   "Convert DATE date to universal time in the current article.
1780 If TYPE is `local', convert to local time; if it is `lapsed', output
1781 how much time has lapsed since DATE. For `lapsed', the value of
1782 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
1783 should replace the \"Date:\" one, or should be added below it."
1784   (interactive (list 'ut t))
1785   (let* ((header (or header
1786                      (message-fetch-field "date")
1787                      ""))
1788          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1789          (date-regexp
1790           (cond
1791            ((not gnus-article-date-lapsed-new-header)
1792             tdate-regexp)
1793            ((eq type 'lapsed)
1794             "^X-Sent:[ \t]")
1795            (t
1796             "^Date:[ \t]")))
1797          (date (if (vectorp header) (mail-header-date header)
1798                  header))
1799          (inhibit-point-motion-hooks t)
1800          (newline t)
1801          bface eface)
1802     (when (and date (not (string= date "")))
1803       (save-excursion
1804         (save-restriction
1805           (article-narrow-to-head)
1806           (when (re-search-forward tdate-regexp nil t)
1807             (setq bface (get-text-property (gnus-point-at-bol) 'face)
1808                   eface (get-text-property (1- (gnus-point-at-eol))
1809                                            'face))
1810             (forward-line 1))
1811           (goto-char (point-min))
1812           (let ((buffer-read-only nil))
1813             ;; Delete any old Date headers.
1814             (while (re-search-forward date-regexp nil t)
1815               (if newline
1816                   (delete-region (progn (beginning-of-line) (point))
1817                                  (progn (end-of-line) (point)))
1818                 (delete-region (progn (beginning-of-line) (point))
1819                                (progn (forward-line 1) (point))))
1820               (setq newline nil))
1821             (when (re-search-forward tdate-regexp nil t)
1822               (forward-line 1))
1823             (insert (article-make-date-line date (or type 'ut)))
1824             (when newline
1825               (insert "\n")
1826               (forward-line -1))
1827             ;; Do highlighting.
1828             (beginning-of-line)
1829             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1830               (put-text-property (match-beginning 1) (1+ (match-end 1))
1831                                  'face bface)
1832               (put-text-property (match-beginning 2) (match-end 2)
1833                                  'face eface))))))))
1834
1835 (defun article-make-date-line (date type)
1836   "Return a DATE line of TYPE."
1837   (let ((time (condition-case ()
1838                   (date-to-time date)
1839                 (error '(0 0)))))
1840     (cond
1841      ;; Convert to the local timezone.  We have to slap a
1842      ;; `condition-case' round the calls to the timezone
1843      ;; functions since they aren't particularly resistant to
1844      ;; buggy dates.
1845      ((eq type 'local)
1846       (let ((tz (car (current-time-zone))))
1847         (format "Date: %s %s%04d" (current-time-string time)
1848                 (if (> tz 0) "+" "-") (abs (/ tz 36)))))
1849      ;; Convert to Universal Time.
1850      ((eq type 'ut)
1851       (concat "Date: "
1852               (current-time-string
1853                (let* ((e (parse-time-string date))
1854                      (tm (apply 'encode-time e))
1855                      (ms (car tm))
1856                      (ls (- (cadr tm) (car (current-time-zone)))))
1857                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1858                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1859                        (t (list ms ls)))))
1860               " UT"))
1861      ;; Get the original date from the article.
1862      ((eq type 'original)
1863       (concat "Date: " (if (string-match "\n+$" date)
1864                            (substring date 0 (match-beginning 0))
1865                          date)))
1866      ;; Let the user define the format.
1867      ((eq type 'user)
1868       (if (gnus-functionp gnus-article-time-format)
1869           (funcall gnus-article-time-format time)
1870         (concat
1871          "Date: "
1872          (format-time-string gnus-article-time-format time))))
1873      ;; ISO 8601.
1874      ((eq type 'iso8601)
1875       (concat
1876        "Date: "
1877        (format-time-string "%Y%m%dT%H%M%S" time)))
1878      ;; Do an X-Sent lapsed format.
1879      ((eq type 'lapsed)
1880       ;; If the date is seriously mangled, the timezone functions are
1881       ;; liable to bug out, so we ignore all errors.
1882       (let* ((now (current-time))
1883              (real-time (subtract-time now time))
1884              (real-sec (and real-time
1885                             (+ (* (float (car real-time)) 65536)
1886                                (cadr real-time))))
1887              (sec (and real-time (abs real-sec)))
1888              num prev)
1889         (cond
1890          ((null real-time)
1891           "X-Sent: Unknown")
1892          ((zerop sec)
1893           "X-Sent: Now")
1894          (t
1895           (concat
1896            "X-Sent: "
1897            ;; This is a bit convoluted, but basically we go
1898            ;; through the time units for years, weeks, etc,
1899            ;; and divide things to see whether that results
1900            ;; in positive answers.
1901            (mapconcat
1902             (lambda (unit)
1903               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1904                   ;; The (remaining) seconds are too few to
1905                   ;; be divided into this time unit.
1906                   ""
1907                 ;; It's big enough, so we output it.
1908                 (setq sec (- sec (* num (cdr unit))))
1909                 (prog1
1910                     (concat (if prev ", " "") (int-to-string
1911                                                (floor num))
1912                             " " (symbol-name (car unit))
1913                             (if (> num 1) "s" ""))
1914                   (setq prev t))))
1915             article-time-units "")
1916            ;; If dates are odd, then it might appear like the
1917            ;; article was sent in the future.
1918            (if (> real-sec 0)
1919                " ago"
1920              " in the future"))))))
1921      (t
1922       (error "Unknown conversion type: %s" type)))))
1923
1924 (defun article-date-local (&optional highlight)
1925   "Convert the current article date to the local timezone."
1926   (interactive (list t))
1927   (article-date-ut 'local highlight))
1928
1929 (defun article-date-original (&optional highlight)
1930   "Convert the current article date to what it was originally.
1931 This is only useful if you have used some other date conversion
1932 function and want to see what the date was before converting."
1933   (interactive (list t))
1934   (article-date-ut 'original highlight))
1935
1936 (defun article-date-lapsed (&optional highlight)
1937   "Convert the current article date to time lapsed since it was sent."
1938   (interactive (list t))
1939   (article-date-ut 'lapsed highlight))
1940
1941 (defun article-update-date-lapsed ()
1942   "Function to be run from a timer to update the lapsed time line."
1943   (let (deactivate-mark)
1944     (save-excursion
1945       (ignore-errors
1946         (walk-windows
1947          (lambda (w)
1948            (set-buffer (window-buffer w))
1949            (when (eq major-mode 'gnus-article-mode)
1950              (goto-char (point-min))
1951              (when (re-search-forward "^X-Sent:" nil t)
1952                (article-date-lapsed t))))
1953          nil 'visible)))))
1954
1955 (defun gnus-start-date-timer (&optional n)
1956   "Start a timer to update the X-Sent header in the article buffers.
1957 The numerical prefix says how frequently (in seconds) the function
1958 is to run."
1959   (interactive "p")
1960   (unless n
1961     (setq n 1))
1962   (gnus-stop-date-timer)
1963   (setq article-lapsed-timer
1964         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1965
1966 (defun gnus-stop-date-timer ()
1967   "Stop the X-Sent timer."
1968   (interactive)
1969   (when article-lapsed-timer
1970     (nnheader-cancel-timer article-lapsed-timer)
1971     (setq article-lapsed-timer nil)))
1972
1973 (defun article-date-user (&optional highlight)
1974   "Convert the current article date to the user-defined format.
1975 This format is defined by the `gnus-article-time-format' variable."
1976   (interactive (list t))
1977   (article-date-ut 'user highlight))
1978
1979 (defun article-date-iso8601 (&optional highlight)
1980   "Convert the current article date to ISO8601."
1981   (interactive (list t))
1982   (article-date-ut 'iso8601 highlight))
1983
1984 (defun article-show-all ()
1985   "Show all hidden text in the article buffer."
1986   (interactive)
1987   (save-excursion
1988     (let ((buffer-read-only nil))
1989       (gnus-article-unhide-text (point-min) (point-max)))))
1990
1991 (defun article-emphasize (&optional arg)
1992   "Emphasize text according to `gnus-emphasis-alist'."
1993   (interactive (gnus-article-hidden-arg))
1994   (unless (gnus-article-check-hidden-text 'emphasis arg)
1995     (save-excursion
1996       (let ((alist (or (with-current-buffer gnus-summary-buffer 
1997                          gnus-article-emphasis-alist) 
1998                        gnus-emphasis-alist))
1999             (buffer-read-only nil)
2000             (props (append '(article-type emphasis)
2001                            gnus-hidden-properties))
2002             regexp elem beg invisible visible face)
2003         (article-goto-body)
2004         (setq beg (point))
2005         (while (setq elem (pop alist))
2006           (goto-char beg)
2007           (setq regexp (car elem)
2008                 invisible (nth 1 elem)
2009                 visible (nth 2 elem)
2010                 face (nth 3 elem))
2011           (while (re-search-forward regexp nil t)
2012             (when (and (match-beginning visible) (match-beginning invisible))
2013               (gnus-article-hide-text
2014                (match-beginning invisible) (match-end invisible) props)
2015               (gnus-article-unhide-text-type
2016                (match-beginning visible) (match-end visible) 'emphasis)
2017               (gnus-put-text-property-excluding-newlines
2018                (match-beginning visible) (match-end visible) 'face face)
2019               (goto-char (match-end invisible)))))))))
2020
2021 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2022   "Setup newsgroup emphasis alist."
2023   (unless gnus-article-emphasis-alist
2024     (let ((name (and gnus-newsgroup-name
2025                      (gnus-group-real-name gnus-newsgroup-name))))
2026       (make-local-variable 'gnus-article-emphasis-alist)
2027       (setq gnus-article-emphasis-alist 
2028             (nconc 
2029              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2030                (while (setq elem (pop alist))
2031                  (when (and name (string-match (car elem) name))
2032                    (setq alist nil
2033                          highlight (copy-list (cdr elem)))))
2034                highlight)
2035              (copy-list highlight-words)
2036              (if gnus-newsgroup-name
2037                  (copy-list (gnus-group-find-parameter 
2038                              gnus-newsgroup-name 'highlight-words t)))
2039              gnus-emphasis-alist)))))
2040
2041 (defvar gnus-summary-article-menu)
2042 (defvar gnus-summary-post-menu)
2043
2044 ;;; Saving functions.
2045
2046 (defun gnus-article-save (save-buffer file &optional num)
2047   "Save the currently selected article."
2048   (unless gnus-save-all-headers
2049     ;; Remove headers according to `gnus-saved-headers'.
2050     (let ((gnus-visible-headers
2051            (or gnus-saved-headers gnus-visible-headers))
2052           (gnus-article-buffer save-buffer))
2053       (save-excursion
2054         (set-buffer save-buffer)
2055         (article-hide-headers 1 t))))
2056   (save-window-excursion
2057     (if (not gnus-default-article-saver)
2058         (error "No default saver is defined")
2059       ;; !!! Magic!  The saving functions all save
2060       ;; `gnus-save-article-buffer' (or so they think), but we
2061       ;; bind that variable to our save-buffer.
2062       (set-buffer gnus-article-buffer)
2063       (let* ((gnus-save-article-buffer save-buffer)
2064              (filename
2065               (cond
2066                ((not gnus-prompt-before-saving) 'default)
2067                ((eq gnus-prompt-before-saving 'always) nil)
2068                (t file)))
2069              (gnus-number-of-articles-to-be-saved
2070               (when (eq gnus-prompt-before-saving t)
2071                 num)))                  ; Magic
2072         (set-buffer gnus-article-current-summary)
2073         (funcall gnus-default-article-saver filename)))))
2074
2075 (defun gnus-read-save-file-name (prompt &optional filename
2076                                         function group headers variable)
2077   (let ((default-name
2078           (funcall function group headers (symbol-value variable)))
2079         result)
2080     (setq
2081      result
2082      (cond
2083       ((eq filename 'default)
2084        default-name)
2085       ((eq filename t)
2086        default-name)
2087       (filename filename)
2088       (t
2089        (let* ((split-name (gnus-get-split-value gnus-split-methods))
2090               (prompt
2091                (format prompt
2092                        (if (and gnus-number-of-articles-to-be-saved
2093                                 (> gnus-number-of-articles-to-be-saved 1))
2094                            (format "these %d articles"
2095                                    gnus-number-of-articles-to-be-saved)
2096                          "this article")))
2097               (file
2098                ;; Let the split methods have their say.
2099                (cond
2100                 ;; No split name was found.
2101                 ((null split-name)
2102                  (read-file-name
2103                   (concat prompt " (default "
2104                           (file-name-nondirectory default-name) ") ")
2105                   (file-name-directory default-name)
2106                   default-name))
2107                 ;; A single group name is returned.
2108                 ((stringp split-name)
2109                  (setq default-name
2110                        (funcall function split-name headers
2111                                 (symbol-value variable)))
2112                  (read-file-name
2113                   (concat prompt " (default "
2114                           (file-name-nondirectory default-name) ") ")
2115                   (file-name-directory default-name)
2116                   default-name))
2117                 ;; A single split name was found
2118                 ((= 1 (length split-name))
2119                  (let* ((name (expand-file-name
2120                                (car split-name) gnus-article-save-directory))
2121                         (dir (cond ((file-directory-p name)
2122                                     (file-name-as-directory name))
2123                                    ((file-exists-p name) name)
2124                                    (t gnus-article-save-directory))))
2125                    (read-file-name
2126                     (concat prompt " (default " name ") ")
2127                     dir name)))
2128                 ;; A list of splits was found.
2129                 (t
2130                  (setq split-name (nreverse split-name))
2131                  (let (result)
2132                    (let ((file-name-history
2133                           (nconc split-name file-name-history)))
2134                      (setq result
2135                            (expand-file-name
2136                             (read-file-name
2137                              (concat prompt " (`M-p' for defaults) ")
2138                              gnus-article-save-directory
2139                              (car split-name))
2140                             gnus-article-save-directory)))
2141                    (car (push result file-name-history)))))))
2142          ;; Create the directory.
2143          (gnus-make-directory (file-name-directory file))
2144          ;; If we have read a directory, we append the default file name.
2145          (when (file-directory-p file)
2146            (setq file (concat (file-name-as-directory file)
2147                               (file-name-nondirectory default-name))))
2148          ;; Possibly translate some characters.
2149          (nnheader-translate-file-chars file)))))
2150     (gnus-make-directory (file-name-directory result))
2151     (set variable result)))
2152
2153 (defun gnus-article-archive-name (group)
2154   "Return the first instance of an \"Archive-name\" in the current buffer."
2155   (let ((case-fold-search t))
2156     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2157       (nnheader-concat gnus-article-save-directory
2158                        (match-string 1)))))
2159
2160 (defun gnus-article-nndoc-name (group)
2161   "If GROUP is an nndoc group, return the name of the parent group."
2162   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2163     (gnus-group-get-parameter group 'save-article-group)))
2164
2165 (defun gnus-summary-save-in-rmail (&optional filename)
2166   "Append this article to Rmail file.
2167 Optional argument FILENAME specifies file name.
2168 Directory to save to is default to `gnus-article-save-directory'."
2169   (setq filename (gnus-read-save-file-name
2170                   "Save %s in rmail file:" filename
2171                   gnus-rmail-save-name gnus-newsgroup-name
2172                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2173   (gnus-eval-in-buffer-window gnus-save-article-buffer
2174     (save-excursion
2175       (save-restriction
2176         (widen)
2177         (gnus-output-to-rmail filename))))
2178   filename)
2179
2180 (defun gnus-summary-save-in-mail (&optional filename)
2181   "Append this article to Unix mail file.
2182 Optional argument FILENAME specifies file name.
2183 Directory to save to is default to `gnus-article-save-directory'."
2184   (setq filename (gnus-read-save-file-name
2185                   "Save %s in Unix mail file:" filename
2186                   gnus-mail-save-name gnus-newsgroup-name
2187                   gnus-current-headers 'gnus-newsgroup-last-mail))
2188   (gnus-eval-in-buffer-window gnus-save-article-buffer
2189     (save-excursion
2190       (save-restriction
2191         (widen)
2192         (if (and (file-readable-p filename)
2193                  (mail-file-babyl-p filename))
2194             (rmail-output-to-rmail-file filename t)
2195           (gnus-output-to-mail filename)))))
2196   filename)
2197
2198 (defun gnus-summary-save-in-file (&optional filename overwrite)
2199   "Append this article to file.
2200 Optional argument FILENAME specifies file name.
2201 Directory to save to is default to `gnus-article-save-directory'."
2202   (setq filename (gnus-read-save-file-name
2203                   "Save %s in file:" filename
2204                   gnus-file-save-name gnus-newsgroup-name
2205                   gnus-current-headers 'gnus-newsgroup-last-file))
2206   (gnus-eval-in-buffer-window gnus-save-article-buffer
2207     (save-excursion
2208       (save-restriction
2209         (widen)
2210         (when (and overwrite
2211                    (file-exists-p filename))
2212           (delete-file filename))
2213         (gnus-output-to-file filename))))
2214   filename)
2215
2216 (defun gnus-summary-write-to-file (&optional filename)
2217   "Write this article to a file.
2218 Optional argument FILENAME specifies file name.
2219 The directory to save in defaults to `gnus-article-save-directory'."
2220   (gnus-summary-save-in-file nil t))
2221
2222 (defun gnus-summary-save-body-in-file (&optional filename)
2223   "Append this article body to a file.
2224 Optional argument FILENAME specifies file name.
2225 The directory to save in defaults to `gnus-article-save-directory'."
2226   (setq filename (gnus-read-save-file-name
2227                   "Save %s body in file:" filename
2228                   gnus-file-save-name gnus-newsgroup-name
2229                   gnus-current-headers 'gnus-newsgroup-last-file))
2230   (gnus-eval-in-buffer-window gnus-save-article-buffer
2231     (save-excursion
2232       (save-restriction
2233         (widen)
2234         (when (article-goto-body)
2235           (narrow-to-region (point) (point-max)))
2236         (gnus-output-to-file filename))))
2237   filename)
2238
2239 (defun gnus-summary-save-in-pipe (&optional command)
2240   "Pipe this article to subprocess."
2241   (setq command
2242         (cond ((and (eq command 'default)
2243                     gnus-last-shell-command)
2244                gnus-last-shell-command)
2245               (command command)
2246               (t (read-string
2247                   (format
2248                    "Shell command on %s: "
2249                    (if (and gnus-number-of-articles-to-be-saved
2250                             (> gnus-number-of-articles-to-be-saved 1))
2251                        (format "these %d articles"
2252                                gnus-number-of-articles-to-be-saved)
2253                      "this article"))
2254                   gnus-last-shell-command))))
2255   (when (string-equal command "")
2256     (setq command gnus-last-shell-command))
2257   (gnus-eval-in-buffer-window gnus-article-buffer
2258     (save-restriction
2259       (widen)
2260       (shell-command-on-region (point-min) (point-max) command nil)))
2261   (setq gnus-last-shell-command command))
2262
2263 ;;; Article file names when saving.
2264
2265 (defun gnus-capitalize-newsgroup (newsgroup)
2266   "Capitalize NEWSGROUP name."
2267   (when (not (zerop (length newsgroup)))
2268     (concat (char-to-string (upcase (aref newsgroup 0)))
2269             (substring newsgroup 1))))
2270
2271 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2272   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2273 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2274 Otherwise, it is like ~/News/news/group/num."
2275   (let ((default
2276           (expand-file-name
2277            (concat (if (gnus-use-long-file-name 'not-save)
2278                        (gnus-capitalize-newsgroup newsgroup)
2279                      (gnus-newsgroup-directory-form newsgroup))
2280                    "/" (int-to-string (mail-header-number headers)))
2281            gnus-article-save-directory)))
2282     (if (and last-file
2283              (string-equal (file-name-directory default)
2284                            (file-name-directory last-file))
2285              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2286         default
2287       (or last-file default))))
2288
2289 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2290   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2291 If variable `gnus-use-long-file-name' is non-nil, it is
2292 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2293   (let ((default
2294           (expand-file-name
2295            (concat (if (gnus-use-long-file-name 'not-save)
2296                        newsgroup
2297                      (gnus-newsgroup-directory-form newsgroup))
2298                    "/" (int-to-string (mail-header-number headers)))
2299            gnus-article-save-directory)))
2300     (if (and last-file
2301              (string-equal (file-name-directory default)
2302                            (file-name-directory last-file))
2303              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2304         default
2305       (or last-file default))))
2306
2307 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2308   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2309 If variable `gnus-use-long-file-name' is non-nil, it is
2310 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2311   (or last-file
2312       (expand-file-name
2313        (if (gnus-use-long-file-name 'not-save)
2314            (gnus-capitalize-newsgroup newsgroup)
2315          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2316        gnus-article-save-directory)))
2317
2318 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2319   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2320 If variable `gnus-use-long-file-name' is non-nil, it is
2321 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2322   (or last-file
2323       (expand-file-name
2324        (if (gnus-use-long-file-name 'not-save)
2325            newsgroup
2326          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2327        gnus-article-save-directory)))
2328
2329 (eval-and-compile
2330   (mapcar
2331    (lambda (func)
2332      (let (afunc gfunc)
2333        (if (consp func)
2334            (setq afunc (car func)
2335                  gfunc (cdr func))
2336          (setq afunc func
2337                gfunc (intern (format "gnus-%s" func))))
2338        (fset gfunc
2339              (if (not (fboundp afunc))
2340                  nil
2341                `(lambda (&optional interactive &rest args)
2342                   ,(documentation afunc t)
2343                   (interactive (list t))
2344                   (save-excursion
2345                     (set-buffer gnus-article-buffer)
2346                     (if interactive
2347                         (call-interactively ',afunc)
2348                       (apply ',afunc args))))))))
2349    '(article-hide-headers
2350      article-hide-boring-headers
2351      article-treat-overstrike
2352      article-fill-long-lines
2353      article-capitalize-sentences
2354      article-remove-cr
2355      article-display-x-face
2356      article-de-quoted-unreadable
2357      article-mime-decode-quoted-printable
2358      article-hide-pgp
2359      article-strip-banner
2360      article-babel
2361      article-hide-pem
2362      article-hide-signature
2363      article-strip-headers-in-body
2364      article-remove-trailing-blank-lines
2365      article-strip-leading-blank-lines
2366      article-strip-multiple-blank-lines
2367      article-strip-leading-space
2368      article-strip-trailing-space
2369      article-strip-blank-lines
2370      article-strip-all-blank-lines
2371      article-date-local
2372      article-date-iso8601
2373      article-date-original
2374      article-date-ut
2375      article-decode-mime-words
2376      article-decode-charset
2377      article-decode-encoded-words
2378      article-date-user
2379      article-date-lapsed
2380      article-emphasize
2381      article-treat-dumbquotes
2382      article-normalize-headers
2383      (article-show-all . gnus-article-show-all-headers))))
2384 \f
2385 ;;;
2386 ;;; Gnus article mode
2387 ;;;
2388
2389 (put 'gnus-article-mode 'mode-class 'special)
2390
2391 (set-keymap-parent gnus-article-mode-map widget-keymap)
2392
2393 (gnus-define-keys gnus-article-mode-map
2394   " " gnus-article-goto-next-page
2395   "\177" gnus-article-goto-prev-page
2396   [delete] gnus-article-goto-prev-page
2397   [backspace] gnus-article-goto-prev-page
2398   "\C-c^" gnus-article-refer-article
2399   "h" gnus-article-show-summary
2400   "s" gnus-article-show-summary
2401   "\C-c\C-m" gnus-article-mail
2402   "?" gnus-article-describe-briefly
2403   "e" gnus-article-edit
2404   "<" beginning-of-buffer
2405   ">" end-of-buffer
2406   "\C-c\C-i" gnus-info-find-node
2407   "\C-c\C-b" gnus-bug
2408
2409   "\C-d" gnus-article-read-summary-keys
2410   "\M-*" gnus-article-read-summary-keys
2411   "\M-#" gnus-article-read-summary-keys
2412   "\M-^" gnus-article-read-summary-keys
2413   "\M-g" gnus-article-read-summary-keys)
2414
2415 (substitute-key-definition
2416  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2417
2418 (defun gnus-article-make-menu-bar ()
2419   (gnus-turn-off-edit-menu 'article)
2420   (unless (boundp 'gnus-article-article-menu)
2421     (easy-menu-define
2422      gnus-article-article-menu gnus-article-mode-map ""
2423      '("Article"
2424        ["Scroll forwards" gnus-article-goto-next-page t]
2425        ["Scroll backwards" gnus-article-goto-prev-page t]
2426        ["Show summary" gnus-article-show-summary t]
2427        ["Fetch Message-ID at point" gnus-article-refer-article t]
2428        ["Mail to address at point" gnus-article-mail t]
2429        ["Send a bug report" gnus-bug t]))
2430
2431     (easy-menu-define
2432      gnus-article-treatment-menu gnus-article-mode-map ""
2433      '("Treatment"
2434        ["Hide headers" gnus-article-hide-headers t]
2435        ["Hide signature" gnus-article-hide-signature t]
2436        ["Hide citation" gnus-article-hide-citation t]
2437        ["Treat overstrike" gnus-article-treat-overstrike t]
2438        ["Remove carriage return" gnus-article-remove-cr t]
2439        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
2440
2441     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2442
2443     (when (boundp 'gnus-summary-post-menu)
2444       (define-key gnus-article-mode-map [menu-bar post]
2445         (cons "Post" gnus-summary-post-menu)))
2446
2447     (gnus-run-hooks 'gnus-article-menu-hook)))
2448
2449 (defun gnus-article-mode ()
2450   "Major mode for displaying an article.
2451
2452 All normal editing commands are switched off.
2453
2454 The following commands are available in addition to all summary mode
2455 commands:
2456 \\<gnus-article-mode-map>
2457 \\[gnus-article-next-page]\t Scroll the article one page forwards
2458 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2459 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2460 \\[gnus-article-show-summary]\t Display the summary buffer
2461 \\[gnus-article-mail]\t Send a reply to the address near point
2462 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2463 \\[gnus-info-find-node]\t Go to the Gnus info node"
2464   (interactive)
2465   (when (gnus-visual-p 'article-menu 'menu)
2466     (gnus-article-make-menu-bar))
2467   (gnus-simplify-mode-line)
2468   (setq mode-name "Article")
2469   (setq major-mode 'gnus-article-mode)
2470   (make-local-variable 'minor-mode-alist)
2471   (use-local-map gnus-article-mode-map)
2472   (gnus-update-format-specifications nil 'article-mode)
2473   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2474   (make-local-variable 'gnus-page-broken)
2475   (make-local-variable 'gnus-button-marker-list)
2476   (make-local-variable 'gnus-article-current-summary)
2477   (make-local-variable 'gnus-article-mime-handles)
2478   (make-local-variable 'gnus-article-decoded-p)
2479   (make-local-variable 'gnus-article-mime-handle-alist)
2480   (make-local-variable 'gnus-article-washed-types)
2481   (gnus-set-default-directory)
2482   (buffer-disable-undo)
2483   (setq buffer-read-only t)
2484   (set-syntax-table gnus-article-mode-syntax-table)
2485   (mm-enable-multibyte)
2486   (gnus-run-hooks 'gnus-article-mode-hook))
2487
2488 (defun gnus-article-setup-buffer ()
2489   "Initialize the article buffer."
2490   (let* ((name (if gnus-single-article-buffer "*Article*"
2491                  (concat "*Article " gnus-newsgroup-name "*")))
2492          (original
2493           (progn (string-match "\\*Article" name)
2494                  (concat " *Original Article"
2495                          (substring name (match-end 0))))))
2496     (setq gnus-article-buffer name)
2497     (setq gnus-original-article-buffer original)
2498     (setq gnus-article-mime-handle-alist nil)
2499     ;; This might be a variable local to the summary buffer.
2500     (unless gnus-single-article-buffer
2501       (save-excursion
2502         (set-buffer gnus-summary-buffer)
2503         (setq gnus-article-buffer name)
2504         (setq gnus-original-article-buffer original)
2505         (gnus-set-global-variables)))
2506     (gnus-article-setup-highlight-words)
2507     ;; Init original article buffer.
2508     (save-excursion
2509       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2510       (mm-enable-multibyte)
2511       (setq major-mode 'gnus-original-article-mode)
2512       (make-local-variable 'gnus-original-article))
2513     (if (get-buffer name)
2514         (save-excursion
2515           (set-buffer name)
2516           (buffer-disable-undo)
2517           (setq buffer-read-only t)
2518           (unless (eq major-mode 'gnus-article-mode)
2519             (gnus-article-mode))
2520           (current-buffer))
2521       (save-excursion
2522         (set-buffer (gnus-get-buffer-create name))
2523         (gnus-article-mode)
2524         (make-local-variable 'gnus-summary-buffer)
2525         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2526         (current-buffer)))))
2527
2528 ;; Set article window start at LINE, where LINE is the number of lines
2529 ;; from the head of the article.
2530 (defun gnus-article-set-window-start (&optional line)
2531   (set-window-start
2532    (get-buffer-window gnus-article-buffer t)
2533    (save-excursion
2534      (set-buffer gnus-article-buffer)
2535      (goto-char (point-min))
2536      (if (not line)
2537          (point-min)
2538        (gnus-message 6 "Moved to bookmark")
2539        (search-forward "\n\n" nil t)
2540        (forward-line line)
2541        (point)))))
2542
2543 (defun gnus-article-prepare (article &optional all-headers header)
2544   "Prepare ARTICLE in article mode buffer.
2545 ARTICLE should either be an article number or a Message-ID.
2546 If ARTICLE is an id, HEADER should be the article headers.
2547 If ALL-HEADERS is non-nil, no headers are hidden."
2548   (save-excursion
2549     ;; Make sure we start in a summary buffer.
2550     (unless (eq major-mode 'gnus-summary-mode)
2551       (set-buffer gnus-summary-buffer))
2552     (setq gnus-summary-buffer (current-buffer))
2553     (let* ((gnus-article (if header (mail-header-number header) article))
2554            (summary-buffer (current-buffer))
2555            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2556            (group gnus-newsgroup-name)
2557            result)
2558       (save-excursion
2559         (gnus-article-setup-buffer)
2560         (set-buffer gnus-article-buffer)
2561         ;; Deactivate active regions.
2562         (when (and (boundp 'transient-mark-mode)
2563                    transient-mark-mode)
2564           (setq mark-active nil))
2565         (if (not (setq result (let ((buffer-read-only nil))
2566                                 (gnus-request-article-this-buffer
2567                                  article group))))
2568             ;; There is no such article.
2569             (save-excursion
2570               (when (and (numberp article)
2571                          (not (memq article gnus-newsgroup-sparse)))
2572                 (setq gnus-article-current
2573                       (cons gnus-newsgroup-name article))
2574                 (set-buffer gnus-summary-buffer)
2575                 (setq gnus-current-article article)
2576                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2577                     (progn
2578                       (gnus-summary-set-agent-mark article)
2579                       (message "Message marked for downloading"))
2580                   (gnus-summary-mark-article article gnus-canceled-mark)
2581                   (unless (memq article gnus-newsgroup-sparse)
2582                     (gnus-error 1
2583                      "No such article (may have expired or been canceled)")))))
2584           (if (or (eq result 'pseudo)
2585                   (eq result 'nneething))
2586               (progn
2587                 (save-excursion
2588                   (set-buffer summary-buffer)
2589                   (push article gnus-newsgroup-history)
2590                   (setq gnus-last-article gnus-current-article
2591                         gnus-current-article 0
2592                         gnus-current-headers nil
2593                         gnus-article-current nil)
2594                   (if (eq result 'nneething)
2595                       (gnus-configure-windows 'summary)
2596                     (gnus-configure-windows 'article))
2597                   (gnus-set-global-variables))
2598                 (let ((gnus-article-mime-handle-alist-1
2599                        gnus-article-mime-handle-alist))
2600                   (gnus-set-mode-line 'article)))
2601             ;; The result from the `request' was an actual article -
2602             ;; or at least some text that is now displayed in the
2603             ;; article buffer.
2604             (when (and (numberp article)
2605                        (not (eq article gnus-current-article)))
2606               ;; Seems like a new article has been selected.
2607               ;; `gnus-current-article' must be an article number.
2608               (save-excursion
2609                 (set-buffer summary-buffer)
2610                 (push article gnus-newsgroup-history)
2611                 (setq gnus-last-article gnus-current-article
2612                       gnus-current-article article
2613                       gnus-current-headers
2614                       (gnus-summary-article-header gnus-current-article)
2615                       gnus-article-current
2616                       (cons gnus-newsgroup-name gnus-current-article))
2617                 (unless (vectorp gnus-current-headers)
2618                   (setq gnus-current-headers nil))
2619                 (gnus-summary-goto-subject gnus-current-article)
2620                 (when (gnus-summary-show-thread)
2621                   ;; If the summary buffer really was folded, the
2622                   ;; previous goto may not actually have gone to
2623                   ;; the right article, but the thread root instead.
2624                   ;; So we go again.
2625                   (gnus-summary-goto-subject gnus-current-article))
2626                 (gnus-run-hooks 'gnus-mark-article-hook)
2627                 (gnus-set-mode-line 'summary)
2628                 (when (gnus-visual-p 'article-highlight 'highlight)
2629                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2630                 ;; Set the global newsgroup variables here.
2631                 (gnus-set-global-variables)
2632                 (setq gnus-have-all-headers
2633                       (or all-headers gnus-show-all-headers))))
2634             (when (or (numberp article)
2635                       (stringp article))
2636               (gnus-article-prepare-display)
2637               ;; Do page break.
2638               (goto-char (point-min))
2639               (setq gnus-page-broken
2640                     (when gnus-break-pages
2641                       (gnus-narrow-to-page)
2642                       t)))
2643             (let ((gnus-article-mime-handle-alist-1
2644                    gnus-article-mime-handle-alist))
2645               (gnus-set-mode-line 'article))
2646             (article-goto-body)
2647             (set-window-point (get-buffer-window (current-buffer)) (point))
2648             (gnus-configure-windows 'article)
2649             t))))))
2650
2651 ;;;###autoload
2652 (defun gnus-article-prepare-display ()
2653   "Make the current buffer look like a nice article."
2654   ;; Hooks for getting information from the article.
2655   ;; This hook must be called before being narrowed.
2656   (let ((gnus-article-buffer (current-buffer))
2657         buffer-read-only)
2658     (unless (eq major-mode 'gnus-article-mode)
2659       (gnus-article-mode))
2660     (setq buffer-read-only nil)
2661     (gnus-run-hooks 'gnus-tmp-internal-hook)
2662     (gnus-run-hooks 'gnus-article-prepare-hook)
2663     (when gnus-display-mime-function
2664       (funcall gnus-display-mime-function))))
2665
2666 ;;;
2667 ;;; Gnus MIME viewing functions
2668 ;;;
2669
2670 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2671   "The following specs can be used:
2672 %t  The MIME type
2673 %T  MIME type, along with additional info
2674 %n  The `name' parameter
2675 %d  The description, if any
2676 %l  The length of the encoded part
2677 %p  The part identifier number
2678 %e  Dots if the part isn't displayed")
2679
2680 (defvar gnus-mime-button-line-format-alist
2681   '((?t gnus-tmp-type ?s)
2682     (?T gnus-tmp-type-long ?s)
2683     (?n gnus-tmp-name ?s)
2684     (?d gnus-tmp-description ?s)
2685     (?p gnus-tmp-id ?s)
2686     (?l gnus-tmp-length ?d)
2687     (?e gnus-tmp-dots ?s)))
2688
2689 (defvar gnus-mime-button-commands
2690   '((gnus-article-press-button  "\r"    "Toggle Display")
2691     (gnus-mime-view-part        "v"     "View Interactively...")
2692     (gnus-mime-save-part        "o"     "Save...")
2693     (gnus-mime-copy-part        "c"     "View As Text, In Other Buffer")
2694     (gnus-mime-inline-part      "i"     "View As Text, In This Buffer")
2695     (gnus-mime-internalize-part "E"     "View Internally")
2696     (gnus-mime-externalize-part "e"     "View Externally")
2697     (gnus-mime-pipe-part        "|"     "Pipe To Command...")))
2698
2699 (defun gnus-article-mime-part-status ()
2700   (if gnus-article-mime-handle-alist-1
2701       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2702     ""))
2703
2704 (defvar gnus-mime-button-map nil)
2705 (unless gnus-mime-button-map
2706   (setq gnus-mime-button-map (make-sparse-keymap))
2707   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2708   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2709   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
2710   (mapcar (lambda (c)
2711             (define-key gnus-mime-button-map (cadr c) (car c)))
2712           gnus-mime-button-commands))
2713
2714 (defun gnus-mime-button-menu (event)
2715   "Construct a context-sensitive menu of MIME commands."
2716   (interactive "e")
2717   (save-excursion
2718     (let ((pos (event-start event)))
2719       (set-buffer (window-buffer (posn-window pos)))
2720       (goto-char (posn-point pos))
2721       (gnus-article-check-buffer)
2722       (let ((response (x-popup-menu
2723                        t `("MIME Part"
2724                            ("" ,@(mapcar (lambda (c)
2725                                            (cons (caddr c) (car c)))
2726                                          gnus-mime-button-commands))))))
2727         (if response
2728             (funcall response))))))
2729
2730 (defun gnus-mime-view-all-parts (&optional handles)
2731   "View all the MIME parts."
2732   (interactive)
2733   (save-current-buffer
2734     (set-buffer gnus-article-buffer)
2735     (let ((handles (or handles gnus-article-mime-handles))
2736           (mail-parse-charset gnus-newsgroup-charset)
2737           (mail-parse-ignored-charsets 
2738            (save-excursion (set-buffer gnus-summary-buffer)
2739                            gnus-newsgroup-ignored-charsets)))
2740       (if (stringp (car handles))
2741           (gnus-mime-view-all-parts (cdr handles))
2742         (mapcar 'mm-display-part handles)))))
2743
2744 (defun gnus-mime-save-part ()
2745   "Save the MIME part under point."
2746   (interactive)
2747   (gnus-article-check-buffer)
2748   (let ((data (get-text-property (point) 'gnus-data)))
2749     (mm-save-part data)))
2750
2751 (defun gnus-mime-pipe-part ()
2752   "Pipe the MIME part under point to a process."
2753   (interactive)
2754   (gnus-article-check-buffer)
2755   (let ((data (get-text-property (point) 'gnus-data)))
2756     (mm-pipe-part data)))
2757
2758 (defun gnus-mime-view-part ()
2759   "Interactively choose a viewing method for the MIME part under point."
2760   (interactive)
2761   (gnus-article-check-buffer)
2762   (let ((data (get-text-property (point) 'gnus-data)))
2763     (mm-interactively-view-part data)))
2764
2765 (defun gnus-mime-copy-part (&optional handle)
2766   "Put the the MIME part under point into a new buffer."
2767   (interactive)
2768   (gnus-article-check-buffer)
2769   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2770          (contents (mm-get-part handle))|
2771          (base (file-name-nondirectory
2772                 (or
2773                  (mail-content-type-get (mm-handle-type handle) 'name)
2774                  (mail-content-type-get (mm-handle-type handle)
2775                                         'filename)
2776                  "*decoded*")))
2777          (buffer (generate-new-buffer base)))
2778     (switch-to-buffer buffer)
2779     (insert contents)
2780     ;; We do it this way to make `normal-mode' set the appropriate mode.
2781     (unwind-protect
2782         (progn
2783           (setq buffer-file-name (expand-file-name base))
2784           (normal-mode))
2785       (setq buffer-file-name nil))
2786     (goto-char (point-min))))
2787
2788 (defun gnus-mime-inline-part (&optional handle)
2789   "Insert the MIME part under point into the current buffer."
2790   (interactive)
2791   (gnus-article-check-buffer)
2792   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2793          contents
2794          (b (point))
2795          buffer-read-only)
2796     (if (mm-handle-undisplayer handle)
2797         (mm-remove-part handle)
2798       (setq contents (mm-get-part handle))
2799       (forward-line 2)
2800       (mm-insert-inline handle contents)
2801       (goto-char b))))
2802
2803 (defun gnus-mime-externalize-part (&optional handle)
2804   "View the MIME part under point with an external viewer."
2805   (interactive)
2806   (gnus-article-check-buffer)
2807   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2808          (mm-user-display-methods nil)
2809          (mm-inline-large-images nil)
2810          (mail-parse-charset gnus-newsgroup-charset)
2811          (mail-parse-ignored-charsets 
2812           (save-excursion (set-buffer gnus-summary-buffer)
2813                           gnus-newsgroup-ignored-charsets)))
2814     (if (mm-handle-undisplayer handle)
2815         (mm-remove-part handle)
2816       (mm-display-part handle))))
2817
2818 (defun gnus-mime-internalize-part (&optional handle)
2819   "View the MIME part under point with an internal viewer."
2820   (interactive)
2821   (gnus-article-check-buffer)
2822   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2823          (mm-user-display-methods '((".*" . inline)))
2824          (mm-inline-large-images t)
2825          (mail-parse-charset gnus-newsgroup-charset)
2826          (mail-parse-ignored-charsets 
2827           (save-excursion (set-buffer gnus-summary-buffer)
2828                           gnus-newsgroup-ignored-charsets)))
2829     (if (mm-handle-undisplayer handle)
2830         (mm-remove-part handle)
2831       (mm-display-part handle))))
2832
2833 (defun gnus-article-part-wrapper (n function)
2834   (save-current-buffer
2835     (set-buffer gnus-article-buffer)
2836     (when (> n (length gnus-article-mime-handle-alist))
2837       (error "No such part"))
2838     (gnus-article-goto-part n)
2839     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2840       (funcall function handle))))
2841
2842 (defun gnus-article-pipe-part (n)
2843   "Pipe MIME part N, which is the numerical prefix."
2844   (interactive "p")
2845   (gnus-article-part-wrapper n 'mm-pipe-part))
2846
2847 (defun gnus-article-save-part (n)
2848   "Save MIME part N, which is the numerical prefix."
2849   (interactive "p")
2850   (gnus-article-part-wrapper n 'mm-save-part))
2851
2852 (defun gnus-article-interactively-view-part (n)
2853   "View MIME part N interactively, which is the numerical prefix."
2854   (interactive "p")
2855   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2856
2857 (defun gnus-article-copy-part (n)
2858   "Copy MIME part N, which is the numerical prefix."
2859   (interactive "p")
2860   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2861
2862 (defun gnus-article-externalize-part (n)
2863   "View MIME part N externally, which is the numerical prefix."
2864   (interactive "p")
2865   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2866
2867 (defun gnus-article-inline-part (n)
2868   "Inline MIME part N, which is the numerical prefix."
2869   (interactive "p")
2870   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
2871
2872 (defun gnus-article-view-part (n)
2873   "View MIME part N, which is the numerical prefix."
2874   (interactive "p")
2875   (save-current-buffer
2876     (set-buffer gnus-article-buffer)
2877     (when (> n (length gnus-article-mime-handle-alist))
2878       (error "No such part"))
2879     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2880       (when (gnus-article-goto-part n)
2881         (if (equal (car handle) "multipart/alternative")
2882             (gnus-article-press-button)
2883           (when (eq (gnus-mm-display-part handle) 'internal)
2884             (gnus-set-window-start)))))))
2885
2886 (defun gnus-mm-display-part (handle)
2887   "Display HANDLE and fix MIME button."
2888   (let ((id (get-text-property (point) 'gnus-part))
2889         (point (point))
2890         buffer-read-only)
2891     (forward-line 1)
2892     (prog1
2893         (let ((window (selected-window))
2894               (mail-parse-charset gnus-newsgroup-charset)
2895               (mail-parse-ignored-charsets 
2896                (save-excursion (set-buffer gnus-summary-buffer)
2897                                gnus-newsgroup-ignored-charsets)))
2898           (save-excursion
2899             (unwind-protect
2900                 (let ((win (get-buffer-window (current-buffer) t))
2901                       (beg (point)))
2902                   (when win
2903                     (select-window win))
2904                   (goto-char point)
2905                   (forward-line)
2906                   (if (mm-handle-displayed-p handle)
2907                       ;; This will remove the part.
2908                       (mm-display-part handle)
2909                     (save-restriction
2910                       (narrow-to-region (point) (1+ (point)))
2911                       (mm-display-part handle)
2912                       ;; We narrow to the part itself and
2913                       ;; then call the treatment functions.
2914                       (goto-char (point-min))
2915                       (forward-line 1)
2916                       (narrow-to-region (point) (point-max))
2917                       (gnus-treat-article
2918                        nil id
2919                        (1- (length gnus-article-mime-handles))
2920                        (mm-handle-media-type handle)))))
2921               (select-window window))))
2922       (goto-char point)
2923       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
2924       (gnus-insert-mime-button
2925        handle id (list (mm-handle-displayed-p handle)))
2926       (goto-char point))))
2927
2928 (defun gnus-article-goto-part (n)
2929   "Go to MIME part N."
2930   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
2931     (when point
2932       (goto-char point))))
2933
2934 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
2935   (let ((gnus-tmp-name
2936          (or (mail-content-type-get (mm-handle-type handle)
2937                                     'name)
2938              (mail-content-type-get (mm-handle-disposition handle)
2939                                     'filename)
2940              ""))
2941         (gnus-tmp-type (mm-handle-media-type handle))
2942         (gnus-tmp-description
2943          (mail-decode-encoded-word-string (or (mm-handle-description handle)
2944                                               "")))
2945         (gnus-tmp-dots
2946          (if (if displayed (car displayed)
2947                (mm-handle-displayed-p handle))
2948              "" "..."))
2949         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
2950                            (buffer-size)))
2951         gnus-tmp-type-long b e)
2952     (when (string-match ".*/" gnus-tmp-name)
2953       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
2954     (setq gnus-tmp-type-long (concat gnus-tmp-type
2955                                      (and (not (equal gnus-tmp-name ""))
2956                                           (concat "; " gnus-tmp-name))))
2957     (or (equal gnus-tmp-description "")
2958         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
2959     (unless (bolp)
2960       (insert "\n"))
2961     (setq b (point))
2962     (gnus-eval-format
2963      gnus-mime-button-line-format gnus-mime-button-line-format-alist
2964      `(local-map ,gnus-mime-button-map
2965                  keymap ,gnus-mime-button-map
2966                  gnus-callback gnus-mm-display-part
2967                  gnus-part ,gnus-tmp-id
2968                  article-type annotation
2969                  gnus-data ,handle))
2970     (setq e (point))
2971     (widget-convert-button 'link b e
2972                            :mime-handle handle
2973                            :action 'gnus-widget-press-button
2974                            :button-keymap gnus-mime-button-map
2975                            :help-echo
2976                            (lambda (widget)
2977                              ;; Needed to properly clear the message
2978                              ;; due to a bug in wid-edit
2979                              (setq help-echo-owns-message t)
2980                              (format
2981                               "Click to %s the MIME part; %s for more options"
2982                               (if (mm-handle-displayed-p
2983                                    (widget-get widget :mime-handle))
2984                                   "hide" "show")
2985                               (if gnus-xemacs "button3" "mouse-3"))))))
2986
2987 (defun gnus-widget-press-button (elems el)
2988   (goto-char (widget-get elems :from))
2989   (gnus-article-press-button))
2990
2991 (defvar gnus-displaying-mime nil)
2992
2993 (defun gnus-display-mime (&optional ihandles)
2994   "Display the MIME parts."
2995   (save-excursion
2996     (save-selected-window
2997       (let ((window (get-buffer-window gnus-article-buffer))
2998             (point (point)))
2999         (when window
3000           (select-window window)
3001           ;; We have to do this since selecting the window
3002           ;; may change the point.  So we set the window point.
3003           (set-window-point window point)))
3004       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3005              buffer-read-only handle name type b e display)
3006         (when (and (not ihandles)
3007                    (not gnus-displaying-mime))
3008           ;; Top-level call; we clean up.
3009           (when gnus-article-mime-handles
3010             (mm-destroy-parts gnus-article-mime-handles)
3011             (setq gnus-article-mime-handle-alist nil)) ;; A trick.
3012           (setq gnus-article-mime-handles handles)
3013           ;; We allow users to glean info from the handles.
3014           (when gnus-article-mime-part-function
3015             (gnus-mime-part-function handles)))
3016         (if (and handles
3017                  (or (not (stringp (car handles)))
3018                      (cdr handles)))
3019             (progn
3020               (when (and (not ihandles)
3021                          (not gnus-displaying-mime))
3022                 ;; Clean up for mime parts.
3023                 (article-goto-body)
3024                 (delete-region (point) (point-max)))
3025               (let ((gnus-displaying-mime t))
3026                 (gnus-mime-display-part handles)))
3027           (save-restriction
3028             (article-goto-body)
3029             (narrow-to-region (point) (point-max))
3030             (gnus-treat-article nil 1 1)
3031             (widen)))
3032         (if (not ihandles)
3033             ;; Highlight the headers.
3034             (save-excursion
3035               (save-restriction
3036                 (article-goto-body)
3037                 (narrow-to-region (point-min) (point))
3038                 (gnus-treat-article 'head))))))))
3039
3040 (defvar gnus-mime-display-multipart-as-mixed nil)
3041
3042 (defun gnus-mime-display-part (handle)
3043   (cond
3044    ;; Single part.
3045    ((not (stringp (car handle)))
3046     (gnus-mime-display-single handle))
3047    ;; User-defined multipart
3048    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3049     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3050              handle))
3051    ;; multipart/alternative
3052    ((and (equal (car handle) "multipart/alternative")
3053          (not gnus-mime-display-multipart-as-mixed))
3054     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3055       (push (cons id handle) gnus-article-mime-handle-alist)
3056       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3057    ;; multipart/related
3058    ((and (equal (car handle) "multipart/related")
3059          (not gnus-mime-display-multipart-as-mixed))
3060     ;;;!!!We should find the start part, but we just default
3061     ;;;!!!to the first part.
3062     (gnus-mime-display-part (cadr handle)))
3063    ;; Other multiparts are handled like multipart/mixed.
3064    (t
3065     (gnus-mime-display-mixed (cdr handle)))))
3066
3067 (defun gnus-mime-part-function (handles)
3068   (if (stringp (car handles))
3069       (mapcar 'gnus-mime-part-function (cdr handles))
3070     (funcall gnus-article-mime-part-function handles)))
3071
3072 (defun gnus-mime-display-mixed (handles)
3073   (mapcar 'gnus-mime-display-part handles))
3074
3075 (defun gnus-mime-display-single (handle)
3076   (let ((type (mm-handle-media-type handle))
3077         (ignored gnus-ignored-mime-types)
3078         (not-attachment t)
3079         (move nil)
3080         display text)
3081     (catch 'ignored
3082       (progn
3083         (while ignored
3084           (when (string-match (pop ignored) type)
3085             (throw 'ignored nil)))
3086         (if (and (setq not-attachment
3087                        (or (not (mm-handle-disposition handle))
3088                            (equal (car (mm-handle-disposition handle))
3089                                   "inline")
3090                            (mm-attachment-override-p handle)))
3091                  (mm-automatic-display-p handle)
3092                  (or (mm-inlined-p handle)
3093                      (mm-automatic-external-display-p type)))
3094             (setq display t)
3095           (when (equal (mm-handle-media-supertype handle) "text")
3096             (setq text t)))
3097         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3098           (push (cons id handle) gnus-article-mime-handle-alist)
3099           (when (or (not display)
3100                     (not (gnus-unbuttonized-mime-type-p type)))
3101             (gnus-article-insert-newline)
3102             (gnus-insert-mime-button
3103              handle id (list (or display (and not-attachment text))))
3104             (gnus-article-insert-newline)
3105             (gnus-article-insert-newline)
3106             (setq move t)))
3107         (let ((beg (point)))
3108           (cond
3109            (display
3110             (when move
3111               (forward-line -2))
3112             (let ((mail-parse-charset gnus-newsgroup-charset)
3113                   (mail-parse-ignored-charsets 
3114                    (save-excursion (set-buffer gnus-summary-buffer)
3115                                    gnus-newsgroup-ignored-charsets)))
3116               (mm-display-part handle t))
3117             (goto-char (point-max)))
3118            ((and text not-attachment)
3119             (when move
3120               (forward-line -2))
3121             (gnus-article-insert-newline)
3122             (mm-insert-inline handle (mm-get-part handle))
3123             (goto-char (point-max))))
3124           ;; Do highlighting.
3125           (save-excursion
3126             (save-restriction
3127               (narrow-to-region beg (point))
3128               (gnus-treat-article
3129                nil (length gnus-article-mime-handle-alist)
3130                (1- (length gnus-article-mime-handles))
3131                (mm-handle-media-type handle)))))))))
3132
3133 (defun gnus-unbuttonized-mime-type-p (type)
3134   "Say whether TYPE is to be unbuttonized."
3135   (unless gnus-inhibit-mime-unbuttonizing
3136     (catch 'found
3137       (let ((types gnus-unbuttonized-mime-types))
3138         (while types
3139           (when (string-match (pop types) type)
3140             (throw 'found t)))))))
3141
3142 (defun gnus-article-insert-newline ()
3143   "Insert a newline, but mark it as undeletable."
3144   (gnus-put-text-property
3145    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3146
3147 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3148   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3149          (ihandles handles)
3150          (point (point))
3151          handle buffer-read-only from props begend not-pref)
3152     (save-window-excursion
3153       (save-restriction
3154         (when ibegend
3155           (narrow-to-region (car ibegend)
3156                             (or (cdr ibegend)
3157                                 (progn
3158                                   (goto-char (car ibegend))
3159                                   (forward-line 2)
3160                                   (point))))
3161           (delete-region (point-min) (point-max))
3162           (mm-remove-parts handles))
3163         (setq begend (list (point-marker)))
3164         ;; Do the toggle.
3165         (unless (setq not-pref (cadr (member preferred ihandles)))
3166           (setq not-pref (car ihandles)))
3167         (when (or ibegend
3168                   (not (gnus-unbuttonized-mime-type-p
3169                         "multipart/alternative")))
3170           (gnus-add-text-properties
3171            (setq from (point))
3172            (progn
3173              (insert (format "%d.  " id))
3174              (point))
3175            `(gnus-callback
3176              (lambda (handles)
3177                (unless ,(not ibegend)
3178                  (setq gnus-article-mime-handle-alist
3179                        ',gnus-article-mime-handle-alist))
3180                (gnus-mime-display-alternative
3181                 ',ihandles ',not-pref ',begend ,id))
3182              local-map ,gnus-mime-button-map
3183              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3184              face ,gnus-article-button-face
3185              keymap ,gnus-mime-button-map
3186              gnus-part ,id
3187              gnus-data ,handle))
3188           (widget-convert-button 'link from (point)
3189                                  :action 'gnus-widget-press-button
3190                                  :button-keymap gnus-widget-button-keymap)
3191           ;; Do the handles
3192           (while (setq handle (pop handles))
3193             (gnus-add-text-properties
3194              (setq from (point))
3195              (progn
3196                (insert (format "(%c) %-18s"
3197                                (if (equal handle preferred) ?* ? )
3198                                (mm-handle-media-type handle)))
3199                (point))
3200              `(gnus-callback
3201                (lambda (handles)
3202                  (unless ,(not ibegend)
3203                    (setq gnus-article-mime-handle-alist
3204                          ',gnus-article-mime-handle-alist))
3205                  (gnus-mime-display-alternative
3206                   ',ihandles ',handle ',begend ,id))
3207                local-map ,gnus-mime-button-map
3208                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3209                face ,gnus-article-button-face
3210                keymap ,gnus-mime-button-map
3211                gnus-part ,id
3212                gnus-data ,handle))
3213             (widget-convert-button 'link from (point)
3214                                    :action 'gnus-widget-press-button
3215                                    :button-keymap gnus-widget-button-keymap)
3216             (insert "  "))
3217           (insert "\n\n"))
3218         (when preferred
3219           (if (stringp (car preferred))
3220               (gnus-display-mime preferred)
3221             (let ((mail-parse-charset gnus-newsgroup-charset)
3222                   (mail-parse-ignored-charsets 
3223                    (save-excursion (set-buffer gnus-summary-buffer)
3224                                    gnus-newsgroup-ignored-charsets)))
3225               (mm-display-part preferred)
3226               ;; Do highlighting.
3227               (save-excursion
3228                 (save-restriction
3229                   (narrow-to-region (car begend) (point-max))
3230                   (gnus-treat-article
3231                    nil (length gnus-article-mime-handle-alist)
3232                    (1- (length gnus-article-mime-handles))
3233                    (mm-handle-media-type handle))))))
3234           (goto-char (point-max))
3235           (setcdr begend (point-marker)))))
3236     (when ibegend
3237       (goto-char point))))
3238
3239 (defun gnus-article-wash-status ()
3240   "Return a string which display status of article washing."
3241   (save-excursion
3242     (set-buffer gnus-article-buffer)
3243     (let ((cite (memq 'cite gnus-article-wash-types))
3244           (headers (memq 'headers gnus-article-wash-types))
3245           (boring (memq 'boring-headers gnus-article-wash-types))
3246           (pgp (memq 'pgp gnus-article-wash-types))
3247           (pem (memq 'pem gnus-article-wash-types))
3248           (signature (memq 'signature gnus-article-wash-types))
3249           (overstrike (memq 'overstrike gnus-article-wash-types))
3250           (emphasis (memq 'emphasis gnus-article-wash-types)))
3251       (format "%c%c%c%c%c%c"
3252               (if cite ?c ? )
3253               (if (or headers boring) ?h ? )
3254               (if (or pgp pem) ?p ? )
3255               (if signature ?s ? )
3256               (if overstrike ?o ? )
3257               (if emphasis ?e ? )))))
3258
3259 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3260
3261 (defun gnus-article-maybe-hide-headers ()
3262   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3263 Provided for backwards compatibility."
3264   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3265                  (not (save-excursion (set-buffer gnus-summary-buffer)
3266                                       gnus-have-all-headers)))
3267              (not gnus-inhibit-hiding))
3268     (gnus-article-hide-headers)))
3269
3270 ;;; Article savers.
3271
3272 (defun gnus-output-to-file (file-name)
3273   "Append the current article to a file named FILE-NAME."
3274   (let ((artbuf (current-buffer)))
3275     (with-temp-buffer
3276       (insert-buffer-substring artbuf)
3277       ;; Append newline at end of the buffer as separator, and then
3278       ;; save it to file.
3279       (goto-char (point-max))
3280       (insert "\n")
3281       (append-to-file (point-min) (point-max) file-name)
3282       t)))
3283
3284 (defun gnus-narrow-to-page (&optional arg)
3285   "Narrow the article buffer to a page.
3286 If given a numerical ARG, move forward ARG pages."
3287   (interactive "P")
3288   (setq arg (if arg (prefix-numeric-value arg) 0))
3289   (save-excursion
3290     (set-buffer gnus-article-buffer)
3291     (goto-char (point-min))
3292     (widen)
3293     ;; Remove any old next/prev buttons.
3294     (when (gnus-visual-p 'page-marker)
3295       (let ((buffer-read-only nil))
3296         (gnus-remove-text-with-property 'gnus-prev)
3297         (gnus-remove-text-with-property 'gnus-next)))
3298     (when
3299         (cond ((< arg 0)
3300                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3301               ((> arg 0)
3302                (re-search-forward page-delimiter nil 'move arg)))
3303       (goto-char (match-end 0)))
3304     (narrow-to-region
3305      (point)
3306      (if (re-search-forward page-delimiter nil 'move)
3307          (match-beginning 0)
3308        (point)))
3309     (when (and (gnus-visual-p 'page-marker)
3310                (not (= (point-min) 1)))
3311       (save-excursion
3312         (goto-char (point-min))
3313         (gnus-insert-prev-page-button)))
3314     (when (and (gnus-visual-p 'page-marker)
3315                (< (+ (point-max) 2) (buffer-size)))
3316       (save-excursion
3317         (goto-char (point-max))
3318         (gnus-insert-next-page-button)))))
3319
3320 ;; Article mode commands
3321
3322 (defun gnus-article-goto-next-page ()
3323   "Show the next page of the article."
3324   (interactive)
3325   (when (gnus-article-next-page)
3326     (goto-char (point-min))
3327     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3328
3329 (defun gnus-article-goto-prev-page ()
3330   "Show the next page of the article."
3331   (interactive)
3332   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3333     (gnus-article-prev-page nil)))
3334
3335 (defun gnus-article-next-page (&optional lines)
3336   "Show the next page of the current article.
3337 If end of article, return non-nil.  Otherwise return nil.
3338 Argument LINES specifies lines to be scrolled up."
3339   (interactive "p")
3340   (move-to-window-line -1)
3341   (if (save-excursion
3342         (end-of-line)
3343         (and (pos-visible-in-window-p)  ;Not continuation line.
3344              (eobp)))
3345       ;; Nothing in this page.
3346       (if (or (not gnus-page-broken)
3347               (save-excursion
3348                 (save-restriction
3349                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3350           t                             ;Nothing more.
3351         (gnus-narrow-to-page 1)         ;Go to next page.
3352         nil)
3353     ;; More in this page.
3354     (let ((scroll-in-place nil))
3355       (condition-case ()
3356           (scroll-up lines)
3357         (end-of-buffer
3358          ;; Long lines may cause an end-of-buffer error.
3359          (goto-char (point-max)))))
3360     (move-to-window-line 0)
3361     nil))
3362
3363 (defun gnus-article-prev-page (&optional lines)
3364   "Show previous page of current article.
3365 Argument LINES specifies lines to be scrolled down."
3366   (interactive "p")
3367   (move-to-window-line 0)
3368   (if (and gnus-page-broken
3369            (bobp)
3370            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3371       (progn
3372         (gnus-narrow-to-page -1)        ;Go to previous page.
3373         (goto-char (point-max))
3374         (recenter -1))
3375     (let ((scroll-in-place nil))
3376       (prog1
3377           (condition-case ()
3378               (scroll-down lines)
3379             (beginning-of-buffer
3380              (goto-char (point-min))))
3381         (move-to-window-line 0)))))
3382
3383 (defun gnus-article-refer-article ()
3384   "Read article specified by message-id around point."
3385   (interactive)
3386   (let ((point (point)))
3387     (search-forward ">" nil t)          ;Move point to end of "<....>".
3388     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3389         (let ((message-id (match-string 1)))
3390           (goto-char point)
3391           (set-buffer gnus-summary-buffer)
3392           (gnus-summary-refer-article message-id))
3393       (goto-char (point))
3394       (error "No references around point"))))
3395
3396 (defun gnus-article-show-summary ()
3397   "Reconfigure windows to show summary buffer."
3398   (interactive)
3399   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3400       (error "There is no summary buffer for this article buffer")
3401     (gnus-article-set-globals)
3402     (gnus-configure-windows 'article)
3403     (gnus-summary-goto-subject gnus-current-article)
3404     (gnus-summary-position-point)))
3405
3406 (defun gnus-article-describe-briefly ()
3407   "Describe article mode commands briefly."
3408   (interactive)
3409   (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")))
3410
3411 (defun gnus-article-summary-command ()
3412   "Execute the last keystroke in the summary buffer."
3413   (interactive)
3414   (let ((obuf (current-buffer))
3415         (owin (current-window-configuration))
3416         func)
3417     (switch-to-buffer gnus-article-current-summary 'norecord)
3418     (setq func (lookup-key (current-local-map) (this-command-keys)))
3419     (call-interactively func)
3420     (set-buffer obuf)
3421     (set-window-configuration owin)
3422     (set-window-point (get-buffer-window (current-buffer)) (point))))
3423
3424 (defun gnus-article-summary-command-nosave ()
3425   "Execute the last keystroke in the summary buffer."
3426   (interactive)
3427   (let (func)
3428     (pop-to-buffer gnus-article-current-summary 'norecord)
3429     (setq func (lookup-key (current-local-map) (this-command-keys)))
3430     (call-interactively func)))
3431
3432 (defun gnus-article-check-buffer ()
3433   "Beep if not in an article buffer."
3434   (unless (equal major-mode 'gnus-article-mode)
3435     (error "Command invoked outside of a Gnus article buffer")))
3436
3437 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3438   "Read a summary buffer key sequence and execute it from the article buffer."
3439   (interactive "P")
3440   (gnus-article-check-buffer)
3441   (let ((nosaves
3442          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3443            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3444            "=" "^" "\M-^" "|"))
3445         (nosave-but-article
3446          '("A\r"))
3447         (nosave-in-article
3448          '("\C-d"))
3449         (up-to-top
3450          '("n" "Gn" "p" "Gp"))
3451         keys new-sum-point)
3452     (save-excursion
3453       (set-buffer gnus-article-current-summary)
3454       (let (gnus-pick-mode)
3455         (push (or key last-command-event) unread-command-events)
3456         (setq keys (if gnus-xemacs
3457                        (events-to-keys (read-key-sequence nil))
3458                      (read-key-sequence nil)))))
3459                      
3460     (message "")
3461
3462     (if (or (member keys nosaves)
3463             (member keys nosave-but-article)
3464             (member keys nosave-in-article))
3465         (let (func)
3466           (save-window-excursion
3467             (pop-to-buffer gnus-article-current-summary 'norecord)
3468             ;; We disable the pick minor mode commands.
3469             (let (gnus-pick-mode)
3470               (setq func (lookup-key (current-local-map) keys))))
3471           (if (not func)
3472               (ding)
3473             (unless (member keys nosave-in-article)
3474               (set-buffer gnus-article-current-summary))
3475             (call-interactively func)
3476             (setq new-sum-point (point)))
3477           (when (member keys nosave-but-article)
3478             (pop-to-buffer gnus-article-buffer 'norecord)))
3479       ;; These commands should restore window configuration.
3480       (let ((obuf (current-buffer))
3481             (owin (current-window-configuration))
3482             (opoint (point))
3483             (summary gnus-article-current-summary)
3484             func in-buffer selected)
3485         (if not-restore-window
3486             (pop-to-buffer summary 'norecord)
3487           (switch-to-buffer summary 'norecord))
3488         (setq in-buffer (current-buffer))
3489         ;; We disable the pick minor mode commands.
3490         (if (setq func (let (gnus-pick-mode)
3491                          (lookup-key (current-local-map) keys)))
3492             (progn
3493               (call-interactively func)
3494               (setq new-sum-point (point)))
3495           (ding))
3496         (when (eq in-buffer (current-buffer))
3497           (setq selected (gnus-summary-select-article))
3498           (set-buffer obuf)
3499           (unless not-restore-window
3500             (set-window-configuration owin))
3501           (when (eq selected 'old)
3502             (article-goto-body)
3503             (set-window-start (get-buffer-window (current-buffer))
3504                               1)
3505             (set-window-point (get-buffer-window (current-buffer))
3506                               (point)))
3507           (let ((win (get-buffer-window gnus-article-current-summary)))
3508             (when win
3509               (set-window-point win new-sum-point))))))))
3510
3511 (defun gnus-article-hide (&optional arg force)
3512   "Hide all the gruft in the current article.
3513 This means that PGP stuff, signatures, cited text and (some)
3514 headers will be hidden.
3515 If given a prefix, show the hidden text instead."
3516   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3517   (gnus-article-hide-headers arg)
3518   (gnus-article-hide-pgp arg)
3519   (gnus-article-hide-citation-maybe arg force)
3520   (gnus-article-hide-signature arg))
3521
3522 (defun gnus-article-maybe-highlight ()
3523   "Do some article highlighting if article highlighting is requested."
3524   (when (gnus-visual-p 'article-highlight 'highlight)
3525     (gnus-article-highlight-some)))
3526
3527 (defun gnus-check-group-server ()
3528   ;; Make sure the connection to the server is alive.
3529   (unless (gnus-server-opened
3530            (gnus-find-method-for-group gnus-newsgroup-name))
3531     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3532     (gnus-request-group gnus-newsgroup-name t)))
3533
3534 (defun gnus-request-article-this-buffer (article group)
3535   "Get an article and insert it into this buffer."
3536   (let (do-update-line sparse-header)
3537     (prog1
3538         (save-excursion
3539           (erase-buffer)
3540           (gnus-kill-all-overlays)
3541           (setq group (or group gnus-newsgroup-name))
3542
3543           ;; Using `gnus-request-article' directly will insert the article into
3544           ;; `nntp-server-buffer' - so we'll save some time by not having to
3545           ;; copy it from the server buffer into the article buffer.
3546
3547           ;; We only request an article by message-id when we do not have the
3548           ;; headers for it, so we'll have to get those.
3549           (when (stringp article)
3550             (let ((gnus-override-method gnus-refer-article-method))
3551               (gnus-read-header article)))
3552
3553           ;; If the article number is negative, that means that this article
3554           ;; doesn't belong in this newsgroup (possibly), so we find its
3555           ;; message-id and request it by id instead of number.
3556           (when (and (numberp article)
3557                      gnus-summary-buffer
3558                      (get-buffer gnus-summary-buffer)
3559                      (gnus-buffer-exists-p gnus-summary-buffer))
3560             (save-excursion
3561               (set-buffer gnus-summary-buffer)
3562               (let ((header (gnus-summary-article-header article)))
3563                 (when (< article 0)
3564                   (cond
3565                    ((memq article gnus-newsgroup-sparse)
3566                     ;; This is a sparse gap article.
3567                     (setq do-update-line article)
3568                     (setq article (mail-header-id header))
3569                     (let ((gnus-override-method gnus-refer-article-method))
3570                       (setq sparse-header (gnus-read-header article)))
3571                     (setq gnus-newsgroup-sparse
3572                           (delq article gnus-newsgroup-sparse)))
3573                    ((vectorp header)
3574                     ;; It's a real article.
3575                     (setq article (mail-header-id header)))
3576                    (t
3577                     ;; It is an extracted pseudo-article.
3578                     (setq article 'pseudo)
3579                     (gnus-request-pseudo-article header))))
3580
3581                 (let ((method (gnus-find-method-for-group
3582                                gnus-newsgroup-name)))
3583                   (when (and (eq (car method) 'nneething)
3584                              (vectorp header))
3585                     (let ((dir (concat
3586                                 (file-name-as-directory
3587                                  (or (cadr (assq 'nneething-address method))
3588                                      (nth 1 method)))
3589                                 (mail-header-subject header))))
3590                       (when (file-directory-p dir)
3591                         (setq article 'nneething)
3592                         (gnus-group-enter-directory dir))))))))
3593
3594           (cond
3595            ;; Refuse to select canceled articles.
3596            ((and (numberp article)
3597                  gnus-summary-buffer
3598                  (get-buffer gnus-summary-buffer)
3599                  (gnus-buffer-exists-p gnus-summary-buffer)
3600                  (eq (cdr (save-excursion
3601                             (set-buffer gnus-summary-buffer)
3602                             (assq article gnus-newsgroup-reads)))
3603                      gnus-canceled-mark))
3604             nil)
3605            ;; We first check `gnus-original-article-buffer'.
3606            ((and (get-buffer gnus-original-article-buffer)
3607                  (numberp article)
3608                  (save-excursion
3609                    (set-buffer gnus-original-article-buffer)
3610                    (and (equal (car gnus-original-article) group)
3611                         (eq (cdr gnus-original-article) article))))
3612             (insert-buffer-substring gnus-original-article-buffer)
3613             'article)
3614            ;; Check the backlog.
3615            ((and gnus-keep-backlog
3616                  (gnus-backlog-request-article group article (current-buffer)))
3617             'article)
3618            ;; Check asynchronous pre-fetch.
3619            ((gnus-async-request-fetched-article group article (current-buffer))
3620             (gnus-async-prefetch-next group article gnus-summary-buffer)
3621             (when (and (numberp article) gnus-keep-backlog)
3622               (gnus-backlog-enter-article group article (current-buffer)))
3623             'article)
3624            ;; Check the cache.
3625            ((and gnus-use-cache
3626                  (numberp article)
3627                  (gnus-cache-request-article article group))
3628             'article)
3629            ;; Get the article and put into the article buffer.
3630            ((or (stringp article) (numberp article))
3631             (let ((gnus-override-method
3632                    (and (stringp article) gnus-refer-article-method))
3633                   (buffer-read-only nil))
3634               (erase-buffer)
3635               (gnus-kill-all-overlays)
3636               (gnus-check-group-server)
3637               (when (gnus-request-article article group (current-buffer))
3638                 (when (numberp article)
3639                   (gnus-async-prefetch-next group article gnus-summary-buffer)
3640                   (when gnus-keep-backlog
3641                     (gnus-backlog-enter-article
3642                      group article (current-buffer))))
3643                 'article)))
3644            ;; It was a pseudo.
3645            (t article)))
3646
3647       ;; Associate this article with the current summary buffer.
3648       (setq gnus-article-current-summary gnus-summary-buffer)
3649
3650       ;; Take the article from the original article buffer
3651       ;; and place it in the buffer it's supposed to be in.
3652       (when (and (get-buffer gnus-article-buffer)
3653                  (equal (buffer-name (current-buffer))
3654                         (buffer-name (get-buffer gnus-article-buffer))))
3655         (save-excursion
3656           (if (get-buffer gnus-original-article-buffer)
3657               (set-buffer gnus-original-article-buffer)
3658             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3659             (buffer-disable-undo)
3660             (setq major-mode 'gnus-original-article-mode)
3661             (setq buffer-read-only t))
3662           (let (buffer-read-only)
3663             (erase-buffer)
3664             (insert-buffer-substring gnus-article-buffer))
3665           (setq gnus-original-article (cons group article)))
3666
3667         ;; Decode charsets.
3668         (run-hooks 'gnus-article-decode-hook)
3669         ;; Mark article as decoded or not.
3670         (setq gnus-article-decoded-p gnus-article-decode-hook))
3671
3672       ;; Update sparse articles.
3673       (when (and do-update-line
3674                  (or (numberp article)
3675                      (stringp article)))
3676         (let ((buf (current-buffer)))
3677           (set-buffer gnus-summary-buffer)
3678           (gnus-summary-update-article do-update-line sparse-header)
3679           (gnus-summary-goto-subject do-update-line nil t)
3680           (set-window-point (get-buffer-window (current-buffer) t)
3681                             (point))
3682           (set-buffer buf))))))
3683
3684 ;;;
3685 ;;; Article editing
3686 ;;;
3687
3688 (defcustom gnus-article-edit-mode-hook nil
3689   "Hook run in article edit mode buffers."
3690   :group 'gnus-article-various
3691   :type 'hook)
3692
3693 (defvar gnus-article-edit-done-function nil)
3694
3695 (defvar gnus-article-edit-mode-map nil)
3696
3697 ;; Should we be using derived.el for this?
3698 (unless gnus-article-edit-mode-map
3699   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3700   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3701
3702   (gnus-define-keys gnus-article-edit-mode-map
3703     "\C-c\C-c" gnus-article-edit-done
3704     "\C-c\C-k" gnus-article-edit-exit)
3705
3706   (gnus-define-keys (gnus-article-edit-wash-map
3707                      "\C-c\C-w" gnus-article-edit-mode-map)
3708     "f" gnus-article-edit-full-stops))
3709
3710 (defun gnus-article-edit-mode ()
3711   "Major mode for editing articles.
3712 This is an extended text-mode.
3713
3714 \\{gnus-article-edit-mode-map}"
3715   (interactive)
3716   (setq major-mode 'gnus-article-edit-mode)
3717   (setq mode-name "Article Edit")
3718   (use-local-map gnus-article-edit-mode-map)
3719   (make-local-variable 'gnus-article-edit-done-function)
3720   (make-local-variable 'gnus-prev-winconf)
3721   (setq buffer-read-only nil)
3722   (buffer-enable-undo)
3723   (widen)
3724   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3725
3726 (defun gnus-article-edit (&optional force)
3727   "Edit the current article.
3728 This will have permanent effect only in mail groups.
3729 If FORCE is non-nil, allow editing of articles even in read-only
3730 groups."
3731   (interactive "P")
3732   (when (and (not force)
3733              (gnus-group-read-only-p))
3734     (error "The current newsgroup does not support article editing"))
3735   (gnus-article-date-original)
3736   (gnus-article-edit-article
3737    'ignore
3738    `(lambda (no-highlight)
3739       'ignore
3740       (gnus-summary-edit-article-done
3741        ,(or (mail-header-references gnus-current-headers) "")
3742        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3743
3744 (defun gnus-article-edit-article (start-func exit-func)
3745   "Start editing the contents of the current article buffer."
3746   (let ((winconf (current-window-configuration)))
3747     (set-buffer gnus-article-buffer)
3748     (gnus-article-edit-mode)
3749     (funcall start-func)
3750     (gnus-configure-windows 'edit-article)
3751     (setq gnus-article-edit-done-function exit-func)
3752     (setq gnus-prev-winconf winconf)
3753     (gnus-message 6 "C-c C-c to end edits")))
3754
3755 (defun gnus-article-edit-done (&optional arg)
3756   "Update the article edits and exit."
3757   (interactive "P")
3758   (save-excursion
3759     (save-restriction
3760       (widen)
3761       (when (article-goto-body)
3762         (let ((lines (count-lines (point) (point-max)))
3763               (length (- (point-max) (point)))
3764               (case-fold-search t)
3765               (body (copy-marker (point))))
3766           (goto-char (point-min))
3767           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3768             (delete-region (match-beginning 1) (match-end 1))
3769             (insert (number-to-string length)))
3770           (goto-char (point-min))
3771           (when (re-search-forward
3772                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3773             (delete-region (match-beginning 1) (match-end 1))
3774             (insert (number-to-string length)))
3775           (goto-char (point-min))
3776           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3777             (delete-region (match-beginning 1) (match-end 1))
3778             (insert (number-to-string lines)))))))
3779   (let ((func gnus-article-edit-done-function)
3780         (buf (current-buffer))
3781         (start (window-start)))
3782     (gnus-article-edit-exit)
3783     (save-excursion
3784       (set-buffer buf)
3785       (let ((buffer-read-only nil))
3786         (funcall func arg))
3787       ;; The cache and backlog have to be flushed somewhat.
3788       (when gnus-keep-backlog
3789         (gnus-backlog-remove-article
3790          (car gnus-article-current) (cdr gnus-article-current)))
3791       ;; Flush original article as well.
3792       (save-excursion
3793         (when (get-buffer gnus-original-article-buffer)
3794           (set-buffer gnus-original-article-buffer)
3795           (setq gnus-original-article nil)))
3796       (when gnus-use-cache
3797         (gnus-cache-update-article
3798          (car gnus-article-current) (cdr gnus-article-current))))
3799     (set-buffer buf)
3800     (set-window-start (get-buffer-window buf) start)
3801     (set-window-point (get-buffer-window buf) (point))))
3802
3803 (defun gnus-article-edit-exit ()
3804   "Exit the article editing without updating."
3805   (interactive)
3806   ;; We remove all text props from the article buffer.
3807   (let ((buf (format "%s" (buffer-string)))
3808         (curbuf (current-buffer))
3809         (p (point))
3810         (window-start (window-start)))
3811     (erase-buffer)
3812     (insert buf)
3813     (let ((winconf gnus-prev-winconf))
3814       (gnus-article-mode)
3815       (set-window-configuration winconf)
3816       ;; Tippy-toe some to make sure that point remains where it was.
3817       (save-current-buffer
3818         (set-buffer curbuf)
3819         (set-window-start (get-buffer-window (current-buffer)) window-start)
3820         (goto-char p)))))
3821
3822 (defun gnus-article-edit-full-stops ()
3823   "Interactively repair spacing at end of sentences."
3824   (interactive)
3825   (save-excursion
3826     (goto-char (point-min))
3827     (search-forward-regexp "^$" nil t)
3828     (let ((case-fold-search nil))
3829       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3830
3831 ;;;
3832 ;;; Article highlights
3833 ;;;
3834
3835 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
3836
3837 ;;; Internal Variables:
3838
3839 (defcustom gnus-button-url-regexp "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)"
3840   "Regular expression that matches URLs."
3841   :group 'gnus-article-buttons
3842   :type 'regexp)
3843
3844 (defcustom gnus-button-alist
3845   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
3846      0 t gnus-button-message-id 2)
3847     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
3848     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
3849      1 t
3850      gnus-button-fetch-group 4)
3851     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
3852     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
3853      t gnus-button-message-id 3)
3854     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
3855     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
3856     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
3857     ;; This is how URLs _should_ be embedded in text...
3858     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
3859     ;; Raw URLs.
3860     (,gnus-button-url-regexp 0 t gnus-button-url 0))
3861   "*Alist of regexps matching buttons in article bodies.
3862
3863 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
3864 REGEXP: is the string matching text around the button,
3865 BUTTON: is the number of the regexp grouping actually matching the button,
3866 FORM: is a lisp expression which must eval to true for the button to
3867 be added,
3868 CALLBACK: is the function to call when the user push this button, and each
3869 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
3870
3871 CALLBACK can also be a variable, in that case the value of that
3872 variable it the real callback function."
3873   :group 'gnus-article-buttons
3874   :type '(repeat (list regexp
3875                        (integer :tag "Button")
3876                        (sexp :tag "Form")
3877                        (function :tag "Callback")
3878                        (repeat :tag "Par"
3879                                :inline t
3880                                (integer :tag "Regexp group")))))
3881
3882 (defcustom gnus-header-button-alist
3883   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
3884      0 t gnus-button-message-id 0)
3885     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
3886     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
3887      0 t gnus-button-mailto 0)
3888     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3889     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3890     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3891     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
3892      gnus-button-message-id 3))
3893   "*Alist of headers and regexps to match buttons in article heads.
3894
3895 This alist is very similar to `gnus-button-alist', except that each
3896 alist has an additional HEADER element first in each entry:
3897
3898 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
3899
3900 HEADER is a regexp to match a header.  For a fuller explanation, see
3901 `gnus-button-alist'."
3902   :group 'gnus-article-buttons
3903   :group 'gnus-article-headers
3904   :type '(repeat (list (regexp :tag "Header")
3905                        regexp
3906                        (integer :tag "Button")
3907                        (sexp :tag "Form")
3908                        (function :tag "Callback")
3909                        (repeat :tag "Par"
3910                                :inline t
3911                                (integer :tag "Regexp group")))))
3912
3913 (defvar gnus-button-regexp nil)
3914 (defvar gnus-button-marker-list nil)
3915 ;; Regexp matching any of the regexps from `gnus-button-alist'.
3916
3917 (defvar gnus-button-last nil)
3918 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
3919
3920 ;;; Commands:
3921
3922 (defun gnus-article-push-button (event)
3923   "Check text under the mouse pointer for a callback function.
3924 If the text under the mouse pointer has a `gnus-callback' property,
3925 call it with the value of the `gnus-data' text property."
3926   (interactive "e")
3927   (set-buffer (window-buffer (posn-window (event-start event))))
3928   (let* ((pos (posn-point (event-start event)))
3929          (data (get-text-property pos 'gnus-data))
3930          (fun (get-text-property pos 'gnus-callback)))
3931     (goto-char pos)
3932     (when fun
3933       (funcall fun data))))
3934
3935 (defun gnus-article-press-button ()
3936   "Check text at point for a callback function.
3937 If the text at point has a `gnus-callback' property,
3938 call it with the value of the `gnus-data' text property."
3939   (interactive)
3940   (let* ((data (get-text-property (point) 'gnus-data))
3941          (fun (get-text-property (point) 'gnus-callback)))
3942     (when fun
3943       (funcall fun data))))
3944
3945 (defun gnus-article-highlight (&optional force)
3946   "Highlight current article.
3947 This function calls `gnus-article-highlight-headers',
3948 `gnus-article-highlight-citation',
3949 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3950 do the highlighting.  See the documentation for those functions."
3951   (interactive (list 'force))
3952   (gnus-article-highlight-headers)
3953   (gnus-article-highlight-citation force)
3954   (gnus-article-highlight-signature)
3955   (gnus-article-add-buttons force)
3956   (gnus-article-add-buttons-to-head))
3957
3958 (defun gnus-article-highlight-some (&optional force)
3959   "Highlight current article.
3960 This function calls `gnus-article-highlight-headers',
3961 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3962 do the highlighting.  See the documentation for those functions."
3963   (interactive (list 'force))
3964   (gnus-article-highlight-headers)
3965   (gnus-article-highlight-signature)
3966   (gnus-article-add-buttons))
3967
3968 (defun gnus-article-highlight-headers ()
3969   "Highlight article headers as specified by `gnus-header-face-alist'."
3970   (interactive)
3971   (save-excursion
3972     (set-buffer gnus-article-buffer)
3973     (save-restriction
3974       (let ((alist gnus-header-face-alist)
3975             (buffer-read-only nil)
3976             (case-fold-search t)
3977             (inhibit-point-motion-hooks t)
3978             entry regexp header-face field-face from hpoints fpoints)
3979         (article-narrow-to-head)
3980         (while (setq entry (pop alist))
3981           (goto-char (point-min))
3982           (setq regexp (concat "^\\("
3983                                (if (string-equal "" (nth 0 entry))
3984                                    "[^\t ]"
3985                                  (nth 0 entry))
3986                                "\\)")
3987                 header-face (nth 1 entry)
3988                 field-face (nth 2 entry))
3989           (while (and (re-search-forward regexp nil t)
3990                       (not (eobp)))
3991             (beginning-of-line)
3992             (setq from (point))
3993             (unless (search-forward ":" nil t)
3994               (forward-char 1))
3995             (when (and header-face
3996                        (not (memq (point) hpoints)))
3997               (push (point) hpoints)
3998               (gnus-put-text-property from (point) 'face header-face))
3999             (when (and field-face
4000                        (not (memq (setq from (point)) fpoints)))
4001               (push from fpoints)
4002               (if (re-search-forward "^[^ \t]" nil t)
4003                   (forward-char -2)
4004                 (goto-char (point-max)))
4005               (gnus-put-text-property from (point) 'face field-face))))))))
4006
4007 (defun gnus-article-highlight-signature ()
4008   "Highlight the signature in an article.
4009 It does this by highlighting everything after
4010 `gnus-signature-separator' using `gnus-signature-face'."
4011   (interactive)
4012   (save-excursion
4013     (set-buffer gnus-article-buffer)
4014     (let ((buffer-read-only nil)
4015           (inhibit-point-motion-hooks t))
4016       (save-restriction
4017         (when (and gnus-signature-face
4018                    (gnus-article-narrow-to-signature))
4019           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4020                             'face gnus-signature-face)
4021           (widen)
4022           (gnus-article-search-signature)
4023           (let ((start (match-beginning 0))
4024                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4025             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4026                                      end)))))))
4027
4028 (defun gnus-button-in-region-p (b e prop)
4029   "Say whether PROP exists in the region."
4030   (text-property-not-all b e prop nil))
4031
4032 (defun gnus-article-add-buttons (&optional force)
4033   "Find external references in the article and make buttons of them.
4034 \"External references\" are things like Message-IDs and URLs, as
4035 specified by `gnus-button-alist'."
4036   (interactive (list 'force))
4037   (save-excursion
4038     (set-buffer gnus-article-buffer)
4039     (let ((buffer-read-only nil)
4040           (inhibit-point-motion-hooks t)
4041           (case-fold-search t)
4042           (alist gnus-button-alist)
4043           beg entry regexp)
4044       ;; Remove all old markers.
4045       (let (marker entry)
4046         (while (setq marker (pop gnus-button-marker-list))
4047           (goto-char marker)
4048           (when (setq entry (gnus-button-entry))
4049             (put-text-property (match-beginning (nth 1 entry))
4050                                (match-end (nth 1 entry))
4051                                'gnus-callback nil))
4052           (set-marker marker nil)))
4053       ;; We skip the headers.
4054       (article-goto-body)
4055       (setq beg (point))
4056       (while (setq entry (pop alist))
4057         (setq regexp (car entry))
4058         (goto-char beg)
4059         (while (re-search-forward regexp nil t)
4060           (let* ((start (and entry (match-beginning (nth 1 entry))))
4061                  (end (and entry (match-end (nth 1 entry))))
4062                  (from (match-beginning 0)))
4063             (when (and (or (eq t (nth 2 entry))
4064                            (eval (nth 2 entry)))
4065                        (not (gnus-button-in-region-p
4066                              start end 'gnus-callback)))
4067               ;; That optional form returned non-nil, so we add the
4068               ;; button.
4069               (gnus-article-add-button
4070                start end 'gnus-button-push
4071                (car (push (set-marker (make-marker) from)
4072                           gnus-button-marker-list))))))))))
4073
4074 ;; Add buttons to the head of an article.
4075 (defun gnus-article-add-buttons-to-head ()
4076   "Add buttons to the head of the article."
4077   (interactive)
4078   (save-excursion
4079     (set-buffer gnus-article-buffer)
4080     (save-restriction
4081       (let ((buffer-read-only nil)
4082             (inhibit-point-motion-hooks t)
4083             (case-fold-search t)
4084             (alist gnus-header-button-alist)
4085             entry beg end)
4086         (article-narrow-to-head)
4087         (while alist
4088           ;; Each alist entry.
4089           (setq entry (car alist)
4090                 alist (cdr alist))
4091           (goto-char (point-min))
4092           (while (re-search-forward (car entry) nil t)
4093             ;; Each header matching the entry.
4094             (setq beg (match-beginning 0))
4095             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4096                                (match-beginning 0))
4097                           (point-max)))
4098             (goto-char beg)
4099             (while (re-search-forward (nth 1 entry) end t)
4100               ;; Each match within a header.
4101               (let* ((entry (cdr entry))
4102                      (start (match-beginning (nth 1 entry)))
4103                      (end (match-end (nth 1 entry)))
4104                      (form (nth 2 entry)))
4105                 (goto-char (match-end 0))
4106                 (when (eval form)
4107                   (gnus-article-add-button
4108                    start end (nth 3 entry)
4109                    (buffer-substring (match-beginning (nth 4 entry))
4110                                      (match-end (nth 4 entry)))))))
4111             (goto-char end)))))))
4112
4113 ;;; External functions:
4114
4115 (defun gnus-article-add-button (from to fun &optional data)
4116   "Create a button between FROM and TO with callback FUN and data DATA."
4117   (when gnus-article-button-face
4118     (gnus-overlay-put (gnus-make-overlay from to)
4119                       'face gnus-article-button-face))
4120   (gnus-add-text-properties
4121    from to
4122    (nconc (and gnus-article-mouse-face
4123                (list gnus-mouse-face-prop gnus-article-mouse-face))
4124           (list 'gnus-callback fun)
4125           (and data (list 'gnus-data data))))
4126   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4127                          :button-keymap gnus-widget-button-keymap))
4128
4129 ;;; Internal functions:
4130
4131 (defun gnus-article-set-globals ()
4132   (save-excursion
4133     (set-buffer gnus-summary-buffer)
4134     (gnus-set-global-variables)))
4135
4136 (defun gnus-signature-toggle (end)
4137   (save-excursion
4138     (set-buffer gnus-article-buffer)
4139     (let ((buffer-read-only nil)
4140           (inhibit-point-motion-hooks t))
4141       (if (get-text-property end 'invisible)
4142           (gnus-article-unhide-text end (point-max))
4143         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4144
4145 (defun gnus-button-entry ()
4146   ;; Return the first entry in `gnus-button-alist' matching this place.
4147   (let ((alist gnus-button-alist)
4148         (entry nil))
4149     (while alist
4150       (setq entry (pop alist))
4151       (if (looking-at (car entry))
4152           (setq alist nil)
4153         (setq entry nil)))
4154     entry))
4155
4156 (defun gnus-button-push (marker)
4157   ;; Push button starting at MARKER.
4158   (save-excursion
4159     (goto-char marker)
4160     (let* ((entry (gnus-button-entry))
4161            (inhibit-point-motion-hooks t)
4162            (fun (nth 3 entry))
4163            (args (mapcar (lambda (group)
4164                            (let ((string (match-string group)))
4165                              (gnus-set-text-properties
4166                               0 (length string) nil string)
4167                              string))
4168                          (nthcdr 4 entry))))
4169       (cond
4170        ((fboundp fun)
4171         (apply fun args))
4172        ((and (boundp fun)
4173              (fboundp (symbol-value fun)))
4174         (apply (symbol-value fun) args))
4175        (t
4176         (gnus-message 1 "You must define `%S' to use this button"
4177                       (cons fun args)))))))
4178
4179 (defun gnus-button-message-id (message-id)
4180   "Fetch MESSAGE-ID."
4181   (save-excursion
4182     (set-buffer gnus-summary-buffer)
4183     (gnus-summary-refer-article message-id)))
4184
4185 (defun gnus-button-fetch-group (address)
4186   "Fetch GROUP specified by ADDRESS."
4187   (if (not (string-match "[:/]" address))
4188       ;; This is just a simple group url.
4189       (gnus-group-read-ephemeral-group address gnus-select-method)
4190     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4191                            address))
4192         (error "Can't parse %s" address)
4193       (gnus-group-read-ephemeral-group
4194        (match-string 4 address)
4195        `(nntp ,(match-string 1 address)
4196               (nntp-address ,(match-string 1 address))
4197               (nntp-port-number ,(if (match-end 3)
4198                                      (match-string 3 address)
4199                                    "nntp")))))))
4200
4201 (defun gnus-url-parse-query-string (query &optional downcase)
4202   (let (retval pairs cur key val)
4203     (setq pairs (split-string query "&"))
4204     (while pairs
4205       (setq cur (car pairs)
4206             pairs (cdr pairs))
4207       (if (not (string-match "=" cur))
4208           nil                           ; Grace
4209         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4210               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4211         (if downcase
4212             (setq key (downcase key)))
4213         (setq cur (assoc key retval))
4214         (if cur
4215             (setcdr cur (cons val (cdr cur)))
4216           (setq retval (cons (list key val) retval)))))
4217     retval))
4218
4219 (defun gnus-url-unhex (x)
4220   (if (> x ?9)
4221       (if (>= x ?a)
4222           (+ 10 (- x ?a))
4223         (+ 10 (- x ?A)))
4224     (- x ?0)))
4225
4226 (defun gnus-url-unhex-string (str &optional allow-newlines)
4227   "Remove %XXX embedded spaces, etc in a url.
4228 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4229 decoding of carriage returns and line feeds in the string, which is normally
4230 forbidden in URL encoding."
4231   (setq str (or str ""))
4232   (let ((tmp "")
4233         (case-fold-search t))
4234     (while (string-match "%[0-9a-f][0-9a-f]" str)
4235       (let* ((start (match-beginning 0))
4236              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4237              (code (+ (* 16 ch1)
4238                       (gnus-url-unhex (elt str (+ start 2))))))
4239         (setq tmp (concat
4240                    tmp (substring str 0 start)
4241                    (cond
4242                     (allow-newlines
4243                      (char-to-string code))
4244                     ((or (= code ?\n) (= code ?\r))
4245                      " ")
4246                     (t (char-to-string code))))
4247               str (substring str (match-end 0)))))
4248     (setq tmp (concat tmp str))
4249     tmp))
4250
4251 (defun gnus-url-mailto (url)
4252   ;; Send mail to someone
4253   (when (string-match "mailto:/*\\(.*\\)" url)
4254     (setq url (substring url (match-beginning 1) nil)))
4255   (let (to args subject func)
4256     (if (string-match (regexp-quote "?") url)
4257         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4258               args (gnus-url-parse-query-string
4259                     (substring url (match-end 0) nil) t))
4260       (setq to (gnus-url-unhex-string url)))
4261     (setq args (cons (list "to" to) args)
4262           subject (cdr-safe (assoc "subject" args)))
4263     (message-mail)
4264     (while args
4265       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4266       (if (fboundp func)
4267           (funcall func)
4268         (message-position-on-field (caar args)))
4269       (insert (mapconcat 'identity (cdar args) ", "))
4270       (setq args (cdr args)))
4271     (if subject
4272         (message-goto-body)
4273       (message-goto-subject))))
4274
4275 (defun gnus-button-mailto (address)
4276   ;; Mail to ADDRESS.
4277   (set-buffer (gnus-copy-article-buffer))
4278   (message-reply address))
4279
4280 (defun gnus-button-reply (address)
4281   ;; Reply to ADDRESS.
4282   (message-reply address))
4283
4284 (defun gnus-button-url (address)
4285   "Browse ADDRESS."
4286   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4287   (if (listp browse-url-browser-function)
4288       (browse-url address)
4289     (funcall browse-url-browser-function address)))
4290
4291 (defun gnus-button-embedded-url (address)
4292   "Browse ADDRESS."
4293   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4294   (if (listp browse-url-browser-function)
4295       (browse-url (gnus-strip-whitespace address))
4296     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
4297
4298 ;;; Next/prev buttons in the article buffer.
4299
4300 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4301 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4302
4303 (defvar gnus-prev-page-map nil)
4304 (unless gnus-prev-page-map
4305   (setq gnus-prev-page-map (make-sparse-keymap))
4306   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4307   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4308
4309 (defun gnus-insert-prev-page-button ()
4310   (let ((buffer-read-only nil))
4311     (gnus-eval-format
4312      gnus-prev-page-line-format nil
4313      `(gnus-prev t local-map ,gnus-prev-page-map
4314                  gnus-callback gnus-article-button-prev-page
4315                  article-type annotation))))
4316
4317 (defvar gnus-next-page-map nil)
4318 (unless gnus-next-page-map
4319   (setq gnus-next-page-map (make-keymap))
4320   (suppress-keymap gnus-prev-page-map)
4321   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4322   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4323
4324 (defun gnus-button-next-page ()
4325   "Go to the next page."
4326   (interactive)
4327   (let ((win (selected-window)))
4328     (select-window (get-buffer-window gnus-article-buffer t))
4329     (gnus-article-next-page)
4330     (select-window win)))
4331
4332 (defun gnus-button-prev-page ()
4333   "Go to the prev page."
4334   (interactive)
4335   (let ((win (selected-window)))
4336     (select-window (get-buffer-window gnus-article-buffer t))
4337     (gnus-article-prev-page)
4338     (select-window win)))
4339
4340 (defun gnus-insert-next-page-button ()
4341   (let ((buffer-read-only nil))
4342     (gnus-eval-format gnus-next-page-line-format nil
4343                       `(gnus-next
4344                         t local-map ,gnus-next-page-map
4345                         gnus-callback gnus-article-button-next-page
4346                         article-type annotation))))
4347
4348 (defun gnus-article-button-next-page (arg)
4349   "Go to the next page."
4350   (interactive "P")
4351   (let ((win (selected-window)))
4352     (select-window (get-buffer-window gnus-article-buffer t))
4353     (gnus-article-next-page)
4354     (select-window win)))
4355
4356 (defun gnus-article-button-prev-page (arg)
4357   "Go to the prev page."
4358   (interactive "P")
4359   (let ((win (selected-window)))
4360     (select-window (get-buffer-window gnus-article-buffer t))
4361     (gnus-article-prev-page)
4362     (select-window win)))
4363
4364 (defvar gnus-decode-header-methods
4365   '(mail-decode-encoded-word-region)
4366   "List of methods used to decode headers.
4367
4368 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
4369 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
4370 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4371 whose names match REGEXP.
4372
4373 For example:
4374 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4375  mail-decode-encoded-word-region
4376  (\"chinese\" . rfc1843-decode-region))
4377 ")
4378
4379 (defvar gnus-decode-header-methods-cache nil)
4380
4381 (defun gnus-multi-decode-header (start end)
4382   "Apply the functions from `gnus-encoded-word-methods' that match."
4383   (unless (and gnus-decode-header-methods-cache
4384                (eq gnus-newsgroup-name
4385                    (car gnus-decode-header-methods-cache)))
4386     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4387     (mapc '(lambda (x)
4388              (if (symbolp x)
4389                  (nconc gnus-decode-header-methods-cache (list x))
4390                (if (and gnus-newsgroup-name
4391                         (string-match (car x) gnus-newsgroup-name))
4392                    (nconc gnus-decode-header-methods-cache
4393                           (list (cdr x))))))
4394           gnus-decode-header-methods))
4395   (let ((xlist gnus-decode-header-methods-cache))
4396     (pop xlist)
4397     (save-restriction
4398       (narrow-to-region start end)
4399       (while xlist
4400         (funcall (pop xlist) (point-min) (point-max))))))
4401
4402 ;;;
4403 ;;; Treatment top-level handling.
4404 ;;;
4405
4406 (defun gnus-treat-article (condition &optional part-number total-parts type)
4407   (let ((length (- (point-max) (point-min)))
4408         (alist gnus-treatment-function-alist)
4409         (article-goto-body-goes-to-point-min-p t)
4410         (treated-type
4411          (or (not type)
4412              (catch 'found
4413                (let ((list gnus-article-treat-types))
4414                  (while list
4415                    (when (string-match (pop list) type)
4416                      (throw 'found t)))))))
4417         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4418         val elem)
4419     (gnus-run-hooks 'gnus-part-display-hook)
4420     (while (setq elem (pop alist))
4421       (setq val (symbol-value (car elem)))
4422       (when (and (or (consp val)
4423                      treated-type)
4424                  (gnus-treat-predicate val)
4425                  (or (not (get (car elem) 'highlight))
4426                      highlightp))
4427         (save-restriction
4428           (funcall (cadr elem)))))))
4429
4430 ;; Dynamic variables.
4431 (defvar part-number)
4432 (defvar total-parts)
4433 (defvar type)
4434 (defvar condition)
4435 (defvar length)
4436 (defun gnus-treat-predicate (val)
4437   (cond
4438    (condition
4439     (eq condition val))
4440    ((null val)
4441     nil)
4442    ((eq val t)
4443     t)
4444    ((eq val 'head)
4445     nil)
4446    ((eq val 'last)
4447     (eq part-number total-parts))
4448    ((numberp val)
4449     (< length val))
4450    ((and (listp val)
4451          (stringp (car val)))
4452     (apply 'gnus-or (mapcar `(lambda (s)
4453                                (string-match s ,(or gnus-newsgroup-name "")))
4454                             val)))
4455    ((listp val)
4456     (let ((pred (pop val)))
4457       (cond
4458        ((eq pred 'or)
4459         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4460        ((eq pred 'and)
4461         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4462        ((eq pred 'not)
4463         (not (gnus-treat-predicate val)))
4464        ((eq pred 'typep)
4465         (equal (cadr val) type))
4466        (t
4467         (error "%S is not a valid predicate" pred)))))
4468    (t
4469     (error "%S is not a valid value" val))))
4470
4471 (gnus-ems-redefine)
4472
4473 (provide 'gnus-art)
4474
4475 (run-hooks 'gnus-art-load-hook)
4476
4477 ;;; gnus-art.el ends here