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