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