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