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