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