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