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