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