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