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