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