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