modify header.
[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"
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)
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             (insert (article-make-date-line date type))
1311             ;; Do highlighting.
1312             (beginning-of-line)
1313             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1314               (put-text-property (match-beginning 1) (1+ (match-end 1))
1315                                  'face bface)
1316               (put-text-property (match-beginning 2) (match-end 2)
1317                                  'face eface))))))))
1318
1319 (defun article-make-date-line (date type)
1320   "Return a DATE line of TYPE."
1321   (cond
1322    ;; Convert to the local timezone.  We have to slap a
1323    ;; `condition-case' round the calls to the timezone
1324    ;; functions since they aren't particularly resistant to
1325    ;; buggy dates.
1326    ((eq type 'local)
1327     (concat "Date: " (condition-case ()
1328                          (timezone-make-date-arpa-standard date)
1329                        (error date))))
1330    ;; Convert to Universal Time.
1331    ((eq type 'ut)
1332     (concat "Date: "
1333             (condition-case ()
1334                 (timezone-make-date-arpa-standard date nil "UT")
1335               (error date))))
1336    ;; Get the original date from the article.
1337    ((eq type 'original)
1338     (concat "Date: " date))
1339    ;; Let the user define the format.
1340    ((eq type 'user)
1341     (concat
1342      "Date: "
1343      (format-time-string gnus-article-time-format
1344                          (ignore-errors
1345                            (gnus-encode-date
1346                             (timezone-make-date-arpa-standard
1347                              date nil "UT"))))))
1348    ;; Do an X-Sent lapsed format.
1349    ((eq type 'lapsed)
1350     ;; If the date is seriously mangled, the timezone functions are
1351     ;; liable to bug out, so we ignore all errors.
1352     (let* ((now (current-time))
1353            (real-time
1354             (ignore-errors
1355               (gnus-time-minus
1356                (gnus-encode-date
1357                 (timezone-make-date-arpa-standard
1358                  (current-time-string now)
1359                  (current-time-zone now) "UT"))
1360                (gnus-encode-date
1361                 (timezone-make-date-arpa-standard
1362                  date nil "UT")))))
1363            (real-sec (and real-time
1364                           (+ (* (float (car real-time)) 65536)
1365                              (cadr real-time))))
1366            (sec (and real-time (abs real-sec)))
1367            num prev)
1368       (cond
1369        ((null real-time)
1370         "X-Sent: Unknown\n")
1371        ((zerop sec)
1372         "X-Sent: Now\n")
1373        (t
1374         (concat
1375          "X-Sent: "
1376          ;; This is a bit convoluted, but basically we go
1377          ;; through the time units for years, weeks, etc,
1378          ;; and divide things to see whether that results
1379          ;; in positive answers.
1380          (mapconcat
1381           (lambda (unit)
1382             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1383                 ;; The (remaining) seconds are too few to
1384                 ;; be divided into this time unit.
1385                 ""
1386               ;; It's big enough, so we output it.
1387               (setq sec (- sec (* num (cdr unit))))
1388               (prog1
1389                   (concat (if prev ", " "") (int-to-string
1390                                              (floor num))
1391                           " " (symbol-name (car unit))
1392                           (if (> num 1) "s" ""))
1393                 (setq prev t))))
1394           article-time-units "")
1395          ;; If dates are odd, then it might appear like the
1396          ;; article was sent in the future.
1397          (if (> real-sec 0)
1398              " ago"
1399            " in the future"))))))
1400    (t
1401     (error "Unknown conversion type: %s" type))))
1402
1403 (defun article-date-local (&optional highlight)
1404   "Convert the current article date to the local timezone."
1405   (interactive (list t))
1406   (article-date-ut 'local highlight))
1407
1408 (defun article-date-original (&optional highlight)
1409   "Convert the current article date to what it was originally.
1410 This is only useful if you have used some other date conversion
1411 function and want to see what the date was before converting."
1412   (interactive (list t))
1413   (article-date-ut 'original highlight))
1414
1415 (defun article-date-lapsed (&optional highlight)
1416   "Convert the current article date to time lapsed since it was sent."
1417   (interactive (list t))
1418   (article-date-ut 'lapsed highlight))
1419
1420 (defun article-update-date-lapsed ()
1421   "Function to be run from a timer to update the lapsed time line."
1422   (save-excursion
1423     (when (gnus-buffer-live-p gnus-article-buffer)
1424       (set-buffer gnus-article-buffer)
1425       (goto-char (point-min))
1426       (when (re-search-forward "^X-Sent:" nil t)
1427         (article-date-lapsed t)))))
1428
1429 (defun gnus-start-date-timer ()
1430   "Start a timer to update the X-Sent header in the article buffers."
1431   (interactive)
1432   (gnus-stop-date-timer)
1433   (setq article-lapsed-timer 
1434         (nnheader-run-at-time 1 1 'article-update-date-lapsed)))
1435
1436 (defun gnus-stop-date-timer ()
1437   "Stop the X-Sent timer."
1438   (interactive)
1439   (when article-lapsed-timer
1440     (nnheader-delete-timer article-lapsed-timer)
1441     (setq article-lapsed-timer nil)))
1442
1443 (defun article-date-user (&optional highlight)
1444   "Convert the current article date to the user-defined format.
1445 This format is defined by the `gnus-article-time-format' variable."
1446   (interactive (list t))
1447   (article-date-ut 'user highlight))
1448
1449 (defun article-show-all ()
1450   "Show all hidden text in the article buffer."
1451   (interactive)
1452   (save-excursion
1453     (let ((buffer-read-only nil))
1454       (gnus-article-unhide-text (point-min) (point-max)))))
1455
1456 (defun article-emphasize (&optional arg)
1457   "Emphasize text according to `gnus-emphasis-alist'."
1458   (interactive (gnus-article-hidden-arg))
1459   (unless (gnus-article-check-hidden-text 'emphasis arg)
1460     (save-excursion
1461       (let ((alist gnus-emphasis-alist)
1462             (buffer-read-only nil)
1463             (props (append '(article-type emphasis)
1464                            gnus-hidden-properties))
1465             regexp elem beg invisible visible face)
1466         (goto-char (point-min))
1467         (search-forward "\n\n" nil t)
1468         (setq beg (point))
1469         (while (setq elem (pop alist))
1470           (goto-char beg)
1471           (setq regexp (car elem)
1472                 invisible (nth 1 elem)
1473                 visible (nth 2 elem)
1474                 face (nth 3 elem))
1475           (while (re-search-forward regexp nil t)
1476             (when (and (match-beginning visible) (match-beginning invisible))
1477               (gnus-article-hide-text
1478                (match-beginning invisible) (match-end invisible) props)
1479               (gnus-article-unhide-text-type
1480                (match-beginning visible) (match-end visible) 'emphasis)
1481               (gnus-put-text-property-excluding-newlines
1482                (match-beginning visible) (match-end visible) 'face face)
1483               (goto-char (match-end invisible)))))))))
1484
1485 (defvar gnus-summary-article-menu)
1486 (defvar gnus-summary-post-menu)
1487
1488 ;;; Saving functions.
1489
1490 (defun gnus-article-save (save-buffer file &optional num)
1491   "Save the currently selected article."
1492   (unless gnus-save-all-headers
1493     ;; Remove headers according to `gnus-saved-headers'.
1494     (let ((gnus-visible-headers
1495            (or gnus-saved-headers gnus-visible-headers))
1496           (gnus-article-buffer save-buffer))
1497       (save-excursion
1498         (set-buffer save-buffer)
1499         (article-hide-headers 1 t))))
1500   (save-window-excursion
1501     (if (not gnus-default-article-saver)
1502         (error "No default saver is defined")
1503       ;; !!! Magic!  The saving functions all save
1504       ;; `gnus-original-article-buffer' (or so they think), but we
1505       ;; bind that variable to our save-buffer.
1506       (set-buffer gnus-article-buffer)
1507       (let* ((gnus-save-article-buffer save-buffer)
1508              (filename
1509               (cond
1510                ((not gnus-prompt-before-saving) 'default)
1511                ((eq gnus-prompt-before-saving 'always) nil)
1512                (t file)))
1513              (gnus-number-of-articles-to-be-saved
1514               (when (eq gnus-prompt-before-saving t)
1515                 num)))                  ; Magic
1516         (set-buffer gnus-summary-buffer)
1517         (funcall gnus-default-article-saver filename)))))
1518
1519 (defun gnus-read-save-file-name (prompt &optional filename
1520                                         function group headers variable)
1521   (let ((default-name
1522           (funcall function group headers (symbol-value variable)))
1523         result)
1524     (setq
1525      result
1526      (cond
1527       ((eq filename 'default)
1528        default-name)
1529       ((eq filename t)
1530        default-name)
1531       (filename filename)
1532       (t
1533        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1534               (prompt
1535                (format prompt
1536                        (if (and gnus-number-of-articles-to-be-saved
1537                                 (> gnus-number-of-articles-to-be-saved 1))
1538                            (format "these %d articles"
1539                                    gnus-number-of-articles-to-be-saved)
1540                          "this article")))
1541               (file
1542                ;; Let the split methods have their say.
1543                (cond
1544                 ;; No split name was found.
1545                 ((null split-name)
1546                  (read-file-name
1547                   (concat prompt " (default "
1548                           (file-name-nondirectory default-name) ") ")
1549                   (file-name-directory default-name)
1550                   default-name))
1551                 ;; A single group name is returned.
1552                 ((stringp split-name)
1553                  (setq default-name
1554                        (funcall function split-name headers
1555                                 (symbol-value variable)))
1556                  (read-file-name
1557                   (concat prompt " (default "
1558                           (file-name-nondirectory default-name) ") ")
1559                   (file-name-directory default-name)
1560                   default-name))
1561                 ;; A single split name was found
1562                 ((= 1 (length split-name))
1563                  (let* ((name (expand-file-name
1564                                (car split-name) gnus-article-save-directory))
1565                         (dir (cond ((file-directory-p name)
1566                                     (file-name-as-directory name))
1567                                    ((file-exists-p name) name)
1568                                    (t gnus-article-save-directory))))
1569                    (read-file-name
1570                     (concat prompt " (default " name ") ")
1571                     dir name)))
1572                 ;; A list of splits was found.
1573                 (t
1574                  (setq split-name (nreverse split-name))
1575                  (let (result)
1576                    (let ((file-name-history
1577                           (nconc split-name file-name-history)))
1578                      (setq result
1579                            (expand-file-name
1580                             (read-file-name
1581                              (concat prompt " (`M-p' for defaults) ")
1582                              gnus-article-save-directory
1583                              (car split-name))
1584                             gnus-article-save-directory)))
1585                    (car (push result file-name-history)))))))
1586          ;; Create the directory.
1587          (gnus-make-directory (file-name-directory file))
1588          ;; If we have read a directory, we append the default file name.
1589          (when (file-directory-p file)
1590            (setq file (concat (file-name-as-directory file)
1591                               (file-name-nondirectory default-name))))
1592          ;; Possibly translate some characters.
1593          (nnheader-translate-file-chars file)))))
1594     (gnus-make-directory (file-name-directory result))
1595     (set variable result)))
1596
1597 (defun gnus-article-archive-name (group)
1598   "Return the first instance of an \"Archive-name\" in the current buffer."
1599   (let ((case-fold-search t))
1600     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1601       (nnheader-concat gnus-article-save-directory
1602                        (match-string 1)))))
1603
1604 (defun gnus-article-nndoc-name (group)
1605   "If GROUP is an nndoc group, return the name of the parent group."
1606   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1607     (gnus-group-get-parameter group 'save-article-group)))
1608
1609 (defun gnus-summary-save-in-rmail (&optional filename)
1610   "Append this article to Rmail file.
1611 Optional argument FILENAME specifies file name.
1612 Directory to save to is default to `gnus-article-save-directory'."
1613   (interactive)
1614   (setq filename (gnus-read-save-file-name
1615                   "Save %s in rmail file:" filename
1616                   gnus-rmail-save-name gnus-newsgroup-name
1617                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1618   (gnus-eval-in-buffer-window gnus-save-article-buffer
1619     (save-excursion
1620       (save-restriction
1621         (widen)
1622         (gnus-output-to-rmail filename)))))
1623
1624 (defun gnus-summary-save-in-mail (&optional filename)
1625   "Append this article to Unix mail file.
1626 Optional argument FILENAME specifies file name.
1627 Directory to save to is default to `gnus-article-save-directory'."
1628   (interactive)
1629   (setq filename (gnus-read-save-file-name
1630                   "Save %s in Unix mail file:" filename
1631                   gnus-mail-save-name gnus-newsgroup-name
1632                   gnus-current-headers 'gnus-newsgroup-last-mail))
1633   (gnus-eval-in-buffer-window gnus-save-article-buffer
1634     (save-excursion
1635       (save-restriction
1636         (widen)
1637         (if (and (file-readable-p filename)
1638                  (mail-file-babyl-p filename))
1639             (gnus-output-to-rmail filename t)
1640           (gnus-output-to-mail filename))))))
1641
1642 (defun gnus-summary-save-in-file (&optional filename overwrite)
1643   "Append this article to file.
1644 Optional argument FILENAME specifies file name.
1645 Directory to save to is default to `gnus-article-save-directory'."
1646   (interactive)
1647   (setq filename (gnus-read-save-file-name
1648                   "Save %s in file:" filename
1649                   gnus-file-save-name gnus-newsgroup-name
1650                   gnus-current-headers 'gnus-newsgroup-last-file))
1651   (gnus-eval-in-buffer-window gnus-save-article-buffer
1652     (save-excursion
1653       (save-restriction
1654         (widen)
1655         (when (and overwrite
1656                    (file-exists-p filename))
1657           (delete-file filename))
1658         (gnus-output-to-file filename)))))
1659
1660 (defun gnus-summary-write-to-file (&optional filename)
1661   "Write this article to a file.
1662 Optional argument FILENAME specifies file name.
1663 The directory to save in defaults to `gnus-article-save-directory'."
1664   (interactive)
1665   (gnus-summary-save-in-file nil t))
1666
1667 (defun gnus-summary-save-body-in-file (&optional filename)
1668   "Append this article body to a file.
1669 Optional argument FILENAME specifies file name.
1670 The directory to save in defaults to `gnus-article-save-directory'."
1671   (interactive)
1672   (setq filename (gnus-read-save-file-name
1673                   "Save %s body in file:" filename
1674                   gnus-file-save-name gnus-newsgroup-name
1675                   gnus-current-headers 'gnus-newsgroup-last-file))
1676   (gnus-eval-in-buffer-window gnus-save-article-buffer
1677     (save-excursion
1678       (save-restriction
1679         (widen)
1680         (goto-char (point-min))
1681         (when (search-forward "\n\n" nil t)
1682           (narrow-to-region (point) (point-max)))
1683         (gnus-output-to-file filename)))))
1684
1685 (defun gnus-summary-save-in-pipe (&optional command)
1686   "Pipe this article to subprocess."
1687   (interactive)
1688   (setq command
1689         (cond ((eq command 'default)
1690                gnus-last-shell-command)
1691               (command command)
1692               (t (read-string
1693                   (format
1694                    "Shell command on %s: "
1695                    (if (and gnus-number-of-articles-to-be-saved
1696                             (> gnus-number-of-articles-to-be-saved 1))
1697                        (format "these %d articles"
1698                                gnus-number-of-articles-to-be-saved)
1699                      "this article"))
1700                   gnus-last-shell-command))))
1701   (when (string-equal command "")
1702     (setq command gnus-last-shell-command))
1703   (gnus-eval-in-buffer-window gnus-article-buffer
1704     (save-restriction
1705       (widen)
1706       (shell-command-on-region (point-min) (point-max) command nil)))
1707   (setq gnus-last-shell-command command))
1708
1709 ;;; Article file names when saving.
1710
1711 (defun gnus-capitalize-newsgroup (newsgroup)
1712   "Capitalize NEWSGROUP name."
1713   (when (not (zerop (length newsgroup)))
1714     (concat (char-to-string (upcase (aref newsgroup 0)))
1715             (substring newsgroup 1))))
1716
1717 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1718   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1719 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1720 Otherwise, it is like ~/News/news/group/num."
1721   (let ((default
1722           (expand-file-name
1723            (concat (if (gnus-use-long-file-name 'not-save)
1724                        (gnus-capitalize-newsgroup newsgroup)
1725                      (gnus-newsgroup-directory-form newsgroup))
1726                    "/" (int-to-string (mail-header-number headers)))
1727            gnus-article-save-directory)))
1728     (if (and last-file
1729              (string-equal (file-name-directory default)
1730                            (file-name-directory last-file))
1731              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1732         default
1733       (or last-file default))))
1734
1735 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1736   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1737 If variable `gnus-use-long-file-name' is non-nil, it is
1738 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1739   (let ((default
1740           (expand-file-name
1741            (concat (if (gnus-use-long-file-name 'not-save)
1742                        newsgroup
1743                      (gnus-newsgroup-directory-form newsgroup))
1744                    "/" (int-to-string (mail-header-number headers)))
1745            gnus-article-save-directory)))
1746     (if (and last-file
1747              (string-equal (file-name-directory default)
1748                            (file-name-directory last-file))
1749              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1750         default
1751       (or last-file default))))
1752
1753 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1754   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1755 If variable `gnus-use-long-file-name' is non-nil, it is
1756 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1757   (or last-file
1758       (expand-file-name
1759        (if (gnus-use-long-file-name 'not-save)
1760            (gnus-capitalize-newsgroup newsgroup)
1761          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1762        gnus-article-save-directory)))
1763
1764 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1765   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1766 If variable `gnus-use-long-file-name' is non-nil, it is
1767 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1768   (or last-file
1769       (expand-file-name
1770        (if (gnus-use-long-file-name 'not-save)
1771            newsgroup
1772          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1773        gnus-article-save-directory)))
1774
1775 (eval-and-compile
1776   (mapcar
1777    (lambda (func)
1778      (let (afunc gfunc)
1779        (if (consp func)
1780            (setq afunc (car func)
1781                  gfunc (cdr func))
1782          (setq afunc func
1783                gfunc (intern (format "gnus-%s" func))))
1784        (fset gfunc
1785              (if (not (fboundp afunc))
1786                  nil
1787                `(lambda (&optional interactive &rest args)
1788                   ,(documentation afunc t)
1789                   (interactive (list t))
1790                   (save-excursion
1791                     (set-buffer gnus-article-buffer)
1792                     (if interactive
1793                         (call-interactively ',afunc)
1794                       (apply ',afunc args))))))))
1795    '(article-hide-headers
1796      article-hide-boring-headers
1797      article-treat-overstrike
1798      (article-fill . gnus-article-word-wrap)
1799      article-remove-cr
1800      article-display-x-face
1801      article-de-quoted-unreadable
1802      article-mime-decode-quoted-printable
1803      article-hide-pgp
1804      article-hide-pem
1805      article-hide-signature
1806      article-remove-trailing-blank-lines
1807      article-strip-leading-blank-lines
1808      article-strip-multiple-blank-lines
1809      article-strip-leading-space
1810      article-strip-blank-lines
1811      article-date-local
1812      article-date-original
1813      article-date-ut
1814      article-date-user
1815      article-date-lapsed
1816      article-emphasize
1817      article-treat-dumbquotes
1818      (article-show-all . gnus-article-show-all-headers))))
1819 \f
1820 ;;;
1821 ;;; Gnus article mode
1822 ;;;
1823
1824 (put 'gnus-article-mode 'mode-class 'special)
1825
1826 (gnus-define-keys gnus-article-mode-map
1827   " " gnus-article-goto-next-page
1828   "\177" gnus-article-goto-prev-page
1829   [delete] gnus-article-goto-prev-page
1830   "\C-c^" gnus-article-refer-article
1831   "h" gnus-article-show-summary
1832   "s" gnus-article-show-summary
1833   "\C-c\C-m" gnus-article-mail
1834   "?" gnus-article-describe-briefly
1835   gnus-mouse-2 gnus-article-push-button
1836   "\r" gnus-article-press-button
1837   "\t" gnus-article-next-button
1838   "\M-\t" gnus-article-prev-button
1839   "e" gnus-article-edit
1840   "<" beginning-of-buffer
1841   ">" end-of-buffer
1842   "\C-c\C-i" gnus-info-find-node
1843   "\C-c\C-b" gnus-bug
1844
1845   "\C-d" gnus-article-read-summary-keys
1846   "\M-*" gnus-article-read-summary-keys
1847   "\M-#" gnus-article-read-summary-keys
1848   "\M-^" gnus-article-read-summary-keys
1849   "\M-g" gnus-article-read-summary-keys)
1850
1851 (substitute-key-definition
1852  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
1853
1854 (defun gnus-article-make-menu-bar ()
1855   (gnus-turn-off-edit-menu 'article)
1856   (unless (boundp 'gnus-article-article-menu)
1857     (easy-menu-define
1858      gnus-article-article-menu gnus-article-mode-map ""
1859      '("Article"
1860        ["Scroll forwards" gnus-article-goto-next-page t]
1861        ["Scroll backwards" gnus-article-goto-prev-page t]
1862        ["Show summary" gnus-article-show-summary t]
1863        ["Fetch Message-ID at point" gnus-article-refer-article t]
1864        ["Mail to address at point" gnus-article-mail t]))
1865
1866     (easy-menu-define
1867      gnus-article-treatment-menu gnus-article-mode-map ""
1868      '("Treatment"
1869        ["Hide headers" gnus-article-hide-headers t]
1870        ["Hide signature" gnus-article-hide-signature t]
1871        ["Hide citation" gnus-article-hide-citation t]
1872        ["Treat overstrike" gnus-article-treat-overstrike t]
1873        ["Remove carriage return" gnus-article-remove-cr t]
1874        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1875
1876     (when nil
1877       (when (boundp 'gnus-summary-article-menu)
1878         (define-key gnus-article-mode-map [menu-bar commands]
1879           (cons "Commands" gnus-summary-article-menu))))
1880
1881     (when (boundp 'gnus-summary-post-menu)
1882       (define-key gnus-article-mode-map [menu-bar post]
1883         (cons "Post" gnus-summary-post-menu)))
1884
1885     (run-hooks 'gnus-article-menu-hook)))
1886
1887 (defun gnus-article-mode ()
1888   "Major mode for displaying an article.
1889
1890 All normal editing commands are switched off.
1891
1892 The following commands are available in addition to all summary mode
1893 commands:
1894 \\<gnus-article-mode-map>
1895 \\[gnus-article-next-page]\t Scroll the article one page forwards
1896 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1897 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1898 \\[gnus-article-show-summary]\t Display the summary buffer
1899 \\[gnus-article-mail]\t Send a reply to the address near point
1900 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1901 \\[gnus-info-find-node]\t Go to the Gnus info node"
1902   (interactive)
1903   (when (gnus-visual-p 'article-menu 'menu)
1904     (gnus-article-make-menu-bar))
1905   (kill-all-local-variables)
1906   (gnus-simplify-mode-line)
1907   (setq mode-name "Article")
1908   (setq major-mode 'gnus-article-mode)
1909   (make-local-variable 'minor-mode-alist)
1910   (unless (assq 'gnus-show-mime minor-mode-alist)
1911     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1912   (use-local-map gnus-article-mode-map)
1913   (gnus-update-format-specifications nil 'article-mode)
1914   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1915   (set (make-local-variable 'gnus-page-broken) nil)
1916   (set (make-local-variable 'gnus-button-marker-list) nil)
1917   (gnus-set-default-directory)
1918   (buffer-disable-undo (current-buffer))
1919   (setq buffer-read-only t)
1920   (set-syntax-table gnus-article-mode-syntax-table)
1921   (run-hooks 'gnus-article-mode-hook))
1922
1923 (defun gnus-article-setup-buffer ()
1924   "Initialize the article buffer."
1925   (let* ((name (if gnus-single-article-buffer "*Article*"
1926                  (concat "*Article " gnus-newsgroup-name "*")))
1927          (original
1928           (progn (string-match "\\*Article" name)
1929                  (concat " *Original Article"
1930                          (substring name (match-end 0))))))
1931     (setq gnus-article-buffer name)
1932     (setq gnus-original-article-buffer original)
1933     ;; This might be a variable local to the summary buffer.
1934     (unless gnus-single-article-buffer
1935       (save-excursion
1936         (set-buffer gnus-summary-buffer)
1937         (setq gnus-article-buffer name)
1938         (setq gnus-original-article-buffer original)
1939         (gnus-set-global-variables)))
1940     ;; Init original article buffer.
1941     (save-excursion
1942       (set-buffer (get-buffer-create gnus-original-article-buffer))
1943       (buffer-disable-undo (current-buffer))
1944       (setq major-mode 'gnus-original-article-mode)
1945       (gnus-add-current-to-buffer-list)
1946       (make-local-variable 'gnus-original-article))
1947     (if (get-buffer name)
1948         (save-excursion
1949           (set-buffer name)
1950           (buffer-disable-undo (current-buffer))
1951           (setq buffer-read-only t)
1952           (gnus-add-current-to-buffer-list)
1953           (unless (eq major-mode 'gnus-article-mode)
1954             (gnus-article-mode))
1955           (current-buffer))
1956       (save-excursion
1957         (set-buffer (get-buffer-create name))
1958         (gnus-add-current-to-buffer-list)
1959         (gnus-article-mode)
1960         (make-local-variable 'gnus-summary-buffer)
1961         (current-buffer)))))
1962
1963 ;; Set article window start at LINE, where LINE is the number of lines
1964 ;; from the head of the article.
1965 (defun gnus-article-set-window-start (&optional line)
1966   (set-window-start
1967    (get-buffer-window gnus-article-buffer t)
1968    (save-excursion
1969      (set-buffer gnus-article-buffer)
1970      (goto-char (point-min))
1971      (if (not line)
1972          (point-min)
1973        (gnus-message 6 "Moved to bookmark")
1974        (search-forward "\n\n" nil t)
1975        (forward-line line)
1976        (point)))))
1977
1978 ;;; @@ article filters
1979 ;;;
1980 (defun gnus-article-preview-mime-message ()
1981   (make-local-variable 'mime-button-mother-dispatcher)
1982   (setq mime-button-mother-dispatcher
1983         (function gnus-article-push-button))
1984   (let ((default-mime-charset
1985           (save-excursion
1986             (set-buffer gnus-summary-buffer)
1987             default-mime-charset))
1988         )
1989     (save-excursion
1990       (mime-view-mode nil nil nil gnus-original-article-buffer
1991                       gnus-article-buffer
1992                       gnus-article-mode-map)
1993       ))
1994   (run-hooks 'gnus-mime-article-prepare-hook)
1995   )
1996
1997 (defun gnus-article-decode-encoded-word ()
1998   "Header filter for gnus-article-mode.
1999 It is registered to variable `mime-view-content-header-filter-alist'."
2000   (goto-char (point-min))
2001   (let ((charset (save-excursion
2002                    (set-buffer gnus-summary-buffer)
2003                    default-mime-charset)))
2004     (save-restriction
2005       (std11-narrow-to-header)
2006       (goto-char (point-min))
2007       (while (re-search-forward "^[^ \t:]+:" nil t)
2008         (let ((start (match-beginning 0))
2009               (end (std11-field-end))
2010               )
2011           (save-restriction
2012             (narrow-to-region start end)
2013             (decode-mime-charset-region start end charset)
2014             (goto-char (point-max))
2015             )))
2016       (eword-decode-header)
2017       )
2018     (decode-mime-charset-region (point) (point-max) charset)
2019     (mime-maybe-hide-echo-buffer)
2020     )
2021   (run-hooks 'gnus-mime-article-prepare-hook)
2022   )
2023
2024 (defun gnus-article-prepare (article &optional all-headers header)
2025   "Prepare ARTICLE in article mode buffer.
2026 ARTICLE should either be an article number or a Message-ID.
2027 If ARTICLE is an id, HEADER should be the article headers.
2028 If ALL-HEADERS is non-nil, no headers are hidden."
2029   (save-excursion
2030     ;; Make sure we start in a summary buffer.
2031     (unless (eq major-mode 'gnus-summary-mode)
2032       (set-buffer gnus-summary-buffer))
2033     (setq gnus-summary-buffer (current-buffer))
2034     ;; Make sure the connection to the server is alive.
2035     (unless (gnus-server-opened
2036              (gnus-find-method-for-group gnus-newsgroup-name))
2037       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
2038       (gnus-request-group gnus-newsgroup-name t))
2039     (let* ((gnus-article (if header (mail-header-number header) article))
2040            (summary-buffer (current-buffer))
2041            (internal-hook gnus-article-internal-prepare-hook)
2042            (group gnus-newsgroup-name)
2043            result)
2044       (save-excursion
2045         (gnus-article-setup-buffer)
2046         (set-buffer gnus-article-buffer)
2047         ;; Deactivate active regions.
2048         (when (and (boundp 'transient-mark-mode)
2049                    transient-mark-mode)
2050           (setq mark-active nil))
2051         (if (not (setq result (let ((buffer-read-only nil))
2052                                 (gnus-request-article-this-buffer
2053                                  article group))))
2054             ;; There is no such article.
2055             (save-excursion
2056               (when (and (numberp article)
2057                          (not (memq article gnus-newsgroup-sparse)))
2058                 (setq gnus-article-current
2059                       (cons gnus-newsgroup-name article))
2060                 (set-buffer gnus-summary-buffer)
2061                 (setq gnus-current-article article)
2062                 (gnus-summary-mark-article article gnus-canceled-mark))
2063               (unless (memq article gnus-newsgroup-sparse)
2064                 (gnus-error
2065                  1 "No such article (may have expired or been canceled)")))
2066           (if (or (eq result 'pseudo) (eq result 'nneething))
2067               (progn
2068                 (save-excursion
2069                   (set-buffer summary-buffer)
2070                   (push article gnus-newsgroup-history)
2071                   (setq gnus-last-article gnus-current-article
2072                         gnus-current-article 0
2073                         gnus-current-headers nil
2074                         gnus-article-current nil)
2075                   (if (eq result 'nneething)
2076                       (gnus-configure-windows 'summary)
2077                     (gnus-configure-windows 'article))
2078                   (gnus-set-global-variables))
2079                 (gnus-set-mode-line 'article))
2080             ;; The result from the `request' was an actual article -
2081             ;; or at least some text that is now displayed in the
2082             ;; article buffer.
2083             (when (and (numberp article)
2084                        (not (eq article gnus-current-article)))
2085               ;; Seems like a new article has been selected.
2086               ;; `gnus-current-article' must be an article number.
2087               (save-excursion
2088                 (set-buffer summary-buffer)
2089                 (push article gnus-newsgroup-history)
2090                 (setq gnus-last-article gnus-current-article
2091                       gnus-current-article article
2092                       gnus-current-headers
2093                       (gnus-summary-article-header gnus-current-article)
2094                       gnus-article-current
2095                       (cons gnus-newsgroup-name gnus-current-article))
2096                 (unless (vectorp gnus-current-headers)
2097                   (setq gnus-current-headers nil))
2098                 (gnus-summary-goto-subject gnus-current-article)
2099                 (gnus-summary-show-thread)
2100                 (run-hooks 'gnus-mark-article-hook)
2101                 (gnus-set-mode-line 'summary)
2102                 (when (gnus-visual-p 'article-highlight 'highlight)
2103                   (run-hooks 'gnus-visual-mark-article-hook))
2104                 ;; Set the global newsgroup variables here.
2105                 ;; Suggested by Jim Sisolak
2106                 ;; <sisolak@trans4.neep.wisc.edu>.
2107                 (gnus-set-global-variables)
2108                 (setq gnus-have-all-headers
2109                       (or all-headers gnus-show-all-headers))
2110                 (and gnus-use-cache
2111                      (vectorp (gnus-summary-article-header article))
2112                      (gnus-cache-possibly-enter-article
2113                       group article
2114                       (gnus-summary-article-header article)
2115                       (memq article gnus-newsgroup-marked)
2116                       (memq article gnus-newsgroup-dormant)
2117                       (memq article gnus-newsgroup-unreads)))))
2118             (when (or (numberp article)
2119                       (stringp article))
2120               ;; Hooks for getting information from the article.
2121               ;; This hook must be called before being narrowed.
2122               (let (buffer-read-only)
2123                 (run-hooks 'internal-hook)
2124                 (run-hooks 'gnus-article-prepare-hook)
2125                 ;; Decode MIME message.
2126                 (when gnus-show-mime
2127                   (if (or (not gnus-strict-mime)
2128                           (gnus-fetch-field "Mime-Version"))
2129                       (funcall gnus-show-mime-method)
2130                     (funcall gnus-decode-encoded-word-method)))
2131                 ;; Perform the article display hooks.
2132                 (run-hooks 'gnus-article-display-hook))
2133               ;; Do page break.
2134               (goto-char (point-min))
2135               (setq gnus-page-broken
2136                     (when gnus-break-pages
2137                       (gnus-narrow-to-page)
2138                       t)))
2139             (gnus-set-mode-line 'article)
2140             (gnus-configure-windows 'article)
2141             (goto-char (point-min))
2142             t))))))
2143
2144 (defun gnus-article-wash-status ()
2145   "Return a string which display status of article washing."
2146   (save-excursion
2147     (set-buffer gnus-article-buffer)
2148     (let ((cite (gnus-article-hidden-text-p 'cite))
2149           (headers (gnus-article-hidden-text-p 'headers))
2150           (boring (gnus-article-hidden-text-p 'boring-headers))
2151           (pgp (gnus-article-hidden-text-p 'pgp))
2152           (pem (gnus-article-hidden-text-p 'pem))
2153           (signature (gnus-article-hidden-text-p 'signature))
2154           (overstrike (gnus-article-hidden-text-p 'overstrike))
2155           (emphasis (gnus-article-hidden-text-p 'emphasis))
2156           (mime gnus-show-mime))
2157       (format "%c%c%c%c%c%c%c"
2158               (if cite ?c ? )
2159               (if (or headers boring) ?h ? )
2160               (if (or pgp pem) ?p ? )
2161               (if signature ?s ? )
2162               (if overstrike ?o ? )
2163               (if mime ?m ? )
2164               (if emphasis ?e ? )))))
2165
2166 (defun gnus-article-hide-headers-if-wanted ()
2167   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2168 Provided for backwards compatibility."
2169   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2170       gnus-inhibit-hiding
2171       (gnus-article-hide-headers)))
2172
2173 ;;; Article savers.
2174
2175 (defun gnus-output-to-file (file-name)
2176   "Append the current article to a file named FILE-NAME."
2177   (let ((artbuf (current-buffer)))
2178     (nnheader-temp-write nil
2179       (insert-buffer-substring artbuf)
2180       ;; Append newline at end of the buffer as separator, and then
2181       ;; save it to file.
2182       (goto-char (point-max))
2183       (insert "\n")
2184       (append-to-file (point-min) (point-max) file-name)
2185       t)))
2186
2187 (defun gnus-narrow-to-page (&optional arg)
2188   "Narrow the article buffer to a page.
2189 If given a numerical ARG, move forward ARG pages."
2190   (interactive "P")
2191   (setq arg (if arg (prefix-numeric-value arg) 0))
2192   (save-excursion
2193     (set-buffer gnus-article-buffer)
2194     (goto-char (point-min))
2195     (widen)
2196     ;; Remove any old next/prev buttons.
2197     (when (gnus-visual-p 'page-marker)
2198       (let ((buffer-read-only nil))
2199         (gnus-remove-text-with-property 'gnus-prev)
2200         (gnus-remove-text-with-property 'gnus-next)))
2201     (when
2202         (cond ((< arg 0)
2203                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2204               ((> arg 0)
2205                (re-search-forward page-delimiter nil 'move arg)))
2206       (goto-char (match-end 0)))
2207     (narrow-to-region
2208      (point)
2209      (if (re-search-forward page-delimiter nil 'move)
2210          (match-beginning 0)
2211        (point)))
2212     (when (and (gnus-visual-p 'page-marker)
2213                (not (= (point-min) 1)))
2214       (save-excursion
2215         (goto-char (point-min))
2216         (gnus-insert-prev-page-button)))
2217     (when (and (gnus-visual-p 'page-marker)
2218                (< (+ (point-max) 2) (buffer-size)))
2219       (save-excursion
2220         (goto-char (point-max))
2221         (gnus-insert-next-page-button)))))
2222
2223 ;; Article mode commands
2224
2225 (defun gnus-article-goto-next-page ()
2226   "Show the next page of the article."
2227   (interactive)
2228   (when (gnus-article-next-page)
2229     (goto-char (point-min))
2230     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2231
2232 (defun gnus-article-goto-prev-page ()
2233   "Show the next page of the article."
2234   (interactive)
2235   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2236     (gnus-article-prev-page nil)))
2237
2238 (defun gnus-article-next-page (&optional lines)
2239   "Show the next page of the current article.
2240 If end of article, return non-nil.  Otherwise return nil.
2241 Argument LINES specifies lines to be scrolled up."
2242   (interactive "p")
2243   (move-to-window-line -1)
2244   (if (save-excursion
2245         (end-of-line)
2246         (and (pos-visible-in-window-p)  ;Not continuation line.
2247              (eobp)))
2248       ;; Nothing in this page.
2249       (if (or (not gnus-page-broken)
2250               (save-excursion
2251                 (save-restriction
2252                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2253           t                             ;Nothing more.
2254         (gnus-narrow-to-page 1)         ;Go to next page.
2255         nil)
2256     ;; More in this page.
2257     (let ((scroll-in-place nil))
2258       (condition-case ()
2259           (scroll-up lines)
2260         (end-of-buffer
2261          ;; Long lines may cause an end-of-buffer error.
2262          (goto-char (point-max)))))
2263     (move-to-window-line 0)
2264     nil))
2265
2266 (defun gnus-article-prev-page (&optional lines)
2267   "Show previous page of current article.
2268 Argument LINES specifies lines to be scrolled down."
2269   (interactive "p")
2270   (move-to-window-line 0)
2271   (if (and gnus-page-broken
2272            (bobp)
2273            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2274       (progn
2275         (gnus-narrow-to-page -1)        ;Go to previous page.
2276         (goto-char (point-max))
2277         (recenter -1))
2278     (let ((scroll-in-place nil))
2279       (prog1
2280           (condition-case ()
2281               (scroll-down lines)
2282             (beginning-of-buffer
2283              (goto-char (point-min))))
2284         (move-to-window-line 0)))))
2285
2286 (defun gnus-article-refer-article ()
2287   "Read article specified by message-id around point."
2288   (interactive)
2289   (let ((point (point)))
2290     (search-forward ">" nil t)          ;Move point to end of "<....>".
2291     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2292         (let ((message-id (match-string 1)))
2293           (goto-char point)
2294           (set-buffer gnus-summary-buffer)
2295           (gnus-summary-refer-article message-id))
2296       (goto-char (point))
2297       (error "No references around point"))))
2298
2299 (defun gnus-article-show-summary ()
2300   "Reconfigure windows to show summary buffer."
2301   (interactive)
2302   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2303       (error "There is no summary buffer for this article buffer")
2304     (gnus-article-set-globals)
2305     (gnus-configure-windows 'article)
2306     (gnus-summary-goto-subject gnus-current-article)))
2307
2308 (defun gnus-article-describe-briefly ()
2309   "Describe article mode commands briefly."
2310   (interactive)
2311   (gnus-message 6
2312                 (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")))
2313
2314 (defun gnus-article-summary-command ()
2315   "Execute the last keystroke in the summary buffer."
2316   (interactive)
2317   (let ((obuf (current-buffer))
2318         (owin (current-window-configuration))
2319         func)
2320     (switch-to-buffer gnus-summary-buffer 'norecord)
2321     (setq func (lookup-key (current-local-map) (this-command-keys)))
2322     (call-interactively func)
2323     (set-buffer obuf)
2324     (set-window-configuration owin)
2325     (set-window-point (get-buffer-window (current-buffer)) (point))))
2326
2327 (defun gnus-article-summary-command-nosave ()
2328   "Execute the last keystroke in the summary buffer."
2329   (interactive)
2330   (let (func)
2331     (pop-to-buffer gnus-summary-buffer 'norecord)
2332     (setq func (lookup-key (current-local-map) (this-command-keys)))
2333     (call-interactively func)))
2334
2335 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2336   "Read a summary buffer key sequence and execute it from the article buffer."
2337   (interactive "P")
2338   (let ((nosaves
2339          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2340            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2341            "=" "^" "\M-^" "|"))
2342         (nosave-but-article
2343          '("A\r"))
2344         (nosave-in-article
2345          '("\C-d"))
2346         keys)
2347     (save-excursion
2348       (set-buffer gnus-summary-buffer)
2349       (let (gnus-pick-mode)
2350         (push (or key last-command-event) unread-command-events)
2351         (setq keys (read-key-sequence nil))))
2352     (message "")
2353
2354     (if (or (member keys nosaves)
2355             (member keys nosave-but-article)
2356             (member keys nosave-in-article))
2357         (let (func)
2358           (save-window-excursion
2359             (pop-to-buffer gnus-summary-buffer 'norecord)
2360             ;; We disable the pick minor mode commands.
2361             (let (gnus-pick-mode)
2362               (setq func (lookup-key (current-local-map) keys))))
2363           (if (not func)
2364               (ding)
2365             (unless (member keys nosave-in-article)
2366               (set-buffer gnus-summary-buffer))
2367             (call-interactively func))
2368           (when (member keys nosave-but-article)
2369             (pop-to-buffer gnus-article-buffer 'norecord)))
2370       ;; These commands should restore window configuration.
2371       (let ((obuf (current-buffer))
2372             (owin (current-window-configuration))
2373             (opoint (point))
2374             func in-buffer)
2375         (if not-restore-window
2376             (pop-to-buffer gnus-summary-buffer 'norecord)
2377           (switch-to-buffer gnus-summary-buffer 'norecord))
2378         (setq in-buffer (current-buffer))
2379         ;; We disable the pick minor mode commands.
2380         (if (setq func (let (gnus-pick-mode)
2381                          (lookup-key (current-local-map) keys)))
2382             (call-interactively func)
2383           (ding))
2384         (when (eq in-buffer (current-buffer))
2385           (set-buffer obuf)
2386           (unless not-restore-window
2387             (set-window-configuration owin))
2388           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
2389
2390 (defun gnus-article-hide (&optional arg force)
2391   "Hide all the gruft in the current article.
2392 This means that PGP stuff, signatures, cited text and (some)
2393 headers will be hidden.
2394 If given a prefix, show the hidden text instead."
2395   (interactive (list current-prefix-arg 'force))
2396   (gnus-article-hide-headers arg)
2397   (gnus-article-hide-pgp arg)
2398   (gnus-article-hide-citation-maybe arg force)
2399   (gnus-article-hide-signature arg))
2400
2401 (defun gnus-article-maybe-highlight ()
2402   "Do some article highlighting if `article-visual' is non-nil."
2403   (when (gnus-visual-p 'article-highlight 'highlight)
2404     (gnus-article-highlight-some)))
2405
2406 (defun gnus-request-article-this-buffer (article group)
2407   "Get an article and insert it into this buffer."
2408   (let (do-update-line)
2409     (prog1
2410         (save-excursion
2411           (erase-buffer)
2412           (gnus-kill-all-overlays)
2413           (setq group (or group gnus-newsgroup-name))
2414
2415           ;; Open server if it has closed.
2416           (gnus-check-server (gnus-find-method-for-group group))
2417
2418           ;; Using `gnus-request-article' directly will insert the article into
2419           ;; `nntp-server-buffer' - so we'll save some time by not having to
2420           ;; copy it from the server buffer into the article buffer.
2421
2422           ;; We only request an article by message-id when we do not have the
2423           ;; headers for it, so we'll have to get those.
2424           (when (stringp article)
2425             (let ((gnus-override-method gnus-refer-article-method))
2426               (gnus-read-header article)))
2427
2428           ;; If the article number is negative, that means that this article
2429           ;; doesn't belong in this newsgroup (possibly), so we find its
2430           ;; message-id and request it by id instead of number.
2431           (when (and (numberp article)
2432                      gnus-summary-buffer
2433                      (get-buffer gnus-summary-buffer)
2434                      (buffer-name (get-buffer gnus-summary-buffer)))
2435             (save-excursion
2436               (set-buffer gnus-summary-buffer)
2437               (let ((header (gnus-summary-article-header article)))
2438                 (when (< article 0)
2439                   (cond
2440                    ((memq article gnus-newsgroup-sparse)
2441                     ;; This is a sparse gap article.
2442                     (setq do-update-line article)
2443                     (setq article (mail-header-id header))
2444                     (let ((gnus-override-method gnus-refer-article-method))
2445                       (gnus-read-header article))
2446                     (setq gnus-newsgroup-sparse
2447                           (delq article gnus-newsgroup-sparse)))
2448                    ((vectorp header)
2449                     ;; It's a real article.
2450                     (setq article (mail-header-id header)))
2451                    (t
2452                     ;; It is an extracted pseudo-article.
2453                     (setq article 'pseudo)
2454                     (gnus-request-pseudo-article header))))
2455
2456                 (let ((method (gnus-find-method-for-group
2457                                gnus-newsgroup-name)))
2458                   (if (not (eq (car method) 'nneething))
2459                       ()
2460                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2461                                        (mail-header-subject header))))
2462                       (when (file-directory-p dir)
2463                         (setq article 'nneething)
2464                         (gnus-group-enter-directory dir))))))))
2465
2466           (cond
2467            ;; Refuse to select canceled articles.
2468            ((and (numberp article)
2469                  gnus-summary-buffer
2470                  (get-buffer gnus-summary-buffer)
2471                  (buffer-name (get-buffer gnus-summary-buffer))
2472                  (eq (cdr (save-excursion
2473                             (set-buffer gnus-summary-buffer)
2474                             (assq article gnus-newsgroup-reads)))
2475                      gnus-canceled-mark))
2476             nil)
2477            ;; We first check `gnus-original-article-buffer'.
2478            ((and (get-buffer gnus-original-article-buffer)
2479                  (numberp article)
2480                  (save-excursion
2481                    (set-buffer gnus-original-article-buffer)
2482                    (and (equal (car gnus-original-article) group)
2483                         (eq (cdr gnus-original-article) article))))
2484             (insert-buffer-substring gnus-original-article-buffer)
2485             'article)
2486            ;; Check the backlog.
2487            ((and gnus-keep-backlog
2488                  (gnus-backlog-request-article group article (current-buffer)))
2489             'article)
2490            ;; Check asynchronous pre-fetch.
2491            ((gnus-async-request-fetched-article group article (current-buffer))
2492             (gnus-async-prefetch-next group article gnus-summary-buffer)
2493             'article)
2494            ;; Check the cache.
2495            ((and gnus-use-cache
2496                  (numberp article)
2497                  (gnus-cache-request-article article group))
2498             'article)
2499            ;; Get the article and put into the article buffer.
2500            ((or (stringp article) (numberp article))
2501             (let ((gnus-override-method
2502                    (and (stringp article) gnus-refer-article-method))
2503                   (buffer-read-only nil))
2504               (erase-buffer)
2505               (gnus-kill-all-overlays)
2506               (when (gnus-request-article article group (current-buffer))
2507                 (when (numberp article)
2508                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2509                   (when gnus-keep-backlog
2510                     (gnus-backlog-enter-article
2511                      group article (current-buffer))))
2512                 'article)))
2513            ;; It was a pseudo.
2514            (t article)))
2515
2516       ;; Take the article from the original article buffer
2517       ;; and place it in the buffer it's supposed to be in.
2518       (when (and (get-buffer gnus-article-buffer)
2519                  ;;(numberp article)
2520                  (equal (buffer-name (current-buffer))
2521                         (buffer-name (get-buffer gnus-article-buffer))))
2522         (save-excursion
2523           (if (get-buffer gnus-original-article-buffer)
2524               (set-buffer (get-buffer gnus-original-article-buffer))
2525             (set-buffer (get-buffer-create gnus-original-article-buffer))
2526             (buffer-disable-undo (current-buffer))
2527             (setq major-mode 'gnus-original-article-mode)
2528             (setq buffer-read-only t)
2529             (gnus-add-current-to-buffer-list))
2530           (let (buffer-read-only)
2531             (erase-buffer)
2532             (insert-buffer-substring gnus-article-buffer))
2533           (setq gnus-original-article (cons group article))))
2534
2535       ;; Update sparse articles.
2536       (when (and do-update-line
2537                  (or (numberp article)
2538                      (stringp article)))
2539         (let ((buf (current-buffer)))
2540           (set-buffer gnus-summary-buffer)
2541           (gnus-summary-update-article do-update-line)
2542           (gnus-summary-goto-subject do-update-line nil t)
2543           (set-window-point (get-buffer-window (current-buffer) t)
2544                             (point))
2545           (set-buffer buf))))))
2546
2547 ;;;
2548 ;;; Article editing
2549 ;;;
2550
2551 (defcustom gnus-article-edit-mode-hook nil
2552   "Hook run in article edit mode buffers."
2553   :group 'gnus-article-various
2554   :type 'hook)
2555
2556 (defvar gnus-article-edit-done-function nil)
2557
2558 (defvar gnus-article-edit-mode-map nil)
2559
2560 (unless gnus-article-edit-mode-map
2561   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2562
2563   (gnus-define-keys gnus-article-edit-mode-map
2564     "\C-c\C-c" gnus-article-edit-done
2565     "\C-c\C-k" gnus-article-edit-exit)
2566
2567   (gnus-define-keys (gnus-article-edit-wash-map
2568                      "\C-c\C-w" gnus-article-edit-mode-map)
2569     "f" gnus-article-edit-full-stops))
2570
2571 (defun gnus-article-edit-mode ()
2572   "Major mode for editing articles.
2573 This is an extended text-mode.
2574
2575 \\{gnus-article-edit-mode-map}"
2576   (interactive)
2577   (kill-all-local-variables)
2578   (setq major-mode 'gnus-article-edit-mode)
2579   (setq mode-name "Article Edit")
2580   (use-local-map gnus-article-edit-mode-map)
2581   (make-local-variable 'gnus-article-edit-done-function)
2582   (make-local-variable 'gnus-prev-winconf)
2583   (setq buffer-read-only nil)
2584   (buffer-enable-undo)
2585   (widen)
2586   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
2587
2588 (defun gnus-article-edit (&optional force)
2589   "Edit the current article.
2590 This will have permanent effect only in mail groups.
2591 If FORCE is non-nil, allow editing of articles even in read-only
2592 groups."
2593   (interactive "P")
2594   (when (and (not force)
2595              (gnus-group-read-only-p))
2596     (error "The current newsgroup does not support article editing"))
2597   (gnus-article-edit-article
2598    `(lambda (no-highlight)
2599       (gnus-summary-edit-article-done
2600        ,(or (mail-header-references gnus-current-headers) "")
2601        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2602
2603 (defun gnus-article-edit-article (exit-func)
2604   "Start editing the contents of the current article buffer."
2605   (let ((winconf (current-window-configuration)))
2606     (set-buffer gnus-article-buffer)
2607     (gnus-article-edit-mode)
2608     (gnus-set-text-properties (point-min) (point-max) nil)
2609     (gnus-configure-windows 'edit-article)
2610     (setq gnus-article-edit-done-function exit-func)
2611     (setq gnus-prev-winconf winconf)
2612     (gnus-message 6 "C-c C-c to end edits")))
2613
2614 (defun gnus-article-edit-done (&optional arg)
2615   "Update the article edits and exit."
2616   (interactive "P")
2617   (let ((func gnus-article-edit-done-function)
2618         (buf (current-buffer))
2619         (start (window-start)))
2620     (gnus-article-edit-exit)
2621     (save-excursion
2622       (set-buffer buf)
2623       (let ((buffer-read-only nil))
2624         (funcall func arg)))
2625     (set-buffer buf)
2626     (set-window-start (get-buffer-window buf) start)
2627     (set-window-point (get-buffer-window buf) (point))))
2628
2629 (defun gnus-article-edit-exit ()
2630   "Exit the article editing without updating."
2631   (interactive)
2632   ;; We remove all text props from the article buffer.
2633   (let ((buf (format "%s" (buffer-string)))
2634         (curbuf (current-buffer))
2635         (p (point))
2636         (window-start (window-start)))
2637     (erase-buffer)
2638     (insert buf)
2639     (let ((winconf gnus-prev-winconf))
2640       (gnus-article-mode)
2641       ;; The cache and backlog have to be flushed somewhat.
2642       (when gnus-use-cache
2643         (gnus-cache-update-article
2644          (car gnus-article-current) (cdr gnus-article-current)))
2645       (when gnus-keep-backlog
2646         (gnus-backlog-remove-article
2647          (car gnus-article-current) (cdr gnus-article-current)))
2648       ;; Flush original article as well.
2649       (save-excursion
2650         (when (get-buffer gnus-original-article-buffer)
2651           (set-buffer gnus-original-article-buffer)
2652           (setq gnus-original-article nil)))
2653       (set-window-configuration winconf)
2654       ;; Tippy-toe some to make sure that point remains where it was.
2655       (let ((buf (current-buffer)))
2656         (set-buffer curbuf)
2657         (set-window-start (get-buffer-window (current-buffer)) window-start)
2658         (goto-char p)
2659         (set-buffer buf)))))
2660
2661 (defun gnus-article-edit-full-stops ()
2662   "Interactively repair spacing at end of sentences."
2663   (interactive)
2664   (save-excursion
2665     (goto-char (point-min))
2666     (search-forward-regexp "^$" nil t)
2667     (let ((case-fold-search nil))
2668       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2669
2670 ;;;
2671 ;;; Article highlights
2672 ;;;
2673
2674 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2675
2676 ;;; Internal Variables:
2677
2678 (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\\)"
2679   "Regular expression that matches URLs."
2680   :group 'gnus-article-buttons
2681   :type 'regexp)
2682
2683 (defcustom gnus-button-alist
2684   `(("<\\(url: ?\\)?news:\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2685      gnus-button-message-id 2)
2686     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2687     ("\\(\\b<\\(url: ?\\)?news:\\(//\\)?\\([^>\n\t ]*\\)>\\)" 1 t
2688      gnus-button-fetch-group 4)
2689     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2690     ("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2691      t gnus-button-message-id 3)
2692     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2693     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2694     ;; This is how URLs _should_ be embedded in text...
2695     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2696     ;; Raw URLs.
2697     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2698   "Alist of regexps matching buttons in article bodies.
2699
2700 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2701 REGEXP: is the string matching text around the button,
2702 BUTTON: is the number of the regexp grouping actually matching the button,
2703 FORM: is a lisp expression which must eval to true for the button to
2704 be added,
2705 CALLBACK: is the function to call when the user push this button, and each
2706 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2707
2708 CALLBACK can also be a variable, in that case the value of that
2709 variable it the real callback function."
2710   :group 'gnus-article-buttons
2711   :type '(repeat (list regexp
2712                        (integer :tag "Button")
2713                        (sexp :tag "Form")
2714                        (function :tag "Callback")
2715                        (repeat :tag "Par"
2716                                :inline t
2717                                (integer :tag "Regexp group")))))
2718
2719 (defcustom gnus-header-button-alist
2720   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2721      0 t gnus-button-message-id 0)
2722     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2723     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2724      0 t gnus-button-mailto 0)
2725     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2726     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2727     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2728     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2729      gnus-button-message-id 3))
2730   "Alist of headers and regexps to match buttons in article heads.
2731
2732 This alist is very similar to `gnus-button-alist', except that each
2733 alist has an additional HEADER element first in each entry:
2734
2735 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2736
2737 HEADER is a regexp to match a header.  For a fuller explanation, see
2738 `gnus-button-alist'."
2739   :group 'gnus-article-buttons
2740   :group 'gnus-article-headers
2741   :type '(repeat (list (regexp :tag "Header")
2742                        regexp
2743                        (integer :tag "Button")
2744                        (sexp :tag "Form")
2745                        (function :tag "Callback")
2746                        (repeat :tag "Par"
2747                                :inline t
2748                                (integer :tag "Regexp group")))))
2749
2750 (defvar gnus-button-regexp nil)
2751 (defvar gnus-button-marker-list nil)
2752 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2753
2754 (defvar gnus-button-last nil)
2755 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2756
2757 ;;; Commands:
2758
2759 (defun gnus-article-push-button (event)
2760   "Check text under the mouse pointer for a callback function.
2761 If the text under the mouse pointer has a `gnus-callback' property,
2762 call it with the value of the `gnus-data' text property."
2763   (interactive "e")
2764   (set-buffer (window-buffer (posn-window (event-start event))))
2765   (let* ((pos (posn-point (event-start event)))
2766          (data (get-text-property pos 'gnus-data))
2767          (fun (get-text-property pos 'gnus-callback)))
2768     (when fun
2769       (funcall fun data))))
2770
2771 (defun gnus-article-press-button ()
2772   "Check text at point for a callback function.
2773 If the text at point has a `gnus-callback' property,
2774 call it with the value of the `gnus-data' text property."
2775   (interactive)
2776   (let* ((data (get-text-property (point) 'gnus-data))
2777          (fun (get-text-property (point) 'gnus-callback)))
2778     (when fun
2779       (funcall fun data))))
2780
2781 (defun gnus-article-prev-button (n)
2782   "Move point to N buttons backward.
2783 If N is negative, move forward instead."
2784   (interactive "p")
2785   (gnus-article-next-button (- n)))
2786
2787 (defun gnus-article-next-button (n)
2788   "Move point to N buttons forward.
2789 If N is negative, move backward instead."
2790   (interactive "p")
2791   (let ((function (if (< n 0) 'previous-single-property-change
2792                     'next-single-property-change))
2793         (inhibit-point-motion-hooks t)
2794         (backward (< n 0))
2795         (limit (if (< n 0) (point-min) (point-max))))
2796     (setq n (abs n))
2797     (while (and (not (= limit (point)))
2798                 (> n 0))
2799       ;; Skip past the current button.
2800       (when (get-text-property (point) 'gnus-callback)
2801         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2802       ;; Go to the next (or previous) button.
2803       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2804       ;; Put point at the start of the button.
2805       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2806         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2807       ;; Skip past intangible buttons.
2808       (when (get-text-property (point) 'intangible)
2809         (incf n))
2810       (decf n))
2811     (unless (zerop n)
2812       (gnus-message 5 "No more buttons"))
2813     n))
2814
2815 (defun gnus-article-highlight (&optional force)
2816   "Highlight current article.
2817 This function calls `gnus-article-highlight-headers',
2818 `gnus-article-highlight-citation',
2819 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2820 do the highlighting.  See the documentation for those functions."
2821   (interactive (list 'force))
2822   (gnus-article-highlight-headers)
2823   (gnus-article-highlight-citation force)
2824   (gnus-article-highlight-signature)
2825   (gnus-article-add-buttons force)
2826   (gnus-article-add-buttons-to-head))
2827
2828 (defun gnus-article-highlight-some (&optional force)
2829   "Highlight current article.
2830 This function calls `gnus-article-highlight-headers',
2831 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2832 do the highlighting.  See the documentation for those functions."
2833   (interactive (list 'force))
2834   (gnus-article-highlight-headers)
2835   (gnus-article-highlight-signature)
2836   (gnus-article-add-buttons))
2837
2838 (defun gnus-article-highlight-headers ()
2839   "Highlight article headers as specified by `gnus-header-face-alist'."
2840   (interactive)
2841   (save-excursion
2842     (set-buffer gnus-article-buffer)
2843     (save-restriction
2844       (let ((alist gnus-header-face-alist)
2845             (buffer-read-only nil)
2846             (case-fold-search t)
2847             (inhibit-point-motion-hooks t)
2848             entry regexp header-face field-face from hpoints fpoints)
2849         (message-narrow-to-head)
2850         (while (setq entry (pop alist))
2851           (goto-char (point-min))
2852           (setq regexp (concat "^\\("
2853                                (if (string-equal "" (nth 0 entry))
2854                                    "[^\t ]"
2855                                  (nth 0 entry))
2856                                "\\)")
2857                 header-face (nth 1 entry)
2858                 field-face (nth 2 entry))
2859           (while (and (re-search-forward regexp nil t)
2860                       (not (eobp)))
2861             (beginning-of-line)
2862             (setq from (point))
2863             (unless (search-forward ":" nil t)
2864               (forward-char 1))
2865             (when (and header-face
2866                        (not (memq (point) hpoints)))
2867               (push (point) hpoints)
2868               (gnus-put-text-property from (point) 'face header-face))
2869             (when (and field-face
2870                        (not (memq (setq from (point)) fpoints)))
2871               (push from fpoints)
2872               (if (re-search-forward "^[^ \t]" nil t)
2873                   (forward-char -2)
2874                 (goto-char (point-max)))
2875               (gnus-put-text-property from (point) 'face field-face))))))))
2876
2877 (defun gnus-article-highlight-signature ()
2878   "Highlight the signature in an article.
2879 It does this by highlighting everything after
2880 `gnus-signature-separator' using `gnus-signature-face'."
2881   (interactive)
2882   (save-excursion
2883     (set-buffer gnus-article-buffer)
2884     (let ((buffer-read-only nil)
2885           (inhibit-point-motion-hooks t))
2886       (save-restriction
2887         (when (and gnus-signature-face
2888                    (gnus-article-narrow-to-signature))
2889           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2890                             'face gnus-signature-face)
2891           (widen)
2892           (gnus-article-search-signature)
2893           (let ((start (match-beginning 0))
2894                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2895             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2896                                      end)))))))
2897
2898 (defun gnus-button-in-region-p (b e prop)
2899   "Say whether PROP exists in the region."
2900   (text-property-not-all b e prop nil))
2901
2902 (defun gnus-article-add-buttons (&optional force)
2903   "Find external references in the article and make buttons of them.
2904 \"External references\" are things like Message-IDs and URLs, as
2905 specified by `gnus-button-alist'."
2906   (interactive (list 'force))
2907   (save-excursion
2908     (set-buffer gnus-article-buffer)
2909     (let ((buffer-read-only nil)
2910           (inhibit-point-motion-hooks t)
2911           (case-fold-search t)
2912           (alist gnus-button-alist)
2913           beg entry regexp)
2914       ;; Remove all old markers.
2915       (let (marker entry)
2916         (while (setq marker (pop gnus-button-marker-list))
2917           (goto-char marker)
2918           (when (setq entry (gnus-button-entry))
2919             (put-text-property (match-beginning (nth 1 entry))
2920                                (match-end (nth 1 entry))
2921                                'gnus-callback nil))
2922           (set-marker marker nil)))
2923       ;; We skip the headers.
2924       (goto-char (point-min))
2925       (unless (search-forward "\n\n" nil t)
2926         (goto-char (point-max)))
2927       (setq beg (point))
2928       (while (setq entry (pop alist))
2929         (setq regexp (car entry))
2930         (goto-char beg)
2931         (while (re-search-forward regexp nil t)
2932           (let* ((start (and entry (match-beginning (nth 1 entry))))
2933                  (end (and entry (match-end (nth 1 entry))))
2934                  (from (match-beginning 0)))
2935             (when (and (or (eq t (nth 2 entry))
2936                            (eval (nth 2 entry)))
2937                        (not (gnus-button-in-region-p
2938                              start end 'gnus-callback)))
2939               ;; That optional form returned non-nil, so we add the
2940               ;; button.
2941               (gnus-article-add-button
2942                start end 'gnus-button-push
2943                (car (push (set-marker (make-marker) from)
2944                           gnus-button-marker-list))))))))))
2945
2946 ;; Add buttons to the head of an article.
2947 (defun gnus-article-add-buttons-to-head ()
2948   "Add buttons to the head of the article."
2949   (interactive)
2950   (save-excursion
2951     (set-buffer gnus-article-buffer)
2952     (let ((buffer-read-only nil)
2953           (inhibit-point-motion-hooks t)
2954           (case-fold-search t)
2955           (alist gnus-header-button-alist)
2956           entry beg end)
2957       (nnheader-narrow-to-headers)
2958       (while alist
2959         ;; Each alist entry.
2960         (setq entry (car alist)
2961               alist (cdr alist))
2962         (goto-char (point-min))
2963         (while (re-search-forward (car entry) nil t)
2964           ;; Each header matching the entry.
2965           (setq beg (match-beginning 0))
2966           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2967                              (match-beginning 0))
2968                         (point-max)))
2969           (goto-char beg)
2970           (while (re-search-forward (nth 1 entry) end t)
2971             ;; Each match within a header.
2972             (let* ((entry (cdr entry))
2973                    (start (match-beginning (nth 1 entry)))
2974                    (end (match-end (nth 1 entry)))
2975                    (form (nth 2 entry)))
2976               (goto-char (match-end 0))
2977               (when (eval form)
2978                 (gnus-article-add-button
2979                  start end (nth 3 entry)
2980                  (buffer-substring (match-beginning (nth 4 entry))
2981                                    (match-end (nth 4 entry)))))))
2982           (goto-char end))))
2983     (widen)))
2984
2985 ;;; External functions:
2986
2987 (defun gnus-article-add-button (from to fun &optional data)
2988   "Create a button between FROM and TO with callback FUN and data DATA."
2989   (when gnus-article-button-face
2990     (gnus-overlay-put (gnus-make-overlay from to)
2991                       'face gnus-article-button-face))
2992   (gnus-add-text-properties
2993    from to
2994    (nconc (and gnus-article-mouse-face
2995                (list gnus-mouse-face-prop gnus-article-mouse-face))
2996           (list 'gnus-callback fun)
2997           (and data (list 'gnus-data data)))))
2998
2999 ;;; Internal functions:
3000
3001 (defun gnus-article-set-globals ()
3002   (save-excursion
3003     (set-buffer gnus-summary-buffer)
3004     (gnus-set-global-variables)))
3005
3006 (defun gnus-signature-toggle (end)
3007   (save-excursion
3008     (set-buffer gnus-article-buffer)
3009     (let ((buffer-read-only nil)
3010           (inhibit-point-motion-hooks t))
3011       (if (get-text-property end 'invisible)
3012           (gnus-article-unhide-text end (point-max))
3013         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3014
3015 (defun gnus-button-entry ()
3016   ;; Return the first entry in `gnus-button-alist' matching this place.
3017   (let ((alist gnus-button-alist)
3018         (entry nil))
3019     (while alist
3020       (setq entry (pop alist))
3021       (if (looking-at (car entry))
3022           (setq alist nil)
3023         (setq entry nil)))
3024     entry))
3025
3026 (defun gnus-button-push (marker)
3027   ;; Push button starting at MARKER.
3028   (save-excursion
3029     (set-buffer gnus-article-buffer)
3030     (goto-char marker)
3031     (let* ((entry (gnus-button-entry))
3032            (inhibit-point-motion-hooks t)
3033            (fun (nth 3 entry))
3034            (args (mapcar (lambda (group)
3035                            (let ((string (match-string group)))
3036                              (gnus-set-text-properties
3037                               0 (length string) nil string)
3038                              string))
3039                          (nthcdr 4 entry))))
3040       (cond
3041        ((fboundp fun)
3042         (apply fun args))
3043        ((and (boundp fun)
3044              (fboundp (symbol-value fun)))
3045         (apply (symbol-value fun) args))
3046        (t
3047         (gnus-message 1 "You must define `%S' to use this button"
3048                       (cons fun args)))))))
3049
3050 (defun gnus-button-message-id (message-id)
3051   "Fetch MESSAGE-ID."
3052   (save-excursion
3053     (set-buffer gnus-summary-buffer)
3054     (gnus-summary-refer-article message-id)))
3055
3056 (defun gnus-button-fetch-group (address)
3057   "Fetch GROUP specified by ADDRESS."
3058   (if (not (string-match "[:/]" address))
3059       ;; This is just a simple group url.
3060       (gnus-group-read-ephemeral-group address gnus-select-method)
3061     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3062                            address))
3063         (error "Can't parse %s" address)
3064       (gnus-group-read-ephemeral-group
3065        (match-string 4 address)
3066        `(nntp ,(match-string 1 address)
3067               (nntp-address ,(match-string 1 address))
3068               (nntp-port-number ,(if (match-end 3)
3069                                      (match-string 3 address)
3070                                    "nntp")))))))
3071
3072 (defun gnus-split-string (string pattern)
3073   "Return a list of substrings of STRING which are separated by PATTERN."
3074   (let (parts (start 0))
3075     (while (string-match pattern string start)
3076       (setq parts (cons (substring string start (match-beginning 0)) parts)
3077             start (match-end 0)))
3078     (nreverse (cons (substring string start) parts))))
3079
3080 (defun gnus-url-parse-query-string (query &optional downcase)
3081   (let (retval pairs cur key val)
3082     (setq pairs (gnus-split-string query "&"))
3083     (while pairs
3084       (setq cur (car pairs)
3085             pairs (cdr pairs))
3086       (if (not (string-match "=" cur))
3087           nil                           ; Grace
3088         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3089               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3090         (if downcase
3091             (setq key (downcase key)))
3092         (setq cur (assoc key retval))
3093         (if cur
3094             (setcdr cur (cons val (cdr cur)))
3095           (setq retval (cons (list key val) retval)))))
3096     retval))
3097
3098 (defun gnus-url-unhex (x)
3099   (if (> x ?9)
3100       (if (>= x ?a)
3101           (+ 10 (- x ?a))
3102         (+ 10 (- x ?A)))
3103     (- x ?0)))
3104
3105 (defun gnus-url-unhex-string (str &optional allow-newlines)
3106   "Remove %XXX embedded spaces, etc in a url.
3107 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3108 decoding of carriage returns and line feeds in the string, which is normally
3109 forbidden in URL encoding."
3110   (setq str (or str ""))
3111   (let ((tmp "")
3112         (case-fold-search t))
3113     (while (string-match "%[0-9a-f][0-9a-f]" str)
3114       (let* ((start (match-beginning 0))
3115              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3116              (code (+ (* 16 ch1)
3117                       (gnus-url-unhex (elt str (+ start 2))))))
3118         (setq tmp (concat
3119                    tmp (substring str 0 start)
3120                    (cond
3121                     (allow-newlines
3122                      (char-to-string code))
3123                     ((or (= code ?\n) (= code ?\r))
3124                      " ")
3125                     (t (char-to-string code))))
3126               str (substring str (match-end 0)))))
3127     (setq tmp (concat tmp str))
3128     tmp))
3129
3130 (defun gnus-url-mailto (url)
3131   ;; Send mail to someone
3132   (when (string-match "mailto:/*\\(.*\\)" url)
3133     (setq url (substring url (match-beginning 1) nil)))
3134   (let (to args source-url subject func)
3135     (if (string-match (regexp-quote "?") url)
3136         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3137               args (gnus-url-parse-query-string
3138                     (substring url (match-end 0) nil) t))
3139       (setq to (gnus-url-unhex-string url)))
3140     (setq args (cons (list "to" to) args)
3141           subject (cdr-safe (assoc "subject" args)))
3142     (message-mail)
3143     (while args
3144       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3145       (if (fboundp func)
3146           (funcall func)
3147         (message-position-on-field (caar args)))
3148       (insert (mapconcat 'identity (cdar args) ", "))
3149       (setq args (cdr args)))
3150     (if subject
3151         (message-goto-body)
3152       (message-goto-subject))))
3153
3154 (defun gnus-button-mailto (address)
3155   ;; Mail to ADDRESS.
3156   (set-buffer (gnus-copy-article-buffer))
3157   (message-reply address))
3158
3159 (defun gnus-button-reply (address)
3160   ;; Reply to ADDRESS.
3161   (message-reply address))
3162
3163 (defun gnus-button-url (address)
3164   "Browse ADDRESS."
3165   (funcall browse-url-browser-function address))
3166
3167 (defun gnus-button-embedded-url (address)
3168   "Browse ADDRESS."
3169   (funcall browse-url-browser-function (gnus-strip-whitespace address)))
3170
3171 ;;; Next/prev buttons in the article buffer.
3172
3173 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3174 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3175
3176 (defvar gnus-prev-page-map nil)
3177 (unless gnus-prev-page-map
3178   (setq gnus-prev-page-map (make-sparse-keymap))
3179   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3180   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3181
3182 (defun gnus-insert-prev-page-button ()
3183   (let ((buffer-read-only nil))
3184     (gnus-eval-format
3185      gnus-prev-page-line-format nil
3186      `(gnus-prev t local-map ,gnus-prev-page-map
3187                  gnus-callback gnus-article-button-prev-page))))
3188
3189 (defvar gnus-next-page-map nil)
3190 (unless gnus-next-page-map
3191   (setq gnus-next-page-map (make-keymap))
3192   (suppress-keymap gnus-prev-page-map)
3193   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3194   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3195
3196 (defun gnus-button-next-page ()
3197   "Go to the next page."
3198   (interactive)
3199   (let ((win (selected-window)))
3200     (select-window (get-buffer-window gnus-article-buffer t))
3201     (gnus-article-next-page)
3202     (select-window win)))
3203
3204 (defun gnus-button-prev-page ()
3205   "Go to the prev page."
3206   (interactive)
3207   (let ((win (selected-window)))
3208     (select-window (get-buffer-window gnus-article-buffer t))
3209     (gnus-article-prev-page)
3210     (select-window win)))
3211
3212 (defun gnus-insert-next-page-button ()
3213   (let ((buffer-read-only nil))
3214     (gnus-eval-format gnus-next-page-line-format nil
3215                       `(gnus-next t local-map ,gnus-next-page-map
3216                                   gnus-callback
3217                                   gnus-article-button-next-page))))
3218
3219 (defun gnus-article-button-next-page (arg)
3220   "Go to the next page."
3221   (interactive "P")
3222   (let ((win (selected-window)))
3223     (select-window (get-buffer-window gnus-article-buffer t))
3224     (gnus-article-next-page)
3225     (select-window win)))
3226
3227 (defun gnus-article-button-prev-page (arg)
3228   "Go to the prev page."
3229   (interactive "P")
3230   (let ((win (selected-window)))
3231     (select-window (get-buffer-window gnus-article-buffer t))
3232     (gnus-article-prev-page)
3233     (select-window win)))
3234
3235
3236 ;;; @ for mime-view
3237 ;;;
3238
3239 (defun gnus-content-header-filter ()
3240   "Header filter for mime-view.
3241 It is registered to variable `mime-view-content-header-filter-alist'."
3242   (goto-char (point-min))
3243   (while (re-search-forward "^[^ \t:]+:" nil t)
3244     (let ((start (match-beginning 0))
3245           (end (std11-field-end))
3246           )
3247       (save-restriction
3248         (narrow-to-region start end)
3249         (decode-mime-charset-region start end default-mime-charset)
3250         (goto-char (point-max))
3251         )))
3252   (eword-decode-header)
3253   )
3254
3255 (defun mime-view-quitting-method-for-gnus ()
3256   (if (not gnus-show-mime)
3257       (mime-view-kill-buffer))
3258   (delete-other-windows)
3259   (gnus-article-show-summary)
3260   (if (or (not gnus-show-mime)
3261           (null gnus-have-all-headers))
3262       (gnus-summary-select-article nil t)
3263     ))
3264
3265 (set-alist 'mime-view-content-header-filter-alist
3266            'gnus-original-article-mode
3267            (function gnus-content-header-filter))
3268
3269 (set-alist 'mime-text-decoder-alist
3270            'gnus-original-article-mode
3271            (function mime-text-decode-buffer))
3272
3273 (set-alist 'mime-view-quitting-method-alist
3274            'gnus-original-article-mode
3275            (function mime-view-quitting-method-for-gnus))
3276
3277 (set-alist 'mime-view-show-summary-method
3278            'gnus-original-article-mode
3279            (function mime-view-quitting-method-for-gnus))
3280
3281
3282 ;;; @ end
3283 ;;;
3284
3285 (gnus-ems-redefine)
3286
3287 (provide 'gnus-art)
3288
3289 (run-hooks 'gnus-art-load-hook)
3290
3291 ;;; gnus-art.el ends here