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