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