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