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