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