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