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