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