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