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