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