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