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