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