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