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