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