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