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