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