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