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