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