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