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