*** empty log message ***
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996,97,98 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:\\|^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 gnus-current-headers)
1455                      (message-fetch-field "date")
1456                      ""))
1457          (date (if (vectorp header) (mail-header-date header)
1458                  header))
1459          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1460          (inhibit-point-motion-hooks t)
1461          bface eface newline)
1462     (when (and date (not (string= date "")))
1463       (save-excursion
1464         (save-restriction
1465           (nnheader-narrow-to-headers)
1466           (let ((buffer-read-only nil))
1467             ;; Delete any old Date headers.
1468             (if (re-search-forward date-regexp nil t)
1469                 (progn
1470                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1471                         eface (get-text-property (1- (gnus-point-at-eol))
1472                                                  'face))
1473                   (delete-region (progn (beginning-of-line) (point))
1474                                  (progn (end-of-line) (point)))
1475                   (beginning-of-line))
1476               (goto-char (point-max))
1477               (setq newline t))
1478             (insert (article-make-date-line date type))
1479             ;; Do highlighting.
1480             (beginning-of-line)
1481             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1482               (put-text-property (match-beginning 1) (1+ (match-end 1))
1483                                  'face bface)
1484               (put-text-property (match-beginning 2) (match-end 2)
1485                                  'face eface))
1486             (when newline
1487               (end-of-line)
1488               (insert "\n"))))))))
1489
1490 (defun article-make-date-line (date type)
1491   "Return a DATE line of TYPE."
1492   (let ((time (condition-case ()
1493                   (date-to-time date)
1494                 (error '(0 0)))))
1495     (cond
1496      ;; Convert to the local timezone.  We have to slap a
1497      ;; `condition-case' round the calls to the timezone
1498      ;; functions since they aren't particularly resistant to
1499      ;; buggy dates.
1500      ((eq type 'local)
1501       (let ((tz (car (current-time-zone))))
1502         (format "Date: %s %s%04d" (current-time-string time)
1503                 (if (> tz 0) "+" "-") (abs (/ tz 36)))))
1504      ;; Convert to Universal Time.
1505      ((eq type 'ut)
1506       (concat "Date: "
1507               (current-time-string
1508                (let* ((e (parse-time-string date))
1509                      (tm (apply 'encode-time e))
1510                      (ms (car tm))
1511                      (ls (- (cadr tm) (car (current-time-zone)))))
1512                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1513                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1514                        (t (list ms ls)))))
1515               " UT"))
1516      ;; Get the original date from the article.
1517      ((eq type 'original)
1518       (concat "Date: " (if (string-match "\n+$" date)
1519                            (substring date 0 (match-beginning 0))
1520                          date)))
1521      ;; Let the user define the format.
1522      ((eq type 'user)
1523       (if (gnus-functionp gnus-article-time-format)
1524           (funcall gnus-article-time-format time)
1525         (concat
1526          "Date: "
1527          (format-time-string gnus-article-time-format time))))
1528      ;; ISO 8601.
1529      ((eq type 'iso8601)
1530       (concat
1531        "Date: "
1532        (format-time-string "%Y%M%DT%h%m%s" time)))
1533      ;; Do an X-Sent lapsed format.
1534      ((eq type 'lapsed)
1535       ;; If the date is seriously mangled, the timezone functions are
1536       ;; liable to bug out, so we ignore all errors.
1537       (let* ((now (current-time))
1538              (real-time (subtract-time now time))
1539              (real-sec (and real-time
1540                             (+ (* (float (car real-time)) 65536)
1541                                (cadr real-time))))
1542              (sec (and real-time (abs real-sec)))
1543              num prev)
1544         (cond
1545          ((null real-time)
1546           "X-Sent: Unknown")
1547          ((zerop sec)
1548           "X-Sent: Now")
1549          (t
1550           (concat
1551            "X-Sent: "
1552            ;; This is a bit convoluted, but basically we go
1553            ;; through the time units for years, weeks, etc,
1554            ;; and divide things to see whether that results
1555            ;; in positive answers.
1556            (mapconcat
1557             (lambda (unit)
1558               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1559                   ;; The (remaining) seconds are too few to
1560                   ;; be divided into this time unit.
1561                   ""
1562                 ;; It's big enough, so we output it.
1563                 (setq sec (- sec (* num (cdr unit))))
1564                 (prog1
1565                     (concat (if prev ", " "") (int-to-string
1566                                                (floor num))
1567                             " " (symbol-name (car unit))
1568                             (if (> num 1) "s" ""))
1569                   (setq prev t))))
1570             article-time-units "")
1571            ;; If dates are odd, then it might appear like the
1572            ;; article was sent in the future.
1573            (if (> real-sec 0)
1574                " ago"
1575              " in the future"))))))
1576      (t
1577       (error "Unknown conversion type: %s" type)))))
1578
1579 (defun article-date-local (&optional highlight)
1580   "Convert the current article date to the local timezone."
1581   (interactive (list t))
1582   (article-date-ut 'local highlight))
1583
1584 (defun article-date-original (&optional highlight)
1585   "Convert the current article date to what it was originally.
1586 This is only useful if you have used some other date conversion
1587 function and want to see what the date was before converting."
1588   (interactive (list t))
1589   (article-date-ut 'original highlight))
1590
1591 (defun article-date-lapsed (&optional highlight)
1592   "Convert the current article date to time lapsed since it was sent."
1593   (interactive (list t))
1594   (article-date-ut 'lapsed highlight))
1595
1596 (defun article-update-date-lapsed ()
1597   "Function to be run from a timer to update the lapsed time line."
1598   (let (deactivate-mark)
1599     (save-excursion
1600       (ignore-errors
1601         (walk-windows
1602          (lambda (w)
1603            (set-buffer (window-buffer w))
1604            (when (eq major-mode 'gnus-article-mode)
1605              (goto-char (point-min))
1606              (when (re-search-forward "^X-Sent:" nil t)
1607                (article-date-lapsed t)))))))))
1608
1609 (defun gnus-start-date-timer (&optional n)
1610   "Start a timer to update the X-Sent header in the article buffers.
1611 The numerical prefix says how frequently (in seconds) the function
1612 is to run."
1613   (interactive "p")
1614   (unless n
1615     (setq n 1))
1616   (gnus-stop-date-timer)
1617   (setq article-lapsed-timer
1618         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1619
1620 (defun gnus-stop-date-timer ()
1621   "Stop the X-Sent timer."
1622   (interactive)
1623   (when article-lapsed-timer
1624     (nnheader-cancel-timer article-lapsed-timer)
1625     (setq article-lapsed-timer nil)))
1626
1627 (defun article-date-user (&optional highlight)
1628   "Convert the current article date to the user-defined format.
1629 This format is defined by the `gnus-article-time-format' variable."
1630   (interactive (list t))
1631   (article-date-ut 'user highlight))
1632
1633 (defun article-date-iso8601 (&optional highlight)
1634   "Convert the current article date to ISO8601."
1635   (interactive (list t))
1636   (article-date-ut 'iso8601 highlight))
1637
1638 (defun article-show-all ()
1639   "Show all hidden text in the article buffer."
1640   (interactive)
1641   (save-excursion
1642     (let ((buffer-read-only nil))
1643       (gnus-article-unhide-text (point-min) (point-max)))))
1644
1645 (defun article-emphasize (&optional arg)
1646   "Emphasize text according to `gnus-emphasis-alist'."
1647   (interactive (gnus-article-hidden-arg))
1648   (unless (gnus-article-check-hidden-text 'emphasis arg)
1649     (save-excursion
1650       (let ((alist gnus-emphasis-alist)
1651             (buffer-read-only nil)
1652             (props (append '(article-type emphasis)
1653                            gnus-hidden-properties))
1654             regexp elem beg invisible visible face)
1655         (article-goto-body)
1656         (setq beg (point))
1657         (while (setq elem (pop alist))
1658           (goto-char beg)
1659           (setq regexp (car elem)
1660                 invisible (nth 1 elem)
1661                 visible (nth 2 elem)
1662                 face (nth 3 elem))
1663           (while (re-search-forward regexp nil t)
1664             (when (and (match-beginning visible) (match-beginning invisible))
1665               (gnus-article-hide-text
1666                (match-beginning invisible) (match-end invisible) props)
1667               (gnus-article-unhide-text-type
1668                (match-beginning visible) (match-end visible) 'emphasis)
1669               (gnus-put-text-property-excluding-newlines
1670                (match-beginning visible) (match-end visible) 'face face)
1671               (goto-char (match-end invisible)))))))))
1672
1673 (defvar gnus-summary-article-menu)
1674 (defvar gnus-summary-post-menu)
1675
1676 ;;; Saving functions.
1677
1678 (defun gnus-article-save (save-buffer file &optional num)
1679   "Save the currently selected article."
1680   (unless gnus-save-all-headers
1681     ;; Remove headers according to `gnus-saved-headers'.
1682     (let ((gnus-visible-headers
1683            (or gnus-saved-headers gnus-visible-headers))
1684           (gnus-article-buffer save-buffer))
1685       (save-excursion
1686         (set-buffer save-buffer)
1687         (article-hide-headers 1 t))))
1688   (save-window-excursion
1689     (if (not gnus-default-article-saver)
1690         (error "No default saver is defined")
1691       ;; !!! Magic!  The saving functions all save
1692       ;; `gnus-save-article-buffer' (or so they think), but we
1693       ;; bind that variable to our save-buffer.
1694       (set-buffer gnus-article-buffer)
1695       (let* ((gnus-save-article-buffer save-buffer)
1696              (filename
1697               (cond
1698                ((not gnus-prompt-before-saving) 'default)
1699                ((eq gnus-prompt-before-saving 'always) nil)
1700                (t file)))
1701              (gnus-number-of-articles-to-be-saved
1702               (when (eq gnus-prompt-before-saving t)
1703                 num)))                  ; Magic
1704         (set-buffer gnus-article-current-summary)
1705         (funcall gnus-default-article-saver filename)))))
1706
1707 (defun gnus-read-save-file-name (prompt &optional filename
1708                                         function group headers variable)
1709   (let ((default-name
1710           (funcall function group headers (symbol-value variable)))
1711         result)
1712     (setq
1713      result
1714      (cond
1715       ((eq filename 'default)
1716        default-name)
1717       ((eq filename t)
1718        default-name)
1719       (filename filename)
1720       (t
1721        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1722               (prompt
1723                (format prompt
1724                        (if (and gnus-number-of-articles-to-be-saved
1725                                 (> gnus-number-of-articles-to-be-saved 1))
1726                            (format "these %d articles"
1727                                    gnus-number-of-articles-to-be-saved)
1728                          "this article")))
1729               (file
1730                ;; Let the split methods have their say.
1731                (cond
1732                 ;; No split name was found.
1733                 ((null split-name)
1734                  (read-file-name
1735                   (concat prompt " (default "
1736                           (file-name-nondirectory default-name) ") ")
1737                   (file-name-directory default-name)
1738                   default-name))
1739                 ;; A single group name is returned.
1740                 ((stringp split-name)
1741                  (setq default-name
1742                        (funcall function split-name headers
1743                                 (symbol-value variable)))
1744                  (read-file-name
1745                   (concat prompt " (default "
1746                           (file-name-nondirectory default-name) ") ")
1747                   (file-name-directory default-name)
1748                   default-name))
1749                 ;; A single split name was found
1750                 ((= 1 (length split-name))
1751                  (let* ((name (expand-file-name
1752                                (car split-name) gnus-article-save-directory))
1753                         (dir (cond ((file-directory-p name)
1754                                     (file-name-as-directory name))
1755                                    ((file-exists-p name) name)
1756                                    (t gnus-article-save-directory))))
1757                    (read-file-name
1758                     (concat prompt " (default " name ") ")
1759                     dir name)))
1760                 ;; A list of splits was found.
1761                 (t
1762                  (setq split-name (nreverse split-name))
1763                  (let (result)
1764                    (let ((file-name-history
1765                           (nconc split-name file-name-history)))
1766                      (setq result
1767                            (expand-file-name
1768                             (read-file-name
1769                              (concat prompt " (`M-p' for defaults) ")
1770                              gnus-article-save-directory
1771                              (car split-name))
1772                             gnus-article-save-directory)))
1773                    (car (push result file-name-history)))))))
1774          ;; Create the directory.
1775          (gnus-make-directory (file-name-directory file))
1776          ;; If we have read a directory, we append the default file name.
1777          (when (file-directory-p file)
1778            (setq file (concat (file-name-as-directory file)
1779                               (file-name-nondirectory default-name))))
1780          ;; Possibly translate some characters.
1781          (nnheader-translate-file-chars file)))))
1782     (gnus-make-directory (file-name-directory result))
1783     (set variable result)))
1784
1785 (defun gnus-article-archive-name (group)
1786   "Return the first instance of an \"Archive-name\" in the current buffer."
1787   (let ((case-fold-search t))
1788     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1789       (nnheader-concat gnus-article-save-directory
1790                        (match-string 1)))))
1791
1792 (defun gnus-article-nndoc-name (group)
1793   "If GROUP is an nndoc group, return the name of the parent group."
1794   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1795     (gnus-group-get-parameter group 'save-article-group)))
1796
1797 (defun gnus-summary-save-in-rmail (&optional filename)
1798   "Append this article to Rmail file.
1799 Optional argument FILENAME specifies file name.
1800 Directory to save to is default to `gnus-article-save-directory'."
1801   (setq filename (gnus-read-save-file-name
1802                   "Save %s in rmail file:" filename
1803                   gnus-rmail-save-name gnus-newsgroup-name
1804                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1805   (gnus-eval-in-buffer-window gnus-save-article-buffer
1806     (save-excursion
1807       (save-restriction
1808         (widen)
1809         (gnus-output-to-rmail filename))))
1810   filename)
1811
1812 (defun gnus-summary-save-in-mail (&optional filename)
1813   "Append this article to Unix mail file.
1814 Optional argument FILENAME specifies file name.
1815 Directory to save to is default to `gnus-article-save-directory'."
1816   (setq filename (gnus-read-save-file-name
1817                   "Save %s in Unix mail file:" filename
1818                   gnus-mail-save-name gnus-newsgroup-name
1819                   gnus-current-headers 'gnus-newsgroup-last-mail))
1820   (gnus-eval-in-buffer-window gnus-save-article-buffer
1821     (save-excursion
1822       (save-restriction
1823         (widen)
1824         (if (and (file-readable-p filename)
1825                  (mail-file-babyl-p filename))
1826             (rmail-output-to-rmail-file filename t)
1827           (gnus-output-to-mail filename)))))
1828   filename)
1829
1830 (defun gnus-summary-save-in-file (&optional filename overwrite)
1831   "Append this article to file.
1832 Optional argument FILENAME specifies file name.
1833 Directory to save to is default to `gnus-article-save-directory'."
1834   (setq filename (gnus-read-save-file-name
1835                   "Save %s in file:" filename
1836                   gnus-file-save-name gnus-newsgroup-name
1837                   gnus-current-headers 'gnus-newsgroup-last-file))
1838   (gnus-eval-in-buffer-window gnus-save-article-buffer
1839     (save-excursion
1840       (save-restriction
1841         (widen)
1842         (when (and overwrite
1843                    (file-exists-p filename))
1844           (delete-file filename))
1845         (gnus-output-to-file filename))))
1846   filename)
1847
1848 (defun gnus-summary-write-to-file (&optional filename)
1849   "Write this article to a file.
1850 Optional argument FILENAME specifies file name.
1851 The directory to save in defaults to `gnus-article-save-directory'."
1852   (gnus-summary-save-in-file nil t))
1853
1854 (defun gnus-summary-save-body-in-file (&optional filename)
1855   "Append this article body to a file.
1856 Optional argument FILENAME specifies file name.
1857 The directory to save in defaults to `gnus-article-save-directory'."
1858   (setq filename (gnus-read-save-file-name
1859                   "Save %s body in file:" filename
1860                   gnus-file-save-name gnus-newsgroup-name
1861                   gnus-current-headers 'gnus-newsgroup-last-file))
1862   (gnus-eval-in-buffer-window gnus-save-article-buffer
1863     (save-excursion
1864       (save-restriction
1865         (widen)
1866         (when (article-goto-body)
1867           (narrow-to-region (point) (point-max)))
1868         (gnus-output-to-file filename))))
1869   filename)
1870
1871 (defun gnus-summary-save-in-pipe (&optional command)
1872   "Pipe this article to subprocess."
1873   (setq command
1874         (cond ((and (eq command 'default)
1875                     gnus-last-shell-command)
1876                gnus-last-shell-command)
1877               (command command)
1878               (t (read-string
1879                   (format
1880                    "Shell command on %s: "
1881                    (if (and gnus-number-of-articles-to-be-saved
1882                             (> gnus-number-of-articles-to-be-saved 1))
1883                        (format "these %d articles"
1884                                gnus-number-of-articles-to-be-saved)
1885                      "this article"))
1886                   gnus-last-shell-command))))
1887   (when (string-equal command "")
1888     (setq command gnus-last-shell-command))
1889   (gnus-eval-in-buffer-window gnus-article-buffer
1890     (save-restriction
1891       (widen)
1892       (shell-command-on-region (point-min) (point-max) command nil)))
1893   (setq gnus-last-shell-command command))
1894
1895 ;;; Article file names when saving.
1896
1897 (defun gnus-capitalize-newsgroup (newsgroup)
1898   "Capitalize NEWSGROUP name."
1899   (when (not (zerop (length newsgroup)))
1900     (concat (char-to-string (upcase (aref newsgroup 0)))
1901             (substring newsgroup 1))))
1902
1903 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1904   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1905 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1906 Otherwise, it is like ~/News/news/group/num."
1907   (let ((default
1908           (expand-file-name
1909            (concat (if (gnus-use-long-file-name 'not-save)
1910                        (gnus-capitalize-newsgroup newsgroup)
1911                      (gnus-newsgroup-directory-form newsgroup))
1912                    "/" (int-to-string (mail-header-number headers)))
1913            gnus-article-save-directory)))
1914     (if (and last-file
1915              (string-equal (file-name-directory default)
1916                            (file-name-directory last-file))
1917              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1918         default
1919       (or last-file default))))
1920
1921 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1922   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1923 If variable `gnus-use-long-file-name' is non-nil, it is
1924 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1925   (let ((default
1926           (expand-file-name
1927            (concat (if (gnus-use-long-file-name 'not-save)
1928                        newsgroup
1929                      (gnus-newsgroup-directory-form newsgroup))
1930                    "/" (int-to-string (mail-header-number headers)))
1931            gnus-article-save-directory)))
1932     (if (and last-file
1933              (string-equal (file-name-directory default)
1934                            (file-name-directory last-file))
1935              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1936         default
1937       (or last-file default))))
1938
1939 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1940   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1941 If variable `gnus-use-long-file-name' is non-nil, it is
1942 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1943   (or last-file
1944       (expand-file-name
1945        (if (gnus-use-long-file-name 'not-save)
1946            (gnus-capitalize-newsgroup newsgroup)
1947          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1948        gnus-article-save-directory)))
1949
1950 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1951   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1952 If variable `gnus-use-long-file-name' is non-nil, it is
1953 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1954   (or last-file
1955       (expand-file-name
1956        (if (gnus-use-long-file-name 'not-save)
1957            newsgroup
1958          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1959        gnus-article-save-directory)))
1960
1961 (eval-and-compile
1962   (mapcar
1963    (lambda (func)
1964      (let (afunc gfunc)
1965        (if (consp func)
1966            (setq afunc (car func)
1967                  gfunc (cdr func))
1968          (setq afunc func
1969                gfunc (intern (format "gnus-%s" func))))
1970        (fset gfunc
1971              (if (not (fboundp afunc))
1972                  nil
1973                `(lambda (&optional interactive &rest args)
1974                   ,(documentation afunc t)
1975                   (interactive (list t))
1976                   (save-excursion
1977                     (set-buffer gnus-article-buffer)
1978                     (if interactive
1979                         (call-interactively ',afunc)
1980                       (apply ',afunc args))))))))
1981    '(article-hide-headers
1982      article-hide-boring-headers
1983      article-treat-overstrike
1984      (article-fill . gnus-article-word-wrap)
1985      article-remove-cr
1986      article-display-x-face
1987      article-de-quoted-unreadable
1988      article-mime-decode-quoted-printable
1989      article-hide-pgp
1990      article-hide-pem
1991      article-hide-signature
1992      article-remove-trailing-blank-lines
1993      article-strip-leading-blank-lines
1994      article-strip-multiple-blank-lines
1995      article-strip-leading-space
1996      article-strip-trailing-space
1997      article-strip-blank-lines
1998      article-strip-all-blank-lines
1999      article-date-local
2000      article-date-iso8601
2001      article-date-original
2002      article-date-ut
2003      article-decode-mime-words
2004      article-decode-charset
2005      article-decode-encoded-words
2006      article-date-user
2007      article-date-lapsed
2008      article-emphasize
2009      article-treat-dumbquotes
2010      article-normalize-headers
2011      (article-show-all . gnus-article-show-all-headers))))
2012 \f
2013 ;;;
2014 ;;; Gnus article mode
2015 ;;;
2016
2017 (put 'gnus-article-mode 'mode-class 'special)
2018
2019 (set-keymap-parent gnus-article-mode-map widget-keymap)
2020
2021 (gnus-define-keys gnus-article-mode-map
2022   " " gnus-article-goto-next-page
2023   "\177" gnus-article-goto-prev-page
2024   [delete] gnus-article-goto-prev-page
2025   "\C-c^" gnus-article-refer-article
2026   "h" gnus-article-show-summary
2027   "s" gnus-article-show-summary
2028   "\C-c\C-m" gnus-article-mail
2029   "?" gnus-article-describe-briefly
2030   "e" gnus-article-edit
2031   "<" beginning-of-buffer
2032   ">" end-of-buffer
2033   "\C-c\C-i" gnus-info-find-node
2034   "\C-c\C-b" gnus-bug
2035
2036   "\C-d" gnus-article-read-summary-keys
2037   "\M-*" gnus-article-read-summary-keys
2038   "\M-#" gnus-article-read-summary-keys
2039   "\M-^" gnus-article-read-summary-keys
2040   "\M-g" gnus-article-read-summary-keys)
2041
2042 (substitute-key-definition
2043  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2044
2045 (defun gnus-article-make-menu-bar ()
2046   (gnus-turn-off-edit-menu 'article)
2047   (unless (boundp 'gnus-article-article-menu)
2048     (easy-menu-define
2049      gnus-article-article-menu gnus-article-mode-map ""
2050      '("Article"
2051        ["Scroll forwards" gnus-article-goto-next-page t]
2052        ["Scroll backwards" gnus-article-goto-prev-page t]
2053        ["Show summary" gnus-article-show-summary t]
2054        ["Fetch Message-ID at point" gnus-article-refer-article t]
2055        ["Mail to address at point" gnus-article-mail t]
2056        ["Send a bug report" gnus-bug t]))
2057
2058     (easy-menu-define
2059      gnus-article-treatment-menu gnus-article-mode-map ""
2060      '("Treatment"
2061        ["Hide headers" gnus-article-hide-headers t]
2062        ["Hide signature" gnus-article-hide-signature t]
2063        ["Hide citation" gnus-article-hide-citation t]
2064        ["Treat overstrike" gnus-article-treat-overstrike t]
2065        ["Remove carriage return" gnus-article-remove-cr t]
2066        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
2067
2068     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2069
2070     (when (boundp 'gnus-summary-post-menu)
2071       (define-key gnus-article-mode-map [menu-bar post]
2072         (cons "Post" gnus-summary-post-menu)))
2073
2074     (gnus-run-hooks 'gnus-article-menu-hook)))
2075
2076 (defun gnus-article-mode ()
2077   "Major mode for displaying an article.
2078
2079 All normal editing commands are switched off.
2080
2081 The following commands are available in addition to all summary mode
2082 commands:
2083 \\<gnus-article-mode-map>
2084 \\[gnus-article-next-page]\t Scroll the article one page forwards
2085 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2086 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2087 \\[gnus-article-show-summary]\t Display the summary buffer
2088 \\[gnus-article-mail]\t Send a reply to the address near point
2089 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2090 \\[gnus-info-find-node]\t Go to the Gnus info node"
2091   (interactive)
2092   (when (gnus-visual-p 'article-menu 'menu)
2093     (gnus-article-make-menu-bar))
2094   (gnus-simplify-mode-line)
2095   (setq mode-name "Article")
2096   (setq major-mode 'gnus-article-mode)
2097   (make-local-variable 'minor-mode-alist)
2098   (use-local-map gnus-article-mode-map)
2099   (gnus-update-format-specifications nil 'article-mode)
2100   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2101   (make-local-variable 'gnus-page-broken)
2102   (make-local-variable 'gnus-button-marker-list)
2103   (make-local-variable 'gnus-article-current-summary)
2104   (make-local-variable 'gnus-article-mime-handles)
2105   (make-local-variable 'gnus-article-decoded-p)
2106   (make-local-variable 'gnus-article-mime-handle-alist)
2107   (gnus-set-default-directory)
2108   (buffer-disable-undo)
2109   (setq buffer-read-only t)
2110   (set-syntax-table gnus-article-mode-syntax-table)
2111   (mm-enable-multibyte)
2112   (gnus-run-hooks 'gnus-article-mode-hook))
2113
2114 (defun gnus-article-setup-buffer ()
2115   "Initialize the article buffer."
2116   (let* ((name (if gnus-single-article-buffer "*Article*"
2117                  (concat "*Article " gnus-newsgroup-name "*")))
2118          (original
2119           (progn (string-match "\\*Article" name)
2120                  (concat " *Original Article"
2121                          (substring name (match-end 0))))))
2122     (setq gnus-article-buffer name)
2123     (setq gnus-original-article-buffer original)
2124     (setq gnus-article-mime-handle-alist nil)
2125     ;; This might be a variable local to the summary buffer.
2126     (unless gnus-single-article-buffer
2127       (save-excursion
2128         (set-buffer gnus-summary-buffer)
2129         (setq gnus-article-buffer name)
2130         (setq gnus-original-article-buffer original)
2131         (gnus-set-global-variables)))
2132     ;; Init original article buffer.
2133     (save-excursion
2134       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2135       (mm-enable-multibyte)
2136       (setq major-mode 'gnus-original-article-mode)
2137       (make-local-variable 'gnus-original-article))
2138     (if (get-buffer name)
2139         (save-excursion
2140           (set-buffer name)
2141           (buffer-disable-undo)
2142           (setq buffer-read-only t)
2143           (unless (eq major-mode 'gnus-article-mode)
2144             (gnus-article-mode))
2145           (current-buffer))
2146       (save-excursion
2147         (set-buffer (gnus-get-buffer-create name))
2148         (gnus-article-mode)
2149         (make-local-variable 'gnus-summary-buffer)
2150         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2151         (current-buffer)))))
2152
2153 ;; Set article window start at LINE, where LINE is the number of lines
2154 ;; from the head of the article.
2155 (defun gnus-article-set-window-start (&optional line)
2156   (set-window-start
2157    (get-buffer-window gnus-article-buffer t)
2158    (save-excursion
2159      (set-buffer gnus-article-buffer)
2160      (goto-char (point-min))
2161      (if (not line)
2162          (point-min)
2163        (gnus-message 6 "Moved to bookmark")
2164        (search-forward "\n\n" nil t)
2165        (forward-line line)
2166        (point)))))
2167
2168 (defun gnus-article-prepare (article &optional all-headers header)
2169   "Prepare ARTICLE in article mode buffer.
2170 ARTICLE should either be an article number or a Message-ID.
2171 If ARTICLE is an id, HEADER should be the article headers.
2172 If ALL-HEADERS is non-nil, no headers are hidden."
2173   (save-excursion
2174     ;; Make sure we start in a summary buffer.
2175     (unless (eq major-mode 'gnus-summary-mode)
2176       (set-buffer gnus-summary-buffer))
2177     (setq gnus-summary-buffer (current-buffer))
2178     (let* ((gnus-article (if header (mail-header-number header) article))
2179            (summary-buffer (current-buffer))
2180            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2181            (group gnus-newsgroup-name)
2182            result)
2183       (save-excursion
2184         (gnus-article-setup-buffer)
2185         (set-buffer gnus-article-buffer)
2186         ;; Deactivate active regions.
2187         (when (and (boundp 'transient-mark-mode)
2188                    transient-mark-mode)
2189           (setq mark-active nil))
2190         (if (not (setq result (let ((buffer-read-only nil))
2191                                 (gnus-request-article-this-buffer
2192                                  article group))))
2193             ;; There is no such article.
2194             (save-excursion
2195               (when (and (numberp article)
2196                          (not (memq article gnus-newsgroup-sparse)))
2197                 (setq gnus-article-current
2198                       (cons gnus-newsgroup-name article))
2199                 (set-buffer gnus-summary-buffer)
2200                 (setq gnus-current-article article)
2201                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2202                     (progn
2203                       (gnus-summary-set-agent-mark article)
2204                       (message "Message marked for downloading"))
2205                   (gnus-summary-mark-article article gnus-canceled-mark)
2206                   (unless (memq article gnus-newsgroup-sparse)
2207                     (gnus-error 1
2208                      "No such article (may have expired or been canceled)")))))
2209           (if (or (eq result 'pseudo)
2210                   (eq result 'nneething))
2211               (progn
2212                 (save-excursion
2213                   (set-buffer summary-buffer)
2214                   (push article gnus-newsgroup-history)
2215                   (setq gnus-last-article gnus-current-article
2216                         gnus-current-article 0
2217                         gnus-current-headers nil
2218                         gnus-article-current nil)
2219                   (if (eq result 'nneething)
2220                       (gnus-configure-windows 'summary)
2221                     (gnus-configure-windows 'article))
2222                   (gnus-set-global-variables))
2223                 (let ((gnus-article-mime-handle-alist-1
2224                        gnus-article-mime-handle-alist))
2225                   (gnus-set-mode-line 'article)))
2226             ;; The result from the `request' was an actual article -
2227             ;; or at least some text that is now displayed in the
2228             ;; article buffer.
2229             (when (and (numberp article)
2230                        (not (eq article gnus-current-article)))
2231               ;; Seems like a new article has been selected.
2232               ;; `gnus-current-article' must be an article number.
2233               (save-excursion
2234                 (set-buffer summary-buffer)
2235                 (push article gnus-newsgroup-history)
2236                 (setq gnus-last-article gnus-current-article
2237                       gnus-current-article article
2238                       gnus-current-headers
2239                       (gnus-summary-article-header gnus-current-article)
2240                       gnus-article-current
2241                       (cons gnus-newsgroup-name gnus-current-article))
2242                 (unless (vectorp gnus-current-headers)
2243                   (setq gnus-current-headers nil))
2244                 (gnus-summary-goto-subject gnus-current-article)
2245                 (when (gnus-summary-show-thread)
2246                   ;; If the summary buffer really was folded, the
2247                   ;; previous goto may not actually have gone to
2248                   ;; the right article, but the thread root instead.
2249                   ;; So we go again.
2250                   (gnus-summary-goto-subject gnus-current-article))
2251                 (gnus-run-hooks 'gnus-mark-article-hook)
2252                 (gnus-set-mode-line 'summary)
2253                 (when (gnus-visual-p 'article-highlight 'highlight)
2254                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2255                 ;; Set the global newsgroup variables here.
2256                 ;; Suggested by Jim Sisolak
2257                 ;; <sisolak@trans4.neep.wisc.edu>.
2258                 (gnus-set-global-variables)
2259                 (setq gnus-have-all-headers
2260                       (or all-headers gnus-show-all-headers))))
2261             (when (or (numberp article)
2262                       (stringp article))
2263               (gnus-article-prepare-display)
2264               ;; Do page break.
2265               (goto-char (point-min))
2266               (setq gnus-page-broken
2267                     (when gnus-break-pages
2268                       (gnus-narrow-to-page)
2269                       t)))
2270             (let ((gnus-article-mime-handle-alist-1
2271                    gnus-article-mime-handle-alist))
2272               (gnus-set-mode-line 'article))
2273             (gnus-configure-windows 'article)
2274             (article-goto-body)
2275             (set-window-point (get-buffer-window (current-buffer)) (point))
2276             t))))))
2277
2278 ;;;###autoload
2279 (defun gnus-article-prepare-display ()
2280   "Make the current buffer look like a nice article."
2281   ;; Hooks for getting information from the article.
2282   ;; This hook must be called before being narrowed.
2283   (let ((gnus-article-buffer (current-buffer))
2284         buffer-read-only)
2285     (unless (eq major-mode 'gnus-article-mode)
2286       (gnus-article-mode))
2287     (setq buffer-read-only nil)
2288     (gnus-run-hooks 'gnus-tmp-internal-hook)
2289     (gnus-run-hooks 'gnus-article-prepare-hook)
2290     (when gnus-display-mime-function
2291       (funcall gnus-display-mime-function))
2292     ;; Perform the article display hooks.
2293     (gnus-run-hooks 'gnus-article-display-hook)))
2294
2295 ;;;
2296 ;;; Gnus MIME viewing functions
2297 ;;;
2298
2299 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2300   "The following specs can be used:
2301 %t  The MIME type
2302 %T  MIME type, along with additional info
2303 %n  The `name' parameter
2304 %d  The description, if any
2305 %l  The length of the encoded part
2306 %p  The part identifier number
2307 %e  Dots if the part isn't displayed")
2308
2309 (defvar gnus-mime-button-line-format-alist
2310   '((?t gnus-tmp-type ?s)
2311     (?T gnus-tmp-type-long ?s)
2312     (?n gnus-tmp-name ?s)
2313     (?d gnus-tmp-description ?s)
2314     (?p gnus-tmp-id ?s)
2315     (?l gnus-tmp-length ?d)
2316     (?e gnus-tmp-dots ?s)))
2317
2318 (defvar gnus-mime-button-commands
2319   '((gnus-article-press-button  "\r"    "Toggle Display")
2320     (gnus-mime-view-part        "v"     "View Interactively...")
2321     (gnus-mime-save-part        "o"     "Save...")
2322     (gnus-mime-copy-part        "c"     "View As Text, In Other Buffer")
2323     (gnus-mime-inline-part      "i"     "View As Text, In This Buffer")
2324     (gnus-mime-internalize-part "E"     "View Internally")
2325     (gnus-mime-externalize-part "e"     "View Externally")
2326     (gnus-mime-pipe-part        "|"     "Pipe To Command...")))
2327
2328 (defun gnus-article-mime-part-status ()
2329   (if gnus-article-mime-handle-alist-1
2330       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2331     ""))
2332
2333 (defvar gnus-mime-button-map nil)
2334 (unless gnus-mime-button-map
2335   (setq gnus-mime-button-map (make-sparse-keymap))
2336   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2337   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2338   (define-key gnus-mime-button-map gnus-mouse-3 'gnus-mime-button-menu)
2339   (mapcar (lambda (c)
2340             (define-key gnus-mime-button-map (cadr c) (car c)))
2341           gnus-mime-button-commands))
2342
2343 (defun gnus-mime-button-menu (event)
2344   "Construct a context-sensitive menu of MIME commands."
2345   (interactive "e")
2346   (gnus-article-check-buffer)
2347   (let ((response (x-popup-menu 
2348                   t `("MIME Part" 
2349                       ("" ,@(mapcar (lambda (c)
2350                                       (cons (caddr c) (car c)))
2351                                     gnus-mime-button-commands)))))
2352         (pos (event-start event)))
2353     (when response
2354       (set-buffer (window-buffer (posn-window pos)))
2355       (goto-char (posn-point pos))
2356       (funcall response))))
2357
2358 (defun gnus-mime-view-all-parts (&optional handles)
2359   "View all the MIME parts."
2360   (interactive)
2361   (save-current-buffer
2362     (set-buffer gnus-article-buffer)
2363     (let ((handles (or handles gnus-article-mime-handles))
2364           (rfc2047-default-charset gnus-newsgroup-default-charset)
2365           (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2366       (if (stringp (car handles))
2367           (gnus-mime-view-all-parts (cdr handles))
2368         (mapcar 'mm-display-part handles)))))
2369
2370 (defun gnus-mime-save-part ()
2371   "Save the MIME part under point."
2372   (interactive)
2373   (gnus-article-check-buffer)
2374   (let ((data (get-text-property (point) 'gnus-data)))
2375     (mm-save-part data)))
2376
2377 (defun gnus-mime-pipe-part ()
2378   "Pipe the MIME part under point to a process."
2379   (interactive)
2380   (gnus-article-check-buffer)
2381   (let ((data (get-text-property (point) 'gnus-data)))
2382     (mm-pipe-part data)))
2383
2384 (defun gnus-mime-view-part ()
2385   "Interactively choose a view method for the MIME part under point."
2386   (interactive)
2387   (gnus-article-check-buffer)
2388   (let ((data (get-text-property (point) 'gnus-data)))
2389     (mm-interactively-view-part data)))
2390
2391 (defun gnus-mime-copy-part (&optional handle)
2392   "Put the the MIME part under point into a new buffer."
2393   (interactive)
2394   (gnus-article-check-buffer)
2395   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2396          (contents (mm-get-part handle))|
2397          (base (file-name-nondirectory
2398                 (or
2399                  (mail-content-type-get (mm-handle-type handle) 'name)
2400                  (mail-content-type-get (mm-handle-type handle)
2401                                         'filename)
2402                  "*decoded*")))
2403          (buffer (generate-new-buffer base)))
2404     (switch-to-buffer buffer)
2405     (insert contents)
2406     ;; We do it this way to make `normal-mode' set the appropriate mode.
2407     (unwind-protect
2408         (progn
2409           (setq buffer-file-name (expand-file-name base))
2410           (normal-mode))
2411       (setq buffer-file-name nil))
2412     (goto-char (point-min))))
2413
2414 (defun gnus-mime-inline-part (&optional charset)
2415   "Insert the MIME part under point into the current buffer."
2416   (interactive "P") ; For compatibility reasons we are not using "z".
2417   (gnus-article-check-buffer)
2418   (let* ((data (get-text-property (point) 'gnus-data))
2419          contents
2420          (b (point))
2421          buffer-read-only)
2422     (if (mm-handle-undisplayer data)
2423         (mm-remove-part data)
2424       (setq contents (mm-get-part data))
2425       (forward-line 2)
2426       (when charset 
2427         (unless (symbolp charset)
2428           (setq charset (mm-read-coding-system "Charset: ")))
2429         (setq contents (mm-decode-coding-string contents charset)))
2430       (mm-insert-inline data contents)
2431       (goto-char b))))
2432
2433 (defun gnus-mime-externalize-part (&optional handle)
2434   "View the MIME part under point with an external viewer."
2435   (interactive)
2436   (gnus-article-check-buffer)
2437   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2438          (mm-user-display-methods nil)
2439          (mm-all-images-fit t)
2440          (rfc2047-default-charset gnus-newsgroup-default-charset)
2441          (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2442     (if (mm-handle-undisplayer handle)
2443         (mm-remove-part handle)
2444       (mm-display-part handle))))
2445
2446 (defun gnus-mime-internalize-part (&optional handle)
2447   "View the MIME part under point with an internal viewer."
2448   (interactive)
2449   (gnus-article-check-buffer)
2450   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2451          (mm-user-display-methods '((".*" . inline)))
2452          (mm-all-images-fit t)
2453          (rfc2047-default-charset gnus-newsgroup-default-charset)
2454          (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2455     (if (mm-handle-undisplayer handle)
2456         (mm-remove-part handle)
2457       (mm-display-part handle))))
2458
2459 (defun gnus-article-part-wrapper (n function)
2460   (save-current-buffer
2461     (set-buffer gnus-article-buffer)
2462     (when (> n (length gnus-article-mime-handle-alist))
2463       (error "No such part"))
2464     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2465       (funcall function handle))))
2466
2467 (defun gnus-article-pipe-part (n)
2468   "Pipe MIME part N, which is the numerical prefix."
2469   (interactive "p")
2470   (gnus-article-part-wrapper n 'mm-pipe-part))
2471   
2472 (defun gnus-article-save-part (n)
2473   "Save MIME part N, which is the numerical prefix."
2474   (interactive "p")
2475   (gnus-article-part-wrapper n 'mm-save-part))
2476   
2477 (defun gnus-article-interactively-view-part (n)
2478   "Pipe MIME part N, which is the numerical prefix."
2479   (interactive "p")
2480   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2481   
2482 (defun gnus-article-copy-part (n)
2483   "Pipe MIME part N, which is the numerical prefix."
2484   (interactive "p")
2485   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2486
2487 (defun gnus-article-externalize-part (n)
2488   "Pipe MIME part N, which is the numerical prefix."
2489   (interactive "p")
2490   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2491   
2492 (defun gnus-article-view-part (n)
2493   "View MIME part N, which is the numerical prefix."
2494   (interactive "p")
2495   (save-current-buffer
2496     (set-buffer gnus-article-buffer)
2497     (when (> n (length gnus-article-mime-handle-alist))
2498       (error "No such part"))
2499     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2500       (when (gnus-article-goto-part n)
2501         (if (equal (car handle) "multipart/alternative")
2502             (gnus-article-press-button)
2503           (when (eq (gnus-mm-display-part handle) 'internal)
2504             (gnus-set-window-start)))))))
2505
2506 (defun gnus-mm-display-part (handle)
2507   "Display HANDLE and fix MIME button."
2508   (let ((id (get-text-property (point) 'gnus-part))
2509         (point (point))
2510         buffer-read-only)
2511     (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
2512     (gnus-insert-mime-button
2513      handle id (list (not (mm-handle-displayed-p handle))))
2514     (prog1
2515         (let ((window (selected-window))
2516               (rfc2047-default-charset gnus-newsgroup-default-charset)
2517               (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2518           (save-excursion
2519             (unwind-protect
2520                 (let ((win (get-buffer-window (current-buffer) t)))
2521                   (if win
2522                       (select-window win))
2523                   (goto-char point)
2524                   (forward-line)
2525                   (mm-display-part handle))
2526               (select-window window))))
2527       (goto-char point))))
2528
2529 (defun gnus-article-goto-part (n)
2530   "Go to MIME part N."
2531   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
2532     (when point
2533       (goto-char point))))
2534
2535 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
2536   (let ((gnus-tmp-name
2537          (or (mail-content-type-get (mm-handle-type handle)
2538                                     'name)
2539              (mail-content-type-get (mm-handle-disposition handle)
2540                                     'filename)
2541              ""))
2542         (gnus-tmp-type (car (mm-handle-type handle)))
2543         (gnus-tmp-description
2544          (mail-decode-encoded-word-string (or (mm-handle-description handle)
2545                                               "")))
2546         (gnus-tmp-dots
2547          (if (if displayed (car displayed)
2548                (mm-handle-displayed-p handle))
2549              "" "..."))
2550         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
2551                            (buffer-size)))
2552         gnus-tmp-type-long b e)
2553     (when (string-match ".*/" gnus-tmp-name)
2554       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
2555     (setq gnus-tmp-type-long (concat gnus-tmp-type
2556                                      (and (not (equal gnus-tmp-name ""))
2557                                           (concat "; " gnus-tmp-name))))
2558     (or (equal gnus-tmp-description "")
2559         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
2560     (unless (bolp)
2561       (insert "\n"))
2562     (setq b (point))
2563     (gnus-eval-format
2564      gnus-mime-button-line-format gnus-mime-button-line-format-alist
2565      `(local-map ,gnus-mime-button-map
2566                  keymap ,gnus-mime-button-map
2567                  gnus-callback gnus-mm-display-part
2568                  gnus-part ,gnus-tmp-id
2569                  article-type annotation
2570                  gnus-data ,handle))
2571     (setq e (point))
2572     (widget-convert-button 'link b e
2573                            :mime-handle handle
2574                            :action 'gnus-widget-press-button
2575                            :button-keymap gnus-mime-button-map
2576                            :help-echo
2577                            (lambda (widget)
2578                              ;; Needed to properly clear the message
2579                              ;; due to a bug in wid-edit
2580                              (setq help-echo-owns-message t)
2581                              (format
2582                               "Click to %s the MIME part; %s for more options"
2583                               (if (mm-handle-displayed-p
2584                                    (widget-get widget :mime-handle))
2585                                   "hide" "show")
2586                               (if gnus-xemacs "button3" "mouse-3"))))))
2587
2588 (defun gnus-widget-press-button (elems el)
2589   (goto-char (widget-get elems :from))
2590   (gnus-article-press-button))
2591
2592 (defun gnus-display-mime (&optional ihandles)
2593   "Insert MIME buttons in the buffer."
2594   (save-excursion
2595     (save-selected-window
2596       (let ((window (get-buffer-window gnus-article-buffer))
2597             (point (point)))
2598         (when window
2599           (select-window window)
2600           ;; We have to do this since selecting the window
2601           ;; may change the point.  So we set the window point.
2602           (set-window-point window point)))
2603       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
2604              handle name type b e display)
2605         (unless ihandles
2606           ;; Top-level call; we clean up.
2607           (mm-destroy-parts gnus-article-mime-handles)
2608           (setq gnus-article-mime-handles handles
2609                 gnus-article-mime-handle-alist nil)
2610           ;; We allow users to glean info from the handles.
2611           (when gnus-article-mime-part-function
2612             (gnus-mime-part-function handles)))
2613         (when (and handles
2614                    (or (not (stringp (car handles)))
2615                        (cdr handles)))
2616           (unless ihandles
2617             ;; Clean up for mime parts.
2618             (article-goto-body)
2619             (delete-region (point) (point-max)))
2620           (gnus-mime-display-part handles))))))
2621
2622 (defvar gnus-mime-display-multipart-as-mixed nil)
2623
2624 (defun gnus-mime-display-part (handle)
2625   (cond
2626    ;; Single part.
2627    ((not (stringp (car handle)))
2628     (gnus-mime-display-single handle))
2629    ;; multipart/alternative
2630    ((and (equal (car handle) "multipart/alternative")
2631          (not gnus-mime-display-multipart-as-mixed))
2632     (let ((id (1+ (length gnus-article-mime-handle-alist))))
2633       (push (cons id handle) gnus-article-mime-handle-alist)
2634       (gnus-mime-display-alternative (cdr handle) nil nil id)))
2635    ;; multipart/related
2636    ((and (equal (car handle) "multipart/related")
2637          (not gnus-mime-display-multipart-as-mixed))
2638     ;;;!!!We should find the start part, but we just default
2639     ;;;!!!to the first part.
2640     (gnus-mime-display-part (cadr handle)))
2641    ;; Other multiparts are handled like multipart/mixed.
2642    (t
2643     (gnus-mime-display-mixed (cdr handle)))))
2644
2645 (defun gnus-mime-part-function (handles)
2646   (if (stringp (car handles))
2647       (mapcar 'gnus-mime-part-function (cdr handles))
2648     (funcall gnus-article-mime-part-function handles)))
2649
2650 (defun gnus-mime-display-mixed (handles)
2651   (mapcar 'gnus-mime-display-part handles))
2652
2653 (defun gnus-mime-display-single (handle)
2654   (let ((type (car (mm-handle-type handle)))
2655         (ignored gnus-ignored-mime-types)
2656         (not-attachment t)
2657         (move nil)
2658         display text)
2659     (catch 'ignored
2660       (progn
2661         (while ignored
2662           (when (string-match (pop ignored) type)
2663             (throw 'ignored nil)))
2664         (if (and (setq not-attachment
2665                        (or (not (mm-handle-disposition handle))
2666                            (equal (car (mm-handle-disposition handle))
2667                                   "inline")))
2668                  (mm-automatic-display-p type)
2669                  (or (mm-inlinable-part-p type)
2670                      (mm-automatic-external-display-p type)))
2671             (setq display t)
2672           (when (equal (car (split-string type "/"))
2673                        "text")
2674             (setq text t)))
2675         (let ((id (1+ (length gnus-article-mime-handle-alist))))
2676           (push (cons id handle) gnus-article-mime-handle-alist)
2677           (when (or (not display)
2678                     (not (gnus-unbuttonized-mime-type-p type)))
2679             (gnus-article-insert-newline)
2680             (gnus-insert-mime-button
2681              handle id (list (or display
2682                                  (and not-attachment text))))
2683             (gnus-article-insert-newline)
2684             (gnus-article-insert-newline)
2685             (setq move t)))
2686         (cond
2687          (display
2688           (when move
2689             (forward-line -2))
2690           (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
2691                 (mm-charset-iso-8859-1-forced 
2692                  gnus-newsgroup-iso-8859-1-forced))
2693             (mm-display-part handle t))
2694           (goto-char (point-max)))
2695          ((and text not-attachment)
2696           (when move
2697             (forward-line -2))
2698           (gnus-article-insert-newline)
2699           (mm-insert-inline handle (mm-get-part handle))
2700           (goto-char (point-max))))))))
2701
2702 (defun gnus-unbuttonized-mime-type-p (type)
2703   "Say whether TYPE is to be unbuttonized."
2704   (unless gnus-inhibit-mime-unbuttonizing
2705     (catch 'found
2706       (let ((types gnus-unbuttonized-mime-types))
2707         (while types
2708           (when (string-match (pop types) type)
2709             (throw 'found t)))))))
2710
2711 (defun gnus-article-insert-newline ()
2712   "Insert a newline, but mark it as undeletable."
2713   (gnus-put-text-property
2714    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
2715
2716 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
2717   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
2718          (ihandles handles)
2719          (point (point))
2720          handle buffer-read-only from props begend not-pref)
2721     (save-window-excursion
2722       (save-restriction
2723         (when ibegend
2724           (narrow-to-region (car ibegend)
2725                             (or (cdr ibegend)
2726                                 (progn
2727                                   (goto-char (car ibegend))
2728                                   (forward-line 2)
2729                                   (point))))
2730           (delete-region (point-min) (point-max))
2731           (mm-remove-parts handles))
2732         (setq begend (list (point-marker)))
2733         ;; Do the toggle.
2734         (unless (setq not-pref (cadr (member preferred ihandles)))
2735           (setq not-pref (car ihandles)))
2736         (when (or ibegend
2737                   (not (gnus-unbuttonized-mime-type-p
2738                         "multipart/alternative")))
2739           (gnus-add-text-properties
2740            (setq from (point))
2741            (progn
2742              (insert (format "%d.  " id))
2743              (point))
2744            `(gnus-callback
2745              (lambda (handles)
2746                (unless ,(not ibegend)
2747                  (setq gnus-article-mime-handle-alist
2748                        ',gnus-article-mime-handle-alist))
2749                (gnus-mime-display-alternative
2750                 ',ihandles ',not-pref ',begend ,id))
2751              local-map ,gnus-mime-button-map
2752              ,gnus-mouse-face-prop ,gnus-article-mouse-face
2753              face ,gnus-article-button-face
2754              keymap ,gnus-mime-button-map
2755              gnus-part ,id
2756              gnus-data ,handle))
2757           (widget-convert-button 'link from (point)
2758                                  :action 'gnus-widget-press-button
2759                                  :button-keymap gnus-widget-button-keymap)
2760           ;; Do the handles
2761           (while (setq handle (pop handles))
2762             (gnus-add-text-properties
2763              (setq from (point))
2764              (progn
2765                (insert (format "(%c) %-18s"
2766                                (if (equal handle preferred) ?* ? )
2767                                (if (stringp (car handle))
2768                                    (car handle)
2769                                  (car (mm-handle-type handle)))))
2770                (point))
2771              `(gnus-callback
2772                (lambda (handles)
2773                  (unless ,(not ibegend)
2774                    (setq gnus-article-mime-handle-alist
2775                          ',gnus-article-mime-handle-alist))
2776                  (gnus-mime-display-alternative
2777                   ',ihandles ',handle ',begend ,id))
2778                local-map ,gnus-mime-button-map
2779                ,gnus-mouse-face-prop ,gnus-article-mouse-face
2780                face ,gnus-article-button-face
2781                keymap ,gnus-mime-button-map
2782                gnus-part ,id
2783                gnus-data ,handle))
2784             (widget-convert-button 'link from (point)
2785                                    :action 'gnus-widget-press-button
2786                                    :button-keymap gnus-widget-button-keymap)
2787             (insert "  "))
2788           (insert "\n\n"))
2789         (when preferred
2790           (if (stringp (car preferred))
2791               (gnus-display-mime preferred)
2792             (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
2793                   (mm-charset-iso-8859-1-forced 
2794                    gnus-newsgroup-iso-8859-1-forced))
2795               (mm-display-part preferred)))
2796           (goto-char (point-max))
2797           (setcdr begend (point-marker)))))
2798     (when ibegend
2799       (goto-char point))))
2800
2801 (defun gnus-article-wash-status ()
2802   "Return a string which display status of article washing."
2803   (save-excursion
2804     (set-buffer gnus-article-buffer)
2805     (let ((cite (gnus-article-hidden-text-p 'cite))
2806           (headers (gnus-article-hidden-text-p 'headers))
2807           (boring (gnus-article-hidden-text-p 'boring-headers))
2808           (pgp (gnus-article-hidden-text-p 'pgp))
2809           (pem (gnus-article-hidden-text-p 'pem))
2810           (signature (gnus-article-hidden-text-p 'signature))
2811           (overstrike (gnus-article-hidden-text-p 'overstrike))
2812           (emphasis (gnus-article-hidden-text-p 'emphasis)))
2813       (format "%c%c%c%c%c%c"
2814               (if cite ?c ? )
2815               (if (or headers boring) ?h ? )
2816               (if (or pgp pem) ?p ? )
2817               (if signature ?s ? )
2818               (if overstrike ?o ? )
2819               (if emphasis ?e ? )))))
2820
2821 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2822
2823 (defun gnus-article-maybe-hide-headers ()
2824   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2825 Provided for backwards compatibility."
2826   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
2827                  (not (save-excursion (set-buffer gnus-summary-buffer)
2828                                       gnus-have-all-headers)))
2829              (not gnus-inhibit-hiding))
2830     (gnus-article-hide-headers)))
2831
2832 ;;; Article savers.
2833
2834 (defun gnus-output-to-file (file-name)
2835   "Append the current article to a file named FILE-NAME."
2836   (let ((artbuf (current-buffer)))
2837     (with-temp-buffer
2838       (insert-buffer-substring artbuf)
2839       ;; Append newline at end of the buffer as separator, and then
2840       ;; save it to file.
2841       (goto-char (point-max))
2842       (insert "\n")
2843       (append-to-file (point-min) (point-max) file-name)
2844       t)))
2845
2846 (defun gnus-narrow-to-page (&optional arg)
2847   "Narrow the article buffer to a page.
2848 If given a numerical ARG, move forward ARG pages."
2849   (interactive "P")
2850   (setq arg (if arg (prefix-numeric-value arg) 0))
2851   (save-excursion
2852     (set-buffer gnus-article-buffer)
2853     (goto-char (point-min))
2854     (widen)
2855     ;; Remove any old next/prev buttons.
2856     (when (gnus-visual-p 'page-marker)
2857       (let ((buffer-read-only nil))
2858         (gnus-remove-text-with-property 'gnus-prev)
2859         (gnus-remove-text-with-property 'gnus-next)))
2860     (when
2861         (cond ((< arg 0)
2862                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2863               ((> arg 0)
2864                (re-search-forward page-delimiter nil 'move arg)))
2865       (goto-char (match-end 0)))
2866     (narrow-to-region
2867      (point)
2868      (if (re-search-forward page-delimiter nil 'move)
2869          (match-beginning 0)
2870        (point)))
2871     (when (and (gnus-visual-p 'page-marker)
2872                (not (= (point-min) 1)))
2873       (save-excursion
2874         (goto-char (point-min))
2875         (gnus-insert-prev-page-button)))
2876     (when (and (gnus-visual-p 'page-marker)
2877                (< (+ (point-max) 2) (buffer-size)))
2878       (save-excursion
2879         (goto-char (point-max))
2880         (gnus-insert-next-page-button)))))
2881
2882 ;; Article mode commands
2883
2884 (defun gnus-article-goto-next-page ()
2885   "Show the next page of the article."
2886   (interactive)
2887   (when (gnus-article-next-page)
2888     (goto-char (point-min))
2889     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2890
2891 (defun gnus-article-goto-prev-page ()
2892   "Show the next page of the article."
2893   (interactive)
2894   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2895     (gnus-article-prev-page nil)))
2896
2897 (defun gnus-article-next-page (&optional lines)
2898   "Show the next page of the current article.
2899 If end of article, return non-nil.  Otherwise return nil.
2900 Argument LINES specifies lines to be scrolled up."
2901   (interactive "p")
2902   (move-to-window-line -1)
2903   (if (save-excursion
2904         (end-of-line)
2905         (and (pos-visible-in-window-p)  ;Not continuation line.
2906              (eobp)))
2907       ;; Nothing in this page.
2908       (if (or (not gnus-page-broken)
2909               (save-excursion
2910                 (save-restriction
2911                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2912           t                             ;Nothing more.
2913         (gnus-narrow-to-page 1)         ;Go to next page.
2914         nil)
2915     ;; More in this page.
2916     (let ((scroll-in-place nil))
2917       (condition-case ()
2918           (scroll-up lines)
2919         (end-of-buffer
2920          ;; Long lines may cause an end-of-buffer error.
2921          (goto-char (point-max)))))
2922     (move-to-window-line 0)
2923     nil))
2924
2925 (defun gnus-article-prev-page (&optional lines)
2926   "Show previous page of current article.
2927 Argument LINES specifies lines to be scrolled down."
2928   (interactive "p")
2929   (move-to-window-line 0)
2930   (if (and gnus-page-broken
2931            (bobp)
2932            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2933       (progn
2934         (gnus-narrow-to-page -1)        ;Go to previous page.
2935         (goto-char (point-max))
2936         (recenter -1))
2937     (let ((scroll-in-place nil))
2938       (prog1
2939           (condition-case ()
2940               (scroll-down lines)
2941             (beginning-of-buffer
2942              (goto-char (point-min))))
2943         (move-to-window-line 0)))))
2944
2945 (defun gnus-article-refer-article ()
2946   "Read article specified by message-id around point."
2947   (interactive)
2948   (let ((point (point)))
2949     (search-forward ">" nil t)          ;Move point to end of "<....>".
2950     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2951         (let ((message-id (match-string 1)))
2952           (goto-char point)
2953           (set-buffer gnus-summary-buffer)
2954           (gnus-summary-refer-article message-id))
2955       (goto-char (point))
2956       (error "No references around point"))))
2957
2958 (defun gnus-article-show-summary ()
2959   "Reconfigure windows to show summary buffer."
2960   (interactive)
2961   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2962       (error "There is no summary buffer for this article buffer")
2963     (gnus-article-set-globals)
2964     (gnus-configure-windows 'article)
2965     (gnus-summary-goto-subject gnus-current-article)
2966     (gnus-summary-position-point)))
2967
2968 (defun gnus-article-describe-briefly ()
2969   "Describe article mode commands briefly."
2970   (interactive)
2971   (gnus-message 6
2972                 (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")))
2973
2974 (defun gnus-article-summary-command ()
2975   "Execute the last keystroke in the summary buffer."
2976   (interactive)
2977   (let ((obuf (current-buffer))
2978         (owin (current-window-configuration))
2979         func)
2980     (switch-to-buffer gnus-article-current-summary 'norecord)
2981     (setq func (lookup-key (current-local-map) (this-command-keys)))
2982     (call-interactively func)
2983     (set-buffer obuf)
2984     (set-window-configuration owin)
2985     (set-window-point (get-buffer-window (current-buffer)) (point))))
2986
2987 (defun gnus-article-summary-command-nosave ()
2988   "Execute the last keystroke in the summary buffer."
2989   (interactive)
2990   (let (func)
2991     (pop-to-buffer gnus-article-current-summary 'norecord)
2992     (setq func (lookup-key (current-local-map) (this-command-keys)))
2993     (call-interactively func)))
2994
2995 (defun gnus-article-check-buffer ()
2996   "Beep if not in an article buffer."
2997   (unless (equal major-mode 'gnus-article-mode)
2998     (error "Command invoked outside of a Gnus article buffer")))
2999
3000 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3001   "Read a summary buffer key sequence and execute it from the article buffer."
3002   (interactive "P")
3003   (gnus-article-check-buffer)
3004   (let ((nosaves
3005          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3006            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3007            "=" "^" "\M-^" "|"))
3008         (nosave-but-article
3009          '("A\r"))
3010         (nosave-in-article
3011          '("\C-d"))
3012         (up-to-top
3013          '("n" "Gn" "p" "Gp"))
3014         keys new-sum-point)
3015     (save-excursion
3016       (set-buffer gnus-article-current-summary)
3017       (let (gnus-pick-mode)
3018         (push (or key last-command-event) unread-command-events)
3019         (setq keys (read-key-sequence nil))))
3020     (message "")
3021
3022     (if (or (member keys nosaves)
3023             (member keys nosave-but-article)
3024             (member keys nosave-in-article))
3025         (let (func)
3026           (save-window-excursion
3027             (pop-to-buffer gnus-article-current-summary 'norecord)
3028             ;; We disable the pick minor mode commands.
3029             (let (gnus-pick-mode)
3030               (setq func (lookup-key (current-local-map) keys))))
3031           (if (not func)
3032               (ding)
3033             (unless (member keys nosave-in-article)
3034               (set-buffer gnus-article-current-summary))
3035             (call-interactively func)
3036             (setq new-sum-point (point)))
3037           (when (member keys nosave-but-article)
3038             (pop-to-buffer gnus-article-buffer 'norecord)))
3039       ;; These commands should restore window configuration.
3040       (let ((obuf (current-buffer))
3041             (owin (current-window-configuration))
3042             (opoint (point))
3043             (summary gnus-article-current-summary)
3044             func in-buffer selected)
3045         (if not-restore-window
3046             (pop-to-buffer summary 'norecord)
3047           (switch-to-buffer summary 'norecord))
3048         (setq in-buffer (current-buffer))
3049         ;; We disable the pick minor mode commands.
3050         (if (setq func (let (gnus-pick-mode)
3051                          (lookup-key (current-local-map) keys)))
3052             (progn
3053               (call-interactively func)
3054               (setq new-sum-point (point)))
3055           (ding))
3056         (when (eq in-buffer (current-buffer))
3057           (setq selected (gnus-summary-select-article))
3058           (set-buffer obuf)
3059           (unless not-restore-window
3060             (set-window-configuration owin))
3061           (unless (or (not (eq selected 'old)) (member keys up-to-top))
3062             (set-window-point (get-buffer-window (current-buffer))
3063                               opoint))
3064           (let ((win (get-buffer-window gnus-article-current-summary)))
3065             (when win
3066               (set-window-point win new-sum-point))))))))
3067
3068 (defun gnus-article-hide (&optional arg force)
3069   "Hide all the gruft in the current article.
3070 This means that PGP stuff, signatures, cited text and (some)
3071 headers will be hidden.
3072 If given a prefix, show the hidden text instead."
3073   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3074   (gnus-article-hide-headers arg)
3075   (gnus-article-hide-pgp arg)
3076   (gnus-article-hide-citation-maybe arg force)
3077   (gnus-article-hide-signature arg))
3078
3079 (defun gnus-article-maybe-highlight ()
3080   "Do some article highlighting if article highlighting is requested."
3081   (when (gnus-visual-p 'article-highlight 'highlight)
3082     (gnus-article-highlight-some)))
3083
3084 (defun gnus-check-group-server ()
3085   ;; Make sure the connection to the server is alive.
3086   (unless (gnus-server-opened
3087            (gnus-find-method-for-group gnus-newsgroup-name))
3088     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3089     (gnus-request-group gnus-newsgroup-name t)))
3090
3091 (defun gnus-request-article-this-buffer (article group)
3092   "Get an article and insert it into this buffer."
3093   (let (do-update-line sparse-header)
3094     (prog1
3095         (save-excursion
3096           (erase-buffer)
3097           (gnus-kill-all-overlays)
3098           (setq group (or group gnus-newsgroup-name))
3099
3100           ;; Using `gnus-request-article' directly will insert the article into
3101           ;; `nntp-server-buffer' - so we'll save some time by not having to
3102           ;; copy it from the server buffer into the article buffer.
3103
3104           ;; We only request an article by message-id when we do not have the
3105           ;; headers for it, so we'll have to get those.
3106           (when (stringp article)
3107             (let ((gnus-override-method gnus-refer-article-method))
3108               (gnus-read-header article)))
3109
3110           ;; If the article number is negative, that means that this article
3111           ;; doesn't belong in this newsgroup (possibly), so we find its
3112           ;; message-id and request it by id instead of number.
3113           (when (and (numberp article)
3114                      gnus-summary-buffer
3115                      (get-buffer gnus-summary-buffer)
3116                      (gnus-buffer-exists-p gnus-summary-buffer))
3117             (save-excursion
3118               (set-buffer gnus-summary-buffer)
3119               (let ((header (gnus-summary-article-header article)))
3120                 (when (< article 0)
3121                   (cond
3122                    ((memq article gnus-newsgroup-sparse)
3123                     ;; This is a sparse gap article.
3124                     (setq do-update-line article)
3125                     (setq article (mail-header-id header))
3126                     (let ((gnus-override-method gnus-refer-article-method))
3127                       (setq sparse-header (gnus-read-header article)))
3128                     (setq gnus-newsgroup-sparse
3129                           (delq article gnus-newsgroup-sparse)))
3130                    ((vectorp header)
3131                     ;; It's a real article.
3132                     (setq article (mail-header-id header)))
3133                    (t
3134                     ;; It is an extracted pseudo-article.
3135                     (setq article 'pseudo)
3136                     (gnus-request-pseudo-article header))))
3137
3138                 (let ((method (gnus-find-method-for-group
3139                                gnus-newsgroup-name)))
3140                   (when (and (eq (car method) 'nneething)
3141                              (vectorp header))
3142                     (let ((dir (concat
3143                                 (file-name-as-directory
3144                                  (or (cadr (assq 'nneething-address method))
3145                                      (nth 1 method)))
3146                                 (mail-header-subject header))))
3147                       (when (file-directory-p dir)
3148                         (setq article 'nneething)
3149                         (gnus-group-enter-directory dir))))))))
3150
3151           (cond
3152            ;; Refuse to select canceled articles.
3153            ((and (numberp article)
3154                  gnus-summary-buffer
3155                  (get-buffer gnus-summary-buffer)
3156                  (gnus-buffer-exists-p gnus-summary-buffer)
3157                  (eq (cdr (save-excursion
3158                             (set-buffer gnus-summary-buffer)
3159                             (assq article gnus-newsgroup-reads)))
3160                      gnus-canceled-mark))
3161             nil)
3162            ;; We first check `gnus-original-article-buffer'.
3163            ((and (get-buffer gnus-original-article-buffer)
3164                  (numberp article)
3165                  (save-excursion
3166                    (set-buffer gnus-original-article-buffer)
3167                    (and (equal (car gnus-original-article) group)
3168                         (eq (cdr gnus-original-article) article))))
3169             (insert-buffer-substring gnus-original-article-buffer)
3170             'article)
3171            ;; Check the backlog.
3172            ((and gnus-keep-backlog
3173                  (gnus-backlog-request-article group article (current-buffer)))
3174             'article)
3175            ;; Check asynchronous pre-fetch.
3176            ((gnus-async-request-fetched-article group article (current-buffer))
3177             (gnus-async-prefetch-next group article gnus-summary-buffer)
3178             (when (and (numberp article) gnus-keep-backlog)
3179               (gnus-backlog-enter-article group article (current-buffer)))
3180             'article)
3181            ;; Check the cache.
3182            ((and gnus-use-cache
3183                  (numberp article)
3184                  (gnus-cache-request-article article group))
3185             'article)
3186            ;; Get the article and put into the article buffer.
3187            ((or (stringp article) (numberp article))
3188             (let ((gnus-override-method
3189                    (and (stringp article) gnus-refer-article-method))
3190                   (buffer-read-only nil))
3191               (erase-buffer)
3192               (gnus-kill-all-overlays)
3193               (gnus-check-group-server)
3194               (when (gnus-request-article article group (current-buffer))
3195                 (when (numberp article)
3196                   (gnus-async-prefetch-next group article gnus-summary-buffer)
3197                   (when gnus-keep-backlog
3198                     (gnus-backlog-enter-article
3199                      group article (current-buffer))))
3200                 'article)))
3201            ;; It was a pseudo.
3202            (t article)))
3203
3204       ;; Associate this article with the current summary buffer.
3205       (setq gnus-article-current-summary gnus-summary-buffer)
3206
3207       ;; Take the article from the original article buffer
3208       ;; and place it in the buffer it's supposed to be in.
3209       (when (and (get-buffer gnus-article-buffer)
3210                  (equal (buffer-name (current-buffer))
3211                         (buffer-name (get-buffer gnus-article-buffer))))
3212         (save-excursion
3213           (if (get-buffer gnus-original-article-buffer)
3214               (set-buffer gnus-original-article-buffer)
3215             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3216             (buffer-disable-undo)
3217             (setq major-mode 'gnus-original-article-mode)
3218             (setq buffer-read-only t))
3219           (let (buffer-read-only)
3220             (erase-buffer)
3221             (insert-buffer-substring gnus-article-buffer))
3222           (setq gnus-original-article (cons group article)))
3223
3224         ;; Decode charsets.
3225         (run-hooks 'gnus-article-decode-hook)
3226         ;; Mark article as decoded or not.
3227         (setq gnus-article-decoded-p gnus-article-decode-hook))
3228
3229       ;; Update sparse articles.
3230       (when (and do-update-line
3231                  (or (numberp article)
3232                      (stringp article)))
3233         (let ((buf (current-buffer)))
3234           (set-buffer gnus-summary-buffer)
3235           (gnus-summary-update-article do-update-line sparse-header)
3236           (gnus-summary-goto-subject do-update-line nil t)
3237           (set-window-point (get-buffer-window (current-buffer) t)
3238                             (point))
3239           (set-buffer buf))))))
3240
3241 ;;;
3242 ;;; Article editing
3243 ;;;
3244
3245 (defcustom gnus-article-edit-mode-hook nil
3246   "Hook run in article edit mode buffers."
3247   :group 'gnus-article-various
3248   :type 'hook)
3249
3250 (defvar gnus-article-edit-done-function nil)
3251
3252 (defvar gnus-article-edit-mode-map nil)
3253
3254 ;; Should we be using derived.el for this?
3255 (unless gnus-article-edit-mode-map
3256   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3257   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3258
3259   (gnus-define-keys gnus-article-edit-mode-map
3260     "\C-c\C-c" gnus-article-edit-done
3261     "\C-c\C-k" gnus-article-edit-exit)
3262
3263   (gnus-define-keys (gnus-article-edit-wash-map
3264                      "\C-c\C-w" gnus-article-edit-mode-map)
3265     "f" gnus-article-edit-full-stops))
3266
3267 (defun gnus-article-edit-mode ()
3268   "Major mode for editing articles.
3269 This is an extended text-mode.
3270
3271 \\{gnus-article-edit-mode-map}"
3272   (interactive)
3273   (setq major-mode 'gnus-article-edit-mode)
3274   (setq mode-name "Article Edit")
3275   (use-local-map gnus-article-edit-mode-map)
3276   (make-local-variable 'gnus-article-edit-done-function)
3277   (make-local-variable 'gnus-prev-winconf)
3278   (setq buffer-read-only nil)
3279   (buffer-enable-undo)
3280   (widen)
3281   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3282
3283 (defun gnus-article-edit (&optional force)
3284   "Edit the current article.
3285 This will have permanent effect only in mail groups.
3286 If FORCE is non-nil, allow editing of articles even in read-only
3287 groups."
3288   (interactive "P")
3289   (when (and (not force)
3290              (gnus-group-read-only-p))
3291     (error "The current newsgroup does not support article editing"))
3292   (gnus-article-date-original)
3293   (gnus-article-edit-article
3294    `(lambda (no-highlight)
3295       (gnus-summary-edit-article-done
3296        ,(or (mail-header-references gnus-current-headers) "")
3297        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3298
3299 (defun gnus-article-edit-article (exit-func)
3300   "Start editing the contents of the current article buffer."
3301   (let ((winconf (current-window-configuration)))
3302     (set-buffer gnus-article-buffer)
3303     (gnus-article-edit-mode)
3304     (gnus-article-delete-text-of-type 'annotation)
3305     (gnus-set-text-properties (point-min) (point-max) nil)
3306     (gnus-configure-windows 'edit-article)
3307     (setq gnus-article-edit-done-function exit-func)
3308     (setq gnus-prev-winconf winconf)
3309     (gnus-message 6 "C-c C-c to end edits")))
3310
3311 (defun gnus-article-edit-done (&optional arg)
3312   "Update the article edits and exit."
3313   (interactive "P")
3314   (save-excursion
3315     (save-restriction
3316       (widen)
3317       (when (article-goto-body)
3318         (let ((lines (count-lines (point) (point-max)))
3319               (length (- (point-max) (point)))
3320               (case-fold-search t)
3321               (body (copy-marker (point))))
3322           (goto-char (point-min))
3323           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3324             (delete-region (match-beginning 1) (match-end 1))
3325             (insert (number-to-string length)))
3326           (goto-char (point-min))
3327           (when (re-search-forward
3328                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3329             (delete-region (match-beginning 1) (match-end 1))
3330             (insert (number-to-string length)))
3331           (goto-char (point-min))
3332           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3333             (delete-region (match-beginning 1) (match-end 1))
3334             (insert (number-to-string lines)))))))
3335   (let ((func gnus-article-edit-done-function)
3336         (buf (current-buffer))
3337         (start (window-start)))
3338     (gnus-article-edit-exit)
3339     (save-excursion
3340       (set-buffer buf)
3341       (let ((buffer-read-only nil))
3342         (funcall func arg))
3343       ;; The cache and backlog have to be flushed somewhat.
3344       (when gnus-keep-backlog
3345         (gnus-backlog-remove-article
3346          (car gnus-article-current) (cdr gnus-article-current)))
3347       ;; Flush original article as well.
3348       (save-excursion
3349         (when (get-buffer gnus-original-article-buffer)
3350           (set-buffer gnus-original-article-buffer)
3351           (setq gnus-original-article nil)))
3352       (when gnus-use-cache
3353         (gnus-cache-update-article
3354          (car gnus-article-current) (cdr gnus-article-current))))
3355     (set-buffer buf)
3356     (set-window-start (get-buffer-window buf) start)
3357     (set-window-point (get-buffer-window buf) (point))))
3358
3359 (defun gnus-article-edit-exit ()
3360   "Exit the article editing without updating."
3361   (interactive)
3362   ;; We remove all text props from the article buffer.
3363   (let ((buf (format "%s" (buffer-string)))
3364         (curbuf (current-buffer))
3365         (p (point))
3366         (window-start (window-start)))
3367     (erase-buffer)
3368     (insert buf)
3369     (let ((winconf gnus-prev-winconf))
3370       (gnus-article-mode)
3371       (set-window-configuration winconf)
3372       ;; Tippy-toe some to make sure that point remains where it was.
3373       (save-current-buffer
3374         (set-buffer curbuf)
3375         (set-window-start (get-buffer-window (current-buffer)) window-start)
3376         (goto-char p)))))
3377
3378 (defun gnus-article-edit-full-stops ()
3379   "Interactively repair spacing at end of sentences."
3380   (interactive)
3381   (save-excursion
3382     (goto-char (point-min))
3383     (search-forward-regexp "^$" nil t)
3384     (let ((case-fold-search nil))
3385       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3386
3387 ;;;
3388 ;;; Article highlights
3389 ;;;
3390
3391 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
3392
3393 ;;; Internal Variables:
3394
3395 (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\\)"
3396   "Regular expression that matches URLs."
3397   :group 'gnus-article-buttons
3398   :type 'regexp)
3399
3400 (defcustom gnus-button-alist
3401   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
3402      0 t gnus-button-message-id 2)
3403     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
3404     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
3405      1 t
3406      gnus-button-fetch-group 4)
3407     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
3408     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
3409      t gnus-button-message-id 3)
3410     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
3411     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
3412     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
3413     ;; This is how URLs _should_ be embedded in text...
3414     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
3415     ;; Raw URLs.
3416     (,gnus-button-url-regexp 0 t gnus-button-url 0))
3417   "*Alist of regexps matching buttons in article bodies.
3418
3419 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
3420 REGEXP: is the string matching text around the button,
3421 BUTTON: is the number of the regexp grouping actually matching the button,
3422 FORM: is a lisp expression which must eval to true for the button to
3423 be added,
3424 CALLBACK: is the function to call when the user push this button, and each
3425 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
3426
3427 CALLBACK can also be a variable, in that case the value of that
3428 variable it the real callback function."
3429   :group 'gnus-article-buttons
3430   :type '(repeat (list regexp
3431                        (integer :tag "Button")
3432                        (sexp :tag "Form")
3433                        (function :tag "Callback")
3434                        (repeat :tag "Par"
3435                                :inline t
3436                                (integer :tag "Regexp group")))))
3437
3438 (defcustom gnus-header-button-alist
3439   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
3440      0 t gnus-button-message-id 0)
3441     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
3442     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
3443      0 t gnus-button-mailto 0)
3444     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3445     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3446     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3447     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
3448      gnus-button-message-id 3))
3449   "*Alist of headers and regexps to match buttons in article heads.
3450
3451 This alist is very similar to `gnus-button-alist', except that each
3452 alist has an additional HEADER element first in each entry:
3453
3454 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
3455
3456 HEADER is a regexp to match a header.  For a fuller explanation, see
3457 `gnus-button-alist'."
3458   :group 'gnus-article-buttons
3459   :group 'gnus-article-headers
3460   :type '(repeat (list (regexp :tag "Header")
3461                        regexp
3462                        (integer :tag "Button")
3463                        (sexp :tag "Form")
3464                        (function :tag "Callback")
3465                        (repeat :tag "Par"
3466                                :inline t
3467                                (integer :tag "Regexp group")))))
3468
3469 (defvar gnus-button-regexp nil)
3470 (defvar gnus-button-marker-list nil)
3471 ;; Regexp matching any of the regexps from `gnus-button-alist'.
3472
3473 (defvar gnus-button-last nil)
3474 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
3475
3476 ;;; Commands:
3477
3478 (defun gnus-article-push-button (event)
3479   "Check text under the mouse pointer for a callback function.
3480 If the text under the mouse pointer has a `gnus-callback' property,
3481 call it with the value of the `gnus-data' text property."
3482   (interactive "e")
3483   (set-buffer (window-buffer (posn-window (event-start event))))
3484   (let* ((pos (posn-point (event-start event)))
3485          (data (get-text-property pos 'gnus-data))
3486          (fun (get-text-property pos 'gnus-callback)))
3487     (goto-char pos)
3488     (when fun
3489       (funcall fun data))))
3490
3491 (defun gnus-article-press-button ()
3492   "Check text at point for a callback function.
3493 If the text at point has a `gnus-callback' property,
3494 call it with the value of the `gnus-data' text property."
3495   (interactive)
3496   (let* ((data (get-text-property (point) 'gnus-data))
3497          (fun (get-text-property (point) 'gnus-callback)))
3498     (when fun
3499       (funcall fun data))))
3500
3501 (defun gnus-article-highlight (&optional force)
3502   "Highlight current article.
3503 This function calls `gnus-article-highlight-headers',
3504 `gnus-article-highlight-citation',
3505 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3506 do the highlighting.  See the documentation for those functions."
3507   (interactive (list 'force))
3508   (gnus-article-highlight-headers)
3509   (gnus-article-highlight-citation force)
3510   (gnus-article-highlight-signature)
3511   (gnus-article-add-buttons force)
3512   (gnus-article-add-buttons-to-head))
3513
3514 (defun gnus-article-highlight-some (&optional force)
3515   "Highlight current article.
3516 This function calls `gnus-article-highlight-headers',
3517 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3518 do the highlighting.  See the documentation for those functions."
3519   (interactive (list 'force))
3520   (gnus-article-highlight-headers)
3521   (gnus-article-highlight-signature)
3522   (gnus-article-add-buttons))
3523
3524 (defun gnus-article-highlight-headers ()
3525   "Highlight article headers as specified by `gnus-header-face-alist'."
3526   (interactive)
3527   (save-excursion
3528     (set-buffer gnus-article-buffer)
3529     (save-restriction
3530       (let ((alist gnus-header-face-alist)
3531             (buffer-read-only nil)
3532             (case-fold-search t)
3533             (inhibit-point-motion-hooks t)
3534             entry regexp header-face field-face from hpoints fpoints)
3535         (message-narrow-to-head)
3536         (while (setq entry (pop alist))
3537           (goto-char (point-min))
3538           (setq regexp (concat "^\\("
3539                                (if (string-equal "" (nth 0 entry))
3540                                    "[^\t ]"
3541                                  (nth 0 entry))
3542                                "\\)")
3543                 header-face (nth 1 entry)
3544                 field-face (nth 2 entry))
3545           (while (and (re-search-forward regexp nil t)
3546                       (not (eobp)))
3547             (beginning-of-line)
3548             (setq from (point))
3549             (unless (search-forward ":" nil t)
3550               (forward-char 1))
3551             (when (and header-face
3552                        (not (memq (point) hpoints)))
3553               (push (point) hpoints)
3554               (gnus-put-text-property from (point) 'face header-face))
3555             (when (and field-face
3556                        (not (memq (setq from (point)) fpoints)))
3557               (push from fpoints)
3558               (if (re-search-forward "^[^ \t]" nil t)
3559                   (forward-char -2)
3560                 (goto-char (point-max)))
3561               (gnus-put-text-property from (point) 'face field-face))))))))
3562
3563 (defun gnus-article-highlight-signature ()
3564   "Highlight the signature in an article.
3565 It does this by highlighting everything after
3566 `gnus-signature-separator' using `gnus-signature-face'."
3567   (interactive)
3568   (save-excursion
3569     (set-buffer gnus-article-buffer)
3570     (let ((buffer-read-only nil)
3571           (inhibit-point-motion-hooks t))
3572       (save-restriction
3573         (when (and gnus-signature-face
3574                    (gnus-article-narrow-to-signature))
3575           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
3576                             'face gnus-signature-face)
3577           (widen)
3578           (gnus-article-search-signature)
3579           (let ((start (match-beginning 0))
3580                 (end (set-marker (make-marker) (1+ (match-end 0)))))
3581             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
3582                                      end)))))))
3583
3584 (defun gnus-button-in-region-p (b e prop)
3585   "Say whether PROP exists in the region."
3586   (text-property-not-all b e prop nil))
3587
3588 (defun gnus-article-add-buttons (&optional force)
3589   "Find external references in the article and make buttons of them.
3590 \"External references\" are things like Message-IDs and URLs, as
3591 specified by `gnus-button-alist'."
3592   (interactive (list 'force))
3593   (save-excursion
3594     (set-buffer gnus-article-buffer)
3595     (let ((buffer-read-only nil)
3596           (inhibit-point-motion-hooks t)
3597           (case-fold-search t)
3598           (alist gnus-button-alist)
3599           beg entry regexp)
3600       ;; Remove all old markers.
3601       (let (marker entry)
3602         (while (setq marker (pop gnus-button-marker-list))
3603           (goto-char marker)
3604           (when (setq entry (gnus-button-entry))
3605             (put-text-property (match-beginning (nth 1 entry))
3606                                (match-end (nth 1 entry))
3607                                'gnus-callback nil))
3608           (set-marker marker nil)))
3609       ;; We skip the headers.
3610       (article-goto-body)
3611       (setq beg (point))
3612       (while (setq entry (pop alist))
3613         (setq regexp (car entry))
3614         (goto-char beg)
3615         (while (re-search-forward regexp nil t)
3616           (let* ((start (and entry (match-beginning (nth 1 entry))))
3617                  (end (and entry (match-end (nth 1 entry))))
3618                  (from (match-beginning 0)))
3619             (when (and (or (eq t (nth 2 entry))
3620                            (eval (nth 2 entry)))
3621                        (not (gnus-button-in-region-p
3622                              start end 'gnus-callback)))
3623               ;; That optional form returned non-nil, so we add the
3624               ;; button.
3625               (gnus-article-add-button
3626                start end 'gnus-button-push
3627                (car (push (set-marker (make-marker) from)
3628                           gnus-button-marker-list))))))))))
3629
3630 ;; Add buttons to the head of an article.
3631 (defun gnus-article-add-buttons-to-head ()
3632   "Add buttons to the head of the article."
3633   (interactive)
3634   (save-excursion
3635     (set-buffer gnus-article-buffer)
3636     (let ((buffer-read-only nil)
3637           (inhibit-point-motion-hooks t)
3638           (case-fold-search t)
3639           (alist gnus-header-button-alist)
3640           entry beg end)
3641       (nnheader-narrow-to-headers)
3642       (while alist
3643         ;; Each alist entry.
3644         (setq entry (car alist)
3645               alist (cdr alist))
3646         (goto-char (point-min))
3647         (while (re-search-forward (car entry) nil t)
3648           ;; Each header matching the entry.
3649           (setq beg (match-beginning 0))
3650           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
3651                              (match-beginning 0))
3652                         (point-max)))
3653           (goto-char beg)
3654           (while (re-search-forward (nth 1 entry) end t)
3655             ;; Each match within a header.
3656             (let* ((entry (cdr entry))
3657                    (start (match-beginning (nth 1 entry)))
3658                    (end (match-end (nth 1 entry)))
3659                    (form (nth 2 entry)))
3660               (goto-char (match-end 0))
3661               (when (eval form)
3662                 (gnus-article-add-button
3663                  start end (nth 3 entry)
3664                  (buffer-substring (match-beginning (nth 4 entry))
3665                                    (match-end (nth 4 entry)))))))
3666           (goto-char end))))
3667     (widen)))
3668
3669 ;;; External functions:
3670
3671 (defun gnus-article-add-button (from to fun &optional data)
3672   "Create a button between FROM and TO with callback FUN and data DATA."
3673   (when gnus-article-button-face
3674     (gnus-overlay-put (gnus-make-overlay from to)
3675                       'face gnus-article-button-face))
3676   (gnus-add-text-properties
3677    from to
3678    (nconc (and gnus-article-mouse-face
3679                (list gnus-mouse-face-prop gnus-article-mouse-face))
3680           (list 'gnus-callback fun)
3681           (and data (list 'gnus-data data))))
3682   (widget-convert-button 'link from to :action 'gnus-widget-press-button
3683                          :button-keymap gnus-widget-button-keymap))
3684
3685 ;;; Internal functions:
3686
3687 (defun gnus-article-set-globals ()
3688   (save-excursion
3689     (set-buffer gnus-summary-buffer)
3690     (gnus-set-global-variables)))
3691
3692 (defun gnus-signature-toggle (end)
3693   (save-excursion
3694     (set-buffer gnus-article-buffer)
3695     (let ((buffer-read-only nil)
3696           (inhibit-point-motion-hooks t))
3697       (if (get-text-property end 'invisible)
3698           (gnus-article-unhide-text end (point-max))
3699         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3700
3701 (defun gnus-button-entry ()
3702   ;; Return the first entry in `gnus-button-alist' matching this place.
3703   (let ((alist gnus-button-alist)
3704         (entry nil))
3705     (while alist
3706       (setq entry (pop alist))
3707       (if (looking-at (car entry))
3708           (setq alist nil)
3709         (setq entry nil)))
3710     entry))
3711
3712 (defun gnus-button-push (marker)
3713   ;; Push button starting at MARKER.
3714   (save-excursion
3715     (goto-char marker)
3716     (let* ((entry (gnus-button-entry))
3717            (inhibit-point-motion-hooks t)
3718            (fun (nth 3 entry))
3719            (args (mapcar (lambda (group)
3720                            (let ((string (match-string group)))
3721                              (gnus-set-text-properties
3722                               0 (length string) nil string)
3723                              string))
3724                          (nthcdr 4 entry))))
3725       (cond
3726        ((fboundp fun)
3727         (apply fun args))
3728        ((and (boundp fun)
3729              (fboundp (symbol-value fun)))
3730         (apply (symbol-value fun) args))
3731        (t
3732         (gnus-message 1 "You must define `%S' to use this button"
3733                       (cons fun args)))))))
3734
3735 (defun gnus-button-message-id (message-id)
3736   "Fetch MESSAGE-ID."
3737   (save-excursion
3738     (set-buffer gnus-summary-buffer)
3739     (gnus-summary-refer-article message-id)))
3740
3741 (defun gnus-button-fetch-group (address)
3742   "Fetch GROUP specified by ADDRESS."
3743   (if (not (string-match "[:/]" address))
3744       ;; This is just a simple group url.
3745       (gnus-group-read-ephemeral-group address gnus-select-method)
3746     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3747                            address))
3748         (error "Can't parse %s" address)
3749       (gnus-group-read-ephemeral-group
3750        (match-string 4 address)
3751        `(nntp ,(match-string 1 address)
3752               (nntp-address ,(match-string 1 address))
3753               (nntp-port-number ,(if (match-end 3)
3754                                      (match-string 3 address)
3755                                    "nntp")))))))
3756
3757 (defun gnus-url-parse-query-string (query &optional downcase)
3758   (let (retval pairs cur key val)
3759     (setq pairs (split-string query "&"))
3760     (while pairs
3761       (setq cur (car pairs)
3762             pairs (cdr pairs))
3763       (if (not (string-match "=" cur))
3764           nil                           ; Grace
3765         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3766               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3767         (if downcase
3768             (setq key (downcase key)))
3769         (setq cur (assoc key retval))
3770         (if cur
3771             (setcdr cur (cons val (cdr cur)))
3772           (setq retval (cons (list key val) retval)))))
3773     retval))
3774
3775 (defun gnus-url-unhex (x)
3776   (if (> x ?9)
3777       (if (>= x ?a)
3778           (+ 10 (- x ?a))
3779         (+ 10 (- x ?A)))
3780     (- x ?0)))
3781
3782 (defun gnus-url-unhex-string (str &optional allow-newlines)
3783   "Remove %XXX embedded spaces, etc in a url.
3784 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3785 decoding of carriage returns and line feeds in the string, which is normally
3786 forbidden in URL encoding."
3787   (setq str (or str ""))
3788   (let ((tmp "")
3789         (case-fold-search t))
3790     (while (string-match "%[0-9a-f][0-9a-f]" str)
3791       (let* ((start (match-beginning 0))
3792              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3793              (code (+ (* 16 ch1)
3794                       (gnus-url-unhex (elt str (+ start 2))))))
3795         (setq tmp (concat
3796                    tmp (substring str 0 start)
3797                    (cond
3798                     (allow-newlines
3799                      (char-to-string code))
3800                     ((or (= code ?\n) (= code ?\r))
3801                      " ")
3802                     (t (char-to-string code))))
3803               str (substring str (match-end 0)))))
3804     (setq tmp (concat tmp str))
3805     tmp))
3806
3807 (defun gnus-url-mailto (url)
3808   ;; Send mail to someone
3809   (when (string-match "mailto:/*\\(.*\\)" url)
3810     (setq url (substring url (match-beginning 1) nil)))
3811   (let (to args subject func)
3812     (if (string-match (regexp-quote "?") url)
3813         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3814               args (gnus-url-parse-query-string
3815                     (substring url (match-end 0) nil) t))
3816       (setq to (gnus-url-unhex-string url)))
3817     (setq args (cons (list "to" to) args)
3818           subject (cdr-safe (assoc "subject" args)))
3819     (message-mail)
3820     (while args
3821       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3822       (if (fboundp func)
3823           (funcall func)
3824         (message-position-on-field (caar args)))
3825       (insert (mapconcat 'identity (cdar args) ", "))
3826       (setq args (cdr args)))
3827     (if subject
3828         (message-goto-body)
3829       (message-goto-subject))))
3830
3831 (defun gnus-button-mailto (address)
3832   ;; Mail to ADDRESS.
3833   (set-buffer (gnus-copy-article-buffer))
3834   (message-reply address))
3835
3836 (defun gnus-button-reply (address)
3837   ;; Reply to ADDRESS.
3838   (message-reply address))
3839
3840 (defun gnus-button-url (address)
3841   "Browse ADDRESS."
3842   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3843   (if (listp browse-url-browser-function)
3844       (browse-url address)
3845     (funcall browse-url-browser-function address)))
3846
3847 (defun gnus-button-embedded-url (address)
3848   "Browse ADDRESS."
3849   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3850   (if (listp browse-url-browser-function)
3851       (browse-url (gnus-strip-whitespace address))
3852     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
3853
3854 ;;; Next/prev buttons in the article buffer.
3855
3856 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3857 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3858
3859 (defvar gnus-prev-page-map nil)
3860 (unless gnus-prev-page-map
3861   (setq gnus-prev-page-map (make-sparse-keymap))
3862   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3863   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3864
3865 (defun gnus-insert-prev-page-button ()
3866   (let ((buffer-read-only nil))
3867     (gnus-eval-format
3868      gnus-prev-page-line-format nil
3869      `(gnus-prev t local-map ,gnus-prev-page-map
3870                  gnus-callback gnus-article-button-prev-page
3871                  article-type annotation))))
3872
3873 (defvar gnus-next-page-map nil)
3874 (unless gnus-next-page-map
3875   (setq gnus-next-page-map (make-keymap))
3876   (suppress-keymap gnus-prev-page-map)
3877   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3878   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3879
3880 (defun gnus-button-next-page ()
3881   "Go to the next page."
3882   (interactive)
3883   (let ((win (selected-window)))
3884     (select-window (get-buffer-window gnus-article-buffer t))
3885     (gnus-article-next-page)
3886     (select-window win)))
3887
3888 (defun gnus-button-prev-page ()
3889   "Go to the prev page."
3890   (interactive)
3891   (let ((win (selected-window)))
3892     (select-window (get-buffer-window gnus-article-buffer t))
3893     (gnus-article-prev-page)
3894     (select-window win)))
3895
3896 (defun gnus-insert-next-page-button ()
3897   (let ((buffer-read-only nil))
3898     (gnus-eval-format gnus-next-page-line-format nil
3899                       `(gnus-next
3900                         t local-map ,gnus-next-page-map
3901                         gnus-callback gnus-article-button-next-page
3902                         article-type annotation))))
3903
3904 (defun gnus-article-button-next-page (arg)
3905   "Go to the next page."
3906   (interactive "P")
3907   (let ((win (selected-window)))
3908     (select-window (get-buffer-window gnus-article-buffer t))
3909     (gnus-article-next-page)
3910     (select-window win)))
3911
3912 (defun gnus-article-button-prev-page (arg)
3913   "Go to the prev page."
3914   (interactive "P")
3915   (let ((win (selected-window)))
3916     (select-window (get-buffer-window gnus-article-buffer t))
3917     (gnus-article-prev-page)
3918     (select-window win)))
3919
3920 (defvar gnus-decode-header-methods
3921   '(mail-decode-encoded-word-region)
3922   "List of methods used to decode headers.
3923
3924 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
3925 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
3926 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
3927 whose names match REGEXP.
3928
3929 For example: 
3930 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
3931  mail-decode-encoded-word-region 
3932  (\"chinese\" . rfc1843-decode-region))
3933 ")
3934
3935 (defvar gnus-decode-header-methods-cache nil)
3936
3937 (defun gnus-multi-decode-header (start end)
3938   "Apply the functions from `gnus-encoded-word-methods' that match."
3939   (unless (and gnus-decode-header-methods-cache
3940                (eq gnus-newsgroup-name 
3941                    (car gnus-decode-header-methods-cache)))
3942     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
3943     (mapc '(lambda (x) 
3944              (if (symbolp x)
3945                  (nconc gnus-decode-header-methods-cache (list x))
3946                (if (and gnus-newsgroup-name 
3947                         (string-match (car x) gnus-newsgroup-name))
3948                    (nconc gnus-decode-header-methods-cache 
3949                           (list (cdr x))))))
3950           gnus-decode-header-methods))
3951   (let ((xlist gnus-decode-header-methods-cache))
3952     (pop xlist)
3953     (save-restriction
3954       (narrow-to-region start end)
3955       (while xlist
3956         (funcall (pop xlist) (point-min) (point-max))))))
3957
3958 (gnus-ems-redefine)
3959
3960 (provide 'gnus-art)
3961
3962 (run-hooks 'gnus-art-load-hook)
3963
3964 ;;; gnus-art.el ends here