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