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