Speedups; see ChangeLog for fine details.
[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 (memq (char-after) '(?\t ?\ ))
705               (forward-line 1))
706             (while (not (eobp))
707               ;; Mark the rank of the header.
708               (put-text-property
709                (point) (1+ (point)) 'message-rank
710                (if (or (and visible (looking-at visible))
711                        (and ignored
712                             (not (looking-at ignored))))
713                    (gnus-article-header-rank)
714                  (+ 2 max)))
715               (forward-line 1)
716               (while (memq (char-after) '(?\t ?\ ))
717                 (forward-line 1)))
718             (message-sort-headers-1)
719             (when (setq beg (text-property-any
720                              (point-min) (point-max) 'message-rank (+ 2 max)))
721               ;; We make the unwanted headers invisible.
722               (if delete
723                   (delete-region beg (point-max))
724                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
725                 (gnus-article-hide-text-type beg (point-max) 'headers))
726               ;; Work around XEmacs lossage.
727               (put-text-property (point-min) beg 'invisible nil))))))))
728
729 (defun article-hide-boring-headers (&optional arg)
730   "Toggle hiding of headers that aren't very interesting.
731 If given a negative prefix, always show; if given a positive prefix,
732 always hide."
733   (interactive (gnus-article-hidden-arg))
734   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
735              (not gnus-show-all-headers))
736     (save-excursion
737       (save-restriction
738         (let ((buffer-read-only nil)
739               (list gnus-boring-article-headers)
740               (inhibit-point-motion-hooks t)
741               elem)
742           (nnheader-narrow-to-headers)
743           (while list
744             (setq elem (pop list))
745             (goto-char (point-min))
746             (cond
747              ;; Hide empty headers.
748              ((eq elem 'empty)
749               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
750                 (forward-line -1)
751                 (gnus-article-hide-text-type
752                  (point)
753                  (progn
754                    (while (and (zerop (forward-line 1))
755                                (memq (char-after) '(?\t ?\ ))))
756                    (point))
757                  'boring-headers)))
758              ;; Hide boring Newsgroups header.
759              ((eq elem 'newsgroups)
760               (when (equal (gnus-fetch-field "newsgroups")
761                            (gnus-group-real-name
762                             (if (boundp 'gnus-newsgroup-name)
763                                 gnus-newsgroup-name
764                               "")))
765                 (gnus-article-hide-header "newsgroups")))
766              ((eq elem 'followup-to)
767               (when (equal (message-fetch-field "followup-to")
768                            (message-fetch-field "newsgroups"))
769                 (gnus-article-hide-header "followup-to")))
770              ((eq elem 'reply-to)
771               (let ((from (message-fetch-field "from"))
772                     (reply-to (message-fetch-field "reply-to")))
773                 (when (and
774                        from reply-to
775                        (ignore-errors
776                          (equal
777                           (nth 1 (funcall gnus-extract-address-components from))
778                           (nth 1 (funcall gnus-extract-address-components reply-to)))))
779                   (gnus-article-hide-header "reply-to"))))
780              ((eq elem 'date)
781               (let ((date (message-fetch-field "date")))
782                 (when (and date
783                            (< (gnus-days-between (current-time-string) date)
784                               4))
785                   (gnus-article-hide-header "date"))))
786              ((eq elem 'long-to)
787               (let ((to (message-fetch-field "to")))
788                 (when (> (length to) 1024)
789                   (gnus-article-hide-header "to"))))
790              ((eq elem 'many-to)
791               (let ((to-count 0))
792                 (goto-char (point-min))
793                 (while (re-search-forward "^to:" nil t)
794                   (setq to-count (1+ to-count)))
795                 (when (> to-count 1)
796                   (while (> to-count 0)
797                     (goto-char (point-min))
798                     (save-restriction
799                       (re-search-forward "^to:" nil nil to-count)
800                       (forward-line -1)
801                       (narrow-to-region (point) (point-max))
802                       (gnus-article-hide-header "to"))
803                     (setq to-count (1- to-count)))))))))))))
804
805 (defun gnus-article-hide-header (header)
806   (save-excursion
807     (goto-char (point-min))
808     (when (re-search-forward (concat "^" header ":") nil t)
809       (gnus-article-hide-text-type
810        (progn (beginning-of-line) (point))
811        (progn
812          (while (and (zerop (forward-line 1))
813                      (memq (char-after) '(?\t ?\ ))))
814          (point))
815        'boring-headers))))
816
817 (defun article-treat-dumbquotes ()
818   "Translate M******** sm*rtq**t*s into proper text."
819   (interactive)
820   (article-translate-characters "\221\222\223\223" "`'\"\""))
821
822 (defun article-translate-characters (from to)
823   "Translate all characters in the body of the article according to FROM and TO.
824 FROM is a string of characters to translate from; to is a string of
825 characters to translate to."
826   (save-excursion
827     (goto-char (point-min))
828     (when (search-forward "\n\n" nil t)
829       (let ((buffer-read-only nil)
830             (x (make-string 225 ?x))
831             (i -1))
832         (while (< (incf i) (length x))
833           (aset x i i))
834         (setq i 0)
835         (while (< i (length from))
836           (aset x (aref from i) (aref to i))
837           (incf i))
838         (translate-region (point) (point-max) x)))))
839
840 (defun article-treat-overstrike ()
841   "Translate overstrikes into bold text."
842   (interactive)
843   (save-excursion
844     (goto-char (point-min))
845     (when (search-forward "\n\n" nil t)
846       (let ((buffer-read-only nil))
847         (while (search-forward "\b" nil t)
848           (let ((next (char-after))
849                 (previous (char-after (- (point) 2))))
850             ;; We do the boldification/underlining by hiding the
851             ;; overstrikes and putting the proper text property
852             ;; on the letters.
853             (cond
854              ((eq next previous)
855               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
856               (put-text-property (point) (1+ (point)) 'face 'bold))
857              ((eq next ?_)
858               (gnus-article-hide-text-type
859                (1- (point)) (1+ (point)) 'overstrike)
860               (put-text-property
861                (- (point) 2) (1- (point)) 'face 'underline))
862              ((eq previous ?_)
863               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
864               (put-text-property
865                (point) (1+ (point)) 'face 'underline)))))))))
866
867 (defun article-fill ()
868   "Format too long lines."
869   (interactive)
870   (save-excursion
871     (let ((buffer-read-only nil))
872       (widen)
873       (goto-char (point-min))
874       (search-forward "\n\n" nil t)
875       (end-of-line 1)
876       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
877             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
878             (adaptive-fill-mode t))
879         (while (not (eobp))
880           (and (>= (current-column) (min fill-column (window-width)))
881                (not (eq (preceding-char) ?:))
882                (fill-paragraph nil))
883           (end-of-line 2))))))
884
885 (defun article-remove-cr ()
886   "Remove carriage returns from an article."
887   (interactive)
888   (save-excursion
889     (let ((buffer-read-only nil))
890       (goto-char (point-min))
891       (while (search-forward "\r" nil t)
892         (replace-match "" t t)))))
893
894 (defun article-remove-trailing-blank-lines ()
895   "Remove all trailing blank lines from the article."
896   (interactive)
897   (save-excursion
898     (let ((buffer-read-only nil))
899       (goto-char (point-max))
900       (delete-region
901        (point)
902        (progn
903          (while (and (not (bobp))
904                      (looking-at "^[ \t]*$"))
905            (forward-line -1))
906          (forward-line 1)
907          (point))))))
908
909 (defun article-display-x-face (&optional force)
910   "Look for an X-Face header and display it if present."
911   (interactive (list 'force))
912   (save-excursion
913     ;; Delete the old process, if any.
914     (when (process-status "article-x-face")
915       (delete-process "article-x-face"))
916     (let ((inhibit-point-motion-hooks t)
917           (case-fold-search t)
918           from last)
919       (save-restriction
920         (nnheader-narrow-to-headers)
921         (setq from (message-fetch-field "from"))
922         (goto-char (point-min))
923         (while (and gnus-article-x-face-command
924                     (not last)
925                     (or force
926                         ;; Check whether this face is censored.
927                         (not gnus-article-x-face-too-ugly)
928                         (and gnus-article-x-face-too-ugly from
929                              (not (string-match gnus-article-x-face-too-ugly
930                                                 from))))
931                     ;; Has to be present.
932                     (re-search-forward "^X-Face: " nil t))
933           ;; This used to try to do multiple faces (`while' instead of
934           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
935           ;; work (b) it can crash some versions of Emacs (c) are
936           ;; multiple faces really something to encourage?
937           (when (stringp gnus-article-x-face-command)
938             (setq last t))
939           ;; We now have the area of the buffer where the X-Face is stored.
940           (save-excursion
941             (let ((beg (point))
942                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
943               ;; We display the face.
944               (if (symbolp gnus-article-x-face-command)
945                   ;; The command is a lisp function, so we call it.
946                   (if (gnus-functionp gnus-article-x-face-command)
947                       (funcall gnus-article-x-face-command beg end)
948                     (error "%s is not a function" gnus-article-x-face-command))
949                 ;; The command is a string, so we interpret the command
950                 ;; as a, well, command, and fork it off.
951                 (let ((process-connection-type nil))
952                   (process-kill-without-query
953                    (start-process
954                     "article-x-face" nil shell-file-name shell-command-switch
955                     gnus-article-x-face-command))
956                   (process-send-region "article-x-face" beg end)
957                   (process-send-eof "article-x-face"))))))))))
958
959 (defun gnus-article-decode-rfc1522 ()
960   "Decode MIME encoded-words in header fields."
961   (let (buffer-read-only)
962     (let ((charset (save-excursion
963                      (set-buffer gnus-summary-buffer)
964                      default-mime-charset)))
965       (mime-decode-header-in-buffer charset)
966       )))
967
968 (defun article-hide-pgp (&optional arg)
969   "Toggle hiding of any PGP headers and signatures in the current article.
970 If given a negative prefix, always show; if given a positive prefix,
971 always hide."
972   (interactive (gnus-article-hidden-arg))
973   (unless (gnus-article-check-hidden-text 'pgp arg)
974     (save-excursion
975       (let ((inhibit-point-motion-hooks t)
976             buffer-read-only beg end)
977         (widen)
978         (goto-char (point-min))
979         ;; Hide the "header".
980         (when (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
981           (delete-region (1+ (match-beginning 0)) (match-end 0))
982           (setq beg (point))
983           ;; Hide the actual signature.
984           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
985                (setq end (1+ (match-beginning 0)))
986                (delete-region
987                 end
988                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
989                     (match-end 0)
990                   ;; Perhaps we shouldn't hide to the end of the buffer
991                   ;; if there is no end to the signature?
992                   (point-max))))
993           ;; Hide "- " PGP quotation markers.
994           (when (and beg end)
995             (narrow-to-region beg end)
996             (goto-char (point-min))
997             (while (re-search-forward "^- " nil t)
998               (delete-region
999                (match-beginning 0) (match-end 0)))
1000             (widen))
1001           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1002
1003 (defun article-hide-pem (&optional arg)
1004   "Toggle hiding of any PEM headers and signatures in the current article.
1005 If given a negative prefix, always show; if given a positive prefix,
1006 always hide."
1007   (interactive (gnus-article-hidden-arg))
1008   (unless (gnus-article-check-hidden-text 'pem arg)
1009     (save-excursion
1010       (let (buffer-read-only end)
1011         (widen)
1012         (goto-char (point-min))
1013         ;; hide the horrendously ugly "header".
1014         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1015                              nil
1016                              t)
1017              (setq end (1+ (match-beginning 0)))
1018              (gnus-article-hide-text-type
1019               end
1020               (if (search-forward "\n\n" nil t)
1021                   (match-end 0)
1022                 (point-max))
1023               'pem))
1024         ;; hide the trailer as well
1025         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1026                              nil
1027                              t)
1028              (gnus-article-hide-text-type
1029               (match-beginning 0) (match-end 0) 'pem))))))
1030
1031 (defun article-hide-signature (&optional arg)
1032   "Hide the signature in the current article.
1033 If given a negative prefix, always show; if given a positive prefix,
1034 always hide."
1035   (interactive (gnus-article-hidden-arg))
1036   (unless (gnus-article-check-hidden-text 'signature arg)
1037     (save-excursion
1038       (save-restriction
1039         (let ((buffer-read-only nil))
1040           (when (gnus-article-narrow-to-signature)
1041             (gnus-article-hide-text-type
1042              (point-min) (point-max) 'signature)))))))
1043
1044 (defun article-strip-leading-blank-lines ()
1045   "Remove all blank lines from the beginning of the article."
1046   (interactive)
1047   (save-excursion
1048     (let ((inhibit-point-motion-hooks t)
1049           buffer-read-only)
1050       (goto-char (point-min))
1051       (when (search-forward "\n\n" nil t)
1052         (while (and (not (eobp))
1053                     (looking-at "[ \t]*$"))
1054           (gnus-delete-line))))))
1055
1056 (defun article-strip-multiple-blank-lines ()
1057   "Replace consecutive blank lines with one empty line."
1058   (interactive)
1059   (save-excursion
1060     (let ((inhibit-point-motion-hooks t)
1061           buffer-read-only)
1062       ;; First make all blank lines empty.
1063       (goto-char (point-min))
1064       (search-forward "\n\n" nil t)
1065       (while (re-search-forward "^[ \t]+$" nil t)
1066         (replace-match "" nil t))
1067       ;; Then replace multiple empty lines with a single empty line.
1068       (goto-char (point-min))
1069       (search-forward "\n\n" nil t)
1070       (while (re-search-forward "\n\n\n+" nil t)
1071         (replace-match "\n\n" t t)))))
1072
1073 (defun article-strip-leading-space ()
1074   "Remove all white space from the beginning of the lines in the article."
1075   (interactive)
1076   (save-excursion
1077     (let ((inhibit-point-motion-hooks t)
1078           buffer-read-only)
1079       (goto-char (point-min))
1080       (search-forward "\n\n" nil t)
1081       (while (re-search-forward "^[ \t]+" nil t)
1082         (replace-match "" t t)))))
1083
1084 (defun article-strip-blank-lines ()
1085   "Strip leading, trailing and multiple blank lines."
1086   (interactive)
1087   (article-strip-leading-blank-lines)
1088   (article-remove-trailing-blank-lines)
1089   (article-strip-multiple-blank-lines))
1090
1091 (defun article-strip-all-blank-lines ()
1092   "Strip all blank lines."
1093   (interactive)
1094   (save-excursion
1095     (let ((inhibit-point-motion-hooks t)
1096           buffer-read-only)
1097       (goto-char (point-min))
1098       (search-forward "\n\n" nil t)
1099       (while (re-search-forward "^[ \t]*\n" nil t)
1100         (replace-match "" t t)))))
1101
1102 (defun gnus-article-narrow-to-signature ()
1103   "Narrow to the signature; return t if a signature is found, else nil."
1104   (widen)
1105   (let ((inhibit-point-motion-hooks t))
1106     (when (gnus-article-search-signature)
1107       (forward-line 1)
1108       ;; Check whether we have some limits to what we consider
1109       ;; to be a signature.
1110       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1111                       (list gnus-signature-limit)))
1112             limit limited)
1113         (while (setq limit (pop limits))
1114           (if (or (and (integerp limit)
1115                        (< (- (point-max) (point)) limit))
1116                   (and (floatp limit)
1117                        (< (count-lines (point) (point-max)) limit))
1118                   (and (gnus-functionp limit)
1119                        (funcall limit))
1120                   (and (stringp limit)
1121                        (not (re-search-forward limit nil t))))
1122               ()                        ; This limit did not succeed.
1123             (setq limited t
1124                   limits nil)))
1125         (unless limited
1126           (narrow-to-region (point) (point-max))
1127           t)))))
1128
1129 (defun gnus-article-search-signature ()
1130   "Search the current buffer for the signature separator.
1131 Put point at the beginning of the signature separator."
1132   (let ((cur (point)))
1133     (goto-char (point-max))
1134     (if (if (stringp gnus-signature-separator)
1135             (re-search-backward gnus-signature-separator nil t)
1136           (let ((seps gnus-signature-separator))
1137             (while (and seps
1138                         (not (re-search-backward (car seps) nil t)))
1139               (pop seps))
1140             seps))
1141         t
1142       (goto-char cur)
1143       nil)))
1144
1145 (eval-and-compile
1146   (autoload 'w3-display "w3-parse")
1147   (autoload 'w3-do-setup "w3" "" t)
1148   (autoload 'w3-region "w3-display" "" t))
1149
1150 (defun gnus-article-treat-html ()
1151   "Render HTML."
1152   (interactive)
1153   (let ((cbuf (current-buffer)))
1154     (set-buffer gnus-article-buffer)
1155     (let (buf buffer-read-only b e)
1156       (w3-do-setup)
1157       (goto-char (point-min))
1158       (narrow-to-region
1159        (if (search-forward "\n\n" nil t)
1160            (setq b (point))
1161          (point-max))
1162        (setq e (point-max)))
1163       (nnheader-temp-write nil
1164         (insert-buffer-substring gnus-article-buffer b e)
1165         (require 'url)
1166         (save-window-excursion
1167           (w3-region (point-min) (point-max))
1168           (setq buf (buffer-substring-no-properties (point-min) (point-max)))))
1169       (when buf
1170         (delete-region (point-min) (point-max))
1171         (insert buf))
1172       (widen)
1173       (goto-char (point-min))
1174       (set-window-start (get-buffer-window (current-buffer)) (point-min))
1175       (set-buffer cbuf))))
1176
1177 (defun gnus-article-hidden-arg ()
1178   "Return the current prefix arg as a number, or 0 if no prefix."
1179   (list (if current-prefix-arg
1180             (prefix-numeric-value current-prefix-arg)
1181           0)))
1182
1183 (defun gnus-article-check-hidden-text (type arg)
1184   "Return nil if hiding is necessary.
1185 Arg can be nil or a number.  Nil and positive means hide, negative
1186 means show, 0 means toggle."
1187   (save-excursion
1188     (save-restriction
1189       (widen)
1190       (let ((hide (gnus-article-hidden-text-p type)))
1191         (cond
1192          ((or (null arg)
1193               (> arg 0))
1194           nil)
1195          ((< arg 0)
1196           (gnus-article-show-hidden-text type))
1197          (t
1198           (if (eq hide 'hidden)
1199               (gnus-article-show-hidden-text type)
1200             nil)))))))
1201
1202 (defun gnus-article-hidden-text-p (type)
1203   "Say whether the current buffer contains hidden text of type TYPE."
1204   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1205     (while (and pos
1206                 (not (get-text-property pos 'invisible)))
1207       (setq pos
1208             (text-property-any (1+ pos) (point-max) 'article-type type)))
1209     (if pos
1210         'hidden
1211       'shown)))
1212
1213 (defun gnus-article-show-hidden-text (type &optional hide)
1214   "Show all hidden text of type TYPE.
1215 If HIDE, hide the text instead."
1216   (save-excursion
1217     (let ((buffer-read-only nil)
1218           (inhibit-point-motion-hooks t)
1219           (end (point-min))
1220           beg)
1221       (while (setq beg (text-property-any end (point-max) 'article-type type))
1222         (goto-char beg)
1223         (setq end (or
1224                    (text-property-not-all beg (point-max) 'article-type type)
1225                    (point-max)))
1226         (if hide
1227             (gnus-article-hide-text beg end gnus-hidden-properties)
1228           (gnus-article-unhide-text beg end))
1229         (goto-char end))
1230       t)))
1231
1232 (defconst article-time-units
1233   `((year . ,(* 365.25 24 60 60))
1234     (week . ,(* 7 24 60 60))
1235     (day . ,(* 24 60 60))
1236     (hour . ,(* 60 60))
1237     (minute . 60)
1238     (second . 1))
1239   "Mapping from time units to seconds.")
1240
1241 (defun article-date-ut (&optional type highlight header)
1242   "Convert DATE date to universal time in the current article.
1243 If TYPE is `local', convert to local time; if it is `lapsed', output
1244 how much time has lapsed since DATE."
1245   (interactive (list 'ut t))
1246   (let* ((header (or header
1247                      (mail-header-date gnus-current-headers)
1248                      (message-fetch-field "date")
1249                      ""))
1250          (date (if (vectorp header) (mail-header-date header)
1251                  header))
1252          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1253          (inhibit-point-motion-hooks t)
1254          bface eface newline)
1255     (when (and date (not (string= date "")))
1256       (save-excursion
1257         (save-restriction
1258           (nnheader-narrow-to-headers)
1259           (let ((buffer-read-only nil))
1260             ;; Delete any old Date headers.
1261             (if (re-search-forward date-regexp nil t)
1262                 (progn
1263                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1264                         eface (get-text-property (1- (gnus-point-at-eol))
1265                                                  'face))
1266                   (delete-region (progn (beginning-of-line) (point))
1267                                  (progn (end-of-line) (point)))
1268                   (beginning-of-line))
1269               (goto-char (point-max))
1270               (setq newline t))
1271             (insert (article-make-date-line date type))
1272             ;; Do highlighting.
1273             (beginning-of-line)
1274             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1275               (put-text-property (match-beginning 1) (1+ (match-end 1))
1276                                  'face bface)
1277               (put-text-property (match-beginning 2) (match-end 2)
1278                                  'face eface))
1279             (when newline
1280               (end-of-line)
1281               (insert "\n"))))))))
1282
1283 (defun article-make-date-line (date type)
1284   "Return a DATE line of TYPE."
1285   (cond
1286    ;; Convert to the local timezone.  We have to slap a
1287    ;; `condition-case' round the calls to the timezone
1288    ;; functions since they aren't particularly resistant to
1289    ;; buggy dates.
1290    ((eq type 'local)
1291     (concat "Date: " (condition-case ()
1292                          (timezone-make-date-arpa-standard date)
1293                        (error date))))
1294    ;; Convert to Universal Time.
1295    ((eq type 'ut)
1296     (concat "Date: "
1297             (condition-case ()
1298                 (timezone-make-date-arpa-standard date nil "UT")
1299               (error date))))
1300    ;; Get the original date from the article.
1301    ((eq type 'original)
1302     (concat "Date: " date))
1303    ;; Let the user define the format.
1304    ((eq type 'user)
1305     (if (gnus-functionp gnus-article-time-format)
1306         (funcall
1307          gnus-article-time-format
1308          (ignore-errors
1309            (gnus-encode-date
1310             (timezone-make-date-arpa-standard
1311              date nil "UT"))))
1312       (concat
1313        "Date: "
1314        (format-time-string gnus-article-time-format
1315                            (ignore-errors
1316                              (gnus-encode-date
1317                               (timezone-make-date-arpa-standard
1318                                date nil "UT")))))))
1319    ;; ISO 8601.
1320    ((eq type 'iso8601)
1321     (concat
1322      "Date: "
1323      (format-time-string "%Y%M%DT%h%m%s"
1324                          (ignore-errors
1325                            (gnus-encode-date
1326                             (timezone-make-date-arpa-standard
1327                              date nil "UT"))))))
1328    ;; Do an X-Sent lapsed format.
1329    ((eq type 'lapsed)
1330     ;; If the date is seriously mangled, the timezone functions are
1331     ;; liable to bug out, so we ignore all errors.
1332     (let* ((now (current-time))
1333            (real-time
1334             (ignore-errors
1335               (gnus-time-minus
1336                (gnus-encode-date
1337                 (timezone-make-date-arpa-standard
1338                  (current-time-string now)
1339                  (current-time-zone now) "UT"))
1340                (gnus-encode-date
1341                 (timezone-make-date-arpa-standard
1342                  date nil "UT")))))
1343            (real-sec (and real-time
1344                           (+ (* (float (car real-time)) 65536)
1345                              (cadr real-time))))
1346            (sec (and real-time (abs real-sec)))
1347            num prev)
1348       (cond
1349        ((null real-time)
1350         "X-Sent: Unknown")
1351        ((zerop sec)
1352         "X-Sent: Now")
1353        (t
1354         (concat
1355          "X-Sent: "
1356          ;; This is a bit convoluted, but basically we go
1357          ;; through the time units for years, weeks, etc,
1358          ;; and divide things to see whether that results
1359          ;; in positive answers.
1360          (mapconcat
1361           (lambda (unit)
1362             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1363                 ;; The (remaining) seconds are too few to
1364                 ;; be divided into this time unit.
1365                 ""
1366               ;; It's big enough, so we output it.
1367               (setq sec (- sec (* num (cdr unit))))
1368               (prog1
1369                   (concat (if prev ", " "") (int-to-string
1370                                              (floor num))
1371                           " " (symbol-name (car unit))
1372                           (if (> num 1) "s" ""))
1373                 (setq prev t))))
1374           article-time-units "")
1375          ;; If dates are odd, then it might appear like the
1376          ;; article was sent in the future.
1377          (if (> real-sec 0)
1378              " ago"
1379            " in the future"))))))
1380    (t
1381     (error "Unknown conversion type: %s" type))))
1382
1383 (defun article-date-local (&optional highlight)
1384   "Convert the current article date to the local timezone."
1385   (interactive (list t))
1386   (article-date-ut 'local highlight))
1387
1388 (defun article-date-original (&optional highlight)
1389   "Convert the current article date to what it was originally.
1390 This is only useful if you have used some other date conversion
1391 function and want to see what the date was before converting."
1392   (interactive (list t))
1393   (article-date-ut 'original highlight))
1394
1395 (defun article-date-lapsed (&optional highlight)
1396   "Convert the current article date to time lapsed since it was sent."
1397   (interactive (list t))
1398   (article-date-ut 'lapsed highlight))
1399
1400 (defun article-update-date-lapsed ()
1401   "Function to be run from a timer to update the lapsed time line."
1402   (let (deactivate-mark)
1403     (save-excursion
1404       (ignore-errors
1405         (when (gnus-buffer-live-p gnus-article-buffer)
1406           (set-buffer gnus-article-buffer)
1407           (goto-char (point-min))
1408           (when (re-search-forward "^X-Sent:" nil t)
1409             (article-date-lapsed t)))))))
1410
1411 (defun gnus-start-date-timer (&optional n)
1412   "Start a timer to update the X-Sent header in the article buffers.
1413 The numerical prefix says how frequently (in seconds) the function
1414 is to run."
1415   (interactive "p")
1416   (unless n
1417     (setq n 1))
1418   (gnus-stop-date-timer)
1419   (setq article-lapsed-timer
1420         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1421
1422 (defun gnus-stop-date-timer ()
1423   "Stop the X-Sent timer."
1424   (interactive)
1425   (when article-lapsed-timer
1426     (nnheader-cancel-timer article-lapsed-timer)
1427     (setq article-lapsed-timer nil)))
1428
1429 (defun article-date-user (&optional highlight)
1430   "Convert the current article date to the user-defined format.
1431 This format is defined by the `gnus-article-time-format' variable."
1432   (interactive (list t))
1433   (article-date-ut 'user highlight))
1434
1435 (defun article-date-iso8601 (&optional highlight)
1436   "Convert the current article date to ISO8601."
1437   (interactive (list t))
1438   (article-date-ut 'iso8601 highlight))
1439
1440 (defun article-show-all ()
1441   "Show all hidden text in the article buffer."
1442   (interactive)
1443   (save-excursion
1444     (let ((buffer-read-only nil))
1445       (gnus-article-unhide-text (point-min) (point-max)))))
1446
1447 (defun article-emphasize (&optional arg)
1448   "Emphasize text according to `gnus-emphasis-alist'."
1449   (interactive (gnus-article-hidden-arg))
1450   (unless (gnus-article-check-hidden-text 'emphasis arg)
1451     (save-excursion
1452       (let ((alist gnus-emphasis-alist)
1453             (buffer-read-only nil)
1454             (props (append '(article-type emphasis)
1455                            gnus-hidden-properties))
1456             regexp elem beg invisible visible face)
1457         (goto-char (point-min))
1458         (search-forward "\n\n" nil t)
1459         (setq beg (point))
1460         (while (setq elem (pop alist))
1461           (goto-char beg)
1462           (setq regexp (car elem)
1463                 invisible (nth 1 elem)
1464                 visible (nth 2 elem)
1465                 face (nth 3 elem))
1466           (while (re-search-forward regexp nil t)
1467             (when (and (match-beginning visible) (match-beginning invisible))
1468               (gnus-article-hide-text
1469                (match-beginning invisible) (match-end invisible) props)
1470               (gnus-article-unhide-text-type
1471                (match-beginning visible) (match-end visible) 'emphasis)
1472               (gnus-put-text-property-excluding-newlines
1473                (match-beginning visible) (match-end visible) 'face face)
1474               (goto-char (match-end invisible)))))))))
1475
1476 (defvar gnus-summary-article-menu)
1477 (defvar gnus-summary-post-menu)
1478
1479 ;;; Saving functions.
1480
1481 (defun gnus-article-save (save-buffer file &optional num)
1482   "Save the currently selected article."
1483   (unless gnus-save-all-headers
1484     ;; Remove headers according to `gnus-saved-headers'.
1485     (let ((gnus-visible-headers
1486            (or gnus-saved-headers gnus-visible-headers))
1487           (gnus-article-buffer save-buffer))
1488       (save-excursion
1489         (set-buffer save-buffer)
1490         (article-hide-headers 1 t))))
1491   (save-window-excursion
1492     (if (not gnus-default-article-saver)
1493         (error "No default saver is defined")
1494       ;; !!! Magic!  The saving functions all save
1495       ;; `gnus-original-article-buffer' (or so they think), but we
1496       ;; bind that variable to our save-buffer.
1497       (set-buffer gnus-article-buffer)
1498       (let* ((gnus-save-article-buffer save-buffer)
1499              (filename
1500               (cond
1501                ((not gnus-prompt-before-saving) 'default)
1502                ((eq gnus-prompt-before-saving 'always) nil)
1503                (t file)))
1504              (gnus-number-of-articles-to-be-saved
1505               (when (eq gnus-prompt-before-saving t)
1506                 num)))                  ; Magic
1507         (set-buffer gnus-article-current-summary)
1508         (funcall gnus-default-article-saver filename)))))
1509
1510 (defun gnus-read-save-file-name (prompt &optional filename
1511                                         function group headers variable)
1512   (let ((default-name
1513           (funcall function group headers (symbol-value variable)))
1514         result)
1515     (setq
1516      result
1517      (cond
1518       ((eq filename 'default)
1519        default-name)
1520       ((eq filename t)
1521        default-name)
1522       (filename filename)
1523       (t
1524        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1525               (prompt
1526                (format prompt
1527                        (if (and gnus-number-of-articles-to-be-saved
1528                                 (> gnus-number-of-articles-to-be-saved 1))
1529                            (format "these %d articles"
1530                                    gnus-number-of-articles-to-be-saved)
1531                          "this article")))
1532               (file
1533                ;; Let the split methods have their say.
1534                (cond
1535                 ;; No split name was found.
1536                 ((null split-name)
1537                  (read-file-name
1538                   (concat prompt " (default "
1539                           (file-name-nondirectory default-name) ") ")
1540                   (file-name-directory default-name)
1541                   default-name))
1542                 ;; A single group name is returned.
1543                 ((stringp split-name)
1544                  (setq default-name
1545                        (funcall function split-name headers
1546                                 (symbol-value variable)))
1547                  (read-file-name
1548                   (concat prompt " (default "
1549                           (file-name-nondirectory default-name) ") ")
1550                   (file-name-directory default-name)
1551                   default-name))
1552                 ;; A single split name was found
1553                 ((= 1 (length split-name))
1554                  (let* ((name (expand-file-name
1555                                (car split-name) gnus-article-save-directory))
1556                         (dir (cond ((file-directory-p name)
1557                                     (file-name-as-directory name))
1558                                    ((file-exists-p name) name)
1559                                    (t gnus-article-save-directory))))
1560                    (read-file-name
1561                     (concat prompt " (default " name ") ")
1562                     dir name)))
1563                 ;; A list of splits was found.
1564                 (t
1565                  (setq split-name (nreverse split-name))
1566                  (let (result)
1567                    (let ((file-name-history
1568                           (nconc split-name file-name-history)))
1569                      (setq result
1570                            (expand-file-name
1571                             (read-file-name
1572                              (concat prompt " (`M-p' for defaults) ")
1573                              gnus-article-save-directory
1574                              (car split-name))
1575                             gnus-article-save-directory)))
1576                    (car (push result file-name-history)))))))
1577          ;; Create the directory.
1578          (gnus-make-directory (file-name-directory file))
1579          ;; If we have read a directory, we append the default file name.
1580          (when (file-directory-p file)
1581            (setq file (concat (file-name-as-directory file)
1582                               (file-name-nondirectory default-name))))
1583          ;; Possibly translate some characters.
1584          (nnheader-translate-file-chars file)))))
1585     (gnus-make-directory (file-name-directory result))
1586     (set variable result)))
1587
1588 (defun gnus-article-archive-name (group)
1589   "Return the first instance of an \"Archive-name\" in the current buffer."
1590   (let ((case-fold-search t))
1591     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1592       (nnheader-concat gnus-article-save-directory
1593                        (match-string 1)))))
1594
1595 (defun gnus-article-nndoc-name (group)
1596   "If GROUP is an nndoc group, return the name of the parent group."
1597   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1598     (gnus-group-get-parameter group 'save-article-group)))
1599
1600 (defun gnus-summary-save-in-rmail (&optional filename)
1601   "Append this article to Rmail file.
1602 Optional argument FILENAME specifies file name.
1603 Directory to save to is default to `gnus-article-save-directory'."
1604   (setq filename (gnus-read-save-file-name
1605                   "Save %s in rmail file:" filename
1606                   gnus-rmail-save-name gnus-newsgroup-name
1607                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1608   (gnus-eval-in-buffer-window gnus-save-article-buffer
1609     (save-excursion
1610       (save-restriction
1611         (widen)
1612         (gnus-output-to-rmail filename))))
1613   filename)
1614
1615 (defun gnus-summary-save-in-mail (&optional filename)
1616   "Append this article to Unix mail file.
1617 Optional argument FILENAME specifies file name.
1618 Directory to save to is default to `gnus-article-save-directory'."
1619   (setq filename (gnus-read-save-file-name
1620                   "Save %s in Unix mail file:" filename
1621                   gnus-mail-save-name gnus-newsgroup-name
1622                   gnus-current-headers 'gnus-newsgroup-last-mail))
1623   (gnus-eval-in-buffer-window gnus-save-article-buffer
1624     (save-excursion
1625       (save-restriction
1626         (widen)
1627         (if (and (file-readable-p filename)
1628                  (mail-file-babyl-p filename))
1629             (gnus-output-to-rmail filename t)
1630           (gnus-output-to-mail filename)))))
1631   filename)
1632
1633 (defun gnus-summary-save-in-file (&optional filename overwrite)
1634   "Append this article to file.
1635 Optional argument FILENAME specifies file name.
1636 Directory to save to is default to `gnus-article-save-directory'."
1637   (setq filename (gnus-read-save-file-name
1638                   "Save %s in file:" filename
1639                   gnus-file-save-name gnus-newsgroup-name
1640                   gnus-current-headers 'gnus-newsgroup-last-file))
1641   (gnus-eval-in-buffer-window gnus-save-article-buffer
1642     (save-excursion
1643       (save-restriction
1644         (widen)
1645         (when (and overwrite
1646                    (file-exists-p filename))
1647           (delete-file filename))
1648         (gnus-output-to-file filename))))
1649   filename)
1650
1651 (defun gnus-summary-write-to-file (&optional filename)
1652   "Write this article to a file.
1653 Optional argument FILENAME specifies file name.
1654 The directory to save in defaults to `gnus-article-save-directory'."
1655   (gnus-summary-save-in-file nil t))
1656
1657 (defun gnus-summary-save-body-in-file (&optional filename)
1658   "Append this article body to a file.
1659 Optional argument FILENAME specifies file name.
1660 The directory to save in defaults to `gnus-article-save-directory'."
1661   (setq filename (gnus-read-save-file-name
1662                   "Save %s body in file:" filename
1663                   gnus-file-save-name gnus-newsgroup-name
1664                   gnus-current-headers 'gnus-newsgroup-last-file))
1665   (gnus-eval-in-buffer-window gnus-save-article-buffer
1666     (save-excursion
1667       (save-restriction
1668         (widen)
1669         (goto-char (point-min))
1670         (when (search-forward "\n\n" nil t)
1671           (narrow-to-region (point) (point-max)))
1672         (gnus-output-to-file filename))))
1673   filename)
1674
1675 (defun gnus-summary-save-in-pipe (&optional command)
1676   "Pipe this article to subprocess."
1677   (setq command
1678         (cond ((eq command 'default)
1679                gnus-last-shell-command)
1680               (command command)
1681               (t (read-string
1682                   (format
1683                    "Shell command on %s: "
1684                    (if (and gnus-number-of-articles-to-be-saved
1685                             (> gnus-number-of-articles-to-be-saved 1))
1686                        (format "these %d articles"
1687                                gnus-number-of-articles-to-be-saved)
1688                      "this article"))
1689                   gnus-last-shell-command))))
1690   (when (string-equal command "")
1691     (setq command gnus-last-shell-command))
1692   (gnus-eval-in-buffer-window gnus-article-buffer
1693     (save-restriction
1694       (widen)
1695       (shell-command-on-region (point-min) (point-max) command nil)))
1696   (setq gnus-last-shell-command command))
1697
1698 ;;; Article file names when saving.
1699
1700 (defun gnus-capitalize-newsgroup (newsgroup)
1701   "Capitalize NEWSGROUP name."
1702   (when (not (zerop (length newsgroup)))
1703     (concat (char-to-string (upcase (aref newsgroup 0)))
1704             (substring newsgroup 1))))
1705
1706 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1707   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1708 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1709 Otherwise, it is like ~/News/news/group/num."
1710   (let ((default
1711           (expand-file-name
1712            (concat (if (gnus-use-long-file-name 'not-save)
1713                        (gnus-capitalize-newsgroup newsgroup)
1714                      (gnus-newsgroup-directory-form newsgroup))
1715                    "/" (int-to-string (mail-header-number headers)))
1716            gnus-article-save-directory)))
1717     (if (and last-file
1718              (string-equal (file-name-directory default)
1719                            (file-name-directory last-file))
1720              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1721         default
1722       (or last-file default))))
1723
1724 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1725   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1726 If variable `gnus-use-long-file-name' is non-nil, it is
1727 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1728   (let ((default
1729           (expand-file-name
1730            (concat (if (gnus-use-long-file-name 'not-save)
1731                        newsgroup
1732                      (gnus-newsgroup-directory-form newsgroup))
1733                    "/" (int-to-string (mail-header-number headers)))
1734            gnus-article-save-directory)))
1735     (if (and last-file
1736              (string-equal (file-name-directory default)
1737                            (file-name-directory last-file))
1738              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1739         default
1740       (or last-file default))))
1741
1742 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1743   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1744 If variable `gnus-use-long-file-name' is non-nil, it is
1745 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1746   (or last-file
1747       (expand-file-name
1748        (if (gnus-use-long-file-name 'not-save)
1749            (gnus-capitalize-newsgroup newsgroup)
1750          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1751        gnus-article-save-directory)))
1752
1753 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1754   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1755 If variable `gnus-use-long-file-name' is non-nil, it is
1756 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1757   (or last-file
1758       (expand-file-name
1759        (if (gnus-use-long-file-name 'not-save)
1760            newsgroup
1761          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1762        gnus-article-save-directory)))
1763
1764 (eval-and-compile
1765   (mapcar
1766    (lambda (func)
1767      (let (afunc gfunc)
1768        (if (consp func)
1769            (setq afunc (car func)
1770                  gfunc (cdr func))
1771          (setq afunc func
1772                gfunc (intern (format "gnus-%s" func))))
1773        (fset gfunc
1774              (if (not (fboundp afunc))
1775                  nil
1776                `(lambda (&optional interactive &rest args)
1777                   ,(documentation afunc t)
1778                   (interactive (list t))
1779                   (save-excursion
1780                     (set-buffer gnus-article-buffer)
1781                     (if interactive
1782                         (call-interactively ',afunc)
1783                       (apply ',afunc args))))))))
1784    '(article-hide-headers
1785      article-hide-boring-headers
1786      article-treat-overstrike
1787      (article-fill . gnus-article-word-wrap)
1788      article-remove-cr
1789      article-display-x-face
1790      article-hide-pgp
1791      article-hide-pem
1792      article-hide-signature
1793      article-remove-trailing-blank-lines
1794      article-strip-leading-blank-lines
1795      article-strip-multiple-blank-lines
1796      article-strip-leading-space
1797      article-strip-blank-lines
1798      article-strip-all-blank-lines
1799      article-date-local
1800      article-date-iso8601
1801      article-date-original
1802      article-date-ut
1803      article-date-user
1804      article-date-lapsed
1805      article-emphasize
1806      article-treat-dumbquotes
1807      (article-show-all . gnus-article-show-all-headers))))
1808 \f
1809 ;;;
1810 ;;; Gnus article mode
1811 ;;;
1812
1813 (put 'gnus-article-mode 'mode-class 'special)
1814
1815 (gnus-define-keys gnus-article-mode-map
1816   " " gnus-article-goto-next-page
1817   "\177" gnus-article-goto-prev-page
1818   [delete] gnus-article-goto-prev-page
1819   "\C-c^" gnus-article-refer-article
1820   "h" gnus-article-show-summary
1821   "s" gnus-article-show-summary
1822   "\C-c\C-m" gnus-article-mail
1823   "?" gnus-article-describe-briefly
1824   gnus-mouse-2 gnus-article-push-button
1825   "\r" gnus-article-press-button
1826   "\t" gnus-article-next-button
1827   "\M-\t" gnus-article-prev-button
1828   "e" gnus-article-edit
1829   "<" beginning-of-buffer
1830   ">" end-of-buffer
1831   "\C-c\C-i" gnus-info-find-node
1832   "\C-c\C-b" gnus-bug
1833
1834   "\C-d" gnus-article-read-summary-keys
1835   "\M-*" gnus-article-read-summary-keys
1836   "\M-#" gnus-article-read-summary-keys
1837   "\M-^" gnus-article-read-summary-keys
1838   "\M-g" gnus-article-read-summary-keys)
1839
1840 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
1841 (mapcar
1842  (lambda (key)
1843    (unless (lookup-key gnus-article-mode-map key)
1844      (define-key gnus-article-mode-map key
1845        'gnus-article-read-summary-keys)))
1846  (delq nil
1847        (append
1848         (mapcar
1849          (lambda (elt)
1850            (let ((key (car elt)))
1851              (and (> (length key) 0)
1852                   (not (eq 'menu-bar (aref key 0)))
1853                   (symbolp (lookup-key gnus-summary-mode-map key))
1854                   key)))
1855          (accessible-keymaps gnus-summary-mode-map))
1856         (let ((c 127)
1857               keys)
1858           (while (>= c 32)
1859             (push (char-to-string c) keys)
1860             (decf c))
1861           keys))))
1862
1863 (defun gnus-article-make-menu-bar ()
1864   (gnus-turn-off-edit-menu 'article)
1865   (unless (boundp 'gnus-article-article-menu)
1866     (easy-menu-define
1867      gnus-article-article-menu gnus-article-mode-map ""
1868      '("Article"
1869        ["Scroll forwards" gnus-article-goto-next-page t]
1870        ["Scroll backwards" gnus-article-goto-prev-page t]
1871        ["Show summary" gnus-article-show-summary t]
1872        ["Fetch Message-ID at point" gnus-article-refer-article t]
1873        ["Mail to address at point" gnus-article-mail t]
1874        ["Send a bug report" gnus-bug t]))
1875
1876     (easy-menu-define
1877      gnus-article-treatment-menu gnus-article-mode-map ""
1878      '("Treatment"
1879        ["Hide headers" gnus-article-hide-headers t]
1880        ["Hide signature" gnus-article-hide-signature t]
1881        ["Hide citation" gnus-article-hide-citation t]
1882        ["Treat overstrike" gnus-article-treat-overstrike t]
1883        ["Remove carriage return" gnus-article-remove-cr t]
1884        ))
1885
1886     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
1887
1888     (when (boundp 'gnus-summary-post-menu)
1889       (define-key gnus-article-mode-map [menu-bar post]
1890         (cons "Post" gnus-summary-post-menu)))
1891
1892     (gnus-run-hooks 'gnus-article-menu-hook)))
1893
1894 (defun gnus-article-mode ()
1895   "Major mode for displaying an article.
1896
1897 All normal editing commands are switched off.
1898
1899 The following commands are available in addition to all summary mode
1900 commands:
1901 \\<gnus-article-mode-map>
1902 \\[gnus-article-next-page]\t Scroll the article one page forwards
1903 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1904 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1905 \\[gnus-article-show-summary]\t Display the summary buffer
1906 \\[gnus-article-mail]\t Send a reply to the address near point
1907 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1908 \\[gnus-info-find-node]\t Go to the Gnus info node"
1909   (interactive)
1910   (when (gnus-visual-p 'article-menu 'menu)
1911     (gnus-article-make-menu-bar))
1912   (gnus-simplify-mode-line)
1913   (setq mode-name "Article")
1914   (setq major-mode 'gnus-article-mode)
1915   (make-local-variable 'minor-mode-alist)
1916   (unless (assq 'gnus-show-mime minor-mode-alist)
1917     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1918   (use-local-map gnus-article-mode-map)
1919   (gnus-update-format-specifications nil 'article-mode)
1920   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1921   (make-local-variable 'gnus-page-broken)
1922   (make-local-variable 'gnus-button-marker-list)
1923   (make-local-variable 'gnus-article-current-summary)
1924   (gnus-set-default-directory)
1925   (buffer-disable-undo (current-buffer))
1926   (setq buffer-read-only t)
1927   (set-syntax-table gnus-article-mode-syntax-table)
1928   (gnus-run-hooks 'gnus-article-mode-hook))
1929
1930 (defun gnus-article-setup-buffer ()
1931   "Initialize the article buffer."
1932   (let* ((name (if gnus-single-article-buffer "*Article*"
1933                  (concat "*Article " gnus-newsgroup-name "*")))
1934          (original
1935           (progn (string-match "\\*Article" name)
1936                  (concat " *Original Article"
1937                          (substring name (match-end 0))))))
1938     (setq gnus-article-buffer name)
1939     (setq gnus-original-article-buffer original)
1940     ;; This might be a variable local to the summary buffer.
1941     (unless gnus-single-article-buffer
1942       (save-excursion
1943         (set-buffer gnus-summary-buffer)
1944         (setq gnus-article-buffer name)
1945         (setq gnus-original-article-buffer original)
1946         (gnus-set-global-variables)))
1947     ;; Init original article buffer.
1948     (save-excursion
1949       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
1950       (buffer-disable-undo (current-buffer))
1951       (setq major-mode 'gnus-original-article-mode)
1952       (make-local-variable 'gnus-original-article))
1953     (if (get-buffer name)
1954         (save-excursion
1955           (set-buffer name)
1956           (buffer-disable-undo (current-buffer))
1957           (setq buffer-read-only t)
1958           (unless (eq major-mode 'gnus-article-mode)
1959             (gnus-article-mode))
1960           (current-buffer))
1961       (save-excursion
1962         (set-buffer (gnus-get-buffer-create name))
1963         (gnus-article-mode)
1964         (make-local-variable 'gnus-summary-buffer)
1965         (current-buffer)))))
1966
1967 ;; Set article window start at LINE, where LINE is the number of lines
1968 ;; from the head of the article.
1969 (defun gnus-article-set-window-start (&optional line)
1970   (set-window-start
1971    (get-buffer-window gnus-article-buffer t)
1972    (save-excursion
1973      (set-buffer gnus-article-buffer)
1974      (goto-char (point-min))
1975      (if (not line)
1976          (point-min)
1977        (gnus-message 6 "Moved to bookmark")
1978        (search-forward "\n\n" nil t)
1979        (forward-line line)
1980        (point)))))
1981
1982 ;;; @@ article filters
1983 ;;;
1984
1985 (defun gnus-article-display-mime-message ()
1986   "Article display method for MIME message."
1987   ;; called from `gnus-original-article-buffer'.
1988   (let (charset all-headers)
1989     (with-current-buffer gnus-summary-buffer
1990       (setq charset default-mime-charset
1991             all-headers gnus-have-all-headers))
1992     (make-local-variable 'default-mime-charset)
1993     (setq default-mime-charset charset)
1994     (with-current-buffer (get-buffer-create gnus-article-buffer)
1995       (make-local-variable 'default-mime-charset)
1996       (setq default-mime-charset charset))
1997     (mime-display-message mime-message-structure
1998                           gnus-article-buffer nil gnus-article-mode-map)
1999     (when all-headers
2000       (gnus-article-hide-headers nil -1))
2001     )
2002   ;; `mime-display-message' changes current buffer to `gnus-article-buffer'.
2003   (make-local-variable 'mime-button-mother-dispatcher)
2004   (setq mime-button-mother-dispatcher
2005         (function gnus-article-push-button))
2006   (run-hooks 'gnus-mime-article-prepare-hook))
2007
2008 (defun gnus-article-display-traditional-message ()
2009   "Article display method for traditional message."
2010   (set-buffer gnus-article-buffer)
2011   (let (buffer-read-only)
2012     (erase-buffer)
2013     (insert-buffer-substring gnus-original-article-buffer)))
2014
2015 (defun gnus-article-make-full-mail-header (&optional number charset)
2016   "Create a new mail header structure in a raw article buffer."
2017   (unless (and number charset)
2018     (save-current-buffer
2019       (set-buffer gnus-summary-buffer)
2020       (unless number
2021         (setq number (or (cdr gnus-article-current) 0)))
2022       (unless charset
2023         (setq charset (or default-mime-charset 'x-ctext)))))
2024   (goto-char (point-min))
2025   (let ((header-end (if (search-forward "\n\n" nil t)
2026                         (1- (point))
2027                       (goto-char (point-max))))
2028         (chars (- (point-max) (point)))
2029         (lines (count-lines (point) (point-max)))
2030         (default-mime-charset charset)
2031         xref)
2032     (narrow-to-region (point-min) header-end)
2033     (setq xref (std11-fetch-field "xref"))
2034     (prog1
2035         (make-full-mail-header
2036          number
2037          (std11-fetch-field "subject")
2038          (std11-fetch-field "from")
2039          (std11-fetch-field "date")
2040          (std11-fetch-field "message-id")
2041          (std11-fetch-field "references")
2042          chars
2043          lines
2044          (when xref (concat "Xref: " xref)))
2045       (widen))))
2046
2047 (defun gnus-article-prepare (article &optional all-headers header)
2048   "Prepare ARTICLE in article mode buffer.
2049 ARTICLE should either be an article number or a Message-ID.
2050 If ARTICLE is an id, HEADER should be the article headers.
2051 If ALL-HEADERS is non-nil, no headers are hidden."
2052   (save-excursion
2053     ;; Make sure we start in a summary buffer.
2054     (unless (eq major-mode 'gnus-summary-mode)
2055       (set-buffer gnus-summary-buffer))
2056     (setq gnus-summary-buffer (current-buffer))
2057     (let* ((gnus-article (if header (mail-header-number header) article))
2058            (summary-buffer (current-buffer))
2059            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2060            (group gnus-newsgroup-name)
2061            result)
2062       (save-excursion
2063         (gnus-article-setup-buffer)
2064         (set-buffer gnus-original-article-buffer)
2065         ;; Deactivate active regions.
2066         (when (and (boundp 'transient-mark-mode)
2067                    transient-mark-mode)
2068           (setq mark-active nil))
2069         (if (not (setq result (let ((buffer-read-only nil))
2070                                 (gnus-request-article-this-buffer
2071                                  article group))))
2072             ;; There is no such article.
2073             (save-excursion
2074               (when (and (numberp article)
2075                          (not (memq article gnus-newsgroup-sparse)))
2076                 (setq gnus-article-current
2077                       (cons gnus-newsgroup-name article))
2078                 (set-buffer gnus-summary-buffer)
2079                 (setq gnus-current-article article)
2080                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2081                     (progn
2082                       (gnus-summary-set-agent-mark article)
2083                       (message "Message marked for downloading"))
2084                   (gnus-summary-mark-article article gnus-canceled-mark)
2085                   (unless (memq article gnus-newsgroup-sparse)
2086                     (gnus-error 1
2087                      "No such article (may have expired or been canceled)")))))
2088           (if (or (eq result 'pseudo)
2089                   (eq result 'nneething))
2090               (progn
2091                 (save-excursion
2092                   (set-buffer summary-buffer)
2093                   (push article gnus-newsgroup-history)
2094                   (setq gnus-last-article gnus-current-article
2095                         gnus-current-article 0
2096                         gnus-current-headers nil
2097                         gnus-article-current nil)
2098                   (if (eq result 'nneething)
2099                       (gnus-configure-windows 'summary)
2100                     (gnus-configure-windows 'article))
2101                   (gnus-set-global-variables))
2102                 (gnus-set-mode-line 'article))
2103             ;; The result from the `request' was an actual article -
2104             ;; or at least some text that is now displayed in the
2105             ;; article buffer.
2106             (when (and (numberp article)
2107                        (not (eq article gnus-current-article)))
2108               ;; Seems like a new article has been selected.
2109               ;; `gnus-current-article' must be an article number.
2110               (save-excursion
2111                 (set-buffer summary-buffer)
2112                 (push article gnus-newsgroup-history)
2113                 (setq gnus-last-article gnus-current-article
2114                       gnus-current-article article
2115                       gnus-current-headers
2116                       (gnus-summary-article-header gnus-current-article)
2117                       gnus-article-current
2118                       (cons gnus-newsgroup-name gnus-current-article))
2119                 (unless (vectorp gnus-current-headers)
2120                   (setq gnus-current-headers nil))
2121                 (gnus-summary-goto-subject gnus-current-article)
2122                 (when (gnus-summary-show-thread)
2123                   ;; If the summary buffer really was folded, the
2124                   ;; previous goto may not actually have gone to
2125                   ;; the right article, but the thread root instead.
2126                   ;; So we go again.
2127                   (gnus-summary-goto-subject gnus-current-article))
2128                 (gnus-run-hooks 'gnus-mark-article-hook)
2129                 (gnus-set-mode-line 'summary)
2130                 (when (gnus-visual-p 'article-highlight 'highlight)
2131                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2132                 ;; Set the global newsgroup variables here.
2133                 ;; Suggested by Jim Sisolak
2134                 ;; <sisolak@trans4.neep.wisc.edu>.
2135                 (gnus-set-global-variables)
2136                 (setq gnus-have-all-headers
2137                       (or all-headers gnus-show-all-headers))))
2138             (when (or (numberp article)
2139                       (stringp article))
2140               (gnus-article-prepare-display)
2141               ;; Do page break.
2142               (goto-char (point-min))
2143               (setq gnus-page-broken
2144                     (when gnus-break-pages
2145                       (gnus-narrow-to-page)
2146                       t)))
2147             (gnus-set-mode-line 'article)
2148             (gnus-configure-windows 'article)
2149             (goto-char (point-min))
2150             (search-forward "\n\n" nil t)
2151             (set-window-point (get-buffer-window (current-buffer)) (point))
2152             t))))))
2153
2154 (defun gnus-article-prepare-display ()
2155   "Make the current buffer look like a nice article."
2156   (let ((method
2157          (if gnus-show-mime
2158              (progn
2159                (setq mime-message-structure gnus-current-headers)
2160                gnus-article-display-method-for-mime)
2161            gnus-article-display-method-for-traditional)))
2162     (gnus-run-hooks 'gnus-tmp-internal-hook)
2163     (gnus-run-hooks 'gnus-article-prepare-hook)
2164     ;; Display message.
2165     (funcall method)
2166     ;; Associate this article with the current summary buffer.
2167     (setq gnus-article-current-summary gnus-summary-buffer)
2168     ;; Perform the article display hooks.
2169     (gnus-run-hooks 'gnus-article-display-hook)))
2170
2171 (defun gnus-article-wash-status ()
2172   "Return a string which display status of article washing."
2173   (save-excursion
2174     (set-buffer gnus-article-buffer)
2175     (let ((cite (gnus-article-hidden-text-p 'cite))
2176           (headers (gnus-article-hidden-text-p 'headers))
2177           (boring (gnus-article-hidden-text-p 'boring-headers))
2178           (pgp (gnus-article-hidden-text-p 'pgp))
2179           (pem (gnus-article-hidden-text-p 'pem))
2180           (signature (gnus-article-hidden-text-p 'signature))
2181           (overstrike (gnus-article-hidden-text-p 'overstrike))
2182           (emphasis (gnus-article-hidden-text-p 'emphasis))
2183           (mime gnus-show-mime))
2184       (format "%c%c%c%c%c%c%c"
2185               (if cite ?c ? )
2186               (if (or headers boring) ?h ? )
2187               (if (or pgp pem) ?p ? )
2188               (if signature ?s ? )
2189               (if overstrike ?o ? )
2190               (if mime ?m ? )
2191               (if emphasis ?e ? )))))
2192
2193 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2194
2195 (defun gnus-article-maybe-hide-headers ()
2196   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2197 Provided for backwards compatibility."
2198   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2199       gnus-inhibit-hiding
2200       (gnus-article-hide-headers)))
2201
2202 ;;; Article savers.
2203
2204 (defun gnus-output-to-file (file-name)
2205   "Append the current article to a file named FILE-NAME."
2206   (let ((artbuf (current-buffer)))
2207     (nnheader-temp-write nil
2208       (insert-buffer-substring artbuf)
2209       ;; Append newline at end of the buffer as separator, and then
2210       ;; save it to file.
2211       (goto-char (point-max))
2212       (insert "\n")
2213       (append-to-file (point-min) (point-max) file-name)
2214       t)))
2215
2216 (defun gnus-narrow-to-page (&optional arg)
2217   "Narrow the article buffer to a page.
2218 If given a numerical ARG, move forward ARG pages."
2219   (interactive "P")
2220   (setq arg (if arg (prefix-numeric-value arg) 0))
2221   (save-excursion
2222     (set-buffer gnus-article-buffer)
2223     (goto-char (point-min))
2224     (widen)
2225     ;; Remove any old next/prev buttons.
2226     (when (gnus-visual-p 'page-marker)
2227       (let ((buffer-read-only nil))
2228         (gnus-remove-text-with-property 'gnus-prev)
2229         (gnus-remove-text-with-property 'gnus-next)))
2230     (when
2231         (cond ((< arg 0)
2232                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2233               ((> arg 0)
2234                (re-search-forward page-delimiter nil 'move arg)))
2235       (goto-char (match-end 0)))
2236     (narrow-to-region
2237      (point)
2238      (if (re-search-forward page-delimiter nil 'move)
2239          (match-beginning 0)
2240        (point)))
2241     (when (and (gnus-visual-p 'page-marker)
2242                (not (= (point-min) 1)))
2243       (save-excursion
2244         (goto-char (point-min))
2245         (gnus-insert-prev-page-button)))
2246     (when (and (gnus-visual-p 'page-marker)
2247                (< (+ (point-max) 2) (buffer-size)))
2248       (save-excursion
2249         (goto-char (point-max))
2250         (gnus-insert-next-page-button)))))
2251
2252 ;; Article mode commands
2253
2254 (defun gnus-article-goto-next-page ()
2255   "Show the next page of the article."
2256   (interactive)
2257   (when (gnus-article-next-page)
2258     (goto-char (point-min))
2259     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2260
2261 (defun gnus-article-goto-prev-page ()
2262   "Show the next page of the article."
2263   (interactive)
2264   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2265     (gnus-article-prev-page nil)))
2266
2267 (defun gnus-article-next-page (&optional lines)
2268   "Show the next page of the current article.
2269 If end of article, return non-nil.  Otherwise return nil.
2270 Argument LINES specifies lines to be scrolled up."
2271   (interactive "p")
2272   (move-to-window-line -1)
2273   (if (save-excursion
2274         (end-of-line)
2275         (and (pos-visible-in-window-p)  ;Not continuation line.
2276              (eobp)))
2277       ;; Nothing in this page.
2278       (if (or (not gnus-page-broken)
2279               (save-excursion
2280                 (save-restriction
2281                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2282           t                             ;Nothing more.
2283         (gnus-narrow-to-page 1)         ;Go to next page.
2284         nil)
2285     ;; More in this page.
2286     (let ((scroll-in-place nil))
2287       (condition-case ()
2288           (scroll-up lines)
2289         (end-of-buffer
2290          ;; Long lines may cause an end-of-buffer error.
2291          (goto-char (point-max)))))
2292     (move-to-window-line 0)
2293     nil))
2294
2295 (defun gnus-article-prev-page (&optional lines)
2296   "Show previous page of current article.
2297 Argument LINES specifies lines to be scrolled down."
2298   (interactive "p")
2299   (move-to-window-line 0)
2300   (if (and gnus-page-broken
2301            (bobp)
2302            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2303       (progn
2304         (gnus-narrow-to-page -1)        ;Go to previous page.
2305         (goto-char (point-max))
2306         (recenter -1))
2307     (let ((scroll-in-place nil))
2308       (prog1
2309           (condition-case ()
2310               (scroll-down lines)
2311             (beginning-of-buffer
2312              (goto-char (point-min))))
2313         (move-to-window-line 0)))))
2314
2315 (defun gnus-article-refer-article ()
2316   "Read article specified by message-id around point."
2317   (interactive)
2318   (let ((point (point)))
2319     (search-forward ">" nil t)          ;Move point to end of "<....>".
2320     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2321         (let ((message-id (match-string 1)))
2322           (goto-char point)
2323           (set-buffer gnus-summary-buffer)
2324           (gnus-summary-refer-article message-id))
2325       (goto-char (point))
2326       (error "No references around point"))))
2327
2328 (defun gnus-article-show-summary ()
2329   "Reconfigure windows to show summary buffer."
2330   (interactive)
2331   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2332       (error "There is no summary buffer for this article buffer")
2333     (gnus-article-set-globals)
2334     (gnus-configure-windows 'article)
2335     (gnus-summary-goto-subject gnus-current-article)
2336     (gnus-summary-position-point)))
2337
2338 (defun gnus-article-describe-briefly ()
2339   "Describe article mode commands briefly."
2340   (interactive)
2341   (gnus-message 6
2342                 (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")))
2343
2344 (defun gnus-article-summary-command ()
2345   "Execute the last keystroke in the summary buffer."
2346   (interactive)
2347   (let ((obuf (current-buffer))
2348         (owin (current-window-configuration))
2349         func)
2350     (switch-to-buffer gnus-article-current-summary 'norecord)
2351     (setq func (lookup-key (current-local-map) (this-command-keys)))
2352     (call-interactively func)
2353     (set-buffer obuf)
2354     (set-window-configuration owin)
2355     (set-window-point (get-buffer-window (current-buffer)) (point))))
2356
2357 (defun gnus-article-summary-command-nosave ()
2358   "Execute the last keystroke in the summary buffer."
2359   (interactive)
2360   (let (func)
2361     (pop-to-buffer gnus-article-current-summary 'norecord)
2362     (setq func (lookup-key (current-local-map) (this-command-keys)))
2363     (call-interactively func)))
2364
2365 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2366   "Read a summary buffer key sequence and execute it from the article buffer."
2367   (interactive "P")
2368   (let ((nosaves
2369          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2370            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2371            "=" "^" "\M-^" "|"))
2372         (nosave-but-article
2373          '("A\r"))
2374         (nosave-in-article
2375          '("\C-d"))
2376         (up-to-top
2377          '("n" "Gn" "p" "Gp"))
2378         keys new-sum-point)
2379     (save-excursion
2380       (set-buffer gnus-article-current-summary)
2381       (let (gnus-pick-mode)
2382         (push (or key last-command-event) unread-command-events)
2383         (setq keys (read-key-sequence nil))))
2384     (message "")
2385
2386     (if (or (member keys nosaves)
2387             (member keys nosave-but-article)
2388             (member keys nosave-in-article))
2389         (let (func)
2390           (save-window-excursion
2391             (pop-to-buffer gnus-article-current-summary 'norecord)
2392             ;; We disable the pick minor mode commands.
2393             (let (gnus-pick-mode)
2394               (setq func (lookup-key (current-local-map) keys))))
2395           (if (not func)
2396               (ding)
2397             (unless (member keys nosave-in-article)
2398               (set-buffer gnus-article-current-summary))
2399             (call-interactively func)
2400             (setq new-sum-point (point)))
2401           (when (member keys nosave-but-article)
2402             (pop-to-buffer gnus-article-buffer 'norecord)))
2403       ;; These commands should restore window configuration.
2404       (let ((obuf (current-buffer))
2405             (owin (current-window-configuration))
2406             (opoint (point))
2407             (summary gnus-article-current-summary)
2408             func in-buffer selected)
2409         (if not-restore-window
2410             (pop-to-buffer summary 'norecord)
2411           (switch-to-buffer summary 'norecord))
2412         (setq in-buffer (current-buffer))
2413         ;; We disable the pick minor mode commands.
2414         (if (setq func (let (gnus-pick-mode)
2415                          (lookup-key (current-local-map) keys)))
2416             (progn
2417               (call-interactively func)
2418               (setq new-sum-point (point)))
2419           (ding))
2420         (when (eq in-buffer (current-buffer))
2421           (setq selected (gnus-summary-select-article))
2422           (set-buffer obuf)
2423           (unless not-restore-window
2424             (set-window-configuration owin))
2425           (unless (or (not (eq selected 'old)) (member keys up-to-top))
2426             (set-window-point (get-buffer-window (current-buffer))
2427                               opoint))
2428           (let ((win (get-buffer-window gnus-article-current-summary)))
2429             (when win
2430               (set-window-point win new-sum-point))))))))
2431
2432 (defun gnus-article-hide (&optional arg force)
2433   "Hide all the gruft in the current article.
2434 This means that PGP stuff, signatures, cited text and (some)
2435 headers will be hidden.
2436 If given a prefix, show the hidden text instead."
2437   (interactive (append (gnus-article-hidden-arg) (list 'force)))
2438   (gnus-article-hide-headers arg)
2439   (gnus-article-hide-pgp arg)
2440   (gnus-article-hide-citation-maybe arg force)
2441   (gnus-article-hide-signature arg))
2442
2443 (defun gnus-article-maybe-highlight ()
2444   "Do some article highlighting if article highlighting is requested."
2445   (when (gnus-visual-p 'article-highlight 'highlight)
2446     (gnus-article-highlight-some)))
2447
2448 (defun gnus-check-group-server ()
2449   ;; Make sure the connection to the server is alive.
2450   (unless (gnus-server-opened
2451            (gnus-find-method-for-group gnus-newsgroup-name))
2452     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
2453     (gnus-request-group gnus-newsgroup-name t)))
2454
2455 (defun gnus-request-article-this-buffer (article group)
2456   "Get an article and insert it into this buffer."
2457   (let (do-update-line sparse-header)
2458     (prog1
2459         (save-excursion
2460           (erase-buffer)
2461           (gnus-kill-all-overlays)
2462           (setq group (or group gnus-newsgroup-name))
2463
2464           ;; Using `gnus-request-article' directly will insert the article into
2465           ;; `nntp-server-buffer' - so we'll save some time by not having to
2466           ;; copy it from the server buffer into the article buffer.
2467
2468           ;; We only request an article by message-id when we do not have the
2469           ;; headers for it, so we'll have to get those.
2470           (when (stringp article)
2471             (let ((gnus-override-method gnus-refer-article-method))
2472               (gnus-read-header article)))
2473
2474           ;; If the article number is negative, that means that this article
2475           ;; doesn't belong in this newsgroup (possibly), so we find its
2476           ;; message-id and request it by id instead of number.
2477           (when (and (numberp article)
2478                      gnus-summary-buffer
2479                      (get-buffer gnus-summary-buffer)
2480                      (gnus-buffer-exists-p gnus-summary-buffer))
2481             (save-excursion
2482               (set-buffer gnus-summary-buffer)
2483               (let ((header (gnus-summary-article-header article)))
2484                 (when (< article 0)
2485                   (cond
2486                    ((memq article gnus-newsgroup-sparse)
2487                     ;; This is a sparse gap article.
2488                     (setq do-update-line article)
2489                     (setq article (mail-header-id header))
2490                     (let ((gnus-override-method gnus-refer-article-method))
2491                       (setq sparse-header (gnus-read-header article)))
2492                     (setq gnus-newsgroup-sparse
2493                           (delq article gnus-newsgroup-sparse)))
2494                    ((vectorp header)
2495                     ;; It's a real article.
2496                     (setq article (mail-header-id header)))
2497                    (t
2498                     ;; It is an extracted pseudo-article.
2499                     (setq article 'pseudo)
2500                     (gnus-request-pseudo-article header))))
2501
2502                 (let ((method (gnus-find-method-for-group
2503                                gnus-newsgroup-name)))
2504                   (when (and (eq (car method) 'nneething)
2505                              (vectorp header))
2506                     (let ((dir (concat
2507                                 (file-name-as-directory
2508                                  (or (cadr (assq 'nneething-address method))
2509                                      (nth 1 method)))
2510                                 (mail-header-subject header))))
2511                       (when (file-directory-p dir)
2512                         (setq article 'nneething)
2513                         (gnus-group-enter-directory dir))))))))
2514
2515           (cond
2516            ;; Refuse to select canceled articles.
2517            ((and (numberp article)
2518                  gnus-summary-buffer
2519                  (get-buffer gnus-summary-buffer)
2520                  (gnus-buffer-exists-p gnus-summary-buffer)
2521                  (eq (cdr (save-excursion
2522                             (set-buffer gnus-summary-buffer)
2523                             (assq article gnus-newsgroup-reads)))
2524                      gnus-canceled-mark))
2525             nil)
2526            ;; Check the backlog.
2527            ((and gnus-keep-backlog
2528                  (gnus-backlog-request-article group article (current-buffer)))
2529             'article)
2530            ;; Check asynchronous pre-fetch.
2531            ((gnus-async-request-fetched-article group article (current-buffer))
2532             (gnus-async-prefetch-next group article gnus-summary-buffer)
2533             (when (and (numberp article) gnus-keep-backlog)
2534               (gnus-backlog-enter-article group article (current-buffer)))
2535             'article)
2536            ;; Check the cache.
2537            ((and gnus-use-cache
2538                  (numberp article)
2539                  (gnus-cache-request-article article group))
2540             'article)
2541            ;; Get the article and put into the article buffer.
2542            ((or (stringp article) (numberp article))
2543             (let ((gnus-override-method
2544                    (and (stringp article) gnus-refer-article-method))
2545                   (buffer-read-only nil))
2546               (erase-buffer)
2547               (gnus-kill-all-overlays)
2548               (gnus-check-group-server)
2549               (when (gnus-request-article article group (current-buffer))
2550                 (when (numberp article)
2551                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2552                   (when gnus-keep-backlog
2553                     (gnus-backlog-enter-article
2554                      group article (current-buffer))))
2555                 'article)))
2556            ;; It was a pseudo.
2557            (t article)))
2558
2559       ;; Associate this article with the current summary buffer.
2560       (setq gnus-article-current-summary gnus-summary-buffer)
2561
2562       ;; Take the article from the original article buffer
2563       ;; and place it in the buffer it's supposed to be in.
2564       (when (and (get-buffer gnus-article-buffer)
2565                  (equal (buffer-name (current-buffer))
2566                         (buffer-name (get-buffer gnus-article-buffer))))
2567         (save-excursion
2568           (if (get-buffer gnus-original-article-buffer)
2569               (set-buffer gnus-original-article-buffer)
2570             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2571             (buffer-disable-undo (current-buffer))
2572             (setq major-mode 'gnus-original-article-mode)
2573             (setq buffer-read-only t))
2574           (let (buffer-read-only)
2575             (erase-buffer)
2576             (insert-buffer-substring gnus-article-buffer))
2577           (setq gnus-original-article (cons group article))))
2578
2579       ;; Update sparse articles.
2580       (when (and do-update-line
2581                  (or (numberp article)
2582                      (stringp article)))
2583         (let ((buf (current-buffer)))
2584           (set-buffer gnus-summary-buffer)
2585           (gnus-summary-update-article do-update-line sparse-header)
2586           (gnus-summary-goto-subject do-update-line nil t)
2587           (set-window-point (get-buffer-window (current-buffer) t)
2588                             (point))
2589           (set-buffer buf))))))
2590
2591 ;;;
2592 ;;; Article editing
2593 ;;;
2594
2595 (defcustom gnus-article-edit-mode-hook nil
2596   "Hook run in article edit mode buffers."
2597   :group 'gnus-article-various
2598   :type 'hook)
2599
2600 (defcustom gnus-article-edit-article-setup-function
2601   'gnus-article-mime-edit-article-setup
2602   "Function called to setup an editing article buffer."
2603   :group 'gnus-article-various
2604   :type 'function)
2605
2606 (defvar gnus-article-edit-done-function nil)
2607
2608 (defvar gnus-article-edit-mode-map nil)
2609
2610 (unless gnus-article-edit-mode-map
2611   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2612
2613   (gnus-define-keys gnus-article-edit-mode-map
2614     "\C-c\C-c" gnus-article-edit-done
2615     "\C-c\C-k" gnus-article-edit-exit)
2616
2617   (gnus-define-keys (gnus-article-edit-wash-map
2618                      "\C-c\C-w" gnus-article-edit-mode-map)
2619     "f" gnus-article-edit-full-stops))
2620
2621 (defun gnus-article-edit-mode ()
2622   "Major mode for editing articles.
2623 This is an extended text-mode.
2624
2625 \\{gnus-article-edit-mode-map}"
2626   (interactive)
2627   (setq major-mode 'gnus-article-edit-mode)
2628   (setq mode-name "Article Edit")
2629   (use-local-map gnus-article-edit-mode-map)
2630   (make-local-variable 'gnus-article-edit-done-function)
2631   (make-local-variable 'gnus-prev-winconf)
2632   (setq buffer-read-only nil)
2633   (buffer-enable-undo)
2634   (widen)
2635   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
2636
2637 (defun gnus-article-edit (&optional force)
2638   "Edit the current article.
2639 This will have permanent effect only in mail groups.
2640 If FORCE is non-nil, allow editing of articles even in read-only
2641 groups."
2642   (interactive "P")
2643   (when (and (not force)
2644              (gnus-group-read-only-p))
2645     (error "The current newsgroup does not support article editing"))
2646   (gnus-article-date-original)
2647   (gnus-article-edit-article
2648    `(lambda (no-highlight)
2649       (gnus-summary-edit-article-done
2650        ,(or (mail-header-references gnus-current-headers) "")
2651        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2652
2653 (defun gnus-article-edit-article (exit-func)
2654   "Start editing the contents of the current article buffer."
2655   (let ((winconf (current-window-configuration)))
2656     (set-buffer gnus-article-buffer)
2657     (gnus-article-edit-mode)
2658     (gnus-article-delete-text-of-type 'annotation)
2659     (gnus-set-text-properties (point-min) (point-max) nil)
2660     (gnus-configure-windows 'edit-article)
2661     (setq gnus-article-edit-done-function exit-func)
2662     (setq gnus-prev-winconf winconf)
2663     (when gnus-article-edit-article-setup-function
2664       (funcall gnus-article-edit-article-setup-function))
2665     (gnus-message 6 "C-c C-c to end edits")))
2666
2667 (defun gnus-article-edit-done (&optional arg)
2668   "Update the article edits and exit."
2669   (interactive "P")
2670   (save-excursion
2671     (save-restriction
2672       (widen)
2673       (goto-char (point-min))
2674       (when (search-forward "\n\n" nil 1)
2675         (let ((lines (count-lines (point) (point-max)))
2676               (length (- (point-max) (point)))
2677               (case-fold-search t)
2678               (body (copy-marker (point))))
2679           (goto-char (point-min))
2680           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
2681             (delete-region (match-beginning 1) (match-end 1))
2682             (insert (number-to-string length)))
2683           (goto-char (point-min))
2684           (when (re-search-forward
2685                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
2686             (delete-region (match-beginning 1) (match-end 1))
2687             (insert (number-to-string length)))
2688           (goto-char (point-min))
2689           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
2690             (delete-region (match-beginning 1) (match-end 1))
2691             (insert (number-to-string lines)))))))
2692   (let ((func gnus-article-edit-done-function)
2693         (buf (current-buffer))
2694         (start (window-start)))
2695     (remove-hook 'gnus-article-mode-hook
2696                  'gnus-article-mime-edit-article-unwind)
2697     (gnus-article-edit-exit)
2698     (save-excursion
2699       (set-buffer buf)
2700       (let ((buffer-read-only nil))
2701         (funcall func arg)))
2702     (set-buffer buf)
2703     (set-window-start (get-buffer-window buf) start)
2704     (set-window-point (get-buffer-window buf) (point))))
2705
2706 (defun gnus-article-edit-exit ()
2707   "Exit the article editing without updating."
2708   (interactive)
2709   ;; We remove all text props from the article buffer.
2710   (let ((buf (format "%s" (buffer-substring (point-min) (point-max))))
2711         (curbuf (current-buffer))
2712         (p (point))
2713         (window-start (window-start)))
2714     (erase-buffer)
2715     (insert buf)
2716     (let ((winconf gnus-prev-winconf))
2717       (gnus-article-mode)
2718       ;; The cache and backlog have to be flushed somewhat.
2719       (when gnus-use-cache
2720         (gnus-cache-update-article
2721          (car gnus-article-current) (cdr gnus-article-current)))
2722       (when gnus-keep-backlog
2723         (gnus-backlog-remove-article
2724          (car gnus-article-current) (cdr gnus-article-current)))
2725       ;; Flush original article as well.
2726       (save-excursion
2727         (when (get-buffer gnus-original-article-buffer)
2728           (set-buffer gnus-original-article-buffer)
2729           (setq gnus-original-article nil)))
2730       (set-window-configuration winconf)
2731       ;; Tippy-toe some to make sure that point remains where it was.
2732       (let ((buf (current-buffer)))
2733         (set-buffer curbuf)
2734         (set-window-start (get-buffer-window (current-buffer)) window-start)
2735         (goto-char p)
2736         (set-buffer buf)))))
2737
2738 (defun gnus-article-edit-full-stops ()
2739   "Interactively repair spacing at end of sentences."
2740   (interactive)
2741   (save-excursion
2742     (goto-char (point-min))
2743     (search-forward-regexp "^$" nil t)
2744     (let ((case-fold-search nil))
2745       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2746
2747 ;;;
2748 ;;; Article editing with MIME-Edit
2749 ;;;
2750
2751 (defcustom gnus-article-mime-edit-article-setup-hook nil
2752   "Hook run after setting up a MIME editing article buffer."
2753   :group 'gnus-article-various
2754   :type 'hook)
2755
2756 (defun gnus-article-mime-edit-article-unwind ()
2757   "Unwind `gnus-article-buffer' if article editing was given up."
2758   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
2759   (when mime-edit-mode-flag
2760     (mime-edit-exit 'nomime 'no-error)
2761     (message ""))
2762   (when (featurep 'font-lock)
2763     (setq font-lock-defaults nil)
2764     (font-lock-mode 0)))
2765
2766 (defun gnus-article-mime-edit-article-setup ()
2767   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
2768 after replacing with the original article."
2769   (setq gnus-show-mime t)
2770   (setq gnus-article-edit-done-function
2771         `(lambda (&rest args)
2772            (when mime-edit-mode-flag
2773              (mime-edit-exit)
2774              (message ""))
2775            (goto-char (point-min))
2776            (let (case-fold-search)
2777              (when (re-search-forward
2778                     (format "^%s$" (regexp-quote mail-header-separator))
2779                     nil t)
2780                (replace-match "")))
2781            (when (featurep 'font-lock)
2782              (setq font-lock-defaults nil)
2783              (font-lock-mode 0))
2784            (apply ,gnus-article-edit-done-function args)
2785            (set-buffer gnus-original-article-buffer)
2786            (erase-buffer)
2787            (insert-buffer gnus-article-buffer)
2788            (setq gnus-current-headers (gnus-article-make-full-mail-header))
2789            (gnus-article-prepare-display)))
2790   (define-key (current-local-map) "\C-c\C-k" 'gnus-article-mime-edit-exit)
2791   (erase-buffer)
2792   (insert-buffer gnus-original-article-buffer)
2793   (mime-edit-again)
2794   (when (featurep 'font-lock)
2795     (set (make-local-variable 'font-lock-defaults)
2796          '(message-font-lock-keywords t))
2797     (font-lock-set-defaults)
2798     (turn-on-font-lock))
2799   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
2800   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
2801
2802 (defun gnus-article-mime-edit-exit ()
2803   "Exit the article MIME editing without updating."
2804   (interactive)
2805   (let ((winconf gnus-prev-winconf)
2806         buf)
2807     (when mime-edit-mode-flag
2808       (mime-edit-exit)
2809       (message ""))
2810     (goto-char (point-min))
2811     (let (case-fold-search)
2812       (when (re-search-forward
2813              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
2814         (replace-match "")))
2815     (when (featurep 'font-lock)
2816       (setq font-lock-defaults nil)
2817       (font-lock-mode 0))
2818     ;; We remove all text props from the article buffer.
2819     (setq buf (format "%s" (buffer-substring (point-min) (point-max))))
2820     (set-buffer (get-buffer-create gnus-original-article-buffer))
2821     (erase-buffer)
2822     (insert buf)
2823     (setq gnus-current-headers (gnus-article-make-full-mail-header))
2824     (gnus-article-prepare-display)
2825     (set-window-configuration winconf)))
2826
2827 ;;;
2828 ;;; Article highlights
2829 ;;;
2830
2831 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2832
2833 ;;; Internal Variables:
2834
2835 (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\\)"
2836   "Regular expression that matches URLs."
2837   :group 'gnus-article-buttons
2838   :type 'regexp)
2839
2840 (defcustom gnus-button-alist
2841   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2842      gnus-button-message-id 2)
2843     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2844     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
2845      1 t
2846      gnus-button-fetch-group 4)
2847     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2848     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2849      t gnus-button-message-id 3)
2850     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2851     ("mailto:\\([a-zA-Z.-@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
2852     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2853     ;; This is how URLs _should_ be embedded in text...
2854     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2855     ;; Raw URLs.
2856     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2857   "*Alist of regexps matching buttons in article bodies.
2858
2859 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2860 REGEXP: is the string matching text around the button,
2861 BUTTON: is the number of the regexp grouping actually matching the button,
2862 FORM: is a lisp expression which must eval to true for the button to
2863 be added,
2864 CALLBACK: is the function to call when the user push this button, and each
2865 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2866
2867 CALLBACK can also be a variable, in that case the value of that
2868 variable it the real callback function."
2869   :group 'gnus-article-buttons
2870   :type '(repeat (list regexp
2871                        (integer :tag "Button")
2872                        (sexp :tag "Form")
2873                        (function :tag "Callback")
2874                        (repeat :tag "Par"
2875                                :inline t
2876                                (integer :tag "Regexp group")))))
2877
2878 (defcustom gnus-header-button-alist
2879   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2880      0 t gnus-button-message-id 0)
2881     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2882     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2883      0 t gnus-button-mailto 0)
2884     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2885     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2886     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2887     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2888      gnus-button-message-id 3))
2889   "*Alist of headers and regexps to match buttons in article heads.
2890
2891 This alist is very similar to `gnus-button-alist', except that each
2892 alist has an additional HEADER element first in each entry:
2893
2894 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2895
2896 HEADER is a regexp to match a header.  For a fuller explanation, see
2897 `gnus-button-alist'."
2898   :group 'gnus-article-buttons
2899   :group 'gnus-article-headers
2900   :type '(repeat (list (regexp :tag "Header")
2901                        regexp
2902                        (integer :tag "Button")
2903                        (sexp :tag "Form")
2904                        (function :tag "Callback")
2905                        (repeat :tag "Par"
2906                                :inline t
2907                                (integer :tag "Regexp group")))))
2908
2909 (defvar gnus-button-regexp nil)
2910 (defvar gnus-button-marker-list nil)
2911 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2912
2913 (defvar gnus-button-last nil)
2914 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2915
2916 ;;; Commands:
2917
2918 (defun gnus-article-push-button (event)
2919   "Check text under the mouse pointer for a callback function.
2920 If the text under the mouse pointer has a `gnus-callback' property,
2921 call it with the value of the `gnus-data' text property."
2922   (interactive "e")
2923   (set-buffer (window-buffer (posn-window (event-start event))))
2924   (let* ((pos (posn-point (event-start event)))
2925          (data (get-text-property pos 'gnus-data))
2926          (fun (get-text-property pos 'gnus-callback)))
2927     (goto-char pos)
2928     (when fun
2929       (funcall fun data))))
2930
2931 (defun gnus-article-press-button ()
2932   "Check text at point for a callback function.
2933 If the text at point has a `gnus-callback' property,
2934 call it with the value of the `gnus-data' text property."
2935   (interactive)
2936   (let* ((data (get-text-property (point) 'gnus-data))
2937          (fun (get-text-property (point) 'gnus-callback)))
2938     (when fun
2939       (funcall fun data))))
2940
2941 (defun gnus-article-prev-button (n)
2942   "Move point to N buttons backward.
2943 If N is negative, move forward instead."
2944   (interactive "p")
2945   (gnus-article-next-button (- n)))
2946
2947 (defun gnus-article-next-button (n)
2948   "Move point to N buttons forward.
2949 If N is negative, move backward instead."
2950   (interactive "p")
2951   (let ((function (if (< n 0) 'previous-single-property-change
2952                     'next-single-property-change))
2953         (inhibit-point-motion-hooks t)
2954         (backward (< n 0))
2955         (limit (if (< n 0) (point-min) (point-max))))
2956     (setq n (abs n))
2957     (while (and (not (= limit (point)))
2958                 (> n 0))
2959       ;; Skip past the current button.
2960       (when (get-text-property (point) 'gnus-callback)
2961         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2962       ;; Go to the next (or previous) button.
2963       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2964       ;; Put point at the start of the button.
2965       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2966         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2967       ;; Skip past intangible buttons.
2968       (when (get-text-property (point) 'intangible)
2969         (incf n))
2970       (decf n))
2971     (unless (zerop n)
2972       (gnus-message 5 "No more buttons"))
2973     n))
2974
2975 (defun gnus-article-highlight (&optional force)
2976   "Highlight current article.
2977 This function calls `gnus-article-highlight-headers',
2978 `gnus-article-highlight-citation',
2979 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2980 do the highlighting.  See the documentation for those functions."
2981   (interactive (list 'force))
2982   (gnus-article-highlight-headers)
2983   (gnus-article-highlight-citation force)
2984   (gnus-article-highlight-signature)
2985   (gnus-article-add-buttons force)
2986   (gnus-article-add-buttons-to-head))
2987
2988 (defun gnus-article-highlight-some (&optional force)
2989   "Highlight current article.
2990 This function calls `gnus-article-highlight-headers',
2991 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2992 do the highlighting.  See the documentation for those functions."
2993   (interactive (list 'force))
2994   (gnus-article-highlight-headers)
2995   (gnus-article-highlight-signature)
2996   (gnus-article-add-buttons))
2997
2998 (defun gnus-article-highlight-headers ()
2999   "Highlight article headers as specified by `gnus-header-face-alist'."
3000   (interactive)
3001   (save-excursion
3002     (set-buffer gnus-article-buffer)
3003     (save-restriction
3004       (let ((alist gnus-header-face-alist)
3005             (buffer-read-only nil)
3006             (case-fold-search t)
3007             (inhibit-point-motion-hooks t)
3008             entry regexp header-face field-face from hpoints fpoints)
3009         (message-narrow-to-head)
3010         (while (setq entry (pop alist))
3011           (goto-char (point-min))
3012           (setq regexp (concat "^\\("
3013                                (if (string-equal "" (nth 0 entry))
3014                                    "[^\t ]"
3015                                  (nth 0 entry))
3016                                "\\)")
3017                 header-face (nth 1 entry)
3018                 field-face (nth 2 entry))
3019           (while (and (re-search-forward regexp nil t)
3020                       (not (eobp)))
3021             (beginning-of-line)
3022             (setq from (point))
3023             (unless (search-forward ":" nil t)
3024               (forward-char 1))
3025             (when (and header-face
3026                        (not (memq (point) hpoints)))
3027               (push (point) hpoints)
3028               (gnus-put-text-property from (point) 'face header-face))
3029             (when (and field-face
3030                        (not (memq (setq from (point)) fpoints)))
3031               (push from fpoints)
3032               (while (and (zerop (forward-line 1))
3033                           (memq (char-after) '(?\t ?\ ))))
3034               (unless (eobp)
3035                 ;; Go to the end of the previous line.
3036                 (end-of-line 0))
3037               (gnus-put-text-property from (point) 'face field-face))))))))
3038
3039 (defun gnus-article-highlight-signature ()
3040   "Highlight the signature in an article.
3041 It does this by highlighting everything after
3042 `gnus-signature-separator' using `gnus-signature-face'."
3043   (interactive)
3044   (save-excursion
3045     (set-buffer gnus-article-buffer)
3046     (let ((buffer-read-only nil)
3047           (inhibit-point-motion-hooks t))
3048       (save-restriction
3049         (when (and gnus-signature-face
3050                    (gnus-article-narrow-to-signature))
3051           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
3052                             'face gnus-signature-face)
3053           (widen)
3054           (gnus-article-search-signature)
3055           (let ((start (match-beginning 0))
3056                 (end (set-marker (make-marker) (1+ (match-end 0)))))
3057             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
3058                                      end)))))))
3059
3060 (defun gnus-button-in-region-p (b e prop)
3061   "Say whether PROP exists in the region."
3062   (text-property-not-all b e prop nil))
3063
3064 (defun gnus-article-add-buttons (&optional force)
3065   "Find external references in the article and make buttons of them.
3066 \"External references\" are things like Message-IDs and URLs, as
3067 specified by `gnus-button-alist'."
3068   (interactive (list 'force))
3069   (save-excursion
3070     (set-buffer gnus-article-buffer)
3071     (let ((buffer-read-only nil)
3072           (inhibit-point-motion-hooks t)
3073           (case-fold-search t)
3074           (alist gnus-button-alist)
3075           beg entry regexp)
3076       ;; Remove all old markers.
3077       (let (marker entry)
3078         (while (setq marker (pop gnus-button-marker-list))
3079           (goto-char marker)
3080           (when (setq entry (gnus-button-entry))
3081             (put-text-property (match-beginning (nth 1 entry))
3082                                (match-end (nth 1 entry))
3083                                'gnus-callback nil))
3084           (set-marker marker nil)))
3085       ;; We skip the headers.
3086       (goto-char (point-min))
3087       (unless (search-forward "\n\n" nil t)
3088         (goto-char (point-max)))
3089       (setq beg (point))
3090       (while (setq entry (pop alist))
3091         (setq regexp (car entry))
3092         (goto-char beg)
3093         (while (re-search-forward regexp nil t)
3094           (let* ((start (and entry (match-beginning (nth 1 entry))))
3095                  (end (and entry (match-end (nth 1 entry))))
3096                  (from (match-beginning 0)))
3097             (when (and (or (eq t (nth 2 entry))
3098                            (eval (nth 2 entry)))
3099                        (not (gnus-button-in-region-p
3100                              start end 'gnus-callback)))
3101               ;; That optional form returned non-nil, so we add the
3102               ;; button.
3103               (gnus-article-add-button
3104                start end 'gnus-button-push
3105                (car (push (set-marker (make-marker) from)
3106                           gnus-button-marker-list))))))))))
3107
3108 ;; Add buttons to the head of an article.
3109 (defun gnus-article-add-buttons-to-head ()
3110   "Add buttons to the head of the article."
3111   (interactive)
3112   (save-excursion
3113     (set-buffer gnus-article-buffer)
3114     (let ((buffer-read-only nil)
3115           (inhibit-point-motion-hooks t)
3116           (case-fold-search t)
3117           (alist gnus-header-button-alist)
3118           entry beg end)
3119       (nnheader-narrow-to-headers)
3120       (while alist
3121         ;; Each alist entry.
3122         (setq entry (car alist)
3123               alist (cdr alist))
3124         (goto-char (point-min))
3125         (while (re-search-forward (car entry) nil t)
3126           ;; Each header matching the entry.
3127           (setq beg (match-beginning 0))
3128           (while (and (zerop (forward-line 1))
3129                       (memq (char-after) '(?\t ?\ ))))
3130           (setq end (point))
3131           (goto-char beg)
3132           (while (re-search-forward (nth 1 entry) end t)
3133             ;; Each match within a header.
3134             (let* ((entry (cdr entry))
3135                    (start (match-beginning (nth 1 entry)))
3136                    (end (match-end (nth 1 entry)))
3137                    (form (nth 2 entry)))
3138               (goto-char (match-end 0))
3139               (when (eval form)
3140                 (gnus-article-add-button
3141                  start end (nth 3 entry)
3142                  (buffer-substring (match-beginning (nth 4 entry))
3143                                    (match-end (nth 4 entry)))))))
3144           (goto-char end))))
3145     (widen)))
3146
3147 ;;; External functions:
3148
3149 (defun gnus-article-add-button (from to fun &optional data)
3150   "Create a button between FROM and TO with callback FUN and data DATA."
3151   (when gnus-article-button-face
3152     (gnus-overlay-put (gnus-make-overlay from to)
3153                       'face gnus-article-button-face))
3154   (gnus-add-text-properties
3155    from to
3156    (nconc (and gnus-article-mouse-face
3157                (list gnus-mouse-face-prop gnus-article-mouse-face))
3158           (list 'gnus-callback fun)
3159           (and data (list 'gnus-data data)))))
3160
3161 ;;; Internal functions:
3162
3163 (defun gnus-article-set-globals ()
3164   (save-excursion
3165     (set-buffer gnus-summary-buffer)
3166     (gnus-set-global-variables)))
3167
3168 (defun gnus-signature-toggle (end)
3169   (save-excursion
3170     (set-buffer gnus-article-buffer)
3171     (let ((buffer-read-only nil)
3172           (inhibit-point-motion-hooks t))
3173       (if (get-text-property end 'invisible)
3174           (gnus-article-unhide-text end (point-max))
3175         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3176
3177 (defun gnus-button-entry ()
3178   ;; Return the first entry in `gnus-button-alist' matching this place.
3179   (let ((alist gnus-button-alist)
3180         (entry nil))
3181     (while alist
3182       (setq entry (pop alist))
3183       (if (looking-at (car entry))
3184           (setq alist nil)
3185         (setq entry nil)))
3186     entry))
3187
3188 (defun gnus-button-push (marker)
3189   ;; Push button starting at MARKER.
3190   (save-excursion
3191     (set-buffer gnus-article-buffer)
3192     (goto-char marker)
3193     (let* ((entry (gnus-button-entry))
3194            (inhibit-point-motion-hooks t)
3195            (fun (nth 3 entry))
3196            (args (mapcar (lambda (group)
3197                            (let ((string (match-string group)))
3198                              (gnus-set-text-properties
3199                               0 (length string) nil string)
3200                              string))
3201                          (nthcdr 4 entry))))
3202       (cond
3203        ((fboundp fun)
3204         (apply fun args))
3205        ((and (boundp fun)
3206              (fboundp (symbol-value fun)))
3207         (apply (symbol-value fun) args))
3208        (t
3209         (gnus-message 1 "You must define `%S' to use this button"
3210                       (cons fun args)))))))
3211
3212 (defun gnus-button-message-id (message-id)
3213   "Fetch MESSAGE-ID."
3214   (save-excursion
3215     (set-buffer gnus-summary-buffer)
3216     (gnus-summary-refer-article message-id)))
3217
3218 (defun gnus-button-fetch-group (address)
3219   "Fetch GROUP specified by ADDRESS."
3220   (if (not (string-match "[:/]" address))
3221       ;; This is just a simple group url.
3222       (gnus-group-read-ephemeral-group address gnus-select-method)
3223     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3224                            address))
3225         (error "Can't parse %s" address)
3226       (gnus-group-read-ephemeral-group
3227        (match-string 4 address)
3228        `(nntp ,(match-string 1 address)
3229               (nntp-address ,(match-string 1 address))
3230               (nntp-port-number ,(if (match-end 3)
3231                                      (match-string 3 address)
3232                                    "nntp")))))))
3233
3234 (defun gnus-url-parse-query-string (query &optional downcase)
3235   (let (retval pairs cur key val)
3236     (setq pairs (gnus-split-string query "&"))
3237     (while pairs
3238       (setq cur (car pairs)
3239             pairs (cdr pairs))
3240       (if (not (string-match "=" cur))
3241           nil                           ; Grace
3242         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3243               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3244         (if downcase
3245             (setq key (downcase key)))
3246         (setq cur (assoc key retval))
3247         (if cur
3248             (setcdr cur (cons val (cdr cur)))
3249           (setq retval (cons (list key val) retval)))))
3250     retval))
3251
3252 (defun gnus-url-unhex (x)
3253   (if (> x ?9)
3254       (if (>= x ?a)
3255           (+ 10 (- x ?a))
3256         (+ 10 (- x ?A)))
3257     (- x ?0)))
3258
3259 (defun gnus-url-unhex-string (str &optional allow-newlines)
3260   "Remove %XXX embedded spaces, etc in a url.
3261 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3262 decoding of carriage returns and line feeds in the string, which is normally
3263 forbidden in URL encoding."
3264   (setq str (or str ""))
3265   (let ((tmp "")
3266         (case-fold-search t))
3267     (while (string-match "%[0-9a-f][0-9a-f]" str)
3268       (let* ((start (match-beginning 0))
3269              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3270              (code (+ (* 16 ch1)
3271                       (gnus-url-unhex (elt str (+ start 2))))))
3272         (setq tmp (concat
3273                    tmp (substring str 0 start)
3274                    (cond
3275                     (allow-newlines
3276                      (char-to-string code))
3277                     ((or (= code ?\n) (= code ?\r))
3278                      " ")
3279                     (t (char-to-string code))))
3280               str (substring str (match-end 0)))))
3281     (setq tmp (concat tmp str))
3282     tmp))
3283
3284 (defun gnus-url-mailto (url)
3285   ;; Send mail to someone
3286   (when (string-match "mailto:/*\\(.*\\)" url)
3287     (setq url (substring url (match-beginning 1) nil)))
3288   (let (to args subject func)
3289     (if (string-match (regexp-quote "?") url)
3290         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3291               args (gnus-url-parse-query-string
3292                     (substring url (match-end 0) nil) t))
3293       (setq to (gnus-url-unhex-string url)))
3294     (setq args (cons (list "to" to) args)
3295           subject (cdr-safe (assoc "subject" args)))
3296     (gnus-setup-message 'reply
3297       (message-mail)
3298       (while args
3299         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3300         (if (fboundp func)
3301             (funcall func)
3302           (message-position-on-field (caar args)))
3303         (insert (mapconcat 'identity (cdar args) ", "))
3304         (setq args (cdr args)))
3305       (if subject
3306           (message-goto-body)
3307         (message-goto-subject)))))
3308
3309 (defun gnus-button-mailto (address)
3310   ;; Mail to ADDRESS.
3311   (set-buffer (gnus-copy-article-buffer))
3312   (gnus-setup-message 'reply
3313     (message-reply address)))
3314
3315 (defun gnus-button-reply (address)
3316   ;; Reply to ADDRESS.
3317   (gnus-setup-message 'reply
3318     (message-reply address)))
3319
3320 (defun gnus-button-url (address)
3321   "Browse ADDRESS."
3322   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3323   (if (listp browse-url-browser-function)
3324       (browse-url address)
3325     (funcall browse-url-browser-function address)))
3326
3327 (defun gnus-button-embedded-url (address)
3328   "Browse ADDRESS."
3329   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3330   (if (listp browse-url-browser-function)
3331       (browse-url (gnus-strip-whitespace address))
3332     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
3333
3334 ;;; Next/prev buttons in the article buffer.
3335
3336 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3337 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3338
3339 (defvar gnus-prev-page-map nil)
3340 (unless gnus-prev-page-map
3341   (setq gnus-prev-page-map (make-sparse-keymap))
3342   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3343   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3344
3345 (defun gnus-insert-prev-page-button ()
3346   (let ((buffer-read-only nil))
3347     (gnus-eval-format
3348      gnus-prev-page-line-format nil
3349      `(gnus-prev t local-map ,gnus-prev-page-map
3350                  gnus-callback gnus-article-button-prev-page
3351                  gnus-type annotation))))
3352
3353 (defvar gnus-next-page-map nil)
3354 (unless gnus-next-page-map
3355   (setq gnus-next-page-map (make-keymap))
3356   (suppress-keymap gnus-prev-page-map)
3357   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3358   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3359
3360 (defun gnus-button-next-page ()
3361   "Go to the next page."
3362   (interactive)
3363   (let ((win (selected-window)))
3364     (select-window (get-buffer-window gnus-article-buffer t))
3365     (gnus-article-next-page)
3366     (select-window win)))
3367
3368 (defun gnus-button-prev-page ()
3369   "Go to the prev page."
3370   (interactive)
3371   (let ((win (selected-window)))
3372     (select-window (get-buffer-window gnus-article-buffer t))
3373     (gnus-article-prev-page)
3374     (select-window win)))
3375
3376 (defun gnus-insert-next-page-button ()
3377   (let ((buffer-read-only nil))
3378     (gnus-eval-format gnus-next-page-line-format nil
3379                       `(gnus-next
3380                         t local-map ,gnus-next-page-map
3381                         gnus-callback gnus-article-button-next-page
3382                         gnus-type annotation))))
3383
3384 (defun gnus-article-button-next-page (arg)
3385   "Go to the next page."
3386   (interactive "P")
3387   (let ((win (selected-window)))
3388     (select-window (get-buffer-window gnus-article-buffer t))
3389     (gnus-article-next-page)
3390     (select-window win)))
3391
3392 (defun gnus-article-button-prev-page (arg)
3393   "Go to the prev page."
3394   (interactive "P")
3395   (let ((win (selected-window)))
3396     (select-window (get-buffer-window gnus-article-buffer t))
3397     (gnus-article-prev-page)
3398     (select-window win)))
3399
3400
3401 ;;; @ for mime-view
3402 ;;;
3403
3404 (defun gnus-article-header-presentation-method (entity situation)
3405   (mime-insert-header entity)
3406   )
3407
3408 (set-alist 'mime-header-presentation-method-alist
3409            'gnus-original-article-mode
3410            #'gnus-article-header-presentation-method)
3411
3412 (defun gnus-mime-preview-quitting-method ()
3413   (mime-preview-kill-buffer)
3414   (delete-other-windows)
3415   (gnus-article-show-summary)
3416   (gnus-summary-select-article gnus-show-all-headers t))
3417
3418 (set-alist 'mime-preview-quitting-method-alist
3419            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
3420
3421 (defun gnus-following-method (buf)
3422   (set-buffer buf)
3423   (message-followup)
3424   (message-yank-original)
3425   (kill-buffer buf)
3426   (goto-char (point-min))
3427   )
3428
3429 (set-alist 'mime-preview-following-method-alist
3430            'gnus-original-article-mode #'gnus-following-method)
3431
3432 (set-alist 'mime-preview-over-to-previous-method-alist
3433            'gnus-original-article-mode
3434            (lambda ()
3435              (gnus-article-read-summary-keys
3436               nil (gnus-character-to-event ?P))))
3437
3438 (set-alist 'mime-preview-over-to-next-method-alist
3439            'gnus-original-article-mode'
3440            (lambda ()
3441              (gnus-article-read-summary-keys
3442               nil (gnus-character-to-event ?N))))
3443
3444
3445 ;;; @ end
3446 ;;;
3447
3448 (gnus-ems-redefine)
3449
3450 (provide 'gnus-art)
3451
3452 (run-hooks 'gnus-art-load-hook)
3453
3454 ;;; gnus-art.el ends here