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