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