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