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