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