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