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