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