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