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