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