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