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