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