Quassia Gnus v0.14.
[elisp/gnus.git-] / 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   (gnus-set-global-variables)
1583   (setq filename (gnus-read-save-file-name
1584                   "Save %s in rmail file:" filename
1585                   gnus-rmail-save-name gnus-newsgroup-name
1586                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1587   (gnus-eval-in-buffer-window gnus-save-article-buffer
1588     (save-excursion
1589       (save-restriction
1590         (widen)
1591         (gnus-output-to-rmail filename)))))
1592
1593 (defun gnus-summary-save-in-mail (&optional filename)
1594   "Append this article to Unix mail file.
1595 Optional argument FILENAME specifies file name.
1596 Directory to save to is default to `gnus-article-save-directory'."
1597   (interactive)
1598   (gnus-set-global-variables)
1599   (setq filename (gnus-read-save-file-name
1600                   "Save %s in Unix mail file:" filename
1601                   gnus-mail-save-name gnus-newsgroup-name
1602                   gnus-current-headers 'gnus-newsgroup-last-mail))
1603   (gnus-eval-in-buffer-window gnus-save-article-buffer
1604     (save-excursion
1605       (save-restriction
1606         (widen)
1607         (if (and (file-readable-p filename)
1608                  (mail-file-babyl-p filename))
1609             (gnus-output-to-rmail filename t)
1610           (gnus-output-to-mail filename))))))
1611
1612 (defun gnus-summary-save-in-file (&optional filename overwrite)
1613   "Append this article to file.
1614 Optional argument FILENAME specifies file name.
1615 Directory to save to is default to `gnus-article-save-directory'."
1616   (interactive)
1617   (gnus-set-global-variables)
1618   (setq filename (gnus-read-save-file-name
1619                   "Save %s in file:" filename
1620                   gnus-file-save-name gnus-newsgroup-name
1621                   gnus-current-headers 'gnus-newsgroup-last-file))
1622   (gnus-eval-in-buffer-window gnus-save-article-buffer
1623     (save-excursion
1624       (save-restriction
1625         (widen)
1626         (when (and overwrite
1627                    (file-exists-p filename))
1628           (delete-file filename))
1629         (gnus-output-to-file filename)))))
1630
1631 (defun gnus-summary-write-to-file (&optional filename)
1632   "Write this article to a file.
1633 Optional argument FILENAME specifies file name.
1634 The directory to save in defaults to `gnus-article-save-directory'."
1635   (interactive)
1636   (gnus-summary-save-in-file nil t))
1637
1638 (defun gnus-summary-save-body-in-file (&optional filename)
1639   "Append this article body to a file.
1640 Optional argument FILENAME specifies file name.
1641 The directory to save in defaults to `gnus-article-save-directory'."
1642   (interactive)
1643   (gnus-set-global-variables)
1644   (setq filename (gnus-read-save-file-name
1645                   "Save %s body in file:" filename
1646                   gnus-file-save-name gnus-newsgroup-name
1647                   gnus-current-headers 'gnus-newsgroup-last-file))
1648   (gnus-eval-in-buffer-window gnus-save-article-buffer
1649     (save-excursion
1650       (save-restriction
1651         (widen)
1652         (goto-char (point-min))
1653         (when (search-forward "\n\n" nil t)
1654           (narrow-to-region (point) (point-max)))
1655         (gnus-output-to-file filename)))))
1656
1657 (defun gnus-summary-save-in-pipe (&optional command)
1658   "Pipe this article to subprocess."
1659   (interactive)
1660   (gnus-set-global-variables)
1661   (setq command
1662         (cond ((eq command 'default)
1663                gnus-last-shell-command)
1664               (command command)
1665               (t (read-string
1666                   (format
1667                    "Shell command on %s: "
1668                    (if (and gnus-number-of-articles-to-be-saved
1669                             (> gnus-number-of-articles-to-be-saved 1))
1670                        (format "these %d articles"
1671                                gnus-number-of-articles-to-be-saved)
1672                      "this article"))
1673                   gnus-last-shell-command))))
1674   (when (string-equal command "")
1675     (setq command gnus-last-shell-command))
1676   (gnus-eval-in-buffer-window gnus-article-buffer
1677     (save-restriction
1678       (widen)
1679       (shell-command-on-region (point-min) (point-max) command nil)))
1680   (setq gnus-last-shell-command command))
1681
1682 ;;; Article file names when saving.
1683
1684 (defun gnus-capitalize-newsgroup (newsgroup)
1685   "Capitalize NEWSGROUP name."
1686   (when (not (zerop (length newsgroup)))
1687     (concat (char-to-string (upcase (aref newsgroup 0)))
1688             (substring newsgroup 1))))
1689
1690 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1691   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1692 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1693 Otherwise, it is like ~/News/news/group/num."
1694   (let ((default
1695           (expand-file-name
1696            (concat (if (gnus-use-long-file-name 'not-save)
1697                        (gnus-capitalize-newsgroup newsgroup)
1698                      (gnus-newsgroup-directory-form newsgroup))
1699                    "/" (int-to-string (mail-header-number headers)))
1700            gnus-article-save-directory)))
1701     (if (and last-file
1702              (string-equal (file-name-directory default)
1703                            (file-name-directory last-file))
1704              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1705         default
1706       (or last-file default))))
1707
1708 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1709   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1710 If variable `gnus-use-long-file-name' is non-nil, it is
1711 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1712   (let ((default
1713           (expand-file-name
1714            (concat (if (gnus-use-long-file-name 'not-save)
1715                        newsgroup
1716                      (gnus-newsgroup-directory-form newsgroup))
1717                    "/" (int-to-string (mail-header-number headers)))
1718            gnus-article-save-directory)))
1719     (if (and last-file
1720              (string-equal (file-name-directory default)
1721                            (file-name-directory last-file))
1722              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1723         default
1724       (or last-file default))))
1725
1726 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1727   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1728 If variable `gnus-use-long-file-name' is non-nil, it is
1729 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1730   (or last-file
1731       (expand-file-name
1732        (if (gnus-use-long-file-name 'not-save)
1733            (gnus-capitalize-newsgroup newsgroup)
1734          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1735        gnus-article-save-directory)))
1736
1737 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1738   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1739 If variable `gnus-use-long-file-name' is non-nil, it is
1740 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1741   (or last-file
1742       (expand-file-name
1743        (if (gnus-use-long-file-name 'not-save)
1744            newsgroup
1745          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1746        gnus-article-save-directory)))
1747
1748 (eval-and-compile
1749   (mapcar
1750    (lambda (func)
1751      (let (afunc gfunc)
1752        (if (consp func)
1753            (setq afunc (car func)
1754                  gfunc (cdr func))
1755          (setq afunc func
1756                gfunc (intern (format "gnus-%s" func))))
1757        (fset gfunc
1758              (if (not (fboundp afunc))
1759                  nil
1760                `(lambda (&optional interactive &rest args)
1761                   ,(documentation afunc t)
1762                   (interactive (list t))
1763                   (save-excursion
1764                     (set-buffer gnus-article-buffer)
1765                     (if interactive
1766                         (call-interactively ',afunc)
1767                       (apply ',afunc args))))))))
1768    '(article-hide-headers
1769      article-hide-boring-headers
1770      article-treat-overstrike
1771      (article-fill . gnus-article-word-wrap)
1772      article-remove-cr
1773      article-display-x-face
1774      article-de-quoted-unreadable
1775      article-mime-decode-quoted-printable
1776      article-hide-pgp
1777      article-hide-pem
1778      article-hide-signature
1779      article-remove-trailing-blank-lines
1780      article-strip-leading-blank-lines
1781      article-strip-multiple-blank-lines
1782      article-strip-leading-space
1783      article-strip-blank-lines
1784      article-date-local
1785      article-date-original
1786      article-date-ut
1787      article-date-user
1788      article-date-lapsed
1789      article-emphasize
1790      article-treat-dumbquotes
1791      (article-show-all . gnus-article-show-all-headers))))
1792 \f
1793 ;;;
1794 ;;; Gnus article mode
1795 ;;;
1796
1797 (put 'gnus-article-mode 'mode-class 'special)
1798
1799 (gnus-define-keys gnus-article-mode-map
1800   " " gnus-article-goto-next-page
1801   "\177" gnus-article-goto-prev-page
1802   [delete] gnus-article-goto-prev-page
1803   "\C-c^" gnus-article-refer-article
1804   "h" gnus-article-show-summary
1805   "s" gnus-article-show-summary
1806   "\C-c\C-m" gnus-article-mail
1807   "?" gnus-article-describe-briefly
1808   gnus-mouse-2 gnus-article-push-button
1809   "\r" gnus-article-press-button
1810   "\t" gnus-article-next-button
1811   "\M-\t" gnus-article-prev-button
1812   "e" gnus-article-edit
1813   "<" beginning-of-buffer
1814   ">" end-of-buffer
1815   "\C-c\C-i" gnus-info-find-node
1816   "\C-c\C-b" gnus-bug
1817
1818   "\C-d" gnus-article-read-summary-keys
1819   "\M-*" gnus-article-read-summary-keys
1820   "\M-#" gnus-article-read-summary-keys
1821   "\M-^" gnus-article-read-summary-keys
1822   "\M-g" gnus-article-read-summary-keys)
1823
1824 (substitute-key-definition
1825  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
1826
1827 (defun gnus-article-make-menu-bar ()
1828   (gnus-turn-off-edit-menu 'article)
1829   (unless (boundp 'gnus-article-article-menu)
1830     (easy-menu-define
1831      gnus-article-article-menu gnus-article-mode-map ""
1832      '("Article"
1833        ["Scroll forwards" gnus-article-goto-next-page t]
1834        ["Scroll backwards" gnus-article-goto-prev-page t]
1835        ["Show summary" gnus-article-show-summary t]
1836        ["Fetch Message-ID at point" gnus-article-refer-article t]
1837        ["Mail to address at point" gnus-article-mail t]))
1838
1839     (easy-menu-define
1840      gnus-article-treatment-menu gnus-article-mode-map ""
1841      '("Treatment"
1842        ["Hide headers" gnus-article-hide-headers t]
1843        ["Hide signature" gnus-article-hide-signature t]
1844        ["Hide citation" gnus-article-hide-citation t]
1845        ["Treat overstrike" gnus-article-treat-overstrike t]
1846        ["Remove carriage return" gnus-article-remove-cr t]
1847        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1848
1849     (when nil
1850       (when (boundp 'gnus-summary-article-menu)
1851         (define-key gnus-article-mode-map [menu-bar commands]
1852           (cons "Commands" gnus-summary-article-menu))))
1853
1854     (when (boundp 'gnus-summary-post-menu)
1855       (define-key gnus-article-mode-map [menu-bar post]
1856         (cons "Post" gnus-summary-post-menu)))
1857
1858     (run-hooks 'gnus-article-menu-hook)))
1859
1860 (defun gnus-article-mode ()
1861   "Major mode for displaying an article.
1862
1863 All normal editing commands are switched off.
1864
1865 The following commands are available in addition to all summary mode
1866 commands:
1867 \\<gnus-article-mode-map>
1868 \\[gnus-article-next-page]\t Scroll the article one page forwards
1869 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1870 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1871 \\[gnus-article-show-summary]\t Display the summary buffer
1872 \\[gnus-article-mail]\t Send a reply to the address near point
1873 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1874 \\[gnus-info-find-node]\t Go to the Gnus info node"
1875   (interactive)
1876   (when (gnus-visual-p 'article-menu 'menu)
1877     (gnus-article-make-menu-bar))
1878   (kill-all-local-variables)
1879   (gnus-simplify-mode-line)
1880   (setq mode-name "Article")
1881   (setq major-mode 'gnus-article-mode)
1882   (make-local-variable 'minor-mode-alist)
1883   (unless (assq 'gnus-show-mime minor-mode-alist)
1884     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1885   (use-local-map gnus-article-mode-map)
1886   (gnus-update-format-specifications nil 'article-mode)
1887   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1888   (set (make-local-variable 'gnus-page-broken) nil)
1889   (set (make-local-variable 'gnus-button-marker-list) nil)
1890   (gnus-set-default-directory)
1891   (buffer-disable-undo (current-buffer))
1892   (setq buffer-read-only t)
1893   (set-syntax-table gnus-article-mode-syntax-table)
1894   (run-hooks 'gnus-article-mode-hook))
1895
1896 (defun gnus-article-setup-buffer ()
1897   "Initialize the article buffer."
1898   (let* ((name (if gnus-single-article-buffer "*Article*"
1899                  (concat "*Article " gnus-newsgroup-name "*")))
1900          (original
1901           (progn (string-match "\\*Article" name)
1902                  (concat " *Original Article"
1903                          (substring name (match-end 0))))))
1904     (setq gnus-article-buffer name)
1905     (setq gnus-original-article-buffer original)
1906     ;; This might be a variable local to the summary buffer.
1907     (unless gnus-single-article-buffer
1908       (save-excursion
1909         (set-buffer gnus-summary-buffer)
1910         (setq gnus-article-buffer name)
1911         (setq gnus-original-article-buffer original)
1912         (gnus-set-global-variables)))
1913     ;; Init original article buffer.
1914     (save-excursion
1915       (set-buffer (get-buffer-create gnus-original-article-buffer))
1916       (buffer-disable-undo (current-buffer))
1917       (setq major-mode 'gnus-original-article-mode)
1918       (gnus-add-current-to-buffer-list)
1919       (make-local-variable 'gnus-original-article))
1920     (if (get-buffer name)
1921         (save-excursion
1922           (set-buffer name)
1923           (buffer-disable-undo (current-buffer))
1924           (setq buffer-read-only t)
1925           (gnus-add-current-to-buffer-list)
1926           (unless (eq major-mode 'gnus-article-mode)
1927             (gnus-article-mode))
1928           (current-buffer))
1929       (save-excursion
1930         (set-buffer (get-buffer-create name))
1931         (gnus-add-current-to-buffer-list)
1932         (gnus-article-mode)
1933         (make-local-variable 'gnus-summary-buffer)
1934         (current-buffer)))))
1935
1936 ;; Set article window start at LINE, where LINE is the number of lines
1937 ;; from the head of the article.
1938 (defun gnus-article-set-window-start (&optional line)
1939   (set-window-start
1940    (get-buffer-window gnus-article-buffer t)
1941    (save-excursion
1942      (set-buffer gnus-article-buffer)
1943      (goto-char (point-min))
1944      (if (not line)
1945          (point-min)
1946        (gnus-message 6 "Moved to bookmark")
1947        (search-forward "\n\n" nil t)
1948        (forward-line line)
1949        (point)))))
1950
1951 (defun gnus-article-prepare (article &optional all-headers header)
1952   "Prepare ARTICLE in article mode buffer.
1953 ARTICLE should either be an article number or a Message-ID.
1954 If ARTICLE is an id, HEADER should be the article headers.
1955 If ALL-HEADERS is non-nil, no headers are hidden."
1956   (save-excursion
1957     ;; Make sure we start in a summary buffer.
1958     (unless (eq major-mode 'gnus-summary-mode)
1959       (set-buffer gnus-summary-buffer))
1960     (setq gnus-summary-buffer (current-buffer))
1961     ;; Make sure the connection to the server is alive.
1962     (unless (gnus-server-opened
1963              (gnus-find-method-for-group gnus-newsgroup-name))
1964       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
1965       (gnus-request-group gnus-newsgroup-name t))
1966     (let* ((gnus-article (if header (mail-header-number header) article))
1967            (summary-buffer (current-buffer))
1968            (internal-hook gnus-article-internal-prepare-hook)
1969            (group gnus-newsgroup-name)
1970            result)
1971       (save-excursion
1972         (gnus-article-setup-buffer)
1973         (set-buffer gnus-article-buffer)
1974         ;; Deactivate active regions.
1975         (when (and (boundp 'transient-mark-mode)
1976                    transient-mark-mode)
1977           (setq mark-active nil))
1978         (if (not (setq result (let ((buffer-read-only nil))
1979                                 (gnus-request-article-this-buffer
1980                                  article group))))
1981             ;; There is no such article.
1982             (save-excursion
1983               (when (and (numberp article)
1984                          (not (memq article gnus-newsgroup-sparse)))
1985                 (setq gnus-article-current
1986                       (cons gnus-newsgroup-name article))
1987                 (set-buffer gnus-summary-buffer)
1988                 (setq gnus-current-article article)
1989                 (gnus-summary-mark-article article gnus-canceled-mark))
1990               (unless (memq article gnus-newsgroup-sparse)
1991                 (gnus-error
1992                  1 "No such article (may have expired or been canceled)")))
1993           (if (or (eq result 'pseudo) (eq result 'nneething))
1994               (progn
1995                 (save-excursion
1996                   (set-buffer summary-buffer)
1997                   (push article gnus-newsgroup-history)
1998                   (setq gnus-last-article gnus-current-article
1999                         gnus-current-article 0
2000                         gnus-current-headers nil
2001                         gnus-article-current nil)
2002                   (if (eq result 'nneething)
2003                       (gnus-configure-windows 'summary)
2004                     (gnus-configure-windows 'article))
2005                   (gnus-set-global-variables))
2006                 (gnus-set-mode-line 'article))
2007             ;; The result from the `request' was an actual article -
2008             ;; or at least some text that is now displayed in the
2009             ;; article buffer.
2010             (when (and (numberp article)
2011                        (not (eq article gnus-current-article)))
2012               ;; Seems like a new article has been selected.
2013               ;; `gnus-current-article' must be an article number.
2014               (save-excursion
2015                 (set-buffer summary-buffer)
2016                 (push article gnus-newsgroup-history)
2017                 (setq gnus-last-article gnus-current-article
2018                       gnus-current-article article
2019                       gnus-current-headers
2020                       (gnus-summary-article-header gnus-current-article)
2021                       gnus-article-current
2022                       (cons gnus-newsgroup-name gnus-current-article))
2023                 (unless (vectorp gnus-current-headers)
2024                   (setq gnus-current-headers nil))
2025                 (gnus-summary-goto-subject gnus-current-article)
2026                 (gnus-summary-show-thread)
2027                 (run-hooks 'gnus-mark-article-hook)
2028                 (gnus-set-mode-line 'summary)
2029                 (when (gnus-visual-p 'article-highlight 'highlight)
2030                   (run-hooks 'gnus-visual-mark-article-hook))
2031                 ;; Set the global newsgroup variables here.
2032                 ;; Suggested by Jim Sisolak
2033                 ;; <sisolak@trans4.neep.wisc.edu>.
2034                 (gnus-set-global-variables)
2035                 (setq gnus-have-all-headers
2036                       (or all-headers gnus-show-all-headers))
2037                 (and gnus-use-cache
2038                      (vectorp (gnus-summary-article-header article))
2039                      (gnus-cache-possibly-enter-article
2040                       group article
2041                       (gnus-summary-article-header article)
2042                       (memq article gnus-newsgroup-marked)
2043                       (memq article gnus-newsgroup-dormant)
2044                       (memq article gnus-newsgroup-unreads)))))
2045             (when (or (numberp article)
2046                       (stringp article))
2047               ;; Hooks for getting information from the article.
2048               ;; This hook must be called before being narrowed.
2049               (let (buffer-read-only)
2050                 (run-hooks 'internal-hook)
2051                 (run-hooks 'gnus-article-prepare-hook)
2052                 ;; Decode MIME message.
2053                 (when gnus-show-mime
2054                   (if (or (not gnus-strict-mime)
2055                           (gnus-fetch-field "Mime-Version"))
2056                       (funcall gnus-show-mime-method)
2057                     (funcall gnus-decode-encoded-word-method)))
2058                 ;; Perform the article display hooks.
2059                 (run-hooks 'gnus-article-display-hook))
2060               ;; Do page break.
2061               (goto-char (point-min))
2062               (setq gnus-page-broken
2063                     (when gnus-break-pages
2064                       (gnus-narrow-to-page)
2065                       t)))
2066             (gnus-set-mode-line 'article)
2067             (gnus-configure-windows 'article)
2068             (goto-char (point-min))
2069             t))))))
2070
2071 (defun gnus-article-wash-status ()
2072   "Return a string which display status of article washing."
2073   (save-excursion
2074     (set-buffer gnus-article-buffer)
2075     (let ((cite (gnus-article-hidden-text-p 'cite))
2076           (headers (gnus-article-hidden-text-p 'headers))
2077           (boring (gnus-article-hidden-text-p 'boring-headers))
2078           (pgp (gnus-article-hidden-text-p 'pgp))
2079           (pem (gnus-article-hidden-text-p 'pem))
2080           (signature (gnus-article-hidden-text-p 'signature))
2081           (overstrike (gnus-article-hidden-text-p 'overstrike))
2082           (emphasis (gnus-article-hidden-text-p 'emphasis))
2083           (mime gnus-show-mime))
2084       (format "%c%c%c%c%c%c%c"
2085               (if cite ?c ? )
2086               (if (or headers boring) ?h ? )
2087               (if (or pgp pem) ?p ? )
2088               (if signature ?s ? )
2089               (if overstrike ?o ? )
2090               (if mime ?m ? )
2091               (if emphasis ?e ? )))))
2092
2093 (defun gnus-article-hide-headers-if-wanted ()
2094   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2095 Provided for backwards compatibility."
2096   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2097       gnus-inhibit-hiding
2098       (gnus-article-hide-headers)))
2099
2100 ;;; Article savers.
2101
2102 (defun gnus-output-to-file (file-name)
2103   "Append the current article to a file named FILE-NAME."
2104   (let ((artbuf (current-buffer)))
2105     (nnheader-temp-write nil
2106       (insert-buffer-substring artbuf)
2107       ;; Append newline at end of the buffer as separator, and then
2108       ;; save it to file.
2109       (goto-char (point-max))
2110       (insert "\n")
2111       (append-to-file (point-min) (point-max) file-name)
2112       t)))
2113
2114 (defun gnus-narrow-to-page (&optional arg)
2115   "Narrow the article buffer to a page.
2116 If given a numerical ARG, move forward ARG pages."
2117   (interactive "P")
2118   (setq arg (if arg (prefix-numeric-value arg) 0))
2119   (save-excursion
2120     (set-buffer gnus-article-buffer)
2121     (goto-char (point-min))
2122     (widen)
2123     ;; Remove any old next/prev buttons.
2124     (when (gnus-visual-p 'page-marker)
2125       (let ((buffer-read-only nil))
2126         (gnus-remove-text-with-property 'gnus-prev)
2127         (gnus-remove-text-with-property 'gnus-next)))
2128     (when
2129         (cond ((< arg 0)
2130                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2131               ((> arg 0)
2132                (re-search-forward page-delimiter nil 'move arg)))
2133       (goto-char (match-end 0)))
2134     (narrow-to-region
2135      (point)
2136      (if (re-search-forward page-delimiter nil 'move)
2137          (match-beginning 0)
2138        (point)))
2139     (when (and (gnus-visual-p 'page-marker)
2140                (not (= (point-min) 1)))
2141       (save-excursion
2142         (goto-char (point-min))
2143         (gnus-insert-prev-page-button)))
2144     (when (and (gnus-visual-p 'page-marker)
2145                (< (+ (point-max) 2) (buffer-size)))
2146       (save-excursion
2147         (goto-char (point-max))
2148         (gnus-insert-next-page-button)))))
2149
2150 ;; Article mode commands
2151
2152 (defun gnus-article-goto-next-page ()
2153   "Show the next page of the article."
2154   (interactive)
2155   (when (gnus-article-next-page)
2156     (goto-char (point-min))
2157     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2158
2159 (defun gnus-article-goto-prev-page ()
2160   "Show the next page of the article."
2161   (interactive)
2162   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2163     (gnus-article-prev-page nil)))
2164
2165 (defun gnus-article-next-page (&optional lines)
2166   "Show the next page of the current article.
2167 If end of article, return non-nil.  Otherwise return nil.
2168 Argument LINES specifies lines to be scrolled up."
2169   (interactive "p")
2170   (move-to-window-line -1)
2171   (if (save-excursion
2172         (end-of-line)
2173         (and (pos-visible-in-window-p)  ;Not continuation line.
2174              (eobp)))
2175       ;; Nothing in this page.
2176       (if (or (not gnus-page-broken)
2177               (save-excursion
2178                 (save-restriction
2179                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2180           t                             ;Nothing more.
2181         (gnus-narrow-to-page 1)         ;Go to next page.
2182         nil)
2183     ;; More in this page.
2184     (let ((scroll-in-place nil))
2185       (condition-case ()
2186           (scroll-up lines)
2187         (end-of-buffer
2188          ;; Long lines may cause an end-of-buffer error.
2189          (goto-char (point-max)))))
2190     (move-to-window-line 0)
2191     nil))
2192
2193 (defun gnus-article-prev-page (&optional lines)
2194   "Show previous page of current article.
2195 Argument LINES specifies lines to be scrolled down."
2196   (interactive "p")
2197   (move-to-window-line 0)
2198   (if (and gnus-page-broken
2199            (bobp)
2200            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2201       (progn
2202         (gnus-narrow-to-page -1)        ;Go to previous page.
2203         (goto-char (point-max))
2204         (recenter -1))
2205     (let ((scroll-in-place nil))
2206       (prog1
2207           (condition-case ()
2208               (scroll-down lines)
2209             (beginning-of-buffer
2210              (goto-char (point-min))))
2211         (move-to-window-line 0)))))
2212
2213 (defun gnus-article-refer-article ()
2214   "Read article specified by message-id around point."
2215   (interactive)
2216   (let ((point (point)))
2217     (search-forward ">" nil t)          ;Move point to end of "<....>".
2218     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2219         (let ((message-id (match-string 1)))
2220           (goto-char point)
2221           (set-buffer gnus-summary-buffer)
2222           (gnus-summary-refer-article message-id))
2223       (goto-char (point))
2224       (error "No references around point"))))
2225
2226 (defun gnus-article-show-summary ()
2227   "Reconfigure windows to show summary buffer."
2228   (interactive)
2229   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2230       (error "There is no summary buffer for this article buffer")
2231     (gnus-article-set-globals)
2232     (gnus-configure-windows 'article)
2233     (gnus-summary-goto-subject gnus-current-article)))
2234
2235 (defun gnus-article-describe-briefly ()
2236   "Describe article mode commands briefly."
2237   (interactive)
2238   (gnus-message 6
2239                 (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")))
2240
2241 (defun gnus-article-summary-command ()
2242   "Execute the last keystroke in the summary buffer."
2243   (interactive)
2244   (let ((obuf (current-buffer))
2245         (owin (current-window-configuration))
2246         func)
2247     (switch-to-buffer gnus-summary-buffer 'norecord)
2248     (setq func (lookup-key (current-local-map) (this-command-keys)))
2249     (call-interactively func)
2250     (set-buffer obuf)
2251     (set-window-configuration owin)
2252     (set-window-point (get-buffer-window (current-buffer)) (point))))
2253
2254 (defun gnus-article-summary-command-nosave ()
2255   "Execute the last keystroke in the summary buffer."
2256   (interactive)
2257   (let (func)
2258     (pop-to-buffer gnus-summary-buffer 'norecord)
2259     (setq func (lookup-key (current-local-map) (this-command-keys)))
2260     (call-interactively func)))
2261
2262 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2263   "Read a summary buffer key sequence and execute it from the article buffer."
2264   (interactive "P")
2265   (let ((nosaves
2266          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2267            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2268            "=" "^" "\M-^" "|"))
2269         (nosave-but-article
2270          '("A\r"))
2271         (nosave-in-article
2272          '("\C-d"))
2273         keys)
2274     (save-excursion
2275       (set-buffer gnus-summary-buffer)
2276       (let (gnus-pick-mode)
2277         (push (or key last-command-event) unread-command-events)
2278         (setq keys (read-key-sequence nil))))
2279     (message "")
2280
2281     (if (or (member keys nosaves)
2282             (member keys nosave-but-article)
2283             (member keys nosave-in-article))
2284         (let (func)
2285           (save-window-excursion
2286             (pop-to-buffer gnus-summary-buffer 'norecord)
2287             ;; We disable the pick minor mode commands.
2288             (let (gnus-pick-mode)
2289               (setq func (lookup-key (current-local-map) keys))))
2290           (if (not func)
2291               (ding)
2292             (unless (member keys nosave-in-article)
2293               (set-buffer gnus-summary-buffer))
2294             (call-interactively func))
2295           (when (member keys nosave-but-article)
2296             (pop-to-buffer gnus-article-buffer 'norecord)))
2297       ;; These commands should restore window configuration.
2298       (let ((obuf (current-buffer))
2299             (owin (current-window-configuration))
2300             (opoint (point))
2301             func in-buffer)
2302         (if not-restore-window
2303             (pop-to-buffer gnus-summary-buffer 'norecord)
2304           (switch-to-buffer gnus-summary-buffer 'norecord))
2305         (setq in-buffer (current-buffer))
2306         ;; We disable the pick minor mode commands.
2307         (if (setq func (let (gnus-pick-mode)
2308                          (lookup-key (current-local-map) keys)))
2309             (call-interactively func)
2310           (ding))
2311         (when (eq in-buffer (current-buffer))
2312           (set-buffer obuf)
2313           (unless not-restore-window
2314             (set-window-configuration owin))
2315           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
2316
2317 (defun gnus-article-hide (&optional arg force)
2318   "Hide all the gruft in the current article.
2319 This means that PGP stuff, signatures, cited text and (some)
2320 headers will be hidden.
2321 If given a prefix, show the hidden text instead."
2322   (interactive (list current-prefix-arg 'force))
2323   (gnus-article-hide-headers arg)
2324   (gnus-article-hide-pgp arg)
2325   (gnus-article-hide-citation-maybe arg force)
2326   (gnus-article-hide-signature arg))
2327
2328 (defun gnus-article-maybe-highlight ()
2329   "Do some article highlighting if `article-visual' is non-nil."
2330   (when (gnus-visual-p 'article-highlight 'highlight)
2331     (gnus-article-highlight-some)))
2332
2333 (defun gnus-request-article-this-buffer (article group)
2334   "Get an article and insert it into this buffer."
2335   (let (do-update-line)
2336     (prog1
2337         (save-excursion
2338           (erase-buffer)
2339           (gnus-kill-all-overlays)
2340           (setq group (or group gnus-newsgroup-name))
2341
2342           ;; Open server if it has closed.
2343           (gnus-check-server (gnus-find-method-for-group group))
2344
2345           ;; Using `gnus-request-article' directly will insert the article into
2346           ;; `nntp-server-buffer' - so we'll save some time by not having to
2347           ;; copy it from the server buffer into the article buffer.
2348
2349           ;; We only request an article by message-id when we do not have the
2350           ;; headers for it, so we'll have to get those.
2351           (when (stringp article)
2352             (let ((gnus-override-method gnus-refer-article-method))
2353               (gnus-read-header article)))
2354
2355           ;; If the article number is negative, that means that this article
2356           ;; doesn't belong in this newsgroup (possibly), so we find its
2357           ;; message-id and request it by id instead of number.
2358           (when (and (numberp article)
2359                      gnus-summary-buffer
2360                      (get-buffer gnus-summary-buffer)
2361                      (buffer-name (get-buffer gnus-summary-buffer)))
2362             (save-excursion
2363               (set-buffer gnus-summary-buffer)
2364               (let ((header (gnus-summary-article-header article)))
2365                 (when (< article 0)
2366                   (cond
2367                    ((memq article gnus-newsgroup-sparse)
2368                     ;; This is a sparse gap article.
2369                     (setq do-update-line article)
2370                     (setq article (mail-header-id header))
2371                     (let ((gnus-override-method gnus-refer-article-method))
2372                       (gnus-read-header article))
2373                     (setq gnus-newsgroup-sparse
2374                           (delq article gnus-newsgroup-sparse)))
2375                    ((vectorp header)
2376                     ;; It's a real article.
2377                     (setq article (mail-header-id header)))
2378                    (t
2379                     ;; It is an extracted pseudo-article.
2380                     (setq article 'pseudo)
2381                     (gnus-request-pseudo-article header))))
2382
2383                 (let ((method (gnus-find-method-for-group
2384                                gnus-newsgroup-name)))
2385                   (if (not (eq (car method) 'nneething))
2386                       ()
2387                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2388                                        (mail-header-subject header))))
2389                       (when (file-directory-p dir)
2390                         (setq article 'nneething)
2391                         (gnus-group-enter-directory dir))))))))
2392
2393           (cond
2394            ;; Refuse to select canceled articles.
2395            ((and (numberp article)
2396                  gnus-summary-buffer
2397                  (get-buffer gnus-summary-buffer)
2398                  (buffer-name (get-buffer gnus-summary-buffer))
2399                  (eq (cdr (save-excursion
2400                             (set-buffer gnus-summary-buffer)
2401                             (assq article gnus-newsgroup-reads)))
2402                      gnus-canceled-mark))
2403             nil)
2404            ;; We first check `gnus-original-article-buffer'.
2405            ((and (get-buffer gnus-original-article-buffer)
2406                  (numberp article)
2407                  (save-excursion
2408                    (set-buffer gnus-original-article-buffer)
2409                    (and (equal (car gnus-original-article) group)
2410                         (eq (cdr gnus-original-article) article))))
2411             (insert-buffer-substring gnus-original-article-buffer)
2412             'article)
2413            ;; Check the backlog.
2414            ((and gnus-keep-backlog
2415                  (gnus-backlog-request-article group article (current-buffer)))
2416             'article)
2417            ;; Check asynchronous pre-fetch.
2418            ((gnus-async-request-fetched-article group article (current-buffer))
2419             (gnus-async-prefetch-next group article gnus-summary-buffer)
2420             'article)
2421            ;; Check the cache.
2422            ((and gnus-use-cache
2423                  (numberp article)
2424                  (gnus-cache-request-article article group))
2425             'article)
2426            ;; Get the article and put into the article buffer.
2427            ((or (stringp article) (numberp article))
2428             (let ((gnus-override-method
2429                    (and (stringp article) gnus-refer-article-method))
2430                   (buffer-read-only nil))
2431               (erase-buffer)
2432               (gnus-kill-all-overlays)
2433               (when (gnus-request-article article group (current-buffer))
2434                 (when (numberp article)
2435                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2436                   (when gnus-keep-backlog
2437                     (gnus-backlog-enter-article
2438                      group article (current-buffer))))
2439                 'article)))
2440            ;; It was a pseudo.
2441            (t article)))
2442
2443       ;; Take the article from the original article buffer
2444       ;; and place it in the buffer it's supposed to be in.
2445       (when (and (get-buffer gnus-article-buffer)
2446                  ;;(numberp article)
2447                  (equal (buffer-name (current-buffer))
2448                         (buffer-name (get-buffer gnus-article-buffer))))
2449         (save-excursion
2450           (if (get-buffer gnus-original-article-buffer)
2451               (set-buffer (get-buffer gnus-original-article-buffer))
2452             (set-buffer (get-buffer-create gnus-original-article-buffer))
2453             (buffer-disable-undo (current-buffer))
2454             (setq major-mode 'gnus-original-article-mode)
2455             (setq buffer-read-only t)
2456             (gnus-add-current-to-buffer-list))
2457           (let (buffer-read-only)
2458             (erase-buffer)
2459             (insert-buffer-substring gnus-article-buffer))
2460           (setq gnus-original-article (cons group article))))
2461
2462       ;; Update sparse articles.
2463       (when (and do-update-line
2464                  (or (numberp article)
2465                      (stringp article)))
2466         (let ((buf (current-buffer)))
2467           (set-buffer gnus-summary-buffer)
2468           (gnus-summary-update-article do-update-line)
2469           (gnus-summary-goto-subject do-update-line nil t)
2470           (set-window-point (get-buffer-window (current-buffer) t)
2471                             (point))
2472           (set-buffer buf))))))
2473
2474 ;;;
2475 ;;; Article editing
2476 ;;;
2477
2478 (defcustom gnus-article-edit-mode-hook nil
2479   "Hook run in article edit mode buffers."
2480   :group 'gnus-article-various
2481   :type 'hook)
2482
2483 (defvar gnus-article-edit-done-function nil)
2484
2485 (defvar gnus-article-edit-mode-map nil)
2486
2487 (unless gnus-article-edit-mode-map
2488   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2489
2490   (gnus-define-keys gnus-article-edit-mode-map
2491     "\C-c\C-c" gnus-article-edit-done
2492     "\C-c\C-k" gnus-article-edit-exit)
2493
2494   (gnus-define-keys (gnus-article-edit-wash-map
2495                      "\C-c\C-w" gnus-article-edit-mode-map)
2496     "f" gnus-article-edit-full-stops))
2497
2498 (defun gnus-article-edit-mode ()
2499   "Major mode for editing articles.
2500 This is an extended text-mode.
2501
2502 \\{gnus-article-edit-mode-map}"
2503   (interactive)
2504   (kill-all-local-variables)
2505   (setq major-mode 'gnus-article-edit-mode)
2506   (setq mode-name "Article Edit")
2507   (use-local-map gnus-article-edit-mode-map)
2508   (make-local-variable 'gnus-article-edit-done-function)
2509   (make-local-variable 'gnus-prev-winconf)
2510   (setq buffer-read-only nil)
2511   (buffer-enable-undo)
2512   (widen)
2513   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
2514
2515 (defun gnus-article-edit (&optional force)
2516   "Edit the current article.
2517 This will have permanent effect only in mail groups.
2518 If FORCE is non-nil, allow editing of articles even in read-only
2519 groups."
2520   (interactive "P")
2521   (when (and (not force)
2522              (gnus-group-read-only-p))
2523     (error "The current newsgroup does not support article editing"))
2524   (gnus-article-edit-article
2525    `(lambda (no-highlight)
2526       (gnus-summary-edit-article-done
2527        ,(or (mail-header-references gnus-current-headers) "")
2528        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2529
2530 (defun gnus-article-edit-article (exit-func)
2531   "Start editing the contents of the current article buffer."
2532   (let ((winconf (current-window-configuration)))
2533     (set-buffer gnus-article-buffer)
2534     (gnus-article-edit-mode)
2535     (gnus-set-text-properties (point-min) (point-max) nil)
2536     (gnus-configure-windows 'edit-article)
2537     (setq gnus-article-edit-done-function exit-func)
2538     (setq gnus-prev-winconf winconf)
2539     (gnus-message 6 "C-c C-c to end edits")))
2540
2541 (defun gnus-article-edit-done (&optional arg)
2542   "Update the article edits and exit."
2543   (interactive "P")
2544   (let ((func gnus-article-edit-done-function)
2545         (buf (current-buffer))
2546         (start (window-start)))
2547     (gnus-article-edit-exit)
2548     (save-excursion
2549       (set-buffer buf)
2550       (let ((buffer-read-only nil))
2551         (funcall func arg)))
2552     (set-buffer buf)
2553     (set-window-start (get-buffer-window buf) start)
2554     (set-window-point (get-buffer-window buf) (point))))
2555
2556 (defun gnus-article-edit-exit ()
2557   "Exit the article editing without updating."
2558   (interactive)
2559   ;; We remove all text props from the article buffer.
2560   (let ((buf (format "%s" (buffer-string)))
2561         (curbuf (current-buffer))
2562         (p (point))
2563         (window-start (window-start)))
2564     (erase-buffer)
2565     (insert buf)
2566     (let ((winconf gnus-prev-winconf))
2567       (gnus-article-mode)
2568       ;; The cache and backlog have to be flushed somewhat.
2569       (when gnus-use-cache
2570         (gnus-cache-update-article
2571          (car gnus-article-current) (cdr gnus-article-current)))
2572       (when gnus-keep-backlog
2573         (gnus-backlog-remove-article
2574          (car gnus-article-current) (cdr gnus-article-current)))
2575       ;; Flush original article as well.
2576       (save-excursion
2577         (when (get-buffer gnus-original-article-buffer)
2578           (set-buffer gnus-original-article-buffer)
2579           (setq gnus-original-article nil)))
2580       (set-window-configuration winconf)
2581       ;; Tippy-toe some to make sure that point remains where it was.
2582       (let ((buf (current-buffer)))
2583         (set-buffer curbuf)
2584         (set-window-start (get-buffer-window (current-buffer)) window-start)
2585         (goto-char p)
2586         (set-buffer buf)))))
2587
2588 (defun gnus-article-edit-full-stops ()
2589   "Interactively repair spacing at end of sentences."
2590   (interactive)
2591   (save-excursion
2592     (goto-char (point-min))
2593     (search-forward-regexp "^$" nil t)
2594     (let ((case-fold-search nil))
2595       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2596
2597 ;;;
2598 ;;; Article highlights
2599 ;;;
2600
2601 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2602
2603 ;;; Internal Variables:
2604
2605 (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\\)"
2606   "Regular expression that matches URLs."
2607   :group 'gnus-article-buttons
2608   :type 'regexp)
2609
2610 (defcustom gnus-button-alist
2611   `(("<\\(url: ?\\)?news:\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2612      gnus-button-message-id 2)
2613     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2614     ("\\(\\b<\\(url: ?\\)?news:\\(//\\)?\\([^>\n\t ]*\\)>\\)" 1 t
2615      gnus-button-fetch-group 4)
2616     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2617     ("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2618      t gnus-button-message-id 3)
2619     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2620     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2621     ;; This is how URLs _should_ be embedded in text...
2622     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2623     ;; Raw URLs.
2624     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2625   "Alist of regexps matching buttons in article bodies.
2626
2627 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2628 REGEXP: is the string matching text around the button,
2629 BUTTON: is the number of the regexp grouping actually matching the button,
2630 FORM: is a lisp expression which must eval to true for the button to
2631 be added,
2632 CALLBACK: is the function to call when the user push this button, and each
2633 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2634
2635 CALLBACK can also be a variable, in that case the value of that
2636 variable it the real callback function."
2637   :group 'gnus-article-buttons
2638   :type '(repeat (list regexp
2639                        (integer :tag "Button")
2640                        (sexp :tag "Form")
2641                        (function :tag "Callback")
2642                        (repeat :tag "Par"
2643                                :inline t
2644                                (integer :tag "Regexp group")))))
2645
2646 (defcustom gnus-header-button-alist
2647   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2648      0 t gnus-button-message-id 0)
2649     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2650     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2651      0 t gnus-button-mailto 0)
2652     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2653     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2654     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2655     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2656      gnus-button-message-id 3))
2657   "Alist of headers and regexps to match buttons in article heads.
2658
2659 This alist is very similar to `gnus-button-alist', except that each
2660 alist has an additional HEADER element first in each entry:
2661
2662 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2663
2664 HEADER is a regexp to match a header.  For a fuller explanation, see
2665 `gnus-button-alist'."
2666   :group 'gnus-article-buttons
2667   :group 'gnus-article-headers
2668   :type '(repeat (list (regexp :tag "Header")
2669                        regexp
2670                        (integer :tag "Button")
2671                        (sexp :tag "Form")
2672                        (function :tag "Callback")
2673                        (repeat :tag "Par"
2674                                :inline t
2675                                (integer :tag "Regexp group")))))
2676
2677 (defvar gnus-button-regexp nil)
2678 (defvar gnus-button-marker-list nil)
2679 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2680
2681 (defvar gnus-button-last nil)
2682 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2683
2684 ;;; Commands:
2685
2686 (defun gnus-article-push-button (event)
2687   "Check text under the mouse pointer for a callback function.
2688 If the text under the mouse pointer has a `gnus-callback' property,
2689 call it with the value of the `gnus-data' text property."
2690   (interactive "e")
2691   (set-buffer (window-buffer (posn-window (event-start event))))
2692   (let* ((pos (posn-point (event-start event)))
2693          (data (get-text-property pos 'gnus-data))
2694          (fun (get-text-property pos 'gnus-callback)))
2695     (when fun
2696       (funcall fun data))))
2697
2698 (defun gnus-article-press-button ()
2699   "Check text at point for a callback function.
2700 If the text at point has a `gnus-callback' property,
2701 call it with the value of the `gnus-data' text property."
2702   (interactive)
2703   (let* ((data (get-text-property (point) 'gnus-data))
2704          (fun (get-text-property (point) 'gnus-callback)))
2705     (when fun
2706       (funcall fun data))))
2707
2708 (defun gnus-article-prev-button (n)
2709   "Move point to N buttons backward.
2710 If N is negative, move forward instead."
2711   (interactive "p")
2712   (gnus-article-next-button (- n)))
2713
2714 (defun gnus-article-next-button (n)
2715   "Move point to N buttons forward.
2716 If N is negative, move backward instead."
2717   (interactive "p")
2718   (let ((function (if (< n 0) 'previous-single-property-change
2719                     'next-single-property-change))
2720         (inhibit-point-motion-hooks t)
2721         (backward (< n 0))
2722         (limit (if (< n 0) (point-min) (point-max))))
2723     (setq n (abs n))
2724     (while (and (not (= limit (point)))
2725                 (> n 0))
2726       ;; Skip past the current button.
2727       (when (get-text-property (point) 'gnus-callback)
2728         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2729       ;; Go to the next (or previous) button.
2730       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2731       ;; Put point at the start of the button.
2732       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2733         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2734       ;; Skip past intangible buttons.
2735       (when (get-text-property (point) 'intangible)
2736         (incf n))
2737       (decf n))
2738     (unless (zerop n)
2739       (gnus-message 5 "No more buttons"))
2740     n))
2741
2742 (defun gnus-article-highlight (&optional force)
2743   "Highlight current article.
2744 This function calls `gnus-article-highlight-headers',
2745 `gnus-article-highlight-citation',
2746 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2747 do the highlighting.  See the documentation for those functions."
2748   (interactive (list 'force))
2749   (gnus-article-highlight-headers)
2750   (gnus-article-highlight-citation force)
2751   (gnus-article-highlight-signature)
2752   (gnus-article-add-buttons force)
2753   (gnus-article-add-buttons-to-head))
2754
2755 (defun gnus-article-highlight-some (&optional force)
2756   "Highlight current article.
2757 This function calls `gnus-article-highlight-headers',
2758 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2759 do the highlighting.  See the documentation for those functions."
2760   (interactive (list 'force))
2761   (gnus-article-highlight-headers)
2762   (gnus-article-highlight-signature)
2763   (gnus-article-add-buttons))
2764
2765 (defun gnus-article-highlight-headers ()
2766   "Highlight article headers as specified by `gnus-header-face-alist'."
2767   (interactive)
2768   (save-excursion
2769     (set-buffer gnus-article-buffer)
2770     (save-restriction
2771       (let ((alist gnus-header-face-alist)
2772             (buffer-read-only nil)
2773             (case-fold-search t)
2774             (inhibit-point-motion-hooks t)
2775             entry regexp header-face field-face from hpoints fpoints)
2776         (message-narrow-to-head)
2777         (while (setq entry (pop alist))
2778           (goto-char (point-min))
2779           (setq regexp (concat "^\\("
2780                                (if (string-equal "" (nth 0 entry))
2781                                    "[^\t ]"
2782                                  (nth 0 entry))
2783                                "\\)")
2784                 header-face (nth 1 entry)
2785                 field-face (nth 2 entry))
2786           (while (and (re-search-forward regexp nil t)
2787                       (not (eobp)))
2788             (beginning-of-line)
2789             (setq from (point))
2790             (unless (search-forward ":" nil t)
2791               (forward-char 1))
2792             (when (and header-face
2793                        (not (memq (point) hpoints)))
2794               (push (point) hpoints)
2795               (gnus-put-text-property from (point) 'face header-face))
2796             (when (and field-face
2797                        (not (memq (setq from (point)) fpoints)))
2798               (push from fpoints)
2799               (if (re-search-forward "^[^ \t]" nil t)
2800                   (forward-char -2)
2801                 (goto-char (point-max)))
2802               (gnus-put-text-property from (point) 'face field-face))))))))
2803
2804 (defun gnus-article-highlight-signature ()
2805   "Highlight the signature in an article.
2806 It does this by highlighting everything after
2807 `gnus-signature-separator' using `gnus-signature-face'."
2808   (interactive)
2809   (save-excursion
2810     (set-buffer gnus-article-buffer)
2811     (let ((buffer-read-only nil)
2812           (inhibit-point-motion-hooks t))
2813       (save-restriction
2814         (when (and gnus-signature-face
2815                    (gnus-article-narrow-to-signature))
2816           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2817                             'face gnus-signature-face)
2818           (widen)
2819           (gnus-article-search-signature)
2820           (let ((start (match-beginning 0))
2821                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2822             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2823                                      end)))))))
2824
2825 (defun gnus-button-in-region-p (b e prop)
2826   "Say whether PROP exists in the region."
2827   (text-property-not-all b e prop nil))
2828
2829 (defun gnus-article-add-buttons (&optional force)
2830   "Find external references in the article and make buttons of them.
2831 \"External references\" are things like Message-IDs and URLs, as
2832 specified by `gnus-button-alist'."
2833   (interactive (list 'force))
2834   (save-excursion
2835     (set-buffer gnus-article-buffer)
2836     (let ((buffer-read-only nil)
2837           (inhibit-point-motion-hooks t)
2838           (case-fold-search t)
2839           (alist gnus-button-alist)
2840           beg entry regexp)
2841       ;; Remove all old markers.
2842       (let (marker entry)
2843         (while (setq marker (pop gnus-button-marker-list))
2844           (goto-char marker)
2845           (when (setq entry (gnus-button-entry))
2846             (put-text-property (match-beginning (nth 1 entry))
2847                                (match-end (nth 1 entry))
2848                                'gnus-callback nil))
2849           (set-marker marker nil)))
2850       ;; We skip the headers.
2851       (goto-char (point-min))
2852       (unless (search-forward "\n\n" nil t)
2853         (goto-char (point-max)))
2854       (setq beg (point))
2855       (while (setq entry (pop alist))
2856         (setq regexp (car entry))
2857         (goto-char beg)
2858         (while (re-search-forward regexp nil t)
2859           (let* ((start (and entry (match-beginning (nth 1 entry))))
2860                  (end (and entry (match-end (nth 1 entry))))
2861                  (from (match-beginning 0)))
2862             (when (and (or (eq t (nth 2 entry))
2863                            (eval (nth 2 entry)))
2864                        (not (gnus-button-in-region-p
2865                              start end 'gnus-callback)))
2866               ;; That optional form returned non-nil, so we add the
2867               ;; button.
2868               (gnus-article-add-button
2869                start end 'gnus-button-push
2870                (car (push (set-marker (make-marker) from)
2871                           gnus-button-marker-list))))))))))
2872
2873 ;; Add buttons to the head of an article.
2874 (defun gnus-article-add-buttons-to-head ()
2875   "Add buttons to the head of the article."
2876   (interactive)
2877   (save-excursion
2878     (set-buffer gnus-article-buffer)
2879     (let ((buffer-read-only nil)
2880           (inhibit-point-motion-hooks t)
2881           (case-fold-search t)
2882           (alist gnus-header-button-alist)
2883           entry beg end)
2884       (nnheader-narrow-to-headers)
2885       (while alist
2886         ;; Each alist entry.
2887         (setq entry (car alist)
2888               alist (cdr alist))
2889         (goto-char (point-min))
2890         (while (re-search-forward (car entry) nil t)
2891           ;; Each header matching the entry.
2892           (setq beg (match-beginning 0))
2893           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2894                              (match-beginning 0))
2895                         (point-max)))
2896           (goto-char beg)
2897           (while (re-search-forward (nth 1 entry) end t)
2898             ;; Each match within a header.
2899             (let* ((entry (cdr entry))
2900                    (start (match-beginning (nth 1 entry)))
2901                    (end (match-end (nth 1 entry)))
2902                    (form (nth 2 entry)))
2903               (goto-char (match-end 0))
2904               (when (eval form)
2905                 (gnus-article-add-button
2906                  start end (nth 3 entry)
2907                  (buffer-substring (match-beginning (nth 4 entry))
2908                                    (match-end (nth 4 entry)))))))
2909           (goto-char end))))
2910     (widen)))
2911
2912 ;;; External functions:
2913
2914 (defun gnus-article-add-button (from to fun &optional data)
2915   "Create a button between FROM and TO with callback FUN and data DATA."
2916   (when gnus-article-button-face
2917     (gnus-overlay-put (gnus-make-overlay from to)
2918                       'face gnus-article-button-face))
2919   (gnus-add-text-properties
2920    from to
2921    (nconc (and gnus-article-mouse-face
2922                (list gnus-mouse-face-prop gnus-article-mouse-face))
2923           (list 'gnus-callback fun)
2924           (and data (list 'gnus-data data)))))
2925
2926 ;;; Internal functions:
2927
2928 (defun gnus-article-set-globals ()
2929   (save-excursion
2930     (set-buffer gnus-summary-buffer)
2931     (gnus-set-global-variables)))
2932
2933 (defun gnus-signature-toggle (end)
2934   (save-excursion
2935     (set-buffer gnus-article-buffer)
2936     (let ((buffer-read-only nil)
2937           (inhibit-point-motion-hooks t))
2938       (if (get-text-property end 'invisible)
2939           (gnus-article-unhide-text end (point-max))
2940         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
2941
2942 (defun gnus-button-entry ()
2943   ;; Return the first entry in `gnus-button-alist' matching this place.
2944   (let ((alist gnus-button-alist)
2945         (entry nil))
2946     (while alist
2947       (setq entry (pop alist))
2948       (if (looking-at (car entry))
2949           (setq alist nil)
2950         (setq entry nil)))
2951     entry))
2952
2953 (defun gnus-button-push (marker)
2954   ;; Push button starting at MARKER.
2955   (save-excursion
2956     (set-buffer gnus-article-buffer)
2957     (goto-char marker)
2958     (let* ((entry (gnus-button-entry))
2959            (inhibit-point-motion-hooks t)
2960            (fun (nth 3 entry))
2961            (args (mapcar (lambda (group)
2962                            (let ((string (match-string group)))
2963                              (gnus-set-text-properties
2964                               0 (length string) nil string)
2965                              string))
2966                          (nthcdr 4 entry))))
2967       (cond
2968        ((fboundp fun)
2969         (apply fun args))
2970        ((and (boundp fun)
2971              (fboundp (symbol-value fun)))
2972         (apply (symbol-value fun) args))
2973        (t
2974         (gnus-message 1 "You must define `%S' to use this button"
2975                       (cons fun args)))))))
2976
2977 (defun gnus-button-message-id (message-id)
2978   "Fetch MESSAGE-ID."
2979   (save-excursion
2980     (set-buffer gnus-summary-buffer)
2981     (gnus-summary-refer-article message-id)))
2982
2983 (defun gnus-button-fetch-group (address)
2984   "Fetch GROUP specified by ADDRESS."
2985   (if (not (string-match "[:/]" address))
2986       ;; This is just a simple group url.
2987       (gnus-group-read-ephemeral-group address gnus-select-method)
2988     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
2989                            address))
2990         (error "Can't parse %s" address)
2991       (gnus-group-read-ephemeral-group
2992        (match-string 4 address)
2993        `(nntp ,(match-string 1 address)
2994               (nntp-address ,(match-string 1 address))
2995               (nntp-port-number ,(if (match-end 3)
2996                                      (match-string 3 address)
2997                                    "nntp")))))))
2998
2999 (defun gnus-split-string (string pattern)
3000   "Return a list of substrings of STRING which are separated by PATTERN."
3001   (let (parts (start 0))
3002     (while (string-match pattern string start)
3003       (setq parts (cons (substring string start (match-beginning 0)) parts)
3004             start (match-end 0)))
3005     (nreverse (cons (substring string start) parts))))
3006
3007 (defun gnus-url-parse-query-string (query &optional downcase)
3008   (let (retval pairs cur key val)
3009     (setq pairs (gnus-split-string query "&"))
3010     (while pairs
3011       (setq cur (car pairs)
3012             pairs (cdr pairs))
3013       (if (not (string-match "=" cur))
3014           nil                           ; Grace
3015         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3016               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3017         (if downcase
3018             (setq key (downcase key)))
3019         (setq cur (assoc key retval))
3020         (if cur
3021             (setcdr cur (cons val (cdr cur)))
3022           (setq retval (cons (list key val) retval)))))
3023     retval))
3024
3025 (defun gnus-url-unhex (x)
3026   (if (> x ?9)
3027       (if (>= x ?a)
3028           (+ 10 (- x ?a))
3029         (+ 10 (- x ?A)))
3030     (- x ?0)))
3031
3032 (defun gnus-url-unhex-string (str &optional allow-newlines)
3033   "Remove %XXX embedded spaces, etc in a url.
3034 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3035 decoding of carriage returns and line feeds in the string, which is normally
3036 forbidden in URL encoding."
3037   (setq str (or str ""))
3038   (let ((tmp "")
3039         (case-fold-search t))
3040     (while (string-match "%[0-9a-f][0-9a-f]" str)
3041       (let* ((start (match-beginning 0))
3042              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3043              (code (+ (* 16 ch1)
3044                       (gnus-url-unhex (elt str (+ start 2))))))
3045         (setq tmp (concat
3046                    tmp (substring str 0 start)
3047                    (cond
3048                     (allow-newlines
3049                      (char-to-string code))
3050                     ((or (= code ?\n) (= code ?\r))
3051                      " ")
3052                     (t (char-to-string code))))
3053               str (substring str (match-end 0)))))
3054     (setq tmp (concat tmp str))
3055     tmp))
3056
3057 (defun gnus-url-mailto (url)
3058   ;; Send mail to someone
3059   (when (string-match "mailto:/*\\(.*\\)" url)
3060     (setq url (substring url (match-beginning 1) nil)))
3061   (let (to args source-url subject func)
3062     (if (string-match (regexp-quote "?") url)
3063         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3064               args (gnus-url-parse-query-string
3065                     (substring url (match-end 0) nil) t))
3066       (setq to (gnus-url-unhex-string url)))
3067     (setq args (cons (list "to" to) args)
3068           subject (cdr-safe (assoc "subject" args)))
3069     (message-mail)
3070     (while args
3071       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3072       (if (fboundp func)
3073           (funcall func)
3074         (message-position-on-field (caar args)))
3075       (insert (mapconcat 'identity (cdar args) ", "))
3076       (setq args (cdr args)))
3077     (if subject
3078         (message-goto-body)
3079       (message-goto-subject))))
3080
3081 (defun gnus-button-mailto (address)
3082   ;; Mail to ADDRESS.
3083   (set-buffer (gnus-copy-article-buffer))
3084   (message-reply address))
3085
3086 (defun gnus-button-reply (address)
3087   ;; Reply to ADDRESS.
3088   (message-reply address))
3089
3090 (defun gnus-button-url (address)
3091   "Browse ADDRESS."
3092   (funcall browse-url-browser-function address))
3093
3094 (defun gnus-button-embedded-url (address)
3095   "Browse ADDRESS."
3096   (funcall browse-url-browser-function (gnus-strip-whitespace address)))
3097
3098 ;;; Next/prev buttons in the article buffer.
3099
3100 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3101 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3102
3103 (defvar gnus-prev-page-map nil)
3104 (unless gnus-prev-page-map
3105   (setq gnus-prev-page-map (make-sparse-keymap))
3106   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3107   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3108
3109 (defun gnus-insert-prev-page-button ()
3110   (let ((buffer-read-only nil))
3111     (gnus-eval-format
3112      gnus-prev-page-line-format nil
3113      `(gnus-prev t local-map ,gnus-prev-page-map
3114                  gnus-callback gnus-article-button-prev-page))))
3115
3116 (defvar gnus-next-page-map nil)
3117 (unless gnus-next-page-map
3118   (setq gnus-next-page-map (make-keymap))
3119   (suppress-keymap gnus-prev-page-map)
3120   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3121   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3122
3123 (defun gnus-button-next-page ()
3124   "Go to the next page."
3125   (interactive)
3126   (let ((win (selected-window)))
3127     (select-window (get-buffer-window gnus-article-buffer t))
3128     (gnus-article-next-page)
3129     (select-window win)))
3130
3131 (defun gnus-button-prev-page ()
3132   "Go to the prev page."
3133   (interactive)
3134   (let ((win (selected-window)))
3135     (select-window (get-buffer-window gnus-article-buffer t))
3136     (gnus-article-prev-page)
3137     (select-window win)))
3138
3139 (defun gnus-insert-next-page-button ()
3140   (let ((buffer-read-only nil))
3141     (gnus-eval-format gnus-next-page-line-format nil
3142                       `(gnus-next t local-map ,gnus-next-page-map
3143                                   gnus-callback
3144                                   gnus-article-button-next-page))))
3145
3146 (defun gnus-article-button-next-page (arg)
3147   "Go to the next page."
3148   (interactive "P")
3149   (let ((win (selected-window)))
3150     (select-window (get-buffer-window gnus-article-buffer t))
3151     (gnus-article-next-page)
3152     (select-window win)))
3153
3154 (defun gnus-article-button-prev-page (arg)
3155   "Go to the prev page."
3156   (interactive "P")
3157   (let ((win (selected-window)))
3158     (select-window (get-buffer-window gnus-article-buffer t))
3159     (gnus-article-prev-page)
3160     (select-window win)))
3161
3162 (gnus-ems-redefine)
3163
3164 (provide 'gnus-art)
3165
3166 (run-hooks 'gnus-art-load-hook)
3167
3168 ;;; gnus-art.el ends here