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