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