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