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