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