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