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