* gnus-sum.el (gnus-read-move-group-name):
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996-2000 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 and/or Cc header." long-to)
173               (const :tag "Multiple To and/or Cc 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                     (cc (message-fetch-field "cc")))
1173                 (when (> (length to) 1024)
1174                   (gnus-article-hide-header "to"))
1175                 (when (> (length cc) 1024)
1176                   (gnus-article-hide-header "cc"))))
1177              ((eq elem 'many-to)
1178               (let ((to-count 0)
1179                     (cc-count 0))
1180                 (goto-char (point-min))
1181                 (while (re-search-forward "^to:" nil t)
1182                   (setq to-count (1+ to-count)))
1183                 (when (> to-count 1)
1184                   (while (> to-count 0)
1185                     (goto-char (point-min))
1186                     (save-restriction
1187                       (re-search-forward "^to:" nil nil to-count)
1188                       (forward-line -1)
1189                       (narrow-to-region (point) (point-max))
1190                       (gnus-article-hide-header "to"))
1191                     (setq to-count (1- to-count))))
1192                 (goto-char (point-min))
1193                 (while (re-search-forward "^cc:" nil t)
1194                   (setq cc-count (1+ cc-count)))
1195                 (when (> cc-count 1)
1196                   (while (> cc-count 0)
1197                     (goto-char (point-min))
1198                     (save-restriction
1199                       (re-search-forward "^cc:" nil nil cc-count)
1200                       (forward-line -1)
1201                       (narrow-to-region (point) (point-max))
1202                       (gnus-article-hide-header "cc"))
1203                     (setq cc-count (1- cc-count)))))))))))))
1204
1205 (defun gnus-article-hide-header (header)
1206   (save-excursion
1207     (goto-char (point-min))
1208     (when (re-search-forward (concat "^" header ":") nil t)
1209       (gnus-article-hide-text-type
1210        (progn (beginning-of-line) (point))
1211        (progn
1212          (end-of-line)
1213          (if (re-search-forward "^[^ \t]" nil t)
1214              (match-beginning 0)
1215            (point-max)))
1216        'boring-headers))))
1217
1218 (defvar gnus-article-normalized-header-length 40
1219   "Length of normalized headers.")
1220
1221 (defun article-normalize-headers ()
1222   "Make all header lines 40 characters long."
1223   (interactive)
1224   (let ((buffer-read-only nil)
1225         column)
1226     (save-excursion
1227       (save-restriction
1228         (article-narrow-to-head)
1229         (while (not (eobp))
1230           (cond
1231            ((< (setq column (- (gnus-point-at-eol) (point)))
1232                gnus-article-normalized-header-length)
1233             (end-of-line)
1234             (insert (make-string
1235                      (- gnus-article-normalized-header-length column)
1236                      ? )))
1237            ((> column gnus-article-normalized-header-length)
1238             (gnus-put-text-property
1239              (progn
1240                (forward-char gnus-article-normalized-header-length)
1241                (point))
1242              (gnus-point-at-eol)
1243              'invisible t))
1244            (t
1245             ;; Do nothing.
1246             ))
1247           (forward-line 1))))))
1248
1249 (defun article-treat-dumbquotes ()
1250   "Translate M******** sm*rtq**t*s into proper text.
1251 Note that this function guesses whether a character is a sm*rtq**t* or
1252 not, so it should only be used interactively."
1253   (interactive)
1254   (article-translate-strings gnus-article-dumbquotes-map))
1255
1256 (defun article-translate-characters (from to)
1257   "Translate all characters in the body of the article according to FROM and TO.
1258 FROM is a string of characters to translate from; to is a string of
1259 characters to translate to."
1260   (save-excursion
1261     (when (article-goto-body)
1262       (let ((buffer-read-only nil)
1263             (x (make-string 225 ?x))
1264             (i -1))
1265         (while (< (incf i) (length x))
1266           (aset x i i))
1267         (setq i 0)
1268         (while (< i (length from))
1269           (aset x (aref from i) (aref to i))
1270           (incf i))
1271         (translate-region (point) (point-max) x)))))
1272
1273 (defun article-translate-strings (map)
1274   "Translate all string in the body of the article according to MAP.
1275 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1276   (save-excursion
1277     (when (article-goto-body)
1278       (let ((buffer-read-only nil)
1279             elem)
1280         (while (setq elem (pop map))
1281           (save-excursion
1282             (while (search-forward (car elem) nil t)
1283               (replace-match (cadr elem)))))))))
1284
1285 (defun article-treat-overstrike ()
1286   "Translate overstrikes into bold text."
1287   (interactive)
1288   (save-excursion
1289     (when (article-goto-body)
1290       (let ((buffer-read-only nil))
1291         (while (search-forward "\b" nil t)
1292           (let ((next (char-after))
1293                 (previous (char-after (- (point) 2))))
1294             ;; We do the boldification/underlining by hiding the
1295             ;; overstrikes and putting the proper text property
1296             ;; on the letters.
1297             (cond
1298              ((eq next previous)
1299               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1300               (put-text-property (point) (1+ (point)) 'face 'bold))
1301              ((eq next ?_)
1302               (gnus-article-hide-text-type
1303                (1- (point)) (1+ (point)) 'overstrike)
1304               (put-text-property
1305                (- (point) 2) (1- (point)) 'face 'underline))
1306              ((eq previous ?_)
1307               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1308               (put-text-property
1309                (point) (1+ (point)) 'face 'underline)))))))))
1310
1311 (defun article-fill-long-lines ()
1312   "Fill lines that are wider than the window width."
1313   (interactive)
1314   (save-excursion
1315     (let ((buffer-read-only nil)
1316           (width (window-width (get-buffer-window (current-buffer)))))
1317       (save-restriction
1318         (article-goto-body)
1319         (let ((adaptive-fill-mode nil))
1320           (while (not (eobp))
1321             (end-of-line)
1322             (when (>= (current-column) (min fill-column width))
1323               (narrow-to-region (point) (gnus-point-at-bol))
1324               (fill-paragraph nil)
1325               (goto-char (point-max))
1326               (widen))
1327             (forward-line 1)))))))
1328
1329 (defun article-capitalize-sentences ()
1330   "Capitalize the first word in each sentence."
1331   (interactive)
1332   (save-excursion
1333     (let ((buffer-read-only nil)
1334           (paragraph-start "^[\n\^L]"))
1335       (article-goto-body)
1336       (while (not (eobp))
1337         (capitalize-word 1)
1338         (forward-sentence)))))
1339
1340 (defun article-remove-cr ()
1341   "Remove trailing CRs and then translate remaining CRs into LFs."
1342   (interactive)
1343   (save-excursion
1344     (let ((buffer-read-only nil))
1345       (goto-char (point-min))
1346       (while (re-search-forward "\r+$" nil t)
1347         (replace-match "" t t))
1348       (goto-char (point-min))
1349       (while (search-forward "\r" nil t)
1350         (replace-match "\n" t t)))))
1351
1352 (defun article-remove-trailing-blank-lines ()
1353   "Remove all trailing blank lines from the article."
1354   (interactive)
1355   (save-excursion
1356     (let ((buffer-read-only nil))
1357       (goto-char (point-max))
1358       (delete-region
1359        (point)
1360        (progn
1361          (while (and (not (bobp))
1362                      (looking-at "^[ \t]*$")
1363                      (not (gnus-annotation-in-region-p
1364                            (point) (gnus-point-at-eol))))
1365            (forward-line -1))
1366          (forward-line 1)
1367          (point))))))
1368
1369 (defun article-display-x-face (&optional force)
1370   "Look for an X-Face header and display it if present."
1371   (interactive (list 'force))
1372   (save-excursion
1373     ;; Delete the old process, if any.
1374     (when (process-status "article-x-face")
1375       (delete-process "article-x-face"))
1376     (let ((inhibit-point-motion-hooks t)
1377           (case-fold-search t)
1378           from last)
1379       (save-restriction
1380         (article-narrow-to-head)
1381         (goto-char (point-min))
1382         (setq from (message-fetch-field "from"))
1383         (goto-char (point-min))
1384         (while (and gnus-article-x-face-command
1385                     (not last)
1386                     (or force
1387                         ;; Check whether this face is censored.
1388                         (not gnus-article-x-face-too-ugly)
1389                         (and gnus-article-x-face-too-ugly from
1390                              (not (string-match gnus-article-x-face-too-ugly
1391                                                 from))))
1392                     ;; Has to be present.
1393                     (re-search-forward "^X-Face: " nil t))
1394           ;; This used to try to do multiple faces (`while' instead of
1395           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1396           ;; work (b) it can crash some versions of Emacs (c) are
1397           ;; multiple faces really something to encourage?
1398           (when (stringp gnus-article-x-face-command)
1399             (setq last t))
1400           ;; We now have the area of the buffer where the X-Face is stored.
1401           (save-excursion
1402             (let ((beg (point))
1403                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1404               ;; We display the face.
1405               (if (symbolp gnus-article-x-face-command)
1406                   ;; The command is a lisp function, so we call it.
1407                   (if (gnus-functionp gnus-article-x-face-command)
1408                       (funcall gnus-article-x-face-command beg end)
1409                     (error "%s is not a function" gnus-article-x-face-command))
1410                 ;; The command is a string, so we interpret the command
1411                 ;; as a, well, command, and fork it off.
1412                 (let ((process-connection-type nil))
1413                   (process-kill-without-query
1414                    (start-process
1415                     "article-x-face" nil shell-file-name shell-command-switch
1416                     gnus-article-x-face-command))
1417                   (process-send-region "article-x-face" beg end)
1418                   (process-send-eof "article-x-face"))))))))))
1419
1420 (defun article-decode-mime-words ()
1421   "Decode all MIME-encoded words in the article."
1422   (interactive)
1423   (save-excursion
1424     (set-buffer gnus-article-buffer)
1425     (let ((inhibit-point-motion-hooks t)
1426           buffer-read-only
1427           (mail-parse-charset gnus-newsgroup-charset)
1428           (mail-parse-ignored-charsets 
1429            (save-excursion (set-buffer gnus-summary-buffer)
1430                            gnus-newsgroup-ignored-charsets)))
1431       (mail-decode-encoded-word-region (point-min) (point-max)))))
1432
1433 (defun article-decode-charset (&optional prompt)
1434   "Decode charset-encoded text in the article.
1435 If PROMPT (the prefix), prompt for a coding system to use."
1436   (interactive "P")
1437   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1438         buffer-read-only
1439         (mail-parse-charset gnus-newsgroup-charset)
1440         (mail-parse-ignored-charsets 
1441          (save-excursion (condition-case nil
1442                              (set-buffer gnus-summary-buffer)
1443                            (error))
1444                          gnus-newsgroup-ignored-charsets))
1445         ct cte ctl charset)
1446   (save-excursion
1447     (save-restriction
1448       (article-narrow-to-head)
1449       (setq ct (message-fetch-field "Content-Type" t)
1450             cte (message-fetch-field "Content-Transfer-Encoding" t)
1451             ctl (and ct (ignore-errors
1452                           (mail-header-parse-content-type ct)))
1453             charset (cond
1454                      (prompt
1455                       (mm-read-coding-system "Charset to decode: "))
1456                      (ctl
1457                       (mail-content-type-get ctl 'charset))))
1458       (when cte
1459         (setq cte (mail-header-strip cte)))
1460       (if (and ctl (not (string-match "/" (car ctl)))) 
1461           (setq ctl nil))
1462       (goto-char (point-max)))
1463     (forward-line 1)
1464     (save-restriction
1465       (narrow-to-region (point) (point-max))
1466       (when (and (or (not ctl)
1467                      (equal (car ctl) "text/plain")))
1468         (mm-decode-body
1469          charset (and cte (intern (downcase
1470                                    (gnus-strip-whitespace cte))))
1471          (car ctl)))))))
1472
1473 (defun article-decode-encoded-words ()
1474   "Remove encoded-word encoding from headers."
1475   (let ((inhibit-point-motion-hooks t)
1476         (mail-parse-charset gnus-newsgroup-charset)
1477         (mail-parse-ignored-charsets 
1478          (save-excursion (condition-case nil
1479                              (set-buffer gnus-summary-buffer)
1480                            (error))
1481                          gnus-newsgroup-ignored-charsets))
1482         buffer-read-only)
1483     (save-restriction
1484       (article-narrow-to-head)
1485       (funcall gnus-decode-header-function (point-min) (point-max)))))
1486
1487 (defun article-de-quoted-unreadable (&optional force)
1488   "Translate a quoted-printable-encoded article.
1489 If FORCE, decode the article whether it is marked as quoted-printable
1490 or not."
1491   (interactive (list 'force))
1492   (save-excursion
1493     (let ((buffer-read-only nil)
1494           (type (gnus-fetch-field "content-transfer-encoding"))
1495           (charset gnus-newsgroup-charset))
1496       (when (or force
1497                 (and type (string-match "quoted-printable" (downcase type))))
1498         (article-goto-body)
1499         (quoted-printable-decode-region (point) (point-max) charset)))))
1500
1501 (eval-when-compile
1502   (require 'rfc1843))
1503
1504 (defun article-decode-HZ ()
1505   "Translate a HZ-encoded article."
1506   (interactive)
1507   (require 'rfc1843)
1508   (save-excursion
1509     (let ((buffer-read-only nil))
1510       (rfc1843-decode-region (point-min) (point-max)))))
1511
1512 (defun article-hide-list-identifiers ()
1513   "Remove list identifies from the Subject header.
1514 The `gnus-list-identifiers' variable specifies what to do."
1515   (interactive)
1516   (save-excursion
1517     (save-restriction
1518       (let ((inhibit-point-motion-hooks t)
1519             buffer-read-only)
1520         (article-narrow-to-head)
1521         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1522                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1523           (when regexp
1524             (goto-char (point-min))
1525             (when (re-search-forward
1526                    (concat "^Subject: +\\(Re: +\\)?\\(" regexp " *\\)")
1527                    nil t)
1528               (delete-region (match-beginning 2) (match-end 0)))))))))
1529
1530 (defun article-hide-pgp ()
1531   "Remove any PGP headers and signatures in the current article."
1532   (interactive)
1533   (save-excursion
1534     (save-restriction
1535       (let ((inhibit-point-motion-hooks t)
1536             buffer-read-only beg end)
1537         (article-goto-body)
1538         ;; Hide the "header".
1539         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1540           (push 'pgp gnus-article-wash-types)
1541           (delete-region (match-beginning 0) (match-end 0))
1542           ;; Remove armor headers (rfc2440 6.2)
1543           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1544                                      (point)))
1545           (setq beg (point))
1546           ;; Hide the actual signature.
1547           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1548                (setq end (1+ (match-beginning 0)))
1549                (delete-region
1550                 end
1551                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1552                     (match-end 0)
1553                   ;; Perhaps we shouldn't hide to the end of the buffer
1554                   ;; if there is no end to the signature?
1555                   (point-max))))
1556           ;; Hide "- " PGP quotation markers.
1557           (when (and beg end)
1558             (narrow-to-region beg end)
1559             (goto-char (point-min))
1560             (while (re-search-forward "^- " nil t)
1561               (delete-region
1562                (match-beginning 0) (match-end 0)))
1563             (widen))
1564           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1565
1566 (defun article-hide-pem (&optional arg)
1567   "Toggle hiding of any PEM headers and signatures in the current article.
1568 If given a negative prefix, always show; if given a positive prefix,
1569 always hide."
1570   (interactive (gnus-article-hidden-arg))
1571   (unless (gnus-article-check-hidden-text 'pem arg)
1572     (save-excursion
1573       (let (buffer-read-only end)
1574         (goto-char (point-min))
1575         ;; Hide the horrendously ugly "header".
1576         (when (and (search-forward
1577                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1578                     nil t)
1579                    (setq end (1+ (match-beginning 0))))
1580           (push 'pem gnus-article-wash-types)
1581           (gnus-article-hide-text-type
1582            end
1583            (if (search-forward "\n\n" nil t)
1584                (match-end 0)
1585              (point-max))
1586            'pem)
1587           ;; Hide the trailer as well
1588           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1589                                 nil t)
1590             (gnus-article-hide-text-type
1591              (match-beginning 0) (match-end 0) 'pem)))))))
1592
1593 (defun article-strip-banner ()
1594   "Strip the banner specified by the `banner' group parameter."
1595   (interactive)
1596   (save-excursion
1597     (save-restriction
1598       (let ((inhibit-point-motion-hooks t)
1599             (banner (gnus-group-get-parameter gnus-newsgroup-name 'banner))
1600             (gnus-signature-limit nil)
1601             buffer-read-only beg end)
1602         (when banner
1603           (article-goto-body)
1604           (cond
1605            ((eq banner 'signature)
1606             (when (gnus-article-narrow-to-signature)
1607               (widen)
1608               (forward-line -1)
1609               (delete-region (point) (point-max))))
1610            ((stringp banner)
1611             (while (re-search-forward banner nil t)
1612               (delete-region (match-beginning 0) (match-end 0))))))))))
1613
1614 (defun article-babel ()
1615   "Translate article using an online translation service."
1616   (interactive)
1617   (require 'babel)
1618   (save-excursion
1619     (set-buffer gnus-article-buffer)
1620     (when (article-goto-body)
1621       (let* ((buffer-read-only nil)
1622              (start (point))
1623              (end (point-max))
1624              (orig (buffer-substring start end))
1625              (trans (babel-as-string orig)))
1626         (save-restriction
1627           (narrow-to-region start end)
1628           (delete-region start end)
1629           (insert trans))))))
1630
1631 (defun article-hide-signature (&optional arg)
1632   "Hide the signature in the current article.
1633 If given a negative prefix, always show; if given a positive prefix,
1634 always hide."
1635   (interactive (gnus-article-hidden-arg))
1636   (unless (gnus-article-check-hidden-text 'signature arg)
1637     (save-excursion
1638       (save-restriction
1639         (let ((buffer-read-only nil))
1640           (when (gnus-article-narrow-to-signature)
1641             (gnus-article-hide-text-type
1642              (point-min) (point-max) 'signature)))))))
1643
1644 (defun article-strip-headers-in-body ()
1645   "Strip offensive headers from bodies."
1646   (interactive)
1647   (save-excursion
1648     (article-goto-body)
1649     (let ((case-fold-search t))
1650       (when (looking-at "x-no-archive:")
1651         (gnus-delete-line)))))
1652
1653 (defun article-strip-leading-blank-lines ()
1654   "Remove all blank lines from the beginning of the article."
1655   (interactive)
1656   (save-excursion
1657     (let ((inhibit-point-motion-hooks t)
1658           buffer-read-only)
1659       (when (article-goto-body)
1660         (while (and (not (eobp))
1661                     (looking-at "[ \t]*$"))
1662           (gnus-delete-line))))))
1663
1664 (defun article-narrow-to-head ()
1665   "Narrow the buffer to the head of the message.
1666 Point is left at the beginning of the narrowed-to region."
1667   (narrow-to-region
1668    (goto-char (point-min))
1669    (if (search-forward "\n\n" nil 1)
1670        (1- (point))
1671      (point-max)))
1672   (goto-char (point-min)))
1673
1674 (defun article-goto-body ()
1675   "Place point at the start of the body."
1676   (goto-char (point-min))
1677   (cond
1678    ;; This variable is only bound when dealing with separate
1679    ;; MIME body parts.
1680    (article-goto-body-goes-to-point-min-p
1681     t)
1682    ((search-forward "\n\n" nil t)
1683     t)
1684    (t
1685     (goto-char (point-max))
1686     nil)))
1687
1688 (defun article-strip-multiple-blank-lines ()
1689   "Replace consecutive blank lines with one empty line."
1690   (interactive)
1691   (save-excursion
1692     (let ((inhibit-point-motion-hooks t)
1693           buffer-read-only)
1694       ;; First make all blank lines empty.
1695       (article-goto-body)
1696       (while (re-search-forward "^[ \t]+$" nil t)
1697         (unless (gnus-annotation-in-region-p
1698                  (match-beginning 0) (match-end 0))
1699           (replace-match "" nil t)))
1700       ;; Then replace multiple empty lines with a single empty line.
1701       (article-goto-body)
1702       (while (re-search-forward "\n\n\n+" nil t)
1703         (unless (gnus-annotation-in-region-p
1704                  (match-beginning 0) (match-end 0))
1705           (replace-match "\n\n" t t))))))
1706
1707 (defun article-strip-leading-space ()
1708   "Remove all white space from the beginning of the lines in the article."
1709   (interactive)
1710   (save-excursion
1711     (let ((inhibit-point-motion-hooks t)
1712           buffer-read-only)
1713       (article-goto-body)
1714       (while (re-search-forward "^[ \t]+" nil t)
1715         (replace-match "" t t)))))
1716
1717 (defun article-strip-trailing-space ()
1718   "Remove all white space from the end of the lines in the article."
1719   (interactive)
1720   (save-excursion
1721     (let ((inhibit-point-motion-hooks t)
1722           buffer-read-only)
1723       (article-goto-body)
1724       (while (re-search-forward "[ \t]+$" nil t)
1725         (replace-match "" t t)))))
1726
1727 (defun article-strip-blank-lines ()
1728   "Strip leading, trailing and multiple blank lines."
1729   (interactive)
1730   (article-strip-leading-blank-lines)
1731   (article-remove-trailing-blank-lines)
1732   (article-strip-multiple-blank-lines))
1733
1734 (defun article-strip-all-blank-lines ()
1735   "Strip all blank lines."
1736   (interactive)
1737   (save-excursion
1738     (let ((inhibit-point-motion-hooks t)
1739           buffer-read-only)
1740       (article-goto-body)
1741       (while (re-search-forward "^[ \t]*\n" nil t)
1742         (replace-match "" t t)))))
1743
1744 (defun gnus-article-narrow-to-signature ()
1745   "Narrow to the signature; return t if a signature is found, else nil."
1746   (let ((inhibit-point-motion-hooks t))
1747     (when (gnus-article-search-signature)
1748       (forward-line 1)
1749       ;; Check whether we have some limits to what we consider
1750       ;; to be a signature.
1751       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1752                       (list gnus-signature-limit)))
1753             limit limited)
1754         (while (setq limit (pop limits))
1755           (if (or (and (integerp limit)
1756                        (< (- (point-max) (point)) limit))
1757                   (and (floatp limit)
1758                        (< (count-lines (point) (point-max)) limit))
1759                   (and (gnus-functionp limit)
1760                        (funcall limit))
1761                   (and (stringp limit)
1762                        (not (re-search-forward limit nil t))))
1763               ()                        ; This limit did not succeed.
1764             (setq limited t
1765                   limits nil)))
1766         (unless limited
1767           (narrow-to-region (point) (point-max))
1768           t)))))
1769
1770 (defun gnus-article-search-signature ()
1771   "Search the current buffer for the signature separator.
1772 Put point at the beginning of the signature separator."
1773   (let ((cur (point)))
1774     (goto-char (point-max))
1775     (if (if (stringp gnus-signature-separator)
1776             (re-search-backward gnus-signature-separator nil t)
1777           (let ((seps gnus-signature-separator))
1778             (while (and seps
1779                         (not (re-search-backward (car seps) nil t)))
1780               (pop seps))
1781             seps))
1782         t
1783       (goto-char cur)
1784       nil)))
1785
1786 (defun gnus-article-hidden-arg ()
1787   "Return the current prefix arg as a number, or 0 if no prefix."
1788   (list (if current-prefix-arg
1789             (prefix-numeric-value current-prefix-arg)
1790           0)))
1791
1792 (defun gnus-article-check-hidden-text (type arg)
1793   "Return nil if hiding is necessary.
1794 Arg can be nil or a number.  Nil and positive means hide, negative
1795 means show, 0 means toggle."
1796   (save-excursion
1797     (save-restriction
1798       (let ((hide (gnus-article-hidden-text-p type)))
1799         (cond
1800          ((or (null arg)
1801               (> arg 0))
1802           nil)
1803          ((< arg 0)
1804           (gnus-article-show-hidden-text type))
1805          (t
1806           (if (eq hide 'hidden)
1807               (gnus-article-show-hidden-text type)
1808             nil)))))))
1809
1810 (defun gnus-article-hidden-text-p (type)
1811   "Say whether the current buffer contains hidden text of type TYPE."
1812   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1813     (while (and pos
1814                 (not (get-text-property pos 'invisible))
1815                 (not (get-text-property pos 'dummy-invisible)))
1816       (setq pos
1817             (text-property-any (1+ pos) (point-max) 'article-type type)))
1818     (if pos
1819         'hidden
1820       nil)))
1821
1822 (defun gnus-article-show-hidden-text (type &optional hide)
1823   "Show all hidden text of type TYPE.
1824 If HIDE, hide the text instead."
1825   (save-excursion
1826     (let ((buffer-read-only nil)
1827           (inhibit-point-motion-hooks t)
1828           (end (point-min))
1829           beg)
1830       (while (setq beg (text-property-any end (point-max) 'article-type type))
1831         (goto-char beg)
1832         (setq end (or
1833                    (text-property-not-all beg (point-max) 'article-type type)
1834                    (point-max)))
1835         (if hide
1836             (gnus-article-hide-text beg end gnus-hidden-properties)
1837           (gnus-article-unhide-text beg end))
1838         (goto-char end))
1839       t)))
1840
1841 (defconst article-time-units
1842   `((year . ,(* 365.25 24 60 60))
1843     (week . ,(* 7 24 60 60))
1844     (day . ,(* 24 60 60))
1845     (hour . ,(* 60 60))
1846     (minute . 60)
1847     (second . 1))
1848   "Mapping from time units to seconds.")
1849
1850 (defun article-date-ut (&optional type highlight header)
1851   "Convert DATE date to universal time in the current article.
1852 If TYPE is `local', convert to local time; if it is `lapsed', output
1853 how much time has lapsed since DATE.  For `lapsed', the value of
1854 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
1855 should replace the \"Date:\" one, or should be added below it."
1856   (interactive (list 'ut t))
1857   (let* ((header (or header
1858                      (message-fetch-field "date")
1859                      ""))
1860          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1861          (date-regexp
1862           (cond
1863            ((not gnus-article-date-lapsed-new-header)
1864             tdate-regexp)
1865            ((eq type 'lapsed)
1866             "^X-Sent:[ \t]")
1867            (t
1868             "^Date:[ \t]")))
1869          (date (if (vectorp header) (mail-header-date header)
1870                  header))
1871          (inhibit-point-motion-hooks t)
1872          pos
1873          bface eface)
1874     (save-excursion
1875       (save-restriction
1876         (article-narrow-to-head)
1877         (when (re-search-forward tdate-regexp nil t)
1878           (setq bface (get-text-property (gnus-point-at-bol) 'face)
1879                 date (or (get-text-property (gnus-point-at-bol)
1880                                             'original-date)
1881                          date)
1882                 eface (get-text-property (1- (gnus-point-at-eol)) 'face))
1883           (forward-line 1))
1884         (when (and date (not (string= date "")))
1885           (goto-char (point-min))
1886           (let ((buffer-read-only nil))
1887             ;; Delete any old Date headers.
1888             (while (re-search-forward date-regexp nil t)
1889               (if pos
1890                   (delete-region (progn (beginning-of-line) (point))
1891                                  (progn (forward-line 1) (point)))
1892                 (delete-region (progn (beginning-of-line) (point))
1893                                (progn (end-of-line) (point)))
1894                 (setq pos (point))))
1895             (when (and (not pos) (re-search-forward tdate-regexp nil t))
1896               (forward-line 1))
1897             (if pos (goto-char pos))
1898             (insert (article-make-date-line date (or type 'ut)))
1899             (when (not pos)
1900               (insert "\n")
1901               (forward-line -1))
1902             ;; Do highlighting.
1903             (beginning-of-line)
1904             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1905               (put-text-property (match-beginning 1) (1+ (match-end 1))
1906                                  'original-date date)
1907               (put-text-property (match-beginning 1) (1+ (match-end 1))
1908                                  'face bface)
1909               (put-text-property (match-beginning 2) (match-end 2)
1910                                  'face eface))))))))
1911
1912 (defun article-make-date-line (date type)
1913   "Return a DATE line of TYPE."
1914   (let ((time (condition-case ()
1915                   (date-to-time date)
1916                 (error '(0 0)))))
1917     (cond
1918      ;; Convert to the local timezone.  We have to slap a
1919      ;; `condition-case' round the calls to the timezone
1920      ;; functions since they aren't particularly resistant to
1921      ;; buggy dates.
1922      ((eq type 'local)
1923       (let ((tz (car (current-time-zone time))))
1924         (format "Date: %s %s%02d%02d" (current-time-string time)
1925                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
1926                 (/ (% (abs tz) 3600) 60))))
1927      ;; Convert to Universal Time.
1928      ((eq type 'ut)
1929       (concat "Date: "
1930               (current-time-string
1931                (let* ((e (parse-time-string date))
1932                       (tm (apply 'encode-time e))
1933                       (ms (car tm))
1934                       (ls (- (cadr tm) (car (current-time-zone time)))))
1935                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1936                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1937                        (t (list ms ls)))))
1938               " UT"))
1939      ;; Get the original date from the article.
1940      ((eq type 'original)
1941       (concat "Date: " (if (string-match "\n+$" date)
1942                            (substring date 0 (match-beginning 0))
1943                          date)))
1944      ;; Let the user define the format.
1945      ((eq type 'user)
1946       (if (gnus-functionp gnus-article-time-format)
1947           (funcall gnus-article-time-format time)
1948         (concat
1949          "Date: "
1950          (format-time-string gnus-article-time-format time))))
1951      ;; ISO 8601.
1952      ((eq type 'iso8601)
1953       (let ((tz (car (current-time-zone time))))
1954         (concat
1955          "Date: "
1956          (format-time-string "%Y%m%dT%H%M%S" time)
1957          (format "%s%02d%02d"
1958                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
1959                  (/ (% (abs tz) 3600) 60)))))
1960      ;; Do an X-Sent lapsed format.
1961      ((eq type 'lapsed)
1962       ;; If the date is seriously mangled, the timezone functions are
1963       ;; liable to bug out, so we ignore all errors.
1964       (let* ((now (current-time))
1965              (real-time (subtract-time now time))
1966              (real-sec (and real-time
1967                             (+ (* (float (car real-time)) 65536)
1968                                (cadr real-time))))
1969              (sec (and real-time (abs real-sec)))
1970              num prev)
1971         (cond
1972          ((null real-time)
1973           "X-Sent: Unknown")
1974          ((zerop sec)
1975           "X-Sent: Now")
1976          (t
1977           (concat
1978            "X-Sent: "
1979            ;; This is a bit convoluted, but basically we go
1980            ;; through the time units for years, weeks, etc,
1981            ;; and divide things to see whether that results
1982            ;; in positive answers.
1983            (mapconcat
1984             (lambda (unit)
1985               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1986                   ;; The (remaining) seconds are too few to
1987                   ;; be divided into this time unit.
1988                   ""
1989                 ;; It's big enough, so we output it.
1990                 (setq sec (- sec (* num (cdr unit))))
1991                 (prog1
1992                     (concat (if prev ", " "") (int-to-string
1993                                                (floor num))
1994                             " " (symbol-name (car unit))
1995                             (if (> num 1) "s" ""))
1996                   (setq prev t))))
1997             article-time-units "")
1998            ;; If dates are odd, then it might appear like the
1999            ;; article was sent in the future.
2000            (if (> real-sec 0)
2001                " ago"
2002              " in the future"))))))
2003      (t
2004       (error "Unknown conversion type: %s" type)))))
2005
2006 (defun article-date-local (&optional highlight)
2007   "Convert the current article date to the local timezone."
2008   (interactive (list t))
2009   (article-date-ut 'local highlight))
2010
2011 (defun article-date-original (&optional highlight)
2012   "Convert the current article date to what it was originally.
2013 This is only useful if you have used some other date conversion
2014 function and want to see what the date was before converting."
2015   (interactive (list t))
2016   (article-date-ut 'original highlight))
2017
2018 (defun article-date-lapsed (&optional highlight)
2019   "Convert the current article date to time lapsed since it was sent."
2020   (interactive (list t))
2021   (article-date-ut 'lapsed highlight))
2022
2023 (defun article-update-date-lapsed ()
2024   "Function to be run from a timer to update the lapsed time line."
2025   (let (deactivate-mark)
2026     (save-excursion
2027       (ignore-errors
2028         (walk-windows
2029          (lambda (w)
2030            (set-buffer (window-buffer w))
2031            (when (eq major-mode 'gnus-article-mode)
2032              (goto-char (point-min))
2033              (when (re-search-forward "^X-Sent:" nil t)
2034                (article-date-lapsed t))))
2035          nil 'visible)))))
2036
2037 (defun gnus-start-date-timer (&optional n)
2038   "Start a timer to update the X-Sent header in the article buffers.
2039 The numerical prefix says how frequently (in seconds) the function
2040 is to run."
2041   (interactive "p")
2042   (unless n
2043     (setq n 1))
2044   (gnus-stop-date-timer)
2045   (setq article-lapsed-timer
2046         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2047
2048 (defun gnus-stop-date-timer ()
2049   "Stop the X-Sent timer."
2050   (interactive)
2051   (when article-lapsed-timer
2052     (nnheader-cancel-timer article-lapsed-timer)
2053     (setq article-lapsed-timer nil)))
2054
2055 (defun article-date-user (&optional highlight)
2056   "Convert the current article date to the user-defined format.
2057 This format is defined by the `gnus-article-time-format' variable."
2058   (interactive (list t))
2059   (article-date-ut 'user highlight))
2060
2061 (defun article-date-iso8601 (&optional highlight)
2062   "Convert the current article date to ISO8601."
2063   (interactive (list t))
2064   (article-date-ut 'iso8601 highlight))
2065
2066 (defun article-show-all ()
2067   "Show all hidden text in the article buffer."
2068   (interactive)
2069   (save-excursion
2070     (let ((buffer-read-only nil))
2071       (gnus-article-unhide-text (point-min) (point-max)))))
2072
2073 (defun article-emphasize (&optional arg)
2074   "Emphasize text according to `gnus-emphasis-alist'."
2075   (interactive (gnus-article-hidden-arg))
2076   (unless (gnus-article-check-hidden-text 'emphasis arg)
2077     (save-excursion
2078       (let ((alist (or 
2079                     (condition-case nil
2080                         (with-current-buffer gnus-summary-buffer 
2081                           gnus-article-emphasis-alist) 
2082                       (error))
2083                     gnus-emphasis-alist))
2084             (buffer-read-only nil)
2085             (props (append '(article-type emphasis)
2086                            gnus-hidden-properties))
2087             regexp elem beg invisible visible face)
2088         (article-goto-body)
2089         (setq beg (point))
2090         (while (setq elem (pop alist))
2091           (goto-char beg)
2092           (setq regexp (car elem)
2093                 invisible (nth 1 elem)
2094                 visible (nth 2 elem)
2095                 face (nth 3 elem))
2096           (while (re-search-forward regexp nil t)
2097             (when (and (match-beginning visible) (match-beginning invisible))
2098               (push 'emphasis gnus-article-wash-types)
2099               (gnus-article-hide-text
2100                (match-beginning invisible) (match-end invisible) props)
2101               (gnus-article-unhide-text-type
2102                (match-beginning visible) (match-end visible) 'emphasis)
2103               (gnus-put-text-property-excluding-newlines
2104                (match-beginning visible) (match-end visible) 'face face)
2105               (goto-char (match-end invisible)))))))))
2106
2107 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2108   "Setup newsgroup emphasis alist."
2109   (unless gnus-article-emphasis-alist
2110     (let ((name (and gnus-newsgroup-name
2111                      (gnus-group-real-name gnus-newsgroup-name))))
2112       (make-local-variable 'gnus-article-emphasis-alist)
2113       (setq gnus-article-emphasis-alist 
2114             (nconc 
2115              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2116                (while (setq elem (pop alist))
2117                  (when (and name (string-match (car elem) name))
2118                    (setq alist nil
2119                          highlight (copy-sequence (cdr elem)))))
2120                highlight)
2121              (copy-sequence highlight-words)
2122              (if gnus-newsgroup-name
2123                  (copy-sequence (gnus-group-find-parameter 
2124                                  gnus-newsgroup-name 'highlight-words t)))
2125              gnus-emphasis-alist)))))
2126
2127 (defvar gnus-summary-article-menu)
2128 (defvar gnus-summary-post-menu)
2129
2130 ;;; Saving functions.
2131
2132 (defun gnus-article-save (save-buffer file &optional num)
2133   "Save the currently selected article."
2134   (unless gnus-save-all-headers
2135     ;; Remove headers according to `gnus-saved-headers'.
2136     (let ((gnus-visible-headers
2137            (or gnus-saved-headers gnus-visible-headers))
2138           (gnus-article-buffer save-buffer))
2139       (save-excursion
2140         (set-buffer save-buffer)
2141         (article-hide-headers 1 t))))
2142   (save-window-excursion
2143     (if (not gnus-default-article-saver)
2144         (error "No default saver is defined")
2145       ;; !!! Magic!  The saving functions all save
2146       ;; `gnus-save-article-buffer' (or so they think), but we
2147       ;; bind that variable to our save-buffer.
2148       (set-buffer gnus-article-buffer)
2149       (let* ((gnus-save-article-buffer save-buffer)
2150              (filename
2151               (cond
2152                ((not gnus-prompt-before-saving) 'default)
2153                ((eq gnus-prompt-before-saving 'always) nil)
2154                (t file)))
2155              (gnus-number-of-articles-to-be-saved
2156               (when (eq gnus-prompt-before-saving t)
2157                 num)))                  ; Magic
2158         (set-buffer gnus-article-current-summary)
2159         (funcall gnus-default-article-saver filename)))))
2160
2161 (defun gnus-read-save-file-name (prompt &optional filename
2162                                         function group headers variable)
2163   (let ((default-name
2164           (funcall function group headers (symbol-value variable)))
2165         result)
2166     (setq
2167      result
2168      (cond
2169       ((eq filename 'default)
2170        default-name)
2171       ((eq filename t)
2172        default-name)
2173       (filename filename)
2174       (t
2175        (let* ((split-name (gnus-get-split-value gnus-split-methods))
2176               (prompt
2177                (format prompt
2178                        (if (and gnus-number-of-articles-to-be-saved
2179                                 (> gnus-number-of-articles-to-be-saved 1))
2180                            (format "these %d articles"
2181                                    gnus-number-of-articles-to-be-saved)
2182                          "this article")))
2183               (file
2184                ;; Let the split methods have their say.
2185                (cond
2186                 ;; No split name was found.
2187                 ((null split-name)
2188                  (read-file-name
2189                   (concat prompt " (default "
2190                           (file-name-nondirectory default-name) ") ")
2191                   (file-name-directory default-name)
2192                   default-name))
2193                 ;; A single group name is returned.
2194                 ((stringp split-name)
2195                  (setq default-name
2196                        (funcall function split-name headers
2197                                 (symbol-value variable)))
2198                  (read-file-name
2199                   (concat prompt " (default "
2200                           (file-name-nondirectory default-name) ") ")
2201                   (file-name-directory default-name)
2202                   default-name))
2203                 ;; A single split name was found
2204                 ((= 1 (length split-name))
2205                  (let* ((name (expand-file-name
2206                                (car split-name) gnus-article-save-directory))
2207                         (dir (cond ((file-directory-p name)
2208                                     (file-name-as-directory name))
2209                                    ((file-exists-p name) name)
2210                                    (t gnus-article-save-directory))))
2211                    (read-file-name
2212                     (concat prompt " (default " name ") ")
2213                     dir name)))
2214                 ;; A list of splits was found.
2215                 (t
2216                  (setq split-name (nreverse split-name))
2217                  (let (result)
2218                    (let ((file-name-history
2219                           (nconc split-name file-name-history)))
2220                      (setq result
2221                            (expand-file-name
2222                             (read-file-name
2223                              (concat prompt " (`M-p' for defaults) ")
2224                              gnus-article-save-directory
2225                              (car split-name))
2226                             gnus-article-save-directory)))
2227                    (car (push result file-name-history)))))))
2228          ;; Create the directory.
2229          (gnus-make-directory (file-name-directory file))
2230          ;; If we have read a directory, we append the default file name.
2231          (when (file-directory-p file)
2232            (setq file (concat (file-name-as-directory file)
2233                               (file-name-nondirectory default-name))))
2234          ;; Possibly translate some characters.
2235          (nnheader-translate-file-chars file)))))
2236     (gnus-make-directory (file-name-directory result))
2237     (set variable result)))
2238
2239 (defun gnus-article-archive-name (group)
2240   "Return the first instance of an \"Archive-name\" in the current buffer."
2241   (let ((case-fold-search t))
2242     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2243       (nnheader-concat gnus-article-save-directory
2244                        (match-string 1)))))
2245
2246 (defun gnus-article-nndoc-name (group)
2247   "If GROUP is an nndoc group, return the name of the parent group."
2248   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2249     (gnus-group-get-parameter group 'save-article-group)))
2250
2251 (defun gnus-summary-save-in-rmail (&optional filename)
2252   "Append this article to Rmail file.
2253 Optional argument FILENAME specifies file name.
2254 Directory to save to is default to `gnus-article-save-directory'."
2255   (setq filename (gnus-read-save-file-name
2256                   "Save %s in rmail file:" filename
2257                   gnus-rmail-save-name gnus-newsgroup-name
2258                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2259   (gnus-eval-in-buffer-window gnus-save-article-buffer
2260     (save-excursion
2261       (save-restriction
2262         (widen)
2263         (gnus-output-to-rmail filename))))
2264   filename)
2265
2266 (defun gnus-summary-save-in-mail (&optional filename)
2267   "Append this article to Unix mail file.
2268 Optional argument FILENAME specifies file name.
2269 Directory to save to is default to `gnus-article-save-directory'."
2270   (setq filename (gnus-read-save-file-name
2271                   "Save %s in Unix mail file:" filename
2272                   gnus-mail-save-name gnus-newsgroup-name
2273                   gnus-current-headers 'gnus-newsgroup-last-mail))
2274   (gnus-eval-in-buffer-window gnus-save-article-buffer
2275     (save-excursion
2276       (save-restriction
2277         (widen)
2278         (if (and (file-readable-p filename)
2279                  (mail-file-babyl-p filename))
2280             (rmail-output-to-rmail-file filename t)
2281           (gnus-output-to-mail filename)))))
2282   filename)
2283
2284 (defun gnus-summary-save-in-file (&optional filename overwrite)
2285   "Append this article to file.
2286 Optional argument FILENAME specifies file name.
2287 Directory to save to is default to `gnus-article-save-directory'."
2288   (setq filename (gnus-read-save-file-name
2289                   "Save %s in file:" filename
2290                   gnus-file-save-name gnus-newsgroup-name
2291                   gnus-current-headers 'gnus-newsgroup-last-file))
2292   (gnus-eval-in-buffer-window gnus-save-article-buffer
2293     (save-excursion
2294       (save-restriction
2295         (widen)
2296         (when (and overwrite
2297                    (file-exists-p filename))
2298           (delete-file filename))
2299         (gnus-output-to-file filename))))
2300   filename)
2301
2302 (defun gnus-summary-write-to-file (&optional filename)
2303   "Write this article to a file.
2304 Optional argument FILENAME specifies file name.
2305 The directory to save in defaults to `gnus-article-save-directory'."
2306   (gnus-summary-save-in-file nil t))
2307
2308 (defun gnus-summary-save-body-in-file (&optional filename)
2309   "Append this article body to a file.
2310 Optional argument FILENAME specifies file name.
2311 The directory to save in defaults to `gnus-article-save-directory'."
2312   (setq filename (gnus-read-save-file-name
2313                   "Save %s body in file:" filename
2314                   gnus-file-save-name gnus-newsgroup-name
2315                   gnus-current-headers 'gnus-newsgroup-last-file))
2316   (gnus-eval-in-buffer-window gnus-save-article-buffer
2317     (save-excursion
2318       (save-restriction
2319         (widen)
2320         (when (article-goto-body)
2321           (narrow-to-region (point) (point-max)))
2322         (gnus-output-to-file filename))))
2323   filename)
2324
2325 (defun gnus-summary-save-in-pipe (&optional command)
2326   "Pipe this article to subprocess."
2327   (setq command
2328         (cond ((and (eq command 'default)
2329                     gnus-last-shell-command)
2330                gnus-last-shell-command)
2331               (command command)
2332               (t (read-string
2333                   (format
2334                    "Shell command on %s: "
2335                    (if (and gnus-number-of-articles-to-be-saved
2336                             (> gnus-number-of-articles-to-be-saved 1))
2337                        (format "these %d articles"
2338                                gnus-number-of-articles-to-be-saved)
2339                      "this article"))
2340                   gnus-last-shell-command))))
2341   (when (string-equal command "")
2342     (setq command gnus-last-shell-command))
2343   (gnus-eval-in-buffer-window gnus-article-buffer
2344     (save-restriction
2345       (widen)
2346       (shell-command-on-region (point-min) (point-max) command nil)))
2347   (setq gnus-last-shell-command command))
2348
2349 ;;; Article file names when saving.
2350
2351 (defun gnus-capitalize-newsgroup (newsgroup)
2352   "Capitalize NEWSGROUP name."
2353   (when (not (zerop (length newsgroup)))
2354     (concat (char-to-string (upcase (aref newsgroup 0)))
2355             (substring newsgroup 1))))
2356
2357 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2358   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2359 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2360 Otherwise, it is like ~/News/news/group/num."
2361   (let ((default
2362           (expand-file-name
2363            (concat (if (gnus-use-long-file-name 'not-save)
2364                        (gnus-capitalize-newsgroup newsgroup)
2365                      (gnus-newsgroup-directory-form newsgroup))
2366                    "/" (int-to-string (mail-header-number headers)))
2367            gnus-article-save-directory)))
2368     (if (and last-file
2369              (string-equal (file-name-directory default)
2370                            (file-name-directory last-file))
2371              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2372         default
2373       (or last-file default))))
2374
2375 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2376   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2377 If variable `gnus-use-long-file-name' is non-nil, it is
2378 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2379   (let ((default
2380           (expand-file-name
2381            (concat (if (gnus-use-long-file-name 'not-save)
2382                        newsgroup
2383                      (gnus-newsgroup-directory-form newsgroup))
2384                    "/" (int-to-string (mail-header-number headers)))
2385            gnus-article-save-directory)))
2386     (if (and last-file
2387              (string-equal (file-name-directory default)
2388                            (file-name-directory last-file))
2389              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2390         default
2391       (or last-file default))))
2392
2393 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2394   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2395 If variable `gnus-use-long-file-name' is non-nil, it is
2396 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2397   (or last-file
2398       (expand-file-name
2399        (if (gnus-use-long-file-name 'not-save)
2400            (gnus-capitalize-newsgroup newsgroup)
2401          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2402        gnus-article-save-directory)))
2403
2404 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2405   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2406 If variable `gnus-use-long-file-name' is non-nil, it is
2407 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2408   (or last-file
2409       (expand-file-name
2410        (if (gnus-use-long-file-name 'not-save)
2411            newsgroup
2412          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2413        gnus-article-save-directory)))
2414
2415 (eval-and-compile
2416   (mapcar
2417    (lambda (func)
2418      (let (afunc gfunc)
2419        (if (consp func)
2420            (setq afunc (car func)
2421                  gfunc (cdr func))
2422          (setq afunc func
2423                gfunc (intern (format "gnus-%s" func))))
2424        (fset gfunc
2425              (if (not (fboundp afunc))
2426                  nil
2427                `(lambda (&optional interactive &rest args)
2428                   ,(documentation afunc t)
2429                   (interactive (list t))
2430                   (save-excursion
2431                     (set-buffer gnus-article-buffer)
2432                     (if interactive
2433                         (call-interactively ',afunc)
2434                       (apply ',afunc args))))))))
2435    '(article-hide-headers
2436      article-hide-boring-headers
2437      article-treat-overstrike
2438      article-fill-long-lines
2439      article-capitalize-sentences
2440      article-remove-cr
2441      article-display-x-face
2442      article-de-quoted-unreadable
2443      article-decode-HZ
2444      article-mime-decode-quoted-printable
2445      article-hide-list-identifiers
2446      article-hide-pgp
2447      article-strip-banner
2448      article-babel
2449      article-hide-pem
2450      article-hide-signature
2451      article-strip-headers-in-body
2452      article-remove-trailing-blank-lines
2453      article-strip-leading-blank-lines
2454      article-strip-multiple-blank-lines
2455      article-strip-leading-space
2456      article-strip-trailing-space
2457      article-strip-blank-lines
2458      article-strip-all-blank-lines
2459      article-date-local
2460      article-date-iso8601
2461      article-date-original
2462      article-date-ut
2463      article-decode-mime-words
2464      article-decode-charset
2465      article-decode-encoded-words
2466      article-date-user
2467      article-date-lapsed
2468      article-emphasize
2469      article-treat-dumbquotes
2470      article-normalize-headers
2471      (article-show-all . gnus-article-show-all-headers))))
2472 \f
2473 ;;;
2474 ;;; Gnus article mode
2475 ;;;
2476
2477 (put 'gnus-article-mode 'mode-class 'special)
2478
2479 (set-keymap-parent gnus-article-mode-map widget-keymap)
2480
2481 (gnus-define-keys gnus-article-mode-map
2482   " " gnus-article-goto-next-page
2483   "\177" gnus-article-goto-prev-page
2484   [delete] gnus-article-goto-prev-page
2485   [backspace] gnus-article-goto-prev-page
2486   "\C-c^" gnus-article-refer-article
2487   "h" gnus-article-show-summary
2488   "s" gnus-article-show-summary
2489   "\C-c\C-m" gnus-article-mail
2490   "?" gnus-article-describe-briefly
2491   "e" gnus-summary-edit-article
2492   "<" beginning-of-buffer
2493   ">" end-of-buffer
2494   "\C-c\C-i" gnus-info-find-node
2495   "\C-c\C-b" gnus-bug
2496
2497   "\C-d" gnus-article-read-summary-keys
2498   "\M-*" gnus-article-read-summary-keys
2499   "\M-#" gnus-article-read-summary-keys
2500   "\M-^" gnus-article-read-summary-keys
2501   "\M-g" gnus-article-read-summary-keys)
2502
2503 (substitute-key-definition
2504  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2505
2506 (defun gnus-article-make-menu-bar ()
2507   (gnus-turn-off-edit-menu 'article)
2508   (unless (boundp 'gnus-article-article-menu)
2509     (easy-menu-define
2510      gnus-article-article-menu gnus-article-mode-map ""
2511      '("Article"
2512        ["Scroll forwards" gnus-article-goto-next-page t]
2513        ["Scroll backwards" gnus-article-goto-prev-page t]
2514        ["Show summary" gnus-article-show-summary t]
2515        ["Fetch Message-ID at point" gnus-article-refer-article t]
2516        ["Mail to address at point" gnus-article-mail t]
2517        ["Send a bug report" gnus-bug t]))
2518
2519     (easy-menu-define
2520      gnus-article-treatment-menu gnus-article-mode-map ""
2521      '("Treatment"
2522        ["Hide headers" gnus-article-hide-headers t]
2523        ["Hide signature" gnus-article-hide-signature t]
2524        ["Hide citation" gnus-article-hide-citation t]
2525        ["Treat overstrike" gnus-article-treat-overstrike t]
2526        ["Remove carriage return" gnus-article-remove-cr t]
2527        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
2528        ["Decode HZ" gnus-article-decode-HZ t]))
2529
2530     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2531
2532     (when (boundp 'gnus-summary-post-menu)
2533       (define-key gnus-article-mode-map [menu-bar post]
2534         (cons "Post" gnus-summary-post-menu)))
2535
2536     (gnus-run-hooks 'gnus-article-menu-hook)))
2537
2538 (defun gnus-article-mode ()
2539   "Major mode for displaying an article.
2540
2541 All normal editing commands are switched off.
2542
2543 The following commands are available in addition to all summary mode
2544 commands:
2545 \\<gnus-article-mode-map>
2546 \\[gnus-article-next-page]\t Scroll the article one page forwards
2547 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2548 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2549 \\[gnus-article-show-summary]\t Display the summary buffer
2550 \\[gnus-article-mail]\t Send a reply to the address near point
2551 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2552 \\[gnus-info-find-node]\t Go to the Gnus info node"
2553   (interactive)
2554   (when (gnus-visual-p 'article-menu 'menu)
2555     (gnus-article-make-menu-bar))
2556   (gnus-simplify-mode-line)
2557   (setq mode-name "Article")
2558   (setq major-mode 'gnus-article-mode)
2559   (make-local-variable 'minor-mode-alist)
2560   (use-local-map gnus-article-mode-map)
2561   (gnus-update-format-specifications nil 'article-mode)
2562   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2563   (make-local-variable 'gnus-page-broken)
2564   (make-local-variable 'gnus-button-marker-list)
2565   (make-local-variable 'gnus-article-current-summary)
2566   (make-local-variable 'gnus-article-mime-handles)
2567   (make-local-variable 'gnus-article-decoded-p)
2568   (make-local-variable 'gnus-article-mime-handle-alist)
2569   (make-local-variable 'gnus-article-wash-types)
2570   (gnus-set-default-directory)
2571   (buffer-disable-undo)
2572   (setq buffer-read-only t)
2573   (set-syntax-table gnus-article-mode-syntax-table)
2574   (mm-enable-multibyte)
2575   (gnus-run-hooks 'gnus-article-mode-hook))
2576
2577 (defun gnus-article-setup-buffer ()
2578   "Initialize the article buffer."
2579   (let* ((name (if gnus-single-article-buffer "*Article*"
2580                  (concat "*Article " gnus-newsgroup-name "*")))
2581          (original
2582           (progn (string-match "\\*Article" name)
2583                  (concat " *Original Article"
2584                          (substring name (match-end 0))))))
2585     (setq gnus-article-buffer name)
2586     (setq gnus-original-article-buffer original)
2587     (setq gnus-article-mime-handle-alist nil)
2588     ;; This might be a variable local to the summary buffer.
2589     (unless gnus-single-article-buffer
2590       (save-excursion
2591         (set-buffer gnus-summary-buffer)
2592         (setq gnus-article-buffer name)
2593         (setq gnus-original-article-buffer original)
2594         (gnus-set-global-variables)))
2595     (gnus-article-setup-highlight-words)
2596     ;; Init original article buffer.
2597     (save-excursion
2598       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2599       (mm-enable-multibyte)
2600       (setq major-mode 'gnus-original-article-mode)
2601       (make-local-variable 'gnus-original-article))
2602     (if (get-buffer name)
2603         (save-excursion
2604           (set-buffer name)
2605           (when gnus-article-mime-handles
2606             (mm-destroy-parts gnus-article-mime-handles))
2607           (buffer-disable-undo)
2608           (setq buffer-read-only t)
2609           (unless (eq major-mode 'gnus-article-mode)
2610             (gnus-article-mode))
2611           (current-buffer))
2612       (save-excursion
2613         (set-buffer (gnus-get-buffer-create name))
2614         (gnus-article-mode)
2615         (make-local-variable 'gnus-summary-buffer)
2616         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2617         (current-buffer)))))
2618
2619 ;; Set article window start at LINE, where LINE is the number of lines
2620 ;; from the head of the article.
2621 (defun gnus-article-set-window-start (&optional line)
2622   (set-window-start
2623    (get-buffer-window gnus-article-buffer t)
2624    (save-excursion
2625      (set-buffer gnus-article-buffer)
2626      (goto-char (point-min))
2627      (if (not line)
2628          (point-min)
2629        (gnus-message 6 "Moved to bookmark")
2630        (search-forward "\n\n" nil t)
2631        (forward-line line)
2632        (point)))))
2633
2634 (defun gnus-article-prepare (article &optional all-headers header)
2635   "Prepare ARTICLE in article mode buffer.
2636 ARTICLE should either be an article number or a Message-ID.
2637 If ARTICLE is an id, HEADER should be the article headers.
2638 If ALL-HEADERS is non-nil, no headers are hidden."
2639   (save-excursion
2640     ;; Make sure we start in a summary buffer.
2641     (unless (eq major-mode 'gnus-summary-mode)
2642       (set-buffer gnus-summary-buffer))
2643     (setq gnus-summary-buffer (current-buffer))
2644     (let* ((gnus-article (if header (mail-header-number header) article))
2645            (summary-buffer (current-buffer))
2646            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2647            (group gnus-newsgroup-name)
2648            result)
2649       (save-excursion
2650         (gnus-article-setup-buffer)
2651         (set-buffer gnus-article-buffer)
2652         ;; Deactivate active regions.
2653         (when (and (boundp 'transient-mark-mode)
2654                    transient-mark-mode)
2655           (setq mark-active nil))
2656         (if (not (setq result (let ((buffer-read-only nil))
2657                                 (gnus-request-article-this-buffer
2658                                  article group))))
2659             ;; There is no such article.
2660             (save-excursion
2661               (when (and (numberp article)
2662                          (not (memq article gnus-newsgroup-sparse)))
2663                 (setq gnus-article-current
2664                       (cons gnus-newsgroup-name article))
2665                 (set-buffer gnus-summary-buffer)
2666                 (setq gnus-current-article article)
2667                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2668                     (progn
2669                       (gnus-summary-set-agent-mark article)
2670                       (message "Message marked for downloading"))
2671                   (gnus-summary-mark-article article gnus-canceled-mark)
2672                   (unless (memq article gnus-newsgroup-sparse)
2673                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2674           (if (or (eq result 'pseudo)
2675                   (eq result 'nneething))
2676               (progn
2677                 (save-excursion
2678                   (set-buffer summary-buffer)
2679                   (push article gnus-newsgroup-history)
2680                   (setq gnus-last-article gnus-current-article
2681                         gnus-current-article 0
2682                         gnus-current-headers nil
2683                         gnus-article-current nil)
2684                   (if (eq result 'nneething)
2685                       (gnus-configure-windows 'summary)
2686                     (gnus-configure-windows 'article))
2687                   (gnus-set-global-variables))
2688                 (let ((gnus-article-mime-handle-alist-1
2689                        gnus-article-mime-handle-alist))
2690                   (gnus-set-mode-line 'article)))
2691             ;; The result from the `request' was an actual article -
2692             ;; or at least some text that is now displayed in the
2693             ;; article buffer.
2694             (when (and (numberp article)
2695                        (not (eq article gnus-current-article)))
2696               ;; Seems like a new article has been selected.
2697               ;; `gnus-current-article' must be an article number.
2698               (save-excursion
2699                 (set-buffer summary-buffer)
2700                 (push article gnus-newsgroup-history)
2701                 (setq gnus-last-article gnus-current-article
2702                       gnus-current-article article
2703                       gnus-current-headers
2704                       (gnus-summary-article-header gnus-current-article)
2705                       gnus-article-current
2706                       (cons gnus-newsgroup-name gnus-current-article))
2707                 (unless (vectorp gnus-current-headers)
2708                   (setq gnus-current-headers nil))
2709                 (gnus-summary-goto-subject gnus-current-article)
2710                 (when (gnus-summary-show-thread)
2711                   ;; If the summary buffer really was folded, the
2712                   ;; previous goto may not actually have gone to
2713                   ;; the right article, but the thread root instead.
2714                   ;; So we go again.
2715                   (gnus-summary-goto-subject gnus-current-article))
2716                 (gnus-run-hooks 'gnus-mark-article-hook)
2717                 (gnus-set-mode-line 'summary)
2718                 (when (gnus-visual-p 'article-highlight 'highlight)
2719                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2720                 ;; Set the global newsgroup variables here.
2721                 (gnus-set-global-variables)
2722                 (setq gnus-have-all-headers
2723                       (or all-headers gnus-show-all-headers))))
2724             (when (or (numberp article)
2725                       (stringp article))
2726               (gnus-article-prepare-display)
2727               ;; Do page break.
2728               (goto-char (point-min))
2729               (setq gnus-page-broken
2730                     (when gnus-break-pages
2731                       (gnus-narrow-to-page)
2732                       t)))
2733             (let ((gnus-article-mime-handle-alist-1
2734                    gnus-article-mime-handle-alist))
2735               (gnus-set-mode-line 'article))
2736             (article-goto-body)
2737             (set-window-point (get-buffer-window (current-buffer)) (point))
2738             (gnus-configure-windows 'article)
2739             t))))))
2740
2741 ;;;###autoload
2742 (defun gnus-article-prepare-display ()
2743   "Make the current buffer look like a nice article."
2744   ;; Hooks for getting information from the article.
2745   ;; This hook must be called before being narrowed.
2746   (let ((gnus-article-buffer (current-buffer))
2747         buffer-read-only)
2748     (unless (eq major-mode 'gnus-article-mode)
2749       (gnus-article-mode))
2750     (setq buffer-read-only nil
2751           gnus-article-wash-types nil)
2752     (gnus-run-hooks 'gnus-tmp-internal-hook)
2753     (when gnus-display-mime-function
2754       (funcall gnus-display-mime-function))
2755     (gnus-run-hooks 'gnus-article-prepare-hook)))
2756
2757 ;;;
2758 ;;; Gnus MIME viewing functions
2759 ;;;
2760
2761 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2762   "The following specs can be used:
2763 %t  The MIME type
2764 %T  MIME type, along with additional info
2765 %n  The `name' parameter
2766 %d  The description, if any
2767 %l  The length of the encoded part
2768 %p  The part identifier number
2769 %e  Dots if the part isn't displayed")
2770
2771 (defvar gnus-mime-button-line-format-alist
2772   '((?t gnus-tmp-type ?s)
2773     (?T gnus-tmp-type-long ?s)
2774     (?n gnus-tmp-name ?s)
2775     (?d gnus-tmp-description ?s)
2776     (?p gnus-tmp-id ?s)
2777     (?l gnus-tmp-length ?d)
2778     (?e gnus-tmp-dots ?s)))
2779
2780 (defvar gnus-mime-button-commands
2781   '((gnus-article-press-button "\r" "Toggle Display")
2782     (gnus-mime-view-part "v" "View Interactively...")
2783     (gnus-mime-view-part-as-type "t" "View As Type...")
2784     (gnus-mime-save-part "o" "Save...")
2785     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2786     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2787     (gnus-mime-internalize-part "E" "View Internally")
2788     (gnus-mime-externalize-part "e" "View Externally")
2789     (gnus-mime-pipe-part "|" "Pipe To Command...")))
2790
2791 (defun gnus-article-mime-part-status ()
2792   (if gnus-article-mime-handle-alist-1
2793       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2794     ""))
2795
2796 (defvar gnus-mime-button-map nil)
2797 (unless gnus-mime-button-map
2798   (setq gnus-mime-button-map (make-sparse-keymap))
2799   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2800   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2801   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
2802   (mapcar (lambda (c)
2803             (define-key gnus-mime-button-map (cadr c) (car c)))
2804           gnus-mime-button-commands))
2805
2806 (defun gnus-mime-button-menu (event)
2807   "Construct a context-sensitive menu of MIME commands."
2808   (interactive "e")
2809   (save-excursion
2810     (let ((pos (event-start event)))
2811       (set-buffer (window-buffer (posn-window pos)))
2812       (goto-char (posn-point pos))
2813       (gnus-article-check-buffer)
2814       (let ((response (x-popup-menu
2815                        t `("MIME Part"
2816                            ("" ,@(mapcar (lambda (c)
2817                                            (cons (caddr c) (car c)))
2818                                          gnus-mime-button-commands))))))
2819         (if response
2820             (funcall response))))))
2821
2822 (defun gnus-mime-view-all-parts (&optional handles)
2823   "View all the MIME parts."
2824   (interactive)
2825   (save-current-buffer
2826     (set-buffer gnus-article-buffer)
2827     (let ((handles (or handles gnus-article-mime-handles))
2828           (mail-parse-charset gnus-newsgroup-charset)
2829           (mail-parse-ignored-charsets 
2830            (save-excursion (set-buffer gnus-summary-buffer)
2831                            gnus-newsgroup-ignored-charsets)))
2832       (if (stringp (car handles))
2833           (gnus-mime-view-all-parts (cdr handles))
2834         (mapcar 'mm-display-part handles)))))
2835
2836 (defun gnus-mime-save-part ()
2837   "Save the MIME part under point."
2838   (interactive)
2839   (gnus-article-check-buffer)
2840   (let ((data (get-text-property (point) 'gnus-data)))
2841     (mm-save-part data)))
2842
2843 (defun gnus-mime-pipe-part ()
2844   "Pipe the MIME part under point to a process."
2845   (interactive)
2846   (gnus-article-check-buffer)
2847   (let ((data (get-text-property (point) 'gnus-data)))
2848     (mm-pipe-part data)))
2849
2850 (defun gnus-mime-view-part ()
2851   "Interactively choose a viewing method for the MIME part under point."
2852   (interactive)
2853   (gnus-article-check-buffer)
2854   (let ((data (get-text-property (point) 'gnus-data)))
2855     (mm-interactively-view-part data)))
2856
2857 (defun gnus-mime-view-part-as-type-internal ()
2858   (gnus-article-check-buffer)
2859   (let* ((name (mail-content-type-get
2860                 (mm-handle-type (get-text-property (point) 'gnus-data))
2861                 'name))
2862          (def-type (and name (mm-default-file-encoding name))))
2863     (and def-type (cons def-type 0))))
2864
2865 (defun gnus-mime-view-part-as-type (mime-type)
2866   "Choose a MIME media type, and view the part as such."
2867   (interactive
2868    (list (completing-read
2869           "View as MIME type: "
2870           (mapcar (lambda (i) (list i i)) (mailcap-mime-types))
2871           nil nil
2872           (gnus-mime-view-part-as-type-internal))))
2873   (gnus-article-check-buffer)
2874   (let ((handle (get-text-property (point) 'gnus-data)))
2875     (gnus-mm-display-part
2876      (mm-make-handle (mm-handle-buffer handle)
2877                      (cons mime-type (cdr (mm-handle-type handle)))
2878                      (mm-handle-encoding handle)
2879                      (mm-handle-undisplayer handle)
2880                      (mm-handle-disposition handle)
2881                      (mm-handle-description handle)
2882                      (mm-handle-cache handle)
2883                      (mm-handle-id handle)))))
2884
2885 (defun gnus-mime-copy-part (&optional handle)
2886   "Put the the MIME part under point into a new buffer."
2887   (interactive)
2888   (gnus-article-check-buffer)
2889   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2890          (contents (mm-get-part handle))|
2891          (base (file-name-nondirectory
2892                 (or
2893                  (mail-content-type-get (mm-handle-type handle) 'name)
2894                  (mail-content-type-get (mm-handle-type handle)
2895                                         'filename)
2896                  "*decoded*")))
2897          (buffer (generate-new-buffer base)))
2898     (switch-to-buffer buffer)
2899     (insert contents)
2900     ;; We do it this way to make `normal-mode' set the appropriate mode.
2901     (unwind-protect
2902         (progn
2903           (setq buffer-file-name (expand-file-name base))
2904           (normal-mode))
2905       (setq buffer-file-name nil))
2906     (goto-char (point-min))))
2907
2908 (defun gnus-mime-inline-part (&optional handle)
2909   "Insert the MIME part under point into the current buffer."
2910   (interactive)
2911   (gnus-article-check-buffer)
2912   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2913          contents
2914          (b (point))
2915          buffer-read-only)
2916     (if (mm-handle-undisplayer handle)
2917         (mm-remove-part handle)
2918       (setq contents (mm-get-part handle))
2919       (forward-line 2)
2920       (mm-insert-inline handle contents)
2921       (goto-char b))))
2922
2923 (defun gnus-mime-externalize-part (&optional handle)
2924   "View the MIME part under point with an external viewer."
2925   (interactive)
2926   (gnus-article-check-buffer)
2927   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2928          (mm-user-display-methods nil)
2929          (mm-inline-large-images nil)
2930          (mail-parse-charset gnus-newsgroup-charset)
2931          (mail-parse-ignored-charsets 
2932           (save-excursion (set-buffer gnus-summary-buffer)
2933                           gnus-newsgroup-ignored-charsets)))
2934     (if (mm-handle-undisplayer handle)
2935         (mm-remove-part handle)
2936       (mm-display-part handle))))
2937
2938 (defun gnus-mime-internalize-part (&optional handle)
2939   "View the MIME part under point with an internal viewer.
2940 In no internal viewer is available, use an external viewer."
2941   (interactive)
2942   (gnus-article-check-buffer)
2943   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2944          (mm-inlined-types '(".*"))
2945          (mm-inline-large-images t)
2946          (mail-parse-charset gnus-newsgroup-charset)
2947          (mail-parse-ignored-charsets 
2948           (save-excursion (set-buffer gnus-summary-buffer)
2949                           gnus-newsgroup-ignored-charsets)))
2950     (if (mm-handle-undisplayer handle)
2951         (mm-remove-part handle)
2952       (mm-display-part handle))))
2953
2954 (defun gnus-article-part-wrapper (n function)
2955   (save-current-buffer
2956     (set-buffer gnus-article-buffer)
2957     (when (> n (length gnus-article-mime-handle-alist))
2958       (error "No such part"))
2959     (gnus-article-goto-part n)
2960     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2961       (funcall function handle))))
2962
2963 (defun gnus-article-pipe-part (n)
2964   "Pipe MIME part N, which is the numerical prefix."
2965   (interactive "p")
2966   (gnus-article-part-wrapper n 'mm-pipe-part))
2967
2968 (defun gnus-article-save-part (n)
2969   "Save MIME part N, which is the numerical prefix."
2970   (interactive "p")
2971   (gnus-article-part-wrapper n 'mm-save-part))
2972
2973 (defun gnus-article-interactively-view-part (n)
2974   "View MIME part N interactively, which is the numerical prefix."
2975   (interactive "p")
2976   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2977
2978 (defun gnus-article-copy-part (n)
2979   "Copy MIME part N, which is the numerical prefix."
2980   (interactive "p")
2981   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2982
2983 (defun gnus-article-externalize-part (n)
2984   "View MIME part N externally, which is the numerical prefix."
2985   (interactive "p")
2986   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2987
2988 (defun gnus-article-inline-part (n)
2989   "Inline MIME part N, which is the numerical prefix."
2990   (interactive "p")
2991   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
2992
2993 (defun gnus-article-mime-match-handle-first (condition)
2994   (if condition
2995       (let ((alist gnus-article-mime-handle-alist) ihandle n)
2996         (while (setq ihandle (pop alist))
2997           (if (and (cond 
2998                     ((functionp condition)
2999                      (funcall condition (cdr ihandle)))
3000                     ((eq condition 'undisplayed) 
3001                      (not (or (mm-handle-undisplayer (cdr ihandle))
3002                               (equal (mm-handle-media-type (cdr ihandle))
3003                                      "multipart/alternative"))))
3004                     ((eq condition 'undisplayed-alternative)
3005                      (not (mm-handle-undisplayer (cdr ihandle))))
3006                     (t t))
3007                    (gnus-article-goto-part (car ihandle))
3008                    (or (not n) (< (car ihandle) n)))
3009               (setq n (car ihandle))))
3010         (or n 1))
3011     1))
3012
3013 (defun gnus-article-view-part (&optional n)
3014   "View MIME part N, which is the numerical prefix."
3015   (interactive "P")
3016   (save-current-buffer
3017     (set-buffer gnus-article-buffer)
3018     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3019                              gnus-article-mime-match-handle-function)))
3020     (when (> n (length gnus-article-mime-handle-alist))
3021       (error "No such part"))
3022     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3023       (when (gnus-article-goto-part n)
3024         (if (equal (car handle) "multipart/alternative")
3025             (gnus-article-press-button)
3026           (when (eq (gnus-mm-display-part handle) 'internal)
3027             (gnus-set-window-start)))))))
3028
3029 (defun gnus-mm-display-part (handle)
3030   "Display HANDLE and fix MIME button."
3031   (let ((id (get-text-property (point) 'gnus-part))
3032         (point (point))
3033         buffer-read-only)
3034     (forward-line 1)
3035     (prog1
3036         (let ((window (selected-window))
3037               (mail-parse-charset gnus-newsgroup-charset)
3038               (mail-parse-ignored-charsets 
3039                (save-excursion (set-buffer gnus-summary-buffer)
3040                                gnus-newsgroup-ignored-charsets)))
3041           (save-excursion
3042             (unwind-protect
3043                 (let ((win (get-buffer-window (current-buffer) t))
3044                       (beg (point)))
3045                   (when win
3046                     (select-window win))
3047                   (goto-char point)
3048                   (forward-line)
3049                   (if (mm-handle-displayed-p handle)
3050                       ;; This will remove the part.
3051                       (mm-display-part handle)
3052                     (save-restriction
3053                       (narrow-to-region (point) (1+ (point)))
3054                       (mm-display-part handle)
3055                       ;; We narrow to the part itself and
3056                       ;; then call the treatment functions.
3057                       (goto-char (point-min))
3058                       (forward-line 1)
3059                       (narrow-to-region (point) (point-max))
3060                       (gnus-treat-article
3061                        nil id
3062                        (1- (length gnus-article-mime-handles))
3063                        (mm-handle-media-type handle)))))
3064               (select-window window))))
3065       (goto-char point)
3066       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3067       (gnus-insert-mime-button
3068        handle id (list (mm-handle-displayed-p handle)))
3069       (goto-char point))))
3070
3071 (defun gnus-article-goto-part (n)
3072   "Go to MIME part N."
3073   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3074     (when point
3075       (goto-char point))))
3076
3077 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3078   (let ((gnus-tmp-name
3079          (or (mail-content-type-get (mm-handle-type handle)
3080                                     'name)
3081              (mail-content-type-get (mm-handle-disposition handle)
3082                                     'filename)
3083              ""))
3084         (gnus-tmp-type (mm-handle-media-type handle))
3085         (gnus-tmp-description
3086          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3087                                               "")))
3088         (gnus-tmp-dots
3089          (if (if displayed (car displayed)
3090                (mm-handle-displayed-p handle))
3091              "" "..."))
3092         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3093                            (buffer-size)))
3094         gnus-tmp-type-long b e)
3095     (when (string-match ".*/" gnus-tmp-name)
3096       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3097     (setq gnus-tmp-type-long (concat gnus-tmp-type
3098                                      (and (not (equal gnus-tmp-name ""))
3099                                           (concat "; " gnus-tmp-name))))
3100     (or (equal gnus-tmp-description "")
3101         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3102     (unless (bolp)
3103       (insert "\n"))
3104     (setq b (point))
3105     (gnus-eval-format
3106      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3107      `(local-map ,gnus-mime-button-map
3108                  keymap ,gnus-mime-button-map
3109                  gnus-callback gnus-mm-display-part
3110                  gnus-part ,gnus-tmp-id
3111                  article-type annotation
3112                  gnus-data ,handle))
3113     (setq e (point))
3114     (widget-convert-button 'link b e
3115                            :mime-handle handle
3116                            :action 'gnus-widget-press-button
3117                            :button-keymap gnus-mime-button-map
3118                            :help-echo
3119                            (lambda (widget)
3120                              ;; Needed to properly clear the message
3121                              ;; due to a bug in wid-edit
3122                              (setq help-echo-owns-message t)
3123                              (format
3124                               "Click to %s the MIME part; %s for more options"
3125                               (if (mm-handle-displayed-p
3126                                    (widget-get widget :mime-handle))
3127                                   "hide" "show")
3128                               (if gnus-xemacs "button3" "mouse-3"))))))
3129
3130 (defun gnus-widget-press-button (elems el)
3131   (goto-char (widget-get elems :from))
3132   (gnus-article-press-button))
3133
3134 (defvar gnus-displaying-mime nil)
3135
3136 (defun gnus-display-mime (&optional ihandles)
3137   "Display the MIME parts."
3138   (save-excursion
3139     (save-selected-window
3140       (let ((window (get-buffer-window gnus-article-buffer))
3141             (point (point)))
3142         (when window
3143           (select-window window)
3144           ;; We have to do this since selecting the window
3145           ;; may change the point.  So we set the window point.
3146           (set-window-point window point)))
3147       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3148              buffer-read-only handle name type b e display)
3149         (when (and (not ihandles)
3150                    (not gnus-displaying-mime))
3151           ;; Top-level call; we clean up.
3152           (when gnus-article-mime-handles
3153             (mm-destroy-parts gnus-article-mime-handles)
3154             (setq gnus-article-mime-handle-alist nil));; A trick.
3155           (setq gnus-article-mime-handles handles)
3156           ;; We allow users to glean info from the handles.
3157           (when gnus-article-mime-part-function
3158             (gnus-mime-part-function handles)))
3159         (if (and handles
3160                  (or (not (stringp (car handles)))
3161                      (cdr handles)))
3162             (progn
3163               (when (and (not ihandles)
3164                          (not gnus-displaying-mime))
3165                 ;; Clean up for mime parts.
3166                 (article-goto-body)
3167                 (delete-region (point) (point-max)))
3168               (let ((gnus-displaying-mime t))
3169                 (gnus-mime-display-part handles)))
3170           (save-restriction
3171             (article-goto-body)
3172             (narrow-to-region (point) (point-max))
3173             (gnus-treat-article nil 1 1)
3174             (widen)))
3175         (unless ihandles
3176           ;; Highlight the headers.
3177           (save-excursion
3178             (save-restriction
3179               (article-goto-body)
3180               (narrow-to-region (point-min) (point))
3181               (gnus-treat-article 'head))))))))
3182
3183 (defvar gnus-mime-display-multipart-as-mixed nil)
3184
3185 (defun gnus-mime-display-part (handle)
3186   (cond
3187    ;; Single part.
3188    ((not (stringp (car handle)))
3189     (gnus-mime-display-single handle))
3190    ;; User-defined multipart
3191    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3192     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3193              handle))
3194    ;; multipart/alternative
3195    ((and (equal (car handle) "multipart/alternative")
3196          (not gnus-mime-display-multipart-as-mixed))
3197     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3198       (push (cons id handle) gnus-article-mime-handle-alist)
3199       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3200    ;; multipart/related
3201    ((and (equal (car handle) "multipart/related")
3202          (not gnus-mime-display-multipart-as-mixed))
3203     ;;;!!!We should find the start part, but we just default
3204     ;;;!!!to the first part.
3205     (gnus-mime-display-part (cadr handle)))
3206    ;; Other multiparts are handled like multipart/mixed.
3207    (t
3208     (gnus-mime-display-mixed (cdr handle)))))
3209
3210 (defun gnus-mime-part-function (handles)
3211   (if (stringp (car handles))
3212       (mapcar 'gnus-mime-part-function (cdr handles))
3213     (funcall gnus-article-mime-part-function handles)))
3214
3215 (defun gnus-mime-display-mixed (handles)
3216   (mapcar 'gnus-mime-display-part handles))
3217
3218 (defun gnus-mime-display-single (handle)
3219   (let ((type (mm-handle-media-type handle))
3220         (ignored gnus-ignored-mime-types)
3221         (not-attachment t)
3222         (move nil)
3223         display text)
3224     (catch 'ignored
3225       (progn
3226         (while ignored
3227           (when (string-match (pop ignored) type)
3228             (throw 'ignored nil)))
3229         (if (and (setq not-attachment
3230                        (and (not (mm-inline-override-p handle))
3231                             (or (not (mm-handle-disposition handle))
3232                                 (equal (car (mm-handle-disposition handle))
3233                                        "inline")
3234                                 (mm-attachment-override-p handle))))
3235                  (mm-automatic-display-p handle)
3236                  (or (mm-inlined-p handle)
3237                      (mm-automatic-external-display-p type)))
3238             (setq display t)
3239           (when (equal (mm-handle-media-supertype handle) "text")
3240             (setq text t)))
3241         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3242           (push (cons id handle) gnus-article-mime-handle-alist)
3243           (when (or (not display)
3244                     (not (gnus-unbuttonized-mime-type-p type)))
3245             ;(gnus-article-insert-newline)
3246             (gnus-insert-mime-button
3247              handle id (list (or display (and not-attachment text))))
3248             (gnus-article-insert-newline)
3249             ;(gnus-article-insert-newline)
3250             (setq move t)))
3251         (let ((beg (point)))
3252           (cond
3253            (display
3254             (when move
3255               (forward-line -2)
3256               (setq beg (point)))
3257             (let ((mail-parse-charset gnus-newsgroup-charset)
3258                   (mail-parse-ignored-charsets 
3259                    (save-excursion (condition-case ()
3260                                        (set-buffer gnus-summary-buffer)
3261                                      (error))
3262                                    gnus-newsgroup-ignored-charsets)))
3263               (mm-display-part handle t))
3264             (goto-char (point-max)))
3265            ((and text not-attachment)
3266             (when move
3267               (forward-line -2)
3268               (setq beg (point)))
3269             (gnus-article-insert-newline)
3270             (mm-insert-inline handle (mm-get-part handle))
3271             (goto-char (point-max))))
3272           ;; Do highlighting.
3273           (save-excursion
3274             (save-restriction
3275               (narrow-to-region beg (point))
3276               (gnus-treat-article
3277                nil (length gnus-article-mime-handle-alist)
3278                (1- (length gnus-article-mime-handles))
3279                (mm-handle-media-type handle)))))))))
3280
3281 (defun gnus-unbuttonized-mime-type-p (type)
3282   "Say whether TYPE is to be unbuttonized."
3283   (unless gnus-inhibit-mime-unbuttonizing
3284     (catch 'found
3285       (let ((types gnus-unbuttonized-mime-types))
3286         (while types
3287           (when (string-match (pop types) type)
3288             (throw 'found t)))))))
3289
3290 (defun gnus-article-insert-newline ()
3291   "Insert a newline, but mark it as undeletable."
3292   (gnus-put-text-property
3293    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3294
3295 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3296   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3297          (ihandles handles)
3298          (point (point))
3299          handle buffer-read-only from props begend not-pref)
3300     (save-window-excursion
3301       (save-restriction
3302         (when ibegend
3303           (narrow-to-region (car ibegend)
3304                             (or (cdr ibegend)
3305                                 (progn
3306                                   (goto-char (car ibegend))
3307                                   (forward-line 2)
3308                                   (point))))
3309           (delete-region (point-min) (point-max))
3310           (mm-remove-parts handles))
3311         (setq begend (list (point-marker)))
3312         ;; Do the toggle.
3313         (unless (setq not-pref (cadr (member preferred ihandles)))
3314           (setq not-pref (car ihandles)))
3315         (when (or ibegend
3316                   (not (gnus-unbuttonized-mime-type-p
3317                         "multipart/alternative")))
3318           (gnus-add-text-properties
3319            (setq from (point))
3320            (progn
3321              (insert (format "%d.  " id))
3322              (point))
3323            `(gnus-callback
3324              (lambda (handles)
3325                (unless ,(not ibegend)
3326                  (setq gnus-article-mime-handle-alist
3327                        ',gnus-article-mime-handle-alist))
3328                (gnus-mime-display-alternative
3329                 ',ihandles ',not-pref ',begend ,id))
3330              local-map ,gnus-mime-button-map
3331              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3332              face ,gnus-article-button-face
3333              keymap ,gnus-mime-button-map
3334              gnus-part ,id
3335              gnus-data ,handle))
3336           (widget-convert-button 'link from (point)
3337                                  :action 'gnus-widget-press-button
3338                                  :button-keymap gnus-widget-button-keymap)
3339           ;; Do the handles
3340           (while (setq handle (pop handles))
3341             (gnus-add-text-properties
3342              (setq from (point))
3343              (progn
3344                (insert (format "(%c) %-18s"
3345                                (if (equal handle preferred) ?* ? )
3346                                (mm-handle-media-type handle)))
3347                (point))
3348              `(gnus-callback
3349                (lambda (handles)
3350                  (unless ,(not ibegend)
3351                    (setq gnus-article-mime-handle-alist
3352                          ',gnus-article-mime-handle-alist))
3353                  (gnus-mime-display-alternative
3354                   ',ihandles ',handle ',begend ,id))
3355                local-map ,gnus-mime-button-map
3356                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3357                face ,gnus-article-button-face
3358                keymap ,gnus-mime-button-map
3359                gnus-part ,id
3360                gnus-data ,handle))
3361             (widget-convert-button 'link from (point)
3362                                    :action 'gnus-widget-press-button
3363                                    :button-keymap gnus-widget-button-keymap)
3364             (insert "  "))
3365           (insert "\n\n"))
3366         (when preferred
3367           (if (stringp (car preferred))
3368               (gnus-display-mime preferred)
3369             (let ((mail-parse-charset gnus-newsgroup-charset)
3370                   (mail-parse-ignored-charsets 
3371                    (save-excursion (set-buffer gnus-summary-buffer)
3372                                    gnus-newsgroup-ignored-charsets)))
3373               (mm-display-part preferred)
3374               ;; Do highlighting.
3375               (save-excursion
3376                 (save-restriction
3377                   (narrow-to-region (car begend) (point-max))
3378                   (gnus-treat-article
3379                    nil (length gnus-article-mime-handle-alist)
3380                    (1- (length gnus-article-mime-handles))
3381                    (mm-handle-media-type handle))))))
3382           (goto-char (point-max))
3383           (setcdr begend (point-marker)))))
3384     (when ibegend
3385       (goto-char point))))
3386
3387 (defun gnus-article-wash-status ()
3388   "Return a string which display status of article washing."
3389   (save-excursion
3390     (set-buffer gnus-article-buffer)
3391     (let ((cite (memq 'cite gnus-article-wash-types))
3392           (headers (memq 'headers gnus-article-wash-types))
3393           (boring (memq 'boring-headers gnus-article-wash-types))
3394           (pgp (memq 'pgp gnus-article-wash-types))
3395           (pem (memq 'pem gnus-article-wash-types))
3396           (signature (memq 'signature gnus-article-wash-types))
3397           (overstrike (memq 'overstrike gnus-article-wash-types))
3398           (emphasis (memq 'emphasis gnus-article-wash-types)))
3399       (format "%c%c%c%c%c%c"
3400               (if cite ?c ? )
3401               (if (or headers boring) ?h ? )
3402               (if (or pgp pem) ?p ? )
3403               (if signature ?s ? )
3404               (if overstrike ?o ? )
3405               (if emphasis ?e ? )))))
3406
3407 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3408
3409 (defun gnus-article-maybe-hide-headers ()
3410   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3411 Provided for backwards compatibility."
3412   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3413                  (not (save-excursion (set-buffer gnus-summary-buffer)
3414                                       gnus-have-all-headers)))
3415              (not gnus-inhibit-hiding))
3416     (gnus-article-hide-headers)))
3417
3418 ;;; Article savers.
3419
3420 (defun gnus-output-to-file (file-name)
3421   "Append the current article to a file named FILE-NAME."
3422   (let ((artbuf (current-buffer)))
3423     (with-temp-buffer
3424       (insert-buffer-substring artbuf)
3425       ;; Append newline at end of the buffer as separator, and then
3426       ;; save it to file.
3427       (goto-char (point-max))
3428       (insert "\n")
3429       (mm-append-to-file (point-min) (point-max) file-name)
3430       t)))
3431
3432 (defun gnus-narrow-to-page (&optional arg)
3433   "Narrow the article buffer to a page.
3434 If given a numerical ARG, move forward ARG pages."
3435   (interactive "P")
3436   (setq arg (if arg (prefix-numeric-value arg) 0))
3437   (save-excursion
3438     (set-buffer gnus-article-buffer)
3439     (goto-char (point-min))
3440     (widen)
3441     ;; Remove any old next/prev buttons.
3442     (when (gnus-visual-p 'page-marker)
3443       (let ((buffer-read-only nil))
3444         (gnus-remove-text-with-property 'gnus-prev)
3445         (gnus-remove-text-with-property 'gnus-next)))
3446     (when
3447         (cond ((< arg 0)
3448                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3449               ((> arg 0)
3450                (re-search-forward page-delimiter nil 'move arg)))
3451       (goto-char (match-end 0)))
3452     (narrow-to-region
3453      (point)
3454      (if (re-search-forward page-delimiter nil 'move)
3455          (match-beginning 0)
3456        (point)))
3457     (when (and (gnus-visual-p 'page-marker)
3458                (not (= (point-min) 1)))
3459       (save-excursion
3460         (goto-char (point-min))
3461         (gnus-insert-prev-page-button)))
3462     (when (and (gnus-visual-p 'page-marker)
3463                (< (+ (point-max) 2) (buffer-size)))
3464       (save-excursion
3465         (goto-char (point-max))
3466         (gnus-insert-next-page-button)))))
3467
3468 ;; Article mode commands
3469
3470 (defun gnus-article-goto-next-page ()
3471   "Show the next page of the article."
3472   (interactive)
3473   (when (gnus-article-next-page)
3474     (goto-char (point-min))
3475     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3476
3477 (defun gnus-article-goto-prev-page ()
3478   "Show the next page of the article."
3479   (interactive)
3480   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3481     (gnus-article-prev-page nil)))
3482
3483 (defun gnus-article-next-page (&optional lines)
3484   "Show the next page of the current article.
3485 If end of article, return non-nil.  Otherwise return nil.
3486 Argument LINES specifies lines to be scrolled up."
3487   (interactive "p")
3488   (move-to-window-line -1)
3489   (if (save-excursion
3490         (end-of-line)
3491         (and (pos-visible-in-window-p)  ;Not continuation line.
3492              (eobp)))
3493       ;; Nothing in this page.
3494       (if (or (not gnus-page-broken)
3495               (save-excursion
3496                 (save-restriction
3497                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3498           t                             ;Nothing more.
3499         (gnus-narrow-to-page 1)         ;Go to next page.
3500         nil)
3501     ;; More in this page.
3502     (let ((scroll-in-place nil))
3503       (condition-case ()
3504           (scroll-up lines)
3505         (end-of-buffer
3506          ;; Long lines may cause an end-of-buffer error.
3507          (goto-char (point-max)))))
3508     (move-to-window-line 0)
3509     nil))
3510
3511 (defun gnus-article-prev-page (&optional lines)
3512   "Show previous page of current article.
3513 Argument LINES specifies lines to be scrolled down."
3514   (interactive "p")
3515   (move-to-window-line 0)
3516   (if (and gnus-page-broken
3517            (bobp)
3518            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3519       (progn
3520         (gnus-narrow-to-page -1)        ;Go to previous page.
3521         (goto-char (point-max))
3522         (recenter -1))
3523     (let ((scroll-in-place nil))
3524       (prog1
3525           (condition-case ()
3526               (scroll-down lines)
3527             (beginning-of-buffer
3528              (goto-char (point-min))))
3529         (move-to-window-line 0)))))
3530
3531 (defun gnus-article-refer-article ()
3532   "Read article specified by message-id around point."
3533   (interactive)
3534   (let ((point (point)))
3535     (search-forward ">" nil t)          ;Move point to end of "<....>".
3536     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3537         (let ((message-id (match-string 1)))
3538           (goto-char point)
3539           (set-buffer gnus-summary-buffer)
3540           (gnus-summary-refer-article message-id))
3541       (goto-char (point))
3542       (error "No references around point"))))
3543
3544 (defun gnus-article-show-summary ()
3545   "Reconfigure windows to show summary buffer."
3546   (interactive)
3547   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3548       (error "There is no summary buffer for this article buffer")
3549     (gnus-article-set-globals)
3550     (gnus-configure-windows 'article)
3551     (gnus-summary-goto-subject gnus-current-article)
3552     (gnus-summary-position-point)))
3553
3554 (defun gnus-article-describe-briefly ()
3555   "Describe article mode commands briefly."
3556   (interactive)
3557   (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")))
3558
3559 (defun gnus-article-summary-command ()
3560   "Execute the last keystroke in the summary buffer."
3561   (interactive)
3562   (let ((obuf (current-buffer))
3563         (owin (current-window-configuration))
3564         func)
3565     (switch-to-buffer gnus-article-current-summary 'norecord)
3566     (setq func (lookup-key (current-local-map) (this-command-keys)))
3567     (call-interactively func)
3568     (set-buffer obuf)
3569     (set-window-configuration owin)
3570     (set-window-point (get-buffer-window (current-buffer)) (point))))
3571
3572 (defun gnus-article-summary-command-nosave ()
3573   "Execute the last keystroke in the summary buffer."
3574   (interactive)
3575   (let (func)
3576     (pop-to-buffer gnus-article-current-summary 'norecord)
3577     (setq func (lookup-key (current-local-map) (this-command-keys)))
3578     (call-interactively func)))
3579
3580 (defun gnus-article-check-buffer ()
3581   "Beep if not in an article buffer."
3582   (unless (equal major-mode 'gnus-article-mode)
3583     (error "Command invoked outside of a Gnus article buffer")))
3584
3585 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3586   "Read a summary buffer key sequence and execute it from the article buffer."
3587   (interactive "P")
3588   (gnus-article-check-buffer)
3589   (let ((nosaves
3590          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3591            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3592            "=" "^" "\M-^" "|"))
3593         (nosave-but-article
3594          '("A\r"))
3595         (nosave-in-article
3596          '("\C-d"))
3597         (up-to-top
3598          '("n" "Gn" "p" "Gp"))
3599         keys new-sum-point)
3600     (save-excursion
3601       (set-buffer gnus-article-current-summary)
3602       (let (gnus-pick-mode)
3603         (push (or key last-command-event) unread-command-events)
3604         (setq keys (if gnus-xemacs
3605                        (events-to-keys (read-key-sequence nil))
3606                      (read-key-sequence nil)))))
3607                      
3608     (message "")
3609
3610     (if (or (member keys nosaves)
3611             (member keys nosave-but-article)
3612             (member keys nosave-in-article))
3613         (let (func)
3614           (save-window-excursion
3615             (pop-to-buffer gnus-article-current-summary 'norecord)
3616             ;; We disable the pick minor mode commands.
3617             (let (gnus-pick-mode)
3618               (setq func (lookup-key (current-local-map) keys))))
3619           (if (not func)
3620               (ding)
3621             (unless (member keys nosave-in-article)
3622               (set-buffer gnus-article-current-summary))
3623             (call-interactively func)
3624             (setq new-sum-point (point)))
3625           (when (member keys nosave-but-article)
3626             (pop-to-buffer gnus-article-buffer 'norecord)))
3627       ;; These commands should restore window configuration.
3628       (let ((obuf (current-buffer))
3629             (owin (current-window-configuration))
3630             (opoint (point))
3631             (summary gnus-article-current-summary)
3632             func in-buffer selected)
3633         (if not-restore-window
3634             (pop-to-buffer summary 'norecord)
3635           (switch-to-buffer summary 'norecord))
3636         (setq in-buffer (current-buffer))
3637         ;; We disable the pick minor mode commands.
3638         (if (setq func (let (gnus-pick-mode)
3639                          (lookup-key (current-local-map) keys)))
3640             (progn
3641               (call-interactively func)
3642               (setq new-sum-point (point)))
3643           (ding))
3644         (when (eq in-buffer (current-buffer))
3645           (setq selected (gnus-summary-select-article))
3646           (set-buffer obuf)
3647           (unless not-restore-window
3648             (set-window-configuration owin))
3649           (when (eq selected 'old)
3650             (article-goto-body)
3651             (set-window-start (get-buffer-window (current-buffer))
3652                               1)
3653             (set-window-point (get-buffer-window (current-buffer))
3654                               (point)))
3655           (let ((win (get-buffer-window gnus-article-current-summary)))
3656             (when win
3657               (set-window-point win new-sum-point))))))))
3658
3659 (defun gnus-article-hide (&optional arg force)
3660   "Hide all the gruft in the current article.
3661 This means that PGP stuff, signatures, cited text and (some)
3662 headers will be hidden.
3663 If given a prefix, show the hidden text instead."
3664   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3665   (gnus-article-hide-headers arg)
3666   (gnus-article-hide-list-identifiers arg)
3667   (gnus-article-hide-pgp arg)
3668   (gnus-article-hide-citation-maybe arg force)
3669   (gnus-article-hide-signature arg))
3670
3671 (defun gnus-article-maybe-highlight ()
3672   "Do some article highlighting if article highlighting is requested."
3673   (when (gnus-visual-p 'article-highlight 'highlight)
3674     (gnus-article-highlight-some)))
3675
3676 (defun gnus-check-group-server ()
3677   ;; Make sure the connection to the server is alive.
3678   (unless (gnus-server-opened
3679            (gnus-find-method-for-group gnus-newsgroup-name))
3680     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3681     (gnus-request-group gnus-newsgroup-name t)))
3682
3683 (defun gnus-request-article-this-buffer (article group)
3684   "Get an article and insert it into this buffer."
3685   (let (do-update-line sparse-header)
3686     (prog1
3687         (save-excursion
3688           (erase-buffer)
3689           (gnus-kill-all-overlays)
3690           (setq group (or group gnus-newsgroup-name))
3691
3692           ;; Using `gnus-request-article' directly will insert the article into
3693           ;; `nntp-server-buffer' - so we'll save some time by not having to
3694           ;; copy it from the server buffer into the article buffer.
3695
3696           ;; We only request an article by message-id when we do not have the
3697           ;; headers for it, so we'll have to get those.
3698           (when (stringp article)
3699             (gnus-read-header article))
3700
3701           ;; If the article number is negative, that means that this article
3702           ;; doesn't belong in this newsgroup (possibly), so we find its
3703           ;; message-id and request it by id instead of number.
3704           (when (and (numberp article)
3705                      gnus-summary-buffer
3706                      (get-buffer gnus-summary-buffer)
3707                      (gnus-buffer-exists-p gnus-summary-buffer))
3708             (save-excursion
3709               (set-buffer gnus-summary-buffer)
3710               (let ((header (gnus-summary-article-header article)))
3711                 (when (< article 0)
3712                   (cond
3713                    ((memq article gnus-newsgroup-sparse)
3714                     ;; This is a sparse gap article.
3715                     (setq do-update-line article)
3716                     (setq article (mail-header-id header))
3717                     (setq sparse-header (gnus-read-header article))
3718                     (setq gnus-newsgroup-sparse
3719                           (delq article gnus-newsgroup-sparse)))
3720                    ((vectorp header)
3721                     ;; It's a real article.
3722                     (setq article (mail-header-id header)))
3723                    (t
3724                     ;; It is an extracted pseudo-article.
3725                     (setq article 'pseudo)
3726                     (gnus-request-pseudo-article header))))
3727
3728                 (let ((method (gnus-find-method-for-group
3729                                gnus-newsgroup-name)))
3730                   (when (and (eq (car method) 'nneething)
3731                              (vectorp header))
3732                     (let ((dir (concat
3733                                 (file-name-as-directory
3734                                  (or (cadr (assq 'nneething-address method))
3735                                      (nth 1 method)))
3736                                 (mail-header-subject header))))
3737                       (when (file-directory-p dir)
3738                         (setq article 'nneething)
3739                         (gnus-group-enter-directory dir))))))))
3740
3741           (cond
3742            ;; Refuse to select canceled articles.
3743            ((and (numberp article)
3744                  gnus-summary-buffer
3745                  (get-buffer gnus-summary-buffer)
3746                  (gnus-buffer-exists-p gnus-summary-buffer)
3747                  (eq (cdr (save-excursion
3748                             (set-buffer gnus-summary-buffer)
3749                             (assq article gnus-newsgroup-reads)))
3750                      gnus-canceled-mark))
3751             nil)
3752            ;; We first check `gnus-original-article-buffer'.
3753            ((and (get-buffer gnus-original-article-buffer)
3754                  (numberp article)
3755                  (save-excursion
3756                    (set-buffer gnus-original-article-buffer)
3757                    (and (equal (car gnus-original-article) group)
3758                         (eq (cdr gnus-original-article) article))))
3759             (insert-buffer-substring gnus-original-article-buffer)
3760             'article)
3761            ;; Check the backlog.
3762            ((and gnus-keep-backlog
3763                  (gnus-backlog-request-article group article (current-buffer)))
3764             'article)
3765            ;; Check asynchronous pre-fetch.
3766            ((gnus-async-request-fetched-article group article (current-buffer))
3767             (gnus-async-prefetch-next group article gnus-summary-buffer)
3768             (when (and (numberp article) gnus-keep-backlog)
3769               (gnus-backlog-enter-article group article (current-buffer)))
3770             'article)
3771            ;; Check the cache.
3772            ((and gnus-use-cache
3773                  (numberp article)
3774                  (gnus-cache-request-article article group))
3775             'article)
3776            ;; Get the article and put into the article buffer.
3777            ((or (stringp article)
3778                 (numberp article))
3779             (let ((gnus-override-method gnus-override-method)
3780                   (methods (and (stringp article) 
3781                                 gnus-refer-article-method))
3782                   result
3783                   (buffer-read-only nil))
3784               (setq methods
3785                     (if (listp methods)
3786                         methods
3787                       (list methods)))
3788               (when (and (null gnus-override-method)
3789                          methods)
3790                 (setq gnus-override-method (pop methods)))
3791               (while (not result)
3792                 (when (eq gnus-override-method 'current)
3793                   (setq gnus-override-method gnus-current-select-method))
3794                 (erase-buffer)
3795                 (gnus-kill-all-overlays)
3796                 (let ((gnus-newsgroup-name group))
3797                   (gnus-check-group-server))
3798                 (when (gnus-request-article article group (current-buffer))
3799                   (when (numberp article)
3800                     (gnus-async-prefetch-next group article 
3801                                               gnus-summary-buffer)
3802                     (when gnus-keep-backlog
3803                       (gnus-backlog-enter-article
3804                        group article (current-buffer))))
3805                   (setq result 'article))
3806                 (if (not result)
3807                     (if methods
3808                         (setq gnus-override-method (pop methods))
3809                       (setq result 'done))))
3810               (and (eq result 'article) 'article)))
3811            ;; It was a pseudo.
3812            (t article)))
3813
3814       ;; Associate this article with the current summary buffer.
3815       (setq gnus-article-current-summary gnus-summary-buffer)
3816
3817       ;; Take the article from the original article buffer
3818       ;; and place it in the buffer it's supposed to be in.
3819       (when (and (get-buffer gnus-article-buffer)
3820                  (equal (buffer-name (current-buffer))
3821                         (buffer-name (get-buffer gnus-article-buffer))))
3822         (save-excursion
3823           (if (get-buffer gnus-original-article-buffer)
3824               (set-buffer gnus-original-article-buffer)
3825             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3826             (buffer-disable-undo)
3827             (setq major-mode 'gnus-original-article-mode)
3828             (setq buffer-read-only t))
3829           (let (buffer-read-only)
3830             (erase-buffer)
3831             (insert-buffer-substring gnus-article-buffer))
3832           (setq gnus-original-article (cons group article)))
3833
3834         ;; Decode charsets.
3835         (run-hooks 'gnus-article-decode-hook)
3836         ;; Mark article as decoded or not.
3837         (setq gnus-article-decoded-p gnus-article-decode-hook))
3838
3839       ;; Update sparse articles.
3840       (when (and do-update-line
3841                  (or (numberp article)
3842                      (stringp article)))
3843         (let ((buf (current-buffer)))
3844           (set-buffer gnus-summary-buffer)
3845           (gnus-summary-update-article do-update-line sparse-header)
3846           (gnus-summary-goto-subject do-update-line nil t)
3847           (set-window-point (get-buffer-window (current-buffer) t)
3848                             (point))
3849           (set-buffer buf))))))
3850
3851 ;;;
3852 ;;; Article editing
3853 ;;;
3854
3855 (defcustom gnus-article-edit-mode-hook nil
3856   "Hook run in article edit mode buffers."
3857   :group 'gnus-article-various
3858   :type 'hook)
3859
3860 (defvar gnus-article-edit-done-function nil)
3861
3862 (defvar gnus-article-edit-mode-map nil)
3863
3864 ;; Should we be using derived.el for this?
3865 (unless gnus-article-edit-mode-map
3866   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3867   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3868
3869   (gnus-define-keys gnus-article-edit-mode-map
3870     "\C-c\C-c" gnus-article-edit-done
3871     "\C-c\C-k" gnus-article-edit-exit)
3872
3873   (gnus-define-keys (gnus-article-edit-wash-map
3874                      "\C-c\C-w" gnus-article-edit-mode-map)
3875     "f" gnus-article-edit-full-stops))
3876
3877 (defun gnus-article-edit-mode ()
3878   "Major mode for editing articles.
3879 This is an extended text-mode.
3880
3881 \\{gnus-article-edit-mode-map}"
3882   (interactive)
3883   (setq major-mode 'gnus-article-edit-mode)
3884   (setq mode-name "Article Edit")
3885   (use-local-map gnus-article-edit-mode-map)
3886   (make-local-variable 'gnus-article-edit-done-function)
3887   (make-local-variable 'gnus-prev-winconf)
3888   (setq buffer-read-only nil)
3889   (buffer-enable-undo)
3890   (widen)
3891   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3892
3893 (defun gnus-article-edit (&optional force)
3894   "Edit the current article.
3895 This will have permanent effect only in mail groups.
3896 If FORCE is non-nil, allow editing of articles even in read-only
3897 groups."
3898   (interactive "P")
3899   (when (and (not force)
3900              (gnus-group-read-only-p))
3901     (error "The current newsgroup does not support article editing"))
3902   (gnus-article-date-original)
3903   (gnus-article-edit-article
3904    'ignore
3905    `(lambda (no-highlight)
3906       'ignore
3907       (gnus-summary-edit-article-done
3908        ,(or (mail-header-references gnus-current-headers) "")
3909        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3910
3911 (defun gnus-article-edit-article (start-func exit-func)
3912   "Start editing the contents of the current article buffer."
3913   (let ((winconf (current-window-configuration)))
3914     (set-buffer gnus-article-buffer)
3915     (gnus-article-edit-mode)
3916     (funcall start-func)
3917     (gnus-configure-windows 'edit-article)
3918     (setq gnus-article-edit-done-function exit-func)
3919     (setq gnus-prev-winconf winconf)
3920     (gnus-message 6 "C-c C-c to end edits")))
3921
3922 (defun gnus-article-edit-done (&optional arg)
3923   "Update the article edits and exit."
3924   (interactive "P")
3925   (save-excursion
3926     (save-restriction
3927       (widen)
3928       (when (article-goto-body)
3929         (let ((lines (count-lines (point) (point-max)))
3930               (length (- (point-max) (point)))
3931               (case-fold-search t)
3932               (body (copy-marker (point))))
3933           (goto-char (point-min))
3934           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3935             (delete-region (match-beginning 1) (match-end 1))
3936             (insert (number-to-string length)))
3937           (goto-char (point-min))
3938           (when (re-search-forward
3939                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3940             (delete-region (match-beginning 1) (match-end 1))
3941             (insert (number-to-string length)))
3942           (goto-char (point-min))
3943           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3944             (delete-region (match-beginning 1) (match-end 1))
3945             (insert (number-to-string lines)))))))
3946   (let ((func gnus-article-edit-done-function)
3947         (buf (current-buffer))
3948         (start (window-start)))
3949     (gnus-article-edit-exit)
3950     (save-excursion
3951       (set-buffer buf)
3952       (let ((buffer-read-only nil))
3953         (funcall func arg))
3954       ;; The cache and backlog have to be flushed somewhat.
3955       (when gnus-keep-backlog
3956         (gnus-backlog-remove-article
3957          (car gnus-article-current) (cdr gnus-article-current)))
3958       ;; Flush original article as well.
3959       (save-excursion
3960         (when (get-buffer gnus-original-article-buffer)
3961           (set-buffer gnus-original-article-buffer)
3962           (setq gnus-original-article nil)))
3963       (when gnus-use-cache
3964         (gnus-cache-update-article
3965          (car gnus-article-current) (cdr gnus-article-current))))
3966     (set-buffer buf)
3967     (set-window-start (get-buffer-window buf) start)
3968     (set-window-point (get-buffer-window buf) (point))))
3969
3970 (defun gnus-article-edit-exit ()
3971   "Exit the article editing without updating."
3972   (interactive)
3973   ;; We remove all text props from the article buffer.
3974   (let ((buf (format "%s" (buffer-string)))
3975         (curbuf (current-buffer))
3976         (p (point))
3977         (window-start (window-start)))
3978     (erase-buffer)
3979     (insert buf)
3980     (let ((winconf gnus-prev-winconf))
3981       (gnus-article-mode)
3982       (set-window-configuration winconf)
3983       ;; Tippy-toe some to make sure that point remains where it was.
3984       (save-current-buffer
3985         (set-buffer curbuf)
3986         (set-window-start (get-buffer-window (current-buffer)) window-start)
3987         (goto-char p)))))
3988
3989 (defun gnus-article-edit-full-stops ()
3990   "Interactively repair spacing at end of sentences."
3991   (interactive)
3992   (save-excursion
3993     (goto-char (point-min))
3994     (search-forward-regexp "^$" nil t)
3995     (let ((case-fold-search nil))
3996       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3997
3998 ;;;
3999 ;;; Article highlights
4000 ;;;
4001
4002 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4003
4004 ;;; Internal Variables:
4005
4006 (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\\)"
4007   "Regular expression that matches URLs."
4008   :group 'gnus-article-buttons
4009   :type 'regexp)
4010
4011 (defcustom gnus-button-alist
4012   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4013      0 t gnus-button-message-id 2)
4014     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4015     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4016      1 t
4017      gnus-button-fetch-group 4)
4018     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4019     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4020      t gnus-button-message-id 3)
4021     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4022     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4023     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4024     ;; This is how URLs _should_ be embedded in text...
4025     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4026     ;; Raw URLs.
4027     (,gnus-button-url-regexp 0 t browse-url 0))
4028   "*Alist of regexps matching buttons in article bodies.
4029
4030 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4031 REGEXP: is the string matching text around the button,
4032 BUTTON: is the number of the regexp grouping actually matching the button,
4033 FORM: is a lisp expression which must eval to true for the button to
4034 be added,
4035 CALLBACK: is the function to call when the user push this button, and each
4036 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4037
4038 CALLBACK can also be a variable, in that case the value of that
4039 variable it the real callback function."
4040   :group 'gnus-article-buttons
4041   :type '(repeat (list regexp
4042                        (integer :tag "Button")
4043                        (sexp :tag "Form")
4044                        (function :tag "Callback")
4045                        (repeat :tag "Par"
4046                                :inline t
4047                                (integer :tag "Regexp group")))))
4048
4049 (defcustom gnus-header-button-alist
4050   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4051      0 t gnus-button-message-id 0)
4052     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4053     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4054      0 t gnus-button-mailto 0)
4055     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4056     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4057     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4058     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4059      gnus-button-message-id 3))
4060   "*Alist of headers and regexps to match buttons in article heads.
4061
4062 This alist is very similar to `gnus-button-alist', except that each
4063 alist has an additional HEADER element first in each entry:
4064
4065 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4066
4067 HEADER is a regexp to match a header.  For a fuller explanation, see
4068 `gnus-button-alist'."
4069   :group 'gnus-article-buttons
4070   :group 'gnus-article-headers
4071   :type '(repeat (list (regexp :tag "Header")
4072                        regexp
4073                        (integer :tag "Button")
4074                        (sexp :tag "Form")
4075                        (function :tag "Callback")
4076                        (repeat :tag "Par"
4077                                :inline t
4078                                (integer :tag "Regexp group")))))
4079
4080 (defvar gnus-button-regexp nil)
4081 (defvar gnus-button-marker-list nil)
4082 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4083
4084 (defvar gnus-button-last nil)
4085 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4086
4087 ;;; Commands:
4088
4089 (defun gnus-article-push-button (event)
4090   "Check text under the mouse pointer for a callback function.
4091 If the text under the mouse pointer has a `gnus-callback' property,
4092 call it with the value of the `gnus-data' text property."
4093   (interactive "e")
4094   (set-buffer (window-buffer (posn-window (event-start event))))
4095   (let* ((pos (posn-point (event-start event)))
4096          (data (get-text-property pos 'gnus-data))
4097          (fun (get-text-property pos 'gnus-callback)))
4098     (goto-char pos)
4099     (when fun
4100       (funcall fun data))))
4101
4102 (defun gnus-article-press-button ()
4103   "Check text at point for a callback function.
4104 If the text at point has a `gnus-callback' property,
4105 call it with the value of the `gnus-data' text property."
4106   (interactive)
4107   (let* ((data (get-text-property (point) 'gnus-data))
4108          (fun (get-text-property (point) 'gnus-callback)))
4109     (when fun
4110       (funcall fun data))))
4111
4112 (defun gnus-article-highlight (&optional force)
4113   "Highlight current article.
4114 This function calls `gnus-article-highlight-headers',
4115 `gnus-article-highlight-citation',
4116 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4117 do the highlighting.  See the documentation for those functions."
4118   (interactive (list 'force))
4119   (gnus-article-highlight-headers)
4120   (gnus-article-highlight-citation force)
4121   (gnus-article-highlight-signature)
4122   (gnus-article-add-buttons force)
4123   (gnus-article-add-buttons-to-head))
4124
4125 (defun gnus-article-highlight-some (&optional force)
4126   "Highlight current article.
4127 This function calls `gnus-article-highlight-headers',
4128 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4129 do the highlighting.  See the documentation for those functions."
4130   (interactive (list 'force))
4131   (gnus-article-highlight-headers)
4132   (gnus-article-highlight-signature)
4133   (gnus-article-add-buttons))
4134
4135 (defun gnus-article-highlight-headers ()
4136   "Highlight article headers as specified by `gnus-header-face-alist'."
4137   (interactive)
4138   (save-excursion
4139     (set-buffer gnus-article-buffer)
4140     (save-restriction
4141       (let ((alist gnus-header-face-alist)
4142             (buffer-read-only nil)
4143             (case-fold-search t)
4144             (inhibit-point-motion-hooks t)
4145             entry regexp header-face field-face from hpoints fpoints)
4146         (article-narrow-to-head)
4147         (while (setq entry (pop alist))
4148           (goto-char (point-min))
4149           (setq regexp (concat "^\\("
4150                                (if (string-equal "" (nth 0 entry))
4151                                    "[^\t ]"
4152                                  (nth 0 entry))
4153                                "\\)")
4154                 header-face (nth 1 entry)
4155                 field-face (nth 2 entry))
4156           (while (and (re-search-forward regexp nil t)
4157                       (not (eobp)))
4158             (beginning-of-line)
4159             (setq from (point))
4160             (unless (search-forward ":" nil t)
4161               (forward-char 1))
4162             (when (and header-face
4163                        (not (memq (point) hpoints)))
4164               (push (point) hpoints)
4165               (gnus-put-text-property from (point) 'face header-face))
4166             (when (and field-face
4167                        (not (memq (setq from (point)) fpoints)))
4168               (push from fpoints)
4169               (if (re-search-forward "^[^ \t]" nil t)
4170                   (forward-char -2)
4171                 (goto-char (point-max)))
4172               (gnus-put-text-property from (point) 'face field-face))))))))
4173
4174 (defun gnus-article-highlight-signature ()
4175   "Highlight the signature in an article.
4176 It does this by highlighting everything after
4177 `gnus-signature-separator' using `gnus-signature-face'."
4178   (interactive)
4179   (save-excursion
4180     (set-buffer gnus-article-buffer)
4181     (let ((buffer-read-only nil)
4182           (inhibit-point-motion-hooks t))
4183       (save-restriction
4184         (when (and gnus-signature-face
4185                    (gnus-article-narrow-to-signature))
4186           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4187                             'face gnus-signature-face)
4188           (widen)
4189           (gnus-article-search-signature)
4190           (let ((start (match-beginning 0))
4191                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4192             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4193                                      end)))))))
4194
4195 (defun gnus-button-in-region-p (b e prop)
4196   "Say whether PROP exists in the region."
4197   (text-property-not-all b e prop nil))
4198
4199 (defun gnus-article-add-buttons (&optional force)
4200   "Find external references in the article and make buttons of them.
4201 \"External references\" are things like Message-IDs and URLs, as
4202 specified by `gnus-button-alist'."
4203   (interactive (list 'force))
4204   (save-excursion
4205     (set-buffer gnus-article-buffer)
4206     (let ((buffer-read-only nil)
4207           (inhibit-point-motion-hooks t)
4208           (case-fold-search t)
4209           (alist gnus-button-alist)
4210           beg entry regexp)
4211       ;; Remove all old markers.
4212       (let (marker entry new-list)
4213         (while (setq marker (pop gnus-button-marker-list))
4214           (if (or (< marker (point-min)) (>= marker (point-max)))
4215               (push marker new-list)
4216             (goto-char marker)
4217             (when (setq entry (gnus-button-entry))
4218               (put-text-property (match-beginning (nth 1 entry))
4219                                  (match-end (nth 1 entry))
4220                                  'gnus-callback nil))
4221             (set-marker marker nil)))
4222         (setq gnus-button-marker-list new-list))
4223       ;; We skip the headers.
4224       (article-goto-body)
4225       (setq beg (point))
4226       (while (setq entry (pop alist))
4227         (setq regexp (car entry))
4228         (goto-char beg)
4229         (while (re-search-forward regexp nil t)
4230           (let* ((start (and entry (match-beginning (nth 1 entry))))
4231                  (end (and entry (match-end (nth 1 entry))))
4232                  (from (match-beginning 0)))
4233             (when (and (or (eq t (nth 2 entry))
4234                            (eval (nth 2 entry)))
4235                        (not (gnus-button-in-region-p
4236                              start end 'gnus-callback)))
4237               ;; That optional form returned non-nil, so we add the
4238               ;; button.
4239               (gnus-article-add-button
4240                start end 'gnus-button-push
4241                (car (push (set-marker (make-marker) from)
4242                           gnus-button-marker-list))))))))))
4243
4244 ;; Add buttons to the head of an article.
4245 (defun gnus-article-add-buttons-to-head ()
4246   "Add buttons to the head of the article."
4247   (interactive)
4248   (save-excursion
4249     (set-buffer gnus-article-buffer)
4250     (save-restriction
4251       (let ((buffer-read-only nil)
4252             (inhibit-point-motion-hooks t)
4253             (case-fold-search t)
4254             (alist gnus-header-button-alist)
4255             entry beg end)
4256         (article-narrow-to-head)
4257         (while alist
4258           ;; Each alist entry.
4259           (setq entry (car alist)
4260                 alist (cdr alist))
4261           (goto-char (point-min))
4262           (while (re-search-forward (car entry) nil t)
4263             ;; Each header matching the entry.
4264             (setq beg (match-beginning 0))
4265             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4266                                (match-beginning 0))
4267                           (point-max)))
4268             (goto-char beg)
4269             (while (re-search-forward (nth 1 entry) end t)
4270               ;; Each match within a header.
4271               (let* ((entry (cdr entry))
4272                      (start (match-beginning (nth 1 entry)))
4273                      (end (match-end (nth 1 entry)))
4274                      (form (nth 2 entry)))
4275                 (goto-char (match-end 0))
4276                 (when (eval form)
4277                   (gnus-article-add-button
4278                    start end (nth 3 entry)
4279                    (buffer-substring (match-beginning (nth 4 entry))
4280                                      (match-end (nth 4 entry)))))))
4281             (goto-char end)))))))
4282
4283 ;;; External functions:
4284
4285 (defun gnus-article-add-button (from to fun &optional data)
4286   "Create a button between FROM and TO with callback FUN and data DATA."
4287   (when gnus-article-button-face
4288     (gnus-overlay-put (gnus-make-overlay from to)
4289                       'face gnus-article-button-face))
4290   (gnus-add-text-properties
4291    from to
4292    (nconc (and gnus-article-mouse-face
4293                (list gnus-mouse-face-prop gnus-article-mouse-face))
4294           (list 'gnus-callback fun)
4295           (and data (list 'gnus-data data))))
4296   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4297                          :button-keymap gnus-widget-button-keymap))
4298
4299 ;;; Internal functions:
4300
4301 (defun gnus-article-set-globals ()
4302   (save-excursion
4303     (set-buffer gnus-summary-buffer)
4304     (gnus-set-global-variables)))
4305
4306 (defun gnus-signature-toggle (end)
4307   (save-excursion
4308     (set-buffer gnus-article-buffer)
4309     (let ((buffer-read-only nil)
4310           (inhibit-point-motion-hooks t))
4311       (if (get-text-property end 'invisible)
4312           (gnus-article-unhide-text end (point-max))
4313         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4314
4315 (defun gnus-button-entry ()
4316   ;; Return the first entry in `gnus-button-alist' matching this place.
4317   (let ((alist gnus-button-alist)
4318         (entry nil))
4319     (while alist
4320       (setq entry (pop alist))
4321       (if (looking-at (car entry))
4322           (setq alist nil)
4323         (setq entry nil)))
4324     entry))
4325
4326 (defun gnus-button-push (marker)
4327   ;; Push button starting at MARKER.
4328   (save-excursion
4329     (goto-char marker)
4330     (let* ((entry (gnus-button-entry))
4331            (inhibit-point-motion-hooks t)
4332            (fun (nth 3 entry))
4333            (args (mapcar (lambda (group)
4334                            (let ((string (match-string group)))
4335                              (gnus-set-text-properties
4336                               0 (length string) nil string)
4337                              string))
4338                          (nthcdr 4 entry))))
4339       (cond
4340        ((fboundp fun)
4341         (apply fun args))
4342        ((and (boundp fun)
4343              (fboundp (symbol-value fun)))
4344         (apply (symbol-value fun) args))
4345        (t
4346         (gnus-message 1 "You must define `%S' to use this button"
4347                       (cons fun args)))))))
4348
4349 (defun gnus-button-message-id (message-id)
4350   "Fetch MESSAGE-ID."
4351   (save-excursion
4352     (set-buffer gnus-summary-buffer)
4353     (gnus-summary-refer-article message-id)))
4354
4355 (defun gnus-button-fetch-group (address)
4356   "Fetch GROUP specified by ADDRESS."
4357   (if (not (string-match "[:/]" address))
4358       ;; This is just a simple group url.
4359       (gnus-group-read-ephemeral-group address gnus-select-method)
4360     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4361                            address))
4362         (error "Can't parse %s" address)
4363       (gnus-group-read-ephemeral-group
4364        (match-string 4 address)
4365        `(nntp ,(match-string 1 address)
4366               (nntp-address ,(match-string 1 address))
4367               (nntp-port-number ,(if (match-end 3)
4368                                      (match-string 3 address)
4369                                    "nntp")))))))
4370
4371 (defun gnus-url-parse-query-string (query &optional downcase)
4372   (let (retval pairs cur key val)
4373     (setq pairs (split-string query "&"))
4374     (while pairs
4375       (setq cur (car pairs)
4376             pairs (cdr pairs))
4377       (if (not (string-match "=" cur))
4378           nil                           ; Grace
4379         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4380               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4381         (if downcase
4382             (setq key (downcase key)))
4383         (setq cur (assoc key retval))
4384         (if cur
4385             (setcdr cur (cons val (cdr cur)))
4386           (setq retval (cons (list key val) retval)))))
4387     retval))
4388
4389 (defun gnus-url-unhex (x)
4390   (if (> x ?9)
4391       (if (>= x ?a)
4392           (+ 10 (- x ?a))
4393         (+ 10 (- x ?A)))
4394     (- x ?0)))
4395
4396 (defun gnus-url-unhex-string (str &optional allow-newlines)
4397   "Remove %XXX embedded spaces, etc in a url.
4398 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4399 decoding of carriage returns and line feeds in the string, which is normally
4400 forbidden in URL encoding."
4401   (setq str (or str ""))
4402   (let ((tmp "")
4403         (case-fold-search t))
4404     (while (string-match "%[0-9a-f][0-9a-f]" str)
4405       (let* ((start (match-beginning 0))
4406              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4407              (code (+ (* 16 ch1)
4408                       (gnus-url-unhex (elt str (+ start 2))))))
4409         (setq tmp (concat
4410                    tmp (substring str 0 start)
4411                    (cond
4412                     (allow-newlines
4413                      (char-to-string code))
4414                     ((or (= code ?\n) (= code ?\r))
4415                      " ")
4416                     (t (char-to-string code))))
4417               str (substring str (match-end 0)))))
4418     (setq tmp (concat tmp str))
4419     tmp))
4420
4421 (defun gnus-url-mailto (url)
4422   ;; Send mail to someone
4423   (when (string-match "mailto:/*\\(.*\\)" url)
4424     (setq url (substring url (match-beginning 1) nil)))
4425   (let (to args subject func)
4426     (if (string-match (regexp-quote "?") url)
4427         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4428               args (gnus-url-parse-query-string
4429                     (substring url (match-end 0) nil) t))
4430       (setq to (gnus-url-unhex-string url)))
4431     (setq args (cons (list "to" to) args)
4432           subject (cdr-safe (assoc "subject" args)))
4433     (message-mail)
4434     (while args
4435       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4436       (if (fboundp func)
4437           (funcall func)
4438         (message-position-on-field (caar args)))
4439       (insert (mapconcat 'identity (cdar args) ", "))
4440       (setq args (cdr args)))
4441     (if subject
4442         (message-goto-body)
4443       (message-goto-subject))))
4444
4445 (defun gnus-button-mailto (address)
4446   ;; Mail to ADDRESS.
4447   (set-buffer (gnus-copy-article-buffer))
4448   (message-reply address))
4449
4450 (defun gnus-button-reply (address)
4451   ;; Reply to ADDRESS.
4452   (message-reply address))
4453
4454 (defun gnus-button-embedded-url (address)
4455   "Browse ADDRESS."
4456   (browse-url (gnus-strip-whitespace address)))
4457
4458 ;;; Next/prev buttons in the article buffer.
4459
4460 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4461 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4462
4463 (defvar gnus-prev-page-map nil)
4464 (unless gnus-prev-page-map
4465   (setq gnus-prev-page-map (make-sparse-keymap))
4466   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4467   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4468
4469 (defun gnus-insert-prev-page-button ()
4470   (let ((buffer-read-only nil))
4471     (gnus-eval-format
4472      gnus-prev-page-line-format nil
4473      `(gnus-prev t local-map ,gnus-prev-page-map
4474                  gnus-callback gnus-article-button-prev-page
4475                  article-type annotation))))
4476
4477 (defvar gnus-next-page-map nil)
4478 (unless gnus-next-page-map
4479   (setq gnus-next-page-map (make-keymap))
4480   (suppress-keymap gnus-prev-page-map)
4481   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4482   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4483
4484 (defun gnus-button-next-page ()
4485   "Go to the next page."
4486   (interactive)
4487   (let ((win (selected-window)))
4488     (select-window (get-buffer-window gnus-article-buffer t))
4489     (gnus-article-next-page)
4490     (select-window win)))
4491
4492 (defun gnus-button-prev-page ()
4493   "Go to the prev page."
4494   (interactive)
4495   (let ((win (selected-window)))
4496     (select-window (get-buffer-window gnus-article-buffer t))
4497     (gnus-article-prev-page)
4498     (select-window win)))
4499
4500 (defun gnus-insert-next-page-button ()
4501   (let ((buffer-read-only nil))
4502     (gnus-eval-format gnus-next-page-line-format nil
4503                       `(gnus-next
4504                         t local-map ,gnus-next-page-map
4505                         gnus-callback gnus-article-button-next-page
4506                         article-type annotation))))
4507
4508 (defun gnus-article-button-next-page (arg)
4509   "Go to the next page."
4510   (interactive "P")
4511   (let ((win (selected-window)))
4512     (select-window (get-buffer-window gnus-article-buffer t))
4513     (gnus-article-next-page)
4514     (select-window win)))
4515
4516 (defun gnus-article-button-prev-page (arg)
4517   "Go to the prev page."
4518   (interactive "P")
4519   (let ((win (selected-window)))
4520     (select-window (get-buffer-window gnus-article-buffer t))
4521     (gnus-article-prev-page)
4522     (select-window win)))
4523
4524 (defvar gnus-decode-header-methods
4525   '(mail-decode-encoded-word-region)
4526   "List of methods used to decode headers.
4527
4528 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
4529 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
4530 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4531 whose names match REGEXP.
4532
4533 For example:
4534 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4535  mail-decode-encoded-word-region
4536  (\"chinese\" . rfc1843-decode-region))
4537 ")
4538
4539 (defvar gnus-decode-header-methods-cache nil)
4540
4541 (defun gnus-multi-decode-header (start end)
4542   "Apply the functions from `gnus-encoded-word-methods' that match."
4543   (unless (and gnus-decode-header-methods-cache
4544                (eq gnus-newsgroup-name
4545                    (car gnus-decode-header-methods-cache)))
4546     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4547     (mapcar (lambda (x)
4548               (if (symbolp x)
4549                   (nconc gnus-decode-header-methods-cache (list x))
4550                 (if (and gnus-newsgroup-name
4551                          (string-match (car x) gnus-newsgroup-name))
4552                     (nconc gnus-decode-header-methods-cache
4553                            (list (cdr x))))))
4554           gnus-decode-header-methods))
4555   (let ((xlist gnus-decode-header-methods-cache))
4556     (pop xlist)
4557     (save-restriction
4558       (narrow-to-region start end)
4559       (while xlist
4560         (funcall (pop xlist) (point-min) (point-max))))))
4561
4562 ;;;
4563 ;;; Treatment top-level handling.
4564 ;;;
4565
4566 (defun gnus-treat-article (condition &optional part-number total-parts type)
4567   (let ((length (- (point-max) (point-min)))
4568         (alist gnus-treatment-function-alist)
4569         (article-goto-body-goes-to-point-min-p t)
4570         (treated-type
4571          (or (not type)
4572              (catch 'found
4573                (let ((list gnus-article-treat-types))
4574                  (while list
4575                    (when (string-match (pop list) type)
4576                      (throw 'found t)))))))
4577         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4578         val elem)
4579     (gnus-run-hooks 'gnus-part-display-hook)
4580     (while (setq elem (pop alist))
4581       (setq val
4582             (save-excursion
4583               (set-buffer gnus-summary-buffer)
4584               (symbol-value (car elem))))
4585       (when (and (or (consp val)
4586                      treated-type)
4587                  (gnus-treat-predicate val)
4588                  (or (not (get (car elem) 'highlight))
4589                      highlightp))
4590         (save-restriction
4591           (funcall (cadr elem)))))))
4592
4593 ;; Dynamic variables.
4594 (defvar part-number)
4595 (defvar total-parts)
4596 (defvar type)
4597 (defvar condition)
4598 (defvar length)
4599 (defun gnus-treat-predicate (val)
4600   (cond
4601    ((null val)
4602     nil)
4603    ((and (listp val)
4604          (stringp (car val)))
4605     (apply 'gnus-or (mapcar `(lambda (s)
4606                                (string-match s ,(or gnus-newsgroup-name "")))
4607                             val)))
4608    ((listp val)
4609     (let ((pred (pop val)))
4610       (cond
4611        ((eq pred 'or)
4612         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4613        ((eq pred 'and)
4614         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4615        ((eq pred 'not)
4616         (not (gnus-treat-predicate (car val))))
4617        ((eq pred 'typep)
4618         (equal (car val) type))
4619        (t
4620         (error "%S is not a valid predicate" pred)))))
4621    (condition
4622     (eq condition val))
4623    ((eq val t)
4624     t)
4625    ((eq val 'head)
4626     nil)
4627    ((eq val 'last)
4628     (eq part-number total-parts))
4629    ((numberp val)
4630     (< length val))
4631    (t
4632     (error "%S is not a valid value" val))))
4633
4634 (gnus-ems-redefine)
4635
4636 (provide 'gnus-art)
4637
4638 (run-hooks 'gnus-art-load-hook)
4639
4640 ;;; gnus-art.el ends here