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