*** empty log message ***
[gnus] / 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 (defvar gnus-article-current-summary nil)
538
539 (defvar gnus-article-mode-syntax-table
540   (let ((table (copy-syntax-table text-mode-syntax-table)))
541     (modify-syntax-entry ?- "w" table)
542     (modify-syntax-entry ?> ")" table)
543     (modify-syntax-entry ?< "(" table)
544     table)
545   "Syntax table used in article mode buffers.
546 Initialized from `text-mode-syntax-table.")
547
548 (defvar gnus-save-article-buffer nil)
549
550 (defvar gnus-article-mode-line-format-alist
551   (nconc '((?w (gnus-article-wash-status) ?s))
552          gnus-summary-mode-line-format-alist))
553
554 (defvar gnus-number-of-articles-to-be-saved nil)
555
556 (defvar gnus-inhibit-hiding nil)
557
558 (defsubst gnus-article-hide-text (b e props)
559   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
560   (add-text-properties b e props)
561   (when (memq 'intangible props)
562     (put-text-property
563      (max (1- b) (point-min))
564      b 'intangible (cddr (memq 'intangible props)))))
565
566 (defsubst gnus-article-unhide-text (b e)
567   "Remove hidden text properties from region between B and E."
568   (remove-text-properties b e gnus-hidden-properties)
569   (when (memq 'intangible gnus-hidden-properties)
570     (put-text-property (max (1- b) (point-min))
571                        b 'intangible nil)))
572
573 (defun gnus-article-hide-text-type (b e type)
574   "Hide text of TYPE between B and E."
575   (gnus-article-hide-text
576    b e (cons 'article-type (cons type gnus-hidden-properties))))
577
578 (defun gnus-article-unhide-text-type (b e type)
579   "Hide text of TYPE between B and E."
580   (remove-text-properties
581    b e (cons 'article-type (cons type gnus-hidden-properties)))
582   (when (memq 'intangible gnus-hidden-properties)
583     (put-text-property (max (1- b) (point-min))
584                        b 'intangible nil)))
585
586 (defun gnus-article-hide-text-of-type (type)
587   "Hide text of TYPE in the current buffer."
588   (save-excursion
589     (let ((b (point-min))
590           (e (point-max)))
591       (while (setq b (text-property-any b e 'article-type type))
592         (add-text-properties b (incf b) gnus-hidden-properties)))))
593
594 (defun gnus-article-delete-text-of-type (type)
595   "Delete text of TYPE in the current buffer."
596   (save-excursion
597     (let ((b (point-min)))
598       (while (setq b (text-property-any b (point-max) 'article-type type))
599         (delete-region
600          b (or (text-property-not-all b (point-max) 'article-type type)
601                (point-max)))))))
602
603 (defun gnus-article-delete-invisible-text ()
604   "Delete all invisible text in the current buffer."
605   (save-excursion
606     (let ((b (point-min)))
607       (while (setq b (text-property-any b (point-max) 'invisible t))
608         (delete-region
609          b (or (text-property-not-all b (point-max) 'invisible t)
610                (point-max)))))))
611
612 (defun gnus-article-text-type-exists-p (type)
613   "Say whether any text of type TYPE exists in the buffer."
614   (text-property-any (point-min) (point-max) 'article-type type))
615
616 (defsubst gnus-article-header-rank ()
617   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
618   (let ((list gnus-sorted-header-list)
619         (i 0))
620     (while list
621       (when (looking-at (car list))
622         (setq list nil))
623       (setq list (cdr list))
624       (incf i))
625     i))
626
627 (defun article-hide-headers (&optional arg delete)
628   "Toggle whether to hide unwanted headers and possibly sort them as well.
629 If given a negative prefix, always show; if given a positive prefix,
630 always hide."
631   (interactive (gnus-article-hidden-arg))
632   (current-buffer)
633   (if (gnus-article-check-hidden-text 'headers arg)
634       ;; Show boring headers as well.
635       (gnus-article-show-hidden-text 'boring-headers)
636     ;; This function might be inhibited.
637     (unless gnus-inhibit-hiding
638       (save-excursion
639         (save-restriction
640           (let ((buffer-read-only nil)
641                 (props (nconc (list 'article-type 'headers)
642                               gnus-hidden-properties))
643                 (max (1+ (length gnus-sorted-header-list)))
644                 (ignored (when (not gnus-visible-headers)
645                            (cond ((stringp gnus-ignored-headers)
646                                   gnus-ignored-headers)
647                                  ((listp gnus-ignored-headers)
648                                   (mapconcat 'identity gnus-ignored-headers
649                                              "\\|")))))
650                 (visible
651                  (cond ((stringp gnus-visible-headers)
652                         gnus-visible-headers)
653                        ((and gnus-visible-headers
654                              (listp gnus-visible-headers))
655                         (mapconcat 'identity gnus-visible-headers "\\|"))))
656                 (inhibit-point-motion-hooks t)
657                 want-list beg)
658             ;; First we narrow to just the headers.
659             (widen)
660             (goto-char (point-min))
661             ;; Hide any "From " lines at the beginning of (mail) articles.
662             (while (looking-at "From ")
663               (forward-line 1))
664             (unless (bobp)
665               (if delete
666                   (delete-region (point-min) (point))
667                 (gnus-article-hide-text (point-min) (point) props)))
668             ;; Then treat the rest of the header lines.
669             (narrow-to-region
670              (point)
671              (if (search-forward "\n\n" nil t) ; if there's a body
672                  (progn (forward-line -1) (point))
673                (point-max)))
674             ;; Then we use the two regular expressions
675             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
676             ;; select which header lines is to remain visible in the
677             ;; article buffer.
678             (goto-char (point-min))
679             (while (re-search-forward "^[^ \t]*:" nil t)
680               (beginning-of-line)
681               ;; Mark the rank of the header.
682               (put-text-property
683                (point) (1+ (point)) 'message-rank
684                (if (or (and visible (looking-at visible))
685                        (and ignored
686                             (not (looking-at ignored))))
687                    (gnus-article-header-rank)
688                  (+ 2 max)))
689               (forward-line 1))
690             (message-sort-headers-1)
691             (when (setq beg (text-property-any
692                              (point-min) (point-max) 'message-rank (+ 2 max)))
693               ;; We make the unwanted headers invisible.
694               (if delete
695                   (delete-region beg (point-max))
696                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
697                 (gnus-article-hide-text-type beg (point-max) 'headers))
698               ;; Work around XEmacs lossage.
699               (put-text-property (point-min) beg 'invisible nil))))))))
700
701 (defun article-hide-boring-headers (&optional arg)
702   "Toggle hiding of headers that aren't very interesting.
703 If given a negative prefix, always show; if given a positive prefix,
704 always hide."
705   (interactive (gnus-article-hidden-arg))
706   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
707              (not gnus-show-all-headers))
708     (save-excursion
709       (save-restriction
710         (let ((buffer-read-only nil)
711               (list gnus-boring-article-headers)
712               (inhibit-point-motion-hooks t)
713               elem)
714           (nnheader-narrow-to-headers)
715           (while list
716             (setq elem (pop list))
717             (goto-char (point-min))
718             (cond
719              ;; Hide empty headers.
720              ((eq elem 'empty)
721               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
722                 (forward-line -1)
723                 (gnus-article-hide-text-type
724                  (progn (beginning-of-line) (point))
725                  (progn
726                    (end-of-line)
727                    (if (re-search-forward "^[^ \t]" nil t)
728                        (match-beginning 0)
729                      (point-max)))
730                  'boring-headers)))
731              ;; Hide boring Newsgroups header.
732              ((eq elem 'newsgroups)
733               (when (equal (gnus-fetch-field "newsgroups")
734                            (gnus-group-real-name
735                             (if (boundp 'gnus-newsgroup-name)
736                                 gnus-newsgroup-name
737                               "")))
738                 (gnus-article-hide-header "newsgroups")))
739              ((eq elem 'followup-to)
740               (when (equal (message-fetch-field "followup-to")
741                            (message-fetch-field "newsgroups"))
742                 (gnus-article-hide-header "followup-to")))
743              ((eq elem 'reply-to)
744               (let ((from (message-fetch-field "from"))
745                     (reply-to (message-fetch-field "reply-to")))
746                 (when (and
747                        from reply-to
748                        (ignore-errors
749                          (equal
750                           (nth 1 (mail-extract-address-components from))
751                           (nth 1 (mail-extract-address-components reply-to)))))
752                   (gnus-article-hide-header "reply-to"))))
753              ((eq elem 'date)
754               (let ((date (message-fetch-field "date")))
755                 (when (and date
756                            (< (gnus-days-between (current-time-string) date)
757                               4))
758                   (gnus-article-hide-header "date"))))
759              ((eq elem 'long-to)
760               (let ((to (message-fetch-field "to")))
761                 (when (> (length to) 1024)
762                   (gnus-article-hide-header "to"))))
763              ((eq elem 'many-to)
764               (let ((to-count 0))
765                 (goto-char (point-min))
766                 (while (re-search-forward "^to:" nil t)
767                   (setq to-count (1+ to-count)))
768                 (when (> to-count 1)
769                   (while (> to-count 0)
770                     (goto-char (point-min))
771                     (save-restriction
772                       (re-search-forward "^to:" nil nil to-count)
773                       (forward-line -1)
774                       (narrow-to-region (point) (point-max))
775                       (gnus-article-hide-header "to"))
776                     (setq to-count (1- to-count)))))))))))))
777
778 (defun gnus-article-hide-header (header)
779   (save-excursion
780     (goto-char (point-min))
781     (when (re-search-forward (concat "^" header ":") nil t)
782       (gnus-article-hide-text-type
783        (progn (beginning-of-line) (point))
784        (progn
785          (end-of-line)
786          (if (re-search-forward "^[^ \t]" nil t)
787              (match-beginning 0)
788            (point-max)))
789        'boring-headers))))
790
791 (defun article-treat-dumbquotes ()
792   "Translate M******** sm*rtq**t*s into proper text."
793   (interactive)
794   (article-translate-characters "\221\222\223\223" "`'\"\""))
795
796 (defun article-translate-characters (from to)
797   "Translate all characters in the body of the article according to FROM and TO.
798 FROM is a string of characters to translate from; to is a string of
799 characters to translate to."
800   (save-excursion
801     (goto-char (point-min))
802     (when (search-forward "\n\n" nil t)
803       (let ((buffer-read-only nil)
804             (x (make-string 225 ?x))
805             (i -1))
806         (while (< (incf i) (length x))
807           (aset x i i))
808         (setq i 0)
809         (while (< i (length from))
810           (aset x (aref from i) (aref to i))
811           (incf i))
812         (translate-region (point) (point-max) x)))))
813
814 (defun article-treat-overstrike ()
815   "Translate overstrikes into bold text."
816   (interactive)
817   (save-excursion
818     (goto-char (point-min))
819     (when (search-forward "\n\n" nil t)
820       (let ((buffer-read-only nil))
821         (while (search-forward "\b" nil t)
822           (let ((next (following-char))
823                 (previous (char-after (- (point) 2))))
824             ;; We do the boldification/underlining by hiding the
825             ;; overstrikes and putting the proper text property
826             ;; on the letters.
827             (cond
828              ((eq next previous)
829               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
830               (put-text-property (point) (1+ (point)) 'face 'bold))
831              ((eq next ?_)
832               (gnus-article-hide-text-type
833                (1- (point)) (1+ (point)) 'overstrike)
834               (put-text-property
835                (- (point) 2) (1- (point)) 'face 'underline))
836              ((eq previous ?_)
837               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
838               (put-text-property
839                (point) (1+ (point)) 'face 'underline)))))))))
840
841 (defun article-fill ()
842   "Format too long lines."
843   (interactive)
844   (save-excursion
845     (let ((buffer-read-only nil))
846       (widen)
847       (goto-char (point-min))
848       (search-forward "\n\n" nil t)
849       (end-of-line 1)
850       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
851             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
852             (adaptive-fill-mode t))
853         (while (not (eobp))
854           (and (>= (current-column) (min fill-column (window-width)))
855                (/= (preceding-char) ?:)
856                (fill-paragraph nil))
857           (end-of-line 2))))))
858
859 (defun article-remove-cr ()
860   "Remove carriage returns from an article."
861   (interactive)
862   (save-excursion
863     (let ((buffer-read-only nil))
864       (goto-char (point-min))
865       (while (search-forward "\r" nil t)
866         (replace-match "" t t)))))
867
868 (defun article-remove-trailing-blank-lines ()
869   "Remove all trailing blank lines from the article."
870   (interactive)
871   (save-excursion
872     (let ((buffer-read-only nil))
873       (goto-char (point-max))
874       (delete-region
875        (point)
876        (progn
877          (while (and (not (bobp))
878                      (looking-at "^[ \t]*$"))
879            (forward-line -1))
880          (forward-line 1)
881          (point))))))
882
883 (defun article-display-x-face (&optional force)
884   "Look for an X-Face header and display it if present."
885   (interactive (list 'force))
886   (save-excursion
887     ;; Delete the old process, if any.
888     (when (process-status "article-x-face")
889       (delete-process "article-x-face"))
890     (let ((inhibit-point-motion-hooks t)
891           (case-fold-search t)
892           from)
893       (save-restriction
894         (nnheader-narrow-to-headers)
895         (setq from (message-fetch-field "from"))
896         (goto-char (point-min))
897         (while (and gnus-article-x-face-command
898                     (or force
899                         ;; Check whether this face is censored.
900                         (not gnus-article-x-face-too-ugly)
901                         (and gnus-article-x-face-too-ugly from
902                              (not (string-match gnus-article-x-face-too-ugly
903                                                 from))))
904                     ;; Has to be present.
905                     (re-search-forward "^X-Face: " nil t))
906           ;; We now have the area of the buffer where the X-Face is stored.
907           (save-excursion
908             (let ((beg (point))
909                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
910               ;; We display the face.
911               (if (symbolp gnus-article-x-face-command)
912                   ;; The command is a lisp function, so we call it.
913                   (if (gnus-functionp gnus-article-x-face-command)
914                       (funcall gnus-article-x-face-command beg end)
915                     (error "%s is not a function" gnus-article-x-face-command))
916                 ;; The command is a string, so we interpret the command
917                 ;; as a, well, command, and fork it off.
918                 (let ((process-connection-type nil))
919                   (process-kill-without-query
920                    (start-process
921                     "article-x-face" nil shell-file-name shell-command-switch
922                     gnus-article-x-face-command))
923                   (process-send-region "article-x-face" beg end)
924                   (process-send-eof "article-x-face"))))))))))
925
926 (defun gnus-hack-decode-rfc1522 ()
927   "Emergency hack function for avoiding problems when decoding."
928   (let ((buffer-read-only nil))
929     (goto-char (point-min))
930     ;; Remove encoded TABs.
931     (while (search-forward "=09" nil t)
932       (replace-match " " t t))
933     ;; Remove encoded newlines.
934     (goto-char (point-min))
935     (while (search-forward "=10" nil t)
936       (replace-match " " t t))))
937
938 (defalias 'gnus-decode-rfc1522 'article-decode-rfc1522)
939 (defalias 'gnus-article-decode-rfc1522 'article-decode-rfc1522)
940 (defun article-decode-rfc1522 ()
941   "Hack to remove QP encoding from headers."
942   (let ((case-fold-search t)
943         (inhibit-point-motion-hooks t)
944         (buffer-read-only nil)
945         string)
946     (save-restriction
947       (narrow-to-region
948        (goto-char (point-min))
949        (or (search-forward "\n\n" nil t) (point-max)))
950       (goto-char (point-min))
951       (while (re-search-forward
952               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
953         (setq string (match-string 1))
954         (save-restriction
955           (narrow-to-region (match-beginning 0) (match-end 0))
956           (delete-region (point-min) (point-max))
957           (insert string)
958           (article-mime-decode-quoted-printable
959            (goto-char (point-min)) (point-max))
960           (subst-char-in-region (point-min) (point-max) ?_ ? )
961           (goto-char (point-max)))
962         (goto-char (point-min))))))
963
964 (defun article-de-quoted-unreadable (&optional force)
965   "Do a naive translation of a quoted-printable-encoded article.
966 This is in no way, shape or form meant as a replacement for real MIME
967 processing, but is simply a stop-gap measure until MIME support is
968 written.
969 If FORCE, decode the article whether it is marked as quoted-printable
970 or not."
971   (interactive (list 'force))
972   (save-excursion
973     (let ((case-fold-search t)
974           (buffer-read-only nil)
975           (type (gnus-fetch-field "content-transfer-encoding")))
976       (gnus-article-decode-rfc1522)
977       (when (or force
978                 (and type (string-match "quoted-printable" (downcase type))))
979         (goto-char (point-min))
980         (search-forward "\n\n" nil 'move)
981         (article-mime-decode-quoted-printable (point) (point-max))))))
982
983 (defun article-mime-decode-quoted-printable-buffer ()
984   "Decode Quoted-Printable in the current buffer."
985   (article-mime-decode-quoted-printable (point-min) (point-max)))
986
987 (defun article-mime-decode-quoted-printable (from to)
988   "Decode Quoted-Printable in the region between FROM and TO."
989   (interactive "r")
990   (goto-char from)
991   (while (search-forward "=" to t)
992     (cond ((eq (following-char) ?\n)
993            (delete-char -1)
994            (delete-char 1))
995           ((looking-at "[0-9A-F][0-9A-F]")
996            (subst-char-in-region
997             (1- (point)) (point) ?=
998             (hexl-hex-string-to-integer
999              (buffer-substring (point) (+ 2 (point)))))
1000            (delete-char 2))
1001           ((looking-at "=")
1002            (delete-char 1))
1003           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
1004
1005 (defun article-hide-pgp (&optional arg)
1006   "Toggle hiding of any PGP headers and signatures in the current article.
1007 If given a negative prefix, always show; if given a positive prefix,
1008 always hide."
1009   (interactive (gnus-article-hidden-arg))
1010   (unless (gnus-article-check-hidden-text 'pgp arg)
1011     (save-excursion
1012       (let ((inhibit-point-motion-hooks t)
1013             buffer-read-only beg end)
1014         (widen)
1015         (goto-char (point-min))
1016         ;; Hide the "header".
1017         (when (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1018           (gnus-article-hide-text-type (1+ (match-beginning 0))
1019                                        (match-end 0) 'pgp)
1020           (setq beg (point))
1021           ;; Hide the actual signature.
1022           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1023                (setq end (1+ (match-beginning 0)))
1024                (gnus-article-hide-text-type
1025                 end
1026                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1027                     (match-end 0)
1028                   ;; Perhaps we shouldn't hide to the end of the buffer
1029                   ;; if there is no end to the signature?
1030                   (point-max))
1031                 'pgp))
1032           ;; Hide "- " PGP quotation markers.
1033           (when (and beg end)
1034             (narrow-to-region beg end)
1035             (goto-char (point-min))
1036             (while (re-search-forward "^- " nil t)
1037               (gnus-article-hide-text-type
1038                (match-beginning 0) (match-end 0) 'pgp))
1039             (widen))
1040           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1041
1042 (defun article-hide-pem (&optional arg)
1043   "Toggle hiding of any PEM headers and signatures in the current article.
1044 If given a negative prefix, always show; if given a positive prefix,
1045 always hide."
1046   (interactive (gnus-article-hidden-arg))
1047   (unless (gnus-article-check-hidden-text 'pem arg)
1048     (save-excursion
1049       (let (buffer-read-only end)
1050         (widen)
1051         (goto-char (point-min))
1052         ;; hide the horrendously ugly "header".
1053         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1054                              nil
1055                              t)
1056              (setq end (1+ (match-beginning 0)))
1057              (gnus-article-hide-text-type
1058               end
1059               (if (search-forward "\n\n" nil t)
1060                   (match-end 0)
1061                 (point-max))
1062               'pem))
1063         ;; hide the trailer as well
1064         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1065                              nil
1066                              t)
1067              (gnus-article-hide-text-type
1068               (match-beginning 0) (match-end 0) 'pem))))))
1069
1070 (defun article-hide-signature (&optional arg)
1071   "Hide the signature in the current article.
1072 If given a negative prefix, always show; if given a positive prefix,
1073 always hide."
1074   (interactive (gnus-article-hidden-arg))
1075   (unless (gnus-article-check-hidden-text 'signature arg)
1076     (save-excursion
1077       (save-restriction
1078         (let ((buffer-read-only nil))
1079           (when (gnus-article-narrow-to-signature)
1080             (gnus-article-hide-text-type
1081              (point-min) (point-max) 'signature)))))))
1082
1083 (defun article-strip-leading-blank-lines ()
1084   "Remove all blank lines from the beginning of the article."
1085   (interactive)
1086   (save-excursion
1087     (let ((inhibit-point-motion-hooks t)
1088           buffer-read-only)
1089       (goto-char (point-min))
1090       (when (search-forward "\n\n" nil t)
1091         (while (and (not (eobp))
1092                     (looking-at "[ \t]*$"))
1093           (gnus-delete-line))))))
1094
1095 (defun article-strip-multiple-blank-lines ()
1096   "Replace consecutive blank lines with one empty line."
1097   (interactive)
1098   (save-excursion
1099     (let ((inhibit-point-motion-hooks t)
1100           buffer-read-only)
1101       ;; First make all blank lines empty.
1102       (goto-char (point-min))
1103       (search-forward "\n\n" nil t)
1104       (while (re-search-forward "^[ \t]+$" nil t)
1105         (replace-match "" nil t))
1106       ;; Then replace multiple empty lines with a single empty line.
1107       (goto-char (point-min))
1108       (search-forward "\n\n" nil t)
1109       (while (re-search-forward "\n\n\n+" nil t)
1110         (replace-match "\n\n" t t)))))
1111
1112 (defun article-strip-leading-space ()
1113   "Remove all white space from the beginning of the lines in the article."
1114   (interactive)
1115   (save-excursion
1116     (let ((inhibit-point-motion-hooks t)
1117           buffer-read-only)
1118       (goto-char (point-min))
1119       (search-forward "\n\n" nil t)
1120       (while (re-search-forward "^[ \t]+" nil t)
1121         (replace-match "" t t)))))
1122
1123 (defun article-strip-blank-lines ()
1124   "Strip leading, trailing and multiple blank lines."
1125   (interactive)
1126   (article-strip-leading-blank-lines)
1127   (article-remove-trailing-blank-lines)
1128   (article-strip-multiple-blank-lines))
1129
1130 (defun article-strip-all-blank-lines ()
1131   "Strip all blank lines."
1132   (interactive)
1133   (save-excursion
1134     (let ((inhibit-point-motion-hooks t)
1135           buffer-read-only)
1136       (goto-char (point-min))
1137       (search-forward "\n\n" nil t)
1138       (while (re-search-forward "^[ \t]*\n" nil t)
1139         (replace-match "" t t)))))
1140
1141 (defvar mime::preview/content-list)
1142 (defvar mime::preview-content-info/point-min)
1143 (defun gnus-article-narrow-to-signature ()
1144   "Narrow to the signature; return t if a signature is found, else nil."
1145   (widen)
1146   (when (and (boundp 'mime::preview/content-list)
1147              mime::preview/content-list)
1148     ;; We have a MIMEish article, so we use the MIME data to narrow.
1149     (let ((pcinfo (car (last mime::preview/content-list))))
1150       (ignore-errors
1151         (narrow-to-region
1152          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
1153          (point-max)))))
1154
1155   (when (gnus-article-search-signature)
1156     (forward-line 1)
1157     ;; Check whether we have some limits to what we consider
1158     ;; to be a signature.
1159     (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1160                     (list gnus-signature-limit)))
1161           limit limited)
1162       (while (setq limit (pop limits))
1163         (if (or (and (integerp limit)
1164                      (< (- (point-max) (point)) limit))
1165                 (and (floatp limit)
1166                      (< (count-lines (point) (point-max)) limit))
1167                 (and (gnus-functionp limit)
1168                      (funcall limit))
1169                 (and (stringp limit)
1170                      (not (re-search-forward limit nil t))))
1171             ()                          ; This limit did not succeed.
1172           (setq limited t
1173                 limits nil)))
1174       (unless limited
1175         (narrow-to-region (point) (point-max))
1176         t))))
1177
1178 (defun gnus-article-search-signature ()
1179   "Search the current buffer for the signature separator.
1180 Put point at the beginning of the signature separator."
1181   (let ((cur (point)))
1182     (goto-char (point-max))
1183     (if (if (stringp gnus-signature-separator)
1184             (re-search-backward gnus-signature-separator nil t)
1185           (let ((seps gnus-signature-separator))
1186             (while (and seps
1187                         (not (re-search-backward (car seps) nil t)))
1188               (pop seps))
1189             seps))
1190         t
1191       (goto-char cur)
1192       nil)))
1193
1194 (eval-and-compile
1195   (autoload 'w3-display "w3-parse")
1196   (autoload 'w3-do-setup "w3" "" t)
1197   (autoload 'w3-region "w3-display" "" t))
1198
1199 (defun gnus-article-treat-html ()
1200   "Render HTML."
1201   (interactive)
1202   (let ((cbuf (current-buffer)))
1203     (set-buffer gnus-article-buffer)
1204     (let (buf buffer-read-only b e)
1205       (w3-do-setup)
1206       (goto-char (point-min))
1207       (narrow-to-region
1208        (if (search-forward "\n\n" nil t)
1209            (setq b (point))
1210          (point-max))
1211        (setq e (point-max)))
1212       (nnheader-temp-write nil
1213         (insert-buffer-substring gnus-article-buffer b e)
1214         (require 'url)
1215         (save-window-excursion
1216           (w3-region (point-min) (point-max))
1217           (setq buf (buffer-substring-no-properties (point-min) (point-max)))))
1218       (when buf
1219         (delete-region (point-min) (point-max))
1220         (insert buf))
1221       (widen)
1222       (goto-char (point-min))
1223       (set-window-start (get-buffer-window (current-buffer)) (point-min))
1224       (set-buffer cbuf))))
1225
1226 (defun gnus-article-hidden-arg ()
1227   "Return the current prefix arg as a number, or 0 if no prefix."
1228   (list (if current-prefix-arg
1229             (prefix-numeric-value current-prefix-arg)
1230           0)))
1231
1232 (defun gnus-article-check-hidden-text (type arg)
1233   "Return nil if hiding is necessary.
1234 Arg can be nil or a number.  Nil and positive means hide, negative
1235 means show, 0 means toggle."
1236   (save-excursion
1237     (save-restriction
1238       (widen)
1239       (let ((hide (gnus-article-hidden-text-p type)))
1240         (cond
1241          ((or (null arg)
1242               (> arg 0))
1243           nil)
1244          ((< arg 0)
1245           (gnus-article-show-hidden-text type))
1246          (t
1247           (if (eq hide 'hidden)
1248               (gnus-article-show-hidden-text type)
1249             nil)))))))
1250
1251 (defun gnus-article-hidden-text-p (type)
1252   "Say whether the current buffer contains hidden text of type TYPE."
1253   (let ((start (point-min))
1254         (pos (text-property-any (point-min) (point-max) 'article-type type)))
1255     (while (and pos
1256                 (not (get-text-property pos 'invisible)))
1257       (setq pos
1258             (text-property-any (1+ pos) (point-max) 'article-type type)))
1259     (if pos
1260         'hidden
1261       'shown)))
1262
1263 (defun gnus-article-show-hidden-text (type &optional hide)
1264   "Show all hidden text of type TYPE.
1265 If HIDE, hide the text instead."
1266   (save-excursion
1267     (let ((buffer-read-only nil)
1268           (inhibit-point-motion-hooks t)
1269           (end (point-min))
1270           beg)
1271       (while (setq beg (text-property-any end (point-max) 'article-type type))
1272         (goto-char beg)
1273         (setq end (or
1274                    (text-property-not-all beg (point-max) 'article-type type)
1275                    (point-max)))
1276         (if hide
1277             (gnus-article-hide-text beg end gnus-hidden-properties)
1278           (gnus-article-unhide-text beg end))
1279         (goto-char end))
1280       t)))
1281
1282 (defconst article-time-units
1283   `((year . ,(* 365.25 24 60 60))
1284     (week . ,(* 7 24 60 60))
1285     (day . ,(* 24 60 60))
1286     (hour . ,(* 60 60))
1287     (minute . 60)
1288     (second . 1))
1289   "Mapping from time units to seconds.")
1290
1291 (defun article-date-ut (&optional type highlight header)
1292   "Convert DATE date to universal time in the current article.
1293 If TYPE is `local', convert to local time; if it is `lapsed', output
1294 how much time has lapsed since DATE."
1295   (interactive (list 'ut t))
1296   (let* ((header (or header
1297                      (mail-header-date gnus-current-headers)
1298                      (message-fetch-field "date")
1299                      ""))
1300          (date (if (vectorp header) (mail-header-date header)
1301                  header))
1302          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1303          (inhibit-point-motion-hooks t)
1304          bface eface newline)
1305     (when (and date (not (string= date "")))
1306       (save-excursion
1307         (save-restriction
1308           (nnheader-narrow-to-headers)
1309           (let ((buffer-read-only nil))
1310             ;; Delete any old Date headers.
1311             (if (re-search-forward date-regexp nil t)
1312                 (progn
1313                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1314                         eface (get-text-property (1- (gnus-point-at-eol))
1315                                                  'face))
1316                   (delete-region (progn (beginning-of-line) (point))
1317                                  (progn (end-of-line) (point)))
1318                   (beginning-of-line))
1319               (goto-char (point-max))
1320               (setq newline t))
1321             (insert (article-make-date-line date type))
1322             ;; Do highlighting.
1323             (beginning-of-line)
1324             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1325               (put-text-property (match-beginning 1) (1+ (match-end 1))
1326                                  'face bface)
1327               (put-text-property (match-beginning 2) (match-end 2)
1328                                  'face eface))
1329             (when newline
1330               (end-of-line)
1331               (insert "\n"))))))))
1332
1333 (defun article-make-date-line (date type)
1334   "Return a DATE line of TYPE."
1335   (cond
1336    ;; Convert to the local timezone.  We have to slap a
1337    ;; `condition-case' round the calls to the timezone
1338    ;; functions since they aren't particularly resistant to
1339    ;; buggy dates.
1340    ((eq type 'local)
1341     (concat "Date: " (condition-case ()
1342                          (timezone-make-date-arpa-standard date)
1343                        (error date))))
1344    ;; Convert to Universal Time.
1345    ((eq type 'ut)
1346     (concat "Date: "
1347             (condition-case ()
1348                 (timezone-make-date-arpa-standard date nil "UT")
1349               (error date))))
1350    ;; Get the original date from the article.
1351    ((eq type 'original)
1352     (concat "Date: " date))
1353    ;; Let the user define the format.
1354    ((eq type 'user)
1355     (if (gnus-functionp gnus-article-time-format)
1356         (funcall
1357          gnus-article-time-format
1358          (ignore-errors
1359            (gnus-encode-date
1360             (timezone-make-date-arpa-standard
1361              date nil "UT"))))
1362       (concat
1363        "Date: "
1364        (format-time-string gnus-article-time-format
1365                            (ignore-errors
1366                              (gnus-encode-date
1367                               (timezone-make-date-arpa-standard
1368                                date nil "UT")))))))
1369    ;; ISO 8601.
1370    ((eq type 'iso8601)
1371     (concat
1372      "Date: "
1373      (format-time-string "%Y%M%DT%h%m%s"
1374                          (ignore-errors
1375                            (gnus-encode-date
1376                             (timezone-make-date-arpa-standard
1377                              date nil "UT"))))))
1378    ;; Do an X-Sent lapsed format.
1379    ((eq type 'lapsed)
1380     ;; If the date is seriously mangled, the timezone functions are
1381     ;; liable to bug out, so we ignore all errors.
1382     (let* ((now (current-time))
1383            (real-time
1384             (ignore-errors
1385               (gnus-time-minus
1386                (gnus-encode-date
1387                 (timezone-make-date-arpa-standard
1388                  (current-time-string now)
1389                  (current-time-zone now) "UT"))
1390                (gnus-encode-date
1391                 (timezone-make-date-arpa-standard
1392                  date nil "UT")))))
1393            (real-sec (and real-time
1394                           (+ (* (float (car real-time)) 65536)
1395                              (cadr real-time))))
1396            (sec (and real-time (abs real-sec)))
1397            num prev)
1398       (cond
1399        ((null real-time)
1400         "X-Sent: Unknown")
1401        ((zerop sec)
1402         "X-Sent: Now")
1403        (t
1404         (concat
1405          "X-Sent: "
1406          ;; This is a bit convoluted, but basically we go
1407          ;; through the time units for years, weeks, etc,
1408          ;; and divide things to see whether that results
1409          ;; in positive answers.
1410          (mapconcat
1411           (lambda (unit)
1412             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1413                 ;; The (remaining) seconds are too few to
1414                 ;; be divided into this time unit.
1415                 ""
1416               ;; It's big enough, so we output it.
1417               (setq sec (- sec (* num (cdr unit))))
1418               (prog1
1419                   (concat (if prev ", " "") (int-to-string
1420                                              (floor num))
1421                           " " (symbol-name (car unit))
1422                           (if (> num 1) "s" ""))
1423                 (setq prev t))))
1424           article-time-units "")
1425          ;; If dates are odd, then it might appear like the
1426          ;; article was sent in the future.
1427          (if (> real-sec 0)
1428              " ago"
1429            " in the future"))))))
1430    (t
1431     (error "Unknown conversion type: %s" type))))
1432
1433 (defun article-date-local (&optional highlight)
1434   "Convert the current article date to the local timezone."
1435   (interactive (list t))
1436   (article-date-ut 'local highlight))
1437
1438 (defun article-date-original (&optional highlight)
1439   "Convert the current article date to what it was originally.
1440 This is only useful if you have used some other date conversion
1441 function and want to see what the date was before converting."
1442   (interactive (list t))
1443   (article-date-ut 'original highlight))
1444
1445 (defun article-date-lapsed (&optional highlight)
1446   "Convert the current article date to time lapsed since it was sent."
1447   (interactive (list t))
1448   (article-date-ut 'lapsed highlight))
1449
1450 (defun article-update-date-lapsed ()
1451   "Function to be run from a timer to update the lapsed time line."
1452   (save-excursion
1453     (ignore-errors
1454       (when (gnus-buffer-live-p gnus-article-buffer)
1455         (set-buffer gnus-article-buffer)
1456         (goto-char (point-min))
1457         (when (re-search-forward "^X-Sent:" nil t)
1458           (article-date-lapsed t))))))
1459
1460 (defun gnus-start-date-timer (&optional n)
1461   "Start a timer to update the X-Sent header in the article buffers.
1462 The numerical prefix says how frequently (in seconds) the function
1463 is to run."
1464   (interactive "p")
1465   (unless n
1466     (setq n 1))
1467   (gnus-stop-date-timer)
1468   (setq article-lapsed-timer 
1469         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1470
1471 (defun gnus-stop-date-timer ()
1472   "Stop the X-Sent timer."
1473   (interactive)
1474   (when article-lapsed-timer
1475     (nnheader-cancel-timer article-lapsed-timer)
1476     (setq article-lapsed-timer nil)))
1477
1478 (defun article-date-user (&optional highlight)
1479   "Convert the current article date to the user-defined format.
1480 This format is defined by the `gnus-article-time-format' variable."
1481   (interactive (list t))
1482   (article-date-ut 'user highlight))
1483
1484 (defun article-date-iso8601 (&optional highlight)
1485   "Convert the current article date to ISO8601."
1486   (interactive (list t))
1487   (article-date-ut 'iso8601 highlight))
1488
1489 (defun article-show-all ()
1490   "Show all hidden text in the article buffer."
1491   (interactive)
1492   (save-excursion
1493     (let ((buffer-read-only nil))
1494       (gnus-article-unhide-text (point-min) (point-max)))))
1495
1496 (defun article-emphasize (&optional arg)
1497   "Emphasize text according to `gnus-emphasis-alist'."
1498   (interactive (gnus-article-hidden-arg))
1499   (unless (gnus-article-check-hidden-text 'emphasis arg)
1500     (save-excursion
1501       (let ((alist gnus-emphasis-alist)
1502             (buffer-read-only nil)
1503             (props (append '(article-type emphasis)
1504                            gnus-hidden-properties))
1505             regexp elem beg invisible visible face)
1506         (goto-char (point-min))
1507         (search-forward "\n\n" nil t)
1508         (setq beg (point))
1509         (while (setq elem (pop alist))
1510           (goto-char beg)
1511           (setq regexp (car elem)
1512                 invisible (nth 1 elem)
1513                 visible (nth 2 elem)
1514                 face (nth 3 elem))
1515           (while (re-search-forward regexp nil t)
1516             (when (and (match-beginning visible) (match-beginning invisible))
1517               (gnus-article-hide-text
1518                (match-beginning invisible) (match-end invisible) props)
1519               (gnus-article-unhide-text-type
1520                (match-beginning visible) (match-end visible) 'emphasis)
1521               (gnus-put-text-property-excluding-newlines
1522                (match-beginning visible) (match-end visible) 'face face)
1523               (goto-char (match-end invisible)))))))))
1524
1525 (defvar gnus-summary-article-menu)
1526 (defvar gnus-summary-post-menu)
1527
1528 ;;; Saving functions.
1529
1530 (defun gnus-article-save (save-buffer file &optional num)
1531   "Save the currently selected article."
1532   (unless gnus-save-all-headers
1533     ;; Remove headers according to `gnus-saved-headers'.
1534     (let ((gnus-visible-headers
1535            (or gnus-saved-headers gnus-visible-headers))
1536           (gnus-article-buffer save-buffer))
1537       (save-excursion
1538         (set-buffer save-buffer)
1539         (article-hide-headers 1 t))))
1540   (save-window-excursion
1541     (if (not gnus-default-article-saver)
1542         (error "No default saver is defined")
1543       ;; !!! Magic!  The saving functions all save
1544       ;; `gnus-original-article-buffer' (or so they think), but we
1545       ;; bind that variable to our save-buffer.
1546       (set-buffer gnus-article-buffer)
1547       (let* ((gnus-save-article-buffer save-buffer)
1548              (filename
1549               (cond
1550                ((not gnus-prompt-before-saving) 'default)
1551                ((eq gnus-prompt-before-saving 'always) nil)
1552                (t file)))
1553              (gnus-number-of-articles-to-be-saved
1554               (when (eq gnus-prompt-before-saving t)
1555                 num)))                  ; Magic
1556         (set-buffer gnus-article-current-summary)
1557         (funcall gnus-default-article-saver filename)))))
1558
1559 (defun gnus-read-save-file-name (prompt &optional filename
1560                                         function group headers variable)
1561   (let ((default-name
1562           (funcall function group headers (symbol-value variable)))
1563         result)
1564     (setq
1565      result
1566      (cond
1567       ((eq filename 'default)
1568        default-name)
1569       ((eq filename t)
1570        default-name)
1571       (filename filename)
1572       (t
1573        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1574               (prompt
1575                (format prompt
1576                        (if (and gnus-number-of-articles-to-be-saved
1577                                 (> gnus-number-of-articles-to-be-saved 1))
1578                            (format "these %d articles"
1579                                    gnus-number-of-articles-to-be-saved)
1580                          "this article")))
1581               (file
1582                ;; Let the split methods have their say.
1583                (cond
1584                 ;; No split name was found.
1585                 ((null split-name)
1586                  (read-file-name
1587                   (concat prompt " (default "
1588                           (file-name-nondirectory default-name) ") ")
1589                   (file-name-directory default-name)
1590                   default-name))
1591                 ;; A single group name is returned.
1592                 ((stringp split-name)
1593                  (setq default-name
1594                        (funcall function split-name headers
1595                                 (symbol-value variable)))
1596                  (read-file-name
1597                   (concat prompt " (default "
1598                           (file-name-nondirectory default-name) ") ")
1599                   (file-name-directory default-name)
1600                   default-name))
1601                 ;; A single split name was found
1602                 ((= 1 (length split-name))
1603                  (let* ((name (expand-file-name
1604                                (car split-name) gnus-article-save-directory))
1605                         (dir (cond ((file-directory-p name)
1606                                     (file-name-as-directory name))
1607                                    ((file-exists-p name) name)
1608                                    (t gnus-article-save-directory))))
1609                    (read-file-name
1610                     (concat prompt " (default " name ") ")
1611                     dir name)))
1612                 ;; A list of splits was found.
1613                 (t
1614                  (setq split-name (nreverse split-name))
1615                  (let (result)
1616                    (let ((file-name-history
1617                           (nconc split-name file-name-history)))
1618                      (setq result
1619                            (expand-file-name
1620                             (read-file-name
1621                              (concat prompt " (`M-p' for defaults) ")
1622                              gnus-article-save-directory
1623                              (car split-name))
1624                             gnus-article-save-directory)))
1625                    (car (push result file-name-history)))))))
1626          ;; Create the directory.
1627          (gnus-make-directory (file-name-directory file))
1628          ;; If we have read a directory, we append the default file name.
1629          (when (file-directory-p file)
1630            (setq file (concat (file-name-as-directory file)
1631                               (file-name-nondirectory default-name))))
1632          ;; Possibly translate some characters.
1633          (nnheader-translate-file-chars file)))))
1634     (gnus-make-directory (file-name-directory result))
1635     (set variable result)))
1636
1637 (defun gnus-article-archive-name (group)
1638   "Return the first instance of an \"Archive-name\" in the current buffer."
1639   (let ((case-fold-search t))
1640     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1641       (nnheader-concat gnus-article-save-directory
1642                        (match-string 1)))))
1643
1644 (defun gnus-article-nndoc-name (group)
1645   "If GROUP is an nndoc group, return the name of the parent group."
1646   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1647     (gnus-group-get-parameter group 'save-article-group)))
1648
1649 (defun gnus-summary-save-in-rmail (&optional filename)
1650   "Append this article to Rmail file.
1651 Optional argument FILENAME specifies file name.
1652 Directory to save to is default to `gnus-article-save-directory'."
1653   (interactive)
1654   (setq filename (gnus-read-save-file-name
1655                   "Save %s in rmail file:" filename
1656                   gnus-rmail-save-name gnus-newsgroup-name
1657                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1658   (gnus-eval-in-buffer-window gnus-save-article-buffer
1659     (save-excursion
1660       (save-restriction
1661         (widen)
1662         (gnus-output-to-rmail filename))))
1663   filename)
1664
1665 (defun gnus-summary-save-in-mail (&optional filename)
1666   "Append this article to Unix mail file.
1667 Optional argument FILENAME specifies file name.
1668 Directory to save to is default to `gnus-article-save-directory'."
1669   (interactive)
1670   (setq filename (gnus-read-save-file-name
1671                   "Save %s in Unix mail file:" filename
1672                   gnus-mail-save-name gnus-newsgroup-name
1673                   gnus-current-headers 'gnus-newsgroup-last-mail))
1674   (gnus-eval-in-buffer-window gnus-save-article-buffer
1675     (save-excursion
1676       (save-restriction
1677         (widen)
1678         (if (and (file-readable-p filename)
1679                  (mail-file-babyl-p filename))
1680             (gnus-output-to-rmail filename t)
1681           (gnus-output-to-mail filename)))))
1682   filename)
1683
1684 (defun gnus-summary-save-in-file (&optional filename overwrite)
1685   "Append this article to file.
1686 Optional argument FILENAME specifies file name.
1687 Directory to save to is default to `gnus-article-save-directory'."
1688   (interactive)
1689   (setq filename (gnus-read-save-file-name