e60a1dcec994b77dea41dbbd64e151600fc12d58
[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 (with-current-buffer gnus-summary-buffer
1985                    default-mime-charset)))
1986     (make-local-variable 'default-mime-charset)
1987     (setq default-mime-charset charset)
1988     (mime-display-message mime-message-structure
1989                           gnus-article-buffer nil gnus-article-mode-map)
1990     (make-local-variable 'default-mime-charset)
1991     (setq default-mime-charset charset)
1992     )
1993   ;; `mime-display-message' changes current buffer to `gnus-article-buffer'.
1994   (make-local-variable 'mime-button-mother-dispatcher)
1995   (setq mime-button-mother-dispatcher
1996         (function gnus-article-push-button))
1997   (run-hooks 'gnus-mime-article-prepare-hook))
1998
1999 (defun gnus-article-display-traditional-message ()
2000   "Article display method for traditional message."
2001   (set-buffer gnus-article-buffer)
2002   (let (buffer-read-only)
2003     (erase-buffer)
2004     (insert-buffer-substring gnus-original-article-buffer)))
2005
2006 (defun gnus-article-make-full-mail-header (&optional number charset)
2007   "Create a new mail header structure in a raw article buffer."
2008   (unless (and number charset)
2009     (save-current-buffer
2010       (set-buffer gnus-summary-buffer)
2011       (unless number
2012         (setq number (or (cdr gnus-article-current) 0)))
2013       (unless charset
2014         (setq charset (or default-mime-charset 'x-ctext)))))
2015   (goto-char (point-min))
2016   (let ((header-end (if (search-forward "\n\n" nil t)
2017                         (1- (point))
2018                       (goto-char (point-max))))
2019         (chars (- (point-max) (point)))
2020         (lines (count-lines (point) (point-max)))
2021         (default-mime-charset charset)
2022         xref)
2023     (narrow-to-region (point-min) header-end)
2024     (setq xref (std11-fetch-field "xref"))
2025     (prog1
2026         (make-full-mail-header
2027          number
2028          (std11-fetch-field "subject")
2029          (std11-fetch-field "from")
2030          (std11-fetch-field "date")
2031          (std11-fetch-field "message-id")
2032          (std11-fetch-field "references")
2033          chars
2034          lines
2035          (when xref (concat "Xref: " xref)))
2036       (widen))))
2037
2038 (defun gnus-article-prepare (article &optional all-headers header)
2039   "Prepare ARTICLE in article mode buffer.
2040 ARTICLE should either be an article number or a Message-ID.
2041 If ARTICLE is an id, HEADER should be the article headers.
2042 If ALL-HEADERS is non-nil, no headers are hidden."
2043   (save-excursion
2044     ;; Make sure we start in a summary buffer.
2045     (unless (eq major-mode 'gnus-summary-mode)
2046       (set-buffer gnus-summary-buffer))
2047     (setq gnus-summary-buffer (current-buffer))
2048     (let* ((gnus-article (if header (mail-header-number header) article))
2049            (summary-buffer (current-buffer))
2050            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2051            (group gnus-newsgroup-name)
2052            result)
2053       (save-excursion
2054         (gnus-article-setup-buffer)
2055         (set-buffer gnus-original-article-buffer)
2056         ;; Deactivate active regions.
2057         (when (and (boundp 'transient-mark-mode)
2058                    transient-mark-mode)
2059           (setq mark-active nil))
2060         (if (not (setq result (let ((buffer-read-only nil))
2061                                 (gnus-request-article-this-buffer
2062                                  article group))))
2063             ;; There is no such article.
2064             (save-excursion
2065               (when (and (numberp article)
2066                          (not (memq article gnus-newsgroup-sparse)))
2067                 (setq gnus-article-current
2068                       (cons gnus-newsgroup-name article))
2069                 (set-buffer gnus-summary-buffer)
2070                 (setq gnus-current-article article)
2071                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2072                     (progn
2073                       (gnus-summary-set-agent-mark article)
2074                       (message "Message marked for downloading"))
2075                   (gnus-summary-mark-article article gnus-canceled-mark)
2076                   (unless (memq article gnus-newsgroup-sparse)
2077                     (gnus-error 1
2078                      "No such article (may have expired or been canceled)")))))
2079           (if (or (eq result 'pseudo)
2080                   (eq result 'nneething))
2081               (progn
2082                 (save-excursion
2083                   (set-buffer summary-buffer)
2084                   (push article gnus-newsgroup-history)
2085                   (setq gnus-last-article gnus-current-article
2086                         gnus-current-article 0
2087                         gnus-current-headers nil
2088                         gnus-article-current nil)
2089                   (if (eq result 'nneething)
2090                       (gnus-configure-windows 'summary)
2091                     (gnus-configure-windows 'article))
2092                   (gnus-set-global-variables))
2093                 (gnus-set-mode-line 'article))
2094             ;; The result from the `request' was an actual article -
2095             ;; or at least some text that is now displayed in the
2096             ;; article buffer.
2097             (when (and (numberp article)
2098                        (not (eq article gnus-current-article)))
2099               ;; Seems like a new article has been selected.
2100               ;; `gnus-current-article' must be an article number.
2101               (save-excursion
2102                 (set-buffer summary-buffer)
2103                 (push article gnus-newsgroup-history)
2104                 (setq gnus-last-article gnus-current-article
2105                       gnus-current-article article
2106                       gnus-current-headers
2107                       (gnus-summary-article-header gnus-current-article)
2108                       gnus-article-current
2109                       (cons gnus-newsgroup-name gnus-current-article))
2110                 (unless (vectorp gnus-current-headers)
2111                   (setq gnus-current-headers nil))
2112                 (gnus-summary-goto-subject gnus-current-article)
2113                 (when (gnus-summary-show-thread)
2114                   ;; If the summary buffer really was folded, the
2115                   ;; previous goto may not actually have gone to
2116                   ;; the right article, but the thread root instead.
2117                   ;; So we go again.
2118                   (gnus-summary-goto-subject gnus-current-article))
2119                 (gnus-run-hooks 'gnus-mark-article-hook)
2120                 (gnus-set-mode-line 'summary)
2121                 (when (gnus-visual-p 'article-highlight 'highlight)
2122                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2123                 ;; Set the global newsgroup variables here.
2124                 ;; Suggested by Jim Sisolak
2125                 ;; <sisolak@trans4.neep.wisc.edu>.
2126                 (gnus-set-global-variables)
2127                 (setq gnus-have-all-headers
2128                       (or all-headers gnus-show-all-headers))))
2129             (when (or (numberp article)
2130                       (stringp article))
2131               (gnus-article-prepare-display)
2132               ;; Do page break.
2133               (goto-char (point-min))
2134               (setq gnus-page-broken
2135                     (when gnus-break-pages
2136                       (gnus-narrow-to-page)
2137                       t)))
2138             (gnus-set-mode-line 'article)
2139             (gnus-configure-windows 'article)
2140             (goto-char (point-min))
2141             (search-forward "\n\n" nil t)
2142             (set-window-point (get-buffer-window (current-buffer)) (point))
2143             t))))))
2144
2145 (defun gnus-article-prepare-display ()
2146   "Make the current buffer look like a nice article."
2147   (let ((method
2148          (if gnus-show-mime
2149              (progn
2150                (setq mime-message-structure gnus-current-headers)
2151                gnus-article-display-method-for-mime)
2152            gnus-article-display-method-for-traditional)))
2153     (gnus-run-hooks 'gnus-tmp-internal-hook)
2154     (gnus-run-hooks 'gnus-article-prepare-hook)
2155     ;; Display message.
2156     (funcall method)
2157     ;; Associate this article with the current summary buffer.
2158     (setq gnus-article-current-summary gnus-summary-buffer)
2159     ;; Perform the article display hooks.
2160     (gnus-run-hooks 'gnus-article-display-hook)))
2161
2162 (defun gnus-article-wash-status ()
2163   "Return a string which display status of article washing."
2164   (save-excursion
2165     (set-buffer gnus-article-buffer)
2166     (let ((cite (gnus-article-hidden-text-p 'cite))
2167           (headers (gnus-article-hidden-text-p 'headers))
2168           (boring (gnus-article-hidden-text-p 'boring-headers))
2169           (pgp (gnus-article-hidden-text-p 'pgp))
2170           (pem (gnus-article-hidden-text-p 'pem))
2171           (signature (gnus-article-hidden-text-p 'signature))
2172           (overstrike (gnus-article-hidden-text-p 'overstrike))
2173           (emphasis (gnus-article-hidden-text-p 'emphasis))
2174           (mime gnus-show-mime))
2175       (format "%c%c%c%c%c%c%c"
2176               (if cite ?c ? )
2177               (if (or headers boring) ?h ? )
2178               (if (or pgp pem) ?p ? )
2179               (if signature ?s ? )
2180               (if overstrike ?o ? )
2181               (if mime ?m ? )
2182               (if emphasis ?e ? )))))
2183
2184 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2185
2186 (defun gnus-article-maybe-hide-headers ()
2187   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2188 Provided for backwards compatibility."
2189   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2190       gnus-inhibit-hiding
2191       (gnus-article-hide-headers)))
2192
2193 ;;; Article savers.
2194
2195 (defun gnus-output-to-file (file-name)
2196   "Append the current article to a file named FILE-NAME."
2197   (let ((artbuf (current-buffer)))
2198     (nnheader-temp-write nil
2199       (insert-buffer-substring artbuf)
2200       ;; Append newline at end of the buffer as separator, and then
2201       ;; save it to file.
2202       (goto-char (point-max))
2203       (insert "\n")
2204       (append-to-file (point-min) (point-max) file-name)
2205       t)))
2206
2207 (defun gnus-narrow-to-page (&optional arg)
2208   "Narrow the article buffer to a page.
2209 If given a numerical ARG, move forward ARG pages."
2210   (interactive "P")
2211   (setq arg (if arg (prefix-numeric-value arg) 0))
2212   (save-excursion
2213     (set-buffer gnus-article-buffer)
2214     (goto-char (point-min))
2215     (widen)
2216     ;; Remove any old next/prev buttons.
2217     (when (gnus-visual-p 'page-marker)
2218       (let ((buffer-read-only nil))
2219         (gnus-remove-text-with-property 'gnus-prev)
2220         (gnus-remove-text-with-property 'gnus-next)))
2221     (when
2222         (cond ((< arg 0)
2223                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2224               ((> arg 0)
2225                (re-search-forward page-delimiter nil 'move arg)))
2226       (goto-char (match-end 0)))
2227     (narrow-to-region
2228      (point)
2229      (if (re-search-forward page-delimiter nil 'move)
2230          (match-beginning 0)
2231        (point)))
2232     (when (and (gnus-visual-p 'page-marker)
2233                (not (= (point-min) 1)))
2234       (save-excursion
2235         (goto-char (point-min))
2236         (gnus-insert-prev-page-button)))
2237     (when (and (gnus-visual-p 'page-marker)
2238                (< (+ (point-max) 2) (buffer-size)))
2239       (save-excursion
2240         (goto-char (point-max))
2241         (gnus-insert-next-page-button)))))
2242
2243 ;; Article mode commands
2244
2245 (defun gnus-article-goto-next-page ()
2246   "Show the next page of the article."
2247   (interactive)
2248   (when (gnus-article-next-page)
2249     (goto-char (point-min))
2250     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2251
2252 (defun gnus-article-goto-prev-page ()
2253   "Show the next page of the article."
2254   (interactive)
2255   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2256     (gnus-article-prev-page nil)))
2257
2258 (defun gnus-article-next-page (&optional lines)
2259   "Show the next page of the current article.
2260 If end of article, return non-nil.  Otherwise return nil.
2261 Argument LINES specifies lines to be scrolled up."
2262   (interactive "p")
2263   (move-to-window-line -1)
2264   (if (save-excursion
2265         (end-of-line)
2266         (and (pos-visible-in-window-p)  ;Not continuation line.
2267              (eobp)))
2268       ;; Nothing in this page.
2269       (if (or (not gnus-page-broken)
2270               (save-excursion
2271                 (save-restriction
2272                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2273           t                             ;Nothing more.
2274         (gnus-narrow-to-page 1)         ;Go to next page.
2275         nil)
2276     ;; More in this page.
2277     (let ((scroll-in-place nil))
2278       (condition-case ()
2279           (scroll-up lines)
2280         (end-of-buffer
2281          ;; Long lines may cause an end-of-buffer error.
2282          (goto-char (point-max)))))
2283     (move-to-window-line 0)
2284     nil))
2285
2286 (defun gnus-article-prev-page (&optional lines)
2287   "Show previous page of current article.
2288 Argument LINES specifies lines to be scrolled down."
2289   (interactive "p")
2290   (move-to-window-line 0)
2291   (if (and gnus-page-broken
2292            (bobp)
2293            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2294       (progn
2295         (gnus-narrow-to-page -1)        ;Go to previous page.
2296         (goto-char (point-max))
2297         (recenter -1))
2298     (let ((scroll-in-place nil))
2299       (prog1
2300           (condition-case ()
2301               (scroll-down lines)
2302             (beginning-of-buffer
2303              (goto-char (point-min))))
2304         (move-to-window-line 0)))))
2305
2306 (defun gnus-article-refer-article ()
2307   "Read article specified by message-id around point."
2308   (interactive)
2309   (let ((point (point)))
2310     (search-forward ">" nil t)          ;Move point to end of "<....>".
2311     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2312         (let ((message-id (match-string 1)))
2313           (goto-char point)
2314           (set-buffer gnus-summary-buffer)
2315           (gnus-summary-refer-article message-id))
2316       (goto-char (point))
2317       (error "No references around point"))))
2318
2319 (defun gnus-article-show-summary ()
2320   "Reconfigure windows to show summary buffer."
2321   (interactive)
2322   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2323       (error "There is no summary buffer for this article buffer")
2324     (gnus-article-set-globals)
2325     (gnus-configure-windows 'article)
2326     (gnus-summary-goto-subject gnus-current-article)
2327     (gnus-summary-position-point)))
2328
2329 (defun gnus-article-describe-briefly ()
2330   "Describe article mode commands briefly."
2331   (interactive)
2332   (gnus-message 6
2333                 (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")))
2334
2335 (defun gnus-article-summary-command ()
2336   "Execute the last keystroke in the summary buffer."
2337   (interactive)
2338   (let ((obuf (current-buffer))
2339         (owin (current-window-configuration))
2340         func)
2341     (switch-to-buffer gnus-article-current-summary 'norecord)
2342     (setq func (lookup-key (current-local-map) (this-command-keys)))
2343     (call-interactively func)
2344     (set-buffer obuf)
2345     (set-window-configuration owin)
2346     (set-window-point (get-buffer-window (current-buffer)) (point))))
2347
2348 (defun gnus-article-summary-command-nosave ()
2349   "Execute the last keystroke in the summary buffer."
2350   (interactive)
2351   (let (func)
2352     (pop-to-buffer gnus-article-current-summary 'norecord)
2353     (setq func (lookup-key (current-local-map) (this-command-keys)))
2354     (call-interactively func)))
2355
2356 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2357   "Read a summary buffer key sequence and execute it from the article buffer."
2358   (interactive "P")
2359   (let ((nosaves
2360          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2361            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2362            "=" "^" "\M-^" "|"))
2363         (nosave-but-article
2364          '("A\r"))
2365         (nosave-in-article
2366          '("\C-d"))
2367         (up-to-top
2368          '("n" "Gn" "p" "Gp"))
2369         keys new-sum-point)
2370     (save-excursion
2371       (set-buffer gnus-article-current-summary)
2372       (let (gnus-pick-mode)
2373         (push (or key last-command-event) unread-command-events)
2374         (setq keys (read-key-sequence nil))))
2375     (message "")
2376
2377     (if (or (member keys nosaves)
2378             (member keys nosave-but-article)
2379             (member keys nosave-in-article))
2380         (let (func)
2381           (save-window-excursion
2382             (pop-to-buffer gnus-article-current-summary 'norecord)
2383             ;; We disable the pick minor mode commands.
2384             (let (gnus-pick-mode)
2385               (setq func (lookup-key (current-local-map) keys))))
2386           (if (not func)
2387               (ding)
2388             (unless (member keys nosave-in-article)
2389               (set-buffer gnus-article-current-summary))
2390             (call-interactively func)
2391             (setq new-sum-point (point)))
2392           (when (member keys nosave-but-article)
2393             (pop-to-buffer gnus-article-buffer 'norecord)))
2394       ;; These commands should restore window configuration.
2395       (let ((obuf (current-buffer))
2396             (owin (current-window-configuration))
2397             (opoint (point))
2398             (summary gnus-article-current-summary)
2399             func in-buffer selected)
2400         (if not-restore-window
2401             (pop-to-buffer summary 'norecord)
2402           (switch-to-buffer summary 'norecord))
2403         (setq in-buffer (current-buffer))
2404         ;; We disable the pick minor mode commands.
2405         (if (setq func (let (gnus-pick-mode)
2406                          (lookup-key (current-local-map) keys)))
2407             (progn
2408               (call-interactively func)
2409               (setq new-sum-point (point)))
2410           (ding))
2411         (when (eq in-buffer (current-buffer))
2412           (setq selected (gnus-summary-select-article))
2413           (set-buffer obuf)
2414           (unless not-restore-window
2415             (set-window-configuration owin))
2416           (unless (or (not (eq selected 'old)) (member keys up-to-top))
2417             (set-window-point (get-buffer-window (current-buffer))
2418                               opoint))
2419           (let ((win (get-buffer-window gnus-article-current-summary)))
2420             (when win
2421               (set-window-point win new-sum-point))))))))
2422
2423 (defun gnus-article-hide (&optional arg force)
2424   "Hide all the gruft in the current article.
2425 This means that PGP stuff, signatures, cited text and (some)
2426 headers will be hidden.
2427 If given a prefix, show the hidden text instead."
2428   (interactive (append (gnus-article-hidden-arg) (list 'force)))
2429   (gnus-article-hide-headers arg)
2430   (gnus-article-hide-pgp arg)
2431   (gnus-article-hide-citation-maybe arg force)
2432   (gnus-article-hide-signature arg))
2433
2434 (defun gnus-article-maybe-highlight ()
2435   "Do some article highlighting if article highlighting is requested."
2436   (when (gnus-visual-p 'article-highlight 'highlight)
2437     (gnus-article-highlight-some)))
2438
2439 (defun gnus-check-group-server ()
2440   ;; Make sure the connection to the server is alive.
2441   (unless (gnus-server-opened
2442            (gnus-find-method-for-group gnus-newsgroup-name))
2443     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
2444     (gnus-request-group gnus-newsgroup-name t)))
2445
2446 (defun gnus-request-article-this-buffer (article group)
2447   "Get an article and insert it into this buffer."
2448   (let (do-update-line sparse-header)
2449     (prog1
2450         (save-excursion
2451           (erase-buffer)
2452           (gnus-kill-all-overlays)
2453           (setq group (or group gnus-newsgroup-name))
2454
2455           ;; Using `gnus-request-article' directly will insert the article into
2456           ;; `nntp-server-buffer' - so we'll save some time by not having to
2457           ;; copy it from the server buffer into the article buffer.
2458
2459           ;; We only request an article by message-id when we do not have the
2460           ;; headers for it, so we'll have to get those.
2461           (when (stringp article)
2462             (let ((gnus-override-method gnus-refer-article-method))
2463               (gnus-read-header article)))
2464
2465           ;; If the article number is negative, that means that this article
2466           ;; doesn't belong in this newsgroup (possibly), so we find its
2467           ;; message-id and request it by id instead of number.
2468           (when (and (numberp article)
2469                      gnus-summary-buffer
2470                      (get-buffer gnus-summary-buffer)
2471                      (gnus-buffer-exists-p gnus-summary-buffer))
2472             (save-excursion
2473               (set-buffer gnus-summary-buffer)
2474               (let ((header (gnus-summary-article-header article)))
2475                 (when (< article 0)
2476                   (cond
2477                    ((memq article gnus-newsgroup-sparse)
2478                     ;; This is a sparse gap article.
2479                     (setq do-update-line article)
2480                     (setq article (mail-header-id header))
2481                     (let ((gnus-override-method gnus-refer-article-method))
2482                       (setq sparse-header (gnus-read-header article)))
2483                     (setq gnus-newsgroup-sparse
2484                           (delq article gnus-newsgroup-sparse)))
2485                    ((vectorp header)
2486                     ;; It's a real article.
2487                     (setq article (mail-header-id header)))
2488                    (t
2489                     ;; It is an extracted pseudo-article.
2490                     (setq article 'pseudo)
2491                     (gnus-request-pseudo-article header))))
2492
2493                 (let ((method (gnus-find-method-for-group
2494                                gnus-newsgroup-name)))
2495                   (when (and (eq (car method) 'nneething)
2496                              (vectorp header))
2497                     (let ((dir (concat
2498                                 (file-name-as-directory
2499                                  (or (cadr (assq 'nneething-address method))
2500                                      (nth 1 method)))
2501                                 (mail-header-subject header))))
2502                       (when (file-directory-p dir)
2503                         (setq article 'nneething)
2504                         (gnus-group-enter-directory dir))))))))
2505
2506           (cond
2507            ;; Refuse to select canceled articles.
2508            ((and (numberp article)
2509                  gnus-summary-buffer
2510                  (get-buffer gnus-summary-buffer)
2511                  (gnus-buffer-exists-p gnus-summary-buffer)
2512                  (eq (cdr (save-excursion
2513                             (set-buffer gnus-summary-buffer)
2514                             (assq article gnus-newsgroup-reads)))
2515                      gnus-canceled-mark))
2516             nil)
2517            ;; Check the backlog.
2518            ((and gnus-keep-backlog
2519                  (gnus-backlog-request-article group article (current-buffer)))
2520             'article)
2521            ;; Check asynchronous pre-fetch.
2522            ((gnus-async-request-fetched-article group article (current-buffer))
2523             (gnus-async-prefetch-next group article gnus-summary-buffer)
2524             (when (and (numberp article) gnus-keep-backlog)
2525               (gnus-backlog-enter-article group article (current-buffer)))
2526             'article)
2527            ;; Check the cache.
2528            ((and gnus-use-cache
2529                  (numberp article)
2530                  (gnus-cache-request-article article group))
2531             'article)
2532            ;; Get the article and put into the article buffer.
2533            ((or (stringp article) (numberp article))
2534             (let ((gnus-override-method
2535                    (and (stringp article) gnus-refer-article-method))
2536                   (buffer-read-only nil))
2537               (erase-buffer)
2538               (gnus-kill-all-overlays)
2539               (gnus-check-group-server)
2540               (when (gnus-request-article article group (current-buffer))
2541                 (when (numberp article)
2542                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2543                   (when gnus-keep-backlog
2544                     (gnus-backlog-enter-article
2545                      group article (current-buffer))))
2546                 'article)))
2547            ;; It was a pseudo.
2548            (t article)))
2549
2550       ;; Associate this article with the current summary buffer.
2551       (setq gnus-article-current-summary gnus-summary-buffer)
2552
2553       ;; Take the article from the original article buffer
2554       ;; and place it in the buffer it's supposed to be in.
2555       (when (and (get-buffer gnus-article-buffer)
2556                  (equal (buffer-name (current-buffer))
2557                         (buffer-name (get-buffer gnus-article-buffer))))
2558         (save-excursion
2559           (if (get-buffer gnus-original-article-buffer)
2560               (set-buffer gnus-original-article-buffer)
2561             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2562             (buffer-disable-undo (current-buffer))
2563             (setq major-mode 'gnus-original-article-mode)
2564             (setq buffer-read-only t))
2565           (let (buffer-read-only)
2566             (erase-buffer)
2567             (insert-buffer-substring gnus-article-buffer))
2568           (setq gnus-original-article (cons group article))))
2569
2570       ;; Update sparse articles.
2571       (when (and do-update-line
2572                  (or (numberp article)
2573                      (stringp article)))
2574         (let ((buf (current-buffer)))
2575           (set-buffer gnus-summary-buffer)
2576           (gnus-summary-update-article do-update-line sparse-header)
2577           (gnus-summary-goto-subject do-update-line nil t)
2578           (set-window-point (get-buffer-window (current-buffer) t)
2579                             (point))
2580           (set-buffer buf))))))
2581
2582 ;;;
2583 ;;; Article editing
2584 ;;;
2585
2586 (defcustom gnus-article-edit-mode-hook nil
2587   "Hook run in article edit mode buffers."
2588   :group 'gnus-article-various
2589   :type 'hook)
2590
2591 (defcustom gnus-article-edit-article-setup-function
2592   'gnus-article-mime-edit-article-setup
2593   "Function called to setup an editing article buffer."
2594   :group 'gnus-article-various
2595   :type 'function)
2596
2597 (defvar gnus-article-edit-done-function nil)
2598
2599 (defvar gnus-article-edit-mode-map nil)
2600
2601 (unless gnus-article-edit-mode-map
2602   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2603
2604   (gnus-define-keys gnus-article-edit-mode-map
2605     "\C-c\C-c" gnus-article-edit-done
2606     "\C-c\C-k" gnus-article-edit-exit)
2607
2608   (gnus-define-keys (gnus-article-edit-wash-map
2609                      "\C-c\C-w" gnus-article-edit-mode-map)
2610     "f" gnus-article-edit-full-stops))
2611
2612 (defun gnus-article-edit-mode ()
2613   "Major mode for editing articles.
2614 This is an extended text-mode.
2615
2616 \\{gnus-article-edit-mode-map}"
2617   (interactive)
2618   (setq major-mode 'gnus-article-edit-mode)
2619   (setq mode-name "Article Edit")
2620   (use-local-map gnus-article-edit-mode-map)
2621   (make-local-variable 'gnus-article-edit-done-function)
2622   (make-local-variable 'gnus-prev-winconf)
2623   (setq buffer-read-only nil)
2624   (buffer-enable-undo)
2625   (widen)
2626   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
2627
2628 (defun gnus-article-edit (&optional force)
2629   "Edit the current article.
2630 This will have permanent effect only in mail groups.
2631 If FORCE is non-nil, allow editing of articles even in read-only
2632 groups."
2633   (interactive "P")
2634   (when (and (not force)
2635              (gnus-group-read-only-p))
2636     (error "The current newsgroup does not support article editing"))
2637   (gnus-article-date-original)
2638   (gnus-article-edit-article
2639    `(lambda (no-highlight)
2640       (gnus-summary-edit-article-done
2641        ,(or (mail-header-references gnus-current-headers) "")
2642        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2643
2644 (defun gnus-article-edit-article (exit-func)
2645   "Start editing the contents of the current article buffer."
2646   (let ((winconf (current-window-configuration)))
2647     (set-buffer gnus-article-buffer)
2648     (gnus-article-edit-mode)
2649     (gnus-article-delete-text-of-type 'annotation)
2650     (gnus-set-text-properties (point-min) (point-max) nil)
2651     (gnus-configure-windows 'edit-article)
2652     (setq gnus-article-edit-done-function exit-func)
2653     (setq gnus-prev-winconf winconf)
2654     (when gnus-article-edit-article-setup-function
2655       (funcall gnus-article-edit-article-setup-function))
2656     (gnus-message 6 "C-c C-c to end edits")))
2657
2658 (defun gnus-article-edit-done (&optional arg)
2659   "Update the article edits and exit."
2660   (interactive "P")
2661   (save-excursion
2662     (save-restriction
2663       (widen)
2664       (goto-char (point-min))
2665       (when (search-forward "\n\n" nil 1)
2666         (let ((lines (count-lines (point) (point-max)))
2667               (length (- (point-max) (point)))
2668               (case-fold-search t)
2669               (body (copy-marker (point))))
2670           (goto-char (point-min))
2671           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
2672             (delete-region (match-beginning 1) (match-end 1))
2673             (insert (number-to-string length)))
2674           (goto-char (point-min))
2675           (when (re-search-forward
2676                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
2677             (delete-region (match-beginning 1) (match-end 1))
2678             (insert (number-to-string length)))
2679           (goto-char (point-min))
2680           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
2681             (delete-region (match-beginning 1) (match-end 1))
2682             (insert (number-to-string lines)))))))
2683   (let ((func gnus-article-edit-done-function)
2684         (buf (current-buffer))
2685         (start (window-start)))
2686     (remove-hook 'gnus-article-mode-hook
2687                  'gnus-article-mime-edit-article-unwind)
2688     (gnus-article-edit-exit)
2689     (save-excursion
2690       (set-buffer buf)
2691       (let ((buffer-read-only nil))
2692         (funcall func arg)))
2693     (set-buffer buf)
2694     (set-window-start (get-buffer-window buf) start)
2695     (set-window-point (get-buffer-window buf) (point))))
2696
2697 (defun gnus-article-edit-exit ()
2698   "Exit the article editing without updating."
2699   (interactive)
2700   ;; We remove all text props from the article buffer.
2701   (let ((buf (format "%s" (buffer-string)))
2702         (curbuf (current-buffer))
2703         (p (point))
2704         (window-start (window-start)))
2705     (erase-buffer)
2706     (insert buf)
2707     (let ((winconf gnus-prev-winconf))
2708       (gnus-article-mode)
2709       ;; The cache and backlog have to be flushed somewhat.
2710       (when gnus-use-cache
2711         (gnus-cache-update-article
2712          (car gnus-article-current) (cdr gnus-article-current)))
2713       (when gnus-keep-backlog
2714         (gnus-backlog-remove-article
2715          (car gnus-article-current) (cdr gnus-article-current)))
2716       ;; Flush original article as well.
2717       (save-excursion
2718         (when (get-buffer gnus-original-article-buffer)
2719           (set-buffer gnus-original-article-buffer)
2720           (setq gnus-original-article nil)))
2721       (set-window-configuration winconf)
2722       ;; Tippy-toe some to make sure that point remains where it was.
2723       (let ((buf (current-buffer)))
2724         (set-buffer curbuf)
2725         (set-window-start (get-buffer-window (current-buffer)) window-start)
2726         (goto-char p)
2727         (set-buffer buf)))))
2728
2729 (defun gnus-article-edit-full-stops ()
2730   "Interactively repair spacing at end of sentences."
2731   (interactive)
2732   (save-excursion
2733     (goto-char (point-min))
2734     (search-forward-regexp "^$" nil t)
2735     (let ((case-fold-search nil))
2736       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2737
2738 ;;;
2739 ;;; Article editing with MIME-Edit
2740 ;;;
2741
2742 (defcustom gnus-article-mime-edit-article-setup-hook nil
2743   "Hook run after setting up a MIME editing article buffer."
2744   :group 'gnus-article-various
2745   :type 'hook)
2746
2747 (defun gnus-article-mime-edit-article-unwind ()
2748   "Unwind `gnus-article-buffer' if article editing was given up."
2749   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
2750   (when mime-edit-mode-flag
2751     (mime-edit-exit 'nomime 'no-error)
2752     (message ""))
2753   (when (featurep 'font-lock)
2754     (setq font-lock-defaults nil)
2755     (font-lock-mode 0)))
2756
2757 (defun gnus-article-mime-edit-article-setup ()
2758   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
2759 after replacing with the original article."
2760   (setq gnus-show-mime t)
2761   (setq gnus-article-edit-done-function
2762         `(lambda (&rest args)
2763            (when mime-edit-mode-flag
2764              (mime-edit-exit)
2765              (message ""))
2766            (goto-char (point-min))
2767            (let (case-fold-search)
2768              (when (re-search-forward
2769                     (format "^%s$" (regexp-quote mail-header-separator))
2770                     nil t)
2771                (replace-match "")))
2772            (when (featurep 'font-lock)
2773              (setq font-lock-defaults nil)
2774              (font-lock-mode 0))
2775            (apply ,gnus-article-edit-done-function args)
2776            (set-buffer gnus-original-article-buffer)
2777            (erase-buffer)
2778            (insert-buffer gnus-article-buffer)
2779            (setq gnus-current-headers (gnus-article-make-full-mail-header))
2780            (gnus-article-prepare-display)))
2781   (define-key (current-local-map) "\C-c\C-k" 'gnus-article-mime-edit-exit)
2782   (erase-buffer)
2783   (insert-buffer gnus-original-article-buffer)
2784   (mime-edit-again)
2785   (when (featurep 'font-lock)
2786     (set (make-local-variable 'font-lock-defaults)
2787          '(message-font-lock-keywords t))
2788     (font-lock-set-defaults)
2789     (turn-on-font-lock))
2790   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
2791   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
2792
2793 (defun gnus-article-mime-edit-exit ()
2794   "Exit the article MIME editing without updating."
2795   (interactive)
2796   (let ((winconf gnus-prev-winconf)
2797         buf)
2798     (when mime-edit-mode-flag
2799       (mime-edit-exit)
2800       (message ""))
2801     (goto-char (point-min))
2802     (let (case-fold-search)
2803       (when (re-search-forward
2804              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
2805         (replace-match "")))
2806     (when (featurep 'font-lock)
2807       (setq font-lock-defaults nil)
2808       (font-lock-mode 0))
2809     ;; We remove all text props from the article buffer.
2810     (setq buf (format "%s" (buffer-string)))
2811     (set-buffer (get-buffer-create gnus-original-article-buffer))
2812     (erase-buffer)
2813     (insert buf)
2814     (setq gnus-current-headers (gnus-article-make-full-mail-header))
2815     (gnus-article-prepare-display)
2816     (set-window-configuration winconf)))
2817
2818 ;;;
2819 ;;; Article highlights
2820 ;;;
2821
2822 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2823
2824 ;;; Internal Variables:
2825
2826 (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\\)"
2827   "Regular expression that matches URLs."
2828   :group 'gnus-article-buttons
2829   :type 'regexp)
2830
2831 (defcustom gnus-button-alist
2832   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2833      gnus-button-message-id 2)
2834     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2835     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
2836      1 t
2837      gnus-button-fetch-group 4)
2838     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2839     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2840      t gnus-button-message-id 3)
2841     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2842     ("mailto:\\([a-zA-Z.-@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
2843     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2844     ;; This is how URLs _should_ be embedded in text...
2845     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2846     ;; Raw URLs.
2847     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2848   "*Alist of regexps matching buttons in article bodies.
2849
2850 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2851 REGEXP: is the string matching text around the button,
2852 BUTTON: is the number of the regexp grouping actually matching the button,
2853 FORM: is a lisp expression which must eval to true for the button to
2854 be added,
2855 CALLBACK: is the function to call when the user push this button, and each
2856 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2857
2858 CALLBACK can also be a variable, in that case the value of that
2859 variable it the real callback function."
2860   :group 'gnus-article-buttons
2861   :type '(repeat (list regexp
2862                        (integer :tag "Button")
2863                        (sexp :tag "Form")
2864                        (function :tag "Callback")
2865                        (repeat :tag "Par"
2866                                :inline t
2867                                (integer :tag "Regexp group")))))
2868
2869 (defcustom gnus-header-button-alist
2870   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2871      0 t gnus-button-message-id 0)
2872     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2873     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2874      0 t gnus-button-mailto 0)
2875     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2876     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2877     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2878     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2879      gnus-button-message-id 3))
2880   "*Alist of headers and regexps to match buttons in article heads.
2881
2882 This alist is very similar to `gnus-button-alist', except that each
2883 alist has an additional HEADER element first in each entry:
2884
2885 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2886
2887 HEADER is a regexp to match a header.  For a fuller explanation, see
2888 `gnus-button-alist'."
2889   :group 'gnus-article-buttons
2890   :group 'gnus-article-headers
2891   :type '(repeat (list (regexp :tag "Header")
2892                        regexp
2893                        (integer :tag "Button")
2894                        (sexp :tag "Form")
2895                        (function :tag "Callback")
2896                        (repeat :tag "Par"
2897                                :inline t
2898                                (integer :tag "Regexp group")))))
2899
2900 (defvar gnus-button-regexp nil)
2901 (defvar gnus-button-marker-list nil)
2902 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2903
2904 (defvar gnus-button-last nil)
2905 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2906
2907 ;;; Commands:
2908
2909 (defun gnus-article-push-button (event)
2910   "Check text under the mouse pointer for a callback function.
2911 If the text under the mouse pointer has a `gnus-callback' property,
2912 call it with the value of the `gnus-data' text property."
2913   (interactive "e")
2914   (set-buffer (window-buffer (posn-window (event-start event))))
2915   (let* ((pos (posn-point (event-start event)))
2916          (data (get-text-property pos 'gnus-data))
2917          (fun (get-text-property pos 'gnus-callback)))
2918     (goto-char pos)
2919     (when fun
2920       (funcall fun data))))
2921
2922 (defun gnus-article-press-button ()
2923   "Check text at point for a callback function.
2924 If the text at point has a `gnus-callback' property,
2925 call it with the value of the `gnus-data' text property."
2926   (interactive)
2927   (let* ((data (get-text-property (point) 'gnus-data))
2928          (fun (get-text-property (point) 'gnus-callback)))
2929     (when fun
2930       (funcall fun data))))
2931
2932 (defun gnus-article-prev-button (n)
2933   "Move point to N buttons backward.
2934 If N is negative, move forward instead."
2935   (interactive "p")
2936   (gnus-article-next-button (- n)))
2937
2938 (defun gnus-article-next-button (n)
2939   "Move point to N buttons forward.
2940 If N is negative, move backward instead."
2941   (interactive "p")
2942   (let ((function (if (< n 0) 'previous-single-property-change
2943                     'next-single-property-change))
2944         (inhibit-point-motion-hooks t)
2945         (backward (< n 0))
2946         (limit (if (< n 0) (point-min) (point-max))))
2947     (setq n (abs n))
2948     (while (and (not (= limit (point)))
2949                 (> n 0))
2950       ;; Skip past the current button.
2951       (when (get-text-property (point) 'gnus-callback)
2952         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2953       ;; Go to the next (or previous) button.
2954       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2955       ;; Put point at the start of the button.
2956       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2957         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2958       ;; Skip past intangible buttons.
2959       (when (get-text-property (point) 'intangible)
2960         (incf n))
2961       (decf n))
2962     (unless (zerop n)
2963       (gnus-message 5 "No more buttons"))
2964     n))
2965
2966 (defun gnus-article-highlight (&optional force)
2967   "Highlight current article.
2968 This function calls `gnus-article-highlight-headers',
2969 `gnus-article-highlight-citation',
2970 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2971 do the highlighting.  See the documentation for those functions."
2972   (interactive (list 'force))
2973   (gnus-article-highlight-headers)
2974   (gnus-article-highlight-citation force)
2975   (gnus-article-highlight-signature)
2976   (gnus-article-add-buttons force)
2977   (gnus-article-add-buttons-to-head))
2978
2979 (defun gnus-article-highlight-some (&optional force)
2980   "Highlight current article.
2981 This function calls `gnus-article-highlight-headers',
2982 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2983 do the highlighting.  See the documentation for those functions."
2984   (interactive (list 'force))
2985   (gnus-article-highlight-headers)
2986   (gnus-article-highlight-signature)
2987   (gnus-article-add-buttons))
2988
2989 (defun gnus-article-highlight-headers ()
2990   "Highlight article headers as specified by `gnus-header-face-alist'."
2991   (interactive)
2992   (save-excursion
2993     (set-buffer gnus-article-buffer)
2994     (save-restriction
2995       (let ((alist gnus-header-face-alist)
2996             (buffer-read-only nil)
2997             (case-fold-search t)
2998             (inhibit-point-motion-hooks t)
2999             entry regexp header-face field-face from hpoints fpoints)
3000         (message-narrow-to-head)
3001         (while (setq entry (pop alist))
3002           (goto-char (point-min))
3003           (setq regexp (concat "^\\("
3004                                (if (string-equal "" (nth 0 entry))
3005                                    "[^\t ]"
3006                                  (nth 0 entry))
3007                                "\\)")
3008                 header-face (nth 1 entry)
3009                 field-face (nth 2 entry))
3010           (while (and (re-search-forward regexp nil t)
3011                       (not (eobp)))
3012             (beginning-of-line)
3013             (setq from (point))
3014             (unless (search-forward ":" nil t)
3015               (forward-char 1))
3016             (when (and header-face
3017                        (not (memq (point) hpoints)))
3018               (push (point) hpoints)
3019               (gnus-put-text-property from (point) 'face header-face))
3020             (when (and field-face
3021                        (not (memq (setq from (point)) fpoints)))
3022               (push from fpoints)
3023               (if (re-search-forward "^[^ \t]" nil t)
3024                   (forward-char -2)
3025                 (goto-char (point-max)))
3026               (gnus-put-text-property from (point) 'face field-face))))))))
3027
3028 (defun gnus-article-highlight-signature ()
3029   "Highlight the signature in an article.
3030 It does this by highlighting everything after
3031 `gnus-signature-separator' using `gnus-signature-face'."
3032   (interactive)
3033   (save-excursion
3034     (set-buffer gnus-article-buffer)
3035     (let ((buffer-read-only nil)
3036           (inhibit-point-motion-hooks t))
3037       (save-restriction
3038         (when (and gnus-signature-face
3039                    (gnus-article-narrow-to-signature))
3040           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
3041                             'face gnus-signature-face)
3042           (widen)
3043           (gnus-article-search-signature)
3044           (let ((start (match-beginning 0))
3045                 (end (set-marker (make-marker) (1+ (match-end 0)))))
3046             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
3047                                      end)))))))
3048
3049 (defun gnus-button-in-region-p (b e prop)
3050   "Say whether PROP exists in the region."
3051   (text-property-not-all b e prop nil))
3052
3053 (defun gnus-article-add-buttons (&optional force)
3054   "Find external references in the article and make buttons of them.
3055 \"External references\" are things like Message-IDs and URLs, as
3056 specified by `gnus-button-alist'."
3057   (interactive (list 'force))
3058   (save-excursion
3059     (set-buffer gnus-article-buffer)
3060     (let ((buffer-read-only nil)
3061           (inhibit-point-motion-hooks t)
3062           (case-fold-search t)
3063           (alist gnus-button-alist)
3064           beg entry regexp)
3065       ;; Remove all old markers.
3066       (let (marker entry)
3067         (while (setq marker (pop gnus-button-marker-list))
3068           (goto-char marker)
3069           (when (setq entry (gnus-button-entry))
3070             (put-text-property (match-beginning (nth 1 entry))
3071                                (match-end (nth 1 entry))
3072                                'gnus-callback nil))
3073           (set-marker marker nil)))
3074       ;; We skip the headers.
3075       (goto-char (point-min))
3076       (unless (search-forward "\n\n" nil t)
3077         (goto-char (point-max)))
3078       (setq beg (point))
3079       (while (setq entry (pop alist))
3080         (setq regexp (car entry))
3081         (goto-char beg)
3082         (while (re-search-forward regexp nil t)
3083           (let* ((start (and entry (match-beginning (nth 1 entry))))
3084                  (end (and entry (match-end (nth 1 entry))))
3085                  (from (match-beginning 0)))
3086             (when (and (or (eq t (nth 2 entry))
3087                            (eval (nth 2 entry)))
3088                        (not (gnus-button-in-region-p
3089                              start end 'gnus-callback)))
3090               ;; That optional form returned non-nil, so we add the
3091               ;; button.
3092               (gnus-article-add-button
3093                start end 'gnus-button-push
3094                (car (push (set-marker (make-marker) from)
3095                           gnus-button-marker-list))))))))))
3096
3097 ;; Add buttons to the head of an article.
3098 (defun gnus-article-add-buttons-to-head ()
3099   "Add buttons to the head of the article."
3100   (interactive)
3101   (save-excursion
3102     (set-buffer gnus-article-buffer)
3103     (let ((buffer-read-only nil)
3104           (inhibit-point-motion-hooks t)
3105           (case-fold-search t)
3106           (alist gnus-header-button-alist)
3107           entry beg end)
3108       (nnheader-narrow-to-headers)
3109       (while alist
3110         ;; Each alist entry.
3111         (setq entry (car alist)
3112               alist (cdr alist))
3113         (goto-char (point-min))
3114         (while (re-search-forward (car entry) nil t)
3115           ;; Each header matching the entry.
3116           (setq beg (match-beginning 0))
3117           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
3118                              (match-beginning 0))
3119                         (point-max)))
3120           (goto-char beg)
3121           (while (re-search-forward (nth 1 entry) end t)
3122             ;; Each match within a header.
3123             (let* ((entry (cdr entry))
3124                    (start (match-beginning (nth 1 entry)))
3125                    (end (match-end (nth 1 entry)))
3126                    (form (nth 2 entry)))
3127               (goto-char (match-end 0))
3128               (when (eval form)
3129                 (gnus-article-add-button
3130                  start end (nth 3 entry)
3131                  (buffer-substring (match-beginning (nth 4 entry))
3132                                    (match-end (nth 4 entry)))))))
3133           (goto-char end))))
3134     (widen)))
3135
3136 ;;; External functions:
3137
3138 (defun gnus-article-add-button (from to fun &optional data)
3139   "Create a button between FROM and TO with callback FUN and data DATA."
3140   (when gnus-article-button-face
3141     (gnus-overlay-put (gnus-make-overlay from to)
3142                       'face gnus-article-button-face))
3143   (gnus-add-text-properties
3144    from to
3145    (nconc (and gnus-article-mouse-face
3146                (list gnus-mouse-face-prop gnus-article-mouse-face))
3147           (list 'gnus-callback fun)
3148           (and data (list 'gnus-data data)))))
3149
3150 ;;; Internal functions:
3151
3152 (defun gnus-article-set-globals ()
3153   (save-excursion
3154     (set-buffer gnus-summary-buffer)
3155     (gnus-set-global-variables)))
3156
3157 (defun gnus-signature-toggle (end)
3158   (save-excursion
3159     (set-buffer gnus-article-buffer)
3160     (let ((buffer-read-only nil)
3161           (inhibit-point-motion-hooks t))
3162       (if (get-text-property end 'invisible)
3163           (gnus-article-unhide-text end (point-max))
3164         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3165
3166 (defun gnus-button-entry ()
3167   ;; Return the first entry in `gnus-button-alist' matching this place.
3168   (let ((alist gnus-button-alist)
3169         (entry nil))
3170     (while alist
3171       (setq entry (pop alist))
3172       (if (looking-at (car entry))
3173           (setq alist nil)
3174         (setq entry nil)))
3175     entry))
3176
3177 (defun gnus-button-push (marker)
3178   ;; Push button starting at MARKER.
3179   (save-excursion
3180     (set-buffer gnus-article-buffer)
3181     (goto-char marker)
3182     (let* ((entry (gnus-button-entry))
3183            (inhibit-point-motion-hooks t)
3184            (fun (nth 3 entry))
3185            (args (mapcar (lambda (group)
3186                            (let ((string (match-string group)))
3187                              (gnus-set-text-properties
3188                               0 (length string) nil string)
3189                              string))
3190                          (nthcdr 4 entry))))
3191       (cond
3192        ((fboundp fun)
3193         (apply fun args))
3194        ((and (boundp fun)
3195              (fboundp (symbol-value fun)))
3196         (apply (symbol-value fun) args))
3197        (t
3198         (gnus-message 1 "You must define `%S' to use this button"
3199                       (cons fun args)))))))
3200
3201 (defun gnus-button-message-id (message-id)
3202   "Fetch MESSAGE-ID."
3203   (save-excursion
3204     (set-buffer gnus-summary-buffer)
3205     (gnus-summary-refer-article message-id)))
3206
3207 (defun gnus-button-fetch-group (address)
3208   "Fetch GROUP specified by ADDRESS."
3209   (if (not (string-match "[:/]" address))
3210       ;; This is just a simple group url.
3211       (gnus-group-read-ephemeral-group address gnus-select-method)
3212     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3213                            address))
3214         (error "Can't parse %s" address)
3215       (gnus-group-read-ephemeral-group
3216        (match-string 4 address)
3217        `(nntp ,(match-string 1 address)
3218               (nntp-address ,(match-string 1 address))
3219               (nntp-port-number ,(if (match-end 3)
3220                                      (match-string 3 address)
3221                                    "nntp")))))))
3222
3223 (defun gnus-url-parse-query-string (query &optional downcase)
3224   (let (retval pairs cur key val)
3225     (setq pairs (gnus-split-string query "&"))
3226     (while pairs
3227       (setq cur (car pairs)
3228             pairs (cdr pairs))
3229       (if (not (string-match "=" cur))
3230           nil                           ; Grace
3231         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3232               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3233         (if downcase
3234             (setq key (downcase key)))
3235         (setq cur (assoc key retval))
3236         (if cur
3237             (setcdr cur (cons val (cdr cur)))
3238           (setq retval (cons (list key val) retval)))))
3239     retval))
3240
3241 (defun gnus-url-unhex (x)
3242   (if (> x ?9)
3243       (if (>= x ?a)
3244           (+ 10 (- x ?a))
3245         (+ 10 (- x ?A)))
3246     (- x ?0)))
3247
3248 (defun gnus-url-unhex-string (str &optional allow-newlines)
3249   "Remove %XXX embedded spaces, etc in a url.
3250 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3251 decoding of carriage returns and line feeds in the string, which is normally
3252 forbidden in URL encoding."
3253   (setq str (or str ""))
3254   (let ((tmp "")
3255         (case-fold-search t))
3256     (while (string-match "%[0-9a-f][0-9a-f]" str)
3257       (let* ((start (match-beginning 0))
3258              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3259              (code (+ (* 16 ch1)
3260                       (gnus-url-unhex (elt str (+ start 2))))))
3261         (setq tmp (concat
3262                    tmp (substring str 0 start)
3263                    (cond
3264                     (allow-newlines
3265                      (char-to-string code))
3266                     ((or (= code ?\n) (= code ?\r))
3267                      " ")
3268                     (t (char-to-string code))))
3269               str (substring str (match-end 0)))))
3270     (setq tmp (concat tmp str))
3271     tmp))
3272
3273 (defun gnus-url-mailto (url)
3274   ;; Send mail to someone
3275   (when (string-match "mailto:/*\\(.*\\)" url)
3276     (setq url (substring url (match-beginning 1) nil)))
3277   (let (to args subject func)
3278     (if (string-match (regexp-quote "?") url)
3279         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3280               args (gnus-url-parse-query-string
3281                     (substring url (match-end 0) nil) t))
3282       (setq to (gnus-url-unhex-string url)))
3283     (setq args (cons (list "to" to) args)
3284           subject (cdr-safe (assoc "subject" args)))
3285     (gnus-setup-message 'reply
3286       (message-mail)
3287       (while args
3288         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3289         (if (fboundp func)
3290             (funcall func)
3291           (message-position-on-field (caar args)))
3292         (insert (mapconcat 'identity (cdar args) ", "))
3293         (setq args (cdr args)))
3294       (if subject
3295           (message-goto-body)
3296         (message-goto-subject)))))
3297
3298 (defun gnus-button-mailto (address)
3299   ;; Mail to ADDRESS.
3300   (set-buffer (gnus-copy-article-buffer))
3301   (gnus-setup-message 'reply
3302     (message-reply address)))
3303
3304 (defun gnus-button-reply (address)
3305   ;; Reply to ADDRESS.
3306   (gnus-setup-message 'reply
3307     (message-reply address)))
3308
3309 (defun gnus-button-url (address)
3310   "Browse ADDRESS."
3311   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3312   (if (listp browse-url-browser-function)
3313       (browse-url address)
3314     (funcall browse-url-browser-function address)))
3315
3316 (defun gnus-button-embedded-url (address)
3317   "Browse ADDRESS."
3318   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3319   (if (listp browse-url-browser-function)
3320       (browse-url (gnus-strip-whitespace address))
3321     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
3322
3323 ;;; Next/prev buttons in the article buffer.
3324
3325 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3326 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3327
3328 (defvar gnus-prev-page-map nil)
3329 (unless gnus-prev-page-map
3330   (setq gnus-prev-page-map (make-sparse-keymap))
3331   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3332   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3333
3334 (defun gnus-insert-prev-page-button ()
3335   (let ((buffer-read-only nil))
3336     (gnus-eval-format
3337      gnus-prev-page-line-format nil
3338      `(gnus-prev t local-map ,gnus-prev-page-map
3339                  gnus-callback gnus-article-button-prev-page
3340                  gnus-type annotation))))
3341
3342 (defvar gnus-next-page-map nil)
3343 (unless gnus-next-page-map
3344   (setq gnus-next-page-map (make-keymap))
3345   (suppress-keymap gnus-prev-page-map)
3346   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3347   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3348
3349 (defun gnus-button-next-page ()
3350   "Go to the next page."
3351   (interactive)
3352   (let ((win (selected-window)))
3353     (select-window (get-buffer-window gnus-article-buffer t))
3354     (gnus-article-next-page)
3355     (select-window win)))
3356
3357 (defun gnus-button-prev-page ()
3358   "Go to the prev page."
3359   (interactive)
3360   (let ((win (selected-window)))
3361     (select-window (get-buffer-window gnus-article-buffer t))
3362     (gnus-article-prev-page)
3363     (select-window win)))
3364
3365 (defun gnus-insert-next-page-button ()
3366   (let ((buffer-read-only nil))
3367     (gnus-eval-format gnus-next-page-line-format nil
3368                       `(gnus-next
3369                         t local-map ,gnus-next-page-map
3370                         gnus-callback gnus-article-button-next-page
3371                         gnus-type annotation))))
3372
3373 (defun gnus-article-button-next-page (arg)
3374   "Go to the next page."
3375   (interactive "P")
3376   (let ((win (selected-window)))
3377     (select-window (get-buffer-window gnus-article-buffer t))
3378     (gnus-article-next-page)
3379     (select-window win)))
3380
3381 (defun gnus-article-button-prev-page (arg)
3382   "Go to the prev page."
3383   (interactive "P")
3384   (let ((win (selected-window)))
3385     (select-window (get-buffer-window gnus-article-buffer t))
3386     (gnus-article-prev-page)
3387     (select-window win)))
3388
3389
3390 ;;; @ for mime-view
3391 ;;;
3392
3393 (defun gnus-article-header-presentation-method (entity situation)
3394   (mime-insert-header entity)
3395   )
3396
3397 (set-alist 'mime-header-presentation-method-alist
3398            'gnus-original-article-mode
3399            #'gnus-article-header-presentation-method)
3400
3401 (defun gnus-mime-preview-quitting-method ()
3402   (if gnus-show-mime
3403       (gnus-article-show-summary)
3404     (mime-preview-kill-buffer)
3405     (delete-other-windows)
3406     (gnus-article-show-summary)
3407     (gnus-summary-select-article nil t)
3408     ))
3409
3410 (set-alist 'mime-preview-quitting-method-alist
3411            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
3412
3413 (defun gnus-following-method (buf)
3414   (set-buffer buf)
3415   (message-followup)
3416   (message-yank-original)
3417   (kill-buffer buf)
3418   (goto-char (point-min))
3419   )
3420
3421 (set-alist 'mime-preview-following-method-alist
3422            'gnus-original-article-mode #'gnus-following-method)
3423
3424 (set-alist 'mime-preview-over-to-previous-method-alist
3425            'gnus-original-article-mode
3426            (lambda ()
3427              (gnus-article-read-summary-keys
3428               nil (gnus-character-to-event ?P))))
3429
3430 (set-alist 'mime-preview-over-to-next-method-alist
3431            'gnus-original-article-mode'
3432            (lambda ()
3433              (gnus-article-read-summary-keys
3434               nil (gnus-character-to-event ?N))))
3435
3436
3437 ;;; @ end
3438 ;;;
3439
3440 (gnus-ems-redefine)
3441
3442 (provide 'gnus-art)
3443
3444 (run-hooks 'gnus-art-load-hook)
3445
3446 ;;; gnus-art.el ends here