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