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