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