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