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