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