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