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