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