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