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