(gnus-article-prepare): Don't bind coding systems.
[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 (defun article-strip-all-blank-lines ()
1056   "Strip all blank lines."
1057   (interactive)
1058   (save-excursion
1059     (let ((inhibit-point-motion-hooks t)
1060           buffer-read-only)
1061       (goto-char (point-min))
1062       (search-forward "\n\n" nil t)
1063       (while (re-search-forward "^[ \t]*\n" nil t)
1064         (replace-match "" t t)))))
1065
1066 (defvar mime::preview/content-list)
1067 (defvar mime::preview-content-info/point-min)
1068 (defun gnus-article-narrow-to-signature ()
1069   "Narrow to the signature; return t if a signature is found, else nil."
1070   (widen)
1071   (when (and (boundp 'mime::preview/content-list)
1072              mime::preview/content-list)
1073     ;; We have a MIMEish article, so we use the MIME data to narrow.
1074     (let ((pcinfo (car (last mime::preview/content-list))))
1075       (ignore-errors
1076         (narrow-to-region
1077          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
1078          (point-max)))))
1079
1080   (when (gnus-article-search-signature)
1081     (forward-line 1)
1082     ;; Check whether we have some limits to what we consider
1083     ;; to be a signature.
1084     (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1085                     (list gnus-signature-limit)))
1086           limit limited)
1087       (while (setq limit (pop limits))
1088         (if (or (and (integerp limit)
1089                      (< (- (point-max) (point)) limit))
1090                 (and (floatp limit)
1091                      (< (count-lines (point) (point-max)) limit))
1092                 (and (gnus-functionp limit)
1093                      (funcall limit))
1094                 (and (stringp limit)
1095                      (not (re-search-forward limit nil t))))
1096             ()                          ; This limit did not succeed.
1097           (setq limited t
1098                 limits nil)))
1099       (unless limited
1100         (narrow-to-region (point) (point-max))
1101         t))))
1102
1103 (defun gnus-article-search-signature ()
1104   "Search the current buffer for the signature separator.
1105 Put point at the beginning of the signature separator."
1106   (let ((cur (point)))
1107     (goto-char (point-max))
1108     (if (if (stringp gnus-signature-separator)
1109             (re-search-backward gnus-signature-separator nil t)
1110           (let ((seps gnus-signature-separator))
1111             (while (and seps
1112                         (not (re-search-backward (car seps) nil t)))
1113               (pop seps))
1114             seps))
1115         t
1116       (goto-char cur)
1117       nil)))
1118
1119 (eval-and-compile
1120   (autoload 'w3-display "w3-parse")
1121   (autoload 'w3-do-setup "w3" "" t)
1122   (autoload 'w3-region "w3-display" "" t))
1123
1124 (defun gnus-article-treat-html ()
1125   "Render HTML."
1126   (interactive)
1127   (let ((cbuf (current-buffer)))
1128     (set-buffer gnus-article-buffer)
1129     (let (buf buffer-read-only b e)
1130       (w3-do-setup)
1131       (goto-char (point-min))
1132       (narrow-to-region
1133        (if (search-forward "\n\n" nil t)
1134            (setq b (point))
1135          (point-max))
1136        (setq e (point-max)))
1137       (nnheader-temp-write nil
1138         (insert-buffer-substring gnus-article-buffer b e)
1139         (require 'url)
1140         (save-window-excursion
1141           (w3-region (point-min) (point-max))
1142           (setq buf (buffer-substring-no-properties (point-min) (point-max)))))
1143       (when buf
1144         (delete-region (point-min) (point-max))
1145         (insert buf))
1146       (widen)
1147       (goto-char (point-min))
1148       (set-window-start (get-buffer-window (current-buffer)) (point-min))
1149       (set-buffer cbuf))))
1150
1151 (defun gnus-article-hidden-arg ()
1152   "Return the current prefix arg as a number, or 0 if no prefix."
1153   (list (if current-prefix-arg
1154             (prefix-numeric-value current-prefix-arg)
1155           0)))
1156
1157 (defun gnus-article-check-hidden-text (type arg)
1158   "Return nil if hiding is necessary.
1159 Arg can be nil or a number.  Nil and positive means hide, negative
1160 means show, 0 means toggle."
1161   (save-excursion
1162     (save-restriction
1163       (widen)
1164       (let ((hide (gnus-article-hidden-text-p type)))
1165         (cond
1166          ((or (null arg)
1167               (> arg 0))
1168           nil)
1169          ((< arg 0)
1170           (gnus-article-show-hidden-text type))
1171          (t
1172           (if (eq hide 'hidden)
1173               (gnus-article-show-hidden-text type)
1174             nil)))))))
1175
1176 (defun gnus-article-hidden-text-p (type)
1177   "Say whether the current buffer contains hidden text of type TYPE."
1178   (let ((start (point-min))
1179         (pos (text-property-any (point-min) (point-max) 'article-type type)))
1180     (while (and pos
1181                 (not (get-text-property pos 'invisible)))
1182       (setq pos
1183             (text-property-any (1+ pos) (point-max) 'article-type type)))
1184     (if pos
1185         'hidden
1186       'shown)))
1187
1188 (defun gnus-article-show-hidden-text (type &optional hide)
1189   "Show all hidden text of type TYPE.
1190 If HIDE, hide the text instead."
1191   (save-excursion
1192     (let ((buffer-read-only nil)
1193           (inhibit-point-motion-hooks t)
1194           (end (point-min))
1195           beg)
1196       (while (setq beg (text-property-any end (point-max) 'article-type type))
1197         (goto-char beg)
1198         (setq end (or
1199                    (text-property-not-all beg (point-max) 'article-type type)
1200                    (point-max)))
1201         (if hide
1202             (gnus-article-hide-text beg end gnus-hidden-properties)
1203           (gnus-article-unhide-text beg end))
1204         (goto-char end))
1205       t)))
1206
1207 (defconst article-time-units
1208   `((year . ,(* 365.25 24 60 60))
1209     (week . ,(* 7 24 60 60))
1210     (day . ,(* 24 60 60))
1211     (hour . ,(* 60 60))
1212     (minute . 60)
1213     (second . 1))
1214   "Mapping from time units to seconds.")
1215
1216 (defun article-date-ut (&optional type highlight header)
1217   "Convert DATE date to universal time in the current article.
1218 If TYPE is `local', convert to local time; if it is `lapsed', output
1219 how much time has lapsed since DATE."
1220   (interactive (list 'ut t))
1221   (let* ((header (or header
1222                      (mail-header-date gnus-current-headers)
1223                      (message-fetch-field "date")
1224                      ""))
1225          (date (if (vectorp header) (mail-header-date header)
1226                  header))
1227          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1228          (inhibit-point-motion-hooks t)
1229          bface eface newline)
1230     (when (and date (not (string= date "")))
1231       (save-excursion
1232         (save-restriction
1233           (nnheader-narrow-to-headers)
1234           (let ((buffer-read-only nil))
1235             ;; Delete any old Date headers.
1236             (if (re-search-forward date-regexp nil t)
1237                 (progn
1238                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1239                         eface (get-text-property (1- (gnus-point-at-eol))
1240                                                  'face))
1241                   (delete-region (progn (beginning-of-line) (point))
1242                                  (progn (end-of-line) (point)))
1243                   (beginning-of-line))
1244               (goto-char (point-max))
1245               (setq newline t))
1246             (insert (article-make-date-line date type))
1247             ;; Do highlighting.
1248             (beginning-of-line)
1249             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1250               (put-text-property (match-beginning 1) (1+ (match-end 1))
1251                                  'face bface)
1252               (put-text-property (match-beginning 2) (match-end 2)
1253                                  'face eface))
1254             (when newline
1255               (end-of-line)
1256               (insert "\n"))))))))
1257
1258 (defun article-make-date-line (date type)
1259   "Return a DATE line of TYPE."
1260   (cond
1261    ;; Convert to the local timezone.  We have to slap a
1262    ;; `condition-case' round the calls to the timezone
1263    ;; functions since they aren't particularly resistant to
1264    ;; buggy dates.
1265    ((eq type 'local)
1266     (concat "Date: " (condition-case ()
1267                          (timezone-make-date-arpa-standard date)
1268                        (error date))))
1269    ;; Convert to Universal Time.
1270    ((eq type 'ut)
1271     (concat "Date: "
1272             (condition-case ()
1273                 (timezone-make-date-arpa-standard date nil "UT")
1274               (error date))))
1275    ;; Get the original date from the article.
1276    ((eq type 'original)
1277     (concat "Date: " date))
1278    ;; Let the user define the format.
1279    ((eq type 'user)
1280     (concat
1281      "Date: "
1282      (format-time-string gnus-article-time-format
1283                          (ignore-errors
1284                            (gnus-encode-date
1285                             (timezone-make-date-arpa-standard
1286                              date nil "UT"))))))
1287    ;; Do an X-Sent lapsed format.
1288    ((eq type 'lapsed)
1289     ;; If the date is seriously mangled, the timezone functions are
1290     ;; liable to bug out, so we ignore all errors.
1291     (let* ((now (current-time))
1292            (real-time
1293             (ignore-errors
1294               (gnus-time-minus
1295                (gnus-encode-date
1296                 (timezone-make-date-arpa-standard
1297                  (current-time-string now)
1298                  (current-time-zone now) "UT"))
1299                (gnus-encode-date
1300                 (timezone-make-date-arpa-standard
1301                  date nil "UT")))))
1302            (real-sec (and real-time
1303                           (+ (* (float (car real-time)) 65536)
1304                              (cadr real-time))))
1305            (sec (and real-time (abs real-sec)))
1306            num prev)
1307       (cond
1308        ((null real-time)
1309         "X-Sent: Unknown")
1310        ((zerop sec)
1311         "X-Sent: Now")
1312        (t
1313         (concat
1314          "X-Sent: "
1315          ;; This is a bit convoluted, but basically we go
1316          ;; through the time units for years, weeks, etc,
1317          ;; and divide things to see whether that results
1318          ;; in positive answers.
1319          (mapconcat
1320           (lambda (unit)
1321             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1322                 ;; The (remaining) seconds are too few to
1323                 ;; be divided into this time unit.
1324                 ""
1325               ;; It's big enough, so we output it.
1326               (setq sec (- sec (* num (cdr unit))))
1327               (prog1
1328                   (concat (if prev ", " "") (int-to-string
1329                                              (floor num))
1330                           " " (symbol-name (car unit))
1331                           (if (> num 1) "s" ""))
1332                 (setq prev t))))
1333           article-time-units "")
1334          ;; If dates are odd, then it might appear like the
1335          ;; article was sent in the future.
1336          (if (> real-sec 0)
1337              " ago"
1338            " in the future"))))))
1339    (t
1340     (error "Unknown conversion type: %s" type))))
1341
1342 (defun article-date-local (&optional highlight)
1343   "Convert the current article date to the local timezone."
1344   (interactive (list t))
1345   (article-date-ut 'local highlight))
1346
1347 (defun article-date-original (&optional highlight)
1348   "Convert the current article date to what it was originally.
1349 This is only useful if you have used some other date conversion
1350 function and want to see what the date was before converting."
1351   (interactive (list t))
1352   (article-date-ut 'original highlight))
1353
1354 (defun article-date-lapsed (&optional highlight)
1355   "Convert the current article date to time lapsed since it was sent."
1356   (interactive (list t))
1357   (article-date-ut 'lapsed highlight))
1358
1359 (defun article-update-date-lapsed ()
1360   "Function to be run from a timer to update the lapsed time line."
1361   (save-excursion
1362     (ignore-errors
1363       (when (gnus-buffer-live-p gnus-article-buffer)
1364         (set-buffer gnus-article-buffer)
1365         (goto-char (point-min))
1366         (when (re-search-forward "^X-Sent:" nil t)
1367           (article-date-lapsed t))))))
1368
1369 (defun gnus-start-date-timer (&optional n)
1370   "Start a timer to update the X-Sent header in the article buffers.
1371 The numerical prefix says how frequently (in seconds) the function
1372 is to run."
1373   (interactive "p")
1374   (unless n
1375     (setq n 1))
1376   (gnus-stop-date-timer)
1377   (setq article-lapsed-timer 
1378         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1379
1380 (defun gnus-stop-date-timer ()
1381   "Stop the X-Sent timer."
1382   (interactive)
1383   (when article-lapsed-timer
1384     (nnheader-cancel-timer article-lapsed-timer)
1385     (setq article-lapsed-timer nil)))
1386
1387 (defun article-date-user (&optional highlight)
1388   "Convert the current article date to the user-defined format.
1389 This format is defined by the `gnus-article-time-format' variable."
1390   (interactive (list t))
1391   (article-date-ut 'user highlight))
1392
1393 (defun article-show-all ()
1394   "Show all hidden text in the article buffer."
1395   (interactive)
1396   (save-excursion
1397     (let ((buffer-read-only nil))
1398       (gnus-article-unhide-text (point-min) (point-max)))))
1399
1400 (defun article-emphasize (&optional arg)
1401   "Emphasize text according to `gnus-emphasis-alist'."
1402   (interactive (gnus-article-hidden-arg))
1403   (unless (gnus-article-check-hidden-text 'emphasis arg)
1404     (save-excursion
1405       (let ((alist gnus-emphasis-alist)
1406             (buffer-read-only nil)
1407             (props (append '(article-type emphasis)
1408                            gnus-hidden-properties))
1409             regexp elem beg invisible visible face)
1410         (goto-char (point-min))
1411         (search-forward "\n\n" nil t)
1412         (setq beg (point))
1413         (while (setq elem (pop alist))
1414           (goto-char beg)
1415           (setq regexp (car elem)
1416                 invisible (nth 1 elem)
1417                 visible (nth 2 elem)
1418                 face (nth 3 elem))
1419           (while (re-search-forward regexp nil t)
1420             (when (and (match-beginning visible) (match-beginning invisible))
1421               (gnus-article-hide-text
1422                (match-beginning invisible) (match-end invisible) props)
1423               (gnus-article-unhide-text-type
1424                (match-beginning visible) (match-end visible) 'emphasis)
1425               (gnus-put-text-property-excluding-newlines
1426                (match-beginning visible) (match-end visible) 'face face)
1427               (goto-char (match-end invisible)))))))))
1428
1429 (defvar gnus-summary-article-menu)
1430 (defvar gnus-summary-post-menu)
1431
1432 ;;; Saving functions.
1433
1434 (defun gnus-article-save (save-buffer file &optional num)
1435   "Save the currently selected article."
1436   (unless gnus-save-all-headers
1437     ;; Remove headers according to `gnus-saved-headers'.
1438     (let ((gnus-visible-headers
1439            (or gnus-saved-headers gnus-visible-headers))
1440           (gnus-article-buffer save-buffer))
1441       (save-excursion
1442         (set-buffer save-buffer)
1443         (article-hide-headers 1 t))))
1444   (save-window-excursion
1445     (if (not gnus-default-article-saver)
1446         (error "No default saver is defined")
1447       ;; !!! Magic!  The saving functions all save
1448       ;; `gnus-original-article-buffer' (or so they think), but we
1449       ;; bind that variable to our save-buffer.
1450       (set-buffer gnus-article-buffer)
1451       (let* ((gnus-save-article-buffer save-buffer)
1452              (filename
1453               (cond
1454                ((not gnus-prompt-before-saving) 'default)
1455                ((eq gnus-prompt-before-saving 'always) nil)
1456                (t file)))
1457              (gnus-number-of-articles-to-be-saved
1458               (when (eq gnus-prompt-before-saving t)
1459                 num)))                  ; Magic
1460         (set-buffer gnus-summary-buffer)
1461         (funcall gnus-default-article-saver filename)))))
1462
1463 (defun gnus-read-save-file-name (prompt &optional filename
1464                                         function group headers variable)
1465   (let ((default-name
1466           (funcall function group headers (symbol-value variable)))
1467         result)
1468     (setq
1469      result
1470      (cond
1471       ((eq filename 'default)
1472        default-name)
1473       ((eq filename t)
1474        default-name)
1475       (filename filename)
1476       (t
1477        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1478               (prompt
1479                (format prompt
1480                        (if (and gnus-number-of-articles-to-be-saved
1481                                 (> gnus-number-of-articles-to-be-saved 1))
1482                            (format "these %d articles"
1483                                    gnus-number-of-articles-to-be-saved)
1484                          "this article")))
1485               (file
1486                ;; Let the split methods have their say.
1487                (cond
1488                 ;; No split name was found.
1489                 ((null split-name)
1490                  (read-file-name
1491                   (concat prompt " (default "
1492                           (file-name-nondirectory default-name) ") ")
1493                   (file-name-directory default-name)
1494                   default-name))
1495                 ;; A single group name is returned.
1496                 ((stringp split-name)
1497                  (setq default-name
1498                        (funcall function split-name headers
1499                                 (symbol-value variable)))
1500                  (read-file-name
1501                   (concat prompt " (default "
1502                           (file-name-nondirectory default-name) ") ")
1503                   (file-name-directory default-name)
1504                   default-name))
1505                 ;; A single split name was found
1506                 ((= 1 (length split-name))
1507                  (let* ((name (expand-file-name
1508                                (car split-name) gnus-article-save-directory))
1509                         (dir (cond ((file-directory-p name)
1510                                     (file-name-as-directory name))
1511                                    ((file-exists-p name) name)
1512                                    (t gnus-article-save-directory))))
1513                    (read-file-name
1514                     (concat prompt " (default " name ") ")
1515                     dir name)))
1516                 ;; A list of splits was found.
1517                 (t
1518                  (setq split-name (nreverse split-name))
1519                  (let (result)
1520                    (let ((file-name-history
1521                           (nconc split-name file-name-history)))
1522                      (setq result
1523                            (expand-file-name
1524                             (read-file-name
1525                              (concat prompt " (`M-p' for defaults) ")
1526                              gnus-article-save-directory
1527                              (car split-name))
1528                             gnus-article-save-directory)))
1529                    (car (push result file-name-history)))))))
1530          ;; Create the directory.
1531          (gnus-make-directory (file-name-directory file))
1532          ;; If we have read a directory, we append the default file name.
1533          (when (file-directory-p file)
1534            (setq file (concat (file-name-as-directory file)
1535                               (file-name-nondirectory default-name))))
1536          ;; Possibly translate some characters.
1537          (nnheader-translate-file-chars file)))))
1538     (gnus-make-directory (file-name-directory result))
1539     (set variable result)))
1540
1541 (defun gnus-article-archive-name (group)
1542   "Return the first instance of an \"Archive-name\" in the current buffer."
1543   (let ((case-fold-search t))
1544     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1545       (nnheader-concat gnus-article-save-directory
1546                        (match-string 1)))))
1547
1548 (defun gnus-article-nndoc-name (group)
1549   "If GROUP is an nndoc group, return the name of the parent group."
1550   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1551     (gnus-group-get-parameter group 'save-article-group)))
1552
1553 (defun gnus-summary-save-in-rmail (&optional filename)
1554   "Append this article to Rmail file.
1555 Optional argument FILENAME specifies file name.
1556 Directory to save to is default to `gnus-article-save-directory'."
1557   (interactive)
1558   (setq filename (gnus-read-save-file-name
1559                   "Save %s in rmail file:" filename
1560                   gnus-rmail-save-name gnus-newsgroup-name
1561                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1562   (gnus-eval-in-buffer-window gnus-save-article-buffer
1563     (save-excursion
1564       (save-restriction
1565         (widen)
1566         (gnus-output-to-rmail filename))))
1567   filename)
1568
1569 (defun gnus-summary-save-in-mail (&optional filename)
1570   "Append this article to Unix mail file.
1571 Optional argument FILENAME specifies file name.
1572 Directory to save to is default to `gnus-article-save-directory'."
1573   (interactive)
1574   (setq filename (gnus-read-save-file-name
1575                   "Save %s in Unix mail file:" filename
1576                   gnus-mail-save-name gnus-newsgroup-name
1577                   gnus-current-headers 'gnus-newsgroup-last-mail))
1578   (gnus-eval-in-buffer-window gnus-save-article-buffer
1579     (save-excursion
1580       (save-restriction
1581         (widen)
1582         (if (and (file-readable-p filename)
1583                  (mail-file-babyl-p filename))
1584             (gnus-output-to-rmail filename t)
1585           (gnus-output-to-mail filename)))))
1586   filename)
1587
1588 (defun gnus-summary-save-in-file (&optional filename overwrite)
1589   "Append this article to file.
1590 Optional argument FILENAME specifies file name.
1591 Directory to save to is default to `gnus-article-save-directory'."
1592   (interactive)
1593   (setq filename (gnus-read-save-file-name
1594                   "Save %s in file:" filename
1595                   gnus-file-save-name gnus-newsgroup-name
1596                   gnus-current-headers 'gnus-newsgroup-last-file))
1597   (gnus-eval-in-buffer-window gnus-save-article-buffer
1598     (save-excursion
1599       (save-restriction
1600         (widen)
1601         (when (and overwrite
1602                    (file-exists-p filename))
1603           (delete-file filename))
1604         (gnus-output-to-file filename))))
1605   filename)
1606
1607 (defun gnus-summary-write-to-file (&optional filename)
1608   "Write this article to a file.
1609 Optional argument FILENAME specifies file name.
1610 The directory to save in defaults to `gnus-article-save-directory'."
1611   (interactive)
1612   (gnus-summary-save-in-file nil t))
1613
1614 (defun gnus-summary-save-body-in-file (&optional filename)
1615   "Append this article body to a file.
1616 Optional argument FILENAME specifies file name.
1617 The directory to save in defaults to `gnus-article-save-directory'."
1618   (interactive)
1619   (setq filename (gnus-read-save-file-name
1620                   "Save %s body in file:" filename
1621                   gnus-file-save-name gnus-newsgroup-name
1622                   gnus-current-headers 'gnus-newsgroup-last-file))
1623   (gnus-eval-in-buffer-window gnus-save-article-buffer
1624     (save-excursion
1625       (save-restriction
1626         (widen)
1627         (goto-char (point-min))
1628         (when (search-forward "\n\n" nil t)
1629           (narrow-to-region (point) (point-max)))
1630         (gnus-output-to-file filename))))
1631   filename)
1632
1633 (defun gnus-summary-save-in-pipe (&optional command)
1634   "Pipe this article to subprocess."
1635   (interactive)
1636   (setq command
1637         (cond ((eq command 'default)
1638                gnus-last-shell-command)
1639               (command command)
1640               (t (read-string
1641                   (format
1642                    "Shell command on %s: "
1643                    (if (and gnus-number-of-articles-to-be-saved
1644                             (> gnus-number-of-articles-to-be-saved 1))
1645                        (format "these %d articles"
1646                                gnus-number-of-articles-to-be-saved)
1647                      "this article"))
1648                   gnus-last-shell-command))))
1649   (when (string-equal command "")
1650     (setq command gnus-last-shell-command))
1651   (gnus-eval-in-buffer-window gnus-article-buffer
1652     (save-restriction
1653       (widen)
1654       (shell-command-on-region (point-min) (point-max) command nil)))
1655   (setq gnus-last-shell-command command))
1656
1657 ;;; Article file names when saving.
1658
1659 (defun gnus-capitalize-newsgroup (newsgroup)
1660   "Capitalize NEWSGROUP name."
1661   (when (not (zerop (length newsgroup)))
1662     (concat (char-to-string (upcase (aref newsgroup 0)))
1663             (substring newsgroup 1))))
1664
1665 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1666   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1667 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1668 Otherwise, it is like ~/News/news/group/num."
1669   (let ((default
1670           (expand-file-name
1671            (concat (if (gnus-use-long-file-name 'not-save)
1672                        (gnus-capitalize-newsgroup newsgroup)
1673                      (gnus-newsgroup-directory-form newsgroup))
1674                    "/" (int-to-string (mail-header-number headers)))
1675            gnus-article-save-directory)))
1676     (if (and last-file
1677              (string-equal (file-name-directory default)
1678                            (file-name-directory last-file))
1679              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1680         default
1681       (or last-file default))))
1682
1683 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1684   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1685 If variable `gnus-use-long-file-name' is non-nil, it is
1686 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1687   (let ((default
1688           (expand-file-name
1689            (concat (if (gnus-use-long-file-name 'not-save)
1690                        newsgroup
1691                      (gnus-newsgroup-directory-form newsgroup))
1692                    "/" (int-to-string (mail-header-number headers)))
1693            gnus-article-save-directory)))
1694     (if (and last-file
1695              (string-equal (file-name-directory default)
1696                            (file-name-directory last-file))
1697              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1698         default
1699       (or last-file default))))
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            (gnus-capitalize-newsgroup newsgroup)
1709          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1710        gnus-article-save-directory)))
1711
1712 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1713   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1714 If variable `gnus-use-long-file-name' is non-nil, it is
1715 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1716   (or last-file
1717       (expand-file-name
1718        (if (gnus-use-long-file-name 'not-save)
1719            newsgroup
1720          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1721        gnus-article-save-directory)))
1722
1723 (eval-and-compile
1724   (mapcar
1725    (lambda (func)
1726      (let (afunc gfunc)
1727        (if (consp func)
1728            (setq afunc (car func)
1729                  gfunc (cdr func))
1730          (setq afunc func
1731                gfunc (intern (format "gnus-%s" func))))
1732        (fset gfunc
1733              (if (not (fboundp afunc))
1734                  nil
1735                `(lambda (&optional interactive &rest args)
1736                   ,(documentation afunc t)
1737                   (interactive (list t))
1738                   (save-excursion
1739                     (set-buffer gnus-article-buffer)
1740                     (if interactive
1741                         (call-interactively ',afunc)
1742                       (apply ',afunc args))))))))
1743    '(article-hide-headers
1744      article-hide-boring-headers
1745      article-treat-overstrike
1746      (article-fill . gnus-article-word-wrap)
1747      article-remove-cr
1748      article-display-x-face
1749      article-de-quoted-unreadable
1750      article-mime-decode-quoted-printable
1751      article-hide-pgp
1752      article-hide-pem
1753      article-hide-signature
1754      article-remove-trailing-blank-lines
1755      article-strip-leading-blank-lines
1756      article-strip-multiple-blank-lines
1757      article-strip-leading-space
1758      article-strip-blank-lines
1759      article-strip-all-blank-lines
1760      article-date-local
1761      article-date-original
1762      article-date-ut
1763      article-date-user
1764      article-date-lapsed
1765      article-emphasize
1766      article-treat-dumbquotes
1767      (article-show-all . gnus-article-show-all-headers))))
1768 \f
1769 ;;;
1770 ;;; Gnus article mode
1771 ;;;
1772
1773 (put 'gnus-article-mode 'mode-class 'special)
1774
1775 (gnus-define-keys gnus-article-mode-map
1776   " " gnus-article-goto-next-page
1777   "\177" gnus-article-goto-prev-page
1778   [delete] gnus-article-goto-prev-page
1779   "\C-c^" gnus-article-refer-article
1780   "h" gnus-article-show-summary
1781   "s" gnus-article-show-summary
1782   "\C-c\C-m" gnus-article-mail
1783   "?" gnus-article-describe-briefly
1784   gnus-mouse-2 gnus-article-push-button
1785   "\r" gnus-article-press-button
1786   "\t" gnus-article-next-button
1787   "\M-\t" gnus-article-prev-button
1788   "e" gnus-article-edit
1789   "<" beginning-of-buffer
1790   ">" end-of-buffer
1791   "\C-c\C-i" gnus-info-find-node
1792   "\C-c\C-b" gnus-bug
1793
1794   "\C-d" gnus-article-read-summary-keys
1795   "\M-*" gnus-article-read-summary-keys
1796   "\M-#" gnus-article-read-summary-keys
1797   "\M-^" gnus-article-read-summary-keys
1798   "\M-g" gnus-article-read-summary-keys)
1799
1800 (substitute-key-definition
1801  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
1802
1803 (defun gnus-article-make-menu-bar ()
1804   (gnus-turn-off-edit-menu 'article)
1805   (unless (boundp 'gnus-article-article-menu)
1806     (easy-menu-define
1807      gnus-article-article-menu gnus-article-mode-map ""
1808      '("Article"
1809        ["Scroll forwards" gnus-article-goto-next-page t]
1810        ["Scroll backwards" gnus-article-goto-prev-page t]
1811        ["Show summary" gnus-article-show-summary t]
1812        ["Fetch Message-ID at point" gnus-article-refer-article t]
1813        ["Mail to address at point" gnus-article-mail t]))
1814
1815     (easy-menu-define
1816      gnus-article-treatment-menu gnus-article-mode-map ""
1817      '("Treatment"
1818        ["Hide headers" gnus-article-hide-headers t]
1819        ["Hide signature" gnus-article-hide-signature t]
1820        ["Hide citation" gnus-article-hide-citation t]
1821        ["Treat overstrike" gnus-article-treat-overstrike t]
1822        ["Remove carriage return" gnus-article-remove-cr t]
1823        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1824
1825     (when nil
1826       (when (boundp 'gnus-summary-article-menu)
1827         (define-key gnus-article-mode-map [menu-bar commands]
1828           (cons "Commands" gnus-summary-article-menu))))
1829
1830     (when (boundp 'gnus-summary-post-menu)
1831       (define-key gnus-article-mode-map [menu-bar post]
1832         (cons "Post" gnus-summary-post-menu)))
1833
1834     (run-hooks 'gnus-article-menu-hook)))
1835
1836 (defun gnus-article-mode ()
1837   "Major mode for displaying an article.
1838
1839 All normal editing commands are switched off.
1840
1841 The following commands are available in addition to all summary mode
1842 commands:
1843 \\<gnus-article-mode-map>
1844 \\[gnus-article-next-page]\t Scroll the article one page forwards
1845 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1846 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1847 \\[gnus-article-show-summary]\t Display the summary buffer
1848 \\[gnus-article-mail]\t Send a reply to the address near point
1849 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1850 \\[gnus-info-find-node]\t Go to the Gnus info node"
1851   (interactive)
1852   (when (gnus-visual-p 'article-menu 'menu)
1853     (gnus-article-make-menu-bar))
1854   (kill-all-local-variables)
1855   (gnus-simplify-mode-line)
1856   (setq mode-name "Article")
1857   (setq major-mode 'gnus-article-mode)
1858   (make-local-variable 'minor-mode-alist)
1859   (unless (assq 'gnus-show-mime minor-mode-alist)
1860     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1861   (use-local-map gnus-article-mode-map)
1862   (gnus-update-format-specifications nil 'article-mode)
1863   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1864   (set (make-local-variable 'gnus-page-broken) nil)
1865   (set (make-local-variable 'gnus-button-marker-list) nil)
1866   (gnus-set-default-directory)
1867   (buffer-disable-undo (current-buffer))
1868   (setq buffer-read-only t)
1869   (set-syntax-table gnus-article-mode-syntax-table)
1870   (run-hooks 'gnus-article-mode-hook))
1871
1872 (defun gnus-article-setup-buffer ()
1873   "Initialize the article buffer."
1874   (let* ((name (if gnus-single-article-buffer "*Article*"
1875                  (concat "*Article " gnus-newsgroup-name "*")))
1876          (original
1877           (progn (string-match "\\*Article" name)
1878                  (concat " *Original Article"
1879                          (substring name (match-end 0))))))
1880     (setq gnus-article-buffer name)
1881     (setq gnus-original-article-buffer original)
1882     ;; This might be a variable local to the summary buffer.
1883     (unless gnus-single-article-buffer
1884       (save-excursion
1885         (set-buffer gnus-summary-buffer)
1886         (setq gnus-article-buffer name)
1887         (setq gnus-original-article-buffer original)
1888         (gnus-set-global-variables)))
1889     ;; Init original article buffer.
1890     (save-excursion
1891       (set-buffer (get-buffer-create gnus-original-article-buffer))
1892       (buffer-disable-undo (current-buffer))
1893       (setq major-mode 'gnus-original-article-mode)
1894       (gnus-add-current-to-buffer-list)
1895       (make-local-variable 'gnus-original-article))
1896     (if (get-buffer name)
1897         (save-excursion
1898           (set-buffer name)
1899           (buffer-disable-undo (current-buffer))
1900           (setq buffer-read-only t)
1901           (gnus-add-current-to-buffer-list)
1902           (unless (eq major-mode 'gnus-article-mode)
1903             (gnus-article-mode))
1904           (current-buffer))
1905       (save-excursion
1906         (set-buffer (get-buffer-create name))
1907         (gnus-add-current-to-buffer-list)
1908         (gnus-article-mode)
1909         (make-local-variable 'gnus-summary-buffer)
1910         (current-buffer)))))
1911
1912 ;; Set article window start at LINE, where LINE is the number of lines
1913 ;; from the head of the article.
1914 (defun gnus-article-set-window-start (&optional line)
1915   (set-window-start
1916    (get-buffer-window gnus-article-buffer t)
1917    (save-excursion
1918      (set-buffer gnus-article-buffer)
1919      (goto-char (point-min))
1920      (if (not line)
1921          (point-min)
1922        (gnus-message 6 "Moved to bookmark")
1923        (search-forward "\n\n" nil t)
1924        (forward-line line)
1925        (point)))))
1926
1927 ;;; @@ article filters
1928 ;;;
1929 (defun gnus-article-preview-mime-message ()
1930   (make-local-variable 'mime-button-mother-dispatcher)
1931   (setq mime-button-mother-dispatcher
1932         (function gnus-article-push-button))
1933   (let ((default-mime-charset
1934           (save-excursion
1935             (set-buffer gnus-summary-buffer)
1936             default-mime-charset))
1937         )
1938     (save-excursion
1939       (mime-view-mode nil nil nil gnus-original-article-buffer
1940                       gnus-article-buffer
1941                       gnus-article-mode-map)
1942       ))
1943   (run-hooks 'gnus-mime-article-prepare-hook)
1944   )
1945
1946 (defun gnus-article-decode-encoded-word ()
1947   "Header filter for gnus-article-mode.
1948 It is registered to variable `mime-view-content-header-filter-alist'."
1949   (goto-char (point-min))
1950   (let ((charset (save-excursion
1951                    (set-buffer gnus-summary-buffer)
1952                    default-mime-charset)))
1953     (save-restriction
1954       (std11-narrow-to-header)
1955       (goto-char (point-min))
1956       (while (re-search-forward "^[^ \t:]+:" nil t)
1957         (let ((start (match-beginning 0))
1958               (end (std11-field-end))
1959               )
1960           (save-restriction
1961             (narrow-to-region start end)
1962             (decode-mime-charset-region start end charset)
1963             (goto-char (point-max))
1964             )))
1965       (eword-decode-header)
1966       )
1967     (decode-mime-charset-region (point) (point-max) charset)
1968     (mime-maybe-hide-echo-buffer)
1969     )
1970   (run-hooks 'gnus-mime-article-prepare-hook)
1971   )
1972
1973 (defun gnus-article-prepare (article &optional all-headers header)
1974   "Prepare ARTICLE in article mode buffer.
1975 ARTICLE should either be an article number or a Message-ID.
1976 If ARTICLE is an id, HEADER should be the article headers.
1977 If ALL-HEADERS is non-nil, no headers are hidden."
1978   (save-excursion
1979     ;; Make sure we start in a summary buffer.
1980     (unless (eq major-mode 'gnus-summary-mode)
1981       (set-buffer gnus-summary-buffer))
1982     (setq gnus-summary-buffer (current-buffer))
1983     ;; Make sure the connection to the server is alive.
1984     (unless (gnus-server-opened
1985              (gnus-find-method-for-group gnus-newsgroup-name))
1986       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
1987       (gnus-request-group gnus-newsgroup-name t))
1988     (let* ((gnus-article (if header (mail-header-number header) article))
1989            (summary-buffer (current-buffer))
1990            (internal-hook gnus-article-internal-prepare-hook)
1991            (group gnus-newsgroup-name)
1992            result)
1993       (save-excursion
1994         (gnus-article-setup-buffer)
1995         (set-buffer gnus-article-buffer)
1996         ;; Deactivate active regions.
1997         (when (and (boundp 'transient-mark-mode)
1998                    transient-mark-mode)
1999           (setq mark-active nil))
2000         (if (not (setq result (let ((buffer-read-only nil))
2001                                 (gnus-request-article-this-buffer
2002                                  article group))))
2003             ;; There is no such article.
2004             (save-excursion
2005               (when (and (numberp article)
2006                          (not (memq article gnus-newsgroup-sparse)))
2007                 (setq gnus-article-current
2008                       (cons gnus-newsgroup-name article))
2009                 (set-buffer gnus-summary-buffer)
2010                 (setq gnus-current-article article)
2011                 (gnus-summary-mark-article article gnus-canceled-mark))
2012               (unless (memq article gnus-newsgroup-sparse)
2013                 (gnus-error
2014                  1 "No such article (may have expired or been canceled)")))
2015           (if (or (eq result 'pseudo) (eq result 'nneething))
2016               (progn
2017                 (save-excursion
2018                   (set-buffer summary-buffer)
2019                   (push article gnus-newsgroup-history)
2020                   (setq gnus-last-article gnus-current-article
2021                         gnus-current-article 0
2022                         gnus-current-headers nil
2023                         gnus-article-current nil)
2024                   (if (eq result 'nneething)
2025                       (gnus-configure-windows 'summary)
2026                     (gnus-configure-windows 'article))
2027                   (gnus-set-global-variables))
2028                 (gnus-set-mode-line 'article))
2029             ;; The result from the `request' was an actual article -
2030             ;; or at least some text that is now displayed in the
2031             ;; article buffer.
2032             (when (and (numberp article)
2033                        (not (eq article gnus-current-article)))
2034               ;; Seems like a new article has been selected.
2035               ;; `gnus-current-article' must be an article number.
2036               (save-excursion
2037                 (set-buffer summary-buffer)
2038                 (push article gnus-newsgroup-history)
2039                 (setq gnus-last-article gnus-current-article
2040                       gnus-current-article article
2041                       gnus-current-headers
2042                       (gnus-summary-article-header gnus-current-article)
2043                       gnus-article-current
2044                       (cons gnus-newsgroup-name gnus-current-article))
2045                 (unless (vectorp gnus-current-headers)
2046                   (setq gnus-current-headers nil))
2047                 (gnus-summary-goto-subject gnus-current-article)
2048                 (gnus-summary-show-thread)
2049                 (run-hooks 'gnus-mark-article-hook)
2050                 (gnus-set-mode-line 'summary)
2051                 (when (gnus-visual-p 'article-highlight 'highlight)
2052                   (run-hooks 'gnus-visual-mark-article-hook))
2053                 ;; Set the global newsgroup variables here.
2054                 ;; Suggested by Jim Sisolak
2055                 ;; <sisolak@trans4.neep.wisc.edu>.
2056                 (gnus-set-global-variables)
2057                 (setq gnus-have-all-headers
2058                       (or all-headers gnus-show-all-headers))
2059                 (and gnus-use-cache
2060                      (vectorp (gnus-summary-article-header article))
2061                      (gnus-cache-possibly-enter-article
2062                       group article
2063                       (gnus-summary-article-header article)
2064                       (memq article gnus-newsgroup-marked)
2065                       (memq article gnus-newsgroup-dormant)
2066                       (memq article gnus-newsgroup-unreads)))))
2067             (when (or (numberp article)
2068                       (stringp article))
2069               ;; Hooks for getting information from the article.
2070               ;; This hook must be called before being narrowed.
2071               (let (buffer-read-only)
2072                 (run-hooks 'internal-hook)
2073                 (run-hooks 'gnus-article-prepare-hook)
2074                 ;; Decode MIME message.
2075                 (when gnus-show-mime
2076                   (if (or (not gnus-strict-mime)
2077                           (gnus-fetch-field "Mime-Version"))
2078                       (funcall gnus-show-mime-method)
2079                     (funcall gnus-decode-encoded-word-method)))
2080                 ;; Perform the article display hooks.
2081                 (run-hooks 'gnus-article-display-hook))
2082               ;; Do page break.
2083               (goto-char (point-min))
2084               (setq gnus-page-broken
2085                     (when gnus-break-pages
2086                       (gnus-narrow-to-page)
2087                       t)))
2088             (gnus-set-mode-line 'article)
2089             (gnus-configure-windows 'article)
2090             (goto-char (point-min))
2091             t))))))
2092
2093 (defun gnus-article-wash-status ()
2094   "Return a string which display status of article washing."
2095   (save-excursion
2096     (set-buffer gnus-article-buffer)
2097     (let ((cite (gnus-article-hidden-text-p 'cite))
2098           (headers (gnus-article-hidden-text-p 'headers))
2099           (boring (gnus-article-hidden-text-p 'boring-headers))
2100           (pgp (gnus-article-hidden-text-p 'pgp))
2101           (pem (gnus-article-hidden-text-p 'pem))
2102           (signature (gnus-article-hidden-text-p 'signature))
2103           (overstrike (gnus-article-hidden-text-p 'overstrike))
2104           (emphasis (gnus-article-hidden-text-p 'emphasis))
2105           (mime gnus-show-mime))
2106       (format "%c%c%c%c%c%c%c"
2107               (if cite ?c ? )
2108               (if (or headers boring) ?h ? )
2109               (if (or pgp pem) ?p ? )
2110               (if signature ?s ? )
2111               (if overstrike ?o ? )
2112               (if mime ?m ? )
2113               (if emphasis ?e ? )))))
2114
2115 (defun gnus-article-hide-headers-if-wanted ()
2116   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2117 Provided for backwards compatibility."
2118   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2119       gnus-inhibit-hiding
2120       (gnus-article-hide-headers)))
2121
2122 ;;; Article savers.
2123
2124 (defun gnus-output-to-file (file-name)
2125   "Append the current article to a file named FILE-NAME."
2126   (let ((artbuf (current-buffer)))
2127     (nnheader-temp-write nil
2128       (insert-buffer-substring artbuf)
2129       ;; Append newline at end of the buffer as separator, and then
2130       ;; save it to file.
2131       (goto-char (point-max))
2132       (insert "\n")
2133       (append-to-file (point-min) (point-max) file-name)
2134       t)))
2135
2136 (defun gnus-narrow-to-page (&optional arg)
2137   "Narrow the article buffer to a page.
2138 If given a numerical ARG, move forward ARG pages."
2139   (interactive "P")
2140   (setq arg (if arg (prefix-numeric-value arg) 0))
2141   (save-excursion
2142     (set-buffer gnus-article-buffer)
2143     (goto-char (point-min))
2144     (widen)
2145     ;; Remove any old next/prev buttons.
2146     (when (gnus-visual-p 'page-marker)
2147       (let ((buffer-read-only nil))
2148         (gnus-remove-text-with-property 'gnus-prev)
2149         (gnus-remove-text-with-property 'gnus-next)))
2150     (when
2151         (cond ((< arg 0)
2152                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2153               ((> arg 0)
2154                (re-search-forward page-delimiter nil 'move arg)))
2155       (goto-char (match-end 0)))
2156     (narrow-to-region
2157      (point)
2158      (if (re-search-forward page-delimiter nil 'move)
2159          (match-beginning 0)
2160        (point)))
2161     (when (and (gnus-visual-p 'page-marker)
2162                (not (= (point-min) 1)))
2163       (save-excursion
2164         (goto-char (point-min))
2165         (gnus-insert-prev-page-button)))
2166     (when (and (gnus-visual-p 'page-marker)
2167                (< (+ (point-max) 2) (buffer-size)))
2168       (save-excursion
2169         (goto-char (point-max))
2170         (gnus-insert-next-page-button)))))
2171
2172 ;; Article mode commands
2173
2174 (defun gnus-article-goto-next-page ()
2175   "Show the next page of the article."
2176   (interactive)
2177   (when (gnus-article-next-page)
2178     (goto-char (point-min))
2179     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2180
2181 (defun gnus-article-goto-prev-page ()
2182   "Show the next page of the article."
2183   (interactive)
2184   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2185     (gnus-article-prev-page nil)))
2186
2187 (defun gnus-article-next-page (&optional lines)
2188   "Show the next page of the current article.
2189 If end of article, return non-nil.  Otherwise return nil.
2190 Argument LINES specifies lines to be scrolled up."
2191   (interactive "p")
2192   (move-to-window-line -1)
2193   (if (save-excursion
2194         (end-of-line)
2195         (and (pos-visible-in-window-p)  ;Not continuation line.
2196              (eobp)))
2197       ;; Nothing in this page.
2198       (if (or (not gnus-page-broken)
2199               (save-excursion
2200                 (save-restriction
2201                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2202           t                             ;Nothing more.
2203         (gnus-narrow-to-page 1)         ;Go to next page.
2204         nil)
2205     ;; More in this page.
2206     (let ((scroll-in-place nil))
2207       (condition-case ()
2208           (scroll-up lines)
2209         (end-of-buffer
2210          ;; Long lines may cause an end-of-buffer error.
2211          (goto-char (point-max)))))
2212     (move-to-window-line 0)
2213     nil))
2214
2215 (defun gnus-article-prev-page (&optional lines)
2216   "Show previous page of current article.
2217 Argument LINES specifies lines to be scrolled down."
2218   (interactive "p")
2219   (move-to-window-line 0)
2220   (if (and gnus-page-broken
2221            (bobp)
2222            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2223       (progn
2224         (gnus-narrow-to-page -1)        ;Go to previous page.
2225         (goto-char (point-max))
2226         (recenter -1))
2227     (let ((scroll-in-place nil))
2228       (prog1
2229           (condition-case ()
2230               (scroll-down lines)
2231             (beginning-of-buffer
2232              (goto-char (point-min))))
2233         (move-to-window-line 0)))))
2234
2235 (defun gnus-article-refer-article ()
2236   "Read article specified by message-id around point."
2237   (interactive)
2238   (let ((point (point)))
2239     (search-forward ">" nil t)          ;Move point to end of "<....>".
2240     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2241         (let ((message-id (match-string 1)))
2242           (goto-char point)
2243           (set-buffer gnus-summary-buffer)
2244           (gnus-summary-refer-article message-id))
2245       (goto-char (point))
2246       (error "No references around point"))))
2247
2248 (defun gnus-article-show-summary ()
2249   "Reconfigure windows to show summary buffer."
2250   (interactive)
2251   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2252       (error "There is no summary buffer for this article buffer")
2253     (gnus-article-set-globals)
2254     (gnus-configure-windows 'article)
2255     (gnus-summary-goto-subject gnus-current-article)))
2256
2257 (defun gnus-article-describe-briefly ()
2258   "Describe article mode commands briefly."
2259   (interactive)
2260   (gnus-message 6
2261                 (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")))
2262
2263 (defun gnus-article-summary-command ()
2264   "Execute the last keystroke in the summary buffer."
2265   (interactive)
2266   (let ((obuf (current-buffer))
2267         (owin (current-window-configuration))
2268         func)
2269     (switch-to-buffer gnus-summary-buffer 'norecord)
2270     (setq func (lookup-key (current-local-map) (this-command-keys)))
2271     (call-interactively func)
2272     (set-buffer obuf)
2273     (set-window-configuration owin)
2274     (set-window-point (get-buffer-window (current-buffer)) (point))))
2275
2276 (defun gnus-article-summary-command-nosave ()
2277   "Execute the last keystroke in the summary buffer."
2278   (interactive)
2279   (let (func)
2280     (pop-to-buffer gnus-summary-buffer 'norecord)
2281     (setq func (lookup-key (current-local-map) (this-command-keys)))
2282     (call-interactively func)))
2283
2284 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2285   "Read a summary buffer key sequence and execute it from the article buffer."
2286   (interactive "P")
2287   (let ((nosaves
2288          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2289            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2290            "=" "^" "\M-^" "|"))
2291         (nosave-but-article
2292          '("A\r"))
2293         (nosave-in-article
2294          '("\C-d"))
2295         keys)
2296     (save-excursion
2297       (set-buffer gnus-summary-buffer)
2298       (let (gnus-pick-mode)
2299         (push (or key last-command-event) unread-command-events)
2300         (setq keys (read-key-sequence nil))))
2301     (message "")
2302
2303     (if (or (member keys nosaves)
2304             (member keys nosave-but-article)
2305             (member keys nosave-in-article))
2306         (let (func)
2307           (save-window-excursion
2308             (pop-to-buffer gnus-summary-buffer 'norecord)
2309             ;; We disable the pick minor mode commands.
2310             (let (gnus-pick-mode)
2311               (setq func (lookup-key (current-local-map) keys))))
2312           (if (not func)
2313               (ding)
2314             (unless (member keys nosave-in-article)
2315               (set-buffer gnus-summary-buffer))
2316             (call-interactively func))
2317           (when (member keys nosave-but-article)
2318             (pop-to-buffer gnus-article-buffer 'norecord)))
2319       ;; These commands should restore window configuration.
2320       (let ((obuf (current-buffer))
2321             (owin (current-window-configuration))
2322             (opoint (point))
2323             func in-buffer)
2324         (if not-restore-window
2325             (pop-to-buffer gnus-summary-buffer 'norecord)
2326           (switch-to-buffer gnus-summary-buffer 'norecord))
2327         (setq in-buffer (current-buffer))
2328         ;; We disable the pick minor mode commands.
2329         (if (setq func (let (gnus-pick-mode)
2330                          (lookup-key (current-local-map) keys)))
2331             (call-interactively func)
2332           (ding))
2333         (when (eq in-buffer (current-buffer))
2334           (set-buffer obuf)
2335           (unless not-restore-window
2336             (set-window-configuration owin))
2337           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
2338
2339 (defun gnus-article-hide (&optional arg force)
2340   "Hide all the gruft in the current article.
2341 This means that PGP stuff, signatures, cited text and (some)
2342 headers will be hidden.
2343 If given a prefix, show the hidden text instead."
2344   (interactive (list current-prefix-arg 'force))
2345   (gnus-article-hide-headers arg)
2346   (gnus-article-hide-pgp arg)
2347   (gnus-article-hide-citation-maybe arg force)
2348   (gnus-article-hide-signature arg))
2349
2350 (defun gnus-article-maybe-highlight ()
2351   "Do some article highlighting if `article-visual' is non-nil."
2352   (when (gnus-visual-p 'article-highlight 'highlight)
2353     (gnus-article-highlight-some)))
2354
2355 (defun gnus-request-article-this-buffer (article group)
2356   "Get an article and insert it into this buffer."
2357   (let (do-update-line)
2358     (prog1
2359         (save-excursion
2360           (erase-buffer)
2361           (gnus-kill-all-overlays)
2362           (setq group (or group gnus-newsgroup-name))
2363
2364           ;; Open server if it has closed.
2365           (gnus-check-server (gnus-find-method-for-group group))
2366
2367           ;; Using `gnus-request-article' directly will insert the article into
2368           ;; `nntp-server-buffer' - so we'll save some time by not having to
2369           ;; copy it from the server buffer into the article buffer.
2370
2371           ;; We only request an article by message-id when we do not have the
2372           ;; headers for it, so we'll have to get those.
2373           (when (stringp article)
2374             (let ((gnus-override-method gnus-refer-article-method))
2375               (gnus-read-header article)))
2376
2377           ;; If the article number is negative, that means that this article
2378           ;; doesn't belong in this newsgroup (possibly), so we find its
2379           ;; message-id and request it by id instead of number.
2380           (when (and (numberp article)
2381                      gnus-summary-buffer
2382                      (get-buffer gnus-summary-buffer)
2383                      (buffer-name (get-buffer gnus-summary-buffer)))
2384             (save-excursion
2385               (set-buffer gnus-summary-buffer)
2386               (let ((header (gnus-summary-article-header article)))
2387                 (when (< article 0)
2388                   (cond
2389                    ((memq article gnus-newsgroup-sparse)
2390                     ;; This is a sparse gap article.
2391                     (setq do-update-line article)
2392                     (setq article (mail-header-id header))
2393                     (let ((gnus-override-method gnus-refer-article-method))
2394                       (gnus-read-header article))
2395                     (setq gnus-newsgroup-sparse
2396                           (delq article gnus-newsgroup-sparse)))
2397                    ((vectorp header)
2398                     ;; It's a real article.
2399                     (setq article (mail-header-id header)))
2400                    (t
2401                     ;; It is an extracted pseudo-article.
2402                     (setq article 'pseudo)
2403                     (gnus-request-pseudo-article header))))
2404
2405                 (let ((method (gnus-find-method-for-group
2406                                gnus-newsgroup-name)))
2407                   (if (not (eq (car method) 'nneething))
2408                       ()
2409                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2410                                        (mail-header-subject header))))
2411                       (when (file-directory-p dir)
2412                         (setq article 'nneething)
2413                         (gnus-group-enter-directory dir))))))))
2414
2415           (cond
2416            ;; Refuse to select canceled articles.
2417            ((and (numberp article)
2418                  gnus-summary-buffer
2419                  (get-buffer gnus-summary-buffer)
2420                  (buffer-name (get-buffer gnus-summary-buffer))
2421                  (eq (cdr (save-excursion
2422                             (set-buffer gnus-summary-buffer)
2423                             (assq article gnus-newsgroup-reads)))
2424                      gnus-canceled-mark))
2425             nil)
2426            ;; We first check `gnus-original-article-buffer'.
2427            ((and (get-buffer gnus-original-article-buffer)
2428                  (numberp article)
2429                  (save-excursion
2430                    (set-buffer gnus-original-article-buffer)
2431                    (and (equal (car gnus-original-article) group)
2432                         (eq (cdr gnus-original-article) article))))
2433             (insert-buffer-substring gnus-original-article-buffer)
2434             'article)
2435            ;; Check the backlog.
2436            ((and gnus-keep-backlog
2437                  (gnus-backlog-request-article group article (current-buffer)))
2438             'article)
2439            ;; Check asynchronous pre-fetch.
2440            ((gnus-async-request-fetched-article group article (current-buffer))
2441             (gnus-async-prefetch-next group article gnus-summary-buffer)
2442             (when (and (numberp article) gnus-keep-backlog)
2443               (gnus-backlog-enter-article group article (current-buffer)))
2444             'article)
2445            ;; Check the cache.
2446            ((and gnus-use-cache
2447                  (numberp article)
2448                  (gnus-cache-request-article article group))
2449             'article)
2450            ;; Get the article and put into the article buffer.
2451            ((or (stringp article) (numberp article))
2452             (let ((gnus-override-method
2453                    (and (stringp article) gnus-refer-article-method))
2454                   (buffer-read-only nil))
2455               (erase-buffer)
2456               (gnus-kill-all-overlays)
2457               (when (gnus-request-article article group (current-buffer))
2458                 (when (numberp article)
2459                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2460                   (when gnus-keep-backlog
2461                     (gnus-backlog-enter-article
2462                      group article (current-buffer))))
2463                 'article)))
2464            ;; It was a pseudo.
2465            (t article)))
2466
2467       ;; Take the article from the original article buffer
2468       ;; and place it in the buffer it's supposed to be in.
2469       (when (and (get-buffer gnus-article-buffer)
2470                  ;;(numberp article)
2471                  (equal (buffer-name (current-buffer))
2472                         (buffer-name (get-buffer gnus-article-buffer))))
2473         (save-excursion
2474           (if (get-buffer gnus-original-article-buffer)
2475               (set-buffer (get-buffer gnus-original-article-buffer))
2476             (set-buffer (get-buffer-create gnus-original-article-buffer))
2477             (buffer-disable-undo (current-buffer))
2478             (setq major-mode 'gnus-original-article-mode)
2479             (setq buffer-read-only t)
2480             (gnus-add-current-to-buffer-list))
2481           (let (buffer-read-only)
2482             (erase-buffer)
2483             (insert-buffer-substring gnus-article-buffer))
2484           (setq gnus-original-article (cons group article))))
2485
2486       ;; Update sparse articles.
2487       (when (and do-update-line
2488                  (or (numberp article)
2489                      (stringp article)))
2490         (let ((buf (current-buffer)))
2491           (set-buffer gnus-summary-buffer)
2492           (gnus-summary-update-article do-update-line)
2493           (gnus-summary-goto-subject do-update-line nil t)
2494           (set-window-point (get-buffer-window (current-buffer) t)
2495                             (point))
2496           (set-buffer buf))))))
2497
2498 ;;;
2499 ;;; Article editing
2500 ;;;
2501
2502 (defcustom gnus-article-edit-mode-hook nil
2503   "Hook run in article edit mode buffers."
2504   :group 'gnus-article-various
2505   :type 'hook)
2506
2507 (defvar gnus-article-edit-done-function nil)
2508
2509 (defvar gnus-article-edit-mode-map nil)
2510
2511 (unless gnus-article-edit-mode-map
2512   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2513
2514   (gnus-define-keys gnus-article-edit-mode-map
2515     "\C-c\C-c" gnus-article-edit-done
2516     "\C-c\C-k" gnus-article-edit-exit)
2517
2518   (gnus-define-keys (gnus-article-edit-wash-map
2519                      "\C-c\C-w" gnus-article-edit-mode-map)
2520     "f" gnus-article-edit-full-stops))
2521
2522 (defun gnus-article-edit-mode ()
2523   "Major mode for editing articles.
2524 This is an extended text-mode.
2525
2526 \\{gnus-article-edit-mode-map}"
2527   (interactive)
2528   (kill-all-local-variables)
2529   (setq major-mode 'gnus-article-edit-mode)
2530   (setq mode-name "Article Edit")
2531   (use-local-map gnus-article-edit-mode-map)
2532   (make-local-variable 'gnus-article-edit-done-function)
2533   (make-local-variable 'gnus-prev-winconf)
2534   (setq buffer-read-only nil)
2535   (buffer-enable-undo)
2536   (widen)
2537   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
2538
2539 (defun gnus-article-edit (&optional force)
2540   "Edit the current article.
2541 This will have permanent effect only in mail groups.
2542 If FORCE is non-nil, allow editing of articles even in read-only
2543 groups."
2544   (interactive "P")
2545   (when (and (not force)
2546              (gnus-group-read-only-p))
2547     (error "The current newsgroup does not support article editing"))
2548   (gnus-article-edit-article
2549    `(lambda (no-highlight)
2550       (gnus-summary-edit-article-done
2551        ,(or (mail-header-references gnus-current-headers) "")
2552        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2553
2554 (defun gnus-article-edit-article (exit-func)
2555   "Start editing the contents of the current article buffer."
2556   (let ((winconf (current-window-configuration)))
2557     (set-buffer gnus-article-buffer)
2558     (gnus-article-edit-mode)
2559     (gnus-set-text-properties (point-min) (point-max) nil)
2560     (gnus-configure-windows 'edit-article)
2561     (setq gnus-article-edit-done-function exit-func)
2562     (setq gnus-prev-winconf winconf)
2563     (gnus-message 6 "C-c C-c to end edits")))
2564
2565 (defun gnus-article-edit-done (&optional arg)
2566   "Update the article edits and exit."
2567   (interactive "P")
2568   (save-excursion
2569     (save-restriction
2570       (widen)
2571       (goto-char (point-min))
2572       (when (search-forward "\n\n" nil 1)
2573         (let ((lines (count-lines (point) (point-max)))
2574               (length (- (point-max) (point)))
2575               (case-fold-search t)
2576               (body (copy-marker (point))))
2577           (goto-char (point-min))
2578           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
2579             (delete-region (match-beginning 1) (match-end 1))
2580             (insert (number-to-string length)))
2581           (goto-char (point-min))
2582           (when (re-search-forward
2583                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
2584             (delete-region (match-beginning 1) (match-end 1))
2585             (insert (number-to-string length)))
2586           (goto-char (point-min))
2587           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
2588             (delete-region (match-beginning 1) (match-end 1))
2589             (insert (number-to-string lines)))))))
2590   (let ((func gnus-article-edit-done-function)
2591         (buf (current-buffer))
2592         (start (window-start)))
2593     (gnus-article-edit-exit)
2594     (save-excursion
2595       (set-buffer buf)
2596       (let ((buffer-read-only nil))
2597         (funcall func arg)))
2598     (set-buffer buf)
2599     (set-window-start (get-buffer-window buf) start)
2600     (set-window-point (get-buffer-window buf) (point))))
2601
2602 (defun gnus-article-edit-exit ()
2603   "Exit the article editing without updating."
2604   (interactive)
2605   ;; We remove all text props from the article buffer.
2606   (let ((buf (format "%s" (buffer-string)))
2607         (curbuf (current-buffer))
2608         (p (point))
2609         (window-start (window-start)))
2610     (erase-buffer)
2611     (insert buf)
2612     (let ((winconf gnus-prev-winconf))
2613       (gnus-article-mode)
2614       ;; The cache and backlog have to be flushed somewhat.
2615       (when gnus-use-cache
2616         (gnus-cache-update-article
2617          (car gnus-article-current) (cdr gnus-article-current)))
2618       (when gnus-keep-backlog
2619         (gnus-backlog-remove-article
2620          (car gnus-article-current) (cdr gnus-article-current)))
2621       ;; Flush original article as well.
2622       (save-excursion
2623         (when (get-buffer gnus-original-article-buffer)
2624           (set-buffer gnus-original-article-buffer)
2625           (setq gnus-original-article nil)))
2626       (set-window-configuration winconf)
2627       ;; Tippy-toe some to make sure that point remains where it was.
2628       (let ((buf (current-buffer)))
2629         (set-buffer curbuf)
2630         (set-window-start (get-buffer-window (current-buffer)) window-start)
2631         (goto-char p)
2632         (set-buffer buf)))))
2633
2634 (defun gnus-article-edit-full-stops ()
2635   "Interactively repair spacing at end of sentences."
2636   (interactive)
2637   (save-excursion
2638     (goto-char (point-min))
2639     (search-forward-regexp "^$" nil t)
2640     (let ((case-fold-search nil))
2641       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2642
2643 ;;;
2644 ;;; Article highlights
2645 ;;;
2646
2647 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2648
2649 ;;; Internal Variables:
2650
2651 (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\\)"
2652   "Regular expression that matches URLs."
2653   :group 'gnus-article-buttons
2654   :type 'regexp)
2655
2656 (defcustom gnus-button-alist
2657   `(("<\\(url: ?\\)?news:\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2658      gnus-button-message-id 2)
2659     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2660     ("\\(\\b<\\(url: ?\\)?news:\\(//\\)?\\([^>\n\t ]*\\)>\\)" 1 t
2661      gnus-button-fetch-group 4)
2662     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2663     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2664      t gnus-button-message-id 3)
2665     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2666     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2667     ;; This is how URLs _should_ be embedded in text...
2668     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2669     ;; Raw URLs.
2670     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2671   "Alist of regexps matching buttons in article bodies.
2672
2673 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2674 REGEXP: is the string matching text around the button,
2675 BUTTON: is the number of the regexp grouping actually matching the button,
2676 FORM: is a lisp expression which must eval to true for the button to
2677 be added,
2678 CALLBACK: is the function to call when the user push this button, and each
2679 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2680
2681 CALLBACK can also be a variable, in that case the value of that
2682 variable it the real callback function."
2683   :group 'gnus-article-buttons
2684   :type '(repeat (list regexp
2685                        (integer :tag "Button")
2686                        (sexp :tag "Form")
2687                        (function :tag "Callback")
2688                        (repeat :tag "Par"
2689                                :inline t
2690                                (integer :tag "Regexp group")))))
2691
2692 (defcustom gnus-header-button-alist
2693   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2694      0 t gnus-button-message-id 0)
2695     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2696     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2697      0 t gnus-button-mailto 0)
2698     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2699     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2700     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2701     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2702      gnus-button-message-id 3))
2703   "Alist of headers and regexps to match buttons in article heads.
2704
2705 This alist is very similar to `gnus-button-alist', except that each
2706 alist has an additional HEADER element first in each entry:
2707
2708 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2709
2710 HEADER is a regexp to match a header.  For a fuller explanation, see
2711 `gnus-button-alist'."
2712   :group 'gnus-article-buttons
2713   :group 'gnus-article-headers
2714   :type '(repeat (list (regexp :tag "Header")
2715                        regexp
2716                        (integer :tag "Button")
2717                        (sexp :tag "Form")
2718                        (function :tag "Callback")
2719                        (repeat :tag "Par"
2720                                :inline t
2721                                (integer :tag "Regexp group")))))
2722
2723 (defvar gnus-button-regexp nil)
2724 (defvar gnus-button-marker-list nil)
2725 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2726
2727 (defvar gnus-button-last nil)
2728 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2729
2730 ;;; Commands:
2731
2732 (defun gnus-article-push-button (event)
2733   "Check text under the mouse pointer for a callback function.
2734 If the text under the mouse pointer has a `gnus-callback' property,
2735 call it with the value of the `gnus-data' text property."
2736   (interactive "e")
2737   (set-buffer (window-buffer (posn-window (event-start event))))
2738   (let* ((pos (posn-point (event-start event)))
2739          (data (get-text-property pos 'gnus-data))
2740          (fun (get-text-property pos 'gnus-callback)))
2741     (when fun
2742       (funcall fun data))))
2743
2744 (defun gnus-article-press-button ()
2745   "Check text at point for a callback function.
2746 If the text at point has a `gnus-callback' property,
2747 call it with the value of the `gnus-data' text property."
2748   (interactive)
2749   (let* ((data (get-text-property (point) 'gnus-data))
2750          (fun (get-text-property (point) 'gnus-callback)))
2751     (when fun
2752       (funcall fun data))))
2753
2754 (defun gnus-article-prev-button (n)
2755   "Move point to N buttons backward.
2756 If N is negative, move forward instead."
2757   (interactive "p")
2758   (gnus-article-next-button (- n)))
2759
2760 (defun gnus-article-next-button (n)
2761   "Move point to N buttons forward.
2762 If N is negative, move backward instead."
2763   (interactive "p")
2764   (let ((function (if (< n 0) 'previous-single-property-change
2765                     'next-single-property-change))
2766         (inhibit-point-motion-hooks t)
2767         (backward (< n 0))
2768         (limit (if (< n 0) (point-min) (point-max))))
2769     (setq n (abs n))
2770     (while (and (not (= limit (point)))
2771                 (> n 0))
2772       ;; Skip past the current button.
2773       (when (get-text-property (point) 'gnus-callback)
2774         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2775       ;; Go to the next (or previous) button.
2776       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2777       ;; Put point at the start of the button.
2778       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2779         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2780       ;; Skip past intangible buttons.
2781       (when (get-text-property (point) 'intangible)
2782         (incf n))
2783       (decf n))
2784     (unless (zerop n)
2785       (gnus-message 5 "No more buttons"))
2786     n))
2787
2788 (defun gnus-article-highlight (&optional force)
2789   "Highlight current article.
2790 This function calls `gnus-article-highlight-headers',
2791 `gnus-article-highlight-citation',
2792 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2793 do the highlighting.  See the documentation for those functions."
2794   (interactive (list 'force))
2795   (gnus-article-highlight-headers)
2796   (gnus-article-highlight-citation force)
2797   (gnus-article-highlight-signature)
2798   (gnus-article-add-buttons force)
2799   (gnus-article-add-buttons-to-head))
2800
2801 (defun gnus-article-highlight-some (&optional force)
2802   "Highlight current article.
2803 This function calls `gnus-article-highlight-headers',
2804 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2805 do the highlighting.  See the documentation for those functions."
2806   (interactive (list 'force))
2807   (gnus-article-highlight-headers)
2808   (gnus-article-highlight-signature)
2809   (gnus-article-add-buttons))
2810
2811 (defun gnus-article-highlight-headers ()
2812   "Highlight article headers as specified by `gnus-header-face-alist'."
2813   (interactive)
2814   (save-excursion
2815     (set-buffer gnus-article-buffer)
2816     (save-restriction
2817       (let ((alist gnus-header-face-alist)
2818             (buffer-read-only nil)
2819             (case-fold-search t)
2820             (inhibit-point-motion-hooks t)
2821             entry regexp header-face field-face from hpoints fpoints)
2822         (message-narrow-to-head)
2823         (while (setq entry (pop alist))
2824           (goto-char (point-min))
2825           (setq regexp (concat "^\\("
2826                                (if (string-equal "" (nth 0 entry))
2827                                    "[^\t ]"
2828                                  (nth 0 entry))
2829                                "\\)")
2830                 header-face (nth 1 entry)
2831                 field-face (nth 2 entry))
2832           (while (and (re-search-forward regexp nil t)
2833                       (not (eobp)))
2834             (beginning-of-line)
2835             (setq from (point))
2836             (unless (search-forward ":" nil t)
2837               (forward-char 1))
2838             (when (and header-face
2839                        (not (memq (point) hpoints)))
2840               (push (point) hpoints)
2841               (gnus-put-text-property from (point) 'face header-face))
2842             (when (and field-face
2843                        (not (memq (setq from (point)) fpoints)))
2844               (push from fpoints)
2845               (if (re-search-forward "^[^ \t]" nil t)
2846                   (forward-char -2)
2847                 (goto-char (point-max)))
2848               (gnus-put-text-property from (point) 'face field-face))))))))
2849
2850 (defun gnus-article-highlight-signature ()
2851   "Highlight the signature in an article.
2852 It does this by highlighting everything after
2853 `gnus-signature-separator' using `gnus-signature-face'."
2854   (interactive)
2855   (save-excursion
2856     (set-buffer gnus-article-buffer)
2857     (let ((buffer-read-only nil)
2858           (inhibit-point-motion-hooks t))
2859       (save-restriction
2860         (when (and gnus-signature-face
2861                    (gnus-article-narrow-to-signature))
2862           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2863                             'face gnus-signature-face)
2864           (widen)
2865           (gnus-article-search-signature)
2866           (let ((start (match-beginning 0))
2867                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2868             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2869                                      end)))))))
2870
2871 (defun gnus-button-in-region-p (b e prop)
2872   "Say whether PROP exists in the region."
2873   (text-property-not-all b e prop nil))
2874
2875 (defun gnus-article-add-buttons (&optional force)
2876   "Find external references in the article and make buttons of them.
2877 \"External references\" are things like Message-IDs and URLs, as
2878 specified by `gnus-button-alist'."
2879   (interactive (list 'force))
2880   (save-excursion
2881     (set-buffer gnus-article-buffer)
2882     (let ((buffer-read-only nil)
2883           (inhibit-point-motion-hooks t)
2884           (case-fold-search t)
2885           (alist gnus-button-alist)
2886           beg entry regexp)
2887       ;; Remove all old markers.
2888       (let (marker entry)
2889         (while (setq marker (pop gnus-button-marker-list))
2890           (goto-char marker)
2891           (when (setq entry (gnus-button-entry))
2892             (put-text-property (match-beginning (nth 1 entry))
2893                                (match-end (nth 1 entry))
2894                                'gnus-callback nil))
2895           (set-marker marker nil)))
2896       ;; We skip the headers.
2897       (goto-char (point-min))
2898       (unless (search-forward "\n\n" nil t)
2899         (goto-char (point-max)))
2900       (setq beg (point))
2901       (while (setq entry (pop alist))
2902         (setq regexp (car entry))
2903         (goto-char beg)
2904         (while (re-search-forward regexp nil t)
2905           (let* ((start (and entry (match-beginning (nth 1 entry))))
2906                  (end (and entry (match-end (nth 1 entry))))
2907                  (from (match-beginning 0)))
2908             (when (and (or (eq t (nth 2 entry))
2909                            (eval (nth 2 entry)))
2910                        (not (gnus-button-in-region-p
2911                              start end 'gnus-callback)))
2912               ;; That optional form returned non-nil, so we add the
2913               ;; button.
2914               (gnus-article-add-button
2915                start end 'gnus-button-push
2916                (car (push (set-marker (make-marker) from)
2917                           gnus-button-marker-list))))))))))
2918
2919 ;; Add buttons to the head of an article.
2920 (defun gnus-article-add-buttons-to-head ()
2921   "Add buttons to the head of the article."
2922   (interactive)
2923   (save-excursion
2924     (set-buffer gnus-article-buffer)
2925     (let ((buffer-read-only nil)
2926           (inhibit-point-motion-hooks t)
2927           (case-fold-search t)
2928           (alist gnus-header-button-alist)
2929           entry beg end)
2930       (nnheader-narrow-to-headers)
2931       (while alist
2932         ;; Each alist entry.
2933         (setq entry (car alist)
2934               alist (cdr alist))
2935         (goto-char (point-min))
2936         (while (re-search-forward (car entry) nil t)
2937           ;; Each header matching the entry.
2938           (setq beg (match-beginning 0))
2939           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2940                              (match-beginning 0))
2941                         (point-max)))
2942           (goto-char beg)
2943           (while (re-search-forward (nth 1 entry) end t)
2944             ;; Each match within a header.
2945             (let* ((entry (cdr entry))
2946                    (start (match-beginning (nth 1 entry)))
2947                    (end (match-end (nth 1 entry)))
2948                    (form (nth 2 entry)))
2949               (goto-char (match-end 0))
2950               (when (eval form)
2951                 (gnus-article-add-button
2952                  start end (nth 3 entry)
2953                  (buffer-substring (match-beginning (nth 4 entry))
2954                                    (match-end (nth 4 entry)))))))
2955           (goto-char end))))
2956     (widen)))
2957
2958 ;;; External functions:
2959
2960 (defun gnus-article-add-button (from to fun &optional data)
2961   "Create a button between FROM and TO with callback FUN and data DATA."
2962   (when gnus-article-button-face
2963     (gnus-overlay-put (gnus-make-overlay from to)
2964                       'face gnus-article-button-face))
2965   (gnus-add-text-properties
2966    from to
2967    (nconc (and gnus-article-mouse-face
2968                (list gnus-mouse-face-prop gnus-article-mouse-face))
2969           (list 'gnus-callback fun)
2970           (and data (list 'gnus-data data)))))
2971
2972 ;;; Internal functions:
2973
2974 (defun gnus-article-set-globals ()
2975   (save-excursion
2976     (set-buffer gnus-summary-buffer)
2977     (gnus-set-global-variables)))
2978
2979 (defun gnus-signature-toggle (end)
2980   (save-excursion
2981     (set-buffer gnus-article-buffer)
2982     (let ((buffer-read-only nil)
2983           (inhibit-point-motion-hooks t))
2984       (if (get-text-property end 'invisible)
2985           (gnus-article-unhide-text end (point-max))
2986         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
2987
2988 (defun gnus-button-entry ()
2989   ;; Return the first entry in `gnus-button-alist' matching this place.
2990   (let ((alist gnus-button-alist)
2991         (entry nil))
2992     (while alist
2993       (setq entry (pop alist))
2994       (if (looking-at (car entry))
2995           (setq alist nil)
2996         (setq entry nil)))
2997     entry))
2998
2999 (defun gnus-button-push (marker)
3000   ;; Push button starting at MARKER.
3001   (save-excursion
3002     (set-buffer gnus-article-buffer)
3003     (goto-char marker)
3004     (let* ((entry (gnus-button-entry))
3005            (inhibit-point-motion-hooks t)
3006            (fun (nth 3 entry))
3007            (args (mapcar (lambda (group)
3008                            (let ((string (match-string group)))
3009                              (gnus-set-text-properties
3010                               0 (length string) nil string)
3011                              string))
3012                          (nthcdr 4 entry))))
3013       (cond
3014        ((fboundp fun)
3015         (apply fun args))
3016        ((and (boundp fun)
3017              (fboundp (symbol-value fun)))
3018         (apply (symbol-value fun) args))
3019        (t
3020         (gnus-message 1 "You must define `%S' to use this button"
3021                       (cons fun args)))))))
3022
3023 (defun gnus-button-message-id (message-id)
3024   "Fetch MESSAGE-ID."
3025   (save-excursion
3026     (set-buffer gnus-summary-buffer)
3027     (gnus-summary-refer-article message-id)))
3028
3029 (defun gnus-button-fetch-group (address)
3030   "Fetch GROUP specified by ADDRESS."
3031   (if (not (string-match "[:/]" address))
3032       ;; This is just a simple group url.
3033       (gnus-group-read-ephemeral-group address gnus-select-method)
3034     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3035                            address))
3036         (error "Can't parse %s" address)
3037       (gnus-group-read-ephemeral-group
3038        (match-string 4 address)
3039        `(nntp ,(match-string 1 address)
3040               (nntp-address ,(match-string 1 address))
3041               (nntp-port-number ,(if (match-end 3)
3042                                      (match-string 3 address)
3043                                    "nntp")))))))
3044
3045 (defun gnus-split-string (string pattern)
3046   "Return a list of substrings of STRING which are separated by PATTERN."
3047   (let (parts (start 0))
3048     (while (string-match pattern string start)
3049       (setq parts (cons (substring string start (match-beginning 0)) parts)
3050             start (match-end 0)))
3051     (nreverse (cons (substring string start) parts))))
3052
3053 (defun gnus-url-parse-query-string (query &optional downcase)
3054   (let (retval pairs cur key val)
3055     (setq pairs (gnus-split-string query "&"))
3056     (while pairs
3057       (setq cur (car pairs)
3058             pairs (cdr pairs))
3059       (if (not (string-match "=" cur))
3060           nil                           ; Grace
3061         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3062               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3063         (if downcase
3064             (setq key (downcase key)))
3065         (setq cur (assoc key retval))
3066         (if cur
3067             (setcdr cur (cons val (cdr cur)))
3068           (setq retval (cons (list key val) retval)))))
3069     retval))
3070
3071 (defun gnus-url-unhex (x)
3072   (if (> x ?9)
3073       (if (>= x ?a)
3074           (+ 10 (- x ?a))
3075         (+ 10 (- x ?A)))
3076     (- x ?0)))
3077
3078 (defun gnus-url-unhex-string (str &optional allow-newlines)
3079   "Remove %XXX embedded spaces, etc in a url.
3080 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3081 decoding of carriage returns and line feeds in the string, which is normally
3082 forbidden in URL encoding."
3083   (setq str (or str ""))
3084   (let ((tmp "")
3085         (case-fold-search t))
3086     (while (string-match "%[0-9a-f][0-9a-f]" str)
3087       (let* ((start (match-beginning 0))
3088              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3089              (code (+ (* 16 ch1)
3090                       (gnus-url-unhex (elt str (+ start 2))))))
3091         (setq tmp (concat
3092                    tmp (substring str 0 start)
3093                    (cond
3094                     (allow-newlines
3095                      (char-to-string code))
3096                     ((or (= code ?\n) (= code ?\r))
3097                      " ")
3098                     (t (char-to-string code))))
3099               str (substring str (match-end 0)))))
3100     (setq tmp (concat tmp str))
3101     tmp))
3102
3103 (defun gnus-url-mailto (url)
3104   ;; Send mail to someone
3105   (when (string-match "mailto:/*\\(.*\\)" url)
3106     (setq url (substring url (match-beginning 1) nil)))
3107   (let (to args source-url subject func)
3108     (if (string-match (regexp-quote "?") url)
3109         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3110               args (gnus-url-parse-query-string
3111                     (substring url (match-end 0) nil) t))
3112       (setq to (gnus-url-unhex-string url)))
3113     (setq args (cons (list "to" to) args)
3114           subject (cdr-safe (assoc "subject" args)))
3115     (message-mail)
3116     (while args
3117       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3118       (if (fboundp func)
3119           (funcall func)
3120         (message-position-on-field (caar args)))
3121       (insert (mapconcat 'identity (cdar args) ", "))
3122       (setq args (cdr args)))
3123     (if subject
3124         (message-goto-body)
3125       (message-goto-subject))))
3126
3127 (defun gnus-button-mailto (address)
3128   ;; Mail to ADDRESS.
3129   (set-buffer (gnus-copy-article-buffer))
3130   (message-reply address))
3131
3132 (defun gnus-button-reply (address)
3133   ;; Reply to ADDRESS.
3134   (message-reply address))
3135
3136 (defun gnus-button-url (address)
3137   "Browse ADDRESS."
3138   (funcall browse-url-browser-function address))
3139
3140 (defun gnus-button-embedded-url (address)
3141   "Browse ADDRESS."
3142   (funcall browse-url-browser-function (gnus-strip-whitespace address)))
3143
3144 ;;; Next/prev buttons in the article buffer.
3145
3146 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3147 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3148
3149 (defvar gnus-prev-page-map nil)
3150 (unless gnus-prev-page-map
3151   (setq gnus-prev-page-map (make-sparse-keymap))
3152   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3153   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3154
3155 (defun gnus-insert-prev-page-button ()
3156   (let ((buffer-read-only nil))
3157     (gnus-eval-format
3158      gnus-prev-page-line-format nil
3159      `(gnus-prev t local-map ,gnus-prev-page-map
3160                  gnus-callback gnus-article-button-prev-page))))
3161
3162 (defvar gnus-next-page-map nil)
3163 (unless gnus-next-page-map
3164   (setq gnus-next-page-map (make-keymap))
3165   (suppress-keymap gnus-prev-page-map)
3166   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3167   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3168
3169 (defun gnus-button-next-page ()
3170   "Go to the next page."
3171   (interactive)
3172   (let ((win (selected-window)))
3173     (select-window (get-buffer-window gnus-article-buffer t))
3174     (gnus-article-next-page)
3175     (select-window win)))
3176
3177 (defun gnus-button-prev-page ()
3178   "Go to the prev page."
3179   (interactive)
3180   (let ((win (selected-window)))
3181     (select-window (get-buffer-window gnus-article-buffer t))
3182     (gnus-article-prev-page)
3183     (select-window win)))
3184
3185 (defun gnus-insert-next-page-button ()
3186   (let ((buffer-read-only nil))
3187     (gnus-eval-format gnus-next-page-line-format nil
3188                       `(gnus-next t local-map ,gnus-next-page-map
3189                                   gnus-callback
3190                                   gnus-article-button-next-page))))
3191
3192 (defun gnus-article-button-next-page (arg)
3193   "Go to the next page."
3194   (interactive "P")
3195   (let ((win (selected-window)))
3196     (select-window (get-buffer-window gnus-article-buffer t))
3197     (gnus-article-next-page)
3198     (select-window win)))
3199
3200 (defun gnus-article-button-prev-page (arg)
3201   "Go to the prev page."
3202   (interactive "P")
3203   (let ((win (selected-window)))
3204     (select-window (get-buffer-window gnus-article-buffer t))
3205     (gnus-article-prev-page)
3206     (select-window win)))
3207
3208
3209 ;;; @ for mime-view
3210 ;;;
3211
3212 (defun gnus-content-header-filter ()
3213   "Header filter for mime-view.
3214 It is registered to variable `mime-view-content-header-filter-alist'."
3215   (goto-char (point-min))
3216   (while (re-search-forward "^[^ \t:]+:" nil t)
3217     (let ((start (match-beginning 0))
3218           (end (std11-field-end))
3219           )
3220       (save-restriction
3221         (narrow-to-region start end)
3222         (decode-mime-charset-region start end default-mime-charset)
3223         (goto-char (point-max))
3224         )))
3225   (eword-decode-header)
3226   )
3227
3228 (defun mime-view-quitting-method-for-gnus ()
3229   (if (not gnus-show-mime)
3230       (mime-view-kill-buffer))
3231   (delete-other-windows)
3232   (gnus-article-show-summary)
3233   (if (or (not gnus-show-mime)
3234           (null gnus-have-all-headers))
3235       (gnus-summary-select-article nil t)
3236     ))
3237
3238 (set-alist 'mime-view-content-header-filter-alist
3239            'gnus-original-article-mode
3240            (function gnus-content-header-filter))
3241
3242 (set-alist 'mime-text-decoder-alist
3243            'gnus-original-article-mode
3244            (function mime-text-decode-buffer))
3245
3246 (set-alist 'mime-view-quitting-method-alist
3247            'gnus-original-article-mode
3248            (function mime-view-quitting-method-for-gnus))
3249
3250 (set-alist 'mime-view-show-summary-method
3251            'gnus-original-article-mode
3252            (function mime-view-quitting-method-for-gnus))
3253
3254
3255 ;;; @ end
3256 ;;;
3257
3258 (gnus-ems-redefine)
3259
3260 (provide 'gnus-art)
3261
3262 (run-hooks 'gnus-art-load-hook)
3263
3264 ;;; gnus-art.el ends here