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