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