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