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