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