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