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