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