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