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