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