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