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