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