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