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