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