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