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