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