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