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