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