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