e36f60e1ed960f44ff51d988a245d4cd9ed844f3
[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 (gnus-buffer-live-p gnus-article-buffer)
1459           (set-buffer gnus-article-buffer)
1460           (goto-char (point-min))
1461           (when (re-search-forward "^X-Sent:" nil t)
1462             (article-date-lapsed t)))))))
1463
1464 (defun gnus-start-date-timer (&optional n)
1465   "Start a timer to update the X-Sent header in the article buffers.
1466 The numerical prefix says how frequently (in seconds) the function
1467 is to run."
1468   (interactive "p")
1469   (unless n
1470     (setq n 1))
1471   (gnus-stop-date-timer)
1472   (setq article-lapsed-timer
1473         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1474
1475 (defun gnus-stop-date-timer ()
1476   "Stop the X-Sent timer."
1477   (interactive)
1478   (when article-lapsed-timer
1479     (nnheader-cancel-timer article-lapsed-timer)
1480     (setq article-lapsed-timer nil)))
1481
1482 (defun article-date-user (&optional highlight)
1483   "Convert the current article date to the user-defined format.
1484 This format is defined by the `gnus-article-time-format' variable."
1485   (interactive (list t))
1486   (article-date-ut 'user highlight))
1487
1488 (defun article-date-iso8601 (&optional highlight)
1489   "Convert the current article date to ISO8601."
1490   (interactive (list t))
1491   (article-date-ut 'iso8601 highlight))
1492
1493 (defun article-show-all ()
1494   "Show all hidden text in the article buffer."
1495   (interactive)
1496   (save-excursion
1497     (let ((buffer-read-only nil))
1498       (gnus-article-unhide-text (point-min) (point-max)))))
1499
1500 (defun article-emphasize (&optional arg)
1501   "Emphasize text according to `gnus-emphasis-alist'."
1502   (interactive (gnus-article-hidden-arg))
1503   (unless (gnus-article-check-hidden-text 'emphasis arg)
1504     (save-excursion
1505       (let ((alist gnus-emphasis-alist)
1506             (buffer-read-only nil)
1507             (props (append '(article-type emphasis)
1508                            gnus-hidden-properties))
1509             regexp elem beg invisible visible face)
1510         (goto-char (point-min))
1511         (search-forward "\n\n" nil t)
1512         (setq beg (point))
1513         (while (setq elem (pop alist))
1514           (goto-char beg)
1515           (setq regexp (car elem)
1516                 invisible (nth 1 elem)
1517                 visible (nth 2 elem)
1518                 face (nth 3 elem))
1519           (while (re-search-forward regexp nil t)
1520             (when (and (match-beginning visible) (match-beginning invisible))
1521               (gnus-article-hide-text
1522                (match-beginning invisible) (match-end invisible) props)
1523               (gnus-article-unhide-text-type
1524                (match-beginning visible) (match-end visible) 'emphasis)
1525               (gnus-put-text-property-excluding-newlines
1526                (match-beginning visible) (match-end visible) 'face face)
1527               (goto-char (match-end invisible)))))))))
1528
1529 (defvar gnus-summary-article-menu)
1530 (defvar gnus-summary-post-menu)
1531
1532 ;;; Saving functions.
1533
1534 (defun gnus-article-save (save-buffer file &optional num)
1535   "Save the currently selected article."
1536   (unless gnus-save-all-headers
1537     ;; Remove headers according to `gnus-saved-headers'.
1538     (let ((gnus-visible-headers
1539            (or gnus-saved-headers gnus-visible-headers))
1540           (gnus-article-buffer save-buffer))
1541       (save-excursion
1542         (set-buffer save-buffer)
1543         (article-hide-headers 1 t))))
1544   (save-window-excursion
1545     (if (not gnus-default-article-saver)
1546         (error "No default saver is defined")
1547       ;; !!! Magic!  The saving functions all save
1548       ;; `gnus-save-article-buffer' (or so they think), but we
1549       ;; bind that variable to our save-buffer.
1550       (set-buffer gnus-article-buffer)
1551       (let* ((gnus-save-article-buffer save-buffer)
1552              (filename
1553               (cond
1554                ((not gnus-prompt-before-saving) 'default)
1555                ((eq gnus-prompt-before-saving 'always) nil)
1556                (t file)))
1557              (gnus-number-of-articles-to-be-saved
1558               (when (eq gnus-prompt-before-saving t)
1559                 num)))                  ; Magic
1560         (set-buffer gnus-article-current-summary)
1561         (funcall gnus-default-article-saver filename)))))
1562
1563 (defun gnus-read-save-file-name (prompt &optional filename
1564                                         function group headers variable)
1565   (let ((default-name
1566           (funcall function group headers (symbol-value variable)))
1567         result)
1568     (setq
1569      result
1570      (cond
1571       ((eq filename 'default)
1572        default-name)
1573       ((eq filename t)
1574        default-name)
1575       (filename filename)
1576       (t
1577        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1578               (prompt
1579                (format prompt
1580                        (if (and gnus-number-of-articles-to-be-saved
1581                                 (> gnus-number-of-articles-to-be-saved 1))
1582                            (format "these %d articles"
1583                                    gnus-number-of-articles-to-be-saved)
1584                          "this article")))
1585               (file
1586                ;; Let the split methods have their say.
1587                (cond
1588                 ;; No split name was found.
1589                 ((null split-name)
1590                  (read-file-name
1591                   (concat prompt " (default "
1592                           (file-name-nondirectory default-name) ") ")
1593                   (file-name-directory default-name)
1594                   default-name))
1595                 ;; A single group name is returned.
1596                 ((stringp split-name)
1597                  (setq default-name
1598                        (funcall function split-name headers
1599                                 (symbol-value variable)))
1600                  (read-file-name
1601                   (concat prompt " (default "
1602                           (file-name-nondirectory default-name) ") ")
1603                   (file-name-directory default-name)
1604                   default-name))
1605                 ;; A single split name was found
1606                 ((= 1 (length split-name))
1607                  (let* ((name (expand-file-name
1608                                (car split-name) gnus-article-save-directory))
1609                         (dir (cond ((file-directory-p name)
1610                                     (file-name-as-directory name))
1611                                    ((file-exists-p name) name)
1612                                    (t gnus-article-save-directory))))
1613                    (read-file-name
1614                     (concat prompt " (default " name ") ")
1615                     dir name)))
1616                 ;; A list of splits was found.
1617                 (t
1618                  (setq split-name (nreverse split-name))
1619                  (let (result)
1620                    (let ((file-name-history
1621                           (nconc split-name file-name-history)))
1622                      (setq result
1623                            (expand-file-name
1624                             (read-file-name
1625                              (concat prompt " (`M-p' for defaults) ")
1626                              gnus-article-save-directory
1627                              (car split-name))
1628                             gnus-article-save-directory)))
1629                    (car (push result file-name-history)))))))
1630          ;; Create the directory.
1631          (gnus-make-directory (file-name-directory file))
1632          ;; If we have read a directory, we append the default file name.
1633          (when (file-directory-p file)
1634            (setq file (concat (file-name-as-directory file)
1635                               (file-name-nondirectory default-name))))
1636          ;; Possibly translate some characters.
1637          (nnheader-translate-file-chars file)))))
1638     (gnus-make-directory (file-name-directory result))
1639     (set variable result)))
1640
1641 (defun gnus-article-archive-name (group)
1642   "Return the first instance of an \"Archive-name\" in the current buffer."
1643   (let ((case-fold-search t))
1644     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1645       (nnheader-concat gnus-article-save-directory
1646                        (match-string 1)))))
1647
1648 (defun gnus-article-nndoc-name (group)
1649   "If GROUP is an nndoc group, return the name of the parent group."
1650   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1651     (gnus-group-get-parameter group 'save-article-group)))
1652
1653 (defun gnus-summary-save-in-rmail (&optional filename)
1654   "Append this article to Rmail file.
1655 Optional argument FILENAME specifies file name.
1656 Directory to save to is default to `gnus-article-save-directory'."
1657   (setq filename (gnus-read-save-file-name
1658                   "Save %s in rmail file:" filename
1659                   gnus-rmail-save-name gnus-newsgroup-name
1660                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1661   (gnus-eval-in-buffer-window gnus-save-article-buffer
1662     (save-excursion
1663       (save-restriction
1664         (widen)
1665         (rmail-output-to-rmail-file filename))))
1666   filename)
1667
1668 (defun gnus-summary-save-in-mail (&optional filename)
1669   "Append this article to Unix mail file.
1670 Optional argument FILENAME specifies file name.
1671 Directory to save to is default to `gnus-article-save-directory'."
1672   (setq filename (gnus-read-save-file-name
1673                   "Save %s in Unix mail file:" filename
1674                   gnus-mail-save-name gnus-newsgroup-name
1675                   gnus-current-headers 'gnus-newsgroup-last-mail))
1676   (gnus-eval-in-buffer-window gnus-save-article-buffer
1677     (save-excursion
1678       (save-restriction
1679         (widen)
1680         (if (and (file-readable-p filename)
1681                  (mail-file-babyl-p filename))
1682             (rmail-output-to-rmail-file filename t)
1683           (gnus-output-to-mail filename)))))
1684   filename)
1685
1686 (defun gnus-summary-save-in-file (&optional filename overwrite)
1687   "Append this article to file.
1688 Optional argument FILENAME specifies file name.
1689 Directory to save to is default to `gnus-article-save-directory'."
1690   (setq filename (gnus-read-save-file-name
1691                   "Save %s in file:" filename
1692                   gnus-file-save-name gnus-newsgroup-name
1693                   gnus-current-headers 'gnus-newsgroup-last-file))
1694   (gnus-eval-in-buffer-window gnus-save-article-buffer
1695     (save-excursion
1696       (save-restriction
1697         (widen)
1698         (when (and overwrite
1699                    (file-exists-p filename))
1700           (delete-file filename))
1701         (gnus-output-to-file filename))))
1702   filename)
1703
1704 (defun gnus-summary-write-to-file (&optional filename)
1705   "Write this article to a file.
1706 Optional argument FILENAME specifies file name.
1707 The directory to save in defaults to `gnus-article-save-directory'."
1708   (gnus-summary-save-in-file nil t))
1709
1710 (defun gnus-summary-save-body-in-file (&optional filename)
1711   "Append this article body to a file.
1712 Optional argument FILENAME specifies file name.
1713 The directory to save in defaults to `gnus-article-save-directory'."
1714   (setq filename (gnus-read-save-file-name
1715                   "Save %s body in file:" filename
1716                   gnus-file-save-name gnus-newsgroup-name
1717                   gnus-current-headers 'gnus-newsgroup-last-file))
1718   (gnus-eval-in-buffer-window gnus-save-article-buffer
1719     (save-excursion
1720       (save-restriction
1721         (widen)
1722         (goto-char (point-min))
1723         (when (search-forward "\n\n" nil t)
1724           (narrow-to-region (point) (point-max)))
1725         (gnus-output-to-file filename))))
1726   filename)
1727
1728 (defun gnus-summary-save-in-pipe (&optional command)
1729   "Pipe this article to subprocess."
1730   (setq command
1731         (cond ((eq command 'default)
1732                gnus-last-shell-command)
1733               (command command)
1734               (t (read-string
1735                   (format
1736                    "Shell command on %s: "
1737                    (if (and gnus-number-of-articles-to-be-saved
1738                             (> gnus-number-of-articles-to-be-saved 1))
1739                        (format "these %d articles"
1740                                gnus-number-of-articles-to-be-saved)
1741                      "this article"))
1742                   gnus-last-shell-command))))
1743   (when (string-equal command "")
1744     (setq command gnus-last-shell-command))
1745   (gnus-eval-in-buffer-window gnus-article-buffer
1746     (save-restriction
1747       (widen)
1748       (shell-command-on-region (point-min) (point-max) command nil)))
1749   (setq gnus-last-shell-command command))
1750
1751 ;;; Article file names when saving.
1752
1753 (defun gnus-capitalize-newsgroup (newsgroup)
1754   "Capitalize NEWSGROUP name."
1755   (when (not (zerop (length newsgroup)))
1756     (concat (char-to-string (upcase (aref newsgroup 0)))
1757             (substring newsgroup 1))))
1758
1759 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1760   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1761 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1762 Otherwise, it is like ~/News/news/group/num."
1763   (let ((default
1764           (expand-file-name
1765            (concat (if (gnus-use-long-file-name 'not-save)
1766                        (gnus-capitalize-newsgroup newsgroup)
1767                      (gnus-newsgroup-directory-form newsgroup))
1768                    "/" (int-to-string (mail-header-number headers)))
1769            gnus-article-save-directory)))
1770     (if (and last-file
1771              (string-equal (file-name-directory default)
1772                            (file-name-directory last-file))
1773              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1774         default
1775       (or last-file default))))
1776
1777 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1778   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1779 If variable `gnus-use-long-file-name' is non-nil, it is
1780 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1781   (let ((default
1782           (expand-file-name
1783            (concat (if (gnus-use-long-file-name 'not-save)
1784                        newsgroup
1785                      (gnus-newsgroup-directory-form newsgroup))
1786                    "/" (int-to-string (mail-header-number headers)))
1787            gnus-article-save-directory)))
1788     (if (and last-file
1789              (string-equal (file-name-directory default)
1790                            (file-name-directory last-file))
1791              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1792         default
1793       (or last-file default))))
1794
1795 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1796   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1797 If variable `gnus-use-long-file-name' is non-nil, it is
1798 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1799   (or last-file
1800       (expand-file-name
1801        (if (gnus-use-long-file-name 'not-save)
1802            (gnus-capitalize-newsgroup newsgroup)
1803          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1804        gnus-article-save-directory)))
1805
1806 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1807   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1808 If variable `gnus-use-long-file-name' is non-nil, it is
1809 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1810   (or last-file
1811       (expand-file-name
1812        (if (gnus-use-long-file-name 'not-save)
1813            newsgroup
1814          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1815        gnus-article-save-directory)))
1816
1817 (eval-and-compile
1818   (mapcar
1819    (lambda (func)
1820      (let (afunc gfunc)
1821        (if (consp func)
1822            (setq afunc (car func)
1823                  gfunc (cdr func))
1824          (setq afunc func
1825                gfunc (intern (format "gnus-%s" func))))
1826        (fset gfunc
1827              (if (not (fboundp afunc))
1828                  nil
1829                `(lambda (&optional interactive &rest args)
1830                   ,(documentation afunc t)
1831                   (interactive (list t))
1832                   (save-excursion
1833                     (set-buffer gnus-article-buffer)
1834                     (if interactive
1835                         (call-interactively ',afunc)
1836                       (apply ',afunc args))))))))
1837    '(article-hide-headers
1838      article-hide-boring-headers
1839      article-treat-overstrike
1840      (article-fill . gnus-article-word-wrap)
1841      article-remove-cr
1842      article-display-x-face
1843      article-de-quoted-unreadable
1844      article-mime-decode-quoted-printable
1845      article-hide-pgp
1846      article-hide-pem
1847      article-hide-signature
1848      article-remove-trailing-blank-lines
1849      article-strip-leading-blank-lines
1850      article-strip-multiple-blank-lines
1851      article-strip-leading-space
1852      article-strip-blank-lines
1853      article-strip-all-blank-lines
1854      article-date-local
1855      article-date-iso8601
1856      article-date-original
1857      article-date-ut
1858      article-decode-mime-words
1859      article-decode-charset
1860      article-decode-encoded-words
1861      article-date-user
1862      article-date-lapsed
1863      article-emphasize
1864      article-treat-dumbquotes
1865      (article-show-all . gnus-article-show-all-headers))))
1866 \f
1867 ;;;
1868 ;;; Gnus article mode
1869 ;;;
1870
1871 (put 'gnus-article-mode 'mode-class 'special)
1872
1873 (set-keymap-parent gnus-article-mode-map widget-keymap)
1874
1875 (gnus-define-keys gnus-article-mode-map
1876   " " gnus-article-goto-next-page
1877   "\177" gnus-article-goto-prev-page
1878   [delete] gnus-article-goto-prev-page
1879   "\r" widget-button-press
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               ;; Hooks for getting information from the article.
2113               ;; This hook must be called before being narrowed.
2114               (let (buffer-read-only)
2115                 (gnus-run-hooks 'gnus-tmp-internal-hook)
2116                 (gnus-run-hooks 'gnus-article-prepare-hook)
2117                 (when gnus-display-mime-function
2118                   (funcall gnus-display-mime-function))
2119                 ;; Perform the article display hooks.
2120                 (gnus-run-hooks 'gnus-article-display-hook))
2121               ;; Do page break.
2122               (goto-char (point-min))
2123               (setq gnus-page-broken
2124                     (when gnus-break-pages
2125                       (gnus-narrow-to-page)
2126                       t)))
2127             (gnus-set-mode-line 'article)
2128             (gnus-configure-windows 'article)
2129             (goto-char (point-min))
2130             (search-forward "\n\n" nil t)
2131             (set-window-point (get-buffer-window (current-buffer)) (point))
2132             t))))))
2133
2134 ;;;
2135 ;;; Gnus MIME viewing functions
2136 ;;;
2137
2138 (defvar gnus-mime-button-line-format "%{%([%t%d%n]%)%}\n")
2139 (defvar gnus-mime-button-line-format-alist
2140   '((?t gnus-tmp-type ?s)
2141     (?n gnus-tmp-name ?s)
2142     (?d gnus-tmp-description ?s)))
2143
2144 (defvar gnus-mime-button-map nil)
2145 (unless gnus-mime-button-map
2146   (setq gnus-mime-button-map (copy-keymap gnus-article-mode-map))
2147   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2148   (define-key gnus-mime-button-map "\r" 'gnus-article-press-button)
2149   (define-key gnus-mime-button-map "\M-\r" 'gnus-mime-view-part)
2150   (define-key gnus-mime-button-map "v" 'gnus-mime-view-part)
2151   (define-key gnus-mime-button-map "o" 'gnus-mime-save-part)
2152   (define-key gnus-mime-button-map "c" 'gnus-mime-copy-part)
2153   (define-key gnus-mime-button-map "|" 'gnus-mime-pipe-part))
2154
2155 (defun gnus-mime-save-part ()
2156   "Save the MIME part under point."
2157   (interactive)
2158   (let ((data (get-text-property (point) 'gnus-data)))
2159     (mm-save-part data)))
2160
2161 (defun gnus-mime-pipe-part ()
2162   "Pipe the MIME part under point to a process."
2163   (interactive)
2164   (let ((data (get-text-property (point) 'gnus-data)))
2165     (mm-pipe-part data)))
2166
2167 (defun gnus-mime-view-part ()
2168   "Interactively choose a view method for the MIME part under point."
2169   (interactive)
2170   (let ((data (get-text-property (point) 'gnus-data)))
2171     (mm-interactively-view-part data)))
2172
2173 (defun gnus-mime-copy-part ()
2174   "Put the the MIME part under point into a new buffer."
2175   (interactive)
2176   (let* ((data (get-text-property (point) 'gnus-data))
2177          (contents (mm-get-part data)))
2178     (switch-to-buffer (generate-new-buffer "*decoded*"))
2179     (insert contents)
2180     (goto-char (point-min))))
2181
2182 (defun gnus-insert-mime-button (handle)
2183   (let ((gnus-tmp-name (mail-content-type-get (mm-handle-type handle) 'name))
2184         (gnus-tmp-type (car (mm-handle-type handle)))
2185         (gnus-tmp-description (mm-handle-description handle))
2186         b e)
2187     (setq gnus-tmp-name
2188       (if gnus-tmp-name
2189           (concat " (" gnus-tmp-name ")")
2190         ""))
2191     (setq gnus-tmp-description
2192       (if gnus-tmp-description
2193           (concat " (" gnus-tmp-description ")")
2194         ""))
2195     (setq b (point))
2196     (gnus-eval-format
2197      gnus-mime-button-line-format gnus-mime-button-line-format-alist
2198      `(local-map ,gnus-mime-button-map
2199                  keymap ,gnus-mime-button-map
2200                  gnus-callback mm-display-part
2201                  gnus-data ,handle))
2202     (setq e (point))
2203     (widget-convert-text 'link b e b e :action 'gnus-widget-press-button)))
2204
2205 (defun gnus-widget-press-button (elems el)
2206   (goto-char (widget-get elems :from))
2207   (gnus-article-press-button))
2208
2209 (defun gnus-display-mime ()
2210   "Insert MIME buttons in the buffer."
2211   (let (ct ctl)
2212     (save-restriction
2213       (mail-narrow-to-head)
2214       (when (setq ct (mail-fetch-field "content-type"))
2215         (setq ctl (mail-header-parse-content-type ct))))
2216     (let* ((handles (mm-dissect-buffer))
2217            handle name type b e)
2218       (mapcar 'mm-destroy-part gnus-article-mime-handles)
2219       (setq gnus-article-mime-handles handles)
2220       (when handles
2221         (goto-char (point-min))
2222         (search-forward "\n\n" nil t)
2223         (delete-region (point) (point-max))
2224         (if (not (equal (car ctl) "multipart/alternative"))
2225             (while (setq handle (pop handles))
2226               (gnus-insert-mime-button handle)
2227               (insert "\n\n")
2228               (when (and (mm-automatic-display-p (car (mm-handle-type handle)))
2229                          (or (not (mm-handle-disposition handle))
2230                              (equal (car (mm-handle-disposition handle))
2231                                     "inline")))
2232                 (forward-line -2)
2233                 (mm-display-part handle)
2234                 (goto-char (point-max))))
2235           ;; Here we have multipart/alternative
2236           (gnus-mime-display-alternative handles))))))
2237
2238 (defun gnus-mime-display-alternative (handles &optional preferred)
2239   (let* ((preferred (mm-preferred-alternative handles preferred))
2240          (ihandles handles)
2241          handle buffer-read-only)
2242     (goto-char (point-min))
2243     (search-forward "\n\n" nil t)
2244     (delete-region (point) (point-max))
2245     (mapcar 'mm-remove-part gnus-article-mime-handles)
2246     (setq gnus-article-mime-handles handles)
2247     (while (setq handle (pop handles))
2248       (gnus-add-text-properties
2249        (point)
2250        (progn
2251          (insert (format "[%c] %-18s"
2252                          (if (equal handle preferred) ?* ? )
2253                          (car (mm-handle-type handle))))
2254          (point))
2255        `(local-map ,gnus-mime-button-map
2256                    keymap ,gnus-mime-button-map
2257                    gnus-callback
2258                    (lambda (handles)
2259                      (gnus-mime-display-alternative
2260                       ',ihandles ,(car (mm-handle-type handle))))
2261                    gnus-data ,handle))
2262       (insert "  "))
2263     (insert "\n\n")
2264     (when preferred
2265       (mm-display-part preferred))))
2266
2267 (defun gnus-article-wash-status ()
2268   "Return a string which display status of article washing."
2269   (save-excursion
2270     (set-buffer gnus-article-buffer)
2271     (let ((cite (gnus-article-hidden-text-p 'cite))
2272           (headers (gnus-article-hidden-text-p 'headers))
2273           (boring (gnus-article-hidden-text-p 'boring-headers))
2274           (pgp (gnus-article-hidden-text-p 'pgp))
2275           (pem (gnus-article-hidden-text-p 'pem))
2276           (signature (gnus-article-hidden-text-p 'signature))
2277           (overstrike (gnus-article-hidden-text-p 'overstrike))
2278           (emphasis (gnus-article-hidden-text-p 'emphasis)))
2279       (format "%c%c%c%c%c%c%c"
2280               (if cite ?c ? )
2281               (if (or headers boring) ?h ? )
2282               (if (or pgp pem) ?p ? )
2283               (if signature ?s ? )
2284               (if overstrike ?o ? )
2285               (if emphasis ?e ? )))))
2286
2287 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2288
2289 (defun gnus-article-maybe-hide-headers ()
2290   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2291 Provided for backwards compatibility."
2292   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2293       gnus-inhibit-hiding
2294       (gnus-article-hide-headers)))
2295
2296 ;;; Article savers.
2297
2298 (defun gnus-output-to-file (file-name)
2299   "Append the current article to a file named FILE-NAME."
2300   (let ((artbuf (current-buffer)))
2301     (with-temp-buffer
2302       (insert-buffer-substring artbuf)
2303       ;; Append newline at end of the buffer as separator, and then
2304       ;; save it to file.
2305       (goto-char (point-max))
2306       (insert "\n")
2307       (append-to-file (point-min) (point-max) file-name)
2308       t)))
2309
2310 (defun gnus-narrow-to-page (&optional arg)
2311   "Narrow the article buffer to a page.
2312 If given a numerical ARG, move forward ARG pages."
2313   (interactive "P")
2314   (setq arg (if arg (prefix-numeric-value arg) 0))
2315   (save-excursion
2316     (set-buffer gnus-article-buffer)
2317     (goto-char (point-min))
2318     (widen)
2319     ;; Remove any old next/prev buttons.
2320     (when (gnus-visual-p 'page-marker)
2321       (let ((buffer-read-only nil))
2322         (gnus-remove-text-with-property 'gnus-prev)
2323         (gnus-remove-text-with-property 'gnus-next)))
2324     (when
2325         (cond ((< arg 0)
2326                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2327               ((> arg 0)
2328                (re-search-forward page-delimiter nil 'move arg)))
2329       (goto-char (match-end 0)))
2330     (narrow-to-region
2331      (point)
2332      (if (re-search-forward page-delimiter nil 'move)
2333          (match-beginning 0)
2334        (point)))
2335     (when (and (gnus-visual-p 'page-marker)
2336                (not (= (point-min) 1)))
2337       (save-excursion
2338         (goto-char (point-min))
2339         (gnus-insert-prev-page-button)))
2340     (when (and (gnus-visual-p 'page-marker)
2341                (< (+ (point-max) 2) (buffer-size)))
2342       (save-excursion
2343         (goto-char (point-max))
2344         (gnus-insert-next-page-button)))))
2345
2346 ;; Article mode commands
2347
2348 (defun gnus-article-goto-next-page ()
2349   "Show the next page of the article."
2350   (interactive)
2351   (when (gnus-article-next-page)
2352     (goto-char (point-min))
2353     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2354
2355 (defun gnus-article-goto-prev-page ()
2356   "Show the next page of the article."
2357   (interactive)
2358   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2359     (gnus-article-prev-page nil)))
2360
2361 (defun gnus-article-next-page (&optional lines)
2362   "Show the next page of the current article.
2363 If end of article, return non-nil.  Otherwise return nil.
2364 Argument LINES specifies lines to be scrolled up."
2365   (interactive "p")
2366   (move-to-window-line -1)
2367   (if (save-excursion
2368         (end-of-line)
2369         (and (pos-visible-in-window-p)  ;Not continuation line.
2370              (eobp)))
2371       ;; Nothing in this page.
2372       (if (or (not gnus-page-broken)
2373               (save-excursion
2374                 (save-restriction
2375                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2376           t                             ;Nothing more.
2377         (gnus-narrow-to-page 1)         ;Go to next page.
2378         nil)
2379     ;; More in this page.
2380     (let ((scroll-in-place nil))
2381       (condition-case ()
2382           (scroll-up lines)
2383         (end-of-buffer
2384          ;; Long lines may cause an end-of-buffer error.
2385          (goto-char (point-max)))))
2386     (move-to-window-line 0)
2387     nil))
2388
2389 (defun gnus-article-prev-page (&optional lines)
2390   "Show previous page of current article.
2391 Argument LINES specifies lines to be scrolled down."
2392   (interactive "p")
2393   (move-to-window-line 0)
2394   (if (and gnus-page-broken
2395            (bobp)
2396            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2397       (progn
2398         (gnus-narrow-to-page -1)        ;Go to previous page.
2399         (goto-char (point-max))
2400         (recenter -1))
2401     (let ((scroll-in-place nil))
2402       (prog1
2403           (condition-case ()
2404               (scroll-down lines)
2405             (beginning-of-buffer
2406              (goto-char (point-min))))
2407         (move-to-window-line 0)))))
2408
2409 (defun gnus-article-refer-article ()
2410   "Read article specified by message-id around point."
2411   (interactive)
2412   (let ((point (point)))
2413     (search-forward ">" nil t)          ;Move point to end of "<....>".
2414     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2415         (let ((message-id (match-string 1)))
2416           (goto-char point)
2417           (set-buffer gnus-summary-buffer)
2418           (gnus-summary-refer-article message-id))
2419       (goto-char (point))
2420       (error "No references around point"))))
2421
2422 (defun gnus-article-show-summary ()
2423   "Reconfigure windows to show summary buffer."
2424   (interactive)
2425   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2426       (error "There is no summary buffer for this article buffer")
2427     (gnus-article-set-globals)
2428     (gnus-configure-windows 'article)
2429     (gnus-summary-goto-subject gnus-current-article)
2430     (gnus-summary-position-point)))
2431
2432 (defun gnus-article-describe-briefly ()
2433   "Describe article mode commands briefly."
2434   (interactive)
2435   (gnus-message 6
2436                 (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")))
2437
2438 (defun gnus-article-summary-command ()
2439   "Execute the last keystroke in the summary buffer."
2440   (interactive)
2441   (let ((obuf (current-buffer))
2442         (owin (current-window-configuration))
2443         func)
2444     (switch-to-buffer gnus-article-current-summary 'norecord)
2445     (setq func (lookup-key (current-local-map) (this-command-keys)))
2446     (call-interactively func)
2447     (set-buffer obuf)
2448     (set-window-configuration owin)
2449     (set-window-point (get-buffer-window (current-buffer)) (point))))
2450
2451 (defun gnus-article-summary-command-nosave ()
2452   "Execute the last keystroke in the summary buffer."
2453   (interactive)
2454   (let (func)
2455     (pop-to-buffer gnus-article-current-summary 'norecord)
2456     (setq func (lookup-key (current-local-map) (this-command-keys)))
2457     (call-interactively func)))
2458
2459 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2460   "Read a summary buffer key sequence and execute it from the article buffer."
2461   (interactive "P")
2462   (let ((nosaves
2463          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2464            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2465            "=" "^" "\M-^" "|"))
2466         (nosave-but-article
2467          '("A\r"))
2468         (nosave-in-article
2469          '("\C-d"))
2470         (up-to-top
2471          '("n" "Gn" "p" "Gp"))
2472         keys new-sum-point)
2473     (save-excursion
2474       (set-buffer gnus-article-current-summary)
2475       (let (gnus-pick-mode)
2476         (push (or key last-command-event) unread-command-events)
2477         (setq keys (read-key-sequence nil))))
2478     (message "")
2479
2480     (if (or (member keys nosaves)
2481             (member keys nosave-but-article)
2482             (member keys nosave-in-article))
2483         (let (func)
2484           (save-window-excursion
2485             (pop-to-buffer gnus-article-current-summary 'norecord)
2486             ;; We disable the pick minor mode commands.
2487             (let (gnus-pick-mode)
2488               (setq func (lookup-key (current-local-map) keys))))
2489           (if (not func)
2490               (ding)
2491             (unless (member keys nosave-in-article)
2492               (set-buffer gnus-article-current-summary))
2493             (call-interactively func)
2494             (setq new-sum-point (point)))
2495           (when (member keys nosave-but-article)
2496             (pop-to-buffer gnus-article-buffer 'norecord)))
2497       ;; These commands should restore window configuration.
2498       (let ((obuf (current-buffer))
2499             (owin (current-window-configuration))
2500             (opoint (point))
2501             (summary gnus-article-current-summary)
2502             func in-buffer selected)
2503         (if not-restore-window
2504             (pop-to-buffer summary 'norecord)
2505           (switch-to-buffer summary 'norecord))
2506         (setq in-buffer (current-buffer))
2507         ;; We disable the pick minor mode commands.
2508         (if (setq func (let (gnus-pick-mode)
2509                          (lookup-key (current-local-map) keys)))
2510             (progn
2511               (call-interactively func)
2512               (setq new-sum-point (point)))
2513           (ding))
2514         (when (eq in-buffer (current-buffer))
2515           (setq selected (gnus-summary-select-article))
2516           (set-buffer obuf)
2517           (unless not-restore-window
2518             (set-window-configuration owin))
2519           (unless (or (not (eq selected 'old)) (member keys up-to-top))
2520             (set-window-point (get-buffer-window (current-buffer))
2521                               opoint))
2522           (let ((win (get-buffer-window gnus-article-current-summary)))
2523             (when win
2524               (set-window-point win new-sum-point))))))))
2525
2526 (defun gnus-article-hide (&optional arg force)
2527   "Hide all the gruft in the current article.
2528 This means that PGP stuff, signatures, cited text and (some)
2529 headers will be hidden.
2530 If given a prefix, show the hidden text instead."
2531   (interactive (append (gnus-article-hidden-arg) (list 'force)))
2532   (gnus-article-hide-headers arg)
2533   (gnus-article-hide-pgp arg)
2534   (gnus-article-hide-citation-maybe arg force)
2535   (gnus-article-hide-signature arg))
2536
2537 (defun gnus-article-maybe-highlight ()
2538   "Do some article highlighting if article highlighting is requested."
2539   (when (gnus-visual-p 'article-highlight 'highlight)
2540     (gnus-article-highlight-some)))
2541
2542 (defun gnus-check-group-server ()
2543   ;; Make sure the connection to the server is alive.
2544   (unless (gnus-server-opened
2545            (gnus-find-method-for-group gnus-newsgroup-name))
2546     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
2547     (gnus-request-group gnus-newsgroup-name t)))
2548
2549 (defun gnus-request-article-this-buffer (article group)
2550   "Get an article and insert it into this buffer."
2551   (let (do-update-line sparse-header)
2552     (prog1
2553         (save-excursion
2554           (erase-buffer)
2555           (gnus-kill-all-overlays)
2556           (setq group (or group gnus-newsgroup-name))
2557
2558           ;; Using `gnus-request-article' directly will insert the article into
2559           ;; `nntp-server-buffer' - so we'll save some time by not having to
2560           ;; copy it from the server buffer into the article buffer.
2561
2562           ;; We only request an article by message-id when we do not have the
2563           ;; headers for it, so we'll have to get those.
2564           (when (stringp article)
2565             (let ((gnus-override-method gnus-refer-article-method))
2566               (gnus-read-header article)))
2567
2568           ;; If the article number is negative, that means that this article
2569           ;; doesn't belong in this newsgroup (possibly), so we find its
2570           ;; message-id and request it by id instead of number.
2571           (when (and (numberp article)
2572                      gnus-summary-buffer
2573                      (get-buffer gnus-summary-buffer)
2574                      (gnus-buffer-exists-p gnus-summary-buffer))
2575             (save-excursion
2576               (set-buffer gnus-summary-buffer)
2577               (let ((header (gnus-summary-article-header article)))
2578                 (when (< article 0)
2579                   (cond
2580                    ((memq article gnus-newsgroup-sparse)
2581                     ;; This is a sparse gap article.
2582                     (setq do-update-line article)
2583                     (setq article (mail-header-id header))
2584                     (let ((gnus-override-method gnus-refer-article-method))
2585                       (setq sparse-header (gnus-read-header article)))
2586                     (setq gnus-newsgroup-sparse
2587                           (delq article gnus-newsgroup-sparse)))
2588                    ((vectorp header)
2589                     ;; It's a real article.
2590                     (setq article (mail-header-id header)))
2591                    (t
2592                     ;; It is an extracted pseudo-article.
2593                     (setq article 'pseudo)
2594                     (gnus-request-pseudo-article header))))
2595
2596                 (let ((method (gnus-find-method-for-group
2597                                gnus-newsgroup-name)))
2598                   (when (and (eq (car method) 'nneething)
2599                              (vectorp header))
2600                     (let ((dir (concat
2601                                 (file-name-as-directory
2602                                  (or (cadr (assq 'nneething-address method))
2603                                      (nth 1 method)))
2604                                 (mail-header-subject header))))
2605                       (when (file-directory-p dir)
2606                         (setq article 'nneething)
2607                         (gnus-group-enter-directory dir))))))))
2608
2609           (cond
2610            ;; Refuse to select canceled articles.
2611            ((and (numberp article)
2612                  gnus-summary-buffer
2613                  (get-buffer gnus-summary-buffer)
2614                  (gnus-buffer-exists-p gnus-summary-buffer)
2615                  (eq (cdr (save-excursion
2616                             (set-buffer gnus-summary-buffer)
2617                             (assq article gnus-newsgroup-reads)))
2618                      gnus-canceled-mark))
2619             nil)
2620            ;; We first check `gnus-original-article-buffer'.
2621            ((and (get-buffer gnus-original-article-buffer)
2622                  (numberp article)
2623                  (save-excursion
2624                    (set-buffer gnus-original-article-buffer)
2625                    (and (equal (car gnus-original-article) group)
2626                         (eq (cdr gnus-original-article) article))))
2627             (insert-buffer-substring gnus-original-article-buffer)
2628             'article)
2629            ;; Check the backlog.
2630            ((and gnus-keep-backlog
2631                  (gnus-backlog-request-article group article (current-buffer)))
2632             'article)
2633            ;; Check asynchronous pre-fetch.
2634            ((gnus-async-request-fetched-article group article (current-buffer))
2635             (gnus-async-prefetch-next group article gnus-summary-buffer)
2636             (when (and (numberp article) gnus-keep-backlog)
2637               (gnus-backlog-enter-article group article (current-buffer)))
2638             'article)
2639            ;; Check the cache.
2640            ((and gnus-use-cache
2641                  (numberp article)
2642                  (gnus-cache-request-article article group))
2643             'article)
2644            ;; Get the article and put into the article buffer.
2645            ((or (stringp article) (numberp article))
2646             (let ((gnus-override-method
2647                    (and (stringp article) gnus-refer-article-method))
2648                   (buffer-read-only nil))
2649               (erase-buffer)
2650               (gnus-kill-all-overlays)
2651               (gnus-check-group-server)
2652               (when (gnus-request-article article group (current-buffer))
2653                 (when (numberp article)
2654                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2655                   (when gnus-keep-backlog
2656                     (gnus-backlog-enter-article
2657                      group article (current-buffer))))
2658                 'article)))
2659            ;; It was a pseudo.
2660            (t article)))
2661
2662       ;; Associate this article with the current summary buffer.
2663       (setq gnus-article-current-summary gnus-summary-buffer)
2664
2665       ;; Take the article from the original article buffer
2666       ;; and place it in the buffer it's supposed to be in.
2667       (when (and (get-buffer gnus-article-buffer)
2668                  (equal (buffer-name (current-buffer))
2669                         (buffer-name (get-buffer gnus-article-buffer))))
2670         (save-excursion
2671           (if (get-buffer gnus-original-article-buffer)
2672               (set-buffer gnus-original-article-buffer)
2673             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2674             (buffer-disable-undo (current-buffer))
2675             (setq major-mode 'gnus-original-article-mode)
2676             (setq buffer-read-only t))
2677           (let (buffer-read-only)
2678             (erase-buffer)
2679             (insert-buffer-substring gnus-article-buffer))
2680           (setq gnus-original-article (cons group article)))
2681
2682         ;; Decode charsets.
2683         (run-hooks 'gnus-article-decode-hook))
2684       
2685       ;; Update sparse articles.
2686       (when (and do-update-line
2687                  (or (numberp article)
2688                      (stringp article)))
2689         (let ((buf (current-buffer)))
2690           (set-buffer gnus-summary-buffer)
2691           (gnus-summary-update-article do-update-line sparse-header)
2692           (gnus-summary-goto-subject do-update-line nil t)
2693           (set-window-point (get-buffer-window (current-buffer) t)
2694                             (point))
2695           (set-buffer buf))))))
2696
2697 ;;;
2698 ;;; Article editing
2699 ;;;
2700
2701 (defcustom gnus-article-edit-mode-hook nil
2702   "Hook run in article edit mode buffers."
2703   :group 'gnus-article-various
2704   :type 'hook)
2705
2706 (defvar gnus-article-edit-done-function nil)
2707
2708 (defvar gnus-article-edit-mode-map nil)
2709
2710 (unless gnus-article-edit-mode-map
2711   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2712
2713   (gnus-define-keys gnus-article-edit-mode-map
2714     "\C-c\C-c" gnus-article-edit-done
2715     "\C-c\C-k" gnus-article-edit-exit)
2716
2717   (gnus-define-keys (gnus-article-edit-wash-map
2718                      "\C-c\C-w" gnus-article-edit-mode-map)
2719     "f" gnus-article-edit-full-stops))
2720
2721 (defun gnus-article-edit-mode ()
2722   "Major mode for editing articles.
2723 This is an extended text-mode.
2724
2725 \\{gnus-article-edit-mode-map}"
2726   (interactive)
2727   (setq major-mode 'gnus-article-edit-mode)
2728   (setq mode-name "Article Edit")
2729   (use-local-map gnus-article-edit-mode-map)
2730   (make-local-variable 'gnus-article-edit-done-function)
2731   (make-local-variable 'gnus-prev-winconf)
2732   (setq buffer-read-only nil)
2733   (buffer-enable-undo)
2734   (widen)
2735   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
2736
2737 (defun gnus-article-edit (&optional force)
2738   "Edit the current article.
2739 This will have permanent effect only in mail groups.
2740 If FORCE is non-nil, allow editing of articles even in read-only
2741 groups."
2742   (interactive "P")
2743   (when (and (not force)
2744              (gnus-group-read-only-p))
2745     (error "The current newsgroup does not support article editing"))
2746   (gnus-article-date-original)
2747   (gnus-article-edit-article
2748    `(lambda (no-highlight)
2749       (gnus-summary-edit-article-done
2750        ,(or (mail-header-references gnus-current-headers) "")
2751        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2752
2753 (defun gnus-article-edit-article (exit-func)
2754   "Start editing the contents of the current article buffer."
2755   (let ((winconf (current-window-configuration)))
2756     (set-buffer gnus-article-buffer)
2757     (gnus-article-edit-mode)
2758     (gnus-article-delete-text-of-type 'annotation)
2759     (gnus-set-text-properties (point-min) (point-max) nil)
2760     (gnus-configure-windows 'edit-article)
2761     (setq gnus-article-edit-done-function exit-func)
2762     (setq gnus-prev-winconf winconf)
2763     (gnus-message 6 "C-c C-c to end edits")))
2764
2765 (defun gnus-article-edit-done (&optional arg)
2766   "Update the article edits and exit."
2767   (interactive "P")
2768   (save-excursion
2769     (save-restriction
2770       (widen)
2771       (goto-char (point-min))
2772       (when (search-forward "\n\n" nil 1)
2773         (let ((lines (count-lines (point) (point-max)))
2774               (length (- (point-max) (point)))
2775               (case-fold-search t)
2776               (body (copy-marker (point))))
2777           (goto-char (point-min))
2778           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
2779             (delete-region (match-beginning 1) (match-end 1))
2780             (insert (number-to-string length)))
2781           (goto-char (point-min))
2782           (when (re-search-forward
2783                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
2784             (delete-region (match-beginning 1) (match-end 1))
2785             (insert (number-to-string length)))
2786           (goto-char (point-min))
2787           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
2788             (delete-region (match-beginning 1) (match-end 1))
2789             (insert (number-to-string lines)))))))
2790   (let ((func gnus-article-edit-done-function)
2791         (buf (current-buffer))
2792         (start (window-start)))
2793     (gnus-article-edit-exit)
2794     (save-excursion
2795       (set-buffer buf)
2796       (let ((buffer-read-only nil))
2797         (funcall func arg)))
2798     (set-buffer buf)
2799     (set-window-start (get-buffer-window buf) start)
2800     (set-window-point (get-buffer-window buf) (point))))
2801
2802 (defun gnus-article-edit-exit ()
2803   "Exit the article editing without updating."
2804   (interactive)
2805   ;; We remove all text props from the article buffer.
2806   (let ((buf (format "%s" (buffer-string)))
2807         (curbuf (current-buffer))
2808         (p (point))
2809         (window-start (window-start)))
2810     (erase-buffer)
2811     (insert buf)
2812     (let ((winconf gnus-prev-winconf))
2813       (gnus-article-mode)
2814       ;; The cache and backlog have to be flushed somewhat.
2815       (when gnus-use-cache
2816         (gnus-cache-update-article
2817          (car gnus-article-current) (cdr gnus-article-current)))
2818       (when gnus-keep-backlog
2819         (gnus-backlog-remove-article
2820          (car gnus-article-current) (cdr gnus-article-current)))
2821       ;; Flush original article as well.
2822       (save-excursion
2823         (when (get-buffer gnus-original-article-buffer)
2824           (set-buffer gnus-original-article-buffer)
2825           (setq gnus-original-article nil)))
2826       (set-window-configuration winconf)
2827       ;; Tippy-toe some to make sure that point remains where it was.
2828       (let ((buf (current-buffer)))
2829         (set-buffer curbuf)
2830         (set-window-start (get-buffer-window (current-buffer)) window-start)
2831         (goto-char p)
2832         (set-buffer buf)))))
2833
2834 (defun gnus-article-edit-full-stops ()
2835   "Interactively repair spacing at end of sentences."
2836   (interactive)
2837   (save-excursion
2838     (goto-char (point-min))
2839     (search-forward-regexp "^$" nil t)
2840     (let ((case-fold-search nil))
2841       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2842
2843 ;;;
2844 ;;; Article highlights
2845 ;;;
2846
2847 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2848
2849 ;;; Internal Variables:
2850
2851 (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\\)"
2852   "Regular expression that matches URLs."
2853   :group 'gnus-article-buttons
2854   :type 'regexp)
2855
2856 (defcustom gnus-button-alist
2857   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2858      gnus-button-message-id 2)
2859     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2860     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
2861      1 t
2862      gnus-button-fetch-group 4)
2863     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2864     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2865      t gnus-button-message-id 3)
2866     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2867     ("mailto:\\([a-zA-Z.-@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
2868     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2869     ;; This is how URLs _should_ be embedded in text...
2870     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2871     ;; Raw URLs.
2872     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2873   "*Alist of regexps matching buttons in article bodies.
2874
2875 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2876 REGEXP: is the string matching text around the button,
2877 BUTTON: is the number of the regexp grouping actually matching the button,
2878 FORM: is a lisp expression which must eval to true for the button to
2879 be added,
2880 CALLBACK: is the function to call when the user push this button, and each
2881 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2882
2883 CALLBACK can also be a variable, in that case the value of that
2884 variable it the real callback function."
2885   :group 'gnus-article-buttons
2886   :type '(repeat (list regexp
2887                        (integer :tag "Button")
2888                        (sexp :tag "Form")
2889                        (function :tag "Callback")
2890                        (repeat :tag "Par"
2891                                :inline t
2892                                (integer :tag "Regexp group")))))
2893
2894 (defcustom gnus-header-button-alist
2895   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2896      0 t gnus-button-message-id 0)
2897     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2898     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2899      0 t gnus-button-mailto 0)
2900     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2901     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2902     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2903     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2904      gnus-button-message-id 3))
2905   "*Alist of headers and regexps to match buttons in article heads.
2906
2907 This alist is very similar to `gnus-button-alist', except that each
2908 alist has an additional HEADER element first in each entry:
2909
2910 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2911
2912 HEADER is a regexp to match a header.  For a fuller explanation, see
2913 `gnus-button-alist'."
2914   :group 'gnus-article-buttons
2915   :group 'gnus-article-headers
2916   :type '(repeat (list (regexp :tag "Header")
2917                        regexp
2918                        (integer :tag "Button")
2919                        (sexp :tag "Form")
2920                        (function :tag "Callback")
2921                        (repeat :tag "Par"
2922                                :inline t
2923                                (integer :tag "Regexp group")))))
2924
2925 (defvar gnus-button-regexp nil)
2926 (defvar gnus-button-marker-list nil)
2927 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2928
2929 (defvar gnus-button-last nil)
2930 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2931
2932 ;;; Commands:
2933
2934 (defun gnus-article-push-button (event)
2935   "Check text under the mouse pointer for a callback function.
2936 If the text under the mouse pointer has a `gnus-callback' property,
2937 call it with the value of the `gnus-data' text property."
2938   (interactive "e")
2939   (set-buffer (window-buffer (posn-window (event-start event))))
2940   (let* ((pos (posn-point (event-start event)))
2941          (data (get-text-property pos 'gnus-data))
2942          (fun (get-text-property pos 'gnus-callback)))
2943     (goto-char pos)
2944     (when fun
2945       (funcall fun data))))
2946
2947 (defun gnus-article-press-button ()
2948   "Check text at point for a callback function.
2949 If the text at point has a `gnus-callback' property,
2950 call it with the value of the `gnus-data' text property."
2951   (interactive)
2952   (let* ((data (get-text-property (point) 'gnus-data))
2953          (fun (get-text-property (point) 'gnus-callback)))
2954     (when fun
2955       (funcall fun data))))
2956
2957 (defun gnus-article-highlight (&optional force)
2958   "Highlight current article.
2959 This function calls `gnus-article-highlight-headers',
2960 `gnus-article-highlight-citation',
2961 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2962 do the highlighting.  See the documentation for those functions."
2963   (interactive (list 'force))
2964   (gnus-article-highlight-headers)
2965   (gnus-article-highlight-citation force)
2966   (gnus-article-highlight-signature)
2967   (gnus-article-add-buttons force)
2968   (gnus-article-add-buttons-to-head))
2969
2970 (defun gnus-article-highlight-some (&optional force)
2971   "Highlight current article.
2972 This function calls `gnus-article-highlight-headers',
2973 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2974 do the highlighting.  See the documentation for those functions."
2975   (interactive (list 'force))
2976   (gnus-article-highlight-headers)
2977   (gnus-article-highlight-signature)
2978   (gnus-article-add-buttons))
2979
2980 (defun gnus-article-highlight-headers ()
2981   "Highlight article headers as specified by `gnus-header-face-alist'."
2982   (interactive)
2983   (save-excursion
2984     (set-buffer gnus-article-buffer)
2985     (save-restriction
2986       (let ((alist gnus-header-face-alist)
2987             (buffer-read-only nil)
2988             (case-fold-search t)
2989             (inhibit-point-motion-hooks t)
2990             entry regexp header-face field-face from hpoints fpoints)
2991         (message-narrow-to-head)
2992         (while (setq entry (pop alist))
2993           (goto-char (point-min))
2994           (setq regexp (concat "^\\("
2995                                (if (string-equal "" (nth 0 entry))
2996                                    "[^\t ]"
2997                                  (nth 0 entry))
2998                                "\\)")
2999                 header-face (nth 1 entry)
3000                 field-face (nth 2 entry))
3001           (while (and (re-search-forward regexp nil t)
3002                       (not (eobp)))
3003             (beginning-of-line)
3004             (setq from (point))
3005             (unless (search-forward ":" nil t)
3006               (forward-char 1))
3007             (when (and header-face
3008                        (not (memq (point) hpoints)))
3009               (push (point) hpoints)
3010               (gnus-put-text-property from (point) 'face header-face))
3011             (when (and field-face
3012                        (not (memq (setq from (point)) fpoints)))
3013               (push from fpoints)
3014               (if (re-search-forward "^[^ \t]" nil t)
3015                   (forward-char -2)
3016                 (goto-char (point-max)))
3017               (gnus-put-text-property from (point) 'face field-face))))))))
3018
3019 (defun gnus-article-highlight-signature ()
3020   "Highlight the signature in an article.
3021 It does this by highlighting everything after
3022 `gnus-signature-separator' using `gnus-signature-face'."
3023   (interactive)
3024   (save-excursion
3025     (set-buffer gnus-article-buffer)
3026     (let ((buffer-read-only nil)
3027           (inhibit-point-motion-hooks t))
3028       (save-restriction
3029         (when (and gnus-signature-face
3030                    (gnus-article-narrow-to-signature))
3031           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
3032                             'face gnus-signature-face)
3033           (widen)
3034           (gnus-article-search-signature)
3035           (let ((start (match-beginning 0))
3036                 (end (set-marker (make-marker) (1+ (match-end 0)))))
3037             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
3038                                      end)))))))
3039
3040 (defun gnus-button-in-region-p (b e prop)
3041   "Say whether PROP exists in the region."
3042   (text-property-not-all b e prop nil))
3043
3044 (defun gnus-article-add-buttons (&optional force)
3045   "Find external references in the article and make buttons of them.
3046 \"External references\" are things like Message-IDs and URLs, as
3047 specified by `gnus-button-alist'."
3048   (interactive (list 'force))
3049   (save-excursion
3050     (set-buffer gnus-article-buffer)
3051     (let ((buffer-read-only nil)
3052           (inhibit-point-motion-hooks t)
3053           (case-fold-search t)
3054           (alist gnus-button-alist)
3055           beg entry regexp)
3056       ;; Remove all old markers.
3057       (let (marker entry)
3058         (while (setq marker (pop gnus-button-marker-list))
3059           (goto-char marker)
3060           (when (setq entry (gnus-button-entry))
3061             (put-text-property (match-beginning (nth 1 entry))
3062                                (match-end (nth 1 entry))
3063                                'gnus-callback nil))
3064           (set-marker marker nil)))
3065       ;; We skip the headers.
3066       (goto-char (point-min))
3067       (unless (search-forward "\n\n" nil t)
3068         (goto-char (point-max)))
3069       (setq beg (point))
3070       (while (setq entry (pop alist))
3071         (setq regexp (car entry))
3072         (goto-char beg)
3073         (while (re-search-forward regexp nil t)
3074           (let* ((start (and entry (match-beginning (nth 1 entry))))
3075                  (end (and entry (match-end (nth 1 entry))))
3076                  (from (match-beginning 0)))
3077             (when (and (or (eq t (nth 2 entry))
3078                            (eval (nth 2 entry)))
3079                        (not (gnus-button-in-region-p
3080                              start end 'gnus-callback)))
3081               ;; That optional form returned non-nil, so we add the
3082               ;; button.
3083               (gnus-article-add-button
3084                start end 'gnus-button-push
3085                (car (push (set-marker (make-marker) from)
3086                           gnus-button-marker-list))))))))))
3087
3088 ;; Add buttons to the head of an article.
3089 (defun gnus-article-add-buttons-to-head ()
3090   "Add buttons to the head of the article."
3091   (interactive)
3092   (save-excursion
3093     (set-buffer gnus-article-buffer)
3094     (let ((buffer-read-only nil)
3095           (inhibit-point-motion-hooks t)
3096           (case-fold-search t)
3097           (alist gnus-header-button-alist)
3098           entry beg end)
3099       (nnheader-narrow-to-headers)
3100       (while alist
3101         ;; Each alist entry.
3102         (setq entry (car alist)
3103               alist (cdr alist))
3104         (goto-char (point-min))
3105         (while (re-search-forward (car entry) nil t)
3106           ;; Each header matching the entry.
3107           (setq beg (match-beginning 0))
3108           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
3109                              (match-beginning 0))
3110                         (point-max)))
3111           (goto-char beg)
3112           (while (re-search-forward (nth 1 entry) end t)
3113             ;; Each match within a header.
3114             (let* ((entry (cdr entry))
3115                    (start (match-beginning (nth 1 entry)))
3116                    (end (match-end (nth 1 entry)))
3117                    (form (nth 2 entry)))
3118               (goto-char (match-end 0))
3119               (when (eval form)
3120                 (gnus-article-add-button
3121                  start end (nth 3 entry)
3122                  (buffer-substring (match-beginning (nth 4 entry))
3123                                    (match-end (nth 4 entry)))))))
3124           (goto-char end))))
3125     (widen)))
3126
3127 ;;; External functions:
3128
3129 (defun gnus-article-add-button (from to fun &optional data)
3130   "Create a button between FROM and TO with callback FUN and data DATA."
3131   (when gnus-article-button-face
3132     (gnus-overlay-put (gnus-make-overlay from to)
3133                       'face gnus-article-button-face))
3134   (gnus-add-text-properties
3135    from to
3136    (nconc (and gnus-article-mouse-face
3137                (list gnus-mouse-face-prop gnus-article-mouse-face))
3138           (list 'gnus-callback fun)
3139           (and data (list 'gnus-data data))))
3140   (widget-convert-text 'link from to from to
3141                        :action 'gnus-widget-press-button))
3142
3143 ;;; Internal functions:
3144
3145 (defun gnus-article-set-globals ()
3146   (save-excursion
3147     (set-buffer gnus-summary-buffer)
3148     (gnus-set-global-variables)))
3149
3150 (defun gnus-signature-toggle (end)
3151   (save-excursion
3152     (set-buffer gnus-article-buffer)
3153     (let ((buffer-read-only nil)
3154           (inhibit-point-motion-hooks t))
3155       (if (get-text-property end 'invisible)
3156           (gnus-article-unhide-text end (point-max))
3157         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3158
3159 (defun gnus-button-entry ()
3160   ;; Return the first entry in `gnus-button-alist' matching this place.
3161   (let ((alist gnus-button-alist)
3162         (entry nil))
3163     (while alist
3164       (setq entry (pop alist))
3165       (if (looking-at (car entry))
3166           (setq alist nil)
3167         (setq entry nil)))
3168     entry))
3169
3170 (defun gnus-button-push (marker)
3171   ;; Push button starting at MARKER.
3172   (save-excursion
3173     (set-buffer gnus-article-buffer)
3174     (goto-char marker)
3175     (let* ((entry (gnus-button-entry))
3176            (inhibit-point-motion-hooks t)
3177            (fun (nth 3 entry))
3178            (args (mapcar (lambda (group)
3179                            (let ((string (match-string group)))
3180                              (gnus-set-text-properties
3181                               0 (length string) nil string)
3182                              string))
3183                          (nthcdr 4 entry))))
3184       (cond
3185        ((fboundp fun)
3186         (apply fun args))
3187        ((and (boundp fun)
3188              (fboundp (symbol-value fun)))
3189         (apply (symbol-value fun) args))
3190        (t
3191         (gnus-message 1 "You must define `%S' to use this button"
3192                       (cons fun args)))))))
3193
3194 (defun gnus-button-message-id (message-id)
3195   "Fetch MESSAGE-ID."
3196   (save-excursion
3197     (set-buffer gnus-summary-buffer)
3198     (gnus-summary-refer-article message-id)))
3199
3200 (defun gnus-button-fetch-group (address)
3201   "Fetch GROUP specified by ADDRESS."
3202   (if (not (string-match "[:/]" address))
3203       ;; This is just a simple group url.
3204       (gnus-group-read-ephemeral-group address gnus-select-method)
3205     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3206                            address))
3207         (error "Can't parse %s" address)
3208       (gnus-group-read-ephemeral-group
3209        (match-string 4 address)
3210        `(nntp ,(match-string 1 address)
3211               (nntp-address ,(match-string 1 address))
3212               (nntp-port-number ,(if (match-end 3)
3213                                      (match-string 3 address)
3214                                    "nntp")))))))
3215
3216 (defun gnus-url-parse-query-string (query &optional downcase)
3217   (let (retval pairs cur key val)
3218     (setq pairs (split-string query "&"))
3219     (while pairs
3220       (setq cur (car pairs)
3221             pairs (cdr pairs))
3222       (if (not (string-match "=" cur))
3223           nil                           ; Grace
3224         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3225               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3226         (if downcase
3227             (setq key (downcase key)))
3228         (setq cur (assoc key retval))
3229         (if cur
3230             (setcdr cur (cons val (cdr cur)))
3231           (setq retval (cons (list key val) retval)))))
3232     retval))
3233
3234 (defun gnus-url-unhex (x)
3235   (if (> x ?9)
3236       (if (>= x ?a)
3237           (+ 10 (- x ?a))
3238         (+ 10 (- x ?A)))
3239     (- x ?0)))
3240
3241 (defun gnus-url-unhex-string (str &optional allow-newlines)
3242   "Remove %XXX embedded spaces, etc in a url.
3243 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3244 decoding of carriage returns and line feeds in the string, which is normally
3245 forbidden in URL encoding."
3246   (setq str (or str ""))
3247   (let ((tmp "")
3248         (case-fold-search t))
3249     (while (string-match "%[0-9a-f][0-9a-f]" str)
3250       (let* ((start (match-beginning 0))
3251              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3252              (code (+ (* 16 ch1)
3253                       (gnus-url-unhex (elt str (+ start 2))))))
3254         (setq tmp (concat
3255                    tmp (substring str 0 start)
3256                    (cond
3257                     (allow-newlines
3258                      (char-to-string code))
3259                     ((or (= code ?\n) (= code ?\r))
3260                      " ")
3261                     (t (char-to-string code))))
3262               str (substring str (match-end 0)))))
3263     (setq tmp (concat tmp str))
3264     tmp))
3265
3266 (defun gnus-url-mailto (url)
3267   ;; Send mail to someone
3268   (when (string-match "mailto:/*\\(.*\\)" url)
3269     (setq url (substring url (match-beginning 1) nil)))
3270   (let (to args subject func)
3271     (if (string-match (regexp-quote "?") url)
3272         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3273               args (gnus-url-parse-query-string
3274                     (substring url (match-end 0) nil) t))
3275       (setq to (gnus-url-unhex-string url)))
3276     (setq args (cons (list "to" to) args)
3277           subject (cdr-safe (assoc "subject" args)))
3278     (message-mail)
3279     (while args
3280       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3281       (if (fboundp func)
3282           (funcall func)
3283         (message-position-on-field (caar args)))
3284       (insert (mapconcat 'identity (cdar args) ", "))
3285       (setq args (cdr args)))
3286     (if subject
3287         (message-goto-body)
3288       (message-goto-subject))))
3289
3290 (defun gnus-button-mailto (address)
3291   ;; Mail to ADDRESS.
3292   (set-buffer (gnus-copy-article-buffer))
3293   (message-reply address))
3294
3295 (defun gnus-button-reply (address)
3296   ;; Reply to ADDRESS.
3297   (message-reply address))
3298
3299 (defun gnus-button-url (address)
3300   "Browse ADDRESS."
3301   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3302   (if (listp browse-url-browser-function)
3303       (browse-url address)
3304     (funcall browse-url-browser-function address)))
3305
3306 (defun gnus-button-embedded-url (address)
3307   "Browse ADDRESS."
3308   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3309   (if (listp browse-url-browser-function)
3310       (browse-url (gnus-strip-whitespace address))
3311     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
3312
3313 ;;; Next/prev buttons in the article buffer.
3314
3315 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3316 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3317
3318 (defvar gnus-prev-page-map nil)
3319 (unless gnus-prev-page-map
3320   (setq gnus-prev-page-map (make-sparse-keymap))
3321   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3322   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3323
3324 (defun gnus-insert-prev-page-button ()
3325   (let ((buffer-read-only nil))
3326     (gnus-eval-format
3327      gnus-prev-page-line-format nil
3328      `(gnus-prev t local-map ,gnus-prev-page-map
3329                  gnus-callback gnus-article-button-prev-page
3330                  gnus-type annotation))))
3331
3332 (defvar gnus-next-page-map nil)
3333 (unless gnus-next-page-map
3334   (setq gnus-next-page-map (make-keymap))
3335   (suppress-keymap gnus-prev-page-map)
3336   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3337   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3338
3339 (defun gnus-button-next-page ()
3340   "Go to the next page."
3341   (interactive)
3342   (let ((win (selected-window)))
3343     (select-window (get-buffer-window gnus-article-buffer t))
3344     (gnus-article-next-page)
3345     (select-window win)))
3346
3347 (defun gnus-button-prev-page ()
3348   "Go to the prev page."
3349   (interactive)
3350   (let ((win (selected-window)))
3351     (select-window (get-buffer-window gnus-article-buffer t))
3352     (gnus-article-prev-page)
3353     (select-window win)))
3354
3355 (defun gnus-insert-next-page-button ()
3356   (let ((buffer-read-only nil))
3357     (gnus-eval-format gnus-next-page-line-format nil
3358                       `(gnus-next
3359                         t local-map ,gnus-next-page-map
3360                         gnus-callback gnus-article-button-next-page
3361                         gnus-type annotation))))
3362
3363 (defun gnus-article-button-next-page (arg)
3364   "Go to the next page."
3365   (interactive "P")
3366   (let ((win (selected-window)))
3367     (select-window (get-buffer-window gnus-article-buffer t))
3368     (gnus-article-next-page)
3369     (select-window win)))
3370
3371 (defun gnus-article-button-prev-page (arg)
3372   "Go to the prev page."
3373   (interactive "P")
3374   (let ((win (selected-window)))
3375     (select-window (get-buffer-window gnus-article-buffer t))
3376     (gnus-article-prev-page)
3377     (select-window win)))
3378
3379 (gnus-ems-redefine)
3380
3381 (provide 'gnus-art)
3382
3383 (run-hooks 'gnus-art-load-hook)
3384
3385 ;;; gnus-art.el ends here