aca0d7250b5b592441272c4f023fcdae522818ee
[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             (setq gnus-article-mime-handles nil))
2658           (buffer-disable-undo)
2659           (setq buffer-read-only t)
2660           (unless (eq major-mode 'gnus-article-mode)
2661             (gnus-article-mode))
2662           (current-buffer))
2663       (save-excursion
2664         (set-buffer (gnus-get-buffer-create name))
2665         (gnus-article-mode)
2666         (make-local-variable 'gnus-summary-buffer)
2667         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2668         (current-buffer)))))
2669
2670 ;; Set article window start at LINE, where LINE is the number of lines
2671 ;; from the head of the article.
2672 (defun gnus-article-set-window-start (&optional line)
2673   (set-window-start
2674    (get-buffer-window gnus-article-buffer t)
2675    (save-excursion
2676      (set-buffer gnus-article-buffer)
2677      (goto-char (point-min))
2678      (if (not line)
2679          (point-min)
2680        (gnus-message 6 "Moved to bookmark")
2681        (search-forward "\n\n" nil t)
2682        (forward-line line)
2683        (point)))))
2684
2685 (defun gnus-article-prepare (article &optional all-headers header)
2686   "Prepare ARTICLE in article mode buffer.
2687 ARTICLE should either be an article number or a Message-ID.
2688 If ARTICLE is an id, HEADER should be the article headers.
2689 If ALL-HEADERS is non-nil, no headers are hidden."
2690   (save-excursion
2691     ;; Make sure we start in a summary buffer.
2692     (unless (eq major-mode 'gnus-summary-mode)
2693       (set-buffer gnus-summary-buffer))
2694     (setq gnus-summary-buffer (current-buffer))
2695     (let* ((gnus-article (if header (mail-header-number header) article))
2696            (summary-buffer (current-buffer))
2697            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2698            (group gnus-newsgroup-name)
2699            result)
2700       (save-excursion
2701         (gnus-article-setup-buffer)
2702         (set-buffer gnus-article-buffer)
2703         ;; Deactivate active regions.
2704         (when (and (boundp 'transient-mark-mode)
2705                    transient-mark-mode)
2706           (setq mark-active nil))
2707         (if (not (setq result (let ((buffer-read-only nil))
2708                                 (gnus-request-article-this-buffer
2709                                  article group))))
2710             ;; There is no such article.
2711             (save-excursion
2712               (when (and (numberp article)
2713                          (not (memq article gnus-newsgroup-sparse)))
2714                 (setq gnus-article-current
2715                       (cons gnus-newsgroup-name article))
2716                 (set-buffer gnus-summary-buffer)
2717                 (setq gnus-current-article article)
2718                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2719                     (progn
2720                       (gnus-summary-set-agent-mark article)
2721                       (message "Message marked for downloading"))
2722                   (gnus-summary-mark-article article gnus-canceled-mark)
2723                   (unless (memq article gnus-newsgroup-sparse)
2724                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2725           (if (or (eq result 'pseudo)
2726                   (eq result 'nneething))
2727               (progn
2728                 (save-excursion
2729                   (set-buffer summary-buffer)
2730                   (push article gnus-newsgroup-history)
2731                   (setq gnus-last-article gnus-current-article
2732                         gnus-current-article 0
2733                         gnus-current-headers nil
2734                         gnus-article-current nil)
2735                   (if (eq result 'nneething)
2736                       (gnus-configure-windows 'summary)
2737                     (gnus-configure-windows 'article))
2738                   (gnus-set-global-variables))
2739                 (let ((gnus-article-mime-handle-alist-1
2740                        gnus-article-mime-handle-alist))
2741                   (gnus-set-mode-line 'article)))
2742             ;; The result from the `request' was an actual article -
2743             ;; or at least some text that is now displayed in the
2744             ;; article buffer.
2745             (when (and (numberp article)
2746                        (not (eq article gnus-current-article)))
2747               ;; Seems like a new article has been selected.
2748               ;; `gnus-current-article' must be an article number.
2749               (save-excursion
2750                 (set-buffer summary-buffer)
2751                 (push article gnus-newsgroup-history)
2752                 (setq gnus-last-article gnus-current-article
2753                       gnus-current-article article
2754                       gnus-current-headers
2755                       (gnus-summary-article-header gnus-current-article)
2756                       gnus-article-current
2757                       (cons gnus-newsgroup-name gnus-current-article))
2758                 (unless (vectorp gnus-current-headers)
2759                   (setq gnus-current-headers nil))
2760                 (gnus-summary-goto-subject gnus-current-article)
2761                 (when (gnus-summary-show-thread)
2762                   ;; If the summary buffer really was folded, the
2763                   ;; previous goto may not actually have gone to
2764                   ;; the right article, but the thread root instead.
2765                   ;; So we go again.
2766                   (gnus-summary-goto-subject gnus-current-article))
2767                 (gnus-run-hooks 'gnus-mark-article-hook)
2768                 (gnus-set-mode-line 'summary)
2769                 (when (gnus-visual-p 'article-highlight 'highlight)
2770                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2771                 ;; Set the global newsgroup variables here.
2772                 (gnus-set-global-variables)
2773                 (setq gnus-have-all-headers
2774                       (or all-headers gnus-show-all-headers))))
2775             (when (or (numberp article)
2776                       (stringp article))
2777               (gnus-article-prepare-display)
2778               ;; Do page break.
2779               (goto-char (point-min))
2780               (setq gnus-page-broken
2781                     (when gnus-break-pages
2782                       (gnus-narrow-to-page)
2783                       t)))
2784             (let ((gnus-article-mime-handle-alist-1
2785                    gnus-article-mime-handle-alist))
2786               (gnus-set-mode-line 'article))
2787             (article-goto-body)
2788             (set-window-point (get-buffer-window (current-buffer)) (point))
2789             (gnus-configure-windows 'article)
2790             t))))))
2791
2792 ;;;###autoload
2793 (defun gnus-article-prepare-display ()
2794   "Make the current buffer look like a nice article."
2795   ;; Hooks for getting information from the article.
2796   ;; This hook must be called before being narrowed.
2797   (let ((gnus-article-buffer (current-buffer))
2798         buffer-read-only)
2799     (unless (eq major-mode 'gnus-article-mode)
2800       (gnus-article-mode))
2801     (setq buffer-read-only nil
2802           gnus-article-wash-types nil)
2803     (gnus-run-hooks 'gnus-tmp-internal-hook)
2804     (when gnus-display-mime-function
2805       (funcall gnus-display-mime-function))
2806     (gnus-run-hooks 'gnus-article-prepare-hook)))
2807
2808 ;;;
2809 ;;; Gnus MIME viewing functions
2810 ;;;
2811
2812 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2813   "The following specs can be used:
2814 %t  The MIME type
2815 %T  MIME type, along with additional info
2816 %n  The `name' parameter
2817 %d  The description, if any
2818 %l  The length of the encoded part
2819 %p  The part identifier number
2820 %e  Dots if the part isn't displayed")
2821
2822 (defvar gnus-mime-button-line-format-alist
2823   '((?t gnus-tmp-type ?s)
2824     (?T gnus-tmp-type-long ?s)
2825     (?n gnus-tmp-name ?s)
2826     (?d gnus-tmp-description ?s)
2827     (?p gnus-tmp-id ?s)
2828     (?l gnus-tmp-length ?d)
2829     (?e gnus-tmp-dots ?s)))
2830
2831 (defvar gnus-mime-button-commands
2832   '((gnus-article-press-button "\r" "Toggle Display")
2833     (gnus-mime-view-part "v" "View Interactively...")
2834     (gnus-mime-view-part-as-type "t" "View As Type...")
2835     (gnus-mime-save-part "o" "Save...")
2836     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2837     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2838     (gnus-mime-internalize-part "E" "View Internally")
2839     (gnus-mime-externalize-part "e" "View Externally")
2840     (gnus-mime-pipe-part "|" "Pipe To Command...")))
2841
2842 (defun gnus-article-mime-part-status ()
2843   (if gnus-article-mime-handle-alist-1
2844       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2845     ""))
2846
2847 (defvar gnus-mime-button-map
2848   (let ((map (make-sparse-keymap)))
2849     (set-keymap-parent map gnus-article-mode-map)
2850     (define-key map gnus-mouse-2 'gnus-article-push-button)
2851     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
2852     (dolist (c gnus-mime-button-commands)
2853       (define-key map (cadr c) (car c)))
2854     map))
2855
2856 (defun gnus-mime-button-menu (event)
2857   "Construct a context-sensitive menu of MIME commands."
2858   (interactive "e")
2859   (save-excursion
2860     (let ((pos (event-start event)))
2861       (set-buffer (window-buffer (posn-window pos)))
2862       (goto-char (posn-point pos))
2863       (gnus-article-check-buffer)
2864       (let ((response (x-popup-menu
2865                        t `("MIME Part"
2866                            ("" ,@(mapcar (lambda (c)
2867                                            (cons (caddr c) (car c)))
2868                                          gnus-mime-button-commands))))))
2869         (if response
2870             (call-interactively response))))))
2871
2872 (defun gnus-mime-view-all-parts (&optional handles)
2873   "View all the MIME parts."
2874   (interactive)
2875   (save-current-buffer
2876     (set-buffer gnus-article-buffer)
2877     (let ((handles (or handles gnus-article-mime-handles))
2878           (mail-parse-charset gnus-newsgroup-charset)
2879           (mail-parse-ignored-charsets 
2880            (save-excursion (set-buffer gnus-summary-buffer)
2881                            gnus-newsgroup-ignored-charsets)))
2882       (if (stringp (car handles))
2883           (gnus-mime-view-all-parts (cdr handles))
2884         (mapcar 'mm-display-part handles)))))
2885
2886 (defun gnus-mime-save-part ()
2887   "Save the MIME part under point."
2888   (interactive)
2889   (gnus-article-check-buffer)
2890   (let ((data (get-text-property (point) 'gnus-data)))
2891     (mm-save-part data)))
2892
2893 (defun gnus-mime-pipe-part ()
2894   "Pipe the MIME part under point to a process."
2895   (interactive)
2896   (gnus-article-check-buffer)
2897   (let ((data (get-text-property (point) 'gnus-data)))
2898     (mm-pipe-part data)))
2899
2900 (defun gnus-mime-view-part ()
2901   "Interactively choose a viewing method for the MIME part under point."
2902   (interactive)
2903   (gnus-article-check-buffer)
2904   (let ((data (get-text-property (point) 'gnus-data)))
2905     (mm-interactively-view-part data)))
2906
2907 (defun gnus-mime-view-part-as-type-internal ()
2908   (gnus-article-check-buffer)
2909   (let* ((name (mail-content-type-get
2910                 (mm-handle-type (get-text-property (point) 'gnus-data))
2911                 'name))
2912          (def-type (and name (mm-default-file-encoding name))))
2913     (and def-type (cons def-type 0))))
2914
2915 (defun gnus-mime-view-part-as-type (mime-type)
2916   "Choose a MIME media type, and view the part as such."
2917   (interactive
2918    (list (completing-read
2919           "View as MIME type: "
2920           (mapcar #'list (mailcap-mime-types))
2921           nil nil
2922           (gnus-mime-view-part-as-type-internal))))
2923   (gnus-article-check-buffer)
2924   (let ((handle (get-text-property (point) 'gnus-data)))
2925     (gnus-mm-display-part
2926      (mm-make-handle (mm-handle-buffer handle)
2927                      (cons mime-type (cdr (mm-handle-type handle)))
2928                      (mm-handle-encoding handle)
2929                      (mm-handle-undisplayer handle)
2930                      (mm-handle-disposition handle)
2931                      (mm-handle-description handle)
2932                      (mm-handle-cache handle)
2933                      (mm-handle-id handle)))))
2934
2935 (defun gnus-mime-copy-part (&optional handle)
2936   "Put the the MIME part under point into a new buffer."
2937   (interactive)
2938   (gnus-article-check-buffer)
2939   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2940          (contents (mm-get-part handle))|
2941          (base (file-name-nondirectory
2942                 (or
2943                  (mail-content-type-get (mm-handle-type handle) 'name)
2944                  (mail-content-type-get (mm-handle-type handle)
2945                                         'filename)
2946                  "*decoded*")))
2947          (buffer (generate-new-buffer base)))
2948     (switch-to-buffer buffer)
2949     (insert contents)
2950     ;; We do it this way to make `normal-mode' set the appropriate mode.
2951     (unwind-protect
2952         (progn
2953           (setq buffer-file-name (expand-file-name base))
2954           (normal-mode))
2955       (setq buffer-file-name nil))
2956     (goto-char (point-min))))
2957
2958 (defun gnus-mime-inline-part (&optional handle)
2959   "Insert the MIME part under point into the current buffer."
2960   (interactive)
2961   (gnus-article-check-buffer)
2962   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2963          contents
2964          (b (point))
2965          buffer-read-only)
2966     (if (mm-handle-undisplayer handle)
2967         (mm-remove-part handle)
2968       (setq contents (mm-get-part handle))
2969       (forward-line 2)
2970       (mm-insert-inline handle contents)
2971       (goto-char b))))
2972
2973 (defun gnus-mime-externalize-part (&optional handle)
2974   "View the MIME part under point with an external viewer."
2975   (interactive)
2976   (gnus-article-check-buffer)
2977   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2978          (mm-user-display-methods nil)
2979          (mm-inlined-types nil)
2980          (mail-parse-charset gnus-newsgroup-charset)
2981          (mail-parse-ignored-charsets 
2982           (save-excursion (set-buffer gnus-summary-buffer)
2983                           gnus-newsgroup-ignored-charsets)))
2984     (if (mm-handle-undisplayer handle)
2985         (mm-remove-part handle)
2986       (mm-display-part handle))))
2987
2988 (defun gnus-mime-internalize-part (&optional handle)
2989   "View the MIME part under point with an internal viewer.
2990 In no internal viewer is available, use an external viewer."
2991   (interactive)
2992   (gnus-article-check-buffer)
2993   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2994          (mm-inlined-types '(".*"))
2995          (mm-inline-large-images t)
2996          (mail-parse-charset gnus-newsgroup-charset)
2997          (mail-parse-ignored-charsets 
2998           (save-excursion (set-buffer gnus-summary-buffer)
2999                           gnus-newsgroup-ignored-charsets)))
3000     (if (mm-handle-undisplayer handle)
3001         (mm-remove-part handle)
3002       (mm-display-part handle))))
3003
3004 (defun gnus-article-part-wrapper (n function)
3005   (save-current-buffer
3006     (set-buffer gnus-article-buffer)
3007     (when (> n (length gnus-article-mime-handle-alist))
3008       (error "No such part"))
3009     (gnus-article-goto-part n)
3010     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3011       (funcall function handle))))
3012
3013 (defun gnus-article-pipe-part (n)
3014   "Pipe MIME part N, which is the numerical prefix."
3015   (interactive "p")
3016   (gnus-article-part-wrapper n 'mm-pipe-part))
3017
3018 (defun gnus-article-save-part (n)
3019   "Save MIME part N, which is the numerical prefix."
3020   (interactive "p")
3021   (gnus-article-part-wrapper n 'mm-save-part))
3022
3023 (defun gnus-article-interactively-view-part (n)
3024   "View MIME part N interactively, which is the numerical prefix."
3025   (interactive "p")
3026   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3027
3028 (defun gnus-article-copy-part (n)
3029   "Copy MIME part N, which is the numerical prefix."
3030   (interactive "p")
3031   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3032
3033 (defun gnus-article-externalize-part (n)
3034   "View MIME part N externally, which is the numerical prefix."
3035   (interactive "p")
3036   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3037
3038 (defun gnus-article-inline-part (n)
3039   "Inline MIME part N, which is the numerical prefix."
3040   (interactive "p")
3041   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3042
3043 (defun gnus-article-mime-match-handle-first (condition)
3044   (if condition
3045       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3046         (while (setq ihandle (pop alist))
3047           (if (and (cond 
3048                     ((functionp condition)
3049                      (funcall condition (cdr ihandle)))
3050                     ((eq condition 'undisplayed) 
3051                      (not (or (mm-handle-undisplayer (cdr ihandle))
3052                               (equal (mm-handle-media-type (cdr ihandle))
3053                                      "multipart/alternative"))))
3054                     ((eq condition 'undisplayed-alternative)
3055                      (not (mm-handle-undisplayer (cdr ihandle))))
3056                     (t t))
3057                    (gnus-article-goto-part (car ihandle))
3058                    (or (not n) (< (car ihandle) n)))
3059               (setq n (car ihandle))))
3060         (or n 1))
3061     1))
3062
3063 (defun gnus-article-view-part (&optional n)
3064   "View MIME part N, which is the numerical prefix."
3065   (interactive "P")
3066   (save-current-buffer
3067     (set-buffer gnus-article-buffer)
3068     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3069                              gnus-article-mime-match-handle-function)))
3070     (when (> n (length gnus-article-mime-handle-alist))
3071       (error "No such part"))
3072     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3073       (when (gnus-article-goto-part n)
3074         (if (equal (car handle) "multipart/alternative")
3075             (gnus-article-press-button)
3076           (when (eq (gnus-mm-display-part handle) 'internal)
3077             (gnus-set-window-start)))))))
3078
3079 (defun gnus-mm-display-part (handle)
3080   "Display HANDLE and fix MIME button."
3081   (let ((id (get-text-property (point) 'gnus-part))
3082         (point (point))
3083         buffer-read-only)
3084     (forward-line 1)
3085     (prog1
3086         (let ((window (selected-window))
3087               (mail-parse-charset gnus-newsgroup-charset)
3088               (mail-parse-ignored-charsets 
3089                (save-excursion (set-buffer gnus-summary-buffer)
3090                                gnus-newsgroup-ignored-charsets)))
3091           (save-excursion
3092             (unwind-protect
3093                 (let ((win (get-buffer-window (current-buffer) t))
3094                       (beg (point)))
3095                   (when win
3096                     (select-window win))
3097                   (goto-char point)
3098                   (forward-line)
3099                   (if (mm-handle-displayed-p handle)
3100                       ;; This will remove the part.
3101                       (mm-display-part handle)
3102                     (save-restriction
3103                       (narrow-to-region (point) (1+ (point)))
3104                       (mm-display-part handle)
3105                       ;; We narrow to the part itself and
3106                       ;; then call the treatment functions.
3107                       (goto-char (point-min))
3108                       (forward-line 1)
3109                       (narrow-to-region (point) (point-max))
3110                       (gnus-treat-article
3111                        nil id
3112                        (1- (length gnus-article-mime-handles))
3113                        (mm-handle-media-type handle)))))
3114               (select-window window))))
3115       (goto-char point)
3116       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3117       (gnus-insert-mime-button
3118        handle id (list (mm-handle-displayed-p handle)))
3119       (goto-char point))))
3120
3121 (defun gnus-article-goto-part (n)
3122   "Go to MIME part N."
3123   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3124     (when point
3125       (goto-char point))))
3126
3127 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3128   (let ((gnus-tmp-name
3129          (or (mail-content-type-get (mm-handle-type handle)
3130                                     'name)
3131              (mail-content-type-get (mm-handle-disposition handle)
3132                                     'filename)
3133              ""))
3134         (gnus-tmp-type (mm-handle-media-type handle))
3135         (gnus-tmp-description
3136          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3137                                               "")))
3138         (gnus-tmp-dots
3139          (if (if displayed (car displayed)
3140                (mm-handle-displayed-p handle))
3141              "" "..."))
3142         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3143                            (buffer-size)))
3144         gnus-tmp-type-long b e)
3145     (when (string-match ".*/" gnus-tmp-name)
3146       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3147     (setq gnus-tmp-type-long (concat gnus-tmp-type
3148                                      (and (not (equal gnus-tmp-name ""))
3149                                           (concat "; " gnus-tmp-name))))
3150     (or (equal gnus-tmp-description "")
3151         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3152     (unless (bolp)
3153       (insert "\n"))
3154     (setq b (point))
3155     (gnus-eval-format
3156      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3157      `(local-map ,gnus-mime-button-map
3158                  keymap ,gnus-mime-button-map
3159                  gnus-callback gnus-mm-display-part
3160                  gnus-part ,gnus-tmp-id
3161                  article-type annotation
3162                  gnus-data ,handle))
3163     (setq e (point))
3164     (widget-convert-button
3165      'link b e
3166      :mime-handle handle
3167      :action 'gnus-widget-press-button
3168      :button-keymap gnus-mime-button-map
3169      :help-echo
3170      (lambda (widget/window &optional overlay pos)
3171        ;; Needed to properly clear the message due to a bug in
3172        ;; wid-edit (XEmacs only).
3173        (if (boundp 'help-echo-owns-message)
3174            (setq help-echo-owns-message t))
3175        (format
3176         "%S: %s the MIME part; %S: more options"
3177         (aref gnus-mouse-2 0)
3178         ;; XEmacs will get a single widget arg; Emacs 21 will get
3179         ;; window, overlay, position.
3180         (if (mm-handle-displayed-p
3181              (if overlay
3182                  (with-current-buffer (overlay-buffer overlay)
3183                    (widget-get (widget-at (overlay-start overlay))
3184                                :mime-handle))
3185                (widget-get widget/window :mime-handle)))
3186             "hide" "show")
3187         (aref gnus-down-mouse-3 0))))))
3188
3189 (defun gnus-widget-press-button (elems el)
3190   (goto-char (widget-get elems :from))
3191   (gnus-article-press-button))
3192
3193 (defvar gnus-displaying-mime nil)
3194
3195 (defun gnus-display-mime (&optional ihandles)
3196   "Display the MIME parts."
3197   (save-excursion
3198     (save-selected-window
3199       (let ((window (get-buffer-window gnus-article-buffer))
3200             (point (point)))
3201         (when window
3202           (select-window window)
3203           ;; We have to do this since selecting the window
3204           ;; may change the point.  So we set the window point.
3205           (set-window-point window point)))
3206       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3207              buffer-read-only handle name type b e display)
3208         (when (and (not ihandles)
3209                    (not gnus-displaying-mime))
3210           ;; Top-level call; we clean up.
3211           (when gnus-article-mime-handles
3212             (mm-destroy-parts gnus-article-mime-handles)
3213             (setq gnus-article-mime-handle-alist nil));; A trick.
3214           (setq gnus-article-mime-handles handles)
3215           ;; We allow users to glean info from the handles.
3216           (when gnus-article-mime-part-function
3217             (gnus-mime-part-function handles)))
3218         (if (and handles
3219                  (or (not (stringp (car handles)))
3220                      (cdr handles)))
3221             (progn
3222               (when (and (not ihandles)
3223                          (not gnus-displaying-mime))
3224                 ;; Clean up for mime parts.
3225                 (article-goto-body)
3226                 (delete-region (point) (point-max)))
3227               (let ((gnus-displaying-mime t))
3228                 (gnus-mime-display-part handles)))
3229           (save-restriction
3230             (article-goto-body)
3231             (narrow-to-region (point) (point-max))
3232             (gnus-treat-article nil 1 1)
3233             (widen)))
3234         (unless ihandles
3235           ;; Highlight the headers.
3236           (save-excursion
3237             (save-restriction
3238               (article-goto-body)
3239               (narrow-to-region (point-min) (point))
3240               (gnus-treat-article 'head))))))))
3241
3242 (defvar gnus-mime-display-multipart-as-mixed nil)
3243
3244 (defun gnus-mime-display-part (handle)
3245   (cond
3246    ;; Single part.
3247    ((not (stringp (car handle)))
3248     (gnus-mime-display-single handle))
3249    ;; User-defined multipart
3250    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3251     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3252              handle))
3253    ;; multipart/alternative
3254    ((and (equal (car handle) "multipart/alternative")
3255          (not gnus-mime-display-multipart-as-mixed))
3256     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3257       (push (cons id handle) gnus-article-mime-handle-alist)
3258       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3259    ;; multipart/related
3260    ((and (equal (car handle) "multipart/related")
3261          (not gnus-mime-display-multipart-as-mixed))
3262     ;;;!!!We should find the start part, but we just default
3263     ;;;!!!to the first part.
3264     (gnus-mime-display-part (cadr handle)))
3265    ;; Other multiparts are handled like multipart/mixed.
3266    (t
3267     (gnus-mime-display-mixed (cdr handle)))))
3268
3269 (defun gnus-mime-part-function (handles)
3270   (if (stringp (car handles))
3271       (mapcar 'gnus-mime-part-function (cdr handles))
3272     (funcall gnus-article-mime-part-function handles)))
3273
3274 (defun gnus-mime-display-mixed (handles)
3275   (mapcar 'gnus-mime-display-part handles))
3276
3277 (defun gnus-mime-display-single (handle)
3278   (let ((type (mm-handle-media-type handle))
3279         (ignored gnus-ignored-mime-types)
3280         (not-attachment t)
3281         (move nil)
3282         display text)
3283     (catch 'ignored
3284       (progn
3285         (while ignored
3286           (when (string-match (pop ignored) type)
3287             (throw 'ignored nil)))
3288         (if (and (setq not-attachment
3289                        (and (not (mm-inline-override-p handle))
3290                             (or (not (mm-handle-disposition handle))
3291                                 (equal (car (mm-handle-disposition handle))
3292                                        "inline")
3293                                 (mm-attachment-override-p handle))))
3294                  (mm-automatic-display-p handle)
3295                  (or (mm-inlined-p handle)
3296                      (mm-automatic-external-display-p type)))
3297             (setq display t)
3298           (when (equal (mm-handle-media-supertype handle) "text")
3299             (setq text t)))
3300         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3301           (push (cons id handle) gnus-article-mime-handle-alist)
3302           (when (or (not display)
3303                     (not (gnus-unbuttonized-mime-type-p type)))
3304             ;(gnus-article-insert-newline)
3305             (gnus-insert-mime-button
3306              handle id (list (or display (and not-attachment text))))
3307             (gnus-article-insert-newline)
3308             ;(gnus-article-insert-newline)
3309             (setq move t)))
3310         (let ((beg (point)))
3311           (cond
3312            (display
3313             (when move
3314               (forward-line -2)
3315               (setq beg (point)))
3316             (let ((mail-parse-charset gnus-newsgroup-charset)
3317                   (mail-parse-ignored-charsets 
3318                    (save-excursion (condition-case ()
3319                                        (set-buffer gnus-summary-buffer)
3320                                      (error))
3321                                    gnus-newsgroup-ignored-charsets)))
3322               (mm-display-part handle t))
3323             (goto-char (point-max)))
3324            ((and text not-attachment)
3325             (when move
3326               (forward-line -2)
3327               (setq beg (point)))
3328             (gnus-article-insert-newline)
3329             (mm-insert-inline handle (mm-get-part handle))
3330             (goto-char (point-max))))
3331           ;; Do highlighting.
3332           (save-excursion
3333             (save-restriction
3334               (narrow-to-region beg (point))
3335               (gnus-treat-article
3336                nil (length gnus-article-mime-handle-alist)
3337                (1- (length gnus-article-mime-handles))
3338                (mm-handle-media-type handle)))))))))
3339
3340 (defun gnus-unbuttonized-mime-type-p (type)
3341   "Say whether TYPE is to be unbuttonized."
3342   (unless gnus-inhibit-mime-unbuttonizing
3343     (catch 'found
3344       (let ((types gnus-unbuttonized-mime-types))
3345         (while types
3346           (when (string-match (pop types) type)
3347             (throw 'found t)))))))
3348
3349 (defun gnus-article-insert-newline ()
3350   "Insert a newline, but mark it as undeletable."
3351   (gnus-put-text-property
3352    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3353
3354 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3355   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3356          (ihandles handles)
3357          (point (point))
3358          handle buffer-read-only from props begend not-pref)
3359     (save-window-excursion
3360       (save-restriction
3361         (when ibegend
3362           (narrow-to-region (car ibegend)
3363                             (or (cdr ibegend)
3364                                 (progn
3365                                   (goto-char (car ibegend))
3366                                   (forward-line 2)
3367                                   (point))))
3368           (delete-region (point-min) (point-max))
3369           (mm-remove-parts handles))
3370         (setq begend (list (point-marker)))
3371         ;; Do the toggle.
3372         (unless (setq not-pref (cadr (member preferred ihandles)))
3373           (setq not-pref (car ihandles)))
3374         (when (or ibegend
3375                   (not (gnus-unbuttonized-mime-type-p
3376                         "multipart/alternative")))
3377           (gnus-add-text-properties
3378            (setq from (point))
3379            (progn
3380              (insert (format "%d.  " id))
3381              (point))
3382            `(gnus-callback
3383              (lambda (handles)
3384                (unless ,(not ibegend)
3385                  (setq gnus-article-mime-handle-alist
3386                        ',gnus-article-mime-handle-alist))
3387                (gnus-mime-display-alternative
3388                 ',ihandles ',not-pref ',begend ,id))
3389              local-map ,gnus-mime-button-map
3390              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3391              face ,gnus-article-button-face
3392              keymap ,gnus-mime-button-map
3393              gnus-part ,id
3394              gnus-data ,handle))
3395           (widget-convert-button 'link from (point)
3396                                  :action 'gnus-widget-press-button
3397                                  :button-keymap gnus-widget-button-keymap)
3398           ;; Do the handles
3399           (while (setq handle (pop handles))
3400             (gnus-add-text-properties
3401              (setq from (point))
3402              (progn
3403                (insert (format "(%c) %-18s"
3404                                (if (equal handle preferred) ?* ? )
3405                                (mm-handle-media-type handle)))
3406                (point))
3407              `(gnus-callback
3408                (lambda (handles)
3409                  (unless ,(not ibegend)
3410                    (setq gnus-article-mime-handle-alist
3411                          ',gnus-article-mime-handle-alist))
3412                  (gnus-mime-display-alternative
3413                   ',ihandles ',handle ',begend ,id))
3414                local-map ,gnus-mime-button-map
3415                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3416                face ,gnus-article-button-face
3417                keymap ,gnus-mime-button-map
3418                gnus-part ,id
3419                gnus-data ,handle))
3420             (widget-convert-button 'link from (point)
3421                                    :action 'gnus-widget-press-button
3422                                    :button-keymap gnus-widget-button-keymap)
3423             (insert "  "))
3424           (insert "\n\n"))
3425         (when preferred
3426           (if (stringp (car preferred))
3427               (gnus-display-mime preferred)
3428             (let ((mail-parse-charset gnus-newsgroup-charset)
3429                   (mail-parse-ignored-charsets 
3430                    (save-excursion (set-buffer gnus-summary-buffer)
3431                                    gnus-newsgroup-ignored-charsets)))
3432               (mm-display-part preferred)
3433               ;; Do highlighting.
3434               (save-excursion
3435                 (save-restriction
3436                   (narrow-to-region (car begend) (point-max))
3437                   (gnus-treat-article
3438                    nil (length gnus-article-mime-handle-alist)
3439                    (1- (length gnus-article-mime-handles))
3440                    (mm-handle-media-type handle))))))
3441           (goto-char (point-max))
3442           (setcdr begend (point-marker)))))
3443     (when ibegend
3444       (goto-char point))))
3445
3446 (defun gnus-article-wash-status ()
3447   "Return a string which display status of article washing."
3448   (save-excursion
3449     (set-buffer gnus-article-buffer)
3450     (let ((cite (memq 'cite gnus-article-wash-types))
3451           (headers (memq 'headers gnus-article-wash-types))
3452           (boring (memq 'boring-headers gnus-article-wash-types))
3453           (pgp (memq 'pgp gnus-article-wash-types))
3454           (pem (memq 'pem gnus-article-wash-types))
3455           (signature (memq 'signature gnus-article-wash-types))
3456           (overstrike (memq 'overstrike gnus-article-wash-types))
3457           (emphasis (memq 'emphasis gnus-article-wash-types)))
3458       (format "%c%c%c%c%c%c"
3459               (if cite ?c ? )
3460               (if (or headers boring) ?h ? )
3461               (if (or pgp pem) ?p ? )
3462               (if signature ?s ? )
3463               (if overstrike ?o ? )
3464               (if emphasis ?e ? )))))
3465
3466 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3467
3468 (defun gnus-article-maybe-hide-headers ()
3469   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3470 Provided for backwards compatibility."
3471   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3472                  (not (save-excursion (set-buffer gnus-summary-buffer)
3473                                       gnus-have-all-headers)))
3474              (not gnus-inhibit-hiding))
3475     (gnus-article-hide-headers)))
3476
3477 ;;; Article savers.
3478
3479 (defun gnus-output-to-file (file-name)
3480   "Append the current article to a file named FILE-NAME."
3481   (let ((artbuf (current-buffer)))
3482     (with-temp-buffer
3483       (insert-buffer-substring artbuf)
3484       ;; Append newline at end of the buffer as separator, and then
3485       ;; save it to file.
3486       (goto-char (point-max))
3487       (insert "\n")
3488       (mm-append-to-file (point-min) (point-max) file-name)
3489       t)))
3490
3491 (defun gnus-narrow-to-page (&optional arg)
3492   "Narrow the article buffer to a page.
3493 If given a numerical ARG, move forward ARG pages."
3494   (interactive "P")
3495   (setq arg (if arg (prefix-numeric-value arg) 0))
3496   (save-excursion
3497     (set-buffer gnus-article-buffer)
3498     (goto-char (point-min))
3499     (widen)
3500     ;; Remove any old next/prev buttons.
3501     (when (gnus-visual-p 'page-marker)
3502       (let ((buffer-read-only nil))
3503         (gnus-remove-text-with-property 'gnus-prev)
3504         (gnus-remove-text-with-property 'gnus-next)))
3505     (when
3506         (cond ((< arg 0)
3507                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3508               ((> arg 0)
3509                (re-search-forward page-delimiter nil 'move arg)))
3510       (goto-char (match-end 0)))
3511     (narrow-to-region
3512      (point)
3513      (if (re-search-forward page-delimiter nil 'move)
3514          (match-beginning 0)
3515        (point)))
3516     (when (and (gnus-visual-p 'page-marker)
3517                (not (= (point-min) 1)))
3518       (save-excursion
3519         (goto-char (point-min))
3520         (gnus-insert-prev-page-button)))
3521     (when (and (gnus-visual-p 'page-marker)
3522                (< (+ (point-max) 2) (buffer-size)))
3523       (save-excursion
3524         (goto-char (point-max))
3525         (gnus-insert-next-page-button)))))
3526
3527 ;; Article mode commands
3528
3529 (defun gnus-article-goto-next-page ()
3530   "Show the next page of the article."
3531   (interactive)
3532   (when (gnus-article-next-page)
3533     (goto-char (point-min))
3534     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3535
3536 (defun gnus-article-goto-prev-page ()
3537   "Show the next page of the article."
3538   (interactive)
3539   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3540     (gnus-article-prev-page nil)))
3541
3542 (defun gnus-article-next-page (&optional lines)
3543   "Show the next page of the current article.
3544 If end of article, return non-nil.  Otherwise return nil.
3545 Argument LINES specifies lines to be scrolled up."
3546   (interactive "p")
3547   (move-to-window-line -1)
3548   (if (save-excursion
3549         (end-of-line)
3550         (and (pos-visible-in-window-p)  ;Not continuation line.
3551              (eobp)))
3552       ;; Nothing in this page.
3553       (if (or (not gnus-page-broken)
3554               (save-excursion
3555                 (save-restriction
3556                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3557           t                             ;Nothing more.
3558         (gnus-narrow-to-page 1)         ;Go to next page.
3559         nil)
3560     ;; More in this page.
3561     (let ((scroll-in-place nil))
3562       (condition-case ()
3563           (scroll-up lines)
3564         (end-of-buffer
3565          ;; Long lines may cause an end-of-buffer error.
3566          (goto-char (point-max)))))
3567     (move-to-window-line 0)
3568     nil))
3569
3570 (defun gnus-article-prev-page (&optional lines)
3571   "Show previous page of current article.
3572 Argument LINES specifies lines to be scrolled down."
3573   (interactive "p")
3574   (move-to-window-line 0)
3575   (if (and gnus-page-broken
3576            (bobp)
3577            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3578       (progn
3579         (gnus-narrow-to-page -1)        ;Go to previous page.
3580         (goto-char (point-max))
3581         (recenter -1))
3582     (let ((scroll-in-place nil))
3583       (prog1
3584           (condition-case ()
3585               (scroll-down lines)
3586             (beginning-of-buffer
3587              (goto-char (point-min))))
3588         (move-to-window-line 0)))))
3589
3590 (defun gnus-article-refer-article ()
3591   "Read article specified by message-id around point."
3592   (interactive)
3593   (let ((point (point)))
3594     (search-forward ">" nil t)          ;Move point to end of "<....>".
3595     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3596         (let ((message-id (match-string 1)))
3597           (goto-char point)
3598           (set-buffer gnus-summary-buffer)
3599           (gnus-summary-refer-article message-id))
3600       (goto-char (point))
3601       (error "No references around point"))))
3602
3603 (defun gnus-article-show-summary ()
3604   "Reconfigure windows to show summary buffer."
3605   (interactive)
3606   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3607       (error "There is no summary buffer for this article buffer")
3608     (gnus-article-set-globals)
3609     (gnus-configure-windows 'article)
3610     (gnus-summary-goto-subject gnus-current-article)
3611     (gnus-summary-position-point)))
3612
3613 (defun gnus-article-describe-briefly ()
3614   "Describe article mode commands briefly."
3615   (interactive)
3616   (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")))
3617
3618 (defun gnus-article-summary-command ()
3619   "Execute the last keystroke in the summary buffer."
3620   (interactive)
3621   (let ((obuf (current-buffer))
3622         (owin (current-window-configuration))
3623         func)
3624     (switch-to-buffer gnus-article-current-summary 'norecord)
3625     (setq func (lookup-key (current-local-map) (this-command-keys)))
3626     (call-interactively func)
3627     (set-buffer obuf)
3628     (set-window-configuration owin)
3629     (set-window-point (get-buffer-window (current-buffer)) (point))))
3630
3631 (defun gnus-article-summary-command-nosave ()
3632   "Execute the last keystroke in the summary buffer."
3633   (interactive)
3634   (let (func)
3635     (pop-to-buffer gnus-article-current-summary 'norecord)
3636     (setq func (lookup-key (current-local-map) (this-command-keys)))
3637     (call-interactively func)))
3638
3639 (defun gnus-article-check-buffer ()
3640   "Beep if not in an article buffer."
3641   (unless (equal major-mode 'gnus-article-mode)
3642     (error "Command invoked outside of a Gnus article buffer")))
3643
3644 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3645   "Read a summary buffer key sequence and execute it from the article buffer."
3646   (interactive "P")
3647   (gnus-article-check-buffer)
3648   (let ((nosaves
3649          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3650            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3651            "=" "^" "\M-^" "|"))
3652         (nosave-but-article
3653          '("A\r"))
3654         (nosave-in-article
3655          '("\C-d"))
3656         (up-to-top
3657          '("n" "Gn" "p" "Gp"))
3658         keys new-sum-point)
3659     (save-excursion
3660       (set-buffer gnus-article-current-summary)
3661       (let (gnus-pick-mode)
3662         (push (or key last-command-event) unread-command-events)
3663         (setq keys (if gnus-xemacs
3664                        (events-to-keys (read-key-sequence nil))
3665                      (read-key-sequence nil)))))
3666                      
3667     (message "")
3668
3669     (if (or (member keys nosaves)
3670             (member keys nosave-but-article)
3671             (member keys nosave-in-article))
3672         (let (func)
3673           (save-window-excursion
3674             (pop-to-buffer gnus-article-current-summary 'norecord)
3675             ;; We disable the pick minor mode commands.
3676             (let (gnus-pick-mode)
3677               (setq func (lookup-key (current-local-map) keys))))
3678           (if (or (not func)
3679                   (numberp func))
3680               (ding)
3681             (unless (member keys nosave-in-article)
3682               (set-buffer gnus-article-current-summary))
3683             (call-interactively func)
3684             (setq new-sum-point (point)))
3685           (when (member keys nosave-but-article)
3686             (pop-to-buffer gnus-article-buffer 'norecord)))
3687       ;; These commands should restore window configuration.
3688       (let ((obuf (current-buffer))
3689             (owin (current-window-configuration))
3690             (opoint (point))
3691             (summary gnus-article-current-summary)
3692             func in-buffer selected)
3693         (if not-restore-window
3694             (pop-to-buffer summary 'norecord)
3695           (switch-to-buffer summary 'norecord))
3696         (setq in-buffer (current-buffer))
3697         ;; We disable the pick minor mode commands.
3698         (if (setq func (let (gnus-pick-mode)
3699                          (lookup-key (current-local-map) keys)))
3700             (progn
3701               (call-interactively func)
3702               (setq new-sum-point (point)))
3703           (ding))
3704         (when (eq in-buffer (current-buffer))
3705           (setq selected (gnus-summary-select-article))
3706           (set-buffer obuf)
3707           (unless not-restore-window
3708             (set-window-configuration owin))
3709           (when (eq selected 'old)
3710             (article-goto-body)
3711             (set-window-start (get-buffer-window (current-buffer))
3712                               1)
3713             (set-window-point (get-buffer-window (current-buffer))
3714                               (point)))
3715           (let ((win (get-buffer-window gnus-article-current-summary)))
3716             (when win
3717               (set-window-point win new-sum-point))))))))
3718
3719 (defun gnus-article-hide (&optional arg force)
3720   "Hide all the gruft in the current article.
3721 This means that PGP stuff, signatures, cited text and (some)
3722 headers will be hidden.
3723 If given a prefix, show the hidden text instead."
3724   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3725   (gnus-article-hide-headers arg)
3726   (gnus-article-hide-list-identifiers arg)
3727   (gnus-article-hide-pgp arg)
3728   (gnus-article-hide-citation-maybe arg force)
3729   (gnus-article-hide-signature arg))
3730
3731 (defun gnus-article-maybe-highlight ()
3732   "Do some article highlighting if article highlighting is requested."
3733   (when (gnus-visual-p 'article-highlight 'highlight)
3734     (gnus-article-highlight-some)))
3735
3736 (defun gnus-check-group-server ()
3737   ;; Make sure the connection to the server is alive.
3738   (unless (gnus-server-opened
3739            (gnus-find-method-for-group gnus-newsgroup-name))
3740     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3741     (gnus-request-group gnus-newsgroup-name t)))
3742
3743 (defun gnus-request-article-this-buffer (article group)
3744   "Get an article and insert it into this buffer."
3745   (let (do-update-line sparse-header)
3746     (prog1
3747         (save-excursion
3748           (erase-buffer)
3749           (gnus-kill-all-overlays)
3750           (setq group (or group gnus-newsgroup-name))
3751
3752           ;; Using `gnus-request-article' directly will insert the article into
3753           ;; `nntp-server-buffer' - so we'll save some time by not having to
3754           ;; copy it from the server buffer into the article buffer.
3755
3756           ;; We only request an article by message-id when we do not have the
3757           ;; headers for it, so we'll have to get those.
3758           (when (stringp article)
3759             (gnus-read-header article))
3760
3761           ;; If the article number is negative, that means that this article
3762           ;; doesn't belong in this newsgroup (possibly), so we find its
3763           ;; message-id and request it by id instead of number.
3764           (when (and (numberp article)
3765                      gnus-summary-buffer
3766                      (get-buffer gnus-summary-buffer)
3767                      (gnus-buffer-exists-p gnus-summary-buffer))
3768             (save-excursion
3769               (set-buffer gnus-summary-buffer)
3770               (let ((header (gnus-summary-article-header article)))
3771                 (when (< article 0)
3772                   (cond
3773                    ((memq article gnus-newsgroup-sparse)
3774                     ;; This is a sparse gap article.
3775                     (setq do-update-line article)
3776                     (setq article (mail-header-id header))
3777                     (setq sparse-header (gnus-read-header article))
3778                     (setq gnus-newsgroup-sparse
3779                           (delq article gnus-newsgroup-sparse)))
3780                    ((vectorp header)
3781                     ;; It's a real article.
3782                     (setq article (mail-header-id header)))
3783                    (t
3784                     ;; It is an extracted pseudo-article.
3785                     (setq article 'pseudo)
3786                     (gnus-request-pseudo-article header))))
3787
3788                 (let ((method (gnus-find-method-for-group
3789                                gnus-newsgroup-name)))
3790                   (when (and (eq (car method) 'nneething)
3791                              (vectorp header))
3792                     (let ((dir (expand-file-name
3793                                 (mail-header-subject header)
3794                                 (file-name-as-directory
3795                                  (or (cadr (assq 'nneething-address method))
3796                                      (nth 1 method))))))
3797                       (when (file-directory-p dir)
3798                         (setq article 'nneething)
3799                         (gnus-group-enter-directory dir))))))))
3800
3801           (cond
3802            ;; Refuse to select canceled articles.
3803            ((and (numberp article)
3804                  gnus-summary-buffer
3805                  (get-buffer gnus-summary-buffer)
3806                  (gnus-buffer-exists-p gnus-summary-buffer)
3807                  (eq (cdr (save-excursion
3808                             (set-buffer gnus-summary-buffer)
3809                             (assq article gnus-newsgroup-reads)))
3810                      gnus-canceled-mark))
3811             nil)
3812            ;; We first check `gnus-original-article-buffer'.
3813            ((and (get-buffer gnus-original-article-buffer)
3814                  (numberp article)
3815                  (save-excursion
3816                    (set-buffer gnus-original-article-buffer)
3817                    (and (equal (car gnus-original-article) group)
3818                         (eq (cdr gnus-original-article) article))))
3819             (insert-buffer-substring gnus-original-article-buffer)
3820             'article)
3821            ;; Check the backlog.
3822            ((and gnus-keep-backlog
3823                  (gnus-backlog-request-article group article (current-buffer)))
3824             'article)
3825            ;; Check asynchronous pre-fetch.
3826            ((gnus-async-request-fetched-article group article (current-buffer))
3827             (gnus-async-prefetch-next group article gnus-summary-buffer)
3828             (when (and (numberp article) gnus-keep-backlog)
3829               (gnus-backlog-enter-article group article (current-buffer)))
3830             'article)
3831            ;; Check the cache.
3832            ((and gnus-use-cache
3833                  (numberp article)
3834                  (gnus-cache-request-article article group))
3835             'article)
3836            ;; Get the article and put into the article buffer.
3837            ((or (stringp article)
3838                 (numberp article))
3839             (let ((gnus-override-method gnus-override-method)
3840                   (methods (and (stringp article) 
3841                                 gnus-refer-article-method))
3842                   result
3843                   (buffer-read-only nil))
3844               (setq methods
3845                     (if (listp methods)
3846                         methods
3847                       (list methods)))
3848               (when (and (null gnus-override-method)
3849                          methods)
3850                 (setq gnus-override-method (pop methods)))
3851               (while (not result)
3852                 (when (eq gnus-override-method 'current)
3853                   (setq gnus-override-method gnus-current-select-method))
3854                 (erase-buffer)
3855                 (gnus-kill-all-overlays)
3856                 (let ((gnus-newsgroup-name group))
3857                   (gnus-check-group-server))
3858                 (when (gnus-request-article article group (current-buffer))
3859                   (when (numberp article)
3860                     (gnus-async-prefetch-next group article 
3861                                               gnus-summary-buffer)
3862                     (when gnus-keep-backlog
3863                       (gnus-backlog-enter-article
3864                        group article (current-buffer))))
3865                   (setq result 'article))
3866                 (if (not result)
3867                     (if methods
3868                         (setq gnus-override-method (pop methods))
3869                       (setq result 'done))))
3870               (and (eq result 'article) 'article)))
3871            ;; It was a pseudo.
3872            (t article)))
3873
3874       ;; Associate this article with the current summary buffer.
3875       (setq gnus-article-current-summary gnus-summary-buffer)
3876
3877       ;; Take the article from the original article buffer
3878       ;; and place it in the buffer it's supposed to be in.
3879       (when (and (get-buffer gnus-article-buffer)
3880                  (equal (buffer-name (current-buffer))
3881                         (buffer-name (get-buffer gnus-article-buffer))))
3882         (save-excursion
3883           (if (get-buffer gnus-original-article-buffer)
3884               (set-buffer gnus-original-article-buffer)
3885             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3886             (buffer-disable-undo)
3887             (setq major-mode 'gnus-original-article-mode)
3888             (setq buffer-read-only t))
3889           (let (buffer-read-only)
3890             (erase-buffer)
3891             (insert-buffer-substring gnus-article-buffer))
3892           (setq gnus-original-article (cons group article)))
3893
3894         ;; Decode charsets.
3895         (run-hooks 'gnus-article-decode-hook)
3896         ;; Mark article as decoded or not.
3897         (setq gnus-article-decoded-p gnus-article-decode-hook))
3898
3899       ;; Update sparse articles.
3900       (when (and do-update-line
3901                  (or (numberp article)
3902                      (stringp article)))
3903         (let ((buf (current-buffer)))
3904           (set-buffer gnus-summary-buffer)
3905           (gnus-summary-update-article do-update-line sparse-header)
3906           (gnus-summary-goto-subject do-update-line nil t)
3907           (set-window-point (get-buffer-window (current-buffer) t)
3908                             (point))
3909           (set-buffer buf))))))
3910
3911 ;;;
3912 ;;; Article editing
3913 ;;;
3914
3915 (defcustom gnus-article-edit-mode-hook nil
3916   "Hook run in article edit mode buffers."
3917   :group 'gnus-article-various
3918   :type 'hook)
3919
3920 (defvar gnus-article-edit-done-function nil)
3921
3922 (defvar gnus-article-edit-mode-map nil)
3923
3924 ;; Should we be using derived.el for this?
3925 (unless gnus-article-edit-mode-map
3926   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3927   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3928
3929   (gnus-define-keys gnus-article-edit-mode-map
3930     "\C-c\C-c" gnus-article-edit-done
3931     "\C-c\C-k" gnus-article-edit-exit)
3932
3933   (gnus-define-keys (gnus-article-edit-wash-map
3934                      "\C-c\C-w" gnus-article-edit-mode-map)
3935     "f" gnus-article-edit-full-stops))
3936
3937 (defun gnus-article-edit-mode ()
3938   "Major mode for editing articles.
3939 This is an extended text-mode.
3940
3941 \\{gnus-article-edit-mode-map}"
3942   (interactive)
3943   (setq major-mode 'gnus-article-edit-mode)
3944   (setq mode-name "Article Edit")
3945   (use-local-map gnus-article-edit-mode-map)
3946   (make-local-variable 'gnus-article-edit-done-function)
3947   (make-local-variable 'gnus-prev-winconf)
3948   (setq buffer-read-only nil)
3949   (buffer-enable-undo)
3950   (widen)
3951   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3952
3953 (defun gnus-article-edit (&optional force)
3954   "Edit the current article.
3955 This will have permanent effect only in mail groups.
3956 If FORCE is non-nil, allow editing of articles even in read-only
3957 groups."
3958   (interactive "P")
3959   (when (and (not force)
3960              (gnus-group-read-only-p))
3961     (error "The current newsgroup does not support article editing"))
3962   (gnus-article-date-original)
3963   (gnus-article-edit-article
3964    'ignore
3965    `(lambda (no-highlight)
3966       'ignore
3967       (gnus-summary-edit-article-done
3968        ,(or (mail-header-references gnus-current-headers) "")
3969        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3970
3971 (defun gnus-article-edit-article (start-func exit-func)
3972   "Start editing the contents of the current article buffer."
3973   (let ((winconf (current-window-configuration)))
3974     (set-buffer gnus-article-buffer)
3975     (gnus-article-edit-mode)
3976     (funcall start-func)
3977     (gnus-configure-windows 'edit-article)
3978     (setq gnus-article-edit-done-function exit-func)
3979     (setq gnus-prev-winconf winconf)
3980     (gnus-message 6 "C-c C-c to end edits")))
3981
3982 (defun gnus-article-edit-done (&optional arg)
3983   "Update the article edits and exit."
3984   (interactive "P")
3985   (save-excursion
3986     (save-restriction
3987       (widen)
3988       (when (article-goto-body)
3989         (let ((lines (count-lines (point) (point-max)))
3990               (length (- (point-max) (point)))
3991               (case-fold-search t)
3992               (body (copy-marker (point))))
3993           (goto-char (point-min))
3994           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3995             (delete-region (match-beginning 1) (match-end 1))
3996             (insert (number-to-string length)))
3997           (goto-char (point-min))
3998           (when (re-search-forward
3999                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
4000             (delete-region (match-beginning 1) (match-end 1))
4001             (insert (number-to-string length)))
4002           (goto-char (point-min))
4003           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
4004             (delete-region (match-beginning 1) (match-end 1))
4005             (insert (number-to-string lines)))))))
4006   (let ((func gnus-article-edit-done-function)
4007         (buf (current-buffer))
4008         (start (window-start)))
4009     (gnus-article-edit-exit)
4010     (save-excursion
4011       (set-buffer buf)
4012       (let ((buffer-read-only nil))
4013         (funcall func arg))
4014       ;; The cache and backlog have to be flushed somewhat.
4015       (when gnus-keep-backlog
4016         (gnus-backlog-remove-article
4017          (car gnus-article-current) (cdr gnus-article-current)))
4018       ;; Flush original article as well.
4019       (save-excursion
4020         (when (get-buffer gnus-original-article-buffer)
4021           (set-buffer gnus-original-article-buffer)
4022           (setq gnus-original-article nil)))
4023       (when gnus-use-cache
4024         (gnus-cache-update-article
4025          (car gnus-article-current) (cdr gnus-article-current))))
4026     (set-buffer buf)
4027     (set-window-start (get-buffer-window buf) start)
4028     (set-window-point (get-buffer-window buf) (point))))
4029
4030 (defun gnus-article-edit-exit ()
4031   "Exit the article editing without updating."
4032   (interactive)
4033   ;; We remove all text props from the article buffer.
4034   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4035         (curbuf (current-buffer))
4036         (p (point))
4037         (window-start (window-start)))
4038     (erase-buffer)
4039     (insert buf)
4040     (let ((winconf gnus-prev-winconf))
4041       (gnus-article-mode)
4042       (set-window-configuration winconf)
4043       ;; Tippy-toe some to make sure that point remains where it was.
4044       (save-current-buffer
4045         (set-buffer curbuf)
4046         (set-window-start (get-buffer-window (current-buffer)) window-start)
4047         (goto-char p)))))
4048
4049 (defun gnus-article-edit-full-stops ()
4050   "Interactively repair spacing at end of sentences."
4051   (interactive)
4052   (save-excursion
4053     (goto-char (point-min))
4054     (search-forward-regexp "^$" nil t)
4055     (let ((case-fold-search nil))
4056       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4057
4058 ;;;
4059 ;;; Article highlights
4060 ;;;
4061
4062 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4063
4064 ;;; Internal Variables:
4065
4066 (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\\)\\)"
4067   "Regular expression that matches URLs."
4068   :group 'gnus-article-buttons
4069   :type 'regexp)
4070
4071 (defcustom gnus-button-alist
4072   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4073      0 t gnus-button-message-id 2)
4074     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4075     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4076      1 t
4077      gnus-button-fetch-group 4)
4078     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4079     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4080      t gnus-button-message-id 3)
4081     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4082     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4083     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4084     ;; This is how URLs _should_ be embedded in text...
4085     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4086     ;; Raw URLs.
4087     (,gnus-button-url-regexp 0 t browse-url 0))
4088   "*Alist of regexps matching buttons in article bodies.
4089
4090 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4091 REGEXP: is the string matching text around the button,
4092 BUTTON: is the number of the regexp grouping actually matching the button,
4093 FORM: is a lisp expression which must eval to true for the button to
4094 be added,
4095 CALLBACK: is the function to call when the user push this button, and each
4096 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4097
4098 CALLBACK can also be a variable, in that case the value of that
4099 variable it the real callback function."
4100   :group 'gnus-article-buttons
4101   :type '(repeat (list regexp
4102                        (integer :tag "Button")
4103                        (sexp :tag "Form")
4104                        (function :tag "Callback")
4105                        (repeat :tag "Par"
4106                                :inline t
4107                                (integer :tag "Regexp group")))))
4108
4109 (defcustom gnus-header-button-alist
4110   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4111      0 t gnus-button-message-id 0)
4112     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4113     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4114      0 t gnus-button-mailto 0)
4115     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4116     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4117     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4118     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4119      gnus-button-message-id 3))
4120   "*Alist of headers and regexps to match buttons in article heads.
4121
4122 This alist is very similar to `gnus-button-alist', except that each
4123 alist has an additional HEADER element first in each entry:
4124
4125 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4126
4127 HEADER is a regexp to match a header.  For a fuller explanation, see
4128 `gnus-button-alist'."
4129   :group 'gnus-article-buttons
4130   :group 'gnus-article-headers
4131   :type '(repeat (list (regexp :tag "Header")
4132                        regexp
4133                        (integer :tag "Button")
4134                        (sexp :tag "Form")
4135                        (function :tag "Callback")
4136                        (repeat :tag "Par"
4137                                :inline t
4138                                (integer :tag "Regexp group")))))
4139
4140 (defvar gnus-button-regexp nil)
4141 (defvar gnus-button-marker-list nil)
4142 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4143
4144 (defvar gnus-button-last nil)
4145 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4146
4147 ;;; Commands:
4148
4149 (defun gnus-article-push-button (event)
4150   "Check text under the mouse pointer for a callback function.
4151 If the text under the mouse pointer has a `gnus-callback' property,
4152 call it with the value of the `gnus-data' text property."
4153   (interactive "e")
4154   (set-buffer (window-buffer (posn-window (event-start event))))
4155   (let* ((pos (posn-point (event-start event)))
4156          (data (get-text-property pos 'gnus-data))
4157          (fun (get-text-property pos 'gnus-callback)))
4158     (goto-char pos)
4159     (when fun
4160       (funcall fun data))))
4161
4162 (defun gnus-article-press-button ()
4163   "Check text at point for a callback function.
4164 If the text at point has a `gnus-callback' property,
4165 call it with the value of the `gnus-data' text property."
4166   (interactive)
4167   (let* ((data (get-text-property (point) 'gnus-data))
4168          (fun (get-text-property (point) 'gnus-callback)))
4169     (when fun
4170       (funcall fun data))))
4171
4172 (defun gnus-article-highlight (&optional force)
4173   "Highlight current article.
4174 This function calls `gnus-article-highlight-headers',
4175 `gnus-article-highlight-citation',
4176 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4177 do the highlighting.  See the documentation for those functions."
4178   (interactive (list 'force))
4179   (gnus-article-highlight-headers)
4180   (gnus-article-highlight-citation force)
4181   (gnus-article-highlight-signature)
4182   (gnus-article-add-buttons force)
4183   (gnus-article-add-buttons-to-head))
4184
4185 (defun gnus-article-highlight-some (&optional force)
4186   "Highlight current article.
4187 This function calls `gnus-article-highlight-headers',
4188 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4189 do the highlighting.  See the documentation for those functions."
4190   (interactive (list 'force))
4191   (gnus-article-highlight-headers)
4192   (gnus-article-highlight-signature)
4193   (gnus-article-add-buttons))
4194
4195 (defun gnus-article-highlight-headers ()
4196   "Highlight article headers as specified by `gnus-header-face-alist'."
4197   (interactive)
4198   (save-excursion
4199     (set-buffer gnus-article-buffer)
4200     (save-restriction
4201       (let ((alist gnus-header-face-alist)
4202             (buffer-read-only nil)
4203             (case-fold-search t)
4204             (inhibit-point-motion-hooks t)
4205             entry regexp header-face field-face from hpoints fpoints)
4206         (article-narrow-to-head)
4207         (while (setq entry (pop alist))
4208           (goto-char (point-min))
4209           (setq regexp (concat "^\\("
4210                                (if (string-equal "" (nth 0 entry))
4211                                    "[^\t ]"
4212                                  (nth 0 entry))
4213                                "\\)")
4214                 header-face (nth 1 entry)
4215                 field-face (nth 2 entry))
4216           (while (and (re-search-forward regexp nil t)
4217                       (not (eobp)))
4218             (beginning-of-line)
4219             (setq from (point))
4220             (unless (search-forward ":" nil t)
4221               (forward-char 1))
4222             (when (and header-face
4223                        (not (memq (point) hpoints)))
4224               (push (point) hpoints)
4225               (gnus-put-text-property from (point) 'face header-face))
4226             (when (and field-face
4227                        (not (memq (setq from (point)) fpoints)))
4228               (push from fpoints)
4229               (if (re-search-forward "^[^ \t]" nil t)
4230                   (forward-char -2)
4231                 (goto-char (point-max)))
4232               (gnus-put-text-property from (point) 'face field-face))))))))
4233
4234 (defun gnus-article-highlight-signature ()
4235   "Highlight the signature in an article.
4236 It does this by highlighting everything after
4237 `gnus-signature-separator' using `gnus-signature-face'."
4238   (interactive)
4239   (save-excursion
4240     (set-buffer gnus-article-buffer)
4241     (let ((buffer-read-only nil)
4242           (inhibit-point-motion-hooks t))
4243       (save-restriction
4244         (when (and gnus-signature-face
4245                    (gnus-article-narrow-to-signature))
4246           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4247                             'face gnus-signature-face)
4248           (widen)
4249           (gnus-article-search-signature)
4250           (let ((start (match-beginning 0))
4251                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4252             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4253                                      end)))))))
4254
4255 (defun gnus-button-in-region-p (b e prop)
4256   "Say whether PROP exists in the region."
4257   (text-property-not-all b e prop nil))
4258
4259 (defun gnus-article-add-buttons (&optional force)
4260   "Find external references in the article and make buttons of them.
4261 \"External references\" are things like Message-IDs and URLs, as
4262 specified by `gnus-button-alist'."
4263   (interactive (list 'force))
4264   (save-excursion
4265     (set-buffer gnus-article-buffer)
4266     (let ((buffer-read-only nil)
4267           (inhibit-point-motion-hooks t)
4268           (case-fold-search t)
4269           (alist gnus-button-alist)
4270           beg entry regexp)
4271       ;; Remove all old markers.
4272       (let (marker entry new-list)
4273         (while (setq marker (pop gnus-button-marker-list))
4274           (if (or (< marker (point-min)) (>= marker (point-max)))
4275               (push marker new-list)
4276             (goto-char marker)
4277             (when (setq entry (gnus-button-entry))
4278               (put-text-property (match-beginning (nth 1 entry))
4279                                  (match-end (nth 1 entry))
4280                                  'gnus-callback nil))
4281             (set-marker marker nil)))
4282         (setq gnus-button-marker-list new-list))
4283       ;; We skip the headers.
4284       (article-goto-body)
4285       (setq beg (point))
4286       (while (setq entry (pop alist))
4287         (setq regexp (car entry))
4288         (goto-char beg)
4289         (while (re-search-forward regexp nil t)
4290           (let* ((start (and entry (match-beginning (nth 1 entry))))
4291                  (end (and entry (match-end (nth 1 entry))))
4292                  (from (match-beginning 0)))
4293             (when (and (or (eq t (nth 2 entry))
4294                            (eval (nth 2 entry)))
4295                        (not (gnus-button-in-region-p
4296                              start end 'gnus-callback)))
4297               ;; That optional form returned non-nil, so we add the
4298               ;; button.
4299               (gnus-article-add-button
4300                start end 'gnus-button-push
4301                (car (push (set-marker (make-marker) from)
4302                           gnus-button-marker-list))))))))))
4303
4304 ;; Add buttons to the head of an article.
4305 (defun gnus-article-add-buttons-to-head ()
4306   "Add buttons to the head of the article."
4307   (interactive)
4308   (save-excursion
4309     (set-buffer gnus-article-buffer)
4310     (save-restriction
4311       (let ((buffer-read-only nil)
4312             (inhibit-point-motion-hooks t)
4313             (case-fold-search t)
4314             (alist gnus-header-button-alist)
4315             entry beg end)
4316         (article-narrow-to-head)
4317         (while alist
4318           ;; Each alist entry.
4319           (setq entry (car alist)
4320                 alist (cdr alist))
4321           (goto-char (point-min))
4322           (while (re-search-forward (car entry) nil t)
4323             ;; Each header matching the entry.
4324             (setq beg (match-beginning 0))
4325             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4326                                (match-beginning 0))
4327                           (point-max)))
4328             (goto-char beg)
4329             (while (re-search-forward (nth 1 entry) end t)
4330               ;; Each match within a header.
4331               (let* ((entry (cdr entry))
4332                      (start (match-beginning (nth 1 entry)))
4333                      (end (match-end (nth 1 entry)))
4334                      (form (nth 2 entry)))
4335                 (goto-char (match-end 0))
4336                 (when (eval form)
4337                   (gnus-article-add-button
4338                    start end (nth 3 entry)
4339                    (buffer-substring (match-beginning (nth 4 entry))
4340                                      (match-end (nth 4 entry)))))))
4341             (goto-char end)))))))
4342
4343 ;;; External functions:
4344
4345 (defun gnus-article-add-button (from to fun &optional data)
4346   "Create a button between FROM and TO with callback FUN and data DATA."
4347   (when gnus-article-button-face
4348     (gnus-overlay-put (gnus-make-overlay from to)
4349                       'face gnus-article-button-face))
4350   (gnus-add-text-properties
4351    from to
4352    (nconc (and gnus-article-mouse-face
4353                (list gnus-mouse-face-prop gnus-article-mouse-face))
4354           (list 'gnus-callback fun)
4355           (and data (list 'gnus-data data))))
4356   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4357                          :button-keymap gnus-widget-button-keymap))
4358
4359 ;;; Internal functions:
4360
4361 (defun gnus-article-set-globals ()
4362   (save-excursion
4363     (set-buffer gnus-summary-buffer)
4364     (gnus-set-global-variables)))
4365
4366 (defun gnus-signature-toggle (end)
4367   (save-excursion
4368     (set-buffer gnus-article-buffer)
4369     (let ((buffer-read-only nil)
4370           (inhibit-point-motion-hooks t))
4371       (if (get-text-property end 'invisible)
4372           (gnus-article-unhide-text end (point-max))
4373         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4374
4375 (defun gnus-button-entry ()
4376   ;; Return the first entry in `gnus-button-alist' matching this place.
4377   (let ((alist gnus-button-alist)
4378         (entry nil))
4379     (while alist
4380       (setq entry (pop alist))
4381       (if (looking-at (car entry))
4382           (setq alist nil)
4383         (setq entry nil)))
4384     entry))
4385
4386 (defun gnus-button-push (marker)
4387   ;; Push button starting at MARKER.
4388   (save-excursion
4389     (goto-char marker)
4390     (let* ((entry (gnus-button-entry))
4391            (inhibit-point-motion-hooks t)
4392            (fun (nth 3 entry))
4393            (args (mapcar (lambda (group)
4394                            (let ((string (match-string group)))
4395                              (gnus-set-text-properties
4396                               0 (length string) nil string)
4397                              string))
4398                          (nthcdr 4 entry))))
4399       (cond
4400        ((fboundp fun)
4401         (apply fun args))
4402        ((and (boundp fun)
4403              (fboundp (symbol-value fun)))
4404         (apply (symbol-value fun) args))
4405        (t
4406         (gnus-message 1 "You must define `%S' to use this button"
4407                       (cons fun args)))))))
4408
4409 (defun gnus-button-message-id (message-id)
4410   "Fetch MESSAGE-ID."
4411   (save-excursion
4412     (set-buffer gnus-summary-buffer)
4413     (gnus-summary-refer-article message-id)))
4414
4415 (defun gnus-button-fetch-group (address)
4416   "Fetch GROUP specified by ADDRESS."
4417   (if (not (string-match "[:/]" address))
4418       ;; This is just a simple group url.
4419       (gnus-group-read-ephemeral-group address gnus-select-method)
4420     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4421                            address))
4422         (error "Can't parse %s" address)
4423       (gnus-group-read-ephemeral-group
4424        (match-string 4 address)
4425        `(nntp ,(match-string 1 address)
4426               (nntp-address ,(match-string 1 address))
4427               (nntp-port-number ,(if (match-end 3)
4428                                      (match-string 3 address)
4429                                    "nntp")))))))
4430
4431 (defun gnus-url-parse-query-string (query &optional downcase)
4432   (let (retval pairs cur key val)
4433     (setq pairs (split-string query "&"))
4434     (while pairs
4435       (setq cur (car pairs)
4436             pairs (cdr pairs))
4437       (if (not (string-match "=" cur))
4438           nil                           ; Grace
4439         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4440               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4441         (if downcase
4442             (setq key (downcase key)))
4443         (setq cur (assoc key retval))
4444         (if cur
4445             (setcdr cur (cons val (cdr cur)))
4446           (setq retval (cons (list key val) retval)))))
4447     retval))
4448
4449 (defun gnus-url-unhex (x)
4450   (if (> x ?9)
4451       (if (>= x ?a)
4452           (+ 10 (- x ?a))
4453         (+ 10 (- x ?A)))
4454     (- x ?0)))
4455
4456 (defun gnus-url-unhex-string (str &optional allow-newlines)
4457   "Remove %XXX embedded spaces, etc in a url.
4458 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4459 decoding of carriage returns and line feeds in the string, which is normally
4460 forbidden in URL encoding."
4461   (setq str (or str ""))
4462   (let ((tmp "")
4463         (case-fold-search t))
4464     (while (string-match "%[0-9a-f][0-9a-f]" str)
4465       (let* ((start (match-beginning 0))
4466              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4467              (code (+ (* 16 ch1)
4468                       (gnus-url-unhex (elt str (+ start 2))))))
4469         (setq tmp (concat
4470                    tmp (substring str 0 start)
4471                    (cond
4472                     (allow-newlines
4473                      (char-to-string code))
4474                     ((or (= code ?\n) (= code ?\r))
4475                      " ")
4476                     (t (char-to-string code))))
4477               str (substring str (match-end 0)))))
4478     (setq tmp (concat tmp str))
4479     tmp))
4480
4481 (defun gnus-url-mailto (url)
4482   ;; Send mail to someone
4483   (when (string-match "mailto:/*\\(.*\\)" url)
4484     (setq url (substring url (match-beginning 1) nil)))
4485   (let (to args subject func)
4486     (if (string-match (regexp-quote "?") url)
4487         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4488               args (gnus-url-parse-query-string
4489                     (substring url (match-end 0) nil) t))
4490       (setq to (gnus-url-unhex-string url)))
4491     (setq args (cons (list "to" to) args)
4492           subject (cdr-safe (assoc "subject" args)))
4493     (message-mail)
4494     (while args
4495       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4496       (if (fboundp func)
4497           (funcall func)
4498         (message-position-on-field (caar args)))
4499       (insert (mapconcat 'identity (cdar args) ", "))
4500       (setq args (cdr args)))
4501     (if subject
4502         (message-goto-body)
4503       (message-goto-subject))))
4504
4505 (defun gnus-button-mailto (address)
4506   ;; Mail to ADDRESS.
4507   (set-buffer (gnus-copy-article-buffer))
4508   (message-reply address))
4509
4510 (defun gnus-button-reply (address)
4511   ;; Reply to ADDRESS.
4512   (message-reply address))
4513
4514 (defun gnus-button-embedded-url (address)
4515   "Browse ADDRESS."
4516   (browse-url (gnus-strip-whitespace address)))
4517
4518 ;;; Next/prev buttons in the article buffer.
4519
4520 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4521 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4522
4523 (defvar gnus-prev-page-map nil)
4524 (unless gnus-prev-page-map
4525   (setq gnus-prev-page-map (make-sparse-keymap))
4526   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4527   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4528
4529 (defun gnus-insert-prev-page-button ()
4530   (let ((buffer-read-only nil))
4531     (gnus-eval-format
4532      gnus-prev-page-line-format nil
4533      `(gnus-prev t local-map ,gnus-prev-page-map
4534                  gnus-callback gnus-article-button-prev-page
4535                  article-type annotation))))
4536
4537 (defvar gnus-next-page-map nil)
4538 (unless gnus-next-page-map
4539   (setq gnus-next-page-map (make-keymap))
4540   (suppress-keymap gnus-prev-page-map)
4541   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4542   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4543
4544 (defun gnus-button-next-page ()
4545   "Go to the next page."
4546   (interactive)
4547   (let ((win (selected-window)))
4548     (select-window (get-buffer-window gnus-article-buffer t))
4549     (gnus-article-next-page)
4550     (select-window win)))
4551
4552 (defun gnus-button-prev-page ()
4553   "Go to the prev page."
4554   (interactive)
4555   (let ((win (selected-window)))
4556     (select-window (get-buffer-window gnus-article-buffer t))
4557     (gnus-article-prev-page)
4558     (select-window win)))
4559
4560 (defun gnus-insert-next-page-button ()
4561   (let ((buffer-read-only nil))
4562     (gnus-eval-format gnus-next-page-line-format nil
4563                       `(gnus-next
4564                         t local-map ,gnus-next-page-map
4565                         gnus-callback gnus-article-button-next-page
4566                         article-type annotation))))
4567
4568 (defun gnus-article-button-next-page (arg)
4569   "Go to the next page."
4570   (interactive "P")
4571   (let ((win (selected-window)))
4572     (select-window (get-buffer-window gnus-article-buffer t))
4573     (gnus-article-next-page)
4574     (select-window win)))
4575
4576 (defun gnus-article-button-prev-page (arg)
4577   "Go to the prev page."
4578   (interactive "P")
4579   (let ((win (selected-window)))
4580     (select-window (get-buffer-window gnus-article-buffer t))
4581     (gnus-article-prev-page)
4582     (select-window win)))
4583
4584 (defvar gnus-decode-header-methods
4585   '(mail-decode-encoded-word-region)
4586   "List of methods used to decode headers.
4587
4588 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
4589 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
4590 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4591 whose names match REGEXP.
4592
4593 For example:
4594 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4595  mail-decode-encoded-word-region
4596  (\"chinese\" . rfc1843-decode-region))
4597 ")
4598
4599 (defvar gnus-decode-header-methods-cache nil)
4600
4601 (defun gnus-multi-decode-header (start end)
4602   "Apply the functions from `gnus-encoded-word-methods' that match."
4603   (unless (and gnus-decode-header-methods-cache
4604                (eq gnus-newsgroup-name
4605                    (car gnus-decode-header-methods-cache)))
4606     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4607     (mapcar (lambda (x)
4608               (if (symbolp x)
4609                   (nconc gnus-decode-header-methods-cache (list x))
4610                 (if (and gnus-newsgroup-name
4611                          (string-match (car x) gnus-newsgroup-name))
4612                     (nconc gnus-decode-header-methods-cache
4613                            (list (cdr x))))))
4614           gnus-decode-header-methods))
4615   (let ((xlist gnus-decode-header-methods-cache))
4616     (pop xlist)
4617     (save-restriction
4618       (narrow-to-region start end)
4619       (while xlist
4620         (funcall (pop xlist) (point-min) (point-max))))))
4621
4622 ;;;
4623 ;;; Treatment top-level handling.
4624 ;;;
4625
4626 (defun gnus-treat-article (condition &optional part-number total-parts type)
4627   (let ((length (- (point-max) (point-min)))
4628         (alist gnus-treatment-function-alist)
4629         (article-goto-body-goes-to-point-min-p t)
4630         (treated-type
4631          (or (not type)
4632              (catch 'found
4633                (let ((list gnus-article-treat-types))
4634                  (while list
4635                    (when (string-match (pop list) type)
4636                      (throw 'found t)))))))
4637         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4638         val elem)
4639     (gnus-run-hooks 'gnus-part-display-hook)
4640     (while (setq elem (pop alist))
4641       (setq val
4642             (save-excursion
4643               (if (gnus-buffer-live-p gnus-summary-buffer)
4644                   (set-buffer gnus-summary-buffer))
4645               (symbol-value (car elem))))
4646       (when (and (or (consp val)
4647                      treated-type)
4648                  (gnus-treat-predicate val)
4649                  (or (not (get (car elem) 'highlight))
4650                      highlightp))
4651         (save-restriction
4652           (funcall (cadr elem)))))))
4653
4654 ;; Dynamic variables.
4655 (defvar part-number)
4656 (defvar total-parts)
4657 (defvar type)
4658 (defvar condition)
4659 (defvar length)
4660 (defun gnus-treat-predicate (val)
4661   (cond
4662    ((null val)
4663     nil)
4664    ((and (listp val)
4665          (stringp (car val)))
4666     (apply 'gnus-or (mapcar `(lambda (s)
4667                                (string-match s ,(or gnus-newsgroup-name "")))
4668                             val)))
4669    ((listp val)
4670     (let ((pred (pop val)))
4671       (cond
4672        ((eq pred 'or)
4673         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4674        ((eq pred 'and)
4675         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4676        ((eq pred 'not)
4677         (not (gnus-treat-predicate (car val))))
4678        ((eq pred 'typep)
4679         (equal (car val) type))
4680        (t
4681         (error "%S is not a valid predicate" pred)))))
4682    (condition
4683     (eq condition val))
4684    ((eq val t)
4685     t)
4686    ((eq val 'head)
4687     nil)
4688    ((eq val 'last)
4689     (eq part-number total-parts))
4690    ((numberp val)
4691     (< length val))
4692    (t
4693     (error "%S is not a valid value" val))))
4694
4695 (gnus-ems-redefine)
4696
4697 (provide 'gnus-art)
4698
4699 (run-hooks 'gnus-art-load-hook)
4700
4701 ;;; gnus-art.el ends here