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