hide all PGP headers, not just Hash:
[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           ;; Remove armor headers (rfc2440 6.2)
1499           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1500                                      (point)))
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          pos
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 pos
1847                   (delete-region (progn (beginning-of-line) (point))
1848                                  (progn (forward-line 1) (point)))
1849                 (delete-region (progn (beginning-of-line) (point))
1850                                (progn (end-of-line) (point)))
1851                 (setq pos (point))))
1852             (when (and (not pos) (re-search-forward tdate-regexp nil t))
1853               (forward-line 1))
1854             (if pos (goto-char pos))
1855             (insert (article-make-date-line date (or type 'ut)))
1856             (when (not pos)
1857               (insert "\n")
1858               (forward-line -1))
1859             ;; Do highlighting.
1860             (beginning-of-line)
1861             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1862               (put-text-property (match-beginning 1) (1+ (match-end 1))
1863                                  'original-date date)
1864               (put-text-property (match-beginning 1) (1+ (match-end 1))
1865                                  'face bface)
1866               (put-text-property (match-beginning 2) (match-end 2)
1867                                  'face eface))))))))
1868
1869 (defun article-make-date-line (date type)
1870   "Return a DATE line of TYPE."
1871   (let ((time (condition-case ()
1872                   (date-to-time date)
1873                 (error '(0 0)))))
1874     (cond
1875      ;; Convert to the local timezone.  We have to slap a
1876      ;; `condition-case' round the calls to the timezone
1877      ;; functions since they aren't particularly resistant to
1878      ;; buggy dates.
1879      ((eq type 'local)
1880       (let ((tz (car (current-time-zone time))))
1881         (format "Date: %s %s%02d%02d" (current-time-string time)
1882                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
1883                 (/ (% (abs tz) 3600) 60))))
1884      ;; Convert to Universal Time.
1885      ((eq type 'ut)
1886       (concat "Date: "
1887               (current-time-string
1888                (let* ((e (parse-time-string date))
1889                      (tm (apply 'encode-time e))
1890                      (ms (car tm))
1891                      (ls (- (cadr tm) (car (current-time-zone time)))))
1892                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1893                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1894                        (t (list ms ls)))))
1895               " UT"))
1896      ;; Get the original date from the article.
1897      ((eq type 'original)
1898       (concat "Date: " (if (string-match "\n+$" date)
1899                            (substring date 0 (match-beginning 0))
1900                          date)))
1901      ;; Let the user define the format.
1902      ((eq type 'user)
1903       (if (gnus-functionp gnus-article-time-format)
1904           (funcall gnus-article-time-format time)
1905         (concat
1906          "Date: "
1907          (format-time-string gnus-article-time-format time))))
1908      ;; ISO 8601.
1909      ((eq type 'iso8601)
1910       (let ((tz (car (current-time-zone time))))
1911         (concat
1912          "Date: "
1913          (format-time-string "%Y%m%dT%H%M%S" time)
1914          (format "%s%02d%02d"
1915                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
1916                  (/ (% (abs tz) 3600) 60)))))
1917      ;; Do an X-Sent lapsed format.
1918      ((eq type 'lapsed)
1919       ;; If the date is seriously mangled, the timezone functions are
1920       ;; liable to bug out, so we ignore all errors.
1921       (let* ((now (current-time))
1922              (real-time (subtract-time now time))
1923              (real-sec (and real-time
1924                             (+ (* (float (car real-time)) 65536)
1925                                (cadr real-time))))
1926              (sec (and real-time (abs real-sec)))
1927              num prev)
1928         (cond
1929          ((null real-time)
1930           "X-Sent: Unknown")
1931          ((zerop sec)
1932           "X-Sent: Now")
1933          (t
1934           (concat
1935            "X-Sent: "
1936            ;; This is a bit convoluted, but basically we go
1937            ;; through the time units for years, weeks, etc,
1938            ;; and divide things to see whether that results
1939            ;; in positive answers.
1940            (mapconcat
1941             (lambda (unit)
1942               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1943                   ;; The (remaining) seconds are too few to
1944                   ;; be divided into this time unit.
1945                   ""
1946                 ;; It's big enough, so we output it.
1947                 (setq sec (- sec (* num (cdr unit))))
1948                 (prog1
1949                     (concat (if prev ", " "") (int-to-string
1950                                                (floor num))
1951                             " " (symbol-name (car unit))
1952                             (if (> num 1) "s" ""))
1953                   (setq prev t))))
1954             article-time-units "")
1955            ;; If dates are odd, then it might appear like the
1956            ;; article was sent in the future.
1957            (if (> real-sec 0)
1958                " ago"
1959              " in the future"))))))
1960      (t
1961       (error "Unknown conversion type: %s" type)))))
1962
1963 (defun article-date-local (&optional highlight)
1964   "Convert the current article date to the local timezone."
1965   (interactive (list t))
1966   (article-date-ut 'local highlight))
1967
1968 (defun article-date-original (&optional highlight)
1969   "Convert the current article date to what it was originally.
1970 This is only useful if you have used some other date conversion
1971 function and want to see what the date was before converting."
1972   (interactive (list t))
1973   (article-date-ut 'original highlight))
1974
1975 (defun article-date-lapsed (&optional highlight)
1976   "Convert the current article date to time lapsed since it was sent."
1977   (interactive (list t))
1978   (article-date-ut 'lapsed highlight))
1979
1980 (defun article-update-date-lapsed ()
1981   "Function to be run from a timer to update the lapsed time line."
1982   (let (deactivate-mark)
1983     (save-excursion
1984       (ignore-errors
1985         (walk-windows
1986          (lambda (w)
1987            (set-buffer (window-buffer w))
1988            (when (eq major-mode 'gnus-article-mode)
1989              (goto-char (point-min))
1990              (when (re-search-forward "^X-Sent:" nil t)
1991                (article-date-lapsed t))))
1992          nil 'visible)))))
1993
1994 (defun gnus-start-date-timer (&optional n)
1995   "Start a timer to update the X-Sent header in the article buffers.
1996 The numerical prefix says how frequently (in seconds) the function
1997 is to run."
1998   (interactive "p")
1999   (unless n
2000     (setq n 1))
2001   (gnus-stop-date-timer)
2002   (setq article-lapsed-timer
2003         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2004
2005 (defun gnus-stop-date-timer ()
2006   "Stop the X-Sent timer."
2007   (interactive)
2008   (when article-lapsed-timer
2009     (nnheader-cancel-timer article-lapsed-timer)
2010     (setq article-lapsed-timer nil)))
2011
2012 (defun article-date-user (&optional highlight)
2013   "Convert the current article date to the user-defined format.
2014 This format is defined by the `gnus-article-time-format' variable."
2015   (interactive (list t))
2016   (article-date-ut 'user highlight))
2017
2018 (defun article-date-iso8601 (&optional highlight)
2019   "Convert the current article date to ISO8601."
2020   (interactive (list t))
2021   (article-date-ut 'iso8601 highlight))
2022
2023 (defun article-show-all ()
2024   "Show all hidden text in the article buffer."
2025   (interactive)
2026   (save-excursion
2027     (let ((buffer-read-only nil))
2028       (gnus-article-unhide-text (point-min) (point-max)))))
2029
2030 (defun article-emphasize (&optional arg)
2031   "Emphasize text according to `gnus-emphasis-alist'."
2032   (interactive (gnus-article-hidden-arg))
2033   (unless (gnus-article-check-hidden-text 'emphasis arg)
2034     (save-excursion
2035       (let ((alist (or (with-current-buffer gnus-summary-buffer 
2036                          gnus-article-emphasis-alist) 
2037                        gnus-emphasis-alist))
2038             (buffer-read-only nil)
2039             (props (append '(article-type emphasis)
2040                            gnus-hidden-properties))
2041             regexp elem beg invisible visible face)
2042         (article-goto-body)
2043         (setq beg (point))
2044         (while (setq elem (pop alist))
2045           (goto-char beg)
2046           (setq regexp (car elem)
2047                 invisible (nth 1 elem)
2048                 visible (nth 2 elem)
2049                 face (nth 3 elem))
2050           (while (re-search-forward regexp nil t)
2051             (when (and (match-beginning visible) (match-beginning invisible))
2052               (push 'emphasis gnus-article-wash-types)
2053               (gnus-article-hide-text
2054                (match-beginning invisible) (match-end invisible) props)
2055               (gnus-article-unhide-text-type
2056                (match-beginning visible) (match-end visible) 'emphasis)
2057               (gnus-put-text-property-excluding-newlines
2058                (match-beginning visible) (match-end visible) 'face face)
2059               (goto-char (match-end invisible)))))))))
2060
2061 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2062   "Setup newsgroup emphasis alist."
2063   (unless gnus-article-emphasis-alist
2064     (let ((name (and gnus-newsgroup-name
2065                      (gnus-group-real-name gnus-newsgroup-name))))
2066       (make-local-variable 'gnus-article-emphasis-alist)
2067       (setq gnus-article-emphasis-alist 
2068             (nconc 
2069              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2070                (while (setq elem (pop alist))
2071                  (when (and name (string-match (car elem) name))
2072                    (setq alist nil
2073                          highlight (copy-list (cdr elem)))))
2074                highlight)
2075              (copy-list highlight-words)
2076              (if gnus-newsgroup-name
2077                  (copy-list (gnus-group-find-parameter 
2078                              gnus-newsgroup-name 'highlight-words t)))
2079              gnus-emphasis-alist)))))
2080
2081 (defvar gnus-summary-article-menu)
2082 (defvar gnus-summary-post-menu)
2083
2084 ;;; Saving functions.
2085
2086 (defun gnus-article-save (save-buffer file &optional num)
2087   "Save the currently selected article."
2088   (unless gnus-save-all-headers
2089     ;; Remove headers according to `gnus-saved-headers'.
2090     (let ((gnus-visible-headers
2091            (or gnus-saved-headers gnus-visible-headers))
2092           (gnus-article-buffer save-buffer))
2093       (save-excursion
2094         (set-buffer save-buffer)
2095         (article-hide-headers 1 t))))
2096   (save-window-excursion
2097     (if (not gnus-default-article-saver)
2098         (error "No default saver is defined")
2099       ;; !!! Magic!  The saving functions all save
2100       ;; `gnus-save-article-buffer' (or so they think), but we
2101       ;; bind that variable to our save-buffer.
2102       (set-buffer gnus-article-buffer)
2103       (let* ((gnus-save-article-buffer save-buffer)
2104              (filename
2105               (cond
2106                ((not gnus-prompt-before-saving) 'default)
2107                ((eq gnus-prompt-before-saving 'always) nil)
2108                (t file)))
2109              (gnus-number-of-articles-to-be-saved
2110               (when (eq gnus-prompt-before-saving t)
2111                 num)))                  ; Magic
2112         (set-buffer gnus-article-current-summary)
2113         (funcall gnus-default-article-saver filename)))))
2114
2115 (defun gnus-read-save-file-name (prompt &optional filename
2116                                         function group headers variable)
2117   (let ((default-name
2118           (funcall function group headers (symbol-value variable)))
2119         result)
2120     (setq
2121      result
2122      (cond
2123       ((eq filename 'default)
2124        default-name)
2125       ((eq filename t)
2126        default-name)
2127       (filename filename)
2128       (t
2129        (let* ((split-name (gnus-get-split-value gnus-split-methods))
2130               (prompt
2131                (format prompt
2132                        (if (and gnus-number-of-articles-to-be-saved
2133                                 (> gnus-number-of-articles-to-be-saved 1))
2134                            (format "these %d articles"
2135                                    gnus-number-of-articles-to-be-saved)
2136                          "this article")))
2137               (file
2138                ;; Let the split methods have their say.
2139                (cond
2140                 ;; No split name was found.
2141                 ((null split-name)
2142                  (read-file-name
2143                   (concat prompt " (default "
2144                           (file-name-nondirectory default-name) ") ")
2145                   (file-name-directory default-name)
2146                   default-name))
2147                 ;; A single group name is returned.
2148                 ((stringp split-name)
2149                  (setq default-name
2150                        (funcall function split-name headers
2151                                 (symbol-value variable)))
2152                  (read-file-name
2153                   (concat prompt " (default "
2154                           (file-name-nondirectory default-name) ") ")
2155                   (file-name-directory default-name)
2156                   default-name))
2157                 ;; A single split name was found
2158                 ((= 1 (length split-name))
2159                  (let* ((name (expand-file-name
2160                                (car split-name) gnus-article-save-directory))
2161                         (dir (cond ((file-directory-p name)
2162                                     (file-name-as-directory name))
2163                                    ((file-exists-p name) name)
2164                                    (t gnus-article-save-directory))))
2165                    (read-file-name
2166                     (concat prompt " (default " name ") ")
2167                     dir name)))
2168                 ;; A list of splits was found.
2169                 (t
2170                  (setq split-name (nreverse split-name))
2171                  (let (result)
2172                    (let ((file-name-history
2173                           (nconc split-name file-name-history)))
2174                      (setq result
2175                            (expand-file-name
2176                             (read-file-name
2177                              (concat prompt " (`M-p' for defaults) ")
2178                              gnus-article-save-directory
2179                              (car split-name))
2180                             gnus-article-save-directory)))
2181                    (car (push result file-name-history)))))))
2182          ;; Create the directory.
2183          (gnus-make-directory (file-name-directory file))
2184          ;; If we have read a directory, we append the default file name.
2185          (when (file-directory-p file)
2186            (setq file (concat (file-name-as-directory file)
2187                               (file-name-nondirectory default-name))))
2188          ;; Possibly translate some characters.
2189          (nnheader-translate-file-chars file)))))
2190     (gnus-make-directory (file-name-directory result))
2191     (set variable result)))
2192
2193 (defun gnus-article-archive-name (group)
2194   "Return the first instance of an \"Archive-name\" in the current buffer."
2195   (let ((case-fold-search t))
2196     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2197       (nnheader-concat gnus-article-save-directory
2198                        (match-string 1)))))
2199
2200 (defun gnus-article-nndoc-name (group)
2201   "If GROUP is an nndoc group, return the name of the parent group."
2202   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2203     (gnus-group-get-parameter group 'save-article-group)))
2204
2205 (defun gnus-summary-save-in-rmail (&optional filename)
2206   "Append this article to Rmail file.
2207 Optional argument FILENAME specifies file name.
2208 Directory to save to is default to `gnus-article-save-directory'."
2209   (setq filename (gnus-read-save-file-name
2210                   "Save %s in rmail file:" filename
2211                   gnus-rmail-save-name gnus-newsgroup-name
2212                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2213   (gnus-eval-in-buffer-window gnus-save-article-buffer
2214     (save-excursion
2215       (save-restriction
2216         (widen)
2217         (gnus-output-to-rmail filename))))
2218   filename)
2219
2220 (defun gnus-summary-save-in-mail (&optional filename)
2221   "Append this article to Unix mail file.
2222 Optional argument FILENAME specifies file name.
2223 Directory to save to is default to `gnus-article-save-directory'."
2224   (setq filename (gnus-read-save-file-name
2225                   "Save %s in Unix mail file:" filename
2226                   gnus-mail-save-name gnus-newsgroup-name
2227                   gnus-current-headers 'gnus-newsgroup-last-mail))
2228   (gnus-eval-in-buffer-window gnus-save-article-buffer
2229     (save-excursion
2230       (save-restriction
2231         (widen)
2232         (if (and (file-readable-p filename)
2233                  (mail-file-babyl-p filename))
2234             (rmail-output-to-rmail-file filename t)
2235           (gnus-output-to-mail filename)))))
2236   filename)
2237
2238 (defun gnus-summary-save-in-file (&optional filename overwrite)
2239   "Append this article to file.
2240 Optional argument FILENAME specifies file name.
2241 Directory to save to is default to `gnus-article-save-directory'."
2242   (setq filename (gnus-read-save-file-name
2243                   "Save %s in file:" filename
2244                   gnus-file-save-name gnus-newsgroup-name
2245                   gnus-current-headers 'gnus-newsgroup-last-file))
2246   (gnus-eval-in-buffer-window gnus-save-article-buffer
2247     (save-excursion
2248       (save-restriction
2249         (widen)
2250         (when (and overwrite
2251                    (file-exists-p filename))
2252           (delete-file filename))
2253         (gnus-output-to-file filename))))
2254   filename)
2255
2256 (defun gnus-summary-write-to-file (&optional filename)
2257   "Write this article to a file.
2258 Optional argument FILENAME specifies file name.
2259 The directory to save in defaults to `gnus-article-save-directory'."
2260   (gnus-summary-save-in-file nil t))
2261
2262 (defun gnus-summary-save-body-in-file (&optional filename)
2263   "Append this article body to a file.
2264 Optional argument FILENAME specifies file name.
2265 The directory to save in defaults to `gnus-article-save-directory'."
2266   (setq filename (gnus-read-save-file-name
2267                   "Save %s body in file:" filename
2268                   gnus-file-save-name gnus-newsgroup-name
2269                   gnus-current-headers 'gnus-newsgroup-last-file))
2270   (gnus-eval-in-buffer-window gnus-save-article-buffer
2271     (save-excursion
2272       (save-restriction
2273         (widen)
2274         (when (article-goto-body)
2275           (narrow-to-region (point) (point-max)))
2276         (gnus-output-to-file filename))))
2277   filename)
2278
2279 (defun gnus-summary-save-in-pipe (&optional command)
2280   "Pipe this article to subprocess."
2281   (setq command
2282         (cond ((and (eq command 'default)
2283                     gnus-last-shell-command)
2284                gnus-last-shell-command)
2285               (command command)
2286               (t (read-string
2287                   (format
2288                    "Shell command on %s: "
2289                    (if (and gnus-number-of-articles-to-be-saved
2290                             (> gnus-number-of-articles-to-be-saved 1))
2291                        (format "these %d articles"
2292                                gnus-number-of-articles-to-be-saved)
2293                      "this article"))
2294                   gnus-last-shell-command))))
2295   (when (string-equal command "")
2296     (setq command gnus-last-shell-command))
2297   (gnus-eval-in-buffer-window gnus-article-buffer
2298     (save-restriction
2299       (widen)
2300       (shell-command-on-region (point-min) (point-max) command nil)))
2301   (setq gnus-last-shell-command command))
2302
2303 ;;; Article file names when saving.
2304
2305 (defun gnus-capitalize-newsgroup (newsgroup)
2306   "Capitalize NEWSGROUP name."
2307   (when (not (zerop (length newsgroup)))
2308     (concat (char-to-string (upcase (aref newsgroup 0)))
2309             (substring newsgroup 1))))
2310
2311 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2312   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2313 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2314 Otherwise, it is like ~/News/news/group/num."
2315   (let ((default
2316           (expand-file-name
2317            (concat (if (gnus-use-long-file-name 'not-save)
2318                        (gnus-capitalize-newsgroup newsgroup)
2319                      (gnus-newsgroup-directory-form newsgroup))
2320                    "/" (int-to-string (mail-header-number headers)))
2321            gnus-article-save-directory)))
2322     (if (and last-file
2323              (string-equal (file-name-directory default)
2324                            (file-name-directory last-file))
2325              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2326         default
2327       (or last-file default))))
2328
2329 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2330   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2331 If variable `gnus-use-long-file-name' is non-nil, it is
2332 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2333   (let ((default
2334           (expand-file-name
2335            (concat (if (gnus-use-long-file-name 'not-save)
2336                        newsgroup
2337                      (gnus-newsgroup-directory-form newsgroup))
2338                    "/" (int-to-string (mail-header-number headers)))
2339            gnus-article-save-directory)))
2340     (if (and last-file
2341              (string-equal (file-name-directory default)
2342                            (file-name-directory last-file))
2343              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2344         default
2345       (or last-file default))))
2346
2347 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2348   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2349 If variable `gnus-use-long-file-name' is non-nil, it is
2350 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2351   (or last-file
2352       (expand-file-name
2353        (if (gnus-use-long-file-name 'not-save)
2354            (gnus-capitalize-newsgroup newsgroup)
2355          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2356        gnus-article-save-directory)))
2357
2358 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2359   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2360 If variable `gnus-use-long-file-name' is non-nil, it is
2361 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2362   (or last-file
2363       (expand-file-name
2364        (if (gnus-use-long-file-name 'not-save)
2365            newsgroup
2366          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2367        gnus-article-save-directory)))
2368
2369 (eval-and-compile
2370   (mapcar
2371    (lambda (func)
2372      (let (afunc gfunc)
2373        (if (consp func)
2374            (setq afunc (car func)
2375                  gfunc (cdr func))
2376          (setq afunc func
2377                gfunc (intern (format "gnus-%s" func))))
2378        (fset gfunc
2379              (if (not (fboundp afunc))
2380                  nil
2381                `(lambda (&optional interactive &rest args)
2382                   ,(documentation afunc t)
2383                   (interactive (list t))
2384                   (save-excursion
2385                     (set-buffer gnus-article-buffer)
2386                     (if interactive
2387                         (call-interactively ',afunc)
2388                       (apply ',afunc args))))))))
2389    '(article-hide-headers
2390      article-hide-boring-headers
2391      article-treat-overstrike
2392      article-fill-long-lines
2393      article-capitalize-sentences
2394      article-remove-cr
2395      article-display-x-face
2396      article-de-quoted-unreadable
2397      article-mime-decode-quoted-printable
2398      article-hide-list-identifiers
2399      article-hide-pgp
2400      article-strip-banner
2401      article-babel
2402      article-hide-pem
2403      article-hide-signature
2404      article-strip-headers-in-body
2405      article-remove-trailing-blank-lines
2406      article-strip-leading-blank-lines
2407      article-strip-multiple-blank-lines
2408      article-strip-leading-space
2409      article-strip-trailing-space
2410      article-strip-blank-lines
2411      article-strip-all-blank-lines
2412      article-date-local
2413      article-date-iso8601
2414      article-date-original
2415      article-date-ut
2416      article-decode-mime-words
2417      article-decode-charset
2418      article-decode-encoded-words
2419      article-date-user
2420      article-date-lapsed
2421      article-emphasize
2422      article-treat-dumbquotes
2423      article-normalize-headers
2424      (article-show-all . gnus-article-show-all-headers))))
2425 \f
2426 ;;;
2427 ;;; Gnus article mode
2428 ;;;
2429
2430 (put 'gnus-article-mode 'mode-class 'special)
2431
2432 (set-keymap-parent gnus-article-mode-map widget-keymap)
2433
2434 (gnus-define-keys gnus-article-mode-map
2435   " " gnus-article-goto-next-page
2436   "\177" gnus-article-goto-prev-page
2437   [delete] gnus-article-goto-prev-page
2438   [backspace] gnus-article-goto-prev-page
2439   "\C-c^" gnus-article-refer-article
2440   "h" gnus-article-show-summary
2441   "s" gnus-article-show-summary
2442   "\C-c\C-m" gnus-article-mail
2443   "?" gnus-article-describe-briefly
2444   "e" gnus-article-edit
2445   "<" beginning-of-buffer
2446   ">" end-of-buffer
2447   "\C-c\C-i" gnus-info-find-node
2448   "\C-c\C-b" gnus-bug
2449
2450   "\C-d" gnus-article-read-summary-keys
2451   "\M-*" gnus-article-read-summary-keys
2452   "\M-#" gnus-article-read-summary-keys
2453   "\M-^" gnus-article-read-summary-keys
2454   "\M-g" gnus-article-read-summary-keys)
2455
2456 (substitute-key-definition
2457  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2458
2459 (defun gnus-article-make-menu-bar ()
2460   (gnus-turn-off-edit-menu 'article)
2461   (unless (boundp 'gnus-article-article-menu)
2462     (easy-menu-define
2463      gnus-article-article-menu gnus-article-mode-map ""
2464      '("Article"
2465        ["Scroll forwards" gnus-article-goto-next-page t]
2466        ["Scroll backwards" gnus-article-goto-prev-page t]
2467        ["Show summary" gnus-article-show-summary t]
2468        ["Fetch Message-ID at point" gnus-article-refer-article t]
2469        ["Mail to address at point" gnus-article-mail t]
2470        ["Send a bug report" gnus-bug t]))
2471
2472     (easy-menu-define
2473      gnus-article-treatment-menu gnus-article-mode-map ""
2474      '("Treatment"
2475        ["Hide headers" gnus-article-hide-headers t]
2476        ["Hide signature" gnus-article-hide-signature t]
2477        ["Hide citation" gnus-article-hide-citation t]
2478        ["Treat overstrike" gnus-article-treat-overstrike t]
2479        ["Remove carriage return" gnus-article-remove-cr t]
2480        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
2481
2482     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2483
2484     (when (boundp 'gnus-summary-post-menu)
2485       (define-key gnus-article-mode-map [menu-bar post]
2486         (cons "Post" gnus-summary-post-menu)))
2487
2488     (gnus-run-hooks 'gnus-article-menu-hook)))
2489
2490 (defun gnus-article-mode ()
2491   "Major mode for displaying an article.
2492
2493 All normal editing commands are switched off.
2494
2495 The following commands are available in addition to all summary mode
2496 commands:
2497 \\<gnus-article-mode-map>
2498 \\[gnus-article-next-page]\t Scroll the article one page forwards
2499 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2500 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2501 \\[gnus-article-show-summary]\t Display the summary buffer
2502 \\[gnus-article-mail]\t Send a reply to the address near point
2503 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2504 \\[gnus-info-find-node]\t Go to the Gnus info node"
2505   (interactive)
2506   (when (gnus-visual-p 'article-menu 'menu)
2507     (gnus-article-make-menu-bar))
2508   (gnus-simplify-mode-line)
2509   (setq mode-name "Article")
2510   (setq major-mode 'gnus-article-mode)
2511   (make-local-variable 'minor-mode-alist)
2512   (use-local-map gnus-article-mode-map)
2513   (gnus-update-format-specifications nil 'article-mode)
2514   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2515   (make-local-variable 'gnus-page-broken)
2516   (make-local-variable 'gnus-button-marker-list)
2517   (make-local-variable 'gnus-article-current-summary)
2518   (make-local-variable 'gnus-article-mime-handles)
2519   (make-local-variable 'gnus-article-decoded-p)
2520   (make-local-variable 'gnus-article-mime-handle-alist)
2521   (make-local-variable 'gnus-article-washed-types)
2522   (gnus-set-default-directory)
2523   (buffer-disable-undo)
2524   (setq buffer-read-only t)
2525   (set-syntax-table gnus-article-mode-syntax-table)
2526   (mm-enable-multibyte)
2527   (gnus-run-hooks 'gnus-article-mode-hook))
2528
2529 (defun gnus-article-setup-buffer ()
2530   "Initialize the article buffer."
2531   (let* ((name (if gnus-single-article-buffer "*Article*"
2532                  (concat "*Article " gnus-newsgroup-name "*")))
2533          (original
2534           (progn (string-match "\\*Article" name)
2535                  (concat " *Original Article"
2536                          (substring name (match-end 0))))))
2537     (setq gnus-article-buffer name)
2538     (setq gnus-original-article-buffer original)
2539     (setq gnus-article-mime-handle-alist nil)
2540     ;; This might be a variable local to the summary buffer.
2541     (unless gnus-single-article-buffer
2542       (save-excursion
2543         (set-buffer gnus-summary-buffer)
2544         (setq gnus-article-buffer name)
2545         (setq gnus-original-article-buffer original)
2546         (gnus-set-global-variables)))
2547     (gnus-article-setup-highlight-words)
2548     ;; Init original article buffer.
2549     (save-excursion
2550       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2551       (mm-enable-multibyte)
2552       (setq major-mode 'gnus-original-article-mode)
2553       (make-local-variable 'gnus-original-article))
2554     (if (get-buffer name)
2555         (save-excursion
2556           (set-buffer name)
2557           (if gnus-article-mime-handles
2558               (mm-destroy-parts gnus-article-mime-handles))
2559           (kill-all-local-variables)
2560           (buffer-disable-undo)
2561           (setq buffer-read-only t)
2562           (unless (eq major-mode 'gnus-article-mode)
2563             (gnus-article-mode))
2564           (current-buffer))
2565       (save-excursion
2566         (set-buffer (gnus-get-buffer-create name))
2567         (gnus-article-mode)
2568         (make-local-variable 'gnus-summary-buffer)
2569         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2570         (current-buffer)))))
2571
2572 ;; Set article window start at LINE, where LINE is the number of lines
2573 ;; from the head of the article.
2574 (defun gnus-article-set-window-start (&optional line)
2575   (set-window-start
2576    (get-buffer-window gnus-article-buffer t)
2577    (save-excursion
2578      (set-buffer gnus-article-buffer)
2579      (goto-char (point-min))
2580      (if (not line)
2581          (point-min)
2582        (gnus-message 6 "Moved to bookmark")
2583        (search-forward "\n\n" nil t)
2584        (forward-line line)
2585        (point)))))
2586
2587 (defun gnus-article-prepare (article &optional all-headers header)
2588   "Prepare ARTICLE in article mode buffer.
2589 ARTICLE should either be an article number or a Message-ID.
2590 If ARTICLE is an id, HEADER should be the article headers.
2591 If ALL-HEADERS is non-nil, no headers are hidden."
2592   (save-excursion
2593     ;; Make sure we start in a summary buffer.
2594     (unless (eq major-mode 'gnus-summary-mode)
2595       (set-buffer gnus-summary-buffer))
2596     (setq gnus-summary-buffer (current-buffer))
2597     (let* ((gnus-article (if header (mail-header-number header) article))
2598            (summary-buffer (current-buffer))
2599            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2600            (group gnus-newsgroup-name)
2601            result)
2602       (save-excursion
2603         (gnus-article-setup-buffer)
2604         (set-buffer gnus-article-buffer)
2605         ;; Deactivate active regions.
2606         (when (and (boundp 'transient-mark-mode)
2607                    transient-mark-mode)
2608           (setq mark-active nil))
2609         (if (not (setq result (let ((buffer-read-only nil))
2610                                 (gnus-request-article-this-buffer
2611                                  article group))))
2612             ;; There is no such article.
2613             (save-excursion
2614               (when (and (numberp article)
2615                          (not (memq article gnus-newsgroup-sparse)))
2616                 (setq gnus-article-current
2617                       (cons gnus-newsgroup-name article))
2618                 (set-buffer gnus-summary-buffer)
2619                 (setq gnus-current-article article)
2620                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2621                     (progn
2622                       (gnus-summary-set-agent-mark article)
2623                       (message "Message marked for downloading"))
2624                   (gnus-summary-mark-article article gnus-canceled-mark)
2625                   (unless (memq article gnus-newsgroup-sparse)
2626                     (gnus-error 1
2627                      "No such article (may have expired or been canceled)")))))
2628           (if (or (eq result 'pseudo)
2629                   (eq result 'nneething))
2630               (progn
2631                 (save-excursion
2632                   (set-buffer summary-buffer)
2633                   (push article gnus-newsgroup-history)
2634                   (setq gnus-last-article gnus-current-article
2635                         gnus-current-article 0
2636                         gnus-current-headers nil
2637                         gnus-article-current nil)
2638                   (if (eq result 'nneething)
2639                       (gnus-configure-windows 'summary)
2640                     (gnus-configure-windows 'article))
2641                   (gnus-set-global-variables))
2642                 (let ((gnus-article-mime-handle-alist-1
2643                        gnus-article-mime-handle-alist))
2644                   (gnus-set-mode-line 'article)))
2645             ;; The result from the `request' was an actual article -
2646             ;; or at least some text that is now displayed in the
2647             ;; article buffer.
2648             (when (and (numberp article)
2649                        (not (eq article gnus-current-article)))
2650               ;; Seems like a new article has been selected.
2651               ;; `gnus-current-article' must be an article number.
2652               (save-excursion
2653                 (set-buffer summary-buffer)
2654                 (push article gnus-newsgroup-history)
2655                 (setq gnus-last-article gnus-current-article
2656                       gnus-current-article article
2657                       gnus-current-headers
2658                       (gnus-summary-article-header gnus-current-article)
2659                       gnus-article-current
2660                       (cons gnus-newsgroup-name gnus-current-article))
2661                 (unless (vectorp gnus-current-headers)
2662                   (setq gnus-current-headers nil))
2663                 (gnus-summary-goto-subject gnus-current-article)
2664                 (when (gnus-summary-show-thread)
2665                   ;; If the summary buffer really was folded, the
2666                   ;; previous goto may not actually have gone to
2667                   ;; the right article, but the thread root instead.
2668                   ;; So we go again.
2669                   (gnus-summary-goto-subject gnus-current-article))
2670                 (gnus-run-hooks 'gnus-mark-article-hook)
2671                 (gnus-set-mode-line 'summary)
2672                 (when (gnus-visual-p 'article-highlight 'highlight)
2673                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2674                 ;; Set the global newsgroup variables here.
2675                 (gnus-set-global-variables)
2676                 (setq gnus-have-all-headers
2677                       (or all-headers gnus-show-all-headers))))
2678             (when (or (numberp article)
2679                       (stringp article))
2680               (gnus-article-prepare-display)
2681               ;; Do page break.
2682               (goto-char (point-min))
2683               (setq gnus-page-broken
2684                     (when gnus-break-pages
2685                       (gnus-narrow-to-page)
2686                       t)))
2687             (let ((gnus-article-mime-handle-alist-1
2688                    gnus-article-mime-handle-alist))
2689               (gnus-set-mode-line 'article))
2690             (article-goto-body)
2691             (set-window-point (get-buffer-window (current-buffer)) (point))
2692             (gnus-configure-windows 'article)
2693             t))))))
2694
2695 ;;;###autoload
2696 (defun gnus-article-prepare-display ()
2697   "Make the current buffer look like a nice article."
2698   ;; Hooks for getting information from the article.
2699   ;; This hook must be called before being narrowed.
2700   (let ((gnus-article-buffer (current-buffer))
2701         buffer-read-only)
2702     (unless (eq major-mode 'gnus-article-mode)
2703       (gnus-article-mode))
2704     (setq buffer-read-only nil
2705           gnus-article-wash-types nil)
2706     (gnus-run-hooks 'gnus-tmp-internal-hook)
2707     (gnus-run-hooks 'gnus-article-prepare-hook)
2708     (when gnus-display-mime-function
2709       (funcall gnus-display-mime-function))))
2710
2711 ;;;
2712 ;;; Gnus MIME viewing functions
2713 ;;;
2714
2715 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2716   "The following specs can be used:
2717 %t  The MIME type
2718 %T  MIME type, along with additional info
2719 %n  The `name' parameter
2720 %d  The description, if any
2721 %l  The length of the encoded part
2722 %p  The part identifier number
2723 %e  Dots if the part isn't displayed")
2724
2725 (defvar gnus-mime-button-line-format-alist
2726   '((?t gnus-tmp-type ?s)
2727     (?T gnus-tmp-type-long ?s)
2728     (?n gnus-tmp-name ?s)
2729     (?d gnus-tmp-description ?s)
2730     (?p gnus-tmp-id ?s)
2731     (?l gnus-tmp-length ?d)
2732     (?e gnus-tmp-dots ?s)))
2733
2734 (defvar gnus-mime-button-commands
2735   '((gnus-article-press-button "\r" "Toggle Display")
2736     (gnus-mime-view-part "v" "View Interactively...")
2737     (gnus-mime-view-part-as-type "t" "View As Type...")
2738     (gnus-mime-save-part "o" "Save...")
2739     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2740     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2741     (gnus-mime-internalize-part "E" "View Internally")
2742     (gnus-mime-externalize-part "e" "View Externally")
2743     (gnus-mime-pipe-part "|" "Pipe To Command...")))
2744
2745 (defun gnus-article-mime-part-status ()
2746   (if gnus-article-mime-handle-alist-1
2747       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2748     ""))
2749
2750 (defvar gnus-mime-button-map nil)
2751 (unless gnus-mime-button-map
2752   (setq gnus-mime-button-map (make-sparse-keymap))
2753   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2754   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2755   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
2756   (mapcar (lambda (c)
2757             (define-key gnus-mime-button-map (cadr c) (car c)))
2758           gnus-mime-button-commands))
2759
2760 (defun gnus-mime-button-menu (event)
2761   "Construct a context-sensitive menu of MIME commands."
2762   (interactive "e")
2763   (save-excursion
2764     (let ((pos (event-start event)))
2765       (set-buffer (window-buffer (posn-window pos)))
2766       (goto-char (posn-point pos))
2767       (gnus-article-check-buffer)
2768       (let ((response (x-popup-menu
2769                        t `("MIME Part"
2770                            ("" ,@(mapcar (lambda (c)
2771                                            (cons (caddr c) (car c)))
2772                                          gnus-mime-button-commands))))))
2773         (if response
2774             (funcall response))))))
2775
2776 (defun gnus-mime-view-all-parts (&optional handles)
2777   "View all the MIME parts."
2778   (interactive)
2779   (save-current-buffer
2780     (set-buffer gnus-article-buffer)
2781     (let ((handles (or handles gnus-article-mime-handles))
2782           (mail-parse-charset gnus-newsgroup-charset)
2783           (mail-parse-ignored-charsets 
2784            (save-excursion (set-buffer gnus-summary-buffer)
2785                            gnus-newsgroup-ignored-charsets)))
2786       (if (stringp (car handles))
2787           (gnus-mime-view-all-parts (cdr handles))
2788         (mapcar 'mm-display-part handles)))))
2789
2790 (defun gnus-mime-save-part ()
2791   "Save the MIME part under point."
2792   (interactive)
2793   (gnus-article-check-buffer)
2794   (let ((data (get-text-property (point) 'gnus-data)))
2795     (mm-save-part data)))
2796
2797 (defun gnus-mime-pipe-part ()
2798   "Pipe the MIME part under point to a process."
2799   (interactive)
2800   (gnus-article-check-buffer)
2801   (let ((data (get-text-property (point) 'gnus-data)))
2802     (mm-pipe-part data)))
2803
2804 (defun gnus-mime-view-part ()
2805   "Interactively choose a viewing method for the MIME part under point."
2806   (interactive)
2807   (gnus-article-check-buffer)
2808   (let ((data (get-text-property (point) 'gnus-data)))
2809     (mm-interactively-view-part data)))
2810
2811 (defun gnus-mime-view-part-as-type ()
2812   "Choose a MIME media type, and view the part as such."
2813   (interactive
2814    (list (completing-read "View as MIME type: " mailcap-mime-types)))
2815   (gnus-article-check-buffer)
2816   (let ((handle (get-text-property (point) 'gnus-data)))
2817     (gnus-mm-display-part handle)))
2818
2819 (defun gnus-mime-copy-part (&optional handle)
2820   "Put the the MIME part under point into a new buffer."
2821   (interactive)
2822   (gnus-article-check-buffer)
2823   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2824          (contents (mm-get-part handle))|
2825          (base (file-name-nondirectory
2826                 (or
2827                  (mail-content-type-get (mm-handle-type handle) 'name)
2828                  (mail-content-type-get (mm-handle-type handle)
2829                                         'filename)
2830                  "*decoded*")))
2831          (buffer (generate-new-buffer base)))
2832     (switch-to-buffer buffer)
2833     (insert contents)
2834     ;; We do it this way to make `normal-mode' set the appropriate mode.
2835     (unwind-protect
2836         (progn
2837           (setq buffer-file-name (expand-file-name base))
2838           (normal-mode))
2839       (setq buffer-file-name nil))
2840     (goto-char (point-min))))
2841
2842 (defun gnus-mime-inline-part (&optional handle)
2843   "Insert the MIME part under point into the current buffer."
2844   (interactive)
2845   (gnus-article-check-buffer)
2846   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2847          contents
2848          (b (point))
2849          buffer-read-only)
2850     (if (mm-handle-undisplayer handle)
2851         (mm-remove-part handle)
2852       (setq contents (mm-get-part handle))
2853       (forward-line 2)
2854       (mm-insert-inline handle contents)
2855       (goto-char b))))
2856
2857 (defun gnus-mime-externalize-part (&optional handle)
2858   "View the MIME part under point with an external viewer."
2859   (interactive)
2860   (gnus-article-check-buffer)
2861   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2862          (mm-user-display-methods nil)
2863          (mm-inline-large-images nil)
2864          (mail-parse-charset gnus-newsgroup-charset)
2865          (mail-parse-ignored-charsets 
2866           (save-excursion (set-buffer gnus-summary-buffer)
2867                           gnus-newsgroup-ignored-charsets)))
2868     (if (mm-handle-undisplayer handle)
2869         (mm-remove-part handle)
2870       (mm-display-part handle))))
2871
2872 (defun gnus-mime-internalize-part (&optional handle)
2873   "View the MIME part under point with an internal viewer."
2874   (interactive)
2875   (gnus-article-check-buffer)
2876   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2877          (mm-user-display-methods '((".*" . inline)))
2878          (mm-inline-large-images t)
2879          (mail-parse-charset gnus-newsgroup-charset)
2880          (mail-parse-ignored-charsets 
2881           (save-excursion (set-buffer gnus-summary-buffer)
2882                           gnus-newsgroup-ignored-charsets)))
2883     (if (mm-handle-undisplayer handle)
2884         (mm-remove-part handle)
2885       (mm-display-part handle))))
2886
2887 (defun gnus-article-part-wrapper (n function)
2888   (save-current-buffer
2889     (set-buffer gnus-article-buffer)
2890     (when (> n (length gnus-article-mime-handle-alist))
2891       (error "No such part"))
2892     (gnus-article-goto-part n)
2893     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2894       (funcall function handle))))
2895
2896 (defun gnus-article-pipe-part (n)
2897   "Pipe MIME part N, which is the numerical prefix."
2898   (interactive "p")
2899   (gnus-article-part-wrapper n 'mm-pipe-part))
2900
2901 (defun gnus-article-save-part (n)
2902   "Save MIME part N, which is the numerical prefix."
2903   (interactive "p")
2904   (gnus-article-part-wrapper n 'mm-save-part))
2905
2906 (defun gnus-article-interactively-view-part (n)
2907   "View MIME part N interactively, which is the numerical prefix."
2908   (interactive "p")
2909   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2910
2911 (defun gnus-article-copy-part (n)
2912   "Copy MIME part N, which is the numerical prefix."
2913   (interactive "p")
2914   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2915
2916 (defun gnus-article-externalize-part (n)
2917   "View MIME part N externally, which is the numerical prefix."
2918   (interactive "p")
2919   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2920
2921 (defun gnus-article-inline-part (n)
2922   "Inline MIME part N, which is the numerical prefix."
2923   (interactive "p")
2924   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
2925
2926 (defun gnus-article-view-part (n)
2927   "View MIME part N, which is the numerical prefix."
2928   (interactive "p")
2929   (save-current-buffer
2930     (set-buffer gnus-article-buffer)
2931     (when (> n (length gnus-article-mime-handle-alist))
2932       (error "No such part"))
2933     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2934       (when (gnus-article-goto-part n)
2935         (if (equal (car handle) "multipart/alternative")
2936             (gnus-article-press-button)
2937           (when (eq (gnus-mm-display-part handle) 'internal)
2938             (gnus-set-window-start)))))))
2939
2940 (defun gnus-mm-display-part (handle)
2941   "Display HANDLE and fix MIME button."
2942   (let ((id (get-text-property (point) 'gnus-part))
2943         (point (point))
2944         buffer-read-only)
2945     (forward-line 1)
2946     (prog1
2947         (let ((window (selected-window))
2948               (mail-parse-charset gnus-newsgroup-charset)
2949               (mail-parse-ignored-charsets 
2950                (save-excursion (set-buffer gnus-summary-buffer)
2951                                gnus-newsgroup-ignored-charsets)))
2952           (save-excursion
2953             (unwind-protect
2954                 (let ((win (get-buffer-window (current-buffer) t))
2955                       (beg (point)))
2956                   (when win
2957                     (select-window win))
2958                   (goto-char point)
2959                   (forward-line)
2960                   (if (mm-handle-displayed-p handle)
2961                       ;; This will remove the part.
2962                       (mm-display-part handle)
2963                     (save-restriction
2964                       (narrow-to-region (point) (1+ (point)))
2965                       (mm-display-part handle)
2966                       ;; We narrow to the part itself and
2967                       ;; then call the treatment functions.
2968                       (goto-char (point-min))
2969                       (forward-line 1)
2970                       (narrow-to-region (point) (point-max))
2971                       (gnus-treat-article
2972                        nil id
2973                        (1- (length gnus-article-mime-handles))
2974                        (mm-handle-media-type handle)))))
2975               (select-window window))))
2976       (goto-char point)
2977       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
2978       (gnus-insert-mime-button
2979        handle id (list (mm-handle-displayed-p handle)))
2980       (goto-char point))))
2981
2982 (defun gnus-article-goto-part (n)
2983   "Go to MIME part N."
2984   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
2985     (when point
2986       (goto-char point))))
2987
2988 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
2989   (let ((gnus-tmp-name
2990          (or (mail-content-type-get (mm-handle-type handle)
2991                                     'name)
2992              (mail-content-type-get (mm-handle-disposition handle)
2993                                     'filename)
2994              ""))
2995         (gnus-tmp-type (mm-handle-media-type handle))
2996         (gnus-tmp-description
2997          (mail-decode-encoded-word-string (or (mm-handle-description handle)
2998                                               "")))
2999         (gnus-tmp-dots
3000          (if (if displayed (car displayed)
3001                (mm-handle-displayed-p handle))
3002              "" "..."))
3003         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3004                            (buffer-size)))
3005         gnus-tmp-type-long b e)
3006     (when (string-match ".*/" gnus-tmp-name)
3007       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3008     (setq gnus-tmp-type-long (concat gnus-tmp-type
3009                                      (and (not (equal gnus-tmp-name ""))
3010                                           (concat "; " gnus-tmp-name))))
3011     (or (equal gnus-tmp-description "")
3012         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3013     (unless (bolp)
3014       (insert "\n"))
3015     (setq b (point))
3016     (gnus-eval-format
3017      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3018      `(local-map ,gnus-mime-button-map
3019                  keymap ,gnus-mime-button-map
3020                  gnus-callback gnus-mm-display-part
3021                  gnus-part ,gnus-tmp-id
3022                  article-type annotation
3023                  gnus-data ,handle))
3024     (setq e (point))
3025     (widget-convert-button 'link b e
3026                            :mime-handle handle
3027                            :action 'gnus-widget-press-button
3028                            :button-keymap gnus-mime-button-map
3029                            :help-echo
3030                            (lambda (widget)
3031                              ;; Needed to properly clear the message
3032                              ;; due to a bug in wid-edit
3033                              (setq help-echo-owns-message t)
3034                              (format
3035                               "Click to %s the MIME part; %s for more options"
3036                               (if (mm-handle-displayed-p
3037                                    (widget-get widget :mime-handle))
3038                                   "hide" "show")
3039                               (if gnus-xemacs "button3" "mouse-3"))))))
3040
3041 (defun gnus-widget-press-button (elems el)
3042   (goto-char (widget-get elems :from))
3043   (gnus-article-press-button))
3044
3045 (defvar gnus-displaying-mime nil)
3046
3047 (defun gnus-display-mime (&optional ihandles)
3048   "Display the MIME parts."
3049   (save-excursion
3050     (save-selected-window
3051       (let ((window (get-buffer-window gnus-article-buffer))
3052             (point (point)))
3053         (when window
3054           (select-window window)
3055           ;; We have to do this since selecting the window
3056           ;; may change the point.  So we set the window point.
3057           (set-window-point window point)))
3058       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3059              buffer-read-only handle name type b e display)
3060         (when (and (not ihandles)
3061                    (not gnus-displaying-mime))
3062           ;; Top-level call; we clean up.
3063           (when gnus-article-mime-handles
3064             (mm-destroy-parts gnus-article-mime-handles)
3065             (setq gnus-article-mime-handle-alist nil)) ;; A trick.
3066           (setq gnus-article-mime-handles handles)
3067           ;; We allow users to glean info from the handles.
3068           (when gnus-article-mime-part-function
3069             (gnus-mime-part-function handles)))
3070         (if (and handles
3071                  (or (not (stringp (car handles)))
3072                      (cdr handles)))
3073             (progn
3074               (when (and (not ihandles)
3075                          (not gnus-displaying-mime))
3076                 ;; Clean up for mime parts.
3077                 (article-goto-body)
3078                 (delete-region (point) (point-max)))
3079               (let ((gnus-displaying-mime t))
3080                 (gnus-mime-display-part handles)))
3081           (save-restriction
3082             (article-goto-body)
3083             (narrow-to-region (point) (point-max))
3084             (gnus-treat-article nil 1 1)
3085             (widen)))
3086         (if (not ihandles)
3087             ;; Highlight the headers.
3088             (save-excursion
3089               (save-restriction
3090                 (article-goto-body)
3091                 (narrow-to-region (point-min) (point))
3092                 (gnus-treat-article 'head))))))))
3093
3094 (defvar gnus-mime-display-multipart-as-mixed nil)
3095
3096 (defun gnus-mime-display-part (handle)
3097   (cond
3098    ;; Single part.
3099    ((not (stringp (car handle)))
3100     (gnus-mime-display-single handle))
3101    ;; User-defined multipart
3102    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3103     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3104              handle))
3105    ;; multipart/alternative
3106    ((and (equal (car handle) "multipart/alternative")
3107          (not gnus-mime-display-multipart-as-mixed))
3108     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3109       (push (cons id handle) gnus-article-mime-handle-alist)
3110       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3111    ;; multipart/related
3112    ((and (equal (car handle) "multipart/related")
3113          (not gnus-mime-display-multipart-as-mixed))
3114     ;;;!!!We should find the start part, but we just default
3115     ;;;!!!to the first part.
3116     (gnus-mime-display-part (cadr handle)))
3117    ;; Other multiparts are handled like multipart/mixed.
3118    (t
3119     (gnus-mime-display-mixed (cdr handle)))))
3120
3121 (defun gnus-mime-part-function (handles)
3122   (if (stringp (car handles))
3123       (mapcar 'gnus-mime-part-function (cdr handles))
3124     (funcall gnus-article-mime-part-function handles)))
3125
3126 (defun gnus-mime-display-mixed (handles)
3127   (mapcar 'gnus-mime-display-part handles))
3128
3129 (defun gnus-mime-display-single (handle)
3130   (let ((type (mm-handle-media-type handle))
3131         (ignored gnus-ignored-mime-types)
3132         (not-attachment t)
3133         (move nil)
3134         display text)
3135     (catch 'ignored
3136       (progn
3137         (while ignored
3138           (when (string-match (pop ignored) type)
3139             (throw 'ignored nil)))
3140         (if (and (setq not-attachment
3141                        (or (not (mm-handle-disposition handle))
3142                            (equal (car (mm-handle-disposition handle))
3143                                   "inline")
3144                            (mm-attachment-override-p handle)))
3145                  (mm-automatic-display-p handle)
3146                  (or (mm-inlined-p handle)
3147                      (mm-automatic-external-display-p type)))
3148             (setq display t)
3149           (when (equal (mm-handle-media-supertype handle) "text")
3150             (setq text t)))
3151         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3152           (push (cons id handle) gnus-article-mime-handle-alist)
3153           (when (or (not display)
3154                     (not (gnus-unbuttonized-mime-type-p type)))
3155             (gnus-article-insert-newline)
3156             (gnus-insert-mime-button
3157              handle id (list (or display (and not-attachment text))))
3158             (gnus-article-insert-newline)
3159             (gnus-article-insert-newline)
3160             (setq move t)))
3161         (let ((beg (point)))
3162           (cond
3163            (display
3164             (when move
3165               (forward-line -2)
3166               (setq beg (point)))
3167             (let ((mail-parse-charset gnus-newsgroup-charset)
3168                   (mail-parse-ignored-charsets 
3169                    (save-excursion (set-buffer gnus-summary-buffer)
3170                                    gnus-newsgroup-ignored-charsets)))
3171               (mm-display-part handle t))
3172             (goto-char (point-max)))
3173            ((and text not-attachment)
3174             (when move
3175               (forward-line -2)
3176               (setq beg (point)))
3177             (gnus-article-insert-newline)
3178             (mm-insert-inline handle (mm-get-part handle))
3179             (goto-char (point-max))))
3180           ;; Do highlighting.
3181           (save-excursion
3182             (save-restriction
3183               (narrow-to-region beg (point))
3184               (gnus-treat-article
3185                nil (length gnus-article-mime-handle-alist)
3186                (1- (length gnus-article-mime-handles))
3187                (mm-handle-media-type handle)))))))))
3188
3189 (defun gnus-unbuttonized-mime-type-p (type)
3190   "Say whether TYPE is to be unbuttonized."
3191   (unless gnus-inhibit-mime-unbuttonizing
3192     (catch 'found
3193       (let ((types gnus-unbuttonized-mime-types))
3194         (while types
3195           (when (string-match (pop types) type)
3196             (throw 'found t)))))))
3197
3198 (defun gnus-article-insert-newline ()
3199   "Insert a newline, but mark it as undeletable."
3200   (gnus-put-text-property
3201    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3202
3203 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3204   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3205          (ihandles handles)
3206          (point (point))
3207          handle buffer-read-only from props begend not-pref)
3208     (save-window-excursion
3209       (save-restriction
3210         (when ibegend
3211           (narrow-to-region (car ibegend)
3212                             (or (cdr ibegend)
3213                                 (progn
3214                                   (goto-char (car ibegend))
3215                                   (forward-line 2)
3216                                   (point))))
3217           (delete-region (point-min) (point-max))
3218           (mm-remove-parts handles))
3219         (setq begend (list (point-marker)))
3220         ;; Do the toggle.
3221         (unless (setq not-pref (cadr (member preferred ihandles)))
3222           (setq not-pref (car ihandles)))
3223         (when (or ibegend
3224                   (not (gnus-unbuttonized-mime-type-p
3225                         "multipart/alternative")))
3226           (gnus-add-text-properties
3227            (setq from (point))
3228            (progn
3229              (insert (format "%d.  " id))
3230              (point))
3231            `(gnus-callback
3232              (lambda (handles)
3233                (unless ,(not ibegend)
3234                  (setq gnus-article-mime-handle-alist
3235                        ',gnus-article-mime-handle-alist))
3236                (gnus-mime-display-alternative
3237                 ',ihandles ',not-pref ',begend ,id))
3238              local-map ,gnus-mime-button-map
3239              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3240              face ,gnus-article-button-face
3241              keymap ,gnus-mime-button-map
3242              gnus-part ,id
3243              gnus-data ,handle))
3244           (widget-convert-button 'link from (point)
3245                                  :action 'gnus-widget-press-button
3246                                  :button-keymap gnus-widget-button-keymap)
3247           ;; Do the handles
3248           (while (setq handle (pop handles))
3249             (gnus-add-text-properties
3250              (setq from (point))
3251              (progn
3252                (insert (format "(%c) %-18s"
3253                                (if (equal handle preferred) ?* ? )
3254                                (mm-handle-media-type handle)))
3255                (point))
3256              `(gnus-callback
3257                (lambda (handles)
3258                  (unless ,(not ibegend)
3259                    (setq gnus-article-mime-handle-alist
3260                          ',gnus-article-mime-handle-alist))
3261                  (gnus-mime-display-alternative
3262                   ',ihandles ',handle ',begend ,id))
3263                local-map ,gnus-mime-button-map
3264                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3265                face ,gnus-article-button-face
3266                keymap ,gnus-mime-button-map
3267                gnus-part ,id
3268                gnus-data ,handle))
3269             (widget-convert-button 'link from (point)
3270                                    :action 'gnus-widget-press-button
3271                                    :button-keymap gnus-widget-button-keymap)
3272             (insert "  "))
3273           (insert "\n\n"))
3274         (when preferred
3275           (if (stringp (car preferred))
3276               (gnus-display-mime preferred)
3277             (let ((mail-parse-charset gnus-newsgroup-charset)
3278                   (mail-parse-ignored-charsets 
3279                    (save-excursion (set-buffer gnus-summary-buffer)
3280                                    gnus-newsgroup-ignored-charsets)))
3281               (mm-display-part preferred)
3282               ;; Do highlighting.
3283               (save-excursion
3284                 (save-restriction
3285                   (narrow-to-region (car begend) (point-max))
3286                   (gnus-treat-article
3287                    nil (length gnus-article-mime-handle-alist)
3288                    (1- (length gnus-article-mime-handles))
3289                    (mm-handle-media-type handle))))))
3290           (goto-char (point-max))
3291           (setcdr begend (point-marker)))))
3292     (when ibegend
3293       (goto-char point))))
3294
3295 (defun gnus-article-wash-status ()
3296   "Return a string which display status of article washing."
3297   (save-excursion
3298     (set-buffer gnus-article-buffer)
3299     (let ((cite (memq 'cite gnus-article-wash-types))
3300           (headers (memq 'headers gnus-article-wash-types))
3301           (boring (memq 'boring-headers gnus-article-wash-types))
3302           (pgp (memq 'pgp gnus-article-wash-types))
3303           (pem (memq 'pem gnus-article-wash-types))
3304           (signature (memq 'signature gnus-article-wash-types))
3305           (overstrike (memq 'overstrike gnus-article-wash-types))
3306           (emphasis (memq 'emphasis gnus-article-wash-types)))
3307       (format "%c%c%c%c%c%c"
3308               (if cite ?c ? )
3309               (if (or headers boring) ?h ? )
3310               (if (or pgp pem) ?p ? )
3311               (if signature ?s ? )
3312               (if overstrike ?o ? )
3313               (if emphasis ?e ? )))))
3314
3315 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3316
3317 (defun gnus-article-maybe-hide-headers ()
3318   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3319 Provided for backwards compatibility."
3320   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3321                  (not (save-excursion (set-buffer gnus-summary-buffer)
3322                                       gnus-have-all-headers)))
3323              (not gnus-inhibit-hiding))
3324     (gnus-article-hide-headers)))
3325
3326 ;;; Article savers.
3327
3328 (defun gnus-output-to-file (file-name)
3329   "Append the current article to a file named FILE-NAME."
3330   (let ((artbuf (current-buffer)))
3331     (with-temp-buffer
3332       (insert-buffer-substring artbuf)
3333       ;; Append newline at end of the buffer as separator, and then
3334       ;; save it to file.
3335       (goto-char (point-max))
3336       (insert "\n")
3337       (mm-append-to-file (point-min) (point-max) file-name)
3338       t)))
3339
3340 (defun gnus-narrow-to-page (&optional arg)
3341   "Narrow the article buffer to a page.
3342 If given a numerical ARG, move forward ARG pages."
3343   (interactive "P")
3344   (setq arg (if arg (prefix-numeric-value arg) 0))
3345   (save-excursion
3346     (set-buffer gnus-article-buffer)
3347     (goto-char (point-min))
3348     (widen)
3349     ;; Remove any old next/prev buttons.
3350     (when (gnus-visual-p 'page-marker)
3351       (let ((buffer-read-only nil))
3352         (gnus-remove-text-with-property 'gnus-prev)
3353         (gnus-remove-text-with-property 'gnus-next)))
3354     (when
3355         (cond ((< arg 0)
3356                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3357               ((> arg 0)
3358                (re-search-forward page-delimiter nil 'move arg)))
3359       (goto-char (match-end 0)))
3360     (narrow-to-region
3361      (point)
3362      (if (re-search-forward page-delimiter nil 'move)
3363          (match-beginning 0)
3364        (point)))
3365     (when (and (gnus-visual-p 'page-marker)
3366                (not (= (point-min) 1)))
3367       (save-excursion
3368         (goto-char (point-min))
3369         (gnus-insert-prev-page-button)))
3370     (when (and (gnus-visual-p 'page-marker)
3371                (< (+ (point-max) 2) (buffer-size)))
3372       (save-excursion
3373         (goto-char (point-max))
3374         (gnus-insert-next-page-button)))))
3375
3376 ;; Article mode commands
3377
3378 (defun gnus-article-goto-next-page ()
3379   "Show the next page of the article."
3380   (interactive)
3381   (when (gnus-article-next-page)
3382     (goto-char (point-min))
3383     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3384
3385 (defun gnus-article-goto-prev-page ()
3386   "Show the next page of the article."
3387   (interactive)
3388   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3389     (gnus-article-prev-page nil)))
3390
3391 (defun gnus-article-next-page (&optional lines)
3392   "Show the next page of the current article.
3393 If end of article, return non-nil.  Otherwise return nil.
3394 Argument LINES specifies lines to be scrolled up."
3395   (interactive "p")
3396   (move-to-window-line -1)
3397   (if (save-excursion
3398         (end-of-line)
3399         (and (pos-visible-in-window-p)  ;Not continuation line.
3400              (eobp)))
3401       ;; Nothing in this page.
3402       (if (or (not gnus-page-broken)
3403               (save-excursion
3404                 (save-restriction
3405                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3406           t                             ;Nothing more.
3407         (gnus-narrow-to-page 1)         ;Go to next page.
3408         nil)
3409     ;; More in this page.
3410     (let ((scroll-in-place nil))
3411       (condition-case ()
3412           (scroll-up lines)
3413         (end-of-buffer
3414          ;; Long lines may cause an end-of-buffer error.
3415          (goto-char (point-max)))))
3416     (move-to-window-line 0)
3417     nil))
3418
3419 (defun gnus-article-prev-page (&optional lines)
3420   "Show previous page of current article.
3421 Argument LINES specifies lines to be scrolled down."
3422   (interactive "p")
3423   (move-to-window-line 0)
3424   (if (and gnus-page-broken
3425            (bobp)
3426            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3427       (progn
3428         (gnus-narrow-to-page -1)        ;Go to previous page.
3429         (goto-char (point-max))
3430         (recenter -1))
3431     (let ((scroll-in-place nil))
3432       (prog1
3433           (condition-case ()
3434               (scroll-down lines)
3435             (beginning-of-buffer
3436              (goto-char (point-min))))
3437         (move-to-window-line 0)))))
3438
3439 (defun gnus-article-refer-article ()
3440   "Read article specified by message-id around point."
3441   (interactive)
3442   (let ((point (point)))
3443     (search-forward ">" nil t)          ;Move point to end of "<....>".
3444     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3445         (let ((message-id (match-string 1)))
3446           (goto-char point)
3447           (set-buffer gnus-summary-buffer)
3448           (gnus-summary-refer-article message-id))
3449       (goto-char (point))
3450       (error "No references around point"))))
3451
3452 (defun gnus-article-show-summary ()
3453   "Reconfigure windows to show summary buffer."
3454   (interactive)
3455   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3456       (error "There is no summary buffer for this article buffer")
3457     (gnus-article-set-globals)
3458     (gnus-configure-windows 'article)
3459     (gnus-summary-goto-subject gnus-current-article)
3460     (gnus-summary-position-point)))
3461
3462 (defun gnus-article-describe-briefly ()
3463   "Describe article mode commands briefly."
3464   (interactive)
3465   (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")))
3466
3467 (defun gnus-article-summary-command ()
3468   "Execute the last keystroke in the summary buffer."
3469   (interactive)
3470   (let ((obuf (current-buffer))
3471         (owin (current-window-configuration))
3472         func)
3473     (switch-to-buffer gnus-article-current-summary 'norecord)
3474     (setq func (lookup-key (current-local-map) (this-command-keys)))
3475     (call-interactively func)
3476     (set-buffer obuf)
3477     (set-window-configuration owin)
3478     (set-window-point (get-buffer-window (current-buffer)) (point))))
3479
3480 (defun gnus-article-summary-command-nosave ()
3481   "Execute the last keystroke in the summary buffer."
3482   (interactive)
3483   (let (func)
3484     (pop-to-buffer gnus-article-current-summary 'norecord)
3485     (setq func (lookup-key (current-local-map) (this-command-keys)))
3486     (call-interactively func)))
3487
3488 (defun gnus-article-check-buffer ()
3489   "Beep if not in an article buffer."
3490   (unless (equal major-mode 'gnus-article-mode)
3491     (error "Command invoked outside of a Gnus article buffer")))
3492
3493 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3494   "Read a summary buffer key sequence and execute it from the article buffer."
3495   (interactive "P")
3496   (gnus-article-check-buffer)
3497   (let ((nosaves
3498          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3499            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3500            "=" "^" "\M-^" "|"))
3501         (nosave-but-article
3502          '("A\r"))
3503         (nosave-in-article
3504          '("\C-d"))
3505         (up-to-top
3506          '("n" "Gn" "p" "Gp"))
3507         keys new-sum-point)
3508     (save-excursion
3509       (set-buffer gnus-article-current-summary)
3510       (let (gnus-pick-mode)
3511         (push (or key last-command-event) unread-command-events)
3512         (setq keys (if gnus-xemacs
3513                        (events-to-keys (read-key-sequence nil))
3514                      (read-key-sequence nil)))))
3515                      
3516     (message "")
3517
3518     (if (or (member keys nosaves)
3519             (member keys nosave-but-article)
3520             (member keys nosave-in-article))
3521         (let (func)
3522           (save-window-excursion
3523             (pop-to-buffer gnus-article-current-summary 'norecord)
3524             ;; We disable the pick minor mode commands.
3525             (let (gnus-pick-mode)
3526               (setq func (lookup-key (current-local-map) keys))))
3527           (if (not func)
3528               (ding)
3529             (unless (member keys nosave-in-article)
3530               (set-buffer gnus-article-current-summary))
3531             (call-interactively func)
3532             (setq new-sum-point (point)))
3533           (when (member keys nosave-but-article)
3534             (pop-to-buffer gnus-article-buffer 'norecord)))
3535       ;; These commands should restore window configuration.
3536       (let ((obuf (current-buffer))
3537             (owin (current-window-configuration))
3538             (opoint (point))
3539             (summary gnus-article-current-summary)
3540             func in-buffer selected)
3541         (if not-restore-window
3542             (pop-to-buffer summary 'norecord)
3543           (switch-to-buffer summary 'norecord))
3544         (setq in-buffer (current-buffer))
3545         ;; We disable the pick minor mode commands.
3546         (if (setq func (let (gnus-pick-mode)
3547                          (lookup-key (current-local-map) keys)))
3548             (progn
3549               (call-interactively func)
3550               (setq new-sum-point (point)))
3551           (ding))
3552         (when (eq in-buffer (current-buffer))
3553           (setq selected (gnus-summary-select-article))
3554           (set-buffer obuf)
3555           (unless not-restore-window
3556             (set-window-configuration owin))
3557           (when (eq selected 'old)
3558             (article-goto-body)
3559             (set-window-start (get-buffer-window (current-buffer))
3560                               1)
3561             (set-window-point (get-buffer-window (current-buffer))
3562                               (point)))
3563           (let ((win (get-buffer-window gnus-article-current-summary)))
3564             (when win
3565               (set-window-point win new-sum-point))))))))
3566
3567 (defun gnus-article-hide (&optional arg force)
3568   "Hide all the gruft in the current article.
3569 This means that PGP stuff, signatures, cited text and (some)
3570 headers will be hidden.
3571 If given a prefix, show the hidden text instead."
3572   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3573   (gnus-article-hide-headers arg)
3574   (gnus-article-hide-list-identifiers arg)
3575   (gnus-article-hide-pgp arg)
3576   (gnus-article-hide-citation-maybe arg force)
3577   (gnus-article-hide-signature arg))
3578
3579 (defun gnus-article-maybe-highlight ()
3580   "Do some article highlighting if article highlighting is requested."
3581   (when (gnus-visual-p 'article-highlight 'highlight)
3582     (gnus-article-highlight-some)))
3583
3584 (defun gnus-check-group-server ()
3585   ;; Make sure the connection to the server is alive.
3586   (unless (gnus-server-opened
3587            (gnus-find-method-for-group gnus-newsgroup-name))
3588     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3589     (gnus-request-group gnus-newsgroup-name t)))
3590
3591 (defun gnus-request-article-this-buffer (article group)
3592   "Get an article and insert it into this buffer."
3593   (let (do-update-line sparse-header)
3594     (prog1
3595         (save-excursion
3596           (erase-buffer)
3597           (gnus-kill-all-overlays)
3598           (setq group (or group gnus-newsgroup-name))
3599
3600           ;; Using `gnus-request-article' directly will insert the article into
3601           ;; `nntp-server-buffer' - so we'll save some time by not having to
3602           ;; copy it from the server buffer into the article buffer.
3603
3604           ;; We only request an article by message-id when we do not have the
3605           ;; headers for it, so we'll have to get those.
3606           (when (stringp article)
3607             (let ((gnus-override-method gnus-refer-article-method))
3608               (gnus-read-header article)))
3609
3610           ;; If the article number is negative, that means that this article
3611           ;; doesn't belong in this newsgroup (possibly), so we find its
3612           ;; message-id and request it by id instead of number.
3613           (when (and (numberp article)
3614                      gnus-summary-buffer
3615                      (get-buffer gnus-summary-buffer)
3616                      (gnus-buffer-exists-p gnus-summary-buffer))
3617             (save-excursion
3618               (set-buffer gnus-summary-buffer)
3619               (let ((header (gnus-summary-article-header article)))
3620                 (when (< article 0)
3621                   (cond
3622                    ((memq article gnus-newsgroup-sparse)
3623                     ;; This is a sparse gap article.
3624                     (setq do-update-line article)
3625                     (setq article (mail-header-id header))
3626                     (let ((gnus-override-method gnus-refer-article-method))
3627                       (setq sparse-header (gnus-read-header article)))
3628                     (setq gnus-newsgroup-sparse
3629                           (delq article gnus-newsgroup-sparse)))
3630                    ((vectorp header)
3631                     ;; It's a real article.
3632                     (setq article (mail-header-id header)))
3633                    (t
3634                     ;; It is an extracted pseudo-article.
3635                     (setq article 'pseudo)
3636                     (gnus-request-pseudo-article header))))
3637
3638                 (let ((method (gnus-find-method-for-group
3639                                gnus-newsgroup-name)))
3640                   (when (and (eq (car method) 'nneething)
3641                              (vectorp header))
3642                     (let ((dir (concat
3643                                 (file-name-as-directory
3644                                  (or (cadr (assq 'nneething-address method))
3645                                      (nth 1 method)))
3646                                 (mail-header-subject header))))
3647                       (when (file-directory-p dir)
3648                         (setq article 'nneething)
3649                         (gnus-group-enter-directory dir))))))))
3650
3651           (cond
3652            ;; Refuse to select canceled articles.
3653            ((and (numberp article)
3654                  gnus-summary-buffer
3655                  (get-buffer gnus-summary-buffer)
3656                  (gnus-buffer-exists-p gnus-summary-buffer)
3657                  (eq (cdr (save-excursion
3658                             (set-buffer gnus-summary-buffer)
3659                             (assq article gnus-newsgroup-reads)))
3660                      gnus-canceled-mark))
3661             nil)
3662            ;; We first check `gnus-original-article-buffer'.
3663            ((and (get-buffer gnus-original-article-buffer)
3664                  (numberp article)
3665                  (save-excursion
3666                    (set-buffer gnus-original-article-buffer)
3667                    (and (equal (car gnus-original-article) group)
3668                         (eq (cdr gnus-original-article) article))))
3669             (insert-buffer-substring gnus-original-article-buffer)
3670             'article)
3671            ;; Check the backlog.
3672            ((and gnus-keep-backlog
3673                  (gnus-backlog-request-article group article (current-buffer)))
3674             'article)
3675            ;; Check asynchronous pre-fetch.
3676            ((gnus-async-request-fetched-article group article (current-buffer))
3677             (gnus-async-prefetch-next group article gnus-summary-buffer)
3678             (when (and (numberp article) gnus-keep-backlog)
3679               (gnus-backlog-enter-article group article (current-buffer)))
3680             'article)
3681            ;; Check the cache.
3682            ((and gnus-use-cache
3683                  (numberp article)
3684                  (gnus-cache-request-article article group))
3685             'article)
3686            ;; Get the article and put into the article buffer.
3687            ((or (stringp article) (numberp article))
3688             (let ((gnus-override-method
3689                    (and (stringp article) gnus-refer-article-method))
3690                   (buffer-read-only nil))
3691               (erase-buffer)
3692               (gnus-kill-all-overlays)
3693               (gnus-check-group-server)
3694               (when (gnus-request-article article group (current-buffer))
3695                 (when (numberp article)
3696                   (gnus-async-prefetch-next group article gnus-summary-buffer)
3697                   (when gnus-keep-backlog
3698                     (gnus-backlog-enter-article
3699                      group article (current-buffer))))
3700                 'article)))
3701            ;; It was a pseudo.
3702            (t article)))
3703
3704       ;; Associate this article with the current summary buffer.
3705       (setq gnus-article-current-summary gnus-summary-buffer)
3706
3707       ;; Take the article from the original article buffer
3708       ;; and place it in the buffer it's supposed to be in.
3709       (when (and (get-buffer gnus-article-buffer)
3710                  (equal (buffer-name (current-buffer))
3711                         (buffer-name (get-buffer gnus-article-buffer))))
3712         (save-excursion
3713           (if (get-buffer gnus-original-article-buffer)
3714               (set-buffer gnus-original-article-buffer)
3715             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3716             (buffer-disable-undo)
3717             (setq major-mode 'gnus-original-article-mode)
3718             (setq buffer-read-only t))
3719           (let (buffer-read-only)
3720             (erase-buffer)
3721             (insert-buffer-substring gnus-article-buffer))
3722           (setq gnus-original-article (cons group article)))
3723
3724         ;; Decode charsets.
3725         (run-hooks 'gnus-article-decode-hook)
3726         ;; Mark article as decoded or not.
3727         (setq gnus-article-decoded-p gnus-article-decode-hook))
3728
3729       ;; Update sparse articles.
3730       (when (and do-update-line
3731                  (or (numberp article)
3732                      (stringp article)))
3733         (let ((buf (current-buffer)))
3734           (set-buffer gnus-summary-buffer)
3735           (gnus-summary-update-article do-update-line sparse-header)
3736           (gnus-summary-goto-subject do-update-line nil t)
3737           (set-window-point (get-buffer-window (current-buffer) t)
3738                             (point))
3739           (set-buffer buf))))))
3740
3741 ;;;
3742 ;;; Article editing
3743 ;;;
3744
3745 (defcustom gnus-article-edit-mode-hook nil
3746   "Hook run in article edit mode buffers."
3747   :group 'gnus-article-various
3748   :type 'hook)
3749
3750 (defvar gnus-article-edit-done-function nil)
3751
3752 (defvar gnus-article-edit-mode-map nil)
3753
3754 ;; Should we be using derived.el for this?
3755 (unless gnus-article-edit-mode-map
3756   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3757   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3758
3759   (gnus-define-keys gnus-article-edit-mode-map
3760     "\C-c\C-c" gnus-article-edit-done
3761     "\C-c\C-k" gnus-article-edit-exit)
3762
3763   (gnus-define-keys (gnus-article-edit-wash-map
3764                      "\C-c\C-w" gnus-article-edit-mode-map)
3765     "f" gnus-article-edit-full-stops))
3766
3767 (defun gnus-article-edit-mode ()
3768   "Major mode for editing articles.
3769 This is an extended text-mode.
3770
3771 \\{gnus-article-edit-mode-map}"
3772   (interactive)
3773   (setq major-mode 'gnus-article-edit-mode)
3774   (setq mode-name "Article Edit")
3775   (use-local-map gnus-article-edit-mode-map)
3776   (make-local-variable 'gnus-article-edit-done-function)
3777   (make-local-variable 'gnus-prev-winconf)
3778   (setq buffer-read-only nil)
3779   (buffer-enable-undo)
3780   (widen)
3781   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3782
3783 (defun gnus-article-edit (&optional force)
3784   "Edit the current article.
3785 This will have permanent effect only in mail groups.
3786 If FORCE is non-nil, allow editing of articles even in read-only
3787 groups."
3788   (interactive "P")
3789   (when (and (not force)
3790              (gnus-group-read-only-p))
3791     (error "The current newsgroup does not support article editing"))
3792   (gnus-article-date-original)
3793   (gnus-article-edit-article
3794    'ignore
3795    `(lambda (no-highlight)
3796       'ignore
3797       (gnus-summary-edit-article-done
3798        ,(or (mail-header-references gnus-current-headers) "")
3799        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3800
3801 (defun gnus-article-edit-article (start-func exit-func)
3802   "Start editing the contents of the current article buffer."
3803   (let ((winconf (current-window-configuration)))
3804     (set-buffer gnus-article-buffer)
3805     (gnus-article-edit-mode)
3806     (funcall start-func)
3807     (gnus-configure-windows 'edit-article)
3808     (setq gnus-article-edit-done-function exit-func)
3809     (setq gnus-prev-winconf winconf)
3810     (gnus-message 6 "C-c C-c to end edits")))
3811
3812 (defun gnus-article-edit-done (&optional arg)
3813   "Update the article edits and exit."
3814   (interactive "P")
3815   (save-excursion
3816     (save-restriction
3817       (widen)
3818       (when (article-goto-body)
3819         (let ((lines (count-lines (point) (point-max)))
3820               (length (- (point-max) (point)))
3821               (case-fold-search t)
3822               (body (copy-marker (point))))
3823           (goto-char (point-min))
3824           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3825             (delete-region (match-beginning 1) (match-end 1))
3826             (insert (number-to-string length)))
3827           (goto-char (point-min))
3828           (when (re-search-forward
3829                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3830             (delete-region (match-beginning 1) (match-end 1))
3831             (insert (number-to-string length)))
3832           (goto-char (point-min))
3833           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3834             (delete-region (match-beginning 1) (match-end 1))
3835             (insert (number-to-string lines)))))))
3836   (let ((func gnus-article-edit-done-function)
3837         (buf (current-buffer))
3838         (start (window-start)))
3839     (gnus-article-edit-exit)
3840     (save-excursion
3841       (set-buffer buf)
3842       (let ((buffer-read-only nil))
3843         (funcall func arg))
3844       ;; The cache and backlog have to be flushed somewhat.
3845       (when gnus-keep-backlog
3846         (gnus-backlog-remove-article
3847          (car gnus-article-current) (cdr gnus-article-current)))
3848       ;; Flush original article as well.
3849       (save-excursion
3850         (when (get-buffer gnus-original-article-buffer)
3851           (set-buffer gnus-original-article-buffer)
3852           (setq gnus-original-article nil)))
3853       (when gnus-use-cache
3854         (gnus-cache-update-article
3855          (car gnus-article-current) (cdr gnus-article-current))))
3856     (set-buffer buf)
3857     (set-window-start (get-buffer-window buf) start)
3858     (set-window-point (get-buffer-window buf) (point))))
3859
3860 (defun gnus-article-edit-exit ()
3861   "Exit the article editing without updating."
3862   (interactive)
3863   ;; We remove all text props from the article buffer.
3864   (let ((buf (format "%s" (buffer-string)))
3865         (curbuf (current-buffer))
3866         (p (point))
3867         (window-start (window-start)))
3868     (erase-buffer)
3869     (insert buf)
3870     (let ((winconf gnus-prev-winconf))
3871       (gnus-article-mode)
3872       (set-window-configuration winconf)
3873       ;; Tippy-toe some to make sure that point remains where it was.
3874       (save-current-buffer
3875         (set-buffer curbuf)
3876         (set-window-start (get-buffer-window (current-buffer)) window-start)
3877         (goto-char p)))))
3878
3879 (defun gnus-article-edit-full-stops ()
3880   "Interactively repair spacing at end of sentences."
3881   (interactive)
3882   (save-excursion
3883     (goto-char (point-min))
3884     (search-forward-regexp "^$" nil t)
3885     (let ((case-fold-search nil))
3886       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3887
3888 ;;;
3889 ;;; Article highlights
3890 ;;;
3891
3892 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
3893
3894 ;;; Internal Variables:
3895
3896 (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\\)"
3897   "Regular expression that matches URLs."
3898   :group 'gnus-article-buttons
3899   :type 'regexp)
3900
3901 (defcustom gnus-button-alist
3902   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
3903      0 t gnus-button-message-id 2)
3904     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
3905     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
3906      1 t
3907      gnus-button-fetch-group 4)
3908     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
3909     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
3910      t gnus-button-message-id 3)
3911     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
3912     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
3913     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
3914     ;; This is how URLs _should_ be embedded in text...
3915     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
3916     ;; Raw URLs.
3917     (,gnus-button-url-regexp 0 t gnus-button-url 0))
3918   "*Alist of regexps matching buttons in article bodies.
3919
3920 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
3921 REGEXP: is the string matching text around the button,
3922 BUTTON: is the number of the regexp grouping actually matching the button,
3923 FORM: is a lisp expression which must eval to true for the button to
3924 be added,
3925 CALLBACK: is the function to call when the user push this button, and each
3926 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
3927
3928 CALLBACK can also be a variable, in that case the value of that
3929 variable it the real callback function."
3930   :group 'gnus-article-buttons
3931   :type '(repeat (list regexp
3932                        (integer :tag "Button")
3933                        (sexp :tag "Form")
3934                        (function :tag "Callback")
3935                        (repeat :tag "Par"
3936                                :inline t
3937                                (integer :tag "Regexp group")))))
3938
3939 (defcustom gnus-header-button-alist
3940   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
3941      0 t gnus-button-message-id 0)
3942     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
3943     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
3944      0 t gnus-button-mailto 0)
3945     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3946     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3947     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3948     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
3949      gnus-button-message-id 3))
3950   "*Alist of headers and regexps to match buttons in article heads.
3951
3952 This alist is very similar to `gnus-button-alist', except that each
3953 alist has an additional HEADER element first in each entry:
3954
3955 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
3956
3957 HEADER is a regexp to match a header.  For a fuller explanation, see
3958 `gnus-button-alist'."
3959   :group 'gnus-article-buttons
3960   :group 'gnus-article-headers
3961   :type '(repeat (list (regexp :tag "Header")
3962                        regexp
3963                        (integer :tag "Button")
3964                        (sexp :tag "Form")
3965                        (function :tag "Callback")
3966                        (repeat :tag "Par"
3967                                :inline t
3968                                (integer :tag "Regexp group")))))
3969
3970 (defvar gnus-button-regexp nil)
3971 (defvar gnus-button-marker-list nil)
3972 ;; Regexp matching any of the regexps from `gnus-button-alist'.
3973
3974 (defvar gnus-button-last nil)
3975 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
3976
3977 ;;; Commands:
3978
3979 (defun gnus-article-push-button (event)
3980   "Check text under the mouse pointer for a callback function.
3981 If the text under the mouse pointer has a `gnus-callback' property,
3982 call it with the value of the `gnus-data' text property."
3983   (interactive "e")
3984   (set-buffer (window-buffer (posn-window (event-start event))))
3985   (let* ((pos (posn-point (event-start event)))
3986          (data (get-text-property pos 'gnus-data))
3987          (fun (get-text-property pos 'gnus-callback)))
3988     (goto-char pos)
3989     (when fun
3990       (funcall fun data))))
3991
3992 (defun gnus-article-press-button ()
3993   "Check text at point for a callback function.
3994 If the text at point has a `gnus-callback' property,
3995 call it with the value of the `gnus-data' text property."
3996   (interactive)
3997   (let* ((data (get-text-property (point) 'gnus-data))
3998          (fun (get-text-property (point) 'gnus-callback)))
3999     (when fun
4000       (funcall fun data))))
4001
4002 (defun gnus-article-highlight (&optional force)
4003   "Highlight current article.
4004 This function calls `gnus-article-highlight-headers',
4005 `gnus-article-highlight-citation',
4006 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4007 do the highlighting.  See the documentation for those functions."
4008   (interactive (list 'force))
4009   (gnus-article-highlight-headers)
4010   (gnus-article-highlight-citation force)
4011   (gnus-article-highlight-signature)
4012   (gnus-article-add-buttons force)
4013   (gnus-article-add-buttons-to-head))
4014
4015 (defun gnus-article-highlight-some (&optional force)
4016   "Highlight current article.
4017 This function calls `gnus-article-highlight-headers',
4018 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4019 do the highlighting.  See the documentation for those functions."
4020   (interactive (list 'force))
4021   (gnus-article-highlight-headers)
4022   (gnus-article-highlight-signature)
4023   (gnus-article-add-buttons))
4024
4025 (defun gnus-article-highlight-headers ()
4026   "Highlight article headers as specified by `gnus-header-face-alist'."
4027   (interactive)
4028   (save-excursion
4029     (set-buffer gnus-article-buffer)
4030     (save-restriction
4031       (let ((alist gnus-header-face-alist)
4032             (buffer-read-only nil)
4033             (case-fold-search t)
4034             (inhibit-point-motion-hooks t)
4035             entry regexp header-face field-face from hpoints fpoints)
4036         (article-narrow-to-head)
4037         (while (setq entry (pop alist))
4038           (goto-char (point-min))
4039           (setq regexp (concat "^\\("
4040                                (if (string-equal "" (nth 0 entry))
4041                                    "[^\t ]"
4042                                  (nth 0 entry))
4043                                "\\)")
4044                 header-face (nth 1 entry)
4045                 field-face (nth 2 entry))
4046           (while (and (re-search-forward regexp nil t)
4047                       (not (eobp)))
4048             (beginning-of-line)
4049             (setq from (point))
4050             (unless (search-forward ":" nil t)
4051               (forward-char 1))
4052             (when (and header-face
4053                        (not (memq (point) hpoints)))
4054               (push (point) hpoints)
4055               (gnus-put-text-property from (point) 'face header-face))
4056             (when (and field-face
4057                        (not (memq (setq from (point)) fpoints)))
4058               (push from fpoints)
4059               (if (re-search-forward "^[^ \t]" nil t)
4060                   (forward-char -2)
4061                 (goto-char (point-max)))
4062               (gnus-put-text-property from (point) 'face field-face))))))))
4063
4064 (defun gnus-article-highlight-signature ()
4065   "Highlight the signature in an article.
4066 It does this by highlighting everything after
4067 `gnus-signature-separator' using `gnus-signature-face'."
4068   (interactive)
4069   (save-excursion
4070     (set-buffer gnus-article-buffer)
4071     (let ((buffer-read-only nil)
4072           (inhibit-point-motion-hooks t))
4073       (save-restriction
4074         (when (and gnus-signature-face
4075                    (gnus-article-narrow-to-signature))
4076           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4077                             'face gnus-signature-face)
4078           (widen)
4079           (gnus-article-search-signature)
4080           (let ((start (match-beginning 0))
4081                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4082             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4083                                      end)))))))
4084
4085 (defun gnus-button-in-region-p (b e prop)
4086   "Say whether PROP exists in the region."
4087   (text-property-not-all b e prop nil))
4088
4089 (defun gnus-article-add-buttons (&optional force)
4090   "Find external references in the article and make buttons of them.
4091 \"External references\" are things like Message-IDs and URLs, as
4092 specified by `gnus-button-alist'."
4093   (interactive (list 'force))
4094   (save-excursion
4095     (set-buffer gnus-article-buffer)
4096     (let ((buffer-read-only nil)
4097           (inhibit-point-motion-hooks t)
4098           (case-fold-search t)
4099           (alist gnus-button-alist)
4100           beg entry regexp)
4101       ;; Remove all old markers.
4102       (let (marker entry new-list)
4103         (while (setq marker (pop gnus-button-marker-list))
4104           (if (or (< marker (point-min)) (>= marker (point-max)))
4105               (push marker new-list)
4106             (goto-char marker)
4107             (when (setq entry (gnus-button-entry))
4108               (put-text-property (match-beginning (nth 1 entry))
4109                                  (match-end (nth 1 entry))
4110                                  'gnus-callback nil))
4111             (set-marker marker nil)))
4112         (setq gnus-button-marker-list new-list))
4113       ;; We skip the headers.
4114       (article-goto-body)
4115       (setq beg (point))
4116       (while (setq entry (pop alist))
4117         (setq regexp (car entry))
4118         (goto-char beg)
4119         (while (re-search-forward regexp nil t)
4120           (let* ((start (and entry (match-beginning (nth 1 entry))))
4121                  (end (and entry (match-end (nth 1 entry))))
4122                  (from (match-beginning 0)))
4123             (when (and (or (eq t (nth 2 entry))
4124                            (eval (nth 2 entry)))
4125                        (not (gnus-button-in-region-p
4126                              start end 'gnus-callback)))
4127               ;; That optional form returned non-nil, so we add the
4128               ;; button.
4129               (gnus-article-add-button
4130                start end 'gnus-button-push
4131                (car (push (set-marker (make-marker) from)
4132                           gnus-button-marker-list))))))))))
4133
4134 ;; Add buttons to the head of an article.
4135 (defun gnus-article-add-buttons-to-head ()
4136   "Add buttons to the head of the article."
4137   (interactive)
4138   (save-excursion
4139     (set-buffer gnus-article-buffer)
4140     (save-restriction
4141       (let ((buffer-read-only nil)
4142             (inhibit-point-motion-hooks t)
4143             (case-fold-search t)
4144             (alist gnus-header-button-alist)
4145             entry beg end)
4146         (article-narrow-to-head)
4147         (while alist
4148           ;; Each alist entry.
4149           (setq entry (car alist)
4150                 alist (cdr alist))
4151           (goto-char (point-min))
4152           (while (re-search-forward (car entry) nil t)
4153             ;; Each header matching the entry.
4154             (setq beg (match-beginning 0))
4155             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4156                                (match-beginning 0))
4157                           (point-max)))
4158             (goto-char beg)
4159             (while (re-search-forward (nth 1 entry) end t)
4160               ;; Each match within a header.
4161               (let* ((entry (cdr entry))
4162                      (start (match-beginning (nth 1 entry)))
4163                      (end (match-end (nth 1 entry)))
4164                      (form (nth 2 entry)))
4165                 (goto-char (match-end 0))
4166                 (when (eval form)
4167                   (gnus-article-add-button
4168                    start end (nth 3 entry)
4169                    (buffer-substring (match-beginning (nth 4 entry))
4170                                      (match-end (nth 4 entry)))))))
4171             (goto-char end)))))))
4172
4173 ;;; External functions:
4174
4175 (defun gnus-article-add-button (from to fun &optional data)
4176   "Create a button between FROM and TO with callback FUN and data DATA."
4177   (when gnus-article-button-face
4178     (gnus-overlay-put (gnus-make-overlay from to)
4179                       'face gnus-article-button-face))
4180   (gnus-add-text-properties
4181    from to
4182    (nconc (and gnus-article-mouse-face
4183                (list gnus-mouse-face-prop gnus-article-mouse-face))
4184           (list 'gnus-callback fun)
4185           (and data (list 'gnus-data data))))
4186   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4187                          :button-keymap gnus-widget-button-keymap))
4188
4189 ;;; Internal functions:
4190
4191 (defun gnus-article-set-globals ()
4192   (save-excursion
4193     (set-buffer gnus-summary-buffer)
4194     (gnus-set-global-variables)))
4195
4196 (defun gnus-signature-toggle (end)
4197   (save-excursion
4198     (set-buffer gnus-article-buffer)
4199     (let ((buffer-read-only nil)
4200           (inhibit-point-motion-hooks t))
4201       (if (get-text-property end 'invisible)
4202           (gnus-article-unhide-text end (point-max))
4203         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4204
4205 (defun gnus-button-entry ()
4206   ;; Return the first entry in `gnus-button-alist' matching this place.
4207   (let ((alist gnus-button-alist)
4208         (entry nil))
4209     (while alist
4210       (setq entry (pop alist))
4211       (if (looking-at (car entry))
4212           (setq alist nil)
4213         (setq entry nil)))
4214     entry))
4215
4216 (defun gnus-button-push (marker)
4217   ;; Push button starting at MARKER.
4218   (save-excursion
4219     (goto-char marker)
4220     (let* ((entry (gnus-button-entry))
4221            (inhibit-point-motion-hooks t)
4222            (fun (nth 3 entry))
4223            (args (mapcar (lambda (group)
4224                            (let ((string (match-string group)))
4225                              (gnus-set-text-properties
4226                               0 (length string) nil string)
4227                              string))
4228                          (nthcdr 4 entry))))
4229       (cond
4230        ((fboundp fun)
4231         (apply fun args))
4232        ((and (boundp fun)
4233              (fboundp (symbol-value fun)))
4234         (apply (symbol-value fun) args))
4235        (t
4236         (gnus-message 1 "You must define `%S' to use this button"
4237                       (cons fun args)))))))
4238
4239 (defun gnus-button-message-id (message-id)
4240   "Fetch MESSAGE-ID."
4241   (save-excursion
4242     (set-buffer gnus-summary-buffer)
4243     (gnus-summary-refer-article message-id)))
4244
4245 (defun gnus-button-fetch-group (address)
4246   "Fetch GROUP specified by ADDRESS."
4247   (if (not (string-match "[:/]" address))
4248       ;; This is just a simple group url.
4249       (gnus-group-read-ephemeral-group address gnus-select-method)
4250     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4251                            address))
4252         (error "Can't parse %s" address)
4253       (gnus-group-read-ephemeral-group
4254        (match-string 4 address)
4255        `(nntp ,(match-string 1 address)
4256               (nntp-address ,(match-string 1 address))
4257               (nntp-port-number ,(if (match-end 3)
4258                                      (match-string 3 address)
4259                                    "nntp")))))))
4260
4261 (defun gnus-url-parse-query-string (query &optional downcase)
4262   (let (retval pairs cur key val)
4263     (setq pairs (split-string query "&"))
4264     (while pairs
4265       (setq cur (car pairs)
4266             pairs (cdr pairs))
4267       (if (not (string-match "=" cur))
4268           nil                           ; Grace
4269         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4270               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4271         (if downcase
4272             (setq key (downcase key)))
4273         (setq cur (assoc key retval))
4274         (if cur
4275             (setcdr cur (cons val (cdr cur)))
4276           (setq retval (cons (list key val) retval)))))
4277     retval))
4278
4279 (defun gnus-url-unhex (x)
4280   (if (> x ?9)
4281       (if (>= x ?a)
4282           (+ 10 (- x ?a))
4283         (+ 10 (- x ?A)))
4284     (- x ?0)))
4285
4286 (defun gnus-url-unhex-string (str &optional allow-newlines)
4287   "Remove %XXX embedded spaces, etc in a url.
4288 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4289 decoding of carriage returns and line feeds in the string, which is normally
4290 forbidden in URL encoding."
4291   (setq str (or str ""))
4292   (let ((tmp "")
4293         (case-fold-search t))
4294     (while (string-match "%[0-9a-f][0-9a-f]" str)
4295       (let* ((start (match-beginning 0))
4296              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4297              (code (+ (* 16 ch1)
4298                       (gnus-url-unhex (elt str (+ start 2))))))
4299         (setq tmp (concat
4300                    tmp (substring str 0 start)
4301                    (cond
4302                     (allow-newlines
4303                      (char-to-string code))
4304                     ((or (= code ?\n) (= code ?\r))
4305                      " ")
4306                     (t (char-to-string code))))
4307               str (substring str (match-end 0)))))
4308     (setq tmp (concat tmp str))
4309     tmp))
4310
4311 (defun gnus-url-mailto (url)
4312   ;; Send mail to someone
4313   (when (string-match "mailto:/*\\(.*\\)" url)
4314     (setq url (substring url (match-beginning 1) nil)))
4315   (let (to args subject func)
4316     (if (string-match (regexp-quote "?") url)
4317         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4318               args (gnus-url-parse-query-string
4319                     (substring url (match-end 0) nil) t))
4320       (setq to (gnus-url-unhex-string url)))
4321     (setq args (cons (list "to" to) args)
4322           subject (cdr-safe (assoc "subject" args)))
4323     (message-mail)
4324     (while args
4325       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4326       (if (fboundp func)
4327           (funcall func)
4328         (message-position-on-field (caar args)))
4329       (insert (mapconcat 'identity (cdar args) ", "))
4330       (setq args (cdr args)))
4331     (if subject
4332         (message-goto-body)
4333       (message-goto-subject))))
4334
4335 (defun gnus-button-mailto (address)
4336   ;; Mail to ADDRESS.
4337   (set-buffer (gnus-copy-article-buffer))
4338   (message-reply address))
4339
4340 (defun gnus-button-reply (address)
4341   ;; Reply to ADDRESS.
4342   (message-reply address))
4343
4344 (defun gnus-button-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 address)
4349     (funcall browse-url-browser-function address)))
4350
4351 (defun gnus-button-embedded-url (address)
4352   "Browse ADDRESS."
4353   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4354   (if (listp browse-url-browser-function)
4355       (browse-url (gnus-strip-whitespace address))
4356     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
4357
4358 ;;; Next/prev buttons in the article buffer.
4359
4360 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4361 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4362
4363 (defvar gnus-prev-page-map nil)
4364 (unless gnus-prev-page-map
4365   (setq gnus-prev-page-map (make-sparse-keymap))
4366   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4367   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4368
4369 (defun gnus-insert-prev-page-button ()
4370   (let ((buffer-read-only nil))
4371     (gnus-eval-format
4372      gnus-prev-page-line-format nil
4373      `(gnus-prev t local-map ,gnus-prev-page-map
4374                  gnus-callback gnus-article-button-prev-page
4375                  article-type annotation))))
4376
4377 (defvar gnus-next-page-map nil)
4378 (unless gnus-next-page-map
4379   (setq gnus-next-page-map (make-keymap))
4380   (suppress-keymap gnus-prev-page-map)
4381   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4382   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4383
4384 (defun gnus-button-next-page ()
4385   "Go to the next page."
4386   (interactive)
4387   (let ((win (selected-window)))
4388     (select-window (get-buffer-window gnus-article-buffer t))
4389     (gnus-article-next-page)
4390     (select-window win)))
4391
4392 (defun gnus-button-prev-page ()
4393   "Go to the prev page."
4394   (interactive)
4395   (let ((win (selected-window)))
4396     (select-window (get-buffer-window gnus-article-buffer t))
4397     (gnus-article-prev-page)
4398     (select-window win)))
4399
4400 (defun gnus-insert-next-page-button ()
4401   (let ((buffer-read-only nil))
4402     (gnus-eval-format gnus-next-page-line-format nil
4403                       `(gnus-next
4404                         t local-map ,gnus-next-page-map
4405                         gnus-callback gnus-article-button-next-page
4406                         article-type annotation))))
4407
4408 (defun gnus-article-button-next-page (arg)
4409   "Go to the next page."
4410   (interactive "P")
4411   (let ((win (selected-window)))
4412     (select-window (get-buffer-window gnus-article-buffer t))
4413     (gnus-article-next-page)
4414     (select-window win)))
4415
4416 (defun gnus-article-button-prev-page (arg)
4417   "Go to the prev page."
4418   (interactive "P")
4419   (let ((win (selected-window)))
4420     (select-window (get-buffer-window gnus-article-buffer t))
4421     (gnus-article-prev-page)
4422     (select-window win)))
4423
4424 (defvar gnus-decode-header-methods
4425   '(mail-decode-encoded-word-region)
4426   "List of methods used to decode headers.
4427
4428 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
4429 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
4430 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4431 whose names match REGEXP.
4432
4433 For example:
4434 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4435  mail-decode-encoded-word-region
4436  (\"chinese\" . rfc1843-decode-region))
4437 ")
4438
4439 (defvar gnus-decode-header-methods-cache nil)
4440
4441 (defun gnus-multi-decode-header (start end)
4442   "Apply the functions from `gnus-encoded-word-methods' that match."
4443   (unless (and gnus-decode-header-methods-cache
4444                (eq gnus-newsgroup-name
4445                    (car gnus-decode-header-methods-cache)))
4446     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4447     (mapc '(lambda (x)
4448              (if (symbolp x)
4449                  (nconc gnus-decode-header-methods-cache (list x))
4450                (if (and gnus-newsgroup-name
4451                         (string-match (car x) gnus-newsgroup-name))
4452                    (nconc gnus-decode-header-methods-cache
4453                           (list (cdr x))))))
4454           gnus-decode-header-methods))
4455   (let ((xlist gnus-decode-header-methods-cache))
4456     (pop xlist)
4457     (save-restriction
4458       (narrow-to-region start end)
4459       (while xlist
4460         (funcall (pop xlist) (point-min) (point-max))))))
4461
4462 ;;;
4463 ;;; Treatment top-level handling.
4464 ;;;
4465
4466 (defun gnus-treat-article (condition &optional part-number total-parts type)
4467   (let ((length (- (point-max) (point-min)))
4468         (alist gnus-treatment-function-alist)
4469         (article-goto-body-goes-to-point-min-p t)
4470         (treated-type
4471          (or (not type)
4472              (catch 'found
4473                (let ((list gnus-article-treat-types))
4474                  (while list
4475                    (when (string-match (pop list) type)
4476                      (throw 'found t)))))))
4477         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4478         val elem)
4479     (gnus-run-hooks 'gnus-part-display-hook)
4480     (while (setq elem (pop alist))
4481       (setq val (symbol-value (car elem)))
4482       (when (and (or (consp val)
4483                      treated-type)
4484                  (gnus-treat-predicate val)
4485                  (or (not (get (car elem) 'highlight))
4486                      highlightp))
4487         (save-restriction
4488           (funcall (cadr elem)))))))
4489
4490 ;; Dynamic variables.
4491 (defvar part-number)
4492 (defvar total-parts)
4493 (defvar type)
4494 (defvar condition)
4495 (defvar length)
4496 (defun gnus-treat-predicate (val)
4497   (cond
4498    (condition
4499     (eq condition val))
4500    ((null val)
4501     nil)
4502    ((eq val t)
4503     t)
4504    ((eq val 'head)
4505     nil)
4506    ((eq val 'last)
4507     (eq part-number total-parts))
4508    ((numberp val)
4509     (< length val))
4510    ((and (listp val)
4511          (stringp (car val)))
4512     (apply 'gnus-or (mapcar `(lambda (s)
4513                                (string-match s ,(or gnus-newsgroup-name "")))
4514                             val)))
4515    ((listp val)
4516     (let ((pred (pop val)))
4517       (cond
4518        ((eq pred 'or)
4519         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4520        ((eq pred 'and)
4521         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4522        ((eq pred 'not)
4523         (not (gnus-treat-predicate (car val))))
4524        ((eq pred 'typep)
4525         (equal (cadr val) type))
4526        (t
4527         (error "%S is not a valid predicate" pred)))))
4528    (t
4529     (error "%S is not a valid value" val))))
4530
4531 (gnus-ems-redefine)
4532
4533 (provide 'gnus-art)
4534
4535 (run-hooks 'gnus-art-load-hook)
4536
4537 ;;; gnus-art.el ends here