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