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