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