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