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