5251553402137706ba6d068f22cf53103d69b09b
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'static))
33
34 (require 'path-util)
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'alist)
40 (require 'mime-view)
41 (require 'wid-edit)
42
43 ;; Avoid byte-compile warnings.
44 (eval-when-compile
45   (require 'mm-bodies)
46   (require 'mail-parse)
47   (require 'mm-decode)
48   (require 'mm-view)
49   (require 'mm-uu)
50   )
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
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:" "^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     "^X-Received:" "^Content-length:" "X-precedence:"
145     "^X-Authenticated-User:" "^X-Comment" "^X-Report:" "^X-Abuse-Info:"
146     "^X-HTTP-Proxy:" "^X-Mydeja-Info:" "^X-Copyright" "^X-No-Markup:"
147     "^X-Abuse-Info:" "^X-From_:" "^X-Accept-Language:" "^Errors-To:"
148     "^X-BeenThere:" "^X-Mailman-Version:" "^List-Help:" "^List-Post:"
149     "^List-Subscribe:" "^List-Id:" "^List-Unsubscribe:" "^List-Archive:"
150      "^X-Content-length:" "^X-Posting-Agent:" "^Original-Received:")
151   "*All headers that start with this regexp will be hidden.
152 This variable can also be a list of regexps of headers to be ignored.
153 If `gnus-visible-headers' is non-nil, this variable will be ignored."
154   :type '(choice :custom-show nil
155                  regexp
156                  (repeat regexp))
157   :group 'gnus-article-hiding)
158
159 (defcustom gnus-visible-headers
160   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
161   "*All headers that do not match this regexp will be hidden.
162 This variable can also be a list of regexp of headers to remain visible.
163 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
164   :type '(repeat :value-to-internal (lambda (widget value)
165                                       (custom-split-regexp-maybe value))
166                  :match (lambda (widget value)
167                           (or (stringp value)
168                               (widget-editable-list-match widget value)))
169                  regexp)
170   :group 'gnus-article-hiding)
171
172 (defcustom gnus-sorted-header-list
173   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
174     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
175   "*This variable is a list of regular expressions.
176 If it is non-nil, headers that match the regular expressions will
177 be placed first in the article buffer in the sequence specified by
178 this list."
179   :type '(repeat regexp)
180   :group 'gnus-article-hiding)
181
182 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
183   "Headers that are only to be displayed if they have interesting data.
184 Possible values in this list are:
185
186   'empty       Headers with no content.
187   'newsgroups  Newsgroup identical to Gnus group.
188   'to-address  To identical to To-address.
189   'followup-to Followup-to identical to Newsgroups.
190   'reply-to    Reply-to identical to From.
191   'date        Date less than four days old.
192   'long-to     To and/or Cc longer than 1024 characters.
193   'many-to     Multiple To and/or Cc."
194   :type '(set (const :tag "Headers with no content." empty)
195               (const :tag "Newsgroups identical to Gnus group." newsgroups)
196               (const :tag "To identical to To-address." to-address)
197               (const :tag "Followup-to identical to Newsgroups." followup-to)
198               (const :tag "Reply-to identical to From." reply-to)
199               (const :tag "Date less than four days old." date)
200               (const :tag "To and/or Cc longer than 1024 characters." long-to)
201               (const :tag "Multiple To and/or Cc headers." many-to))
202   :group 'gnus-article-hiding)
203
204 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
205   "Regexp matching signature separator.
206 This can also be a list of regexps.  In that case, it will be checked
207 from head to tail looking for a separator.  Searches will be done from
208 the end of the buffer."
209   :type '(repeat string)
210   :group 'gnus-article-signature)
211
212 (defcustom gnus-signature-limit nil
213   "Provide a limit to what is considered a signature.
214 If it is a number, no signature may not be longer (in characters) than
215 that number.  If it is a floating point number, no signature may be
216 longer (in lines) than that number.  If it is a function, the function
217 will be called without any parameters, and if it returns nil, there is
218 no signature in the buffer.  If it is a string, it will be used as a
219 regexp.  If it matches, the text in question is not a signature."
220   :type '(choice (integer :value 200)
221                  (number :value 4.0)
222                  (function :value fun)
223                  (regexp :value ".*"))
224   :group 'gnus-article-signature)
225
226 (defcustom gnus-hidden-properties '(invisible t intangible t)
227   "Property list to use for hiding text."
228   :type 'sexp
229   :group 'gnus-article-hiding)
230
231 ;; Fixme: This isn't the right thing for mixed graphical and and
232 ;; non-graphical frames in a session.
233 (defcustom gnus-article-x-face-command
234   (cond
235    ((featurep 'xemacs)
236     (if (or (featurep 'xface)
237             (featurep 'xpm))
238         'gnus-xmas-article-display-xface
239       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -"))
240    ((and (fboundp 'image-type-available-p)
241          (module-installed-p 'x-face-e21))
242     'x-face-decode-message-header)
243    ((and (fboundp 'image-type-available-p)
244          (image-type-available-p 'xbm))
245     'gnus-article-display-xface)
246    ((and window-system
247          (module-installed-p 'x-face-mule))
248     'x-face-mule-gnus-article-display-x-face)
249    (gnus-article-compface-xbm
250     "{ echo '/* Width=48, Height=48 */'; uncompface; } | display -")
251    (t
252     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
253 display -"))
254   "*String or function to be executed to display an X-Face header.
255 If it is a string, the command will be executed in a sub-shell
256 asynchronously.  The compressed face will be piped to this command."
257   :type `(choice
258           ,@(let (x-face-e21 x-face-mule)
259               (if (featurep 'xemacs)
260                   nil
261                 (setq x-face-e21 (module-installed-p 'x-face-e21)
262                       x-face-mule (module-installed-p 'x-face-mule)))
263               (delq nil
264                     (list
265                      'string
266                      (if (and (featurep 'xemacs)
267                               (or (featurep 'xface)
268                                   (featurep 'xpm)))
269                          '(function-item gnus-xmas-article-display-xface))
270                      (if (and x-face-e21
271                               (fboundp 'image-type-available-p))
272                          '(function-item
273                            :tag "x-face-decode-message-header (x-face-e21)"
274                            x-face-decode-message-header))
275                      (if x-face-mule
276                          '(function-item
277                            x-face-mule-gnus-article-display-x-face))
278                      'function))))
279   ;;:version "21.1"
280   :group 'gnus-article-washing)
281
282 (defcustom gnus-article-x-face-too-ugly nil
283   "Regexp matching posters whose face shouldn't be shown automatically."
284   :type '(choice regexp (const nil))
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-banner-alist nil
288   "Banner alist for stripping.
289 For example,
290      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
291   :version "21.1"
292   :type '(repeat (cons symbol regexp))
293   :group 'gnus-article-washing)
294
295 (gnus-define-group-parameter
296  banner
297  :variable-document
298  "Alist of regexps (to match group names) and banner."
299  :variable-group gnus-article-washing
300  :parameter-type
301  '(choice :tag "Banner"
302           :value nil
303           (const :tag "Remove signature" signature)
304           (symbol :tag "Item in `gnus-article-banner-alist'" none)
305           regexp
306           (const :tag "None" nil))
307  :parameter-document
308  "If non-nil, specify how to remove `banners' from articles.
309
310 Symbol `signature' means to remove signatures delimited by
311 `gnus-signature-separator'.  Any other symbol is used to look up a
312 regular expression to match the banner in `gnus-article-banner-alist'.
313 A string is used as a regular expression to match the banner
314 directly.")
315
316 (defcustom gnus-emphasis-alist
317   (let ((format
318          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
319         (types
320          '(("_" "_" underline)
321            ("/" "/" italic)
322            ("\\*" "\\*" bold)
323            ("_/" "/_" underline-italic)
324            ("_\\*" "\\*_" underline-bold)
325            ("\\*/" "/\\*" bold-italic)
326            ("_\\*/" "/\\*_" underline-bold-italic))))
327     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
328        2 3 gnus-emphasis-underline)
329       ,@(mapcar
330          (lambda (spec)
331            (list
332             (format format (car spec) (car (cdr spec)))
333             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
334          types)))
335   "*Alist that says how to fontify certain phrases.
336 Each item looks like this:
337
338   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
339
340 The first element is a regular expression to be matched.  The second
341 is a number that says what regular expression grouping used to find
342 the entire emphasized word.  The third is a number that says what
343 regexp grouping should be displayed and highlighted.  The fourth
344 is the face used for highlighting."
345   :type '(repeat (list :value ("" 0 0 default)
346                        regexp
347                        (integer :tag "Match group")
348                        (integer :tag "Emphasize group")
349                        face))
350   :group 'gnus-article-emphasis)
351
352 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
353   "A regexp to describe whitespace which should not be emphasized.
354 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
355 The former avoids underlining of leading and trailing whitespace,
356 and the latter avoids underlining any whitespace at all."
357   :version "21.1"
358   :group 'gnus-article-emphasis
359   :type 'regexp)
360
361 (defface gnus-emphasis-bold '((t (:bold t)))
362   "Face used for displaying strong emphasized text (*word*)."
363   :group 'gnus-article-emphasis)
364
365 (defface gnus-emphasis-italic '((t (:italic t)))
366   "Face used for displaying italic emphasized text (/word/)."
367   :group 'gnus-article-emphasis)
368
369 (defface gnus-emphasis-underline '((t (:underline t)))
370   "Face used for displaying underlined emphasized text (_word_)."
371   :group 'gnus-article-emphasis)
372
373 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
374   "Face used for displaying underlined bold emphasized text (_*word*_)."
375   :group 'gnus-article-emphasis)
376
377 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
378   "Face used for displaying underlined italic emphasized text (_/word/_)."
379   :group 'gnus-article-emphasis)
380
381 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
382   "Face used for displaying bold italic emphasized text (/*word*/)."
383   :group 'gnus-article-emphasis)
384
385 (defface gnus-emphasis-underline-bold-italic
386   '((t (:bold t :italic t :underline t)))
387   "Face used for displaying underlined bold italic emphasized text.
388 Esample: (_/*word*/_)."
389   :group 'gnus-article-emphasis)
390
391 (defface gnus-emphasis-highlight-words
392   '((t (:background "black" :foreground "yellow")))
393   "Face used for displaying highlighted words."
394   :group 'gnus-article-emphasis)
395
396 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
397   "Format for display of Date headers in article bodies.
398 See `format-time-string' for the possible values.
399
400 The variable can also be function, which should return a complete Date
401 header.  The function is called with one argument, the time, which can
402 be fed to `format-time-string'."
403   :type '(choice string symbol)
404   :link '(custom-manual "(gnus)Article Date")
405   :group 'gnus-article-washing)
406
407 (eval-and-compile
408   (autoload 'mail-extract-address-components "mail-extr"))
409
410 (defcustom gnus-save-all-headers t
411   "*If non-nil, don't remove any headers before saving."
412   :group 'gnus-article-saving
413   :type 'boolean)
414
415 (defcustom gnus-prompt-before-saving 'always
416   "*This variable says how much prompting is to be done when saving articles.
417 If it is nil, no prompting will be done, and the articles will be
418 saved to the default files.  If this variable is `always', each and
419 every article that is saved will be preceded by a prompt, even when
420 saving large batches of articles.  If this variable is neither nil not
421 `always', there the user will be prompted once for a file name for
422 each invocation of the saving commands."
423   :group 'gnus-article-saving
424   :type '(choice (item always)
425                  (item :tag "never" nil)
426                  (sexp :tag "once" :format "%t\n" :value t)))
427
428 (defcustom gnus-saved-headers gnus-visible-headers
429   "Headers to keep if `gnus-save-all-headers' is nil.
430 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
431 If that variable is nil, however, all headers that match this regexp
432 will be kept while the rest will be deleted before saving."
433   :group 'gnus-article-saving
434   :type 'regexp)
435
436 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
437   "A function to save articles in your favourite format.
438 The function must be interactively callable (in other words, it must
439 be an Emacs command).
440
441 Gnus provides the following functions:
442
443 * gnus-summary-save-in-rmail (Rmail format)
444 * gnus-summary-save-in-mail (Unix mail format)
445 * gnus-summary-save-in-folder (MH folder)
446 * gnus-summary-save-in-file (article format)
447 * gnus-summary-save-body-in-file (article body)
448 * gnus-summary-save-in-vm (use VM's folder format)
449 * gnus-summary-write-to-file (article format -- overwrite)."
450   :group 'gnus-article-saving
451   :type '(radio (function-item gnus-summary-save-in-rmail)
452                 (function-item gnus-summary-save-in-mail)
453                 (function-item gnus-summary-save-in-folder)
454                 (function-item gnus-summary-save-in-file)
455                 (function-item gnus-summary-save-body-in-file)
456                 (function-item gnus-summary-save-in-vm)
457                 (function-item gnus-summary-write-to-file)))
458
459 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
460   "A function generating a file name to save articles in Rmail format.
461 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
462   :group 'gnus-article-saving
463   :type 'function)
464
465 (defcustom gnus-mail-save-name 'gnus-plain-save-name
466   "A function generating a file name to save articles in Unix mail format.
467 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
468   :group 'gnus-article-saving
469   :type 'function)
470
471 (defcustom gnus-folder-save-name 'gnus-folder-save-name
472   "A function generating a file name to save articles in MH folder.
473 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
474   :group 'gnus-article-saving
475   :type 'function)
476
477 (defcustom gnus-file-save-name 'gnus-numeric-save-name
478   "A function generating a file name to save articles in article format.
479 The function is called with NEWSGROUP, HEADERS, and optional
480 LAST-FILE."
481   :group 'gnus-article-saving
482   :type 'function)
483
484 (defcustom gnus-split-methods
485   '((gnus-article-archive-name)
486     (gnus-article-nndoc-name))
487   "*Variable used to suggest where articles are to be saved.
488 For instance, if you would like to save articles related to Gnus in
489 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
490 you could set this variable to something like:
491
492  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
493    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
494
495 This variable is an alist where the where the key is the match and the
496 value is a list of possible files to save in if the match is non-nil.
497
498 If the match is a string, it is used as a regexp match on the
499 article.  If the match is a symbol, that symbol will be funcalled
500 from the buffer of the article to be saved with the newsgroup as the
501 parameter.  If it is a list, it will be evaled in the same buffer.
502
503 If this form or function returns a string, this string will be used as
504 a possible file name; and if it returns a non-nil list, that list will
505 be used as possible file names."
506   :group 'gnus-article-saving
507   :type '(repeat (choice (list :value (fun) function)
508                          (cons :value ("" "") regexp (repeat string))
509                          (sexp :value nil))))
510
511 (defcustom gnus-article-display-method-for-mime
512   'gnus-article-display-mime-message
513   "Function to display a MIME message.
514 The function is called from the article buffer."
515   :group 'gnus-article-mime
516   :type 'function)
517
518 (defcustom gnus-article-display-method-for-traditional
519   'gnus-article-display-traditional-message
520   "*Function to display a traditional message.
521 The function is called from the article buffer."
522   :group 'gnus-article-mime
523   :type 'function)
524
525 (defcustom gnus-page-delimiter "^\^L"
526   "*Regexp describing what to use as article page delimiters.
527 The default value is \"^\^L\", which is a form linefeed at the
528 beginning of a line."
529   :type 'regexp
530   :group 'gnus-article-various)
531
532 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
533   "*The format specification for the article mode line.
534 See `gnus-summary-mode-line-format' for a closer description.
535
536 The following additional specs are available:
537
538 %w  The article washing status.
539 %m  The number of MIME parts in the article."
540   :type 'string
541   :group 'gnus-article-various)
542
543 (defcustom gnus-article-mode-hook nil
544   "*A hook for Gnus article mode."
545   :type 'hook
546   :group 'gnus-article-various)
547
548 (when (featurep 'xemacs)
549   ;; Extracted from gnus-xmas-define in order to preserve user settings
550   (when (fboundp 'turn-off-scroll-in-place)
551     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
552   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
553   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
554
555 (defcustom gnus-article-menu-hook nil
556   "*Hook run after the creation of the article mode menu."
557   :type 'hook
558   :group 'gnus-article-various)
559
560 (defcustom gnus-article-prepare-hook nil
561   "*A hook called after an article has been prepared in the article buffer."
562   :type 'hook
563   :group 'gnus-article-various)
564
565 (defcustom gnus-article-hide-pgp-hook nil
566   "*A hook called after successfully hiding a PGP signature."
567   :type 'hook
568   :group 'gnus-article-various)
569
570 (defcustom gnus-article-button-face 'bold
571   "Face used for highlighting buttons in the article buffer.
572
573 An article button is a piece of text that you can activate by pressing
574 `RET' or `mouse-2' above it."
575   :type 'face
576   :group 'gnus-article-buttons)
577
578 (defcustom gnus-article-mouse-face 'highlight
579   "Face used for mouse highlighting in the article buffer.
580
581 Article buttons will be displayed in this face when the cursor is
582 above them."
583   :type 'face
584   :group 'gnus-article-buttons)
585
586 (defcustom gnus-signature-face 'gnus-signature-face
587   "Face used for highlighting a signature in the article buffer.
588 Obsolete; use the face `gnus-signature-face' for customizations instead."
589   :type 'face
590   :group 'gnus-article-highlight
591   :group 'gnus-article-signature)
592
593 (defface gnus-signature-face
594   '((t
595      (:italic t)))
596   "Face used for highlighting a signature in the article buffer."
597   :group 'gnus-article-highlight
598   :group 'gnus-article-signature)
599
600 (defface gnus-header-from-face
601   '((((class color)
602       (background dark))
603      (:foreground "spring green"))
604     (((class color)
605       (background light))
606      (:foreground "red3"))
607     (t
608      (:italic t)))
609   "Face used for displaying from headers."
610   :group 'gnus-article-headers
611   :group 'gnus-article-highlight)
612
613 (defface gnus-header-subject-face
614   '((((class color)
615       (background dark))
616      (:foreground "SeaGreen3"))
617     (((class color)
618       (background light))
619      (:foreground "red4"))
620     (t
621      (:bold t :italic t)))
622   "Face used for displaying subject headers."
623   :group 'gnus-article-headers
624   :group 'gnus-article-highlight)
625
626 (defface gnus-header-newsgroups-face
627   '((((class color)
628       (background dark))
629      (:foreground "yellow" :italic t))
630     (((class color)
631       (background light))
632      (:foreground "MidnightBlue" :italic t))
633     (t
634      (:italic t)))
635   "Face used for displaying newsgroups headers."
636   :group 'gnus-article-headers
637   :group 'gnus-article-highlight)
638
639 (defface gnus-header-name-face
640   '((((class color)
641       (background dark))
642      (:foreground "SeaGreen"))
643     (((class color)
644       (background light))
645      (:foreground "maroon"))
646     (t
647      (:bold t)))
648   "Face used for displaying header names."
649   :group 'gnus-article-headers
650   :group 'gnus-article-highlight)
651
652 (defface gnus-header-content-face
653   '((((class color)
654       (background dark))
655      (:foreground "forest green" :italic t))
656     (((class color)
657       (background light))
658      (:foreground "indianred4" :italic t))
659     (t
660      (:italic t)))
661   "Face used for displaying header content."
662   :group 'gnus-article-headers
663   :group 'gnus-article-highlight)
664
665 (defcustom gnus-header-face-alist
666   '(("From" nil gnus-header-from-face)
667     ("Subject" nil gnus-header-subject-face)
668     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
669     ("" gnus-header-name-face gnus-header-content-face))
670   "*Controls highlighting of article header.
671
672 An alist of the form (HEADER NAME CONTENT).
673
674 HEADER is a regular expression which should match the name of an
675 header header and NAME and CONTENT are either face names or nil.
676
677 The name of each header field will be displayed using the face
678 specified by the first element in the list where HEADER match the
679 header name and NAME is non-nil.  Similarly, the content will be
680 displayed by the first non-nil matching CONTENT face."
681   :group 'gnus-article-headers
682   :group 'gnus-article-highlight
683   :type '(repeat (list (regexp :tag "Header")
684                        (choice :tag "Name"
685                                (item :tag "skip" nil)
686                                (face :value default))
687                        (choice :tag "Content"
688                                (item :tag "skip" nil)
689                                (face :value default)))))
690
691 (defcustom gnus-article-decode-hook nil
692   "*Hook run to decode charsets in articles."
693   :group 'gnus-article-headers
694   :type 'hook)
695
696 (defcustom gnus-display-mime-function 'gnus-display-mime
697   "Function to display MIME articles."
698   :group 'gnus-article-mime
699   :type 'function)
700
701 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
702   "Function used to decode headers.")
703
704 (defvar gnus-article-dumbquotes-map
705   '(("\200" "EUR")
706     ("\202" ",")
707     ("\203" "f")
708     ("\204" ",,")
709     ("\205" "...")
710     ("\213" "<")
711     ("\214" "OE")
712     ("\221" "`")
713     ("\222" "'")
714     ("\223" "``")
715     ("\224" "\"")
716     ("\225" "*")
717     ("\226" "-")
718     ("\227" "--")
719     ("\231" "(TM)")
720     ("\233" ">")
721     ("\234" "oe")
722     ("\264" "'"))
723   "Table for MS-to-Latin1 translation.")
724
725 (defcustom gnus-ignored-mime-types nil
726   "List of MIME types that should be ignored by Gnus."
727   :version "21.1"
728   :group 'gnus-article-mime
729   :type '(repeat regexp))
730
731 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
732   "List of MIME types that should not be given buttons when rendered inline.
733 See also `gnus-buttonized-mime-types' which may override this variable."
734   :version "21.1"
735   :group 'gnus-article-mime
736   :type '(repeat regexp))
737
738 (defcustom gnus-buttonized-mime-types nil
739   "List of MIME types that should be given buttons when rendered inline.
740 If set, this variable overrides `gnus-unbuttonized-mime-types'.
741 To see e.g. security buttons you could set this to
742 `(\"multipart/signed\")'."
743   :version "21.1"
744   :group 'gnus-article-mime
745   :type '(repeat regexp))
746
747 (defcustom gnus-article-mime-part-function nil
748   "Function called with a MIME handle as the argument.
749 This is meant for people who want to do something automatic based
750 on parts -- for instance, adding Vcard info to a database."
751   :group 'gnus-article-mime
752   :type 'function)
753
754 (defcustom gnus-mime-multipart-functions nil
755   "An alist of MIME types to functions to display them."
756   :version "21.1"
757   :group 'gnus-article-mime
758   :type 'alist)
759
760 (defcustom gnus-article-date-lapsed-new-header nil
761   "Whether the X-Sent and Date headers can coexist.
762 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
763 either replace the old \"Date:\" header (if this variable is nil), or
764 be added below it (otherwise)."
765   :version "21.1"
766   :group 'gnus-article-headers
767   :type 'boolean)
768
769 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
770   "Function called with a MIME handle as the argument.
771 This is meant for people who want to view first matched part.
772 For `undisplayed-alternative' (default), the first undisplayed
773 part or alternative part is used.  For `undisplayed', the first
774 undisplayed part is used.  For a function, the first part which
775 the function return `t' is used.  For `nil', the first part is
776 used."
777   :version "21.1"
778   :group 'gnus-article-mime
779   :type '(choice
780           (item :tag "first" :value nil)
781           (item :tag "undisplayed" :value undisplayed)
782           (item :tag "undisplayed or alternative"
783                 :value undisplayed-alternative)
784           (function)))
785
786 (defcustom gnus-mime-action-alist
787   '(("save to file" . gnus-mime-save-part)
788     ("save and strip" . gnus-mime-save-part-and-strip)
789     ("display as text" . gnus-mime-inline-part)
790     ("view the part" . gnus-mime-view-part)
791     ("pipe to command" . gnus-mime-pipe-part)
792     ("toggle display" . gnus-article-press-button)
793     ("toggle display" . gnus-article-view-part-as-charset)
794     ("view as type" . gnus-mime-view-part-as-type)
795     ("internalize type" . gnus-mime-internalize-part)
796     ("externalize type" . gnus-mime-externalize-part))
797   "An alist of actions that run on the MIME attachment."
798   :group 'gnus-article-mime
799   :type '(repeat (cons (string :tag "name")
800                        (function))))
801
802 (defcustom gnus-mime-action-alist
803   '(("save to file" . gnus-mime-save-part)
804     ("display as text" . gnus-mime-inline-part)
805     ("view the part" . gnus-mime-view-part)
806     ("pipe to command" . gnus-mime-pipe-part)
807     ("toggle display" . gnus-article-press-button)
808     ("view as type" . gnus-mime-view-part-as-type)
809     ("internalize type" . gnus-mime-internalize-part)
810     ("externalize type" . gnus-mime-externalize-part))
811   "An alist of actions that run on the MIME attachment."
812   :version "21.1"
813   :group 'gnus-article-mime
814   :type '(repeat (cons (string :tag "name")
815                        (function))))
816
817 ;;;
818 ;;; The treatment variables
819 ;;;
820
821 (defvar gnus-part-display-hook nil
822   "Hook called on parts that are to receive treatment.")
823
824 (defvar gnus-article-treat-custom
825   '(choice (const :tag "Off" nil)
826            (const :tag "On" t)
827            (const :tag "Header" head)
828            (const :tag "Last" last)
829            (const :tag "Mime" mime)
830            (integer :tag "Less")
831            (repeat :tag "Groups" regexp)
832            (sexp :tag "Predicate")))
833
834 (defvar gnus-article-treat-head-custom
835   '(choice (const :tag "Off" nil)
836            (const :tag "Header" head)))
837
838 (defvar gnus-article-treat-types '("text/plain")
839   "Parts to treat.")
840
841 (defvar gnus-inhibit-treatment nil
842   "Whether to inhibit treatment.")
843
844 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
845   "Highlight the signature.
846 Valid values are nil, t, `head', `last', an integer or a predicate.
847 See Info node `(gnus)Customizing Articles'."
848   :group 'gnus-article-treat
849   :type gnus-article-treat-custom)
850 (put 'gnus-treat-highlight-signature 'highlight t)
851
852 (defcustom gnus-treat-buttonize 100000
853   "Add buttons.
854 Valid values are nil, t, `head', `last', an integer or a predicate.
855 See the manual for details."
856   :group 'gnus-article-treat
857   :type gnus-article-treat-custom)
858 (put 'gnus-treat-buttonize 'highlight t)
859
860 (defcustom gnus-treat-buttonize-head 'head
861   "Add buttons to the head.
862 Valid values are nil, t, `head', `last', an integer or a predicate.
863 See the manual for details."
864   :group 'gnus-article-treat
865   :type gnus-article-treat-head-custom)
866 (put 'gnus-treat-buttonize-head 'highlight t)
867
868 (defcustom gnus-treat-emphasize
869   (and (or window-system
870            (featurep 'xemacs)
871            (>= (string-to-number emacs-version) 21))
872        50000)
873   "Emphasize text.
874 Valid values are nil, t, `head', `last', an integer or a predicate.
875 See the manual for details."
876   :group 'gnus-article-treat
877   :type gnus-article-treat-custom)
878 (put 'gnus-treat-emphasize 'highlight t)
879
880 (defcustom gnus-treat-strip-cr nil
881   "Remove carriage returns.
882 Valid values are nil, t, `head', `last', an integer or a predicate.
883 See the manual for details."
884   :group 'gnus-article-treat
885   :type gnus-article-treat-custom)
886
887 (defcustom gnus-treat-leading-whitespace nil
888   "Remove leading whitespace in headers.
889 Valid values are nil, t, `head', `last', an integer or a predicate.
890 See the manual for details."
891   :group 'gnus-article-treat
892   :type gnus-article-treat-custom)
893
894 (defcustom gnus-treat-hide-headers 'head
895   "Hide headers.
896 Valid values are nil, t, `head', `last', an integer or a predicate.
897 See the manual for details."
898   :group 'gnus-article-treat
899   :type gnus-article-treat-head-custom)
900
901 (defcustom gnus-treat-hide-boring-headers nil
902   "Hide boring headers.
903 Valid values are nil, t, `head', `last', an integer or a predicate.
904 See the manual for details."
905   :group 'gnus-article-treat
906   :type gnus-article-treat-head-custom)
907
908 (defcustom gnus-treat-hide-signature nil
909   "Hide the signature.
910 Valid values are nil, t, `head', `last', an integer or a predicate.
911 See the manual for details."
912   :group 'gnus-article-treat
913   :type gnus-article-treat-custom)
914
915 (defcustom gnus-treat-fill-article nil
916   "Fill the article.
917 Valid values are nil, t, `head', `last', an integer or a predicate.
918 See the manual for details."
919   :group 'gnus-article-treat
920   :type gnus-article-treat-custom)
921
922 (defcustom gnus-treat-hide-citation nil
923   "Hide cited text.
924 Valid values are nil, t, `head', `last', an integer or a predicate.
925 See the manual for details."
926   :group 'gnus-article-treat
927   :type gnus-article-treat-custom)
928
929 (defcustom gnus-treat-hide-citation-maybe nil
930   "Hide cited text.
931 Valid values are nil, t, `head', `last', an integer or a predicate.
932 See the manual for details."
933   :group 'gnus-article-treat
934   :type gnus-article-treat-custom)
935
936 (defcustom gnus-treat-strip-list-identifiers 'head
937   "Strip list identifiers from `gnus-list-identifiers`.
938 Valid values are nil, t, `head', `last', an integer or a predicate.
939 See the manual for details."
940   :version "21.1"
941   :group 'gnus-article-treat
942   :type gnus-article-treat-custom)
943
944 (defcustom gnus-treat-strip-pgp t
945   "Strip PGP signatures.
946 Valid values are nil, t, `head', `last', an integer or a predicate.
947 See the manual for details."
948   :group 'gnus-article-treat
949   :type gnus-article-treat-custom)
950
951 (defcustom gnus-treat-strip-pem nil
952   "Strip PEM signatures.
953 Valid values are nil, t, `head', `last', an integer or a predicate.
954 See the manual for details."
955   :group 'gnus-article-treat
956   :type gnus-article-treat-custom)
957
958 (defcustom gnus-treat-strip-banner t
959   "Strip banners from articles.
960 The banner to be stripped is specified in the `banner' group parameter.
961 Valid values are nil, t, `head', `last', an integer or a predicate.
962 See the manual for details."
963   :group 'gnus-article-treat
964   :type gnus-article-treat-custom)
965
966 (defcustom gnus-treat-highlight-headers 'head
967   "Highlight the headers.
968 Valid values are nil, t, `head', `last', an integer or a predicate.
969 See the manual for details."
970   :group 'gnus-article-treat
971   :type gnus-article-treat-head-custom)
972 (put 'gnus-treat-highlight-headers 'highlight t)
973
974 (defcustom gnus-treat-highlight-citation t
975   "Highlight cited text.
976 Valid values are nil, t, `head', `last', an integer or a predicate.
977 See the manual for details."
978   :group 'gnus-article-treat
979   :type gnus-article-treat-custom)
980 (put 'gnus-treat-highlight-citation 'highlight t)
981
982 (defcustom gnus-treat-date-ut nil
983   "Display the Date in UT (GMT).
984 Valid values are nil, t, `head', `last', an integer or a predicate.
985 See the manual for details."
986   :group 'gnus-article-treat
987   :type gnus-article-treat-head-custom)
988
989 (defcustom gnus-treat-date-local nil
990   "Display the Date in the local timezone.
991 Valid values are nil, t, `head', `last', an integer or a predicate.
992 See the manual for details."
993   :group 'gnus-article-treat
994   :type gnus-article-treat-head-custom)
995
996 (defcustom gnus-treat-date-english nil
997   "Display the Date in a format that can be read aloud in English.
998 Valid values are nil, t, `head', `last', an integer or a predicate.
999 See the manual for details."
1000   :group 'gnus-article-treat
1001   :type gnus-article-treat-head-custom)
1002
1003 (defcustom gnus-treat-date-lapsed nil
1004   "Display the Date header in a way that says how much time has elapsed.
1005 Valid values are nil, t, `head', `last', an integer or a predicate.
1006 See the manual for details."
1007   :group 'gnus-article-treat
1008   :type gnus-article-treat-head-custom)
1009
1010 (defcustom gnus-treat-date-original nil
1011   "Display the date in the original timezone.
1012 Valid values are nil, t, `head', `last', an integer or a predicate.
1013 See the manual for details."
1014   :group 'gnus-article-treat
1015   :type gnus-article-treat-head-custom)
1016
1017 (defcustom gnus-treat-date-iso8601 nil
1018   "Display the date in the ISO8601 format.
1019 Valid values are nil, t, `head', `last', an integer or a predicate.
1020 See the manual for details."
1021   :version "21.1"
1022   :group 'gnus-article-treat
1023   :type gnus-article-treat-head-custom)
1024
1025 (defcustom gnus-treat-date-user-defined nil
1026   "Display the date in a user-defined format.
1027 The format is defined by the `gnus-article-time-format' variable.
1028 Valid values are nil, t, `head', `last', an integer or a predicate.
1029 See the manual for details."
1030   :group 'gnus-article-treat
1031   :type gnus-article-treat-head-custom)
1032
1033 (defcustom gnus-treat-strip-headers-in-body t
1034   "Strip the X-No-Archive header line from the beginning of the body.
1035 Valid values are nil, t, `head', `last', an integer or a predicate.
1036 See the manual for details."
1037   :version "21.1"
1038   :group 'gnus-article-treat
1039   :type gnus-article-treat-custom)
1040
1041 (defcustom gnus-treat-strip-trailing-blank-lines nil
1042   "Strip trailing blank lines.
1043 Valid values are nil, t, `head', `last', an integer or a predicate.
1044 See the manual for details."
1045   :group 'gnus-article-treat
1046   :type gnus-article-treat-custom)
1047
1048 (defcustom gnus-treat-strip-leading-blank-lines nil
1049   "Strip leading blank lines.
1050 Valid values are nil, t, `head', `last', an integer or a predicate.
1051 See the manual for details."
1052   :group 'gnus-article-treat
1053   :type gnus-article-treat-custom)
1054
1055 (defcustom gnus-treat-strip-multiple-blank-lines nil
1056   "Strip multiple blank lines.
1057 Valid values are nil, t, `head', `last', an integer or a predicate.
1058 See the manual for details."
1059   :group 'gnus-article-treat
1060   :type gnus-article-treat-custom)
1061
1062 (defcustom gnus-treat-overstrike t
1063   "Treat overstrike highlighting.
1064 Valid values are nil, t, `head', `last', an integer or a predicate.
1065 See the manual for details."
1066   :group 'gnus-article-treat
1067   :type gnus-article-treat-custom)
1068 (put 'gnus-treat-overstrike 'highlight t)
1069
1070 (defcustom gnus-treat-display-xface
1071   (and (or (and (fboundp 'image-type-available-p)
1072                 (image-type-available-p 'xbm)
1073                 (string-match "^0x" (shell-command-to-string "uncompface")))
1074            (and (featurep 'xemacs) (featurep 'xface))
1075            (eq 'x-face-mule-gnus-article-display-x-face
1076                gnus-article-x-face-command))
1077        'head)
1078   "Display X-Face headers.
1079 Valid values are nil, t, `head', `last', an integer or a predicate.
1080 See the manual for details."
1081   :group 'gnus-article-treat
1082   ;;:version "21.1"
1083   :type gnus-article-treat-head-custom)
1084 (put 'gnus-treat-display-xface 'highlight t)
1085
1086 (defcustom gnus-treat-display-smileys
1087   (if (or (and (featurep 'xemacs)
1088                (featurep 'xpm))
1089           (and (fboundp 'image-type-available-p)
1090                (image-type-available-p 'pbm))
1091           (and (not (featurep 'xemacs))
1092                window-system
1093                (module-installed-p 'smiley-mule)))
1094       t
1095     nil)
1096   "Display smileys.
1097 Valid values are nil, t, `head', `last', an integer or a predicate.
1098 See the manual for details."
1099   :group 'gnus-article-treat
1100   ;;:version "21.1"
1101   :type gnus-article-treat-custom)
1102 (put 'gnus-treat-display-smileys 'highlight t)
1103
1104 (defcustom gnus-treat-display-picons (if (featurep 'xemacs) 'head nil)
1105   "Display picons.
1106 Valid values are nil, t, `head', `last', an integer or a predicate.
1107 See the manual for details."
1108   :group 'gnus-article-treat
1109   :type gnus-article-treat-head-custom)
1110 (put 'gnus-treat-display-picons 'highlight t)
1111
1112 (defcustom gnus-treat-capitalize-sentences nil
1113   "Capitalize sentence-starting words.
1114 Valid values are nil, t, `head', `last', an integer or a predicate.
1115 See the manual for details."
1116   :version "21.1"
1117   :group 'gnus-article-treat
1118   :type gnus-article-treat-custom)
1119
1120 (defcustom gnus-treat-fill-long-lines nil
1121   "Fill long lines.
1122 Valid values are nil, t, `head', `last', an integer or a predicate.
1123 See the manual for details."
1124   :group 'gnus-article-treat
1125   :type gnus-article-treat-custom)
1126
1127 (defcustom gnus-treat-play-sounds nil
1128   "Play sounds.
1129 Valid values are nil, t, `head', `last', an integer or a predicate.
1130 See the manual for details."
1131   :version "21.1"
1132   :group 'gnus-article-treat
1133   :type gnus-article-treat-custom)
1134
1135 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1136   "Decode an article as `default-mime-charset'.  For instance, if you want to
1137 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1138 you could set this variable to something like: nil for don't decode, t for
1139 decode the body, '(or header t) for the whole article, etc."
1140   :group 'gnus-article-treat
1141   :type '(radio (const :tag "Off" nil)
1142                 (const :tag "Decode body" t)
1143                 (const :tag "Decode all" (or head t))))
1144
1145 (defcustom gnus-treat-translate nil
1146   "Translate articles from one language to another.
1147 Valid values are nil, t, `head', `last', an integer or a predicate.
1148 See the manual for details."
1149   :version "21.1"
1150   :group 'gnus-article-treat
1151   :type gnus-article-treat-custom)
1152
1153 (defcustom gnus-treat-x-pgp-sig nil
1154   "Verify X-PGP-Sig.
1155 To automatically treat X-PGP-Sig, set it to head.
1156 Valid values are nil, t, `head', `last', an integer or a predicate.
1157 See the manual for details."
1158   :group 'gnus-article-treat
1159   :group 'mime-security
1160   :type gnus-article-treat-custom)
1161
1162 (defvar gnus-article-encrypt-protocol-alist
1163   '(("PGP" . mml2015-self-encrypt)))
1164
1165 ;; Set to nil if more than one protocol added to
1166 ;; gnus-article-encrypt-protocol-alist.
1167 (defcustom gnus-article-encrypt-protocol "PGP"
1168   "The protocol used for encrypt articles.
1169 It is a string, such as \"PGP\". If nil, ask user."
1170   :type 'string
1171   :group 'mime-security)
1172
1173 ;;; Internal variables
1174
1175 (defvar gnus-english-month-names
1176   '("January" "February" "March" "April" "May" "June" "July" "August"
1177     "September" "October" "November" "December"))
1178
1179 (defvar article-goto-body-goes-to-point-min-p nil)
1180 (defvar gnus-article-wash-types nil)
1181 (defvar gnus-article-emphasis-alist nil)
1182
1183 (defvar gnus-article-mime-handle-alist-1 nil)
1184 (defvar gnus-treatment-function-alist
1185   `((gnus-treat-decode-article-as-default-mime-charset
1186      gnus-article-decode-article-as-default-mime-charset)
1187     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1188     (gnus-treat-strip-banner gnus-article-strip-banner)
1189     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1190     (gnus-treat-buttonize gnus-article-add-buttons)
1191     (gnus-treat-fill-article gnus-article-fill-cited-article)
1192     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1193     (gnus-treat-strip-cr gnus-article-remove-cr)
1194     (gnus-treat-display-xface gnus-article-display-x-face)
1195     (gnus-treat-date-ut gnus-article-date-ut)
1196     (gnus-treat-date-local gnus-article-date-local)
1197     (gnus-treat-date-english gnus-article-date-english)
1198     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1199     (gnus-treat-date-original gnus-article-date-original)
1200     (gnus-treat-date-user-defined gnus-article-date-user)
1201     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1202     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1203     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1204     (gnus-treat-hide-signature gnus-article-hide-signature)
1205     (gnus-treat-hide-citation gnus-article-hide-citation)
1206     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1207     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1208     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1209     (gnus-treat-strip-pgp gnus-article-hide-pgp)
1210     (gnus-treat-strip-pem gnus-article-hide-pem)
1211     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1212     (gnus-treat-emphasize gnus-article-emphasize)
1213     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1214     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1215     (gnus-treat-strip-trailing-blank-lines
1216      gnus-article-remove-trailing-blank-lines)
1217     (gnus-treat-strip-leading-blank-lines
1218      gnus-article-strip-leading-blank-lines)
1219     (gnus-treat-strip-multiple-blank-lines
1220      gnus-article-strip-multiple-blank-lines)
1221     (gnus-treat-overstrike gnus-article-treat-overstrike)
1222     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1223     (gnus-treat-display-smileys ,(if (or (featurep 'xemacs)
1224                                          (>= emacs-major-version 21))
1225                                      'gnus-smiley-display
1226                                    'gnus-article-smiley-display))
1227     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1228     (gnus-treat-display-picons gnus-article-display-picons)
1229     (gnus-treat-play-sounds gnus-earcon-display)))
1230
1231 (defvar gnus-article-mime-handle-alist nil)
1232 (defvar article-lapsed-timer nil)
1233 (defvar gnus-article-current-summary nil)
1234
1235 (defvar gnus-article-mode-syntax-table
1236   (let ((table (copy-syntax-table text-mode-syntax-table)))
1237     ;; This causes the citation match run O(2^n).
1238     ;; (modify-syntax-entry ?- "w" table)
1239     (modify-syntax-entry ?> ")" table)
1240     (modify-syntax-entry ?< "(" table)
1241     table)
1242   "Syntax table used in article mode buffers.
1243 Initialized from `text-mode-syntax-table.")
1244
1245 (defvar gnus-save-article-buffer nil)
1246
1247 (defvar gnus-article-mode-line-format-alist
1248   (nconc '((?w (gnus-article-wash-status) ?s)
1249            (?m (gnus-article-mime-part-status) ?s))
1250          gnus-summary-mode-line-format-alist))
1251
1252 (defvar gnus-number-of-articles-to-be-saved nil)
1253
1254 (defvar gnus-inhibit-hiding nil)
1255
1256 (defsubst gnus-article-hide-text (b e props)
1257   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1258   (gnus-add-text-properties-when 'article-type nil b e props)
1259   (when (memq 'intangible props)
1260     (put-text-property
1261      (max (1- b) (point-min))
1262      b 'intangible (cddr (memq 'intangible props)))))
1263
1264 (defsubst gnus-article-unhide-text (b e)
1265   "Remove hidden text properties from region between B and E."
1266   (remove-text-properties b e gnus-hidden-properties)
1267   (when (memq 'intangible gnus-hidden-properties)
1268     (put-text-property (max (1- b) (point-min))
1269                        b 'intangible nil)))
1270
1271 (defun gnus-article-hide-text-type (b e type)
1272   "Hide text of TYPE between B and E."
1273   (push type gnus-article-wash-types)
1274   (gnus-article-hide-text
1275    b e (cons 'article-type (cons type gnus-hidden-properties))))
1276
1277 (defun gnus-article-unhide-text-type (b e type)
1278   "Unhide text of TYPE between B and E."
1279   (setq gnus-article-wash-types
1280         (delq type gnus-article-wash-types))
1281   (remove-text-properties
1282    b e (cons 'article-type (cons type gnus-hidden-properties)))
1283   (when (memq 'intangible gnus-hidden-properties)
1284     (put-text-property (max (1- b) (point-min))
1285                        b 'intangible nil)))
1286
1287 (defun gnus-article-hide-text-of-type (type)
1288   "Hide text of TYPE in the current buffer."
1289   (save-excursion
1290     (let ((b (point-min))
1291           (e (point-max)))
1292       (while (setq b (text-property-any b e 'article-type type))
1293         (add-text-properties b (incf b) gnus-hidden-properties)))))
1294
1295 (defun gnus-article-delete-text-of-type (type)
1296   "Delete text of TYPE in the current buffer."
1297   (save-excursion
1298     (let ((b (point-min)))
1299       (while (setq b (text-property-any b (point-max) 'article-type type))
1300         (delete-region
1301          b (or (text-property-not-all b (point-max) 'article-type type)
1302                (point-max)))))))
1303
1304 (defun gnus-article-delete-invisible-text ()
1305   "Delete all invisible text in the current buffer."
1306   (save-excursion
1307     (let ((b (point-min)))
1308       (while (setq b (text-property-any b (point-max) 'invisible t))
1309         (delete-region
1310          b (or (text-property-not-all b (point-max) 'invisible t)
1311                (point-max)))))))
1312
1313 (defun gnus-article-text-type-exists-p (type)
1314   "Say whether any text of type TYPE exists in the buffer."
1315   (text-property-any (point-min) (point-max) 'article-type type))
1316
1317 (defsubst gnus-article-header-rank ()
1318   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1319   (let ((list gnus-sorted-header-list)
1320         (i 0))
1321     (while list
1322       (when (looking-at (car list))
1323         (setq list nil))
1324       (setq list (cdr list))
1325       (incf i))
1326     i))
1327
1328 (defun article-hide-headers (&optional arg delete)
1329   "Hide unwanted headers and possibly sort them as well."
1330   (interactive (gnus-article-hidden-arg))
1331   ;; Lars said that this function might be inhibited.
1332   (if (gnus-article-check-hidden-text 'headers arg)
1333       (progn
1334         ;; Show boring headers as well.
1335         (gnus-article-show-hidden-text 'boring-headers)
1336         (when (eq 1 (point-min))
1337           (set-window-start (get-buffer-window (current-buffer)) 1)))
1338     (unless gnus-inhibit-hiding
1339       (save-excursion
1340         (save-restriction
1341           (let ((inhibit-read-only t)
1342                 (case-fold-search t)
1343                 (max (1+ (length gnus-sorted-header-list)))
1344                 (ignored (when (not gnus-visible-headers)
1345                            (cond ((stringp gnus-ignored-headers)
1346                                   gnus-ignored-headers)
1347                                  ((listp gnus-ignored-headers)
1348                                   (mapconcat 'identity gnus-ignored-headers
1349                                              "\\|")))))
1350                 (visible
1351                  (cond ((stringp gnus-visible-headers)
1352                         gnus-visible-headers)
1353                        ((and gnus-visible-headers
1354                              (listp gnus-visible-headers))
1355                         (mapconcat 'identity gnus-visible-headers "\\|"))))
1356                 (inhibit-point-motion-hooks t)
1357                 beg)
1358             ;; First we narrow to just the headers.
1359             (article-narrow-to-head)
1360             ;; Hide any "From " lines at the beginning of (mail) articles.
1361             (while (looking-at "From ")
1362               (forward-line 1))
1363             (unless (bobp)
1364               (if delete
1365                   (delete-region (point-min) (point))
1366                 (gnus-article-hide-text (point-min) (point)
1367                                         (nconc (list 'article-type 'headers)
1368                                                gnus-hidden-properties))))
1369             ;; Then treat the rest of the header lines.
1370             ;; Then we use the two regular expressions
1371             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1372             ;; select which header lines is to remain visible in the
1373             ;; article buffer.
1374             (while (re-search-forward "^[^ \t:]*:" nil t)
1375               (beginning-of-line)
1376               ;; Mark the rank of the header.
1377               (put-text-property
1378                (point) (1+ (point)) 'message-rank
1379                (if (or (and visible (looking-at visible))
1380                        (and ignored
1381                             (not (looking-at ignored))))
1382                    (gnus-article-header-rank)
1383                  (+ 2 max)))
1384               (forward-line 1))
1385             (message-sort-headers-1)
1386             (when (setq beg (text-property-any
1387                              (point-min) (point-max) 'message-rank (+ 2 max)))
1388               ;; We delete or make invisible the unwanted headers.
1389               (push 'headers gnus-article-wash-types)
1390               (if delete
1391                   (progn
1392                     (add-text-properties
1393                      (point-min) (+ 5 (point-min))
1394                      '(article-type headers dummy-invisible t))
1395                     (delete-region beg (point-max)))
1396                 (gnus-article-hide-text-type beg (point-max) 'headers))))))))
1397   )
1398
1399 (defun article-hide-boring-headers (&optional arg)
1400   "Toggle hiding of headers that aren't very interesting.
1401 If given a negative prefix, always show; if given a positive prefix,
1402 always hide."
1403   (interactive (gnus-article-hidden-arg))
1404   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1405              (not gnus-show-all-headers))
1406     (save-excursion
1407       (save-restriction
1408         (let ((buffer-read-only nil)
1409               (list gnus-boring-article-headers)
1410               (inhibit-point-motion-hooks t)
1411               elem)
1412           (article-narrow-to-head)
1413           (while list
1414             (setq elem (pop list))
1415             (goto-char (point-min))
1416             (cond
1417              ;; Hide empty headers.
1418              ((eq elem 'empty)
1419               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1420                 (forward-line -1)
1421                 (gnus-article-hide-text-type
1422                  (progn (beginning-of-line) (point))
1423                  (progn
1424                    (end-of-line)
1425                    (if (re-search-forward "^[^ \t]" nil t)
1426                        (match-beginning 0)
1427                      (point-max)))
1428                  'boring-headers)))
1429              ;; Hide boring Newsgroups header.
1430              ((eq elem 'newsgroups)
1431               (when (gnus-string-equal
1432                      (gnus-fetch-field "newsgroups")
1433                      (gnus-group-real-name
1434                       (if (boundp 'gnus-newsgroup-name)
1435                           gnus-newsgroup-name
1436                         "")))
1437                 (gnus-article-hide-header "newsgroups")))
1438              ((eq elem 'to-address)
1439               (let ((to (message-fetch-field "to"))
1440                     (to-address
1441                      (gnus-parameter-to-address
1442                       (if (boundp 'gnus-newsgroup-name)
1443                           gnus-newsgroup-name ""))))
1444                 (when (and to to-address
1445                            (ignore-errors
1446                              (gnus-string-equal
1447                               ;; only one address in To
1448                               (nth 1 (mail-extract-address-components to))
1449                               to-address)))
1450                   (gnus-article-hide-header "to"))))
1451              ((eq elem 'followup-to)
1452               (when (gnus-string-equal
1453                      (message-fetch-field "followup-to")
1454                      (message-fetch-field "newsgroups"))
1455                 (gnus-article-hide-header "followup-to")))
1456              ((eq elem 'reply-to)
1457               (let ((from (message-fetch-field "from"))
1458                     (reply-to (message-fetch-field "reply-to")))
1459                 (when (and
1460                        from reply-to
1461                        (ignore-errors
1462                          (gnus-string-equal
1463                           (nth 1 (mail-extract-address-components from))
1464                           (nth 1 (mail-extract-address-components reply-to)))))
1465                   (gnus-article-hide-header "reply-to"))))
1466              ((eq elem 'date)
1467               (let ((date (message-fetch-field "date")))
1468                 (when (and date
1469                            (< (days-between (current-time-string) date)
1470                               4))
1471                   (gnus-article-hide-header "date"))))
1472              ((eq elem 'long-to)
1473               (let ((to (message-fetch-field "to"))
1474                     (cc (message-fetch-field "cc")))
1475                 (when (> (length to) 1024)
1476                   (gnus-article-hide-header "to"))
1477                 (when (> (length cc) 1024)
1478                   (gnus-article-hide-header "cc"))))
1479              ((eq elem 'many-to)
1480               (let ((to-count 0)
1481                     (cc-count 0))
1482                 (goto-char (point-min))
1483                 (while (re-search-forward "^to:" nil t)
1484                   (setq to-count (1+ to-count)))
1485                 (when (> to-count 1)
1486                   (while (> to-count 0)
1487                     (goto-char (point-min))
1488                     (save-restriction
1489                       (re-search-forward "^to:" nil nil to-count)
1490                       (forward-line -1)
1491                       (narrow-to-region (point) (point-max))
1492                       (gnus-article-hide-header "to"))
1493                     (setq to-count (1- to-count))))
1494                 (goto-char (point-min))
1495                 (while (re-search-forward "^cc:" nil t)
1496                   (setq cc-count (1+ cc-count)))
1497                 (when (> cc-count 1)
1498                   (while (> cc-count 0)
1499                     (goto-char (point-min))
1500                     (save-restriction
1501                       (re-search-forward "^cc:" nil nil cc-count)
1502                       (forward-line -1)
1503                       (narrow-to-region (point) (point-max))
1504                       (gnus-article-hide-header "cc"))
1505                     (setq cc-count (1- cc-count)))))))))))))
1506
1507 (defun gnus-article-hide-header (header)
1508   (save-excursion
1509     (goto-char (point-min))
1510     (when (re-search-forward (concat "^" header ":") nil t)
1511       (gnus-article-hide-text-type
1512        (progn (beginning-of-line) (point))
1513        (progn
1514          (end-of-line)
1515          (if (re-search-forward "^[^ \t]" nil t)
1516              (match-beginning 0)
1517            (point-max)))
1518        'boring-headers))))
1519
1520 (defun article-toggle-headers (&optional arg)
1521   "Toggle hiding of headers.  If given a negative prefix, always show;
1522 if given a positive prefix, always hide."
1523   (interactive (gnus-article-hidden-arg))
1524   (let ((force (when (numberp arg)
1525                  (cond ((> arg 0) 'always-hide)
1526                        ((< arg 0) 'always-show))))
1527         (window (get-buffer-window gnus-article-buffer))
1528         (header-end (point-min))
1529         header-start field-end field-start
1530         (inhibit-point-motion-hooks t)
1531         (inhibit-read-only t))
1532     (save-restriction
1533       (widen)
1534       (while (and (setq header-start
1535                         (text-property-any header-end (point-max)
1536                                            'article-treated-header t))
1537                   (setq header-end
1538                         (text-property-not-all header-start (point-max)
1539                                                'article-treated-header t)))
1540         (setq field-end header-start)
1541         (cond
1542          (;; Hide exposed invisible fields.
1543           (and (not (eq 'always-show force))
1544                (setq field-start
1545                      (text-property-any field-end header-end
1546                                         'exposed-invisible-field t)))
1547           (while (and field-start
1548                       (setq field-end (text-property-not-all
1549                                        field-start header-end
1550                                        'exposed-invisible-field t)))
1551             (add-text-properties field-start field-end gnus-hidden-properties)
1552             (setq field-start (text-property-any field-end header-end
1553                                                  'exposed-invisible-field t)))
1554           (put-text-property header-start header-end
1555                              'exposed-invisible-field nil))
1556          (;; Expose invisible fields.
1557           (and (not (eq 'always-hide force))
1558                (setq field-start
1559                      (text-property-any field-end header-end 'invisible t)))
1560           (while (and field-start
1561                       (setq field-end (text-property-not-all
1562                                        field-start header-end
1563                                        'invisible t)))
1564             ;; If the invisible text is not terminated with newline, we
1565             ;; won't expose it.  Because it may be created by x-face-mule.
1566             ;; BTW, XEmacs sometimes fail in putting a invisible text
1567             ;; property with `gnus-article-hide-text' (really?).  In that
1568             ;; case, the invisible text might be started from the middle of
1569             ;; a line so we will expose the sort of thing.
1570             (when (or (not (or (eq header-start field-start)
1571                                (eq ?\n (char-before field-start))))
1572                       (eq ?\n (char-before field-end)))
1573               (remove-text-properties field-start field-end
1574                                       gnus-hidden-properties)
1575               (put-text-property field-start field-end
1576                                  'exposed-invisible-field t))
1577             (setq field-start (text-property-any field-end header-end
1578                                                  'invisible t))))
1579          (;; Hide fields.
1580           (not (eq 'always-show force))
1581           (narrow-to-region header-start header-end)
1582           (article-hide-headers)
1583           ;; Re-display X-Face image under XEmacs.
1584           (when (and (featurep 'xemacs)
1585                      (gnus-functionp gnus-article-x-face-command))
1586             (let ((func (cadr (assq 'gnus-treat-display-xface
1587                                     gnus-treatment-function-alist)))
1588                   (condition 'head))
1589               (when (and (not gnus-inhibit-treatment)
1590                          func
1591                          (gnus-treat-predicate gnus-treat-display-xface))
1592                 (funcall func)
1593                 (put-text-property header-start header-end 'read-only nil))))
1594           (widen))
1595          ))
1596       (goto-char (point-min))
1597       (when window
1598         (set-window-start window (point-min))))))
1599
1600 (defvar gnus-article-normalized-header-length 40
1601   "Length of normalized headers.")
1602
1603 (defun article-normalize-headers ()
1604   "Make all header lines 40 characters long."
1605   (interactive)
1606   (let ((buffer-read-only nil)
1607         column)
1608     (save-excursion
1609       (save-restriction
1610         (article-narrow-to-head)
1611         (while (not (eobp))
1612           (cond
1613            ((< (setq column (- (gnus-point-at-eol) (point)))
1614                gnus-article-normalized-header-length)
1615             (end-of-line)
1616             (insert (make-string
1617                      (- gnus-article-normalized-header-length column)
1618                      ? )))
1619            ((> column gnus-article-normalized-header-length)
1620             (gnus-put-text-property
1621              (progn
1622                (forward-char gnus-article-normalized-header-length)
1623                (point))
1624              (gnus-point-at-eol)
1625              'invisible t))
1626            (t
1627             ;; Do nothing.
1628             ))
1629           (forward-line 1))))))
1630
1631 (defun article-treat-dumbquotes ()
1632   "Translate M****s*** sm*rtq**t*s into proper text.
1633 Note that this function guesses whether a character is a sm*rtq**t* or
1634 not, so it should only be used interactively.
1635
1636 Sm*rtq**t*s are M****s***'s unilateral extension to the character map
1637 in an attempt to provide more quoting characters.  If you see
1638 something like \\222 or \\264 where you're expecting some kind of
1639 apostrophe or quotation mark, then try this wash."
1640   (interactive)
1641   (article-translate-strings gnus-article-dumbquotes-map))
1642
1643 (defun article-translate-characters (from to)
1644   "Translate all characters in the body of the article according to FROM and TO.
1645 FROM is a string of characters to translate from; to is a string of
1646 characters to translate to."
1647   (save-excursion
1648     (when (article-goto-body)
1649       (let ((buffer-read-only nil)
1650             (x (make-string 225 ?x))
1651             (i -1))
1652         (while (< (incf i) (length x))
1653           (aset x i i))
1654         (setq i 0)
1655         (while (< i (length from))
1656           (aset x (aref from i) (aref to i))
1657           (incf i))
1658         (translate-region (point) (point-max) x)))))
1659
1660 (defun article-translate-strings (map)
1661   "Translate all string in the body of the article according to MAP.
1662 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1663   (save-excursion
1664     (when (article-goto-body)
1665       (let ((buffer-read-only nil)
1666             elem)
1667         (while (setq elem (pop map))
1668           (save-excursion
1669             (while (search-forward (car elem) nil t)
1670               (replace-match (cadr elem)))))))))
1671
1672 (defun article-treat-overstrike ()
1673   "Translate overstrikes into bold text."
1674   (interactive)
1675   (save-excursion
1676     (when (article-goto-body)
1677       (let ((buffer-read-only nil))
1678         (while (search-forward "\b" nil t)
1679           (let ((next (char-after))
1680                 start end previous)
1681             (backward-char 2)
1682             (setq start (point)
1683                   previous (char-after))
1684             (forward-char 3)
1685             (setq end (point))
1686             (backward-char)
1687             ;; We do the boldification/underlining by hiding the
1688             ;; overstrikes and putting the proper text property
1689             ;; on the letters.
1690             (cond
1691              ((eq next previous)
1692               (gnus-article-hide-text-type start (point) 'overstrike)
1693               (put-text-property (point) end 'face 'bold))
1694              ((eq next ?_)
1695               (gnus-article-hide-text-type
1696                (1- (point)) (1+ (point)) 'overstrike)
1697               (put-text-property
1698                start (1- (point)) 'face 'underline))
1699              ((eq previous ?_)
1700               (gnus-article-hide-text-type start (point) 'overstrike)
1701               (put-text-property
1702                (point) end 'face 'underline)))))))))
1703
1704 (defun article-fill-long-lines ()
1705   "Fill lines that are wider than the window width."
1706   (interactive)
1707   (save-excursion
1708     (let ((buffer-read-only nil)
1709           (width (window-width (get-buffer-window (current-buffer)))))
1710       (save-restriction
1711         (article-goto-body)
1712         (let ((adaptive-fill-mode nil)) ;Why?  -sm
1713           (while (not (eobp))
1714             (end-of-line)
1715             (when (>= (current-column) (min fill-column width))
1716               (narrow-to-region (point) (gnus-point-at-bol))
1717               (fill-paragraph nil)
1718               (goto-char (point-max))
1719               (widen))
1720             (forward-line 1)))))))
1721
1722 (defun article-capitalize-sentences ()
1723   "Capitalize the first word in each sentence."
1724   (interactive)
1725   (save-excursion
1726     (let ((buffer-read-only nil)
1727           (paragraph-start "^[\n\^L]"))
1728       (article-goto-body)
1729       (while (not (eobp))
1730         (capitalize-word 1)
1731         (forward-sentence)))))
1732
1733 (defun article-remove-cr ()
1734   "Remove trailing CRs and then translate remaining CRs into LFs."
1735   (interactive)
1736   (save-excursion
1737     (let ((buffer-read-only nil))
1738       (goto-char (point-min))
1739       (while (re-search-forward "\r+$" nil t)
1740         (replace-match "" t t))
1741       (goto-char (point-min))
1742       (while (search-forward "\r" nil t)
1743         (replace-match "\n" t t)))))
1744
1745 (defun article-remove-trailing-blank-lines ()
1746   "Remove all trailing blank lines from the article."
1747   (interactive)
1748   (save-excursion
1749     (let ((buffer-read-only nil))
1750       (goto-char (point-max))
1751       (delete-region
1752        (point)
1753        (progn
1754          (while (and (not (bobp))
1755                      (looking-at "^[ \t]*$")
1756                      (not (gnus-annotation-in-region-p
1757                            (point) (gnus-point-at-eol))))
1758            (forward-line -1))
1759          (forward-line 1)
1760          (point))))))
1761
1762 (defun article-display-x-face (&optional force)
1763   "Look for an X-Face header and display it if present."
1764   (interactive (list 'force))
1765   (save-excursion
1766     ;; Delete the old process, if any.
1767     (when (process-status "article-x-face")
1768       (delete-process "article-x-face"))
1769     (let ((inhibit-point-motion-hooks t)
1770           x-faces
1771           (case-fold-search t)
1772           from last)
1773       (save-restriction
1774         (article-narrow-to-head)
1775 ;;      (when (and buffer-read-only ;; When type `W f'
1776 ;;                 (progn
1777 ;;                   (goto-char (point-min))
1778 ;;                   (not (re-search-forward "^X-Face:[\t ]*" nil t)))
1779 ;;                 (gnus-buffer-live-p gnus-original-article-buffer))
1780 ;;        (with-current-buffer gnus-original-article-buffer
1781 ;;          (save-restriction
1782 ;;            (article-narrow-to-head)
1783 ;;            (while (re-search-forward "^X-Face:" nil t)
1784 ;;              (setq x-faces
1785 ;;                    (concat
1786 ;;                     (or x-faces "")
1787 ;;                     (buffer-substring
1788 ;;                      (match-beginning 0)
1789 ;;                      (1- (re-search-forward
1790 ;;                           "^\\($\\|[^ \t]\\)" nil t))))))))
1791 ;;        (if x-faces
1792 ;;            (let (point start bface eface buffer-read-only)
1793 ;;              (goto-char (point-max))
1794 ;;              (forward-line -1)
1795 ;;              (setq bface (get-text-property (gnus-point-at-bol) 'face)
1796 ;;                    eface (get-text-property (1- (gnus-point-at-eol)) 'face))
1797 ;;              (goto-char (point-max))
1798 ;;              (setq point (point))
1799 ;;              (insert x-faces)
1800 ;;              (goto-char point)
1801 ;;              (while (looking-at "\\([^:]+\\): *")
1802 ;;                (put-text-property (match-beginning 1) (1+ (match-end 1))
1803 ;;                                   'face bface)
1804 ;;                (setq start (match-end 0))
1805 ;;                (forward-line 1)
1806 ;;                (while (looking-at "[\t ]")
1807 ;;                  (forward-line 1))
1808 ;;                (put-text-property start (point)
1809 ;;                                   'face eface)))))
1810         (goto-char (point-min))
1811         (setq from (message-fetch-field "from"))
1812         (goto-char (point-min))
1813         (while (and gnus-article-x-face-command
1814                     (not last)
1815                     (or force
1816                         ;; Check whether this face is censored.
1817                         (not gnus-article-x-face-too-ugly)
1818                         (and gnus-article-x-face-too-ugly from
1819                              (not (string-match gnus-article-x-face-too-ugly
1820                                                 from))))
1821                     ;; Has to be present.
1822                     (re-search-forward "^X-Face:[\t ]*" nil t))
1823           ;; This used to try to do multiple faces (`while' instead of
1824           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1825           ;; work (b) it can crash some versions of Emacs (c) are
1826           ;; multiple faces really something to encourage?
1827           (when (stringp gnus-article-x-face-command)
1828             (setq last t))
1829           ;; We now have the area of the buffer where the X-Face is stored.
1830           (save-excursion
1831             (let ((beg (point))
1832                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t)))
1833                   buffer-read-only)
1834               ;; We display the face.
1835               (if (symbolp gnus-article-x-face-command)
1836                   ;; The command is a lisp function, so we call it.
1837                   (if (gnus-functionp gnus-article-x-face-command)
1838                       (funcall gnus-article-x-face-command beg end)
1839                     (error "%s is not a function" gnus-article-x-face-command))
1840                 ;; The command is a string, so we interpret the command
1841                 ;; as a, well, command, and fork it off.
1842                 (let ((process-connection-type nil))
1843                   (process-kill-without-query
1844                    (start-process
1845                     "article-x-face" nil shell-file-name shell-command-switch
1846                     gnus-article-x-face-command))
1847                   (process-send-region "article-x-face" beg end)
1848                   (process-send-eof "article-x-face"))))))))))
1849
1850 (defun article-decode-mime-words ()
1851   "Decode all MIME-encoded words in the article."
1852   (interactive)
1853   (save-excursion
1854     (set-buffer gnus-article-buffer)
1855     (let ((inhibit-point-motion-hooks t)
1856           buffer-read-only
1857           (mail-parse-charset gnus-newsgroup-charset)
1858           (mail-parse-ignored-charsets
1859            (save-excursion (set-buffer gnus-summary-buffer)
1860                            gnus-newsgroup-ignored-charsets)))
1861       (mail-decode-encoded-word-region (point-min) (point-max)))))
1862
1863 (defun article-decode-charset (&optional prompt)
1864   "Decode charset-encoded text in the article.
1865 If PROMPT (the prefix), prompt for a coding system to use."
1866   (interactive "P")
1867   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1868         buffer-read-only
1869         (mail-parse-charset gnus-newsgroup-charset)
1870         (mail-parse-ignored-charsets
1871          (save-excursion (condition-case nil
1872                              (set-buffer gnus-summary-buffer)
1873                            (error))
1874                          gnus-newsgroup-ignored-charsets))
1875         ct cte ctl charset format)
1876     (save-excursion
1877       (save-restriction
1878         (article-narrow-to-head)
1879         (setq ct (message-fetch-field "Content-Type" t)
1880               cte (message-fetch-field "Content-Transfer-Encoding" t)
1881               ctl (and ct (ignore-errors
1882                             (mail-header-parse-content-type ct)))
1883               charset (cond
1884                        (prompt
1885                         (mm-read-coding-system "Charset to decode: "))
1886                        (ctl
1887                         (mail-content-type-get ctl 'charset)))
1888               format (and ctl (mail-content-type-get ctl 'format)))
1889         (when cte
1890           (setq cte (mail-header-strip cte)))
1891         (if (and ctl (not (string-match "/" (car ctl))))
1892             (setq ctl nil))
1893         (goto-char (point-max)))
1894       (forward-line 1)
1895       (save-restriction
1896         (narrow-to-region (point) (point-max))
1897         (when (and (eq mail-parse-charset 'gnus-decoded)
1898                    (eq (mm-body-7-or-8) '8bit))
1899           ;; The text code could have been decoded.
1900           (setq charset mail-parse-charset))
1901         (when (and (or (not ctl)
1902                        (equal (car ctl) "text/plain"))
1903                    (not format)) ;; article with format will decode later.
1904           (mm-decode-body
1905            charset (and cte (intern (downcase
1906                                      (gnus-strip-whitespace cte))))
1907            (car ctl)))))))
1908
1909 (defun article-decode-encoded-words ()
1910   "Remove encoded-word encoding from headers."
1911   (let (buffer-read-only)
1912     (let ((charset (save-excursion
1913                      (set-buffer gnus-summary-buffer)
1914                      default-mime-charset)))
1915       (mime-decode-header-in-buffer charset))))
1916
1917 (defun article-decode-group-name ()
1918   "Decode group names in `Newsgroups:'."
1919   (let ((inhibit-point-motion-hooks t)
1920         buffer-read-only
1921         (method (gnus-find-method-for-group gnus-newsgroup-name)))
1922     (when (and (or gnus-group-name-charset-method-alist
1923                    gnus-group-name-charset-group-alist)
1924                (gnus-buffer-live-p gnus-original-article-buffer))
1925       (when (nnmail-fetch-field "Newsgroups")
1926         (nnheader-replace-header "Newsgroups"
1927                                  (gnus-decode-newsgroups
1928                                   (with-current-buffer
1929                                       gnus-original-article-buffer
1930                                     (nnmail-fetch-field "Newsgroups"))
1931                                   gnus-newsgroup-name method)))
1932       (when (nnmail-fetch-field "Followup-To")
1933         (nnheader-replace-header "Followup-To"
1934                                  (gnus-decode-newsgroups
1935                                   (with-current-buffer
1936                                       gnus-original-article-buffer
1937                                     (nnmail-fetch-field "Followup-To"))
1938                                   gnus-newsgroup-name method))))))
1939
1940 (defun article-de-quoted-unreadable (&optional force read-charset)
1941   "Translate a quoted-printable-encoded article.
1942 If FORCE, decode the article whether it is marked as quoted-printable
1943 or not.
1944 If READ-CHARSET, ask for a coding system."
1945   (interactive (list 'force current-prefix-arg))
1946   (save-excursion
1947     (let ((buffer-read-only nil) type charset)
1948       (if (gnus-buffer-live-p gnus-original-article-buffer)
1949           (with-current-buffer gnus-original-article-buffer
1950             (setq type
1951                   (gnus-fetch-field "content-transfer-encoding"))
1952             (let* ((ct (gnus-fetch-field "content-type"))
1953                    (ctl (and ct
1954                              (ignore-errors
1955                                (mail-header-parse-content-type ct)))))
1956               (setq charset (and ctl
1957                                  (mail-content-type-get ctl 'charset)))
1958               (if (stringp charset)
1959                   (setq charset (intern (downcase charset)))))))
1960       (if read-charset
1961           (setq charset (mm-read-coding-system "Charset: " charset)))
1962       (unless charset
1963         (setq charset gnus-newsgroup-charset))
1964       (when (or force
1965                 (and type (let ((case-fold-search t))
1966                             (string-match "quoted-printable" type))))
1967         (article-goto-body)
1968         (quoted-printable-decode-region
1969          (point) (point-max) (mm-charset-to-coding-system charset))))))
1970
1971 (defun article-de-base64-unreadable (&optional force read-charset)
1972   "Translate a base64 article.
1973 If FORCE, decode the article whether it is marked as base64 not.
1974 If READ-CHARSET, ask for a coding system."
1975   (interactive (list 'force current-prefix-arg))
1976   (save-excursion
1977     (let ((buffer-read-only nil) type charset)
1978       (if (gnus-buffer-live-p gnus-original-article-buffer)
1979           (with-current-buffer gnus-original-article-buffer
1980             (setq type
1981                   (gnus-fetch-field "content-transfer-encoding"))
1982             (let* ((ct (gnus-fetch-field "content-type"))
1983                    (ctl (and ct
1984                              (ignore-errors
1985                                (mail-header-parse-content-type ct)))))
1986               (setq charset (and ctl
1987                                  (mail-content-type-get ctl 'charset)))
1988               (if (stringp charset)
1989                   (setq charset (intern (downcase charset)))))))
1990       (if read-charset
1991           (setq charset (mm-read-coding-system "Charset: " charset)))
1992       (unless charset
1993         (setq charset gnus-newsgroup-charset))
1994       (when (or force
1995                 (and type (let ((case-fold-search t))
1996                             (string-match "base64" type))))
1997         (article-goto-body)
1998         (save-restriction
1999           (narrow-to-region (point) (point-max))
2000           (base64-decode-region (point-min) (point-max))
2001           (mm-decode-coding-region
2002            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2003
2004 (eval-when-compile
2005   (require 'rfc1843))
2006
2007 (defun article-decode-HZ ()
2008   "Translate a HZ-encoded article."
2009   (interactive)
2010   (require 'rfc1843)
2011   (save-excursion
2012     (let ((buffer-read-only nil))
2013       (rfc1843-decode-region (point-min) (point-max)))))
2014
2015 (defun article-wash-html (&optional read-charset)
2016   "Format an html article.
2017 If READ-CHARSET, ask for a coding system."
2018   (interactive "P")
2019   (save-excursion
2020     (let ((buffer-read-only nil)
2021           charset)
2022       (if (gnus-buffer-live-p gnus-original-article-buffer)
2023           (with-current-buffer gnus-original-article-buffer
2024             (let* ((ct (gnus-fetch-field "content-type"))
2025                    (ctl (and ct
2026                              (ignore-errors
2027                                (mail-header-parse-content-type ct)))))
2028               (setq charset (and ctl
2029                                  (mail-content-type-get ctl 'charset)))
2030               (if (stringp charset)
2031                   (setq charset (intern (downcase charset)))))))
2032       (if read-charset
2033           (setq charset (mm-read-coding-system "Charset: " charset)))
2034       (unless charset
2035         (setq charset gnus-newsgroup-charset))
2036       (article-goto-body)
2037       (save-window-excursion
2038         (save-restriction
2039           (narrow-to-region (point) (point-max))
2040           (mm-setup-w3)
2041           (let ((w3-strict-width (window-width))
2042                 (url-standalone-mode t))
2043             (condition-case var
2044                 (w3-region (point-min) (point-max))
2045               (error))))))))
2046
2047 (defun article-hide-list-identifiers ()
2048   "Remove list identifies from the Subject header.
2049 The `gnus-list-identifiers' variable specifies what to do."
2050   (interactive)
2051   (let ((inhibit-point-motion-hooks t)
2052         (regexp (if (consp gnus-list-identifiers)
2053                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2054                   gnus-list-identifiers))
2055         buffer-read-only)
2056     (when regexp
2057       (save-excursion
2058         (save-restriction
2059           (article-narrow-to-head)
2060           (goto-char (point-min))
2061           (while (re-search-forward
2062                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2063                   nil t)
2064             (delete-region (match-beginning 2) (match-end 0))
2065             (beginning-of-line))
2066           (when (re-search-forward
2067                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2068             (delete-region (match-beginning 1) (match-end 1))))))))
2069
2070 (defun article-hide-pgp ()
2071   "Remove any PGP headers and signatures in the current article."
2072   (interactive)
2073   (save-excursion
2074     (save-restriction
2075       (let ((inhibit-point-motion-hooks t)
2076             buffer-read-only beg end)
2077         (article-goto-body)
2078         ;; Hide the "header".
2079         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
2080           (push 'pgp gnus-article-wash-types)
2081           (delete-region (match-beginning 0) (match-end 0))
2082           ;; Remove armor headers (rfc2440 6.2)
2083           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
2084                                      (point)))
2085           (setq beg (point))
2086           ;; Hide the actual signature.
2087           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
2088                (setq end (1+ (match-beginning 0)))
2089                (delete-region
2090                 end
2091                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
2092                     (match-end 0)
2093                   ;; Perhaps we shouldn't hide to the end of the buffer
2094                   ;; if there is no end to the signature?
2095                   (point-max))))
2096           ;; Hide "- " PGP quotation markers.
2097           (when (and beg end)
2098             (narrow-to-region beg end)
2099             (goto-char (point-min))
2100             (while (re-search-forward "^- " nil t)
2101               (delete-region
2102                (match-beginning 0) (match-end 0)))
2103             (widen))
2104           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
2105
2106 (defun article-hide-pem (&optional arg)
2107   "Toggle hiding of any PEM headers and signatures in the current article.
2108 If given a negative prefix, always show; if given a positive prefix,
2109 always hide."
2110   (interactive (gnus-article-hidden-arg))
2111   (unless (gnus-article-check-hidden-text 'pem arg)
2112     (save-excursion
2113       (let (buffer-read-only end)
2114         (goto-char (point-min))
2115         ;; Hide the horrendously ugly "header".
2116         (when (and (search-forward
2117                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2118                     nil t)
2119                    (setq end (1+ (match-beginning 0))))
2120           (push 'pem gnus-article-wash-types)
2121           (gnus-article-hide-text-type
2122            end
2123            (if (search-forward "\n\n" nil t)
2124                (match-end 0)
2125              (point-max))
2126            'pem)
2127           ;; Hide the trailer as well
2128           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2129                                 nil t)
2130             (gnus-article-hide-text-type
2131              (match-beginning 0) (match-end 0) 'pem)))))))
2132
2133 (defun article-strip-banner ()
2134   "Strip the banner specified by the `banner' group parameter."
2135   (interactive)
2136   (save-excursion
2137     (save-restriction
2138       (let ((inhibit-point-motion-hooks t)
2139             (banner (gnus-parameter-banner gnus-newsgroup-name))
2140             (gnus-signature-limit nil)
2141             buffer-read-only beg end)
2142         (when banner
2143           (article-goto-body)
2144           (cond
2145            ((eq banner 'signature)
2146             (when (gnus-article-narrow-to-signature)
2147               (widen)
2148               (forward-line -1)
2149               (delete-region (point) (point-max))))
2150            ((symbolp banner)
2151             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2152                 (while (re-search-forward banner nil t)
2153                   (delete-region (match-beginning 0) (match-end 0)))))
2154            ((stringp banner)
2155             (while (re-search-forward banner nil t)
2156               (delete-region (match-beginning 0) (match-end 0))))))))))
2157
2158 (defun article-babel ()
2159   "Translate article using an online translation service."
2160   (interactive)
2161   (require 'babel)
2162   (save-excursion
2163     (set-buffer gnus-article-buffer)
2164     (when (article-goto-body)
2165       (let* ((buffer-read-only nil)
2166              (start (point))
2167              (end (point-max))
2168              (orig (buffer-substring start end))
2169              (trans (babel-as-string orig)))
2170         (save-restriction
2171           (narrow-to-region start end)
2172           (delete-region start end)
2173           (insert trans))))))
2174
2175 (defun article-hide-signature (&optional arg)
2176   "Hide the signature in the current article.
2177 If given a negative prefix, always show; if given a positive prefix,
2178 always hide."
2179   (interactive (gnus-article-hidden-arg))
2180   (save-excursion
2181     (save-restriction
2182       (if (interactive-p)
2183           (progn
2184             (widen)
2185             (article-goto-body))
2186         (goto-char (point-min)))
2187       (unless (gnus-article-check-hidden-text 'signature arg)
2188         (let ((buffer-read-only nil)
2189               (button (point)))
2190           (while (setq button (text-property-any button (point-max)
2191                                                  'gnus-callback
2192                                                  'gnus-signature-toggle))
2193             (setq button (text-property-not-all button (point-max)
2194                                                 'gnus-callback
2195                                                 'gnus-signature-toggle))
2196             (when (and button (not (eobp)))
2197               (gnus-article-hide-text-type
2198                (1+ button)
2199                (next-single-property-change (1+ button) 'mime-view-entity
2200                                             nil (point-max))
2201                'signature))))))))
2202
2203 (defun article-strip-headers-in-body ()
2204   "Strip offensive headers from bodies."
2205   (interactive)
2206   (save-excursion
2207     (article-goto-body)
2208     (let ((case-fold-search t))
2209       (when (looking-at "x-no-archive:")
2210         (gnus-delete-line)))))
2211
2212 (defun article-strip-leading-blank-lines ()
2213   "Remove all blank lines from the beginning of the article."
2214   (interactive)
2215   (save-excursion
2216     (let ((inhibit-point-motion-hooks t)
2217           buffer-read-only)
2218       (when (article-goto-body)
2219         (while (and (not (eobp))
2220                     (looking-at "[ \t]*$"))
2221           (gnus-delete-line))))))
2222
2223 (defun article-narrow-to-head ()
2224   "Narrow the buffer to the head of the message.
2225 Point is left at the beginning of the narrowed-to region."
2226   (narrow-to-region
2227    (goto-char (point-min))
2228    (if (search-forward "\n\n" nil 1)
2229        (1- (point))
2230      (point-max)))
2231   (goto-char (point-min)))
2232
2233 (defun article-goto-body ()
2234   "Place point at the start of the body."
2235   (goto-char (point-min))
2236   (cond
2237    ;; This variable is only bound when dealing with separate
2238    ;; MIME body parts.
2239    (article-goto-body-goes-to-point-min-p
2240     t)
2241    ((search-forward "\n\n" nil t)
2242     t)
2243    (t
2244     (goto-char (point-max))
2245     nil)))
2246
2247 (defun article-strip-multiple-blank-lines ()
2248   "Replace consecutive blank lines with one empty line."
2249   (interactive)
2250   (save-excursion
2251     (let ((inhibit-point-motion-hooks t)
2252           buffer-read-only)
2253       ;; First make all blank lines empty.
2254       (article-goto-body)
2255       (while (re-search-forward "^[ \t]+$" nil t)
2256         (unless (gnus-annotation-in-region-p
2257                  (match-beginning 0) (match-end 0))
2258           (replace-match "" nil t)))
2259       ;; Then replace multiple empty lines with a single empty line.
2260       (article-goto-body)
2261       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2262         (unless (gnus-annotation-in-region-p
2263                  (match-beginning 0) (match-end 0))
2264           (delete-region (match-beginning 1) (match-end 1)))))))
2265
2266 (defun article-strip-leading-space ()
2267   "Remove all white space from the beginning of the lines in the article."
2268   (interactive)
2269   (save-excursion
2270     (let ((inhibit-point-motion-hooks t)
2271           buffer-read-only)
2272       (article-goto-body)
2273       (while (re-search-forward "^[ \t]+" nil t)
2274         (replace-match "" t t)))))
2275
2276 (defun article-strip-trailing-space ()
2277   "Remove all white space from the end of the lines in the article."
2278   (interactive)
2279   (save-excursion
2280     (let ((inhibit-point-motion-hooks t)
2281           buffer-read-only)
2282       (article-goto-body)
2283       (while (re-search-forward "[ \t]+$" nil t)
2284         (replace-match "" t t)))))
2285
2286 (defun article-strip-blank-lines ()
2287   "Strip leading, trailing and multiple blank lines."
2288   (interactive)
2289   (article-strip-leading-blank-lines)
2290   (article-remove-trailing-blank-lines)
2291   (article-strip-multiple-blank-lines))
2292
2293 (defun article-strip-all-blank-lines ()
2294   "Strip all blank lines."
2295   (interactive)
2296   (save-excursion
2297     (let ((inhibit-point-motion-hooks t)
2298           buffer-read-only)
2299       (article-goto-body)
2300       (while (re-search-forward "^[ \t]*\n" nil t)
2301         (replace-match "" t t)))))
2302
2303 (defun gnus-article-narrow-to-signature ()
2304   "Narrow to the signature; return t if a signature is found, else nil."
2305   (let ((inhibit-point-motion-hooks t))
2306     (when (gnus-article-search-signature)
2307       (forward-line 1)
2308       ;; Check whether we have some limits to what we consider
2309       ;; to be a signature.
2310       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2311                       (list gnus-signature-limit)))
2312             limit limited)
2313         (while (setq limit (pop limits))
2314           (if (or (and (integerp limit)
2315                        (< (- (point-max) (point)) limit))
2316                   (and (floatp limit)
2317                        (< (count-lines (point) (point-max)) limit))
2318                   (and (gnus-functionp limit)
2319                        (funcall limit))
2320                   (and (stringp limit)
2321                        (not (re-search-forward limit nil t))))
2322               ()                        ; This limit did not succeed.
2323             (setq limited t
2324                   limits nil)))
2325         (unless limited
2326           (narrow-to-region (point) (point-max))
2327           t)))))
2328
2329 (defun gnus-article-search-signature ()
2330   "Search the current buffer for the signature separator.
2331 Put point at the beginning of the signature separator."
2332   (let ((cur (point)))
2333     (goto-char (point-max))
2334     (if (if (stringp gnus-signature-separator)
2335             (re-search-backward gnus-signature-separator nil t)
2336           (let ((seps gnus-signature-separator))
2337             (while (and seps
2338                         (not (re-search-backward (car seps) nil t)))
2339               (pop seps))
2340             seps))
2341         t
2342       (goto-char cur)
2343       nil)))
2344
2345 (defun gnus-article-hidden-arg ()
2346   "Return the current prefix arg as a number, or 0 if no prefix."
2347   (list (if current-prefix-arg
2348             (prefix-numeric-value current-prefix-arg)
2349           0)))
2350
2351 (defun gnus-article-check-hidden-text (type arg)
2352   "Return nil if hiding is necessary.
2353 Arg can be nil or a number.  Nil and positive means hide, negative
2354 means show, 0 means toggle."
2355   (save-excursion
2356     (save-restriction
2357       (let ((hide (gnus-article-hidden-text-p type)))
2358         (cond
2359          ((or (null arg)
2360               (> arg 0))
2361           nil)
2362          ((< arg 0)
2363           (gnus-article-show-hidden-text type)
2364           t)
2365          (t
2366           (if (eq hide 'hidden)
2367               (progn
2368                 (gnus-article-show-hidden-text type)
2369                 t)
2370             nil)))))))
2371
2372 (defun gnus-article-hidden-text-p (type)
2373   "Say whether the current buffer contains hidden text of type TYPE."
2374   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2375     (while (and pos
2376                 (not (get-text-property pos 'invisible))
2377                 (not (get-text-property pos 'dummy-invisible)))
2378       (setq pos
2379             (text-property-any (1+ pos) (point-max) 'article-type type)))
2380     (if pos
2381         'hidden
2382       nil)))
2383
2384 (defun gnus-article-show-hidden-text (type &optional dummy)
2385   "Show all hidden text of type TYPE.
2386 Originally it is hide instead of DUMMY."
2387   (let ((buffer-read-only nil)
2388         (inhibit-point-motion-hooks t))
2389     (gnus-remove-text-properties-when
2390      'article-type type
2391      (point-min) (point-max)
2392      (cons 'article-type (cons type
2393                                gnus-hidden-properties)))))
2394
2395 (defconst article-time-units
2396   `((year . ,(* 365.25 24 60 60))
2397     (week . ,(* 7 24 60 60))
2398     (day . ,(* 24 60 60))
2399     (hour . ,(* 60 60))
2400     (minute . 60)
2401     (second . 1))
2402   "Mapping from time units to seconds.")
2403
2404 (defun article-date-ut (&optional type highlight header)
2405   "Convert DATE date to universal time in the current article.
2406 If TYPE is `local', convert to local time; if it is `lapsed', output
2407 how much time has lapsed since DATE.  For `lapsed', the value of
2408 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2409 should replace the \"Date:\" one, or should be added below it."
2410   (interactive (list 'ut t))
2411   (let* ((header (or header
2412                      (and (eq 1 (point-min))
2413                           (mail-header-date (save-excursion
2414                                               (set-buffer gnus-summary-buffer)
2415                                               gnus-current-headers)))
2416                      (message-fetch-field "date")
2417                      ""))
2418          (date (if (vectorp header) (mail-header-date header)
2419                  header))
2420          (inhibit-point-motion-hooks t)
2421          bface eface date-pos)
2422     (when (and date (not (string= date "")))
2423       (save-excursion
2424         (save-restriction
2425           (article-narrow-to-head)
2426           (when (or (and (eq type 'lapsed)
2427                          gnus-article-date-lapsed-new-header
2428                          ;; Attempt to get the face of X-Sent first.
2429                          (re-search-forward "^X-Sent:[ \t]" nil t))
2430                     (re-search-forward "^Date:[ \t]" nil t)
2431                     ;; If Date is missing, try again for X-Sent.
2432                     (re-search-forward "^X-Sent:[ \t]" nil t))
2433             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2434                   date (or (get-text-property (gnus-point-at-bol)
2435                                               'original-date)
2436                            date)
2437                   eface (get-text-property (1- (gnus-point-at-eol))
2438                                            'face)))
2439           (let ((buffer-read-only nil))
2440             ;; Delete any old X-Sent headers.
2441             (when (setq date-pos
2442                         (text-property-any (point-min) (point-max)
2443                                            'article-date-lapsed t))
2444               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2445               (delete-region (match-beginning 0)
2446                              (progn (forward-line 1) (point))))
2447             (goto-char (point-min))
2448             ;; Delete any old Date headers.
2449             (while (re-search-forward "^Date:[ \t]" nil t)
2450               (unless date-pos
2451                 (setq date-pos (match-beginning 0)))
2452               (unless (and (eq type 'lapsed)
2453                            gnus-article-date-lapsed-new-header)
2454                 (delete-region (match-beginning 0)
2455                                (progn (message-next-header) (point)))))
2456             (if date-pos
2457                 (progn
2458                   (goto-char date-pos)
2459                   (unless (bolp)
2460                     ;; Possibly, Date has been deleted.
2461                     (insert "\n"))
2462                   (when (and (eq type 'lapsed)
2463                              gnus-article-date-lapsed-new-header
2464                              (looking-at "Date:"))
2465                     (forward-line 1)))
2466               (goto-char (point-min)))
2467             (insert (article-make-date-line date type))
2468             (when (eq type 'lapsed)
2469               (put-text-property (gnus-point-at-bol) (point)
2470                                  'article-date-lapsed t))
2471             (insert "\n")
2472             (forward-line -1)
2473             ;; Do highlighting.
2474             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2475               (put-text-property (match-beginning 1) (1+ (match-end 1))
2476                                  'original-date date)
2477               (put-text-property (match-beginning 1) (1+ (match-end 1))
2478                                  'face bface)
2479               (put-text-property (match-beginning 2) (match-end 2)
2480                                  'face eface))))))))
2481
2482 (defun article-make-date-line (date type)
2483   "Return a DATE line of TYPE."
2484   (unless (memq type '(local ut original user iso8601 lapsed english))
2485     (error "Unknown conversion type: %s" type))
2486   (condition-case ()
2487       (let ((time (date-to-time date)))
2488         (cond
2489          ;; Convert to the local timezone.
2490          ((eq type 'local)
2491           (let ((tz (car (current-time-zone time))))
2492             (format "Date: %s %s%02d%02d" (current-time-string time)
2493                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2494                     (/ (% (abs tz) 3600) 60))))
2495          ;; Convert to Universal Time.
2496          ((eq type 'ut)
2497           (concat "Date: "
2498                   (current-time-string
2499                    (let* ((e (parse-time-string date))
2500                           (tm (apply 'encode-time e))
2501                           (ms (car tm))
2502                           (ls (- (cadr tm) (car (current-time-zone time)))))
2503                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2504                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2505                            (t (list ms ls)))))
2506                   " UT"))
2507          ;; Get the original date from the article.
2508          ((eq type 'original)
2509           (concat "Date: " (if (string-match "\n+$" date)
2510                                (substring date 0 (match-beginning 0))
2511                              date)))
2512          ;; Let the user define the format.
2513          ((eq type 'user)
2514           (if (gnus-functionp gnus-article-time-format)
2515               (funcall gnus-article-time-format time)
2516             (concat
2517              "Date: "
2518              (format-time-string gnus-article-time-format time))))
2519          ;; ISO 8601.
2520          ((eq type 'iso8601)
2521           (let ((tz (car (current-time-zone time))))
2522             (concat
2523              "Date: "
2524              (format-time-string "%Y%m%dT%H%M%S" time)
2525              (format "%s%02d%02d"
2526                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2527                      (/ (% (abs tz) 3600) 60)))))
2528          ;; Do an X-Sent lapsed format.
2529          ((eq type 'lapsed)
2530           ;; If the date is seriously mangled, the timezone functions are
2531           ;; liable to bug out, so we ignore all errors.
2532           (let* ((now (current-time))
2533                  (real-time (subtract-time now time))
2534                  (real-sec (and real-time
2535                                 (+ (* (float (car real-time)) 65536)
2536                                    (cadr real-time))))
2537                  (sec (and real-time (abs real-sec)))
2538                  num prev)
2539             (cond
2540              ((null real-time)
2541               "X-Sent: Unknown")
2542              ((zerop sec)
2543               "X-Sent: Now")
2544              (t
2545               (concat
2546                "X-Sent: "
2547                ;; This is a bit convoluted, but basically we go
2548                ;; through the time units for years, weeks, etc,
2549                ;; and divide things to see whether that results
2550                ;; in positive answers.
2551                (mapconcat
2552                 (lambda (unit)
2553                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2554                       ;; The (remaining) seconds are too few to
2555                       ;; be divided into this time unit.
2556                       ""
2557                     ;; It's big enough, so we output it.
2558                     (setq sec (- sec (* num (cdr unit))))
2559                     (prog1
2560                         (concat (if prev ", " "") (int-to-string
2561                                                    (floor num))
2562                                 " " (symbol-name (car unit))
2563                                 (if (> num 1) "s" ""))
2564                       (setq prev t))))
2565                 article-time-units "")
2566                ;; If dates are odd, then it might appear like the
2567                ;; article was sent in the future.
2568                (if (> real-sec 0)
2569                    " ago"
2570                  " in the future"))))))
2571          ;; Display the date in proper English
2572          ((eq type 'english)
2573           (let ((dtime (decode-time time)))
2574             (concat
2575              "Date: the "
2576              (number-to-string (nth 3 dtime))
2577              (let ((digit (% (nth 3 dtime) 10)))
2578                (cond
2579                 ((memq (nth 3 dtime) '(11 12 13)) "th")
2580                 ((= digit 1) "st")
2581                 ((= digit 2) "nd")
2582                 ((= digit 3) "rd")
2583                 (t "th")))
2584              " of "
2585              (nth (1- (nth 4 dtime)) gnus-english-month-names)
2586              " "
2587              (number-to-string (nth 5 dtime))
2588              " at "
2589              (format "%02d" (nth 2 dtime))
2590              ":"
2591              (format "%02d" (nth 1 dtime)))))))
2592     (error
2593      (format "Date: %s (from Oort)" date))))
2594
2595 (defun article-date-local (&optional highlight)
2596   "Convert the current article date to the local timezone."
2597   (interactive (list t))
2598   (article-date-ut 'local highlight))
2599
2600 (defun article-date-english (&optional highlight)
2601   "Convert the current article date to something that is proper English."
2602   (interactive (list t))
2603   (article-date-ut 'english highlight))
2604
2605 (defun article-date-original (&optional highlight)
2606   "Convert the current article date to what it was originally.
2607 This is only useful if you have used some other date conversion
2608 function and want to see what the date was before converting."
2609   (interactive (list t))
2610   (article-date-ut 'original highlight))
2611
2612 (defun article-date-lapsed (&optional highlight)
2613   "Convert the current article date to time lapsed since it was sent."
2614   (interactive (list t))
2615   (article-date-ut 'lapsed highlight))
2616
2617 (defun article-update-date-lapsed ()
2618   "Function to be run from a timer to update the lapsed time line."
2619   (let (deactivate-mark)
2620     (save-excursion
2621       (ignore-errors
2622         (walk-windows
2623          (lambda (w)
2624            (set-buffer (window-buffer w))
2625            (when (eq major-mode 'gnus-article-mode)
2626              (goto-char (point-min))
2627              (when (re-search-forward "^X-Sent:" nil t)
2628                (article-date-lapsed t))))
2629          nil 'visible)))))
2630
2631 (defun gnus-start-date-timer (&optional n)
2632   "Start a timer to update the X-Sent header in the article buffers.
2633 The numerical prefix says how frequently (in seconds) the function
2634 is to run."
2635   (interactive "p")
2636   (unless n
2637     (setq n 1))
2638   (gnus-stop-date-timer)
2639   (setq article-lapsed-timer
2640         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2641
2642 (defun gnus-stop-date-timer ()
2643   "Stop the X-Sent timer."
2644   (interactive)
2645   (when article-lapsed-timer
2646     (nnheader-cancel-timer article-lapsed-timer)
2647     (setq article-lapsed-timer nil)))
2648
2649 (defun article-date-user (&optional highlight)
2650   "Convert the current article date to the user-defined format.
2651 This format is defined by the `gnus-article-time-format' variable."
2652   (interactive (list t))
2653   (article-date-ut 'user highlight))
2654
2655 (defun article-date-iso8601 (&optional highlight)
2656   "Convert the current article date to ISO8601."
2657   (interactive (list t))
2658   (article-date-ut 'iso8601 highlight))
2659
2660 (defun article-show-all ()
2661   "Show all hidden text in the article buffer."
2662   (interactive)
2663   (save-excursion
2664     (widen)
2665     (let ((buffer-read-only nil))
2666       (gnus-article-unhide-text (point-min) (point-max))
2667       (gnus-remove-text-with-property 'gnus-prev)
2668       (gnus-remove-text-with-property 'gnus-next))))
2669
2670 (defun article-show-all-headers ()
2671   "Show all hidden headers in the article buffer."
2672   (interactive)
2673   (save-excursion
2674     (save-restriction
2675       (widen)
2676       (article-narrow-to-head)
2677       (let ((buffer-read-only nil))
2678         (gnus-article-unhide-text (point-min) (point-max))))))
2679
2680 (defun article-remove-leading-whitespace ()
2681   "Remove excessive whitespace from all headers."
2682   (interactive)
2683   (save-excursion
2684     (save-restriction
2685       (let ((buffer-read-only nil))
2686         (article-narrow-to-head)
2687         (goto-char (point-min))
2688         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
2689           (delete-region (match-beginning 1) (match-end 1)))))))
2690
2691 (defun article-emphasize (&optional arg)
2692   "Emphasize text according to `gnus-emphasis-alist'."
2693   (interactive (gnus-article-hidden-arg))
2694   (unless (gnus-article-check-hidden-text 'emphasis arg)
2695     (save-excursion
2696       (let ((alist (or
2697                     (condition-case nil
2698                         (with-current-buffer gnus-summary-buffer
2699                           gnus-article-emphasis-alist)
2700                       (error))
2701                     gnus-emphasis-alist))
2702             (buffer-read-only nil)
2703             (props (append '(article-type emphasis)
2704                            gnus-hidden-properties))
2705             regexp elem beg invisible visible face)
2706         (article-goto-body)
2707         (setq beg (point))
2708         (while (setq elem (pop alist))
2709           (goto-char beg)
2710           (setq regexp (car elem)
2711                 invisible (nth 1 elem)
2712                 visible (nth 2 elem)
2713                 face (nth 3 elem))
2714           (while (re-search-forward regexp nil t)
2715             (when (and (match-beginning visible) (match-beginning invisible))
2716               (gnus-article-hide-text
2717                (match-beginning invisible) (match-end invisible) props)
2718               (gnus-article-unhide-text-type
2719                (match-beginning visible) (match-end visible) 'emphasis)
2720               (gnus-put-text-property-excluding-newlines
2721                (match-beginning visible) (match-end visible) 'face face)
2722               (push 'emphasis gnus-article-wash-types)
2723               (goto-char (match-end invisible)))))))))
2724
2725 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2726   "Setup newsgroup emphasis alist."
2727   (unless gnus-article-emphasis-alist
2728     (let ((name (and gnus-newsgroup-name
2729                      (gnus-group-real-name gnus-newsgroup-name))))
2730       (make-local-variable 'gnus-article-emphasis-alist)
2731       (setq gnus-article-emphasis-alist
2732             (nconc
2733              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2734                (while (setq elem (pop alist))
2735                  (when (and name (string-match (car elem) name))
2736                    (setq alist nil
2737                          highlight (copy-sequence (cdr elem)))))
2738                highlight)
2739              (copy-sequence highlight-words)
2740              (if gnus-newsgroup-name
2741                  (copy-sequence (gnus-group-find-parameter
2742                                  gnus-newsgroup-name 'highlight-words t)))
2743              gnus-emphasis-alist)))))
2744
2745 (eval-when-compile
2746   (defvar gnus-summary-article-menu)
2747   (defvar gnus-summary-post-menu))
2748
2749 ;;; Saving functions.
2750
2751 (defun gnus-article-save (save-buffer file &optional num)
2752   "Save the currently selected article."
2753   (unless gnus-save-all-headers
2754     ;; Remove headers according to `gnus-saved-headers'.
2755     (let ((gnus-visible-headers
2756            (or gnus-saved-headers gnus-visible-headers))
2757           (gnus-article-buffer save-buffer))
2758       (save-excursion
2759         (set-buffer save-buffer)
2760         (article-hide-headers 1 t))))
2761   (save-window-excursion
2762     (if (not gnus-default-article-saver)
2763         (error "No default saver is defined")
2764       ;; !!! Magic!  The saving functions all save
2765       ;; `gnus-save-article-buffer' (or so they think), but we
2766       ;; bind that variable to our save-buffer.
2767       (set-buffer gnus-article-buffer)
2768       (let* ((gnus-save-article-buffer save-buffer)
2769              (filename
2770               (cond
2771                ((not gnus-prompt-before-saving) 'default)
2772                ((eq gnus-prompt-before-saving 'always) nil)
2773                (t file)))
2774              (gnus-number-of-articles-to-be-saved
2775               (when (eq gnus-prompt-before-saving t)
2776                 num)))                  ; Magic
2777         (set-buffer gnus-article-current-summary)
2778         (funcall gnus-default-article-saver filename)))))
2779
2780 (defun gnus-read-save-file-name (prompt &optional filename
2781                                         function group headers variable)
2782   (let ((default-name
2783           (funcall function group headers (symbol-value variable)))
2784         result)
2785     (setq result
2786           (expand-file-name
2787            (cond
2788             ((eq filename 'default)
2789              default-name)
2790             ((eq filename t)
2791              default-name)
2792             (filename filename)
2793             (t
2794              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2795                     (prompt
2796                      (format prompt
2797                              (if (and gnus-number-of-articles-to-be-saved
2798                                       (> gnus-number-of-articles-to-be-saved 1))
2799                                  (format "these %d articles"
2800                                          gnus-number-of-articles-to-be-saved)
2801                                "this article")))
2802                     (file
2803                      ;; Let the split methods have their say.
2804                      (cond
2805                       ;; No split name was found.
2806                       ((null split-name)
2807                        (read-file-name
2808                         (concat prompt " (default "
2809                                 (file-name-nondirectory default-name) ") ")
2810                         (file-name-directory default-name)
2811                         default-name))
2812                       ;; A single group name is returned.
2813                       ((stringp split-name)
2814                        (setq default-name
2815                              (funcall function split-name headers
2816                                       (symbol-value variable)))
2817                        (read-file-name
2818                         (concat prompt " (default "
2819                                 (file-name-nondirectory default-name) ") ")
2820                         (file-name-directory default-name)
2821                         default-name))
2822                       ;; A single split name was found
2823                       ((= 1 (length split-name))
2824                        (let* ((name (expand-file-name
2825                                      (car split-name)
2826                                      gnus-article-save-directory))
2827                               (dir (cond ((file-directory-p name)
2828                                           (file-name-as-directory name))
2829                                          ((file-exists-p name) name)
2830                                          (t gnus-article-save-directory))))
2831                          (read-file-name
2832                           (concat prompt " (default " name ") ")
2833                           dir name)))
2834                       ;; A list of splits was found.
2835                       (t
2836                        (setq split-name (nreverse split-name))
2837                        (let (result)
2838                          (let ((file-name-history
2839                                 (nconc split-name file-name-history)))
2840                            (setq result
2841                                  (expand-file-name
2842                                   (read-file-name
2843                                    (concat prompt " (`M-p' for defaults) ")
2844                                    gnus-article-save-directory
2845                                    (car split-name))
2846                                   gnus-article-save-directory)))
2847                          (car (push result file-name-history)))))))
2848                ;; Create the directory.
2849                (gnus-make-directory (file-name-directory file))
2850                ;; If we have read a directory, we append the default file name.
2851                (when (file-directory-p file)
2852                  (setq file (expand-file-name (file-name-nondirectory
2853                                                default-name)
2854                                               (file-name-as-directory file))))
2855                ;; Possibly translate some characters.
2856                (nnheader-translate-file-chars file))))))
2857     (gnus-make-directory (file-name-directory result))
2858     (set variable result)))
2859
2860 (defun gnus-article-archive-name (group)
2861   "Return the first instance of an \"Archive-name\" in the current buffer."
2862   (let ((case-fold-search t))
2863     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2864       (nnheader-concat gnus-article-save-directory
2865                        (match-string 1)))))
2866
2867 (defun gnus-article-nndoc-name (group)
2868   "If GROUP is an nndoc group, return the name of the parent group."
2869   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2870     (gnus-group-get-parameter group 'save-article-group)))
2871
2872 (defun gnus-summary-save-in-rmail (&optional filename)
2873   "Append this article to Rmail file.
2874 Optional argument FILENAME specifies file name.
2875 Directory to save to is default to `gnus-article-save-directory'."
2876   (setq filename (gnus-read-save-file-name
2877                   "Save %s in rmail file:" filename
2878                   gnus-rmail-save-name gnus-newsgroup-name
2879                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2880   (gnus-eval-in-buffer-window gnus-save-article-buffer
2881     (save-excursion
2882       (save-restriction
2883         (widen)
2884         (gnus-output-to-rmail filename))))
2885   filename)
2886
2887 (defun gnus-summary-save-in-mail (&optional filename)
2888   "Append this article to Unix mail file.
2889 Optional argument FILENAME specifies file name.
2890 Directory to save to is default to `gnus-article-save-directory'."
2891   (setq filename (gnus-read-save-file-name
2892                   "Save %s in Unix mail file:" filename
2893                   gnus-mail-save-name gnus-newsgroup-name
2894                   gnus-current-headers 'gnus-newsgroup-last-mail))
2895   (gnus-eval-in-buffer-window gnus-save-article-buffer
2896     (save-excursion
2897       (save-restriction
2898         (widen)
2899         (if (and (file-readable-p filename)
2900                  (mail-file-babyl-p filename))
2901             (rmail-output-to-rmail-file filename t)
2902           (gnus-output-to-mail filename)))))
2903   filename)
2904
2905 (defun gnus-summary-save-in-file (&optional filename overwrite)
2906   "Append this article to file.
2907 Optional argument FILENAME specifies file name.
2908 Directory to save to is default to `gnus-article-save-directory'."
2909   (setq filename (gnus-read-save-file-name
2910                   "Save %s in file:" filename
2911                   gnus-file-save-name gnus-newsgroup-name
2912                   gnus-current-headers 'gnus-newsgroup-last-file))
2913   (gnus-eval-in-buffer-window gnus-save-article-buffer
2914     (save-excursion
2915       (save-restriction
2916         (widen)
2917         (when (and overwrite
2918                    (file-exists-p filename))
2919           (delete-file filename))
2920         (gnus-output-to-file filename))))
2921   filename)
2922
2923 (defun gnus-summary-write-to-file (&optional filename)
2924   "Write this article to a file, overwriting it if the file exists.
2925 Optional argument FILENAME specifies file name.
2926 The directory to save in defaults to `gnus-article-save-directory'."
2927   (gnus-summary-save-in-file nil t))
2928
2929 (defun gnus-summary-save-body-in-file (&optional filename)
2930   "Append this article body to a file.
2931 Optional argument FILENAME specifies file name.
2932 The directory to save in defaults to `gnus-article-save-directory'."
2933   (setq filename (gnus-read-save-file-name
2934                   "Save %s body in file:" filename
2935                   gnus-file-save-name gnus-newsgroup-name
2936                   gnus-current-headers 'gnus-newsgroup-last-file))
2937   (gnus-eval-in-buffer-window gnus-save-article-buffer
2938     (save-excursion
2939       (save-restriction
2940         (widen)
2941         (when (article-goto-body)
2942           (narrow-to-region (point) (point-max)))
2943         (gnus-output-to-file filename))))
2944   filename)
2945
2946 (defun gnus-summary-save-in-pipe (&optional command)
2947   "Pipe this article to subprocess."
2948   (setq command
2949         (cond ((and (eq command 'default)
2950                     gnus-last-shell-command)
2951                gnus-last-shell-command)
2952               ((stringp command)
2953                command)
2954               (t (read-string
2955                   (format
2956                    "Shell command on %s: "
2957                    (if (and gnus-number-of-articles-to-be-saved
2958                             (> gnus-number-of-articles-to-be-saved 1))
2959                        (format "these %d articles"
2960                                gnus-number-of-articles-to-be-saved)
2961                      "this article"))
2962                   gnus-last-shell-command))))
2963   (when (string-equal command "")
2964     (if gnus-last-shell-command
2965         (setq command gnus-last-shell-command)
2966       (error "A command is required")))
2967   (gnus-eval-in-buffer-window gnus-article-buffer
2968     (save-restriction
2969       (widen)
2970       (shell-command-on-region (point-min) (point-max) command nil)))
2971   (setq gnus-last-shell-command command))
2972
2973 ;;; Article file names when saving.
2974
2975 (defun gnus-capitalize-newsgroup (newsgroup)
2976   "Capitalize NEWSGROUP name."
2977   (when (not (zerop (length newsgroup)))
2978     (concat (char-to-string (upcase (aref newsgroup 0)))
2979             (substring newsgroup 1))))
2980
2981 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2982   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2983 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2984 Otherwise, it is like ~/News/news/group/num."
2985   (let ((default
2986           (expand-file-name
2987            (concat (if (gnus-use-long-file-name 'not-save)
2988                        (gnus-capitalize-newsgroup newsgroup)
2989                      (gnus-newsgroup-directory-form newsgroup))
2990                    "/" (int-to-string (mail-header-number headers)))
2991            gnus-article-save-directory)))
2992     (if (and last-file
2993              (string-equal (file-name-directory default)
2994                            (file-name-directory last-file))
2995              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2996         default
2997       (or last-file default))))
2998
2999 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3000   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3001 If variable `gnus-use-long-file-name' is non-nil, it is
3002 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3003   (let ((default
3004           (expand-file-name
3005            (concat (if (gnus-use-long-file-name 'not-save)
3006                        newsgroup
3007                      (gnus-newsgroup-directory-form newsgroup))
3008                    "/" (int-to-string (mail-header-number headers)))
3009            gnus-article-save-directory)))
3010     (if (and last-file
3011              (string-equal (file-name-directory default)
3012                            (file-name-directory last-file))
3013              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3014         default
3015       (or last-file default))))
3016
3017 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3018   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3019 If variable `gnus-use-long-file-name' is non-nil, it is
3020 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3021   (or last-file
3022       (expand-file-name
3023        (if (gnus-use-long-file-name 'not-save)
3024            newsgroup
3025          (file-relative-name
3026           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3027           default-directory))
3028        gnus-article-save-directory)))
3029
3030 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3031   "Generate file name from sender."
3032   (let ((from (mail-header-from headers)))
3033     (expand-file-name
3034      (if (and from (string-match "\\([^ <]+\\)@" from))
3035          (match-string 1 from)
3036        "nobody")
3037      gnus-article-save-directory)))
3038
3039 (defun article-verify-x-pgp-sig ()
3040   "Verify X-PGP-Sig."
3041   (interactive)
3042   (if (gnus-buffer-live-p gnus-original-article-buffer)
3043       (let ((sig (with-current-buffer gnus-original-article-buffer
3044                    (gnus-fetch-field "X-PGP-Sig")))
3045             items info headers)
3046         (when (and sig
3047                    mml2015-use
3048                    (mml2015-clear-verify-function))
3049           (with-temp-buffer
3050             (insert-buffer gnus-original-article-buffer)
3051             (setq items (split-string sig))
3052             (message-narrow-to-head)
3053             (let ((inhibit-point-motion-hooks t)
3054                   (case-fold-search t))
3055               ;; Don't verify multiple headers.
3056               (setq headers (mapconcat (lambda (header)
3057                                          (concat header ": "
3058                                                  (mail-fetch-field header) "\n"))
3059                                        (split-string (nth 1 items) ",") "")))
3060             (delete-region (point-min) (point-max))
3061             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3062             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3063             (insert headers)
3064             (widen)
3065             (forward-line)
3066             (while (not (eobp))
3067               (if (looking-at "^-")
3068                   (insert "- "))
3069               (forward-line))
3070             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3071             (insert "Version: " (car items) "\n\n")
3072             (insert (mapconcat 'identity (cddr items) "\n"))
3073             (insert "\n-----END PGP SIGNATURE-----\n")
3074             (let ((mm-security-handle (list (format "multipart/signed"))))
3075               (mml2015-clean-buffer)
3076               (let ((coding-system-for-write (or gnus-newsgroup-charset
3077                                                  'iso-8859-1)))
3078                 (funcall (mml2015-clear-verify-function)))
3079               (setq info
3080                     (or (mm-handle-multipart-ctl-parameter
3081                          mm-security-handle 'gnus-details)
3082                         (mm-handle-multipart-ctl-parameter
3083                          mm-security-handle 'gnus-info)))))
3084           (when info
3085             (let (buffer-read-only bface eface)
3086               (save-restriction
3087                 (message-narrow-to-head)
3088                 (goto-char (point-max))
3089                 (forward-line -1)
3090                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3091                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3092                 (message-remove-header "X-Gnus-PGP-Verify")
3093                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3094                     (forward-line)
3095                   (goto-char (point-max)))
3096                 (narrow-to-region (point) (point))
3097                 (insert "X-Gnus-PGP-Verify: " info "\n")
3098                 (goto-char (point-min))
3099                 (forward-line)
3100                 (while (not (eobp))
3101                   (if (not (looking-at "^[ \t]"))
3102                       (insert " "))
3103                   (forward-line))
3104                 ;; Do highlighting.
3105                 (goto-char (point-min))
3106                 (when (looking-at "\\([^:]+\\): *")
3107                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3108                                      'face bface)
3109                   (put-text-property (match-end 0) (point-max)
3110                                      'face eface)))))))))
3111
3112 (defun article-verify-cancel-lock ()
3113   "Verify Cancel-Lock header."
3114   (interactive)
3115   (if (gnus-buffer-live-p gnus-original-article-buffer)
3116       (canlock-verify gnus-original-article-buffer)))
3117
3118 (eval-and-compile
3119   (mapcar
3120    (lambda (func)
3121      (let (afunc gfunc)
3122        (if (consp func)
3123            (setq afunc (car func)
3124                  gfunc (cdr func))
3125          (setq afunc func
3126                gfunc (intern (format "gnus-%s" func))))
3127        (defalias gfunc
3128          (if (fboundp afunc)
3129              `(lambda (&optional interactive &rest args)
3130                 ,(documentation afunc t)
3131                 (interactive (list t))
3132                 (save-excursion
3133                   (set-buffer gnus-article-buffer)
3134                   (if interactive
3135                       (call-interactively ',afunc)
3136                     (apply ',afunc args))))))))
3137    '(article-hide-headers
3138      article-verify-x-pgp-sig
3139      article-verify-cancel-lock
3140      article-hide-boring-headers
3141      article-toggle-headers
3142      article-treat-overstrike
3143      article-fill-long-lines
3144      article-capitalize-sentences
3145      article-remove-cr
3146      article-remove-leading-whitespace
3147      article-display-x-face
3148      article-de-quoted-unreadable
3149      article-de-base64-unreadable
3150      article-decode-HZ
3151      article-wash-html
3152      article-hide-list-identifiers
3153      article-hide-pgp
3154      article-strip-banner
3155      article-babel
3156      article-hide-pem
3157      article-hide-signature
3158      article-strip-headers-in-body
3159      article-remove-trailing-blank-lines
3160      article-strip-leading-blank-lines
3161      article-strip-multiple-blank-lines
3162      article-strip-leading-space
3163      article-strip-trailing-space
3164      article-strip-blank-lines
3165      article-strip-all-blank-lines
3166      article-date-local
3167      article-date-english
3168      article-date-iso8601
3169      article-date-original
3170      article-date-ut
3171      article-decode-mime-words
3172      article-decode-charset
3173      article-decode-encoded-words
3174      article-date-user
3175      article-date-lapsed
3176      article-emphasize
3177      article-treat-dumbquotes
3178      article-normalize-headers
3179      (article-show-all-headers . gnus-article-show-all-headers)
3180      (article-show-all . gnus-article-show-all))))
3181 \f
3182 ;;;
3183 ;;; Gnus article mode
3184 ;;;
3185
3186 (put 'gnus-article-mode 'mode-class 'special)
3187
3188 (gnus-define-keys gnus-article-mode-map
3189   " " gnus-article-goto-next-page
3190   "\177" gnus-article-goto-prev-page
3191   [delete] gnus-article-goto-prev-page
3192   [backspace] gnus-article-goto-prev-page
3193   "\C-c^" gnus-article-refer-article
3194   "h" gnus-article-show-summary
3195   "s" gnus-article-show-summary
3196   "\C-c\C-m" gnus-article-mail
3197   "?" gnus-article-describe-briefly
3198   "e" gnus-summary-edit-article
3199   "<" beginning-of-buffer
3200   ">" end-of-buffer
3201   "\C-c\C-i" gnus-info-find-node
3202   "\C-c\C-b" gnus-bug
3203   "\C-hk" gnus-article-describe-key
3204   "\C-hc" gnus-article-describe-key-briefly
3205
3206   "\C-d" gnus-article-read-summary-keys
3207   "\M-*" gnus-article-read-summary-keys
3208   "\M-#" gnus-article-read-summary-keys
3209   "\M-^" gnus-article-read-summary-keys
3210   "\M-g" gnus-article-read-summary-keys)
3211
3212 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3213 (let (keys)
3214   (let ((key 32))
3215     (while (<= key 127)
3216       (push (char-to-string key) keys)
3217       (incf key))
3218     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3219       (setq key (car elem))
3220       (when (and (> (length key) 0)
3221                  (not (eq 'menu-bar (aref key 0)))
3222                  (symbolp (lookup-key gnus-summary-mode-map key)))
3223         (push key keys))))
3224   (dolist (key keys)
3225     (unless (lookup-key gnus-article-mode-map key)
3226       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3227
3228 (defun gnus-article-make-menu-bar ()
3229   (unless (boundp 'gnus-article-commands-menu)
3230     (gnus-summary-make-menu-bar))
3231   (gnus-turn-off-edit-menu 'article)
3232   (unless (boundp 'gnus-article-article-menu)
3233     (easy-menu-define
3234      gnus-article-article-menu gnus-article-mode-map ""
3235      '("Article"
3236        ["Scroll forwards" gnus-article-goto-next-page t]
3237        ["Scroll backwards" gnus-article-goto-prev-page t]
3238        ["Show summary" gnus-article-show-summary t]
3239        ["Fetch Message-ID at point" gnus-article-refer-article t]
3240        ["Mail to address at point" gnus-article-mail t]
3241        ["Send a bug report" gnus-bug t]))
3242
3243     (easy-menu-define
3244      gnus-article-treatment-menu gnus-article-mode-map ""
3245      ;; Fixme: this should use :active (and maybe :visible).
3246      '("Treatment"
3247        ["Hide headers" gnus-article-toggle-headers t]
3248        ["Hide signature" gnus-article-hide-signature t]
3249        ["Hide citation" gnus-article-hide-citation t]
3250        ["Treat overstrike" gnus-article-treat-overstrike t]
3251        ["Remove carriage return" gnus-article-remove-cr t]
3252        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3253        ["Decode HZ" gnus-article-decode-HZ t]))
3254
3255     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3256
3257     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3258
3259     (gnus-run-hooks 'gnus-article-menu-hook)))
3260
3261 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3262 ;; Summary.
3263
3264 (defun gnus-article-mode ()
3265   "Major mode for displaying an article.
3266
3267 All normal editing commands are switched off.
3268
3269 The following commands are available in addition to all summary mode
3270 commands:
3271 \\<gnus-article-mode-map>
3272 \\[gnus-article-next-page]\t Scroll the article one page forwards
3273 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3274 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3275 \\[gnus-article-show-summary]\t Display the summary buffer
3276 \\[gnus-article-mail]\t Send a reply to the address near point
3277 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3278 \\[gnus-info-find-node]\t Go to the Gnus info node"
3279   (interactive)
3280   (gnus-simplify-mode-line)
3281   (setq mode-name "Article")
3282   (setq major-mode 'gnus-article-mode)
3283   (make-local-variable 'minor-mode-alist)
3284   (unless (assq 'gnus-show-mime minor-mode-alist)
3285     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3286   (use-local-map gnus-article-mode-map)
3287   (when (gnus-visual-p 'article-menu 'menu)
3288     (gnus-article-make-menu-bar))
3289   (gnus-update-format-specifications nil 'article-mode)
3290   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3291   (make-local-variable 'gnus-page-broken)
3292   (make-local-variable 'gnus-button-marker-list)
3293   (make-local-variable 'gnus-article-current-summary)
3294   (make-local-variable 'gnus-article-mime-handles)
3295   (make-local-variable 'gnus-article-decoded-p)
3296   (make-local-variable 'gnus-article-mime-handle-alist)
3297   (make-local-variable 'gnus-article-wash-types)
3298   (make-local-variable 'gnus-article-charset)
3299   (make-local-variable 'gnus-article-ignored-charsets)
3300   (gnus-set-default-directory)
3301   (buffer-disable-undo)
3302   (setq buffer-read-only t)
3303   (set-syntax-table gnus-article-mode-syntax-table)
3304   (gnus-run-hooks 'gnus-article-mode-hook))
3305
3306 (defun gnus-article-setup-buffer ()
3307   "Initialize the article buffer."
3308   (let* ((name (if gnus-single-article-buffer "*Article*"
3309                  (concat "*Article " gnus-newsgroup-name "*")))
3310          (original
3311           (progn (string-match "\\*Article" name)
3312                  (concat " *Original Article"
3313                          (substring name (match-end 0))))))
3314     (setq gnus-article-buffer name)
3315     (setq gnus-original-article-buffer original)
3316     (setq gnus-article-mime-handle-alist nil)
3317     ;; This might be a variable local to the summary buffer.
3318     (unless gnus-single-article-buffer
3319       (save-excursion
3320         (set-buffer gnus-summary-buffer)
3321         (setq gnus-article-buffer name)
3322         (setq gnus-original-article-buffer original)
3323         (gnus-set-global-variables)))
3324     (gnus-article-setup-highlight-words)
3325     ;; Init original article buffer.
3326     (save-excursion
3327       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3328       (set-buffer-multibyte nil)
3329       (setq major-mode 'gnus-original-article-mode)
3330       (make-local-variable 'gnus-original-article))
3331     (if (get-buffer name)
3332         (save-excursion
3333           (set-buffer name)
3334           (buffer-disable-undo)
3335           (setq buffer-read-only t)
3336           (unless (eq major-mode 'gnus-article-mode)
3337             (gnus-article-mode))
3338           (current-buffer))
3339       (save-excursion
3340         (set-buffer (gnus-get-buffer-create name))
3341         (gnus-article-mode)
3342         (make-local-variable 'gnus-summary-buffer)
3343         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3344         (current-buffer)))))
3345
3346 ;; Set article window start at LINE, where LINE is the number of lines
3347 ;; from the head of the article.
3348 (defun gnus-article-set-window-start (&optional line)
3349   (set-window-start
3350    (get-buffer-window gnus-article-buffer t)
3351    (save-excursion
3352      (set-buffer gnus-article-buffer)
3353      (goto-char (point-min))
3354      (if (not line)
3355          (point-min)
3356        (gnus-message 6 "Moved to bookmark")
3357        (search-forward "\n\n" nil t)
3358        (forward-line line)
3359        (point)))))
3360
3361 ;;; @@ article filters
3362 ;;;
3363
3364 (defun gnus-article-display-mime-message ()
3365   "Article display method for MIME message."
3366   ;; called from `gnus-original-article-buffer'.
3367   (let (charset all-headers)
3368     (with-current-buffer gnus-summary-buffer
3369       (setq charset default-mime-charset
3370             all-headers gnus-have-all-headers))
3371     (make-local-variable 'default-mime-charset)
3372     (setq default-mime-charset charset)
3373     (with-current-buffer (get-buffer-create gnus-article-buffer)
3374       (make-local-variable 'default-mime-charset)
3375       (setq default-mime-charset charset))
3376     (mime-display-message mime-message-structure
3377                           gnus-article-buffer nil gnus-article-mode-map)
3378     (when all-headers
3379       (gnus-article-hide-headers nil -1)))
3380   (run-hooks 'gnus-mime-article-prepare-hook))
3381
3382 (defun gnus-article-display-traditional-message ()
3383   "Article display method for traditional message."
3384   (set-buffer gnus-article-buffer)
3385   (let (buffer-read-only)
3386     (erase-buffer)
3387     (insert-buffer-substring gnus-original-article-buffer)))
3388
3389 (defun gnus-article-make-full-mail-header (&optional number charset)
3390   "Create a new mail header structure in a raw article buffer."
3391   (unless (and number charset)
3392     (save-current-buffer
3393       (set-buffer gnus-summary-buffer)
3394       (unless number
3395         (setq number (or (cdr gnus-article-current) 0)))
3396       (unless charset
3397         (setq charset (or default-mime-charset 'x-ctext)))))
3398   (goto-char (point-min))
3399   (let ((header-end (if (search-forward "\n\n" nil t)
3400                         (1- (point))
3401                       (goto-char (point-max))))
3402         (chars (- (point-max) (point)))
3403         (lines (count-lines (point) (point-max)))
3404         (default-mime-charset charset)
3405         xref)
3406     (narrow-to-region (point-min) header-end)
3407     (setq xref (std11-fetch-field "xref"))
3408     (prog1
3409         (make-full-mail-header
3410          number
3411          (std11-fetch-field "subject")
3412          (std11-fetch-field "from")
3413          (std11-fetch-field "date")
3414          (std11-fetch-field "message-id")
3415          (std11-fetch-field "references")
3416          chars
3417          lines
3418          (when xref (concat "Xref: " xref)))
3419       (widen))))
3420
3421 (defun gnus-article-prepare (article &optional all-headers header)
3422   "Prepare ARTICLE in article mode buffer.
3423 ARTICLE should either be an article number or a Message-ID.
3424 If ARTICLE is an id, HEADER should be the article headers.
3425 If ALL-HEADERS is non-nil, no headers are hidden."
3426   (save-excursion
3427     ;; Make sure we start in a summary buffer.
3428     (unless (eq major-mode 'gnus-summary-mode)
3429       (set-buffer gnus-summary-buffer))
3430     (setq gnus-summary-buffer (current-buffer))
3431     (let* ((gnus-article (if header (mail-header-number header) article))
3432            (summary-buffer (current-buffer))
3433            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3434            (group gnus-newsgroup-name)
3435            result)
3436       (save-excursion
3437         (gnus-article-setup-buffer)
3438         (set-buffer gnus-article-buffer)
3439         ;; Deactivate active regions.
3440         (when (and (boundp 'transient-mark-mode)
3441                    transient-mark-mode)
3442           (setq mark-active nil))
3443         (if (not (setq result (let ((buffer-read-only nil))
3444                                 (gnus-request-article-this-buffer
3445                                  article group))))
3446             ;; There is no such article.
3447             (save-excursion
3448               (when (and (numberp article)
3449                          (not (memq article gnus-newsgroup-sparse)))
3450                 (setq gnus-article-current
3451                       (cons gnus-newsgroup-name article))
3452                 (set-buffer gnus-summary-buffer)
3453                 (setq gnus-current-article article)
3454                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3455                     (progn
3456                       (gnus-summary-set-agent-mark article)
3457                       (message "Message marked for downloading"))
3458                   (gnus-summary-mark-article article gnus-canceled-mark)
3459                   (unless (memq article gnus-newsgroup-sparse)
3460                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3461           (if (or (eq result 'pseudo)
3462                   (eq result 'nneething))
3463               (progn
3464                 (save-excursion
3465                   (set-buffer summary-buffer)
3466                   (push article gnus-newsgroup-history)
3467                   (setq gnus-last-article gnus-current-article
3468                         gnus-current-article 0
3469                         gnus-current-headers nil
3470                         gnus-article-current nil)
3471                   (if (eq result 'nneething)
3472                       (gnus-configure-windows 'summary)
3473                     (gnus-configure-windows 'article))
3474                   (gnus-set-global-variables))
3475                 (let ((gnus-article-mime-handle-alist-1
3476                        gnus-article-mime-handle-alist))
3477                   (gnus-set-mode-line 'article)))
3478             ;; The result from the `request' was an actual article -
3479             ;; or at least some text that is now displayed in the
3480             ;; article buffer.
3481             (when (and (numberp article)
3482                        (not (eq article gnus-current-article)))
3483               ;; Seems like a new article has been selected.
3484               ;; `gnus-current-article' must be an article number.
3485               (save-excursion
3486                 (set-buffer summary-buffer)
3487                 (push article gnus-newsgroup-history)
3488                 (setq gnus-last-article gnus-current-article
3489                       gnus-current-article article
3490                       gnus-current-headers
3491                       (gnus-summary-article-header gnus-current-article)
3492                       gnus-article-current
3493                       (cons gnus-newsgroup-name gnus-current-article))
3494                 (unless (vectorp gnus-current-headers)
3495                   (setq gnus-current-headers nil))
3496                 (gnus-summary-goto-subject gnus-current-article)
3497                 (when (gnus-summary-show-thread)
3498                   ;; If the summary buffer really was folded, the
3499                   ;; previous goto may not actually have gone to
3500                   ;; the right article, but the thread root instead.
3501                   ;; So we go again.
3502                   (gnus-summary-goto-subject gnus-current-article))
3503                 (gnus-run-hooks 'gnus-mark-article-hook)
3504                 (gnus-set-mode-line 'summary)
3505                 (when (gnus-visual-p 'article-highlight 'highlight)
3506                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3507                 ;; Set the global newsgroup variables here.
3508                 (gnus-set-global-variables)
3509                 (setq gnus-have-all-headers
3510                       (or all-headers gnus-show-all-headers))))
3511             (save-excursion
3512               (gnus-configure-windows 'article))
3513             (when (or (numberp article)
3514                       (stringp article))
3515               (gnus-article-prepare-display)
3516               ;; Do page break.
3517               (goto-char (point-min))
3518               (setq gnus-page-broken
3519                     (when gnus-break-pages
3520                       (gnus-narrow-to-page)
3521                       t)))
3522             (let ((gnus-article-mime-handle-alist-1
3523                    gnus-article-mime-handle-alist))
3524               (gnus-set-mode-line 'article))
3525             (article-goto-body)
3526             (set-window-point (get-buffer-window (current-buffer)) (point))
3527             (gnus-configure-windows 'article)
3528             t))))))
3529
3530 (defun gnus-article-prepare-mime-display (&optional number)
3531   (goto-char (point-min))
3532   (when (re-search-forward "^[^\t ]+:" nil t)
3533     (goto-char (match-beginning 0)))
3534   (let ((entity (if (eq 1 (point-min))
3535                     (get-text-property 1 'mime-view-entity)
3536                   (get-text-property (point) 'mime-view-entity)))
3537         last-entity child-entity next type)
3538     (setq child-entity (mime-entity-children entity))
3539     (if child-entity
3540         (setq last-entity (nth (1- (length child-entity))
3541                                child-entity))
3542       (setq last-entity entity))
3543     (save-restriction
3544       (narrow-to-region (point)
3545                         (if (search-forward "\n\n" nil t)
3546                             (point)
3547                           (point-max)))
3548       (gnus-treat-article 'head)
3549       (put-text-property (point-min) (point-max) 'article-treated-header t)
3550       (goto-char (point-max)))
3551     (while (and (not (eobp)) entity)
3552       (setq next (set-marker
3553                   (make-marker)
3554                   (next-single-property-change (point) 'mime-view-entity
3555                                                nil (point-max))))
3556       (let ((types (mime-entity-content-type entity)))
3557         (while (eq 'multipart (mime-content-type-primary-type types))
3558           (setq entity (car (mime-entity-children entity))
3559                 types (mime-entity-content-type entity)))
3560         (when types
3561           (setq type (format "%s/%s"
3562                              (mime-content-type-primary-type types)
3563                              (mime-content-type-subtype types)))))
3564       (if (string-equal type "message/rfc822")
3565           (progn
3566             (setq next (point))
3567             (let ((children (mime-entity-children entity))
3568                   last-children)
3569               (when children
3570                 (setq last-children (nth (1- (length children)) children))
3571                 (while
3572                     (and
3573                      (not (eq last-children
3574                               (get-text-property next 'mime-view-entity)))
3575                      (setq next
3576                            (next-single-property-change next
3577                                                         'mime-view-entity
3578                                                         nil (point-max)))))))
3579             (setq next (next-single-property-change next 'mime-view-entity
3580                                                     nil (point-max)))
3581             (save-restriction
3582               (narrow-to-region (point) next)
3583               (gnus-article-prepare-mime-display)
3584               (goto-char (point-max)))
3585             (setq entity (get-text-property (point) 'mime-view-entity)))
3586         (save-restriction
3587           (narrow-to-region (point) next)
3588           ;; Kludge. We have to count true number, but for now,
3589           ;; part number is here only to achieve `last'.
3590           (gnus-treat-article nil 1
3591                               (if (eq entity last-entity)
3592                                   1 2)
3593                               type)
3594           (goto-char (point-max)))
3595         (setq entity (get-text-property next 'mime-view-entity))))))
3596
3597 ;;;###autoload
3598 (defun gnus-article-prepare-display ()
3599   "Make the current buffer look like a nice article."
3600   (let ((gnus-article-buffer (current-buffer))
3601         buffer-read-only)
3602     (unless (eq major-mode 'gnus-article-mode)
3603       (gnus-article-mode))
3604     (setq buffer-read-only nil
3605           gnus-button-marker-list nil
3606           gnus-article-wash-types nil)
3607     (save-restriction
3608       (widen)
3609       (static-if (featurep 'xemacs)
3610           (map-extents (lambda (extent maparg) (delete-extent extent)))
3611         (let ((lists (overlay-lists)))
3612           (dolist (overlay (nconc (car lists) (cdr lists)))
3613             (delete-overlay overlay)))))
3614     (gnus-run-hooks 'gnus-tmp-internal-hook))
3615   (set-buffer gnus-original-article-buffer)
3616   ;; Display message.
3617   (setq mime-message-structure gnus-current-headers)
3618   (mime-buffer-entity-set-buffer-internal mime-message-structure
3619                                           gnus-original-article-buffer)
3620   (mime-entity-set-representation-type-internal mime-message-structure
3621                                                 'mime-buffer-entity)
3622   (luna-send mime-message-structure 'initialize-instance
3623              mime-message-structure)
3624   (if gnus-show-mime
3625       (let (mime-display-header-hook mime-display-text/plain-hook)
3626         (funcall gnus-article-display-method-for-mime))
3627     (funcall gnus-article-display-method-for-traditional))
3628   ;; Call the treatment functions.
3629   (let ((inhibit-read-only t))
3630     (save-restriction
3631       (widen)
3632       (if gnus-show-mime
3633           (gnus-article-prepare-mime-display)
3634         (narrow-to-region (goto-char (point-min))
3635                           (if (search-forward "\n\n" nil t)
3636                               (point)
3637                             (point-max)))
3638         (gnus-treat-article 'head)
3639         (put-text-property (point-min) (point-max) 'article-treated-header t)
3640         (goto-char (point-max))
3641         (widen)
3642         (narrow-to-region (point) (point-max))
3643         (gnus-treat-article nil))
3644       (put-text-property (point-min) (point-max) 'read-only nil)))
3645   (gnus-run-hooks 'gnus-article-prepare-hook))
3646
3647 (defun gnus-article-decode-article-as-default-mime-charset ()
3648   "Decode an article as `default-mime-charset'.  It won't work if the
3649 value of the variable `gnus-show-mime' is non-nil."
3650   (unless gnus-show-mime
3651     (set (make-local-variable 'default-mime-charset)
3652          (with-current-buffer gnus-summary-buffer
3653            default-mime-charset))
3654     (decode-mime-charset-region (point-min) (point-max)
3655                                 default-mime-charset)))
3656
3657 ;;;
3658 ;;; Gnus MIME viewing functions
3659 ;;;
3660
3661 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3662   "The following specs can be used:
3663 %t  The MIME type
3664 %T  MIME type, along with additional info
3665 %n  The `name' parameter
3666 %d  The description, if any
3667 %l  The length of the encoded part
3668 %p  The part identifier number
3669 %e  Dots if the part isn't displayed")
3670
3671 (defvar gnus-mime-button-line-format-alist
3672   '((?t gnus-tmp-type ?s)
3673     (?T gnus-tmp-type-long ?s)
3674     (?n gnus-tmp-name ?s)
3675     (?d gnus-tmp-description ?s)
3676     (?p gnus-tmp-id ?s)
3677     (?l gnus-tmp-length ?d)
3678     (?e gnus-tmp-dots ?s)))
3679
3680 (defvar gnus-mime-button-commands
3681   '((gnus-article-press-button "\r" "Toggle Display")
3682     (gnus-mime-view-part "v" "View Interactively...")
3683     (gnus-mime-view-part-as-type "t" "View As Type...")
3684     (gnus-mime-view-part-as-charset "C" "View As charset...")
3685     (gnus-mime-save-part "o" "Save...")
3686     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3687     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3688     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3689     (gnus-mime-internalize-part "E" "View Internally")
3690     (gnus-mime-externalize-part "e" "View Externally")
3691     (gnus-mime-pipe-part "|" "Pipe To Command...")
3692     (gnus-mime-action-on-part "." "Take action on the part")))
3693
3694 (defun gnus-article-mime-part-status ()
3695   (with-current-buffer gnus-article-buffer
3696     (let ((entity (get-text-property (point-min) 'mime-view-entity))
3697           children)
3698       (if (and entity
3699                (setq children (mime-entity-children entity))
3700                (setq children (length children)))
3701           (if (eq 1 children)
3702               " (1 part)"
3703             (format " (%d parts)" children))
3704         ""))))
3705
3706 (defvar gnus-mime-button-map
3707   (let ((map (make-sparse-keymap)))
3708     (unless (>= (string-to-number emacs-version) 21)
3709       ;; XEmacs doesn't care.
3710       (set-keymap-parent map gnus-article-mode-map))
3711     (define-key map gnus-mouse-2 'gnus-article-push-button)
3712     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3713     (dolist (c gnus-mime-button-commands)
3714       (define-key map (cadr c) (car c)))
3715     map))
3716
3717 (defun gnus-mime-button-menu (event)
3718   "Construct a context-sensitive menu of MIME commands."
3719   (interactive "e")
3720   (save-window-excursion
3721     (let ((pos (event-start event)))
3722       (select-window (posn-window pos))
3723       (goto-char (posn-point pos))
3724       (gnus-article-check-buffer)
3725       (let ((response (x-popup-menu
3726                        t `("MIME Part"
3727                            ("" ,@(mapcar (lambda (c)
3728                                            (cons (caddr c) (car c)))
3729                                          gnus-mime-button-commands))))))
3730         (if response
3731             (call-interactively response))))))
3732
3733 (defun gnus-mime-view-all-parts (&optional handles)
3734   "View all the MIME parts."
3735   (interactive)
3736   (save-current-buffer
3737     (set-buffer gnus-article-buffer)
3738     (let ((handles (or handles gnus-article-mime-handles))
3739           (mail-parse-charset gnus-newsgroup-charset)
3740           (mail-parse-ignored-charsets
3741            (with-current-buffer gnus-summary-buffer
3742              gnus-newsgroup-ignored-charsets)))
3743       (when handles
3744         (mm-remove-parts handles)
3745         (goto-char (point-min))
3746         (or (search-forward "\n\n") (goto-char (point-max)))
3747         (let (buffer-read-only)
3748           (delete-region (point) (point-max))
3749           (mm-display-parts handles))))))
3750
3751 (defun gnus-mime-save-part-and-strip ()
3752   "Save the MIME part under point then replace it with an external body."
3753   (interactive)
3754   (gnus-article-check-buffer)
3755   (let* ((data (get-text-property (point) 'gnus-data))
3756          file param
3757          (handles gnus-article-mime-handles))
3758     (if (mm-multiple-handles gnus-article-mime-handles)
3759         (error "This function is not implemented"))
3760     (setq file (and data (mm-save-part data)))
3761     (when file
3762       (with-current-buffer (mm-handle-buffer data)
3763         (erase-buffer)
3764         (insert "Content-Type: " (mm-handle-media-type data))
3765         (mml-insert-parameter-string (cdr (mm-handle-type data))
3766                                      '(charset))
3767         (insert "\n")
3768         (insert "Content-ID: " (message-make-message-id) "\n")
3769         (insert "Content-Transfer-Encoding: binary\n")
3770         (insert "\n"))
3771       (setcdr data
3772               (cdr (mm-make-handle nil
3773                                    `("message/external-body"
3774                                      (access-type . "LOCAL-FILE")
3775                                      (name . ,file)))))
3776       (set-buffer gnus-summary-buffer)
3777       (gnus-article-edit-article
3778        `(lambda ()
3779           (erase-buffer)
3780           (let ((mail-parse-charset (or gnus-article-charset
3781                                         ',gnus-newsgroup-charset))
3782                 (mail-parse-ignored-charsets
3783                  (or gnus-article-ignored-charsets
3784                      ',gnus-newsgroup-ignored-charsets))
3785                 (mbl mml-buffer-list))
3786             (setq mml-buffer-list nil)
3787             (insert-buffer gnus-original-article-buffer)
3788             (mime-to-mml ',handles)
3789             (setq gnus-article-mime-handles nil)
3790             (let ((mbl1 mml-buffer-list))
3791               (setq mml-buffer-list mbl)
3792               (set (make-local-variable 'mml-buffer-list) mbl1))
3793             ;; LOCAL argument of add-hook differs between GNU Emacs
3794             ;; and XEmacs. make-local-hook makes sure they are local.
3795             (make-local-hook 'kill-buffer-hook)
3796             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3797        `(lambda (no-highlight)
3798           (let ((mail-parse-charset (or gnus-article-charset
3799                                         ',gnus-newsgroup-charset))
3800                 (message-options message-options)
3801                 (message-options-set-recipient)
3802                 (mail-parse-ignored-charsets
3803                  (or gnus-article-ignored-charsets
3804                      ',gnus-newsgroup-ignored-charsets)))
3805             (mml-to-mime)
3806             (mml-destroy-buffers)
3807             (remove-hook 'kill-buffer-hook
3808                          'mml-destroy-buffers t)
3809             (kill-local-variable 'mml-buffer-list))
3810           (gnus-summary-edit-article-done
3811            ,(or (mail-header-references gnus-current-headers) "")
3812            ,(gnus-group-read-only-p)
3813            ,gnus-summary-buffer no-highlight))))))
3814
3815 (defun gnus-mime-save-part ()
3816   "Save the MIME part under point."
3817   (interactive)
3818   (gnus-article-check-buffer)
3819   (let ((data (get-text-property (point) 'gnus-data)))
3820     (when data
3821       (mm-save-part data))))
3822
3823 (defun gnus-mime-pipe-part ()
3824   "Pipe the MIME part under point to a process."
3825   (interactive)
3826   (gnus-article-check-buffer)
3827   (let ((data (get-text-property (point) 'gnus-data)))
3828     (when data
3829       (mm-pipe-part data))))
3830
3831 (defun gnus-mime-view-part ()
3832   "Interactively choose a viewing method for the MIME part under point."
3833   (interactive)
3834   (gnus-article-check-buffer)
3835   (let ((data (get-text-property (point) 'gnus-data)))
3836     (when data
3837       (setq gnus-article-mime-handles
3838             (mm-merge-handles
3839              gnus-article-mime-handles (setq data (copy-sequence data))))
3840       (mm-interactively-view-part data))))
3841
3842 (defun gnus-mime-view-part-as-type-internal ()
3843   (gnus-article-check-buffer)
3844   (let* ((name (mail-content-type-get
3845                 (mm-handle-type (get-text-property (point) 'gnus-data))
3846                 'name))
3847          (def-type (and name (mm-default-file-encoding name))))
3848     (and def-type (cons def-type 0))))
3849
3850 (defun gnus-mime-view-part-as-type (&optional mime-type)
3851   "Choose a MIME media type, and view the part as such."
3852   (interactive)
3853   (unless mime-type
3854     (setq mime-type (completing-read
3855                      "View as MIME type: "
3856                      (mapcar #'list (mailcap-mime-types))
3857                      nil nil
3858                      (gnus-mime-view-part-as-type-internal))))
3859   (gnus-article-check-buffer)
3860   (let ((handle (get-text-property (point) 'gnus-data)))
3861     (when handle
3862       (setq handle
3863             (mm-make-handle (mm-handle-buffer handle)
3864                             (cons mime-type (cdr (mm-handle-type handle)))
3865                             (mm-handle-encoding handle)
3866                             (mm-handle-undisplayer handle)
3867                             (mm-handle-disposition handle)
3868                             (mm-handle-description handle)
3869                             nil
3870                             (mm-handle-id handle)))
3871       (setq gnus-article-mime-handles
3872             (mm-merge-handles gnus-article-mime-handles handle))
3873       (gnus-mm-display-part handle))))
3874
3875 (defun gnus-mime-copy-part (&optional handle)
3876   "Put the the MIME part under point into a new buffer."
3877   (interactive)
3878   (gnus-article-check-buffer)
3879   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3880          (contents (and handle (mm-get-part handle)))
3881          (base (and handle
3882                     (file-name-nondirectory
3883                      (or
3884                       (mail-content-type-get (mm-handle-type handle) 'name)
3885                       (mail-content-type-get (mm-handle-type handle)
3886                                              'filename)
3887                       "*decoded*"))))
3888          (buffer (and base (generate-new-buffer base))))
3889     (when contents
3890       (switch-to-buffer buffer)
3891       (insert contents)
3892       ;; We do it this way to make `normal-mode' set the appropriate mode.
3893       (unwind-protect
3894           (progn
3895             (setq buffer-file-name (expand-file-name base))
3896             (normal-mode))
3897         (setq buffer-file-name nil))
3898       (goto-char (point-min)))))
3899
3900 (defun gnus-mime-inline-part (&optional handle arg)
3901   "Insert the MIME part under point into the current buffer."
3902   (interactive (list nil current-prefix-arg))
3903   (gnus-article-check-buffer)
3904   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3905          contents charset
3906          (b (point))
3907          buffer-read-only)
3908     (when handle
3909       (if (and (not arg) (mm-handle-undisplayer handle))
3910           (mm-remove-part handle)
3911         (setq contents (mm-get-part handle))
3912         (cond
3913          ((not arg)
3914           (setq charset (or (mail-content-type-get
3915                              (mm-handle-type handle) 'charset)
3916                             gnus-newsgroup-charset)))
3917          ((numberp arg)
3918           (if (mm-handle-undisplayer handle)
3919               (mm-remove-part handle))
3920           (setq charset
3921                 (or (cdr (assq arg
3922                                gnus-summary-show-article-charset-alist))
3923                     (mm-read-coding-system "Charset: ")))))
3924         (forward-line 2)
3925         (mm-insert-inline handle
3926                           (if (and charset
3927                                    (setq charset (mm-charset-to-coding-system
3928                                                   charset))
3929                                    (not (eq charset 'ascii)))
3930                               (mm-decode-coding-string contents charset)
3931                             contents))
3932         (goto-char b)))))
3933
3934 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3935   "Insert the MIME part under point into the current buffer using the
3936 specified charset."
3937   (interactive (list nil current-prefix-arg))
3938   (gnus-article-check-buffer)
3939   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3940          contents charset
3941          (b (point))
3942          buffer-read-only)
3943     (when handle
3944       (if (mm-handle-undisplayer handle)
3945           (mm-remove-part handle))
3946       (let ((gnus-newsgroup-charset
3947              (or (cdr (assq arg
3948                             gnus-summary-show-article-charset-alist))
3949                  (mm-read-coding-system "Charset: ")))
3950             (gnus-newsgroup-ignored-charsets 'gnus-all))
3951         (gnus-article-press-button)))))
3952
3953 (defun gnus-mime-externalize-part (&optional handle)
3954   "View the MIME part under point with an external viewer."
3955   (interactive)
3956   (gnus-article-check-buffer)
3957   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3958          (mm-user-display-methods nil)
3959          (mm-inlined-types nil)
3960          (mail-parse-charset gnus-newsgroup-charset)
3961          (mail-parse-ignored-charsets
3962           (save-excursion (set-buffer gnus-summary-buffer)
3963                           gnus-newsgroup-ignored-charsets)))
3964     (when handle
3965       (if (mm-handle-undisplayer handle)
3966           (mm-remove-part handle)
3967         (mm-display-part handle)))))
3968
3969 (defun gnus-mime-internalize-part (&optional handle)
3970   "View the MIME part under point with an internal viewer.
3971 If no internal viewer is available, use an external viewer."
3972   (interactive)
3973   (gnus-article-check-buffer)
3974   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3975          (mm-inlined-types '(".*"))
3976          (mm-inline-large-images t)
3977          (mail-parse-charset gnus-newsgroup-charset)
3978          (mail-parse-ignored-charsets
3979           (save-excursion (set-buffer gnus-summary-buffer)
3980                           gnus-newsgroup-ignored-charsets)))
3981     (when handle
3982       (if (mm-handle-undisplayer handle)
3983           (mm-remove-part handle)
3984         (mm-display-part handle)))))
3985
3986 (defun gnus-mime-action-on-part (&optional action)
3987   "Do something with the MIME attachment at \(point\)."
3988   (interactive
3989    (list (completing-read "Action: " gnus-mime-action-alist)))
3990   (gnus-article-check-buffer)
3991   (let ((action-pair (assoc action gnus-mime-action-alist)))
3992     (if action-pair
3993         (funcall (cdr action-pair)))))
3994
3995 (defun gnus-article-part-wrapper (n function)
3996   (save-current-buffer
3997     (set-buffer gnus-article-buffer)
3998     (when (> n (length gnus-article-mime-handle-alist))
3999       (error "No such part"))
4000     (gnus-article-goto-part n)
4001     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4002       (funcall function handle))))
4003
4004 (defun gnus-article-pipe-part (n)
4005   "Pipe MIME part N, which is the numerical prefix."
4006   (interactive "p")
4007   (gnus-article-part-wrapper n 'mm-pipe-part))
4008
4009 (defun gnus-article-save-part (n)
4010   "Save MIME part N, which is the numerical prefix."
4011   (interactive "p")
4012   (gnus-article-part-wrapper n 'mm-save-part))
4013
4014 (defun gnus-article-interactively-view-part (n)
4015   "View MIME part N interactively, which is the numerical prefix."
4016   (interactive "p")
4017   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4018
4019 (defun gnus-article-copy-part (n)
4020   "Copy MIME part N, which is the numerical prefix."
4021   (interactive "p")
4022   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4023
4024 (defun gnus-article-view-part-as-charset (n)
4025   "Copy MIME part N, which is the numerical prefix."
4026   (interactive "p")
4027   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4028
4029 (defun gnus-article-externalize-part (n)
4030   "View MIME part N externally, which is the numerical prefix."
4031   (interactive "p")
4032   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
4033
4034 (defun gnus-article-inline-part (n)
4035   "Inline MIME part N, which is the numerical prefix."
4036   (interactive "p")
4037   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4038
4039 (defun gnus-article-mime-match-handle-first (condition)
4040   (if condition
4041       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4042         (while (setq ihandle (pop alist))
4043           (if (and (cond
4044                     ((functionp condition)
4045                      (funcall condition (cdr ihandle)))
4046                     ((eq condition 'undisplayed)
4047                      (not (or (mm-handle-undisplayer (cdr ihandle))
4048                               (equal (mm-handle-media-type (cdr ihandle))
4049                                      "multipart/alternative"))))
4050                     ((eq condition 'undisplayed-alternative)
4051                      (not (mm-handle-undisplayer (cdr ihandle))))
4052                     (t t))
4053                    (gnus-article-goto-part (car ihandle))
4054                    (or (not n) (< (car ihandle) n)))
4055               (setq n (car ihandle))))
4056         (or n 1))
4057     1))
4058
4059 (defun gnus-article-view-part (&optional n)
4060   "View MIME part N, which is the numerical prefix."
4061   (interactive "P")
4062   (save-current-buffer
4063     (set-buffer gnus-article-buffer)
4064     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4065                              gnus-article-mime-match-handle-function)))
4066     (when (> n (length gnus-article-mime-handle-alist))
4067       (error "No such part"))
4068     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4069       (when (gnus-article-goto-part n)
4070         (if (equal (car handle) "multipart/alternative")
4071             (gnus-article-press-button)
4072           (when (eq (gnus-mm-display-part handle) 'internal)
4073             (gnus-set-window-start)))))))
4074
4075 (defsubst gnus-article-mime-total-parts ()
4076   (if (bufferp (car gnus-article-mime-handles))
4077       1 ;; single part
4078     (1- (length gnus-article-mime-handles))))
4079
4080 (defun gnus-mm-display-part (handle)
4081   "Display HANDLE and fix MIME button."
4082   (let ((id (get-text-property (point) 'gnus-part))
4083         (point (point))
4084         buffer-read-only)
4085     (forward-line 1)
4086     (prog1
4087         (let ((window (selected-window))
4088               (mail-parse-charset gnus-newsgroup-charset)
4089               (mail-parse-ignored-charsets
4090                (save-excursion (set-buffer gnus-summary-buffer)
4091                                gnus-newsgroup-ignored-charsets)))
4092           (save-excursion
4093             (unwind-protect
4094                 (let ((win (get-buffer-window (current-buffer) t))
4095                       (beg (point)))
4096                   (when win
4097                     (select-window win))
4098                   (goto-char point)
4099                   (forward-line)
4100                   (if (mm-handle-displayed-p handle)
4101                       ;; This will remove the part.
4102                       (mm-display-part handle)
4103                     (save-restriction
4104                       (narrow-to-region (point)
4105                                         (if (eobp) (point) (1+ (point))))
4106                       (mm-display-part handle)
4107                       ;; We narrow to the part itself and
4108                       ;; then call the treatment functions.
4109                       (goto-char (point-min))
4110                       (forward-line 1)
4111                       (narrow-to-region (point) (point-max))
4112                       (gnus-treat-article
4113                        nil id
4114                        (gnus-article-mime-total-parts)
4115                        (mm-handle-media-type handle)))))
4116               (if (window-live-p window)
4117                   (select-window window)))))
4118       (goto-char point)
4119       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4120       (gnus-insert-mime-button
4121        handle id (list (mm-handle-displayed-p handle)))
4122       (goto-char point))))
4123
4124 (defun gnus-article-goto-part (n)
4125   "Go to MIME part N."
4126   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4127     (when point
4128       (goto-char point))))
4129
4130 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4131   (let ((gnus-tmp-name
4132          (or (mail-content-type-get (mm-handle-type handle) 'name)
4133              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4134              (mail-content-type-get (mm-handle-type handle) 'url)
4135              ""))
4136         (gnus-tmp-type (mm-handle-media-type handle))
4137         (gnus-tmp-description
4138          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4139                                               "")))
4140         (gnus-tmp-dots
4141          (if (if displayed (car displayed)
4142                (mm-handle-displayed-p handle))
4143              "" "..."))
4144         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4145                            (buffer-size)))
4146         gnus-tmp-type-long b e)
4147     (when (string-match ".*/" gnus-tmp-name)
4148       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4149     (setq gnus-tmp-type-long (concat gnus-tmp-type
4150                                      (and (not (equal gnus-tmp-name ""))
4151                                           (concat "; " gnus-tmp-name))))
4152     (unless (equal gnus-tmp-description "")
4153       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4154     (unless (bolp)
4155       (insert "\n"))
4156     (setq b (point))
4157     (gnus-eval-format
4158      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4159      `(keymap ,gnus-mime-button-map
4160               ,@(if (>= (string-to-number emacs-version) 21)
4161                     nil
4162                   (list 'local-map gnus-mime-button-map))
4163               gnus-callback gnus-mm-display-part
4164               gnus-part ,gnus-tmp-id
4165               article-type annotation
4166               gnus-data ,handle))
4167     (setq e (point))
4168     (widget-convert-button
4169      'link b e
4170      :mime-handle handle
4171      :action 'gnus-widget-press-button
4172      :button-keymap gnus-mime-button-map
4173      :help-echo
4174      (lambda (widget/window &optional overlay pos)
4175        ;; Needed to properly clear the message due to a bug in
4176        ;; wid-edit (XEmacs only).
4177        (if (boundp 'help-echo-owns-message)
4178            (setq help-echo-owns-message t))
4179        (format
4180         "%S: %s the MIME part; %S: more options"
4181         (aref gnus-mouse-2 0)
4182         ;; XEmacs will get a single widget arg; Emacs 21 will get
4183         ;; window, overlay, position.
4184         (if (mm-handle-displayed-p
4185              (if overlay
4186                  (with-current-buffer (gnus-overlay-buffer overlay)
4187                    (widget-get (widget-at (gnus-overlay-start overlay))
4188                                :mime-handle))
4189                (widget-get widget/window :mime-handle)))
4190             "hide" "show")
4191         (aref gnus-down-mouse-3 0))))))
4192
4193 (defun gnus-widget-press-button (elems el)
4194   (goto-char (widget-get elems :from))
4195   (gnus-article-press-button))
4196
4197 (defvar gnus-displaying-mime nil)
4198
4199 (defun gnus-display-mime (&optional ihandles)
4200   "Display the MIME parts."
4201   (save-excursion
4202     (save-selected-window
4203       (let ((window (get-buffer-window gnus-article-buffer))
4204             (point (point)))
4205         (when window
4206           (select-window window)
4207           ;; We have to do this since selecting the window
4208           ;; may change the point.  So we set the window point.
4209           (set-window-point window point)))
4210       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
4211              buffer-read-only handle name type b e display)
4212         (when (and (not ihandles)
4213                    (not gnus-displaying-mime))
4214           ;; Top-level call; we clean up.
4215           (when gnus-article-mime-handles
4216             (mm-destroy-parts gnus-article-mime-handles)
4217             (setq gnus-article-mime-handle-alist nil));; A trick.
4218           (setq gnus-article-mime-handles handles)
4219           ;; We allow users to glean info from the handles.
4220           (when gnus-article-mime-part-function
4221             (gnus-mime-part-function handles)))
4222         (if (and handles
4223                  (or (not (stringp (car handles)))
4224                      (cdr handles)))
4225             (progn
4226               (when (and (not ihandles)
4227                          (not gnus-displaying-mime))
4228                 ;; Clean up for mime parts.
4229                 (article-goto-body)
4230                 (delete-region (point) (point-max)))
4231               (let ((gnus-displaying-mime t))
4232                 (gnus-mime-display-part handles)))
4233           (save-restriction
4234             (article-goto-body)
4235             (narrow-to-region (point) (point-max))
4236             (gnus-treat-article nil 1 1)
4237             (widen)))
4238         (unless ihandles
4239           ;; Highlight the headers.
4240           (save-excursion
4241             (save-restriction
4242               (article-goto-body)
4243               (narrow-to-region (point-min) (point))
4244               (gnus-treat-article 'head))))))))
4245
4246 (defvar gnus-mime-display-multipart-as-mixed nil)
4247 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4248 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4249
4250 (defun gnus-mime-display-part (handle)
4251   (cond
4252    ;; Single part.
4253    ((not (stringp (car handle)))
4254     (gnus-mime-display-single handle))
4255    ;; User-defined multipart
4256    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4257     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4258              handle))
4259    ;; multipart/alternative
4260    ((and (equal (car handle) "multipart/alternative")
4261          (not (or gnus-mime-display-multipart-as-mixed
4262                   gnus-mime-display-multipart-alternative-as-mixed)))
4263     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4264       (push (cons id handle) gnus-article-mime-handle-alist)
4265       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4266    ;; multipart/related
4267    ((and (equal (car handle) "multipart/related")
4268          (not (or gnus-mime-display-multipart-as-mixed
4269                   gnus-mime-display-multipart-related-as-mixed)))
4270     ;;;!!!We should find the start part, but we just default
4271     ;;;!!!to the first part.
4272     ;;(gnus-mime-display-part (cadr handle))
4273     ;;;!!! Most multipart/related is an HTML message plus images.
4274     ;;;!!! Unfortunately we are unable to let W3 display those
4275     ;;;!!! included images, so we just display it as a mixed multipart.
4276     ;;(gnus-mime-display-mixed (cdr handle))
4277     ;;;!!! No, w3 can display everything just fine.
4278     (gnus-mime-display-part (cadr handle)))
4279    ((equal (car handle) "multipart/signed")
4280     (or (memq 'signed gnus-article-wash-types)
4281         (push 'signed gnus-article-wash-types))
4282     (gnus-mime-display-security handle))
4283    ((equal (car handle) "multipart/encrypted")
4284     (or (memq 'encrypted gnus-article-wash-types)
4285         (push 'encrypted gnus-article-wash-types))
4286     (gnus-mime-display-security handle))
4287    ;; Other multiparts are handled like multipart/mixed.
4288    (t
4289     (gnus-mime-display-mixed (cdr handle)))))
4290
4291 (defun gnus-mime-part-function (handles)
4292   (if (stringp (car handles))
4293       (mapcar 'gnus-mime-part-function (cdr handles))
4294     (funcall gnus-article-mime-part-function handles)))
4295
4296 (defun gnus-mime-display-mixed (handles)
4297   (mapcar 'gnus-mime-display-part handles))
4298
4299 (defun gnus-mime-display-single (handle)
4300   (let ((type (mm-handle-media-type handle))
4301         (ignored gnus-ignored-mime-types)
4302         (not-attachment t)
4303         (move nil)
4304         display text)
4305     (catch 'ignored
4306       (progn
4307         (while ignored
4308           (when (string-match (pop ignored) type)
4309             (throw 'ignored nil)))
4310         (if (and (setq not-attachment
4311                        (and (not (mm-inline-override-p handle))
4312                             (or (not (mm-handle-disposition handle))
4313                                 (equal (car (mm-handle-disposition handle))
4314                                        "inline")
4315                                 (mm-attachment-override-p handle))))
4316                  (mm-automatic-display-p handle)
4317                  (or (and
4318                       (mm-inlinable-p handle)
4319                       (mm-inlined-p handle))
4320                      (mm-automatic-external-display-p type)))
4321             (setq display t)
4322           (when (equal (mm-handle-media-supertype handle) "text")
4323             (setq text t)))
4324         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4325               beg)
4326           (push (cons id handle) gnus-article-mime-handle-alist)
4327           (when (or (not display)
4328                     (not (gnus-unbuttonized-mime-type-p type)))
4329             ;(gnus-article-insert-newline)
4330             (gnus-insert-mime-button
4331              handle id (list (or display (and not-attachment text))))
4332             (gnus-article-insert-newline)
4333             ;(gnus-article-insert-newline)
4334             ;; Remember modify the number of forward lines.
4335             (setq move t))
4336           (setq beg (point))
4337           (cond
4338            (display
4339             (when move
4340               (forward-line -1)
4341               (setq beg (point)))
4342             (let ((mail-parse-charset gnus-newsgroup-charset)
4343                   (mail-parse-ignored-charsets
4344                    (save-excursion (condition-case ()
4345                                        (set-buffer gnus-summary-buffer)
4346                                      (error))
4347                                    gnus-newsgroup-ignored-charsets)))
4348               (mm-display-part handle t))
4349             (goto-char (point-max)))
4350            ((and text not-attachment)
4351             (when move
4352               (forward-line -1)
4353               (setq beg (point)))
4354             (gnus-article-insert-newline)
4355             (mm-insert-inline handle (mm-get-part handle))
4356             (goto-char (point-max))))
4357           ;; Do highlighting.
4358           (save-excursion
4359             (save-restriction
4360               (narrow-to-region beg (point))
4361               (gnus-treat-article
4362                nil id
4363                (gnus-article-mime-total-parts)
4364                (mm-handle-media-type handle)))))))))
4365
4366 (defun gnus-unbuttonized-mime-type-p (type)
4367   "Say whether TYPE is to be unbuttonized."
4368   (unless gnus-inhibit-mime-unbuttonizing
4369     (when (catch 'found
4370             (let ((types gnus-unbuttonized-mime-types))
4371               (while types
4372                 (when (string-match (pop types) type)
4373                   (throw 'found t)))))
4374       (not (catch 'found
4375              (let ((types gnus-buttonized-mime-types))
4376                (while types
4377                  (when (string-match (pop types) type)
4378                    (throw 'found t)))))))))
4379
4380 (defun gnus-article-insert-newline ()
4381   "Insert a newline, but mark it as undeletable."
4382   (gnus-put-text-property
4383    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4384
4385 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4386   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4387          (ihandles handles)
4388          (point (point))
4389          handle buffer-read-only from props begend not-pref)
4390     (save-window-excursion
4391       (save-restriction
4392         (when ibegend
4393           (narrow-to-region (car ibegend)
4394                             (or (cdr ibegend)
4395                                 (progn
4396                                   (goto-char (car ibegend))
4397                                   (forward-line 2)
4398                                   (point))))
4399           (delete-region (point-min) (point-max))
4400           (mm-remove-parts handles))
4401         (setq begend (list (point-marker)))
4402         ;; Do the toggle.
4403         (unless (setq not-pref (cadr (member preferred ihandles)))
4404           (setq not-pref (car ihandles)))
4405         (when (or ibegend
4406                   (not preferred)
4407                   (not (gnus-unbuttonized-mime-type-p
4408                         "multipart/alternative")))
4409           (gnus-add-text-properties
4410            (setq from (point))
4411            (progn
4412              (insert (format "%d.  " id))
4413              (point))
4414            `(gnus-callback
4415              (lambda (handles)
4416                (unless ,(not ibegend)
4417                  (setq gnus-article-mime-handle-alist
4418                        ',gnus-article-mime-handle-alist))
4419                (gnus-mime-display-alternative
4420                 ',ihandles ',not-pref ',begend ,id))
4421              ,@(if (>= (string-to-number emacs-version) 21)
4422                    nil ;; XEmacs doesn't care
4423                  (list 'local-map gnus-mime-button-map))
4424              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4425              face ,gnus-article-button-face
4426              keymap ,gnus-mime-button-map
4427              gnus-part ,id
4428              gnus-data ,handle))
4429           (widget-convert-button 'link from (point)
4430                                  :action 'gnus-widget-press-button
4431                                  :button-keymap gnus-widget-button-keymap)
4432           ;; Do the handles
4433           (while (setq handle (pop handles))
4434             (gnus-add-text-properties
4435              (setq from (point))
4436              (progn
4437                (insert (format "(%c) %-18s"
4438                                (if (equal handle preferred) ?* ? )
4439                                (mm-handle-media-type handle)))
4440                (point))
4441              `(gnus-callback
4442                (lambda (handles)
4443                  (unless ,(not ibegend)
4444                    (setq gnus-article-mime-handle-alist
4445                          ',gnus-article-mime-handle-alist))
4446                  (gnus-mime-display-alternative
4447                   ',ihandles ',handle ',begend ,id))
4448                ,@(if (>= (string-to-number emacs-version) 21)
4449                      nil ;; XEmacs doesn't care
4450                    (list 'local-map gnus-mime-button-map))
4451                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4452                face ,gnus-article-button-face
4453                keymap ,gnus-mime-button-map
4454                gnus-part ,id
4455                gnus-data ,handle))
4456             (widget-convert-button 'link from (point)
4457                                    :action 'gnus-widget-press-button
4458                                    :button-keymap gnus-widget-button-keymap)
4459             (insert "  "))
4460           (insert "\n\n"))
4461         (when preferred
4462           (if (stringp (car preferred))
4463               (gnus-display-mime preferred)
4464             (let ((mail-parse-charset gnus-newsgroup-charset)
4465                   (mail-parse-ignored-charsets
4466                    (save-excursion (set-buffer gnus-summary-buffer)
4467                                    gnus-newsgroup-ignored-charsets)))
4468               (mm-display-part preferred)
4469               ;; Do highlighting.
4470               (save-excursion
4471                 (save-restriction
4472                   (narrow-to-region (car begend) (point-max))
4473                   (gnus-treat-article
4474                    nil (length gnus-article-mime-handle-alist)
4475                    (gnus-article-mime-total-parts)
4476                    (mm-handle-media-type handle))))))
4477           (goto-char (point-max))
4478           (setcdr begend (point-marker)))))
4479     (when ibegend
4480       (goto-char point))))
4481
4482 (defconst gnus-article-wash-status-strings
4483   (let ((alist '((cite "c" "Possible hidden citation text" 
4484                        " " "All citation text visible")
4485                  (headers "h" "Hidden headers"
4486                           " " "All headers visible.")
4487                  (pgp "p" "Encrypted or signed message status hidden" 
4488                       " " "No hidden encryption nor digital signature status")
4489                  (signature "s" "Signature has been hidden"
4490                             " " "Signature is visible")
4491                  (overstrike "o" "Overstrike (^H) characters applied"
4492                              " " "No overstrike characters applied")
4493                  (gnus-show-mime "m" "Mime processing is activated"
4494                                  " " "Mime processing is not activated")
4495                  (emphasis "e" "/*_Emphasis_*/ characters applied"
4496                            " " "No /*_emphasis_*/ characters applied")))
4497         result)
4498     (dolist (entry alist result)
4499       (let ((key (nth 0 entry))
4500             (on (copy-sequence (nth 1 entry)))
4501             (on-help (nth 2 entry))
4502             (off (copy-sequence (nth 3 entry)))
4503             (off-help (nth 4 entry)))
4504         (put-text-property 0 1 'help-echo on-help on)
4505         (put-text-property 0 1 'help-echo off-help off)
4506         (push (list key on off) result))))
4507   "Alist of strings describing wash status in the mode line.
4508 Each entry has the form (KEY ON OF), where the KEY is a symbol
4509 representing the particular washing function, ON is the string to use
4510 in the article mode line when the washing function is active, and OFF
4511 is the string to use when it is inactive.")
4512
4513 (defun gnus-gnus-article-wash-status-entry (key value)
4514   (let ((entry (assoc key gnus-article-wash-status-strings)))
4515     (if value (nth 1 entry) (nth 2 entry))))
4516
4517 (defun gnus-article-wash-status ()
4518   "Return a string which display status of article washing."
4519   (save-excursion
4520     (set-buffer gnus-article-buffer)
4521     (let ((cite (memq 'cite gnus-article-wash-types))
4522           (headers (memq 'headers gnus-article-wash-types))
4523           (boring (memq 'boring-headers gnus-article-wash-types))
4524           (pgp (memq 'pgp gnus-article-wash-types))
4525           (pem (memq 'pem gnus-article-wash-types))
4526           (signed (memq 'signed gnus-article-wash-types))
4527           (encrypted (memq 'encrypted gnus-article-wash-types))
4528           (signature (memq 'signature gnus-article-wash-types))
4529           (overstrike (memq 'overstrike gnus-article-wash-types))
4530           (emphasis (memq 'emphasis gnus-article-wash-types)))
4531       (concat (gnus-gnus-article-wash-status-entry 'cite cite)
4532               (gnus-gnus-article-wash-status-entry 'headers
4533                                                    (or headers boring))
4534               (gnus-gnus-article-wash-status-entry
4535                'pgp (or pgp pem signed encrypted))
4536               (gnus-gnus-article-wash-status-entry 'signature signature)
4537               (gnus-gnus-article-wash-status-entry 'overstrike overstrike)
4538               (gnus-gnus-article-wash-status-entry 'gnus-show-mime
4539                                                    gnus-show-mime)
4540               (gnus-gnus-article-wash-status-entry 'emphasis emphasis)))))
4541
4542 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4543
4544 (defun gnus-article-maybe-hide-headers ()
4545   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4546 Provided for backwards compatibility."
4547   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4548                  (not (save-excursion (set-buffer gnus-summary-buffer)
4549                                       gnus-have-all-headers)))
4550              (not gnus-inhibit-hiding))
4551     (gnus-article-hide-headers)))
4552
4553 ;;; Article savers.
4554
4555 (defun gnus-output-to-file (file-name)
4556   "Append the current article to a file named FILE-NAME."
4557   (let ((artbuf (current-buffer)))
4558     (with-temp-buffer
4559       (insert-buffer-substring artbuf)
4560       ;; Append newline at end of the buffer as separator, and then
4561       ;; save it to file.
4562       (goto-char (point-max))
4563       (insert "\n")
4564       (let ((file-name-coding-system nnmail-pathname-coding-system)
4565             (pathname-coding-system nnmail-pathname-coding-system))
4566         (write-region-as-binary (point-min) (point-max) file-name 'append))
4567       t)))
4568
4569 (defun gnus-narrow-to-page (&optional arg)
4570   "Narrow the article buffer to a page.
4571 If given a numerical ARG, move forward ARG pages."
4572   (interactive "P")
4573   (setq arg (if arg (prefix-numeric-value arg) 0))
4574   (save-excursion
4575     (set-buffer gnus-article-buffer)
4576     (goto-char (point-min))
4577     (widen)
4578     ;; Remove any old next/prev buttons.
4579     (when (gnus-visual-p 'page-marker)
4580       (let ((buffer-read-only nil))
4581         (gnus-remove-text-with-property 'gnus-prev)
4582         (gnus-remove-text-with-property 'gnus-next)))
4583     (when
4584         (cond ((< arg 0)
4585                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4586               ((> arg 0)
4587                (re-search-forward page-delimiter nil 'move arg)))
4588       (goto-char (match-end 0)))
4589     (narrow-to-region
4590      (point)
4591      (if (re-search-forward page-delimiter nil 'move)
4592          (match-beginning 0)
4593        (point)))
4594     (when (and (gnus-visual-p 'page-marker)
4595                (not (= (point-min) 1)))
4596       (save-excursion
4597         (goto-char (point-min))
4598         (gnus-insert-prev-page-button)))
4599     (when (and (gnus-visual-p 'page-marker)
4600                (< (+ (point-max) 2) (buffer-size)))
4601       (save-excursion
4602         (goto-char (point-max))
4603         (gnus-insert-next-page-button)))))
4604
4605 ;; Article mode commands
4606
4607 (defun gnus-article-goto-next-page ()
4608   "Show the next page of the article."
4609   (interactive)
4610   (when (gnus-article-next-page)
4611     (goto-char (point-min))
4612     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4613
4614 (defun gnus-article-goto-prev-page ()
4615   "Show the next page of the article."
4616   (interactive)
4617   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4618     (gnus-article-prev-page nil)))
4619
4620 (defun gnus-article-next-page (&optional lines)
4621   "Show the next page of the current article.
4622 If end of article, return non-nil.  Otherwise return nil.
4623 Argument LINES specifies lines to be scrolled up."
4624   (interactive "p")
4625   (let ((start (window-start))
4626         end-of-buffer end-of-page)
4627     (save-excursion
4628       (move-to-window-line -1)
4629       (if (<= (point) start)
4630           (progn
4631             (forward-line 2)
4632             (setq start (point)))
4633         (forward-line 1)
4634         (setq start nil))
4635       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4636                          (and (pos-visible-in-window-p)
4637                               (setq end-of-buffer t)))
4638                         ((eobp)
4639                          (setq end-of-page t)))
4640                   (not lines))
4641         (move-to-window-line lines)
4642         (unless (search-backward "\n\n" nil t)
4643           (setq start (point)))))
4644     (cond (end-of-buffer t)
4645           (end-of-page
4646            (gnus-narrow-to-page 1)
4647            nil)
4648           (t
4649            (if start
4650                (set-window-start (selected-window) start)
4651              (let (window-pixel-scroll-increment)
4652                (scroll-up lines)))
4653            nil))))
4654
4655 (defun gnus-article-prev-page (&optional lines)
4656   "Show previous page of current article.
4657 Argument LINES specifies lines to be scrolled down."
4658   (interactive "p")
4659   (let (beginning-of-buffer beginning-of-page)
4660     (save-excursion
4661       (move-to-window-line 0)
4662       (cond ((eq 1 (point))
4663              (setq beginning-of-buffer t))
4664             ((bobp)
4665              (setq beginning-of-page t))))
4666     (cond (beginning-of-buffer)
4667           (beginning-of-page
4668            (gnus-narrow-to-page -1))
4669           (t
4670            (condition-case nil
4671                (let (window-pixel-scroll-increment)
4672                  (scroll-down lines))
4673              (beginning-of-buffer
4674               (goto-char (point-min))))))))
4675
4676 (defun gnus-article-refer-article ()
4677   "Read article specified by message-id around point."
4678   (interactive)
4679   (let ((point (point)))
4680     (search-forward ">" nil t)          ;Move point to end of "<....>".
4681     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4682         (let ((message-id (match-string 1)))
4683           (goto-char point)
4684           (set-buffer gnus-summary-buffer)
4685           (gnus-summary-refer-article message-id))
4686       (goto-char (point))
4687       (error "No references around point"))))
4688
4689 (defun gnus-article-show-summary ()
4690   "Reconfigure windows to show summary buffer."
4691   (interactive)
4692   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4693       (error "There is no summary buffer for this article buffer")
4694     (gnus-article-set-globals)
4695     (gnus-configure-windows 'article)
4696     (gnus-summary-goto-subject gnus-current-article)
4697     (gnus-summary-position-point)))
4698
4699 (defun gnus-article-describe-briefly ()
4700   "Describe article mode commands briefly."
4701   (interactive)
4702   (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")))
4703
4704 (defun gnus-article-summary-command ()
4705   "Execute the last keystroke in the summary buffer."
4706   (interactive)
4707   (let ((obuf (current-buffer))
4708         (owin (current-window-configuration))
4709         func)
4710     (switch-to-buffer gnus-article-current-summary 'norecord)
4711     (setq func (lookup-key (current-local-map) (this-command-keys)))
4712     (call-interactively func)
4713     (set-buffer obuf)
4714     (set-window-configuration owin)
4715     (set-window-point (get-buffer-window (current-buffer)) (point))))
4716
4717 (defun gnus-article-summary-command-nosave ()
4718   "Execute the last keystroke in the summary buffer."
4719   (interactive)
4720   (let (func)
4721     (pop-to-buffer gnus-article-current-summary 'norecord)
4722     (setq func (lookup-key (current-local-map) (this-command-keys)))
4723     (call-interactively func)))
4724
4725 (defun gnus-article-check-buffer ()
4726   "Beep if not in an article buffer."
4727   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4728     (error "Command invoked outside of a Gnus article buffer")))
4729
4730 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4731   "Read a summary buffer key sequence and execute it from the article buffer."
4732   (interactive "P")
4733   (gnus-article-check-buffer)
4734   (let ((nosaves
4735          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4736            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4737            "=" "^" "\M-^" "|"))
4738         (nosave-but-article
4739          '("A\r"))
4740         (nosave-in-article
4741          '("\C-d"))
4742         (up-to-top
4743          '("n" "Gn" "p" "Gp"))
4744         keys new-sum-point)
4745     (save-excursion
4746       (set-buffer gnus-article-current-summary)
4747       (let (gnus-pick-mode)
4748         (push (or key last-command-event) unread-command-events)
4749         (setq keys (static-if (featurep 'xemacs)
4750                        (events-to-keys (read-key-sequence nil))
4751                      (read-key-sequence nil)))))
4752     (message "")
4753
4754     (if (or (member keys nosaves)
4755             (member keys nosave-but-article)
4756             (member keys nosave-in-article))
4757         (let (func)
4758           (save-window-excursion
4759             (pop-to-buffer gnus-article-current-summary 'norecord)
4760             ;; We disable the pick minor mode commands.
4761             (let (gnus-pick-mode)
4762               (setq func (lookup-key (current-local-map) keys))))
4763           (if (or (not func)
4764                   (numberp func))
4765               (ding)
4766             (unless (member keys nosave-in-article)
4767               (set-buffer gnus-article-current-summary))
4768             (call-interactively func)
4769             (setq new-sum-point (point)))
4770           (when (member keys nosave-but-article)
4771             (pop-to-buffer gnus-article-buffer 'norecord)))
4772       ;; These commands should restore window configuration.
4773       (let ((obuf (current-buffer))
4774             (owin (current-window-configuration))
4775             (opoint (point))
4776             (summary gnus-article-current-summary)
4777             func in-buffer selected)
4778         (if not-restore-window
4779             (pop-to-buffer summary 'norecord)
4780           (switch-to-buffer summary 'norecord))
4781         (setq in-buffer (current-buffer))
4782         ;; We disable the pick minor mode commands.
4783         (if (and (setq func (let (gnus-pick-mode)
4784                               (lookup-key (current-local-map) keys)))
4785                  (functionp func))
4786             (progn
4787               (call-interactively func)
4788               (setq new-sum-point (point))
4789               (when (eq in-buffer (current-buffer))
4790                 (setq selected (gnus-summary-select-article))
4791                 (set-buffer obuf)
4792                 (unless not-restore-window
4793                   (set-window-configuration owin))
4794                 (when (eq selected 'old)
4795                   (article-goto-body)
4796                   (set-window-start (get-buffer-window (current-buffer))
4797                                     1)
4798                   (set-window-point (get-buffer-window (current-buffer))
4799                                     (point)))
4800                 (let ((win (get-buffer-window gnus-article-current-summary)))
4801                   (when win
4802                     (set-window-point win new-sum-point))))    )
4803           (switch-to-buffer gnus-article-buffer)
4804           (ding))))))
4805
4806 (defun gnus-article-describe-key (key)
4807   "Display documentation of the function invoked by KEY.  KEY is a string."
4808   (interactive "kDescribe key: ")
4809   (gnus-article-check-buffer)
4810   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4811       (save-excursion
4812         (set-buffer gnus-article-current-summary)
4813         (let (gnus-pick-mode)
4814           (if (featurep 'xemacs)
4815               (progn
4816                 (push (elt key 0) unread-command-events)
4817                 (setq key (events-to-keys
4818                            (read-key-sequence "Describe key: "))))
4819             (setq unread-command-events
4820                   (mapcar
4821                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4822                    key))
4823             (setq key (read-key-sequence "Describe key: "))))
4824         (describe-key key))
4825     (describe-key key)))
4826
4827 (defun gnus-article-describe-key-briefly (key &optional insert)
4828   "Display documentation of the function invoked by KEY.  KEY is a string."
4829   (interactive "kDescribe key: \nP")
4830   (gnus-article-check-buffer)
4831   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4832       (save-excursion
4833         (set-buffer gnus-article-current-summary)
4834         (let (gnus-pick-mode)
4835           (if (featurep 'xemacs)
4836               (progn
4837                 (push (elt key 0) unread-command-events)
4838                 (setq key (events-to-keys
4839                            (read-key-sequence "Describe key: "))))
4840             (setq unread-command-events
4841                   (mapcar
4842                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4843                    key))
4844             (setq key (read-key-sequence "Describe key: "))))
4845         (describe-key-briefly key insert))
4846     (describe-key-briefly key insert)))
4847
4848 (defun gnus-article-hide (&optional arg force)
4849   "Hide all the gruft in the current article.
4850 This means that PGP stuff, signatures, cited text and (some)
4851 headers will be hidden.
4852 If given a prefix, show the hidden text instead."
4853   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4854   (gnus-article-hide-headers arg)
4855   (gnus-article-hide-list-identifiers arg)
4856   (gnus-article-hide-pgp arg)
4857   (gnus-article-hide-citation-maybe arg force)
4858   (gnus-article-hide-signature arg))
4859
4860 (defun gnus-article-maybe-highlight ()
4861   "Do some article highlighting if article highlighting is requested."
4862   (when (gnus-visual-p 'article-highlight 'highlight)
4863     (gnus-article-highlight-some)))
4864
4865 (defun gnus-check-group-server ()
4866   ;; Make sure the connection to the server is alive.
4867   (unless (gnus-server-opened
4868            (gnus-find-method-for-group gnus-newsgroup-name))
4869     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4870     (gnus-request-group gnus-newsgroup-name t)))
4871
4872 (defun gnus-request-article-this-buffer (article group)
4873   "Get an article and insert it into this buffer.
4874 T-gnus change: Insert an article into `gnus-original-article-buffer'."
4875   (let (do-update-line sparse-header)
4876     ;; The current buffer is `gnus-article-buffer'.
4877     (prog1
4878         (save-excursion
4879           (erase-buffer)
4880           (gnus-kill-all-overlays)
4881           (setq group (or group gnus-newsgroup-name))
4882
4883           ;; Using `gnus-request-article' directly will insert the article into
4884           ;; `nntp-server-buffer' - so we'll save some time by not having to
4885           ;; copy it from the server buffer into the article buffer.
4886
4887           ;; We only request an article by message-id when we do not have the
4888           ;; headers for it, so we'll have to get those.
4889           (when (stringp article)
4890             (gnus-read-header article))
4891
4892           ;; If the article number is negative, that means that this article
4893           ;; doesn't belong in this newsgroup (possibly), so we find its
4894           ;; message-id and request it by id instead of number.
4895           (when (and (numberp article)
4896                      gnus-summary-buffer
4897                      (get-buffer gnus-summary-buffer)
4898                      (gnus-buffer-exists-p gnus-summary-buffer))
4899             (save-excursion
4900               (set-buffer gnus-summary-buffer)
4901               (let ((header (gnus-summary-article-header article)))
4902                 (when (< article 0)
4903                   (cond
4904                    ((memq article gnus-newsgroup-sparse)
4905                     ;; This is a sparse gap article.
4906                     (setq do-update-line article)
4907                     (setq article (mail-header-id header))
4908                     (setq sparse-header (gnus-read-header article))
4909                     (setq gnus-newsgroup-sparse
4910                           (delq article gnus-newsgroup-sparse)))
4911                    ((vectorp header)
4912                     ;; It's a real article.
4913                     (setq article (mail-header-id header)))
4914                    (t
4915                     ;; It is an extracted pseudo-article.
4916                     (setq article 'pseudo)
4917                     (gnus-request-pseudo-article header))))
4918
4919                 (let ((method (gnus-find-method-for-group
4920                                gnus-newsgroup-name)))
4921                   (when (and (eq (car method) 'nneething)
4922                              (vectorp header))
4923                     (let ((dir (expand-file-name
4924                                 (mail-header-subject header)
4925                                 (file-name-as-directory
4926                                  (or (cadr (assq 'nneething-address method))
4927                                      (nth 1 method))))))
4928                       (when (file-directory-p dir)
4929                         (setq article 'nneething)
4930                         (gnus-group-enter-directory dir))))))))
4931           (setq gnus-original-article (cons group article))
4932
4933           ;; The current buffer is `gnus-original-article-buffer'. 
4934           (if (get-buffer gnus-original-article-buffer)
4935               (set-buffer gnus-original-article-buffer)
4936             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4937             (set-buffer-multibyte nil)
4938             (buffer-disable-undo)
4939             (setq major-mode 'gnus-original-article-mode)
4940             (setq buffer-read-only nil))
4941
4942           (cond
4943            ;; Refuse to select canceled articles.
4944            ((and (numberp article)
4945                  gnus-summary-buffer
4946                  (get-buffer gnus-summary-buffer)
4947                  (gnus-buffer-exists-p gnus-summary-buffer)
4948                  (eq (cdr (save-excursion
4949                             (set-buffer gnus-summary-buffer)
4950                             (assq article gnus-newsgroup-reads)))
4951                      gnus-canceled-mark))
4952             nil)
4953            ;; Check the backlog.
4954            ((and gnus-keep-backlog
4955                  (gnus-backlog-request-article group article (current-buffer)))
4956             'article)
4957            ;; Check asynchronous pre-fetch.
4958            ((gnus-async-request-fetched-article group article (current-buffer))
4959             (gnus-async-prefetch-next group article gnus-summary-buffer)
4960             (when (and (numberp article) gnus-keep-backlog)
4961               (gnus-backlog-enter-article group article (current-buffer)))
4962             'article)
4963            ;; Check the cache.
4964            ((and gnus-use-cache
4965                  (numberp article)
4966                  (gnus-cache-request-article article group))
4967             'article)
4968            ;; Get the article and put into the article buffer.
4969            ((or (stringp article)
4970                 (numberp article))
4971             (let ((gnus-override-method gnus-override-method)
4972                   (methods (and (stringp article)
4973                                 gnus-refer-article-method))
4974                   result
4975                   (buffer-read-only nil))
4976               (if (or (not (listp methods))
4977                       (and (symbolp (car methods))
4978                            (assq (car methods) nnoo-definition-alist)))
4979                   (setq methods (list methods)))
4980               (when (and (null gnus-override-method)
4981                          methods)
4982                 (setq gnus-override-method (pop methods)))
4983               (while (not result)
4984                 (when (eq gnus-override-method 'current)
4985                   (setq gnus-override-method
4986                         (with-current-buffer gnus-summary-buffer
4987                           gnus-current-select-method)))
4988                 (erase-buffer)
4989                 (gnus-kill-all-overlays)
4990                 (let ((gnus-newsgroup-name group))
4991                   (gnus-check-group-server))
4992                 (when (gnus-request-article article group (current-buffer))
4993                   (when (numberp article)
4994                     (gnus-async-prefetch-next group article
4995                                               gnus-summary-buffer)
4996                     (when gnus-keep-backlog
4997                       (gnus-backlog-enter-article
4998                        group article (current-buffer))))
4999                   (setq result 'article))
5000                 (if (not result)
5001                     (if methods
5002                         (setq gnus-override-method (pop methods))
5003                       (setq result 'done))))
5004               (and (eq result 'article) 'article)))
5005            ;; It was a pseudo.
5006            (t article)))
5007
5008       ;; Associate this article with the current summary buffer.
5009       (setq gnus-article-current-summary gnus-summary-buffer)
5010
5011       ;; Copy the requested article from `gnus-original-article-buffer'.
5012       (unless (equal (buffer-name (current-buffer))
5013                      (buffer-name (get-buffer gnus-original-article-buffer)))
5014         (insert-buffer gnus-original-article-buffer))
5015
5016       ;; Decode charsets.
5017       (run-hooks 'gnus-article-decode-hook)
5018       ;; Mark article as decoded or not.
5019       (setq gnus-article-decoded-p gnus-article-decode-hook)
5020
5021       ;; Update sparse articles.
5022       (when (and do-update-line
5023                  (or (numberp article)
5024                      (stringp article)))
5025         (let ((buf (current-buffer)))
5026           (set-buffer gnus-summary-buffer)
5027           (gnus-summary-update-article do-update-line sparse-header)
5028           (gnus-summary-goto-subject do-update-line nil t)
5029           (set-window-point (get-buffer-window (current-buffer) t)
5030                             (point))
5031           (set-buffer buf))))))
5032
5033 ;;;
5034 ;;; Article editing
5035 ;;;
5036
5037 (defcustom gnus-article-edit-mode-hook nil
5038   "Hook run in article edit mode buffers."
5039   :group 'gnus-article-various
5040   :type 'hook)
5041
5042 (defcustom gnus-article-edit-article-setup-function
5043   'gnus-article-mime-edit-article-setup
5044   "Function called to setup an editing article buffer."
5045   :group 'gnus-article-various
5046   :type 'function)
5047
5048 (defvar gnus-article-edit-done-function nil)
5049
5050 (defvar gnus-article-edit-mode-map nil)
5051
5052 ;; Should we be using derived.el for this?
5053 (unless gnus-article-edit-mode-map
5054   (setq gnus-article-edit-mode-map (make-sparse-keymap))
5055   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5056
5057   (gnus-define-keys gnus-article-edit-mode-map
5058     "\C-c\C-c" gnus-article-edit-done
5059     "\C-c\C-k" gnus-article-edit-exit)
5060
5061   (gnus-define-keys (gnus-article-edit-wash-map
5062                      "\C-c\C-w" gnus-article-edit-mode-map)
5063     "f" gnus-article-edit-full-stops))
5064
5065 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5066   "Major mode for editing articles.
5067 This is an extended text-mode.
5068
5069 \\{gnus-article-edit-mode-map}"
5070   (make-local-variable 'gnus-article-edit-done-function)
5071   (make-local-variable 'gnus-prev-winconf)
5072   (set (make-local-variable 'font-lock-defaults)
5073        '(message-font-lock-keywords t))
5074   (setq buffer-read-only nil)
5075   (buffer-enable-undo)
5076   (widen))
5077
5078 (defun gnus-article-edit (&optional force)
5079   "Edit the current article.
5080 This will have permanent effect only in mail groups.
5081 If FORCE is non-nil, allow editing of articles even in read-only
5082 groups."
5083   (interactive "P")
5084   (when (and (not force)
5085              (gnus-group-read-only-p))
5086     (error "The current newsgroup does not support article editing"))
5087   (gnus-article-date-original)
5088   (gnus-article-edit-article
5089    'ignore
5090    `(lambda (no-highlight)
5091       'ignore
5092       (gnus-summary-edit-article-done
5093        ,(or (mail-header-references gnus-current-headers) "")
5094        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5095
5096 (defun gnus-article-edit-article (start-func exit-func)
5097   "Start editing the contents of the current article buffer."
5098   (let ((winconf (current-window-configuration)))
5099     (set-buffer gnus-article-buffer)
5100     (gnus-article-edit-mode)
5101     (funcall start-func)
5102     (set-buffer-modified-p nil)
5103     (gnus-configure-windows 'edit-article)
5104     (setq gnus-article-edit-done-function exit-func)
5105     (setq gnus-prev-winconf winconf)
5106     (when gnus-article-edit-article-setup-function
5107       (funcall gnus-article-edit-article-setup-function))
5108     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5109
5110 (defun gnus-article-edit-done (&optional arg)
5111   "Update the article edits and exit."
5112   (interactive "P")
5113   (let ((func gnus-article-edit-done-function)
5114         (buf (current-buffer))
5115         (start (window-start)))
5116     (remove-hook 'gnus-article-mode-hook
5117                  'gnus-article-mime-edit-article-unwind)
5118     ;; We remove all text props from the article buffer.
5119     (let ((content
5120            (buffer-substring-no-properties (point-min) (point-max)))
5121           (p (point)))
5122       (erase-buffer)
5123       (insert content)
5124       (let ((winconf gnus-prev-winconf))
5125         (gnus-article-mode)
5126         (set-window-configuration winconf)
5127         ;; Tippy-toe some to make sure that point remains where it was.
5128         (save-current-buffer
5129           (set-buffer buf)
5130           (set-window-start (get-buffer-window (current-buffer)) start)
5131           (goto-char p))))
5132     (save-excursion
5133       (set-buffer buf)
5134       (let ((buffer-read-only nil))
5135         (funcall func arg))
5136       ;; The cache and backlog have to be flushed somewhat.
5137       (when gnus-keep-backlog
5138         (gnus-backlog-remove-article
5139          (car gnus-article-current) (cdr gnus-article-current)))
5140       ;; Flush original article as well.
5141       (save-excursion
5142         (when (get-buffer gnus-original-article-buffer)
5143           (set-buffer gnus-original-article-buffer)
5144           (setq gnus-original-article nil)))
5145       (when gnus-use-cache
5146         (gnus-cache-update-article
5147          (car gnus-article-current) (cdr gnus-article-current))))
5148     (set-buffer buf)
5149     (set-window-start (get-buffer-window buf) start)
5150     (set-window-point (get-buffer-window buf) (point))))
5151
5152 (defun gnus-article-edit-exit ()
5153   "Exit the article editing without updating."
5154   (interactive)
5155   (when (or (not (buffer-modified-p))
5156             (yes-or-no-p "Article modified; kill anyway? "))
5157     (let ((curbuf (current-buffer))
5158           (p (point))
5159           (window-start (window-start)))
5160       (erase-buffer)
5161       (if (gnus-buffer-live-p gnus-original-article-buffer)
5162           (insert-buffer gnus-original-article-buffer))
5163       (let ((winconf gnus-prev-winconf))
5164         (gnus-article-mode)
5165         (set-window-configuration winconf)
5166         ;; Tippy-toe some to make sure that point remains where it was.
5167         (save-current-buffer
5168           (set-buffer curbuf)
5169           (set-window-start (get-buffer-window (current-buffer)) window-start)
5170           (goto-char p))))))
5171
5172 (defun gnus-article-edit-full-stops ()
5173   "Interactively repair spacing at end of sentences."
5174   (interactive)
5175   (save-excursion
5176     (goto-char (point-min))
5177     (search-forward-regexp "^$" nil t)
5178     (let ((case-fold-search nil))
5179       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5180
5181 ;;;
5182 ;;; Article editing with MIME-Edit
5183 ;;;
5184
5185 (defcustom gnus-article-mime-edit-article-setup-hook nil
5186   "Hook run after setting up a MIME editing article buffer."
5187   :group 'gnus-article-various
5188   :type 'hook)
5189
5190 (defun gnus-article-mime-edit-article-unwind ()
5191   "Unwind `gnus-article-buffer' if article editing was given up."
5192   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5193   (when (featurep 'font-lock)
5194     (setq font-lock-defaults nil)
5195     (font-lock-mode -1))
5196   (when mime-edit-mode-flag
5197     (mime-edit-exit 'nomime 'no-error)
5198     (message "")))
5199
5200 (defun gnus-article-mime-edit-article-setup ()
5201   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5202 after replacing with the original article."
5203   (setq gnus-show-mime t)
5204   (setq gnus-article-edit-done-function
5205         `(lambda (&rest args)
5206            (when mime-edit-mode-flag
5207              (let (mime-edit-insert-user-agent-field)
5208                (mime-edit-exit))
5209              (message ""))
5210            (goto-char (point-min))
5211            (let (case-fold-search)
5212              (when (re-search-forward
5213                     (format "^%s$" (regexp-quote mail-header-separator))
5214                     nil t)
5215                (replace-match "")))
5216            (apply ,gnus-article-edit-done-function args)
5217            (insert
5218             (prog1
5219                 (buffer-substring-no-properties (point-min) (point-max))
5220               (set-buffer (get-buffer-create gnus-original-article-buffer))
5221               (erase-buffer)))
5222            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5223            (set-buffer gnus-article-buffer)
5224            (gnus-article-prepare-display)))
5225   (substitute-key-definition 'gnus-article-edit-done
5226                              'gnus-article-mime-edit-done
5227                              gnus-article-edit-mode-map)
5228   (substitute-key-definition 'gnus-article-edit-exit
5229                              'gnus-article-mime-edit-exit
5230                              gnus-article-edit-mode-map)
5231   (erase-buffer)
5232   (insert-buffer gnus-original-article-buffer)
5233   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5234     (fset 'mime-edit-decode-single-part-in-buffer
5235           (lambda (&rest args)
5236             (if (let ((content-type (car args)))
5237                   (and (eq 'message (mime-content-type-primary-type
5238                                      content-type))
5239                        (eq 'rfc822 (mime-content-type-subtype content-type))))
5240                 (setcar (cdr args) 'not-decode-text))
5241             (apply ofn args)))
5242     (unwind-protect
5243         (mime-edit-again)
5244       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
5245   (when (featurep 'font-lock)
5246     (set (make-local-variable 'font-lock-defaults)
5247          '(message-font-lock-keywords t))
5248     (font-lock-set-defaults)
5249     (turn-on-font-lock))
5250   (set-buffer-modified-p nil)
5251   (delete-other-windows)
5252   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5253   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5254
5255 (defun gnus-article-mime-edit-done (&optional arg)
5256   "Update the article MIME edits and exit."
5257   (interactive "P")
5258   (when (featurep 'font-lock)
5259     (setq font-lock-defaults nil)
5260     (font-lock-mode -1))
5261   (gnus-article-edit-done arg))
5262
5263 (defun gnus-article-mime-edit-exit ()
5264   "Exit the article MIME editing without updating."
5265   (interactive)
5266   (when (or (not (buffer-modified-p))
5267             (yes-or-no-p "Article modified; kill anyway? "))
5268     (when (featurep 'font-lock)
5269       (setq font-lock-defaults nil)
5270       (font-lock-mode -1))
5271     (when mime-edit-mode-flag
5272       (let (mime-edit-insert-user-agent-field)
5273         (mime-edit-exit))
5274       (message ""))
5275     (goto-char (point-min))
5276     (let (case-fold-search)
5277       (when (re-search-forward
5278              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5279         (replace-match "")))
5280     (let ((winconf gnus-prev-winconf))
5281       (insert (prog1
5282                   (buffer-substring-no-properties (point-min) (point-max))
5283                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5284                 (erase-buffer)))
5285       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5286       (set-buffer gnus-article-buffer)
5287       (gnus-article-prepare-display)
5288       (set-window-configuration winconf))))
5289
5290 ;;;
5291 ;;; Article highlights
5292 ;;;
5293
5294 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5295
5296 ;;; Internal Variables:
5297
5298 (defcustom gnus-button-url-regexp "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)\\)"
5299   "Regular expression that matches URLs."
5300   :group 'gnus-article-buttons
5301   :type 'regexp)
5302
5303 (defcustom gnus-button-alist
5304   `(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5305      0 t gnus-button-handle-news 3)
5306     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
5307      gnus-button-handle-news 2)
5308     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5309      1 t
5310      gnus-button-fetch-group 4)
5311     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5312     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5313      t gnus-button-message-id 3)
5314     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5315     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5316     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5317     ;; This is info
5318     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t
5319      gnus-button-handle-info 2)
5320     ;; This is how URLs _should_ be embedded in text...
5321     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
5322     ;; Raw URLs.
5323     (,gnus-button-url-regexp 0 t browse-url 0))
5324   "*Alist of regexps matching buttons in article bodies.
5325
5326 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5327 REGEXP: is the string matching text around the button,
5328 BUTTON: is the number of the regexp grouping actually matching the button,
5329 FORM: is a lisp expression which must eval to true for the button to
5330 be added,
5331 CALLBACK: is the function to call when the user push this button, and each
5332 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5333
5334 CALLBACK can also be a variable, in that case the value of that
5335 variable it the real callback function."
5336   :group 'gnus-article-buttons
5337   :type '(repeat (list regexp
5338                        (integer :tag "Button")
5339                        (sexp :tag "Form")
5340                        (function :tag "Callback")
5341                        (repeat :tag "Par"
5342                                :inline t
5343                                (integer :tag "Regexp group")))))
5344
5345 (defcustom gnus-header-button-alist
5346   `(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
5347      0 t gnus-button-message-id 0)
5348     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5349     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5350      0 t gnus-button-mailto 0)
5351     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5352     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5353     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5354     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5355      gnus-button-message-id 3))
5356   "*Alist of headers and regexps to match buttons in article heads.
5357
5358 This alist is very similar to `gnus-button-alist', except that each
5359 alist has an additional HEADER element first in each entry:
5360
5361 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5362
5363 HEADER is a regexp to match a header.  For a fuller explanation, see
5364 `gnus-button-alist'."
5365   :group 'gnus-article-buttons
5366   :group 'gnus-article-headers
5367   :type '(repeat (list (regexp :tag "Header")
5368                        regexp
5369                        (integer :tag "Button")
5370                        (sexp :tag "Form")
5371                        (function :tag "Callback")
5372                        (repeat :tag "Par"
5373                                :inline t
5374                                (integer :tag "Regexp group")))))
5375
5376 (defvar gnus-button-regexp nil)
5377 (defvar gnus-button-marker-list nil)
5378 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5379
5380 (defvar gnus-button-last nil)
5381 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5382
5383 ;;; Commands:
5384
5385 (defun gnus-article-push-button (event)
5386   "Check text under the mouse pointer for a callback function.
5387 If the text under the mouse pointer has a `gnus-callback' property,
5388 call it with the value of the `gnus-data' text property."
5389   (interactive "e")
5390   (set-buffer (window-buffer (posn-window (event-start event))))
5391   (let* ((pos (posn-point (event-start event)))
5392          (data (get-text-property pos 'gnus-data))
5393          (fun (get-text-property pos 'gnus-callback)))
5394     (goto-char pos)
5395     (when fun
5396       (funcall fun data))))
5397
5398 (defun gnus-article-press-button ()
5399   "Check text at point for a callback function.
5400 If the text at point has a `gnus-callback' property,
5401 call it with the value of the `gnus-data' text property."
5402   (interactive)
5403   (let* ((data (get-text-property (point) 'gnus-data))
5404          (fun (get-text-property (point) 'gnus-callback)))
5405     (when fun
5406       (funcall fun data))))
5407
5408 (defun gnus-article-prev-button (n)
5409   "Move point to N buttons backward.
5410 If N is negative, move forward instead."
5411   (interactive "p")
5412   (gnus-article-next-button (- n)))
5413
5414 (defun gnus-article-next-button (n)
5415   "Move point to N buttons forward.
5416 If N is negative, move backward instead."
5417   (interactive "p")
5418   (let ((function (if (< n 0) 'previous-single-property-change
5419                     'next-single-property-change))
5420         (inhibit-point-motion-hooks t)
5421         (backward (< n 0))
5422         (limit (if (< n 0) (point-min) (point-max))))
5423     (setq n (abs n))
5424     (while (and (not (= limit (point)))
5425                 (> n 0))
5426       ;; Skip past the current button.
5427       (when (get-text-property (point) 'gnus-callback)
5428         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5429       ;; Go to the next (or previous) button.
5430       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5431       ;; Put point at the start of the button.
5432       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5433         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5434       ;; Skip past intangible buttons.
5435       (when (get-text-property (point) 'intangible)
5436         (incf n))
5437       (decf n))
5438     (unless (zerop n)
5439       (gnus-message 5 "No more buttons"))
5440     n))
5441
5442 (defun gnus-article-highlight (&optional force)
5443   "Highlight current article.
5444 This function calls `gnus-article-highlight-headers',
5445 `gnus-article-highlight-citation',
5446 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5447 do the highlighting.  See the documentation for those functions."
5448   (interactive (list 'force))
5449   (gnus-article-highlight-headers)
5450   (gnus-article-highlight-citation force)
5451   (gnus-article-highlight-signature)
5452   (gnus-article-add-buttons force)
5453   (gnus-article-add-buttons-to-head))
5454
5455 (defun gnus-article-highlight-some (&optional force)
5456   "Highlight current article.
5457 This function calls `gnus-article-highlight-headers',
5458 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5459 do the highlighting.  See the documentation for those functions."
5460   (interactive (list 'force))
5461   (gnus-article-highlight-headers)
5462   (gnus-article-highlight-signature)
5463   (gnus-article-add-buttons))
5464
5465 (defun gnus-article-highlight-headers ()
5466   "Highlight article headers as specified by `gnus-header-face-alist'."
5467   (interactive)
5468   (save-excursion
5469     (set-buffer gnus-article-buffer)
5470     (save-restriction
5471       (let ((alist gnus-header-face-alist)
5472             (buffer-read-only nil)
5473             (case-fold-search t)
5474             (inhibit-point-motion-hooks t)
5475             entry regexp header-face field-face from hpoints fpoints)
5476         (article-narrow-to-head)
5477         (while (setq entry (pop alist))
5478           (goto-char (point-min))
5479           (setq regexp (concat "^\\("
5480                                (if (string-equal "" (nth 0 entry))
5481                                    "[^\t ]"
5482                                  (nth 0 entry))
5483                                "\\)")
5484                 header-face (nth 1 entry)
5485                 field-face (nth 2 entry))
5486           (while (and (re-search-forward regexp nil t)
5487                       (not (eobp)))
5488             (beginning-of-line)
5489             (setq from (point))
5490             (unless (search-forward ":" nil t)
5491               (forward-char 1))
5492             (when (and header-face
5493                        (not (memq (point) hpoints)))
5494               (push (point) hpoints)
5495               (gnus-put-text-property from (point) 'face header-face))
5496             (when (and field-face
5497                        (not (memq (setq from (point)) fpoints)))
5498               (push from fpoints)
5499               (if (re-search-forward "^[^ \t]" nil t)
5500                   (forward-char -2)
5501                 (goto-char (point-max)))
5502               (gnus-put-text-property from (point) 'face field-face))))))))
5503
5504 (defun gnus-article-highlight-signature ()
5505   "Highlight the signature in an article.
5506 It does this by highlighting everything after
5507 `gnus-signature-separator' using `gnus-signature-face'."
5508   (interactive)
5509   (when gnus-signature-face
5510     (save-excursion
5511       (set-buffer gnus-article-buffer)
5512       (let ((buffer-read-only nil)
5513             (inhibit-point-motion-hooks t))
5514         (save-restriction
5515           (when (gnus-article-narrow-to-signature)
5516             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5517                               'face gnus-signature-face)))))))
5518
5519 (defun gnus-article-buttonize-signature ()
5520   "Add button to the signature."
5521   (interactive)
5522   (save-excursion
5523     (set-buffer gnus-article-buffer)
5524     (let ((buffer-read-only nil)
5525           (inhibit-point-motion-hooks t))
5526       (when (gnus-article-search-signature)
5527         (gnus-article-add-button (match-beginning 0) (match-end 0)
5528                                  'gnus-signature-toggle
5529                                  (set-marker (make-marker)
5530                                              (1+ (match-end 0))))))))
5531
5532 (defun gnus-button-in-region-p (b e prop)
5533   "Say whether PROP exists in the region."
5534   (text-property-not-all b e prop nil))
5535
5536 (defun gnus-article-add-buttons (&optional force)
5537   "Find external references in the article and make buttons of them.
5538 \"External references\" are things like Message-IDs and URLs, as
5539 specified by `gnus-button-alist'."
5540   (interactive (list 'force))
5541   (save-excursion
5542     (set-buffer gnus-article-buffer)
5543     (let ((buffer-read-only nil)
5544           (inhibit-point-motion-hooks t)
5545           (case-fold-search t)
5546           (alist gnus-button-alist)
5547           beg entry regexp)
5548       ;; Remove all old markers.
5549       (let (marker entry new-list)
5550         (while (setq marker (pop gnus-button-marker-list))
5551           (if (or (< marker (point-min)) (>= marker (point-max)))
5552               (push marker new-list)
5553             (goto-char marker)
5554             (when (setq entry (gnus-button-entry))
5555               (put-text-property (match-beginning (nth 1 entry))
5556                                  (match-end (nth 1 entry))
5557                                  'gnus-callback nil))
5558             (set-marker marker nil)))
5559         (setq gnus-button-marker-list new-list))
5560       ;; We skip the headers.
5561       (article-goto-body)
5562       (setq beg (point))
5563       (while (setq entry (pop alist))
5564         (setq regexp (car entry))
5565         (goto-char beg)
5566         (while (re-search-forward regexp nil t)
5567           (let* ((start (and entry (match-beginning (nth 1 entry))))
5568                  (end (and entry (match-end (nth 1 entry))))
5569                  (from (match-beginning 0)))
5570             (when (and (or (eq t (nth 2 entry))
5571                            (eval (nth 2 entry)))
5572                        (not (gnus-button-in-region-p
5573                              start end 'gnus-callback)))
5574               ;; That optional form returned non-nil, so we add the
5575               ;; button.
5576               (gnus-article-add-button
5577                start end 'gnus-button-push
5578                (car (push (set-marker (make-marker) from)
5579                           gnus-button-marker-list))))))))))
5580
5581 ;; Add buttons to the head of an article.
5582 (defun gnus-article-add-buttons-to-head ()
5583   "Add buttons to the head of the article."
5584   (interactive)
5585   (save-excursion
5586     (set-buffer gnus-article-buffer)
5587     (save-restriction
5588       (let ((buffer-read-only nil)
5589             (inhibit-point-motion-hooks t)
5590             (case-fold-search t)
5591             (alist gnus-header-button-alist)
5592             entry beg end)
5593         (article-narrow-to-head)
5594         (while alist
5595           ;; Each alist entry.
5596           (setq entry (car alist)
5597                 alist (cdr alist))
5598           (goto-char (point-min))
5599           (while (re-search-forward (car entry) nil t)
5600             ;; Each header matching the entry.
5601             (setq beg (match-beginning 0))
5602             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5603                                (match-beginning 0))
5604                           (point-max)))
5605             (goto-char beg)
5606             (while (re-search-forward (nth 1 entry) end t)
5607               ;; Each match within a header.
5608               (let* ((entry (cdr entry))
5609                      (start (match-beginning (nth 1 entry)))
5610                      (end (match-end (nth 1 entry)))
5611                      (form (nth 2 entry)))
5612                 (goto-char (match-end 0))
5613                 (when (eval form)
5614                   (gnus-article-add-button
5615                    start end (nth 3 entry)
5616                    (buffer-substring (match-beginning (nth 4 entry))
5617                                      (match-end (nth 4 entry)))))))
5618             (goto-char end)))))))
5619
5620 ;;; External functions:
5621
5622 (defun gnus-article-add-button (from to fun &optional data)
5623   "Create a button between FROM and TO with callback FUN and data DATA."
5624   (when gnus-article-button-face
5625     (gnus-overlay-put (gnus-make-overlay from to)
5626                       'face gnus-article-button-face))
5627   (gnus-add-text-properties
5628    from to
5629    (nconc (and gnus-article-mouse-face
5630                (list gnus-mouse-face-prop gnus-article-mouse-face))
5631           (list 'gnus-callback fun)
5632           (and data (list 'gnus-data data))))
5633   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5634                          ;; Quote `:button-keymap' for Mule 2.3
5635                          ;; but it won't work.
5636                          ':button-keymap gnus-widget-button-keymap))
5637
5638 ;;; Internal functions:
5639
5640 (defun gnus-article-set-globals ()
5641   (save-excursion
5642     (set-buffer gnus-summary-buffer)
5643     (gnus-set-global-variables)))
5644
5645 (defun gnus-signature-toggle (end)
5646   (save-excursion
5647     (set-buffer gnus-article-buffer)
5648     (let ((buffer-read-only nil)
5649           (inhibit-point-motion-hooks t)
5650           (limit (next-single-property-change end 'mime-view-entity
5651                                               nil (point-max))))
5652       (if (text-property-any end limit 'article-type 'signature)
5653           (progn
5654             (setq gnus-article-wash-types
5655                   (delq 'signature gnus-article-wash-types))
5656             (gnus-remove-text-properties-when
5657              'article-type 'signature end limit
5658              (cons 'article-type (cons 'signature
5659                                        gnus-hidden-properties))))
5660         (or (memq 'signature gnus-article-wash-types)
5661             (push 'signature gnus-article-wash-types))
5662         (gnus-add-text-properties-when
5663          'article-type nil end limit
5664          (cons 'article-type (cons 'signature
5665                                    gnus-hidden-properties)))))
5666     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
5667       (gnus-set-mode-line 'article))))
5668
5669 (defun gnus-button-entry ()
5670   ;; Return the first entry in `gnus-button-alist' matching this place.
5671   (let ((alist gnus-button-alist)
5672         (entry nil))
5673     (while alist
5674       (setq entry (pop alist))
5675       (if (looking-at (car entry))
5676           (setq alist nil)
5677         (setq entry nil)))
5678     entry))
5679
5680 (defun gnus-button-push (marker)
5681   ;; Push button starting at MARKER.
5682   (save-excursion
5683     (goto-char marker)
5684     (let* ((entry (gnus-button-entry))
5685            (inhibit-point-motion-hooks t)
5686            (fun (nth 3 entry))
5687            (args (mapcar (lambda (group)
5688                            (let ((string (match-string group)))
5689                              (gnus-set-text-properties
5690                               0 (length string) nil string)
5691                              string))
5692                          (nthcdr 4 entry))))
5693       (cond
5694        ((fboundp fun)
5695         (apply fun args))
5696        ((and (boundp fun)
5697              (fboundp (symbol-value fun)))
5698         (apply (symbol-value fun) args))
5699        (t
5700         (gnus-message 1 "You must define `%S' to use this button"
5701                       (cons fun args)))))))
5702
5703 (defun gnus-parse-news-url (url)
5704   (let (scheme server group message-id articles)
5705     (with-temp-buffer
5706       (insert url)
5707       (goto-char (point-min))
5708       (when (looking-at "\\([A-Za-z]+\\):")
5709         (setq scheme (match-string 1))
5710         (goto-char (match-end 0)))
5711       (when (looking-at "//\\([^/]+\\)/")
5712         (setq server (match-string 1))
5713         (goto-char (match-end 0)))
5714
5715       (cond
5716        ((looking-at "\\(.*@.*\\)")
5717         (setq message-id (match-string 1)))
5718        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
5719         (setq group (match-string 1)
5720               articles (split-string (match-string 2) "-")))
5721        ((looking-at "\\([^/]+\\)/?")
5722         (setq group (match-string 1)))
5723        (t
5724         (error "Unknown news URL syntax"))))
5725     (list scheme server group message-id articles)))
5726
5727 (defun gnus-button-handle-news (url)
5728   "Fetch a news URL."
5729   (destructuring-bind (scheme server group message-id articles)
5730       (gnus-parse-news-url url)
5731     (cond
5732      (message-id
5733       (save-excursion
5734         (set-buffer gnus-summary-buffer)
5735         (if server
5736             (let ((gnus-refer-article-method (list (list 'nntp server))))
5737               (gnus-summary-refer-article message-id))
5738           (gnus-summary-refer-article message-id))))
5739      (group
5740       (gnus-button-fetch-group url)))))
5741
5742 (defun gnus-button-handle-info (url)
5743   "Fetch an info URL."
5744   (if (string-match 
5745        "^\\([^:/]+\\)?/\\(.*\\)"
5746        url)
5747       (gnus-info-find-node
5748        (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
5749                        "Gnus") 
5750                ")" 
5751                (gnus-url-unhex-string (match-string 2 url))))
5752     (error "Can't parse %s" url)))
5753
5754 (defun gnus-button-message-id (message-id)
5755   "Fetch MESSAGE-ID."
5756   (save-excursion
5757     (set-buffer gnus-summary-buffer)
5758     (gnus-summary-refer-article message-id)))
5759
5760 (defun gnus-button-fetch-group (address)
5761   "Fetch GROUP specified by ADDRESS."
5762   (if (not (string-match "[:/]" address))
5763       ;; This is just a simple group url.
5764       (gnus-group-read-ephemeral-group address gnus-select-method)
5765     (if (not
5766          (string-match
5767           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
5768           address))
5769         (error "Can't parse %s" address)
5770       (gnus-group-read-ephemeral-group
5771        (match-string 4 address)
5772        `(nntp ,(match-string 1 address)
5773               (nntp-address ,(match-string 1 address))
5774               (nntp-port-number ,(if (match-end 3)
5775                                      (match-string 3 address)
5776                                    "nntp")))
5777        nil nil nil
5778        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
5779
5780 (defun gnus-url-parse-query-string (query &optional downcase)
5781   (let (retval pairs cur key val)
5782     (setq pairs (split-string query "&"))
5783     (while pairs
5784       (setq cur (car pairs)
5785             pairs (cdr pairs))
5786       (if (not (string-match "=" cur))
5787           nil                           ; Grace
5788         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5789               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5790         (if downcase
5791             (setq key (downcase key)))
5792         (setq cur (assoc key retval))
5793         (if cur
5794             (setcdr cur (cons val (cdr cur)))
5795           (setq retval (cons (list key val) retval)))))
5796     retval))
5797
5798 (defun gnus-url-unhex (x)
5799   (if (> x ?9)
5800       (if (>= x ?a)
5801           (+ 10 (- x ?a))
5802         (+ 10 (- x ?A)))
5803     (- x ?0)))
5804
5805 (defun gnus-url-unhex-string (str &optional allow-newlines)
5806   "Remove %XXX embedded spaces, etc in a url.
5807 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5808 decoding of carriage returns and line feeds in the string, which is normally
5809 forbidden in URL encoding."
5810   (setq str (or (nnheader-replace-chars-in-string str ?+ ? ) ""))
5811   (let ((tmp "")
5812         (case-fold-search t))
5813     (while (string-match "%[0-9a-f][0-9a-f]" str)
5814       (let* ((start (match-beginning 0))
5815              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5816              (code (+ (* 16 ch1)
5817                       (gnus-url-unhex (elt str (+ start 2))))))
5818         (setq tmp (concat
5819                    tmp (substring str 0 start)
5820                    (cond
5821                     (allow-newlines
5822                      (char-to-string code))
5823                     ((or (= code ?\n) (= code ?\r))
5824                      " ")
5825                     (t (char-to-string code))))
5826               str (substring str (match-end 0)))))
5827     (setq tmp (concat tmp str))
5828     tmp))
5829
5830 (defun gnus-url-mailto (url)
5831   ;; Send mail to someone
5832   (when (string-match "mailto:/*\\(.*\\)" url)
5833     (setq url (substring url (match-beginning 1) nil)))
5834   (let (to args subject func)
5835     (if (string-match (regexp-quote "?") url)
5836         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5837               args (gnus-url-parse-query-string
5838                     (substring url (match-end 0) nil) t))
5839       (setq to (gnus-url-unhex-string url)))
5840     (setq args (cons (list "to" to) args)
5841           subject (cdr-safe (assoc "subject" args)))
5842     (gnus-msg-mail)
5843     (while args
5844       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5845       (if (fboundp func)
5846           (funcall func)
5847         (message-position-on-field (caar args)))
5848       (insert (mapconcat 'identity (cdar args) ", "))
5849       (setq args (cdr args)))
5850     (if subject
5851         (message-goto-body)
5852       (message-goto-subject))))
5853
5854 (defun gnus-button-embedded-url (address)
5855   "Activate ADDRESS with `browse-url'."
5856   (browse-url (gnus-strip-whitespace address)))
5857
5858 (eval-when-compile
5859   ;; Silence the byte-compiler.
5860   (autoload 'smiley-toggle-buffer "gnus-bitmap"))
5861 (defun gnus-article-smiley-display ()
5862   "Display \"smileys\" as small graphical icons."
5863   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5864
5865 ;;; Next/prev buttons in the article buffer.
5866
5867 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5868 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5869
5870 (defvar gnus-prev-page-map nil)
5871 (unless gnus-prev-page-map
5872   (setq gnus-prev-page-map (make-sparse-keymap))
5873   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5874   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5875
5876 (static-if (featurep 'xemacs)
5877     (defun gnus-insert-prev-page-button ()
5878       (let ((buffer-read-only nil))
5879         (gnus-eval-format
5880          gnus-prev-page-line-format nil
5881          `(gnus-prev t local-map ,gnus-prev-page-map
5882                      gnus-callback gnus-article-button-prev-page
5883                      article-type annotation))))
5884   (defun gnus-insert-prev-page-button ()
5885     (let ((buffer-read-only nil)
5886           (situation (get-text-property (point-min) 'mime-view-situation)))
5887       (set-keymap-parent gnus-prev-page-map (current-local-map))
5888       (gnus-eval-format
5889        gnus-prev-page-line-format nil
5890        `(gnus-prev t local-map ,gnus-prev-page-map
5891                    gnus-callback gnus-article-button-prev-page
5892                    article-type annotation
5893                    mime-view-situation ,situation))))
5894   )
5895
5896 (defvar gnus-next-page-map nil)
5897 (unless gnus-next-page-map
5898   (setq gnus-next-page-map (make-sparse-keymap))
5899   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5900   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5901
5902 (defun gnus-button-next-page ()
5903   "Go to the next page."
5904   (interactive)
5905   (let ((win (selected-window)))
5906     (select-window (get-buffer-window gnus-article-buffer t))
5907     (gnus-article-next-page)
5908     (select-window win)))
5909
5910 (defun gnus-button-prev-page ()
5911   "Go to the prev page."
5912   (interactive)
5913   (let ((win (selected-window)))
5914     (select-window (get-buffer-window gnus-article-buffer t))
5915     (gnus-article-prev-page)
5916     (select-window win)))
5917
5918 (static-if (featurep 'xemacs)
5919     (defun gnus-insert-next-page-button ()
5920       (let ((buffer-read-only nil))
5921         (gnus-eval-format gnus-next-page-line-format nil
5922                           `(gnus-next
5923                             t local-map ,gnus-next-page-map
5924                             gnus-callback gnus-article-button-next-page
5925                             article-type annotation))))
5926   (defun gnus-insert-next-page-button ()
5927     (let ((buffer-read-only nil)
5928           (situation (get-text-property (point-min) 'mime-view-situation)))
5929       (set-keymap-parent gnus-next-page-map (current-local-map))
5930       (gnus-eval-format gnus-next-page-line-format nil
5931                         `(gnus-next
5932                           t local-map ,gnus-next-page-map
5933                           gnus-callback gnus-article-button-next-page
5934                           article-type annotation
5935                           mime-view-situation ,situation))))
5936   )
5937
5938 (defun gnus-article-button-next-page (arg)
5939   "Go to the next page."
5940   (interactive "P")
5941   (let ((win (selected-window)))
5942     (select-window (get-buffer-window gnus-article-buffer t))
5943     (gnus-article-next-page)
5944     (select-window win)))
5945
5946 (defun gnus-article-button-prev-page (arg)
5947   "Go to the prev page."
5948   (interactive "P")
5949   (let ((win (selected-window)))
5950     (select-window (get-buffer-window gnus-article-buffer t))
5951     (gnus-article-prev-page)
5952     (select-window win)))
5953
5954 (defvar gnus-decode-header-methods
5955   '(mail-decode-encoded-word-region)
5956   "List of methods used to decode headers.
5957
5958 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5959 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5960 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5961 whose names match REGEXP.
5962
5963 For example:
5964 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5965  mail-decode-encoded-word-region
5966  (\"chinese\" . rfc1843-decode-region))
5967 ")
5968
5969 (defvar gnus-decode-header-methods-cache nil)
5970
5971 (defun gnus-multi-decode-header (start end)
5972   "Apply the functions from `gnus-encoded-word-methods' that match."
5973   (unless (and gnus-decode-header-methods-cache
5974                (eq gnus-newsgroup-name
5975                    (car gnus-decode-header-methods-cache)))
5976     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5977     (mapcar (lambda (x)
5978               (if (symbolp x)
5979                   (nconc gnus-decode-header-methods-cache (list x))
5980                 (if (and gnus-newsgroup-name
5981                          (string-match (car x) gnus-newsgroup-name))
5982                     (nconc gnus-decode-header-methods-cache
5983                            (list (cdr x))))))
5984             gnus-decode-header-methods))
5985   (let ((xlist gnus-decode-header-methods-cache))
5986     (pop xlist)
5987     (save-restriction
5988       (narrow-to-region start end)
5989       (while xlist
5990         (funcall (pop xlist) (point-min) (point-max))))))
5991
5992 ;;;
5993 ;;; Treatment top-level handling.
5994 ;;;
5995
5996 (defun gnus-treat-article (condition &optional part-number total-parts type)
5997   (let ((length (- (point-max) (point-min)))
5998         (alist gnus-treatment-function-alist)
5999         (article-goto-body-goes-to-point-min-p t)
6000         (treated-type
6001          (or (not type)
6002              (catch 'found
6003                (let ((list gnus-article-treat-types))
6004                  (while list
6005                    (when (string-match (pop list) type)
6006                      (throw 'found t)))))))
6007         (highlightp (gnus-visual-p 'article-highlight 'highlight))
6008         (entity (static-unless (featurep 'xemacs)
6009                   (when (eq 'head condition)
6010                     (get-text-property (point-min) 'mime-view-entity))))
6011         val elem buttonized)
6012     (gnus-run-hooks 'gnus-part-display-hook)
6013     (unless gnus-inhibit-treatment
6014       (while (setq elem (pop alist))
6015         (setq val
6016               (save-excursion
6017                 (if (gnus-buffer-live-p gnus-summary-buffer)
6018                     (set-buffer gnus-summary-buffer))
6019                 (symbol-value (car elem))))
6020         (when (and (or (consp val)
6021                        treated-type)
6022                    (gnus-treat-predicate val)
6023                    (or (not (get (car elem) 'highlight))
6024                        highlightp))
6025           (when (and (not buttonized)
6026                      (memq (car elem)
6027                            '(gnus-treat-hide-signature
6028                              gnus-treat-highlight-signature)))
6029             (gnus-article-buttonize-signature)
6030             (setq buttonized t))
6031           (save-restriction
6032             (funcall (cadr elem)))))
6033       ;; FSF Emacsen does not inherit the existing text properties
6034       ;; in the new text, so we should do it for `mime-view-entity'.
6035       (static-unless (featurep 'xemacs)
6036         (when entity
6037           (put-text-property (point-min) (point-max)
6038                              'mime-view-entity entity))))))
6039
6040 ;; Dynamic variables.
6041 (eval-when-compile
6042   (defvar part-number)
6043   (defvar total-parts)
6044   (defvar type)
6045   (defvar condition)
6046   (defvar length))
6047
6048 (defun gnus-treat-predicate (val)
6049   (cond
6050    ((null val)
6051     nil)
6052    ((and (listp val)
6053          (stringp (car val)))
6054     (apply 'gnus-or (mapcar `(lambda (s)
6055                                (string-match s ,(or gnus-newsgroup-name "")))
6056                             val)))
6057    ((listp val)
6058     (let ((pred (pop val)))
6059       (cond
6060        ((eq pred 'or)
6061         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
6062        ((eq pred 'and)
6063         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
6064        ((eq pred 'not)
6065         (not (gnus-treat-predicate (car val))))
6066        ((eq pred 'typep)
6067         (equal (car val) type))
6068        (t
6069         (error "%S is not a valid predicate" pred)))))
6070    ((eq val 'mime)
6071     gnus-show-mime)
6072    (condition
6073     (eq condition val))
6074    ((eq val t)
6075     t)
6076    ((eq val 'head)
6077     nil)
6078    ((eq val 'last)
6079     (eq part-number total-parts))
6080    ((numberp val)
6081     (< length val))
6082    (t
6083     (error "%S is not a valid value" val))))
6084
6085 (defun gnus-article-encrypt-body (protocol &optional n)
6086   "Encrypt the article body."
6087   (interactive
6088    (list
6089     (or gnus-article-encrypt-protocol
6090         (completing-read "Encrypt protocol: "
6091                          gnus-article-encrypt-protocol-alist
6092                          nil t))
6093     current-prefix-arg))
6094   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
6095     (unless func
6096       (error (format "Can't find the encrypt protocol %s" protocol)))
6097     (if (equal gnus-newsgroup-name "nndraft:drafts")
6098         (error "Can't encrypt the article in group nndraft:drafts"))
6099     (if (equal gnus-newsgroup-name "nndraft:queue")
6100         (error "Don't encrypt the article in group nndraft:queue"))
6101     (gnus-summary-iterate n
6102       (save-excursion
6103         (set-buffer gnus-summary-buffer)
6104         (let ((mail-parse-charset gnus-newsgroup-charset)
6105               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6106               (summary-buffer gnus-summary-buffer)
6107               references point)
6108           (gnus-set-global-variables)
6109           (when (gnus-group-read-only-p)
6110             (error "The current newsgroup does not support article encrypt"))
6111           (gnus-summary-show-article t)
6112           (setq references
6113                 (or (mail-header-references gnus-current-headers) ""))
6114           (set-buffer gnus-article-buffer)
6115           (let* ((buffer-read-only nil)
6116                  (headers
6117                   (mapcar (lambda (field)
6118                             (and (save-restriction
6119                                    (message-narrow-to-head)
6120                                    (goto-char (point-min))
6121                                    (search-forward field nil t))
6122                                  (prog2
6123                                      (message-narrow-to-field)
6124                                      (buffer-substring (point-min) (point-max))
6125                                    (delete-region (point-min) (point-max))
6126                                    (widen))))
6127                           '("Content-Type:" "Content-Transfer-Encoding:"
6128                             "Content-Disposition:"))))
6129             (message-narrow-to-head)
6130             (message-remove-header "MIME-Version")
6131             (goto-char (point-max))
6132             (setq point (point))
6133             (insert (apply 'concat headers))
6134             (widen)
6135             (narrow-to-region point (point-max))
6136             (let ((message-options message-options))
6137               (message-options-set 'message-sender user-mail-address)
6138               (message-options-set 'message-recipients user-mail-address)
6139               (message-options-set 'message-sign-encrypt 'not)
6140               (funcall func))
6141             (goto-char (point-min))
6142             (insert "MIME-Version: 1.0\n")
6143             (widen)
6144             (gnus-summary-edit-article-done
6145              references nil summary-buffer t))
6146           (when gnus-keep-backlog
6147             (gnus-backlog-remove-article
6148              (car gnus-article-current) (cdr gnus-article-current)))
6149           (save-excursion
6150             (when (get-buffer gnus-original-article-buffer)
6151               (set-buffer gnus-original-article-buffer)
6152               (setq gnus-original-article nil)))
6153           (when gnus-use-cache
6154             (gnus-cache-update-article
6155              (car gnus-article-current) (cdr gnus-article-current))))))))
6156
6157 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6158   "The following specs can be used:
6159 %t  The security MIME type
6160 %i  Additional info
6161 %d  Details
6162 %D  Details if button is pressed")
6163
6164 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6165   "The following specs can be used:
6166 %t  The security MIME type
6167 %i  Additional info
6168 %d  Details
6169 %D  Details if button is pressed")
6170
6171 (defvar gnus-mime-security-button-line-format-alist
6172   '((?t gnus-tmp-type ?s)
6173     (?i gnus-tmp-info ?s)
6174     (?d gnus-tmp-details ?s)
6175     (?D gnus-tmp-pressed-details ?s)))
6176
6177 (defvar gnus-mime-security-button-map
6178   (let ((map (make-sparse-keymap)))
6179     (unless (>= (string-to-number emacs-version) 21)
6180       (set-keymap-parent map gnus-article-mode-map))
6181     (define-key map gnus-mouse-2 'gnus-article-push-button)
6182     (define-key map "\r" 'gnus-article-press-button)
6183     map))
6184
6185 (defvar gnus-mime-security-details-buffer nil)
6186
6187 (defvar gnus-mime-security-button-pressed nil)
6188
6189 (defvar gnus-mime-security-show-details-inline t
6190   "If non-nil, show details in the article buffer.")
6191
6192 (defun gnus-mime-security-verify-or-decrypt (handle)
6193   (mm-remove-parts (cdr handle))
6194   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6195         point buffer-read-only)
6196     (if region
6197         (goto-char (car region)))
6198     (save-restriction
6199       (narrow-to-region (point) (point))
6200       (with-current-buffer (mm-handle-multipart-original-buffer handle)
6201         (let* ((mm-verify-option 'known)
6202                (mm-decrypt-option 'known)
6203                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6204           (unless (eq nparts (cdr handle))
6205             (mm-destroy-parts (cdr handle))
6206             (setcdr handle nparts))))
6207       (setq point (point))
6208       (gnus-mime-display-security handle)
6209       (goto-char (point-max)))
6210     (when region
6211       (delete-region (point) (cdr region))
6212       (set-marker (car region) nil)
6213       (set-marker (cdr region) nil))
6214     (goto-char point)))
6215
6216 (defun gnus-mime-security-show-details (handle)
6217   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6218     (if details
6219         (if gnus-mime-security-show-details-inline
6220             (let ((gnus-mime-security-button-pressed t)
6221                   (gnus-mime-security-button-line-format
6222                    (get-text-property (point) 'gnus-line-format))
6223                   buffer-read-only)
6224               (forward-char -1)
6225               (while (eq (get-text-property (point) 'gnus-line-format)
6226                          gnus-mime-security-button-line-format)
6227                 (forward-char -1))
6228               (forward-char)
6229               (save-restriction
6230                 (narrow-to-region (point) (point))
6231                 (gnus-insert-mime-security-button handle))
6232               (delete-region (point)
6233                              (or (text-property-not-all
6234                                   (point) (point-max)
6235                                   'gnus-line-format
6236                                   gnus-mime-security-button-line-format)
6237                                  (point-max))))
6238           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6239               (with-current-buffer gnus-mime-security-details-buffer
6240                 (erase-buffer)
6241                 t)
6242             (setq gnus-mime-security-details-buffer
6243                   (gnus-get-buffer-create "*MIME Security Details*")))
6244           (with-current-buffer gnus-mime-security-details-buffer
6245             (insert details)
6246             (goto-char (point-min)))
6247           (pop-to-buffer gnus-mime-security-details-buffer))
6248       (gnus-message 5 "No details."))))
6249
6250 (defun gnus-mime-security-press-button (handle)
6251   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6252       (gnus-mime-security-show-details handle)
6253     (gnus-mime-security-verify-or-decrypt handle)))
6254
6255 (defun gnus-insert-mime-security-button (handle &optional displayed)
6256   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6257          (gnus-tmp-type
6258           (concat
6259            (or (nth 2 (assoc protocol mm-verify-function-alist))
6260                (nth 2 (assoc protocol mm-decrypt-function-alist))
6261                "Unknown")
6262            (if (equal (car handle) "multipart/signed")
6263                " Signed" " Encrypted")
6264            " Part"))
6265          (gnus-tmp-info
6266           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6267               "Undecided"))
6268          (gnus-tmp-details
6269           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6270          gnus-tmp-pressed-details
6271          b e)
6272     (setq gnus-tmp-details
6273           (if gnus-tmp-details
6274               (concat "\n" gnus-tmp-details) ""))
6275     (setq gnus-tmp-pressed-details
6276           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6277     (unless (bolp)
6278       (insert "\n"))
6279     (setq b (point))
6280     (gnus-eval-format
6281      gnus-mime-security-button-line-format
6282      gnus-mime-security-button-line-format-alist
6283      `(keymap ,gnus-mime-security-button-map
6284               ,@(if (>= (string-to-number emacs-version) 21)
6285                     nil ;; XEmacs doesn't care
6286                   (list 'local-map gnus-mime-security-button-map))
6287               gnus-callback gnus-mime-security-press-button
6288               gnus-line-format ,gnus-mime-security-button-line-format
6289               article-type annotation
6290               gnus-data ,handle))
6291     (setq e (point))
6292     (widget-convert-button
6293      'link b e
6294      :mime-handle handle
6295      :action 'gnus-widget-press-button
6296      :button-keymap gnus-mime-security-button-map
6297      :help-echo
6298      (lambda (widget/window &optional overlay pos)
6299        ;; Needed to properly clear the message due to a bug in
6300        ;; wid-edit (XEmacs only).
6301        (if (boundp 'help-echo-owns-message)
6302            (setq help-echo-owns-message t))
6303        (format
6304         "%S: show detail"
6305         (aref gnus-mouse-2 0))))))
6306
6307 (defun gnus-mime-display-security (handle)
6308   (save-restriction
6309     (narrow-to-region (point) (point))
6310     (unless (gnus-unbuttonized-mime-type-p (car handle))
6311       (gnus-insert-mime-security-button handle))
6312     (gnus-mime-display-mixed (cdr handle))
6313     (unless (bolp)
6314       (insert "\n"))
6315     (unless (gnus-unbuttonized-mime-type-p (car handle))
6316       (let ((gnus-mime-security-button-line-format
6317              gnus-mime-security-button-end-line-format))
6318         (gnus-insert-mime-security-button handle)))
6319     (mm-set-handle-multipart-parameter
6320      handle 'gnus-region
6321      (cons (set-marker (make-marker) (point-min))
6322            (set-marker (make-marker) (point-max))))))
6323
6324
6325 ;;; @ for mime-view
6326 ;;;
6327
6328 (defun gnus-article-header-presentation-method (entity situation)
6329   (mime-insert-header entity)
6330   (article-decode-group-name))
6331
6332 (set-alist 'mime-header-presentation-method-alist
6333            'gnus-original-article-mode
6334            #'gnus-article-header-presentation-method)
6335
6336 (defun gnus-mime-preview-quitting-method ()
6337   (mime-preview-kill-buffer)
6338   (delete-other-windows)
6339   (gnus-article-show-summary)
6340   (gnus-summary-select-article gnus-show-all-headers t))
6341
6342 (set-alist 'mime-preview-quitting-method-alist
6343            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6344
6345 (set-alist 'mime-preview-following-method-alist
6346            'gnus-original-article-mode #'gnus-following-method)
6347
6348 (set-alist 'mime-preview-over-to-previous-method-alist
6349            'gnus-original-article-mode
6350            (lambda ()
6351              (if (> (point-min) 1)
6352                  (gnus-article-prev-page)
6353                (gnus-article-read-summary-keys
6354                 nil (gnus-character-to-event ?P)))))
6355
6356 (set-alist 'mime-preview-over-to-next-method-alist
6357            'gnus-original-article-mode'
6358            (lambda ()
6359              (if (< (point-max) (buffer-size))
6360                  (gnus-article-next-page)
6361                (gnus-article-read-summary-keys
6362                 nil (gnus-character-to-event ?N)))))
6363
6364
6365 ;;; @ end
6366 ;;;
6367
6368 (gnus-ems-redefine)
6369
6370 (provide 'gnus-art)
6371
6372 (run-hooks 'gnus-art-load-hook)
6373
6374 ;;; gnus-art.el ends here