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