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