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