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