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