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