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