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