* encrypt.el (encrypt-xor-process-buffer): Replace `string-to-int'
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
35 (eval-and-compile
36   (let ((recursive-load-depth-limit 100))
37     (require 'gnus-sum)))
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54
55 (defgroup gnus-article nil
56   "Article display."
57   :link '(custom-manual "(gnus)Article Buffer")
58   :group 'gnus)
59
60 (defgroup gnus-article-treat nil
61   "Treating article parts."
62   :link '(custom-manual "(gnus)Article Hiding")
63   :group 'gnus-article)
64
65 (defgroup gnus-article-hiding nil
66   "Hiding article parts."
67   :link '(custom-manual "(gnus)Article Hiding")
68   :group 'gnus-article)
69
70 (defgroup gnus-article-highlight nil
71   "Article highlighting."
72   :link '(custom-manual "(gnus)Article Highlighting")
73   :group 'gnus-article
74   :group 'gnus-visual)
75
76 (defgroup gnus-article-signature nil
77   "Article signatures."
78   :link '(custom-manual "(gnus)Article Signature")
79   :group 'gnus-article)
80
81 (defgroup gnus-article-headers nil
82   "Article headers."
83   :link '(custom-manual "(gnus)Hiding Headers")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-washing nil
87   "Special commands on articles."
88   :link '(custom-manual "(gnus)Article Washing")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-emphasis nil
92   "Fontisizing articles."
93   :link '(custom-manual "(gnus)Article Fontisizing")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-saving nil
97   "Saving articles."
98   :link '(custom-manual "(gnus)Saving Articles")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-mime nil
102   "Worshiping the MIME wonder."
103   :link '(custom-manual "(gnus)Using MIME")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-buttons nil
107   "Pushable buttons in the article buffer."
108   :link '(custom-manual "(gnus)Article Buttons")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-various nil
112   "Other article options."
113   :link '(custom-manual "(gnus)Misc Article")
114   :group 'gnus-article)
115
116 (defcustom gnus-ignored-headers
117   (mapcar
118    (lambda (header)
119      (concat "^" header ":"))
120    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
121      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
122      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
123      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
124      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
125      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
126      "X-Attribution" "X-Originating-IP" "Delivered-To"
127      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
128      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
129      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
130      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
131      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
132      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
133      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
134      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
135      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
136      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
137      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
138      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
139      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
140      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
141      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
142      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
143      "List-[A-Za-z]+" "X-Listprocessor-Version"
144      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
145      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
146      "X-Received" "Content-length" "X-precedence"
147      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
148      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
149      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
150      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
151      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
152      "X-Content-length" "X-Posting-Agent" "Original-Received"
153      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
154      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
155      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
156      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
157      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"))
158   "*All headers that start with this regexp will be hidden.
159 This variable can also be a list of regexps of headers to be ignored.
160 If `gnus-visible-headers' is non-nil, this variable will be ignored."
161   :type '(choice :custom-show nil
162                  regexp
163                  (repeat regexp))
164   :group 'gnus-article-hiding)
165
166 (defcustom gnus-visible-headers
167   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
168   "*All headers that do not match this regexp will be hidden.
169 This variable can also be a list of regexp of headers to remain visible.
170 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
171   :type '(repeat :value-to-internal (lambda (widget value)
172                                       (custom-split-regexp-maybe value))
173                  :match (lambda (widget value)
174                           (or (stringp value)
175                               (widget-editable-list-match widget value)))
176                  regexp)
177   :group 'gnus-article-hiding)
178
179 (defcustom gnus-sorted-header-list
180   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
181     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
182   "*This variable is a list of regular expressions.
183 If it is non-nil, headers that match the regular expressions will
184 be placed first in the article buffer in the sequence specified by
185 this list."
186   :type '(repeat regexp)
187   :group 'gnus-article-hiding)
188
189 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
190   "Headers that are only to be displayed if they have interesting data.
191 Possible values in this list are:
192
193   'empty       Headers with no content.
194   'newsgroups  Newsgroup identical to Gnus group.
195   'to-address  To identical to To-address.
196   'to-list     To identical to To-list.
197   'cc-list     CC identical to To-list.
198   'followup-to Followup-to identical to Newsgroups.
199   'reply-to    Reply-to identical to From.
200   'date        Date less than four days old.
201   'long-to     To and/or Cc longer than 1024 characters.
202   'many-to     Multiple To and/or Cc."
203   :type '(set (const :tag "Headers with no content." empty)
204               (const :tag "Newsgroups identical to Gnus group." newsgroups)
205               (const :tag "To identical to To-address." to-address)
206               (const :tag "To identical to To-list." to-list)
207               (const :tag "CC identical to To-list." cc-list)
208               (const :tag "Followup-to identical to Newsgroups." followup-to)
209               (const :tag "Reply-to identical to From." reply-to)
210               (const :tag "Date less than four days old." date)
211               (const :tag "To and/or Cc longer than 1024 characters." long-to)
212               (const :tag "Multiple To and/or Cc headers." many-to))
213   :group 'gnus-article-hiding)
214
215 (defcustom gnus-article-skip-boring nil
216   "Skip over text that is not worth reading.
217 By default, if you set this t, then Gnus will display citations and
218 signatures, but will never scroll down to show you a page consisting
219 only of boring text.  Boring text is controlled by
220 `gnus-article-boring-faces'."
221   :version "22.1"
222   :type 'boolean
223   :group 'gnus-article-hiding)
224
225 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
226   "Regexp matching signature separator.
227 This can also be a list of regexps.  In that case, it will be checked
228 from head to tail looking for a separator.  Searches will be done from
229 the end of the buffer."
230   :type '(repeat string)
231   :group 'gnus-article-signature)
232
233 (defcustom gnus-signature-limit nil
234   "Provide a limit to what is considered a signature.
235 If it is a number, no signature may not be longer (in characters) than
236 that number.  If it is a floating point number, no signature may be
237 longer (in lines) than that number.  If it is a function, the function
238 will be called without any parameters, and if it returns nil, there is
239 no signature in the buffer.  If it is a string, it will be used as a
240 regexp.  If it matches, the text in question is not a signature.
241
242 This can also be a list of the above values."
243   :type '(choice (integer :value 200)
244                  (number :value 4.0)
245                  (function :value fun)
246                  (regexp :value ".*"))
247   :group 'gnus-article-signature)
248
249 (defcustom gnus-hidden-properties '(invisible t intangible t)
250   "Property list to use for hiding text."
251   :type 'sexp
252   :group 'gnus-article-hiding)
253
254 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
255 ;; frames in a session.
256 (defcustom gnus-article-x-face-command
257   (if (featurep 'xemacs)
258       (if (or (gnus-image-type-available-p 'xface)
259               (gnus-image-type-available-p 'pbm))
260           'gnus-display-x-face-in-from
261         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
262     (if (gnus-image-type-available-p 'pbm)
263         'gnus-display-x-face-in-from
264       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
265 display -"))
266   "*String or function to be executed to display an X-Face header.
267 If it is a string, the command will be executed in a sub-shell
268 asynchronously.  The compressed face will be piped to this command."
269   :type `(choice string
270                  (function-item gnus-display-x-face-in-from)
271                  function)
272   :version "21.1"
273   :group 'gnus-picon
274   :group 'gnus-article-washing)
275
276 (defcustom gnus-article-x-face-too-ugly nil
277   "Regexp matching posters whose face shouldn't be shown automatically."
278   :type '(choice regexp (const nil))
279   :group 'gnus-article-washing)
280
281 (defcustom gnus-article-banner-alist nil
282   "Banner alist for stripping.
283 For example,
284      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
285   :version "21.1"
286   :type '(repeat (cons symbol regexp))
287   :group 'gnus-article-washing)
288
289 (gnus-define-group-parameter
290  banner
291  :variable-document
292  "Alist of regexps (to match group names) and banner."
293  :variable-group gnus-article-washing
294  :parameter-type
295  '(choice :tag "Banner"
296           :value nil
297           (const :tag "Remove signature" signature)
298           (symbol :tag "Item in `gnus-article-banner-alist'" none)
299           regexp
300           (const :tag "None" nil))
301  :parameter-document
302  "If non-nil, specify how to remove `banners' from articles.
303
304 Symbol `signature' means to remove signatures delimited by
305 `gnus-signature-separator'.  Any other symbol is used to look up a
306 regular expression to match the banner in `gnus-article-banner-alist'.
307 A string is used as a regular expression to match the banner
308 directly.")
309
310 (defcustom gnus-article-address-banner-alist nil
311   "Alist of mail addresses and banners.
312 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
313 to match a mail address in the From: header, BANNER is one of a symbol
314 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
315 If ADDRESS matches author's mail address, it will remove things like
316 advertisements.  For example:
317
318 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
319 "
320   :type '(repeat
321           (cons
322            (regexp :tag "Address")
323            (choice :tag "Banner" :value nil
324                    (const :tag "Remove signature" signature)
325                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
326                    regexp
327                    (const :tag "None" nil))))
328   :version "22.1"
329   :group 'gnus-article-washing)
330
331 (defmacro gnus-emphasis-custom-with-format (&rest body)
332   `(let ((format "\
333 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
334 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
335      ,@body))
336
337 (defun gnus-emphasis-custom-value-to-external (value)
338   (gnus-emphasis-custom-with-format
339    (if (consp (car value))
340        (list (format format (car (car value)) (cdr (car value)))
341              2
342              (if (nth 1 value) 2 3)
343              (nth 2 value))
344      value)))
345
346 (defun gnus-emphasis-custom-value-to-internal (value)
347   (gnus-emphasis-custom-with-format
348    (let ((regexp (concat "\\`"
349                          (format (regexp-quote format)
350                                  "\\([^()]+\\)" "\\([^()]+\\)")
351                          "\\'"))
352          pattern)
353      (if (string-match regexp (setq pattern (car value)))
354          (list (cons (match-string 1 pattern) (match-string 2 pattern))
355                (= (nth 2 value) 2)
356                (nth 3 value))
357        value))))
358
359 (defcustom gnus-emphasis-alist
360   (let ((types
361          '(("\\*" "\\*" bold nil 2)
362            ("_" "_" underline)
363            ("/" "/" italic)
364            ("_/" "/_" underline-italic)
365            ("_\\*" "\\*_" underline-bold)
366            ("\\*/" "/\\*" bold-italic)
367            ("_\\*/" "/\\*_" underline-bold-italic))))
368     (nconc
369      (gnus-emphasis-custom-with-format
370       (mapcar (lambda (spec)
371                 (list (format format (car spec) (cadr spec))
372                       (or (nth 3 spec) 2)
373                       (or (nth 4 spec) 3)
374                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
375               types))
376      '(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
377         2 3 gnus-emphasis-underline))))
378   "*Alist that says how to fontify certain phrases.
379 Each item looks like this:
380
381   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
382
383 The first element is a regular expression to be matched.  The second
384 is a number that says what regular expression grouping used to find
385 the entire emphasized word.  The third is a number that says what
386 regexp grouping should be displayed and highlighted.  The fourth
387 is the face used for highlighting."
388   :type
389   '(repeat
390     (menu-choice
391      :format "%[Customizing Style%]\n%v"
392      :indent 2
393      (group :tag "Default"
394             :value ("" 0 0 default)
395             :value-create
396             (lambda (widget)
397               (let ((value (widget-get
398                             (cadr (widget-get (widget-get widget :parent)
399                                               :args))
400                             :value)))
401                 (if (not (eq (nth 2 value) 'default))
402                     (widget-put
403                      widget
404                      :value
405                      (gnus-emphasis-custom-value-to-external value))))
406               (widget-group-value-create widget))
407             regexp
408             (integer :format "Match group: %v")
409             (integer :format "Emphasize group: %v")
410             face)
411      (group :tag "Simple"
412             :value (("_" . "_") nil default)
413             (cons :format "%v"
414                   (regexp :format "Start regexp: %v")
415                   (regexp :format "End regexp: %v"))
416             (boolean :format "Show start and end patterns: %[%v%]\n"
417                      :on " On " :off " Off ")
418             face)))
419   :get (lambda (symbol)
420          (mapcar 'gnus-emphasis-custom-value-to-internal
421                  (default-value symbol)))
422   :set (lambda (symbol value)
423          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
424                                      value)))
425   :group 'gnus-article-emphasis)
426
427 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
428   "A regexp to describe whitespace which should not be emphasized.
429 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
430 The former avoids underlining of leading and trailing whitespace,
431 and the latter avoids underlining any whitespace at all."
432   :version "21.1"
433   :group 'gnus-article-emphasis
434   :type 'regexp)
435
436 (defface gnus-emphasis-bold '((t (:bold t)))
437   "Face used for displaying strong emphasized text (*word*)."
438   :group 'gnus-article-emphasis)
439
440 (defface gnus-emphasis-italic '((t (:italic t)))
441   "Face used for displaying italic emphasized text (/word/)."
442   :group 'gnus-article-emphasis)
443
444 (defface gnus-emphasis-underline '((t (:underline t)))
445   "Face used for displaying underlined emphasized text (_word_)."
446   :group 'gnus-article-emphasis)
447
448 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
449   "Face used for displaying underlined bold emphasized text (_*word*_)."
450   :group 'gnus-article-emphasis)
451
452 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
453   "Face used for displaying underlined italic emphasized text (_/word/_)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
457   "Face used for displaying bold italic emphasized text (/*word*/)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline-bold-italic
461   '((t (:bold t :italic t :underline t)))
462   "Face used for displaying underlined bold italic emphasized text.
463 Example: (_/*word*/_)."
464   :group 'gnus-article-emphasis)
465
466 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
467                                       '((t (:strikethru t)))
468                                     '((t (:strike-through t))))
469   "Face used for displaying strike-through text (-word-)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-highlight-words
473   '((t (:background "black" :foreground "yellow")))
474   "Face used for displaying highlighted words."
475   :group 'gnus-article-emphasis)
476
477 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
478   "Format for display of Date headers in article bodies.
479 See `format-time-string' for the possible values.
480
481 The variable can also be function, which should return a complete Date
482 header.  The function is called with one argument, the time, which can
483 be fed to `format-time-string'."
484   :type '(choice string function)
485   :link '(custom-manual "(gnus)Article Date")
486   :group 'gnus-article-washing)
487
488 (defcustom gnus-save-all-headers t
489   "*If non-nil, don't remove any headers before saving."
490   :group 'gnus-article-saving
491   :type 'boolean)
492
493 (defcustom gnus-prompt-before-saving 'always
494   "*This variable says how much prompting is to be done when saving articles.
495 If it is nil, no prompting will be done, and the articles will be
496 saved to the default files.  If this variable is `always', each and
497 every article that is saved will be preceded by a prompt, even when
498 saving large batches of articles.  If this variable is neither nil not
499 `always', there the user will be prompted once for a file name for
500 each invocation of the saving commands."
501   :group 'gnus-article-saving
502   :type '(choice (item always)
503                  (item :tag "never" nil)
504                  (sexp :tag "once" :format "%t\n" :value t)))
505
506 (defcustom gnus-saved-headers gnus-visible-headers
507   "Headers to keep if `gnus-save-all-headers' is nil.
508 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
509 If that variable is nil, however, all headers that match this regexp
510 will be kept while the rest will be deleted before saving."
511   :group 'gnus-article-saving
512   :type 'regexp)
513
514 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
515   "A function to save articles in your favourite format.
516 The function must be interactively callable (in other words, it must
517 be an Emacs command).
518
519 Gnus provides the following functions:
520
521 * gnus-summary-save-in-rmail (Rmail format)
522 * gnus-summary-save-in-mail (Unix mail format)
523 * gnus-summary-save-in-folder (MH folder)
524 * gnus-summary-save-in-file (article format)
525 * gnus-summary-save-body-in-file (article body)
526 * gnus-summary-save-in-vm (use VM's folder format)
527 * gnus-summary-write-to-file (article format -- overwrite)."
528   :group 'gnus-article-saving
529   :type '(radio (function-item gnus-summary-save-in-rmail)
530                 (function-item gnus-summary-save-in-mail)
531                 (function-item gnus-summary-save-in-folder)
532                 (function-item gnus-summary-save-in-file)
533                 (function-item gnus-summary-save-body-in-file)
534                 (function-item gnus-summary-save-in-vm)
535                 (function-item gnus-summary-write-to-file)))
536
537 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
538   "A function generating a file name to save articles in Rmail format.
539 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
540   :group 'gnus-article-saving
541   :type 'function)
542
543 (defcustom gnus-mail-save-name 'gnus-plain-save-name
544   "A function generating a file name to save articles in Unix mail format.
545 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
546   :group 'gnus-article-saving
547   :type 'function)
548
549 (defcustom gnus-folder-save-name 'gnus-folder-save-name
550   "A function generating a file name to save articles in MH folder.
551 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
552   :group 'gnus-article-saving
553   :type 'function)
554
555 (defcustom gnus-file-save-name 'gnus-numeric-save-name
556   "A function generating a file name to save articles in article format.
557 The function is called with NEWSGROUP, HEADERS, and optional
558 LAST-FILE."
559   :group 'gnus-article-saving
560   :type 'function)
561
562 (defcustom gnus-split-methods
563   '((gnus-article-archive-name)
564     (gnus-article-nndoc-name))
565   "*Variable used to suggest where articles are to be saved.
566 For instance, if you would like to save articles related to Gnus in
567 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
568 you could set this variable to something like:
569
570  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
571    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
572
573 This variable is an alist where the key is the match and the
574 value is a list of possible files to save in if the match is
575 non-nil.
576
577 If the match is a string, it is used as a regexp match on the
578 article.  If the match is a symbol, that symbol will be funcalled
579 from the buffer of the article to be saved with the newsgroup as
580 the parameter.  If it is a list, it will be evaled in the same
581 buffer.
582
583 If this form or function returns a string, this string will be
584 used as a possible file name; and if it returns a non-nil list,
585 that list will be used as possible file names."
586   :group 'gnus-article-saving
587   :type '(repeat (choice (list :value (fun) function)
588                          (cons :value ("" "") regexp (repeat string))
589                          (sexp :value nil))))
590
591 (defcustom gnus-page-delimiter "^\^L"
592   "*Regexp describing what to use as article page delimiters.
593 The default value is \"^\^L\", which is a form linefeed at the
594 beginning of a line."
595   :type 'regexp
596   :group 'gnus-article-various)
597
598 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
599   "*The format specification for the article mode line.
600 See `gnus-summary-mode-line-format' for a closer description.
601
602 The following additional specs are available:
603
604 %w  The article washing status.
605 %m  The number of MIME parts in the article."
606   :type 'string
607   :group 'gnus-article-various)
608
609 (defcustom gnus-article-mode-hook nil
610   "*A hook for Gnus article mode."
611   :type 'hook
612   :group 'gnus-article-various)
613
614 (when (featurep 'xemacs)
615   ;; Extracted from gnus-xmas-define in order to preserve user settings
616   (when (fboundp 'turn-off-scroll-in-place)
617     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
618   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
619   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
620
621 (defcustom gnus-article-menu-hook nil
622   "*Hook run after the creation of the article mode menu."
623   :type 'hook
624   :group 'gnus-article-various)
625
626 (defcustom gnus-article-prepare-hook nil
627   "*A hook called after an article has been prepared in the article buffer."
628   :type 'hook
629   :group 'gnus-article-various)
630
631 (defcustom gnus-copy-article-ignored-headers nil
632   "List of headers to be removed when copying an article.
633 Each element is a regular expression."
634   :version "23.0" ;; No Gnus
635   :type '(repeat regexp)
636   :group 'gnus-article-various)
637
638 (make-obsolete-variable 'gnus-article-hide-pgp-hook
639                         "This variable is obsolete in Gnus 5.10.")
640
641 (defcustom gnus-article-button-face 'bold
642   "Face used for highlighting buttons in the article buffer.
643
644 An article button is a piece of text that you can activate by pressing
645 `RET' or `mouse-2' above it."
646   :type 'face
647   :group 'gnus-article-buttons)
648
649 (defcustom gnus-article-mouse-face 'highlight
650   "Face used for mouse highlighting in the article buffer.
651
652 Article buttons will be displayed in this face when the cursor is
653 above them."
654   :type 'face
655   :group 'gnus-article-buttons)
656
657 (defcustom gnus-signature-face 'gnus-signature-face
658   "Face used for highlighting a signature in the article buffer.
659 Obsolete; use the face `gnus-signature-face' for customizations instead."
660   :type 'face
661   :group 'gnus-article-highlight
662   :group 'gnus-article-signature)
663
664 (defface gnus-signature-face
665   '((t
666      (:italic t)))
667   "Face used for highlighting a signature in the article buffer."
668   :group 'gnus-article-highlight
669   :group 'gnus-article-signature)
670
671 (defface gnus-header-from-face
672   '((((class color)
673       (background dark))
674      (:foreground "spring green"))
675     (((class color)
676       (background light))
677      (:foreground "red3"))
678     (t
679      (:italic t)))
680   "Face used for displaying from headers."
681   :group 'gnus-article-headers
682   :group 'gnus-article-highlight)
683
684 (defface gnus-header-subject-face
685   '((((class color)
686       (background dark))
687      (:foreground "SeaGreen3"))
688     (((class color)
689       (background light))
690      (:foreground "red4"))
691     (t
692      (:bold t :italic t)))
693   "Face used for displaying subject headers."
694   :group 'gnus-article-headers
695   :group 'gnus-article-highlight)
696
697 (defface gnus-header-newsgroups-face
698   '((((class color)
699       (background dark))
700      (:foreground "yellow" :italic t))
701     (((class color)
702       (background light))
703      (:foreground "MidnightBlue" :italic t))
704     (t
705      (:italic t)))
706   "Face used for displaying newsgroups headers.
707 In the default setup this face is only used for crossposted
708 articles."
709   :group 'gnus-article-headers
710   :group 'gnus-article-highlight)
711
712 (defface gnus-header-name-face
713   '((((class color)
714       (background dark))
715      (:foreground "SeaGreen"))
716     (((class color)
717       (background light))
718      (:foreground "maroon"))
719     (t
720      (:bold t)))
721   "Face used for displaying header names."
722   :group 'gnus-article-headers
723   :group 'gnus-article-highlight)
724
725 (defface gnus-header-content-face
726   '((((class color)
727       (background dark))
728      (:foreground "forest green" :italic t))
729     (((class color)
730       (background light))
731      (:foreground "indianred4" :italic t))
732     (t
733      (:italic t)))  "Face used for displaying header content."
734   :group 'gnus-article-headers
735   :group 'gnus-article-highlight)
736
737 (defcustom gnus-header-face-alist
738   '(("From" nil gnus-header-from-face)
739     ("Subject" nil gnus-header-subject-face)
740     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
741     ("" gnus-header-name-face gnus-header-content-face))
742   "*Controls highlighting of article headers.
743
744 An alist of the form (HEADER NAME CONTENT).
745
746 HEADER is a regular expression which should match the name of a
747 header and NAME and CONTENT are either face names or nil.
748
749 The name of each header field will be displayed using the face
750 specified by the first element in the list where HEADER matches
751 the header name and NAME is non-nil.  Similarly, the content will
752 be displayed by the first non-nil matching CONTENT face."
753   :group 'gnus-article-headers
754   :group 'gnus-article-highlight
755   :type '(repeat (list (regexp :tag "Header")
756                        (choice :tag "Name"
757                                (item :tag "skip" nil)
758                                (face :value default))
759                        (choice :tag "Content"
760                                (item :tag "skip" nil)
761                                (face :value default)))))
762
763 (defcustom gnus-article-decode-hook
764   '(article-decode-charset article-decode-encoded-words
765                            article-decode-group-name article-decode-idna-rhs)
766   "*Hook run to decode charsets in articles."
767   :group 'gnus-article-headers
768   :type 'hook)
769
770 (defcustom gnus-display-mime-function 'gnus-display-mime
771   "Function to display MIME articles."
772   :group 'gnus-article-mime
773   :type 'function)
774
775 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
776   "Function used to decode headers.")
777
778 (defvar gnus-article-dumbquotes-map
779   '(("\200" "EUR")
780     ("\202" ",")
781     ("\203" "f")
782     ("\204" ",,")
783     ("\205" "...")
784     ("\213" "<")
785     ("\214" "OE")
786     ("\221" "`")
787     ("\222" "'")
788     ("\223" "``")
789     ("\224" "\"")
790     ("\225" "*")
791     ("\226" "-")
792     ("\227" "--")
793     ("\230" "~")
794     ("\231" "(TM)")
795     ("\233" ">")
796     ("\234" "oe")
797     ("\264" "'"))
798   "Table for MS-to-Latin1 translation.")
799
800 (defcustom gnus-ignored-mime-types nil
801   "List of MIME types that should be ignored by Gnus."
802   :version "21.1"
803   :group 'gnus-article-mime
804   :type '(repeat regexp))
805
806 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
807   "List of MIME types that should not be given buttons when rendered inline.
808 See also `gnus-buttonized-mime-types' which may override this variable.
809 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
810   :version "21.1"
811   :group 'gnus-article-mime
812   :type '(repeat regexp))
813
814 (defcustom gnus-buttonized-mime-types nil
815   "List of MIME types that should be given buttons when rendered inline.
816 If set, this variable overrides `gnus-unbuttonized-mime-types'.
817 To see e.g. security buttons you could set this to
818 `(\"multipart/signed\")'.
819 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
820   :version "22.1"
821   :group 'gnus-article-mime
822   :type '(repeat regexp))
823
824 (defcustom gnus-inhibit-mime-unbuttonizing nil
825   "If non-nil, all MIME parts get buttons.
826 When nil (the default value), then some MIME parts do not get buttons,
827 as described by the variables `gnus-buttonized-mime-types' and
828 `gnus-unbuttonized-mime-types'."
829   :version "22.1"
830   :group 'gnus-article-mime
831   :type 'boolean)
832
833 (defcustom gnus-body-boundary-delimiter "_"
834   "String used to delimit header and body.
835 This variable is used by `gnus-article-treat-body-boundary' which can
836 be controlled by `gnus-treat-body-boundary'."
837   :version "22.1"
838   :group 'gnus-article-various
839   :type '(choice (item :tag "None" :value nil)
840                  string))
841
842 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
843                                   "/usr/share/picons")
844   "Defines the location of the faces database.
845 For information on obtaining this database of pretty pictures, please
846 see http://www.cs.indiana.edu/picons/ftp/index.html"
847   :version "22.1"
848   :type '(repeat directory)
849   :link '(url-link :tag "download"
850                    "http://www.cs.indiana.edu/picons/ftp/index.html")
851   :link '(custom-manual "(gnus)Picons")
852   :group 'gnus-picon)
853
854 (defun gnus-picons-installed-p ()
855   "Say whether picons are installed on your machine."
856   (let ((installed nil))
857     (dolist (database gnus-picon-databases)
858       (when (file-exists-p database)
859         (setq installed t)))
860     installed))
861
862 (defcustom gnus-article-mime-part-function nil
863   "Function called with a MIME handle as the argument.
864 This is meant for people who want to do something automatic based
865 on parts -- for instance, adding Vcard info to a database."
866   :group 'gnus-article-mime
867   :type 'function)
868
869 (defcustom gnus-mime-multipart-functions nil
870   "An alist of MIME types to functions to display them."
871   :version "21.1"
872   :group 'gnus-article-mime
873   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
874
875 (defcustom gnus-article-date-lapsed-new-header nil
876   "Whether the X-Sent and Date headers can coexist.
877 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
878 either replace the old \"Date:\" header (if this variable is nil), or
879 be added below it (otherwise)."
880   :version "21.1"
881   :group 'gnus-article-headers
882   :type 'boolean)
883
884 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
885   "Function called with a MIME handle as the argument.
886 This is meant for people who want to view first matched part.
887 For `undisplayed-alternative' (default), the first undisplayed
888 part or alternative part is used.  For `undisplayed', the first
889 undisplayed part is used.  For a function, the first part which
890 the function return t is used.  For nil, the first part is
891 used."
892   :version "21.1"
893   :group 'gnus-article-mime
894   :type '(choice
895           (item :tag "first" :value nil)
896           (item :tag "undisplayed" :value undisplayed)
897           (item :tag "undisplayed or alternative"
898                 :value undisplayed-alternative)
899           (function)))
900
901 (defcustom gnus-mime-action-alist
902   '(("save to file" . gnus-mime-save-part)
903     ("save and strip" . gnus-mime-save-part-and-strip)
904     ("delete part" . gnus-mime-delete-part)
905     ("display as text" . gnus-mime-inline-part)
906     ("view the part" . gnus-mime-view-part)
907     ("pipe to command" . gnus-mime-pipe-part)
908     ("toggle display" . gnus-article-press-button)
909     ("toggle display" . gnus-article-view-part-as-charset)
910     ("view as type" . gnus-mime-view-part-as-type)
911     ("view internally" . gnus-mime-view-part-internally)
912     ("view externally" . gnus-mime-view-part-externally))
913   "An alist of actions that run on the MIME attachment."
914   :group 'gnus-article-mime
915   :type '(repeat (cons (string :tag "name")
916                        (function))))
917
918 ;;;
919 ;;; The treatment variables
920 ;;;
921
922 (defvar gnus-part-display-hook nil
923   "Hook called on parts that are to receive treatment.")
924
925 (defvar gnus-article-treat-custom
926   '(choice (const :tag "Off" nil)
927            (const :tag "On" t)
928            (const :tag "Header" head)
929            (const :tag "Last" last)
930            (integer :tag "Less")
931            (repeat :tag "Groups" regexp)
932            (sexp :tag "Predicate")))
933
934 (defvar gnus-article-treat-head-custom
935   '(choice (const :tag "Off" nil)
936            (const :tag "Header" head)))
937
938 (defvar gnus-article-treat-types '("text/plain")
939   "Parts to treat.")
940
941 (defvar gnus-inhibit-treatment nil
942   "Whether to inhibit treatment.")
943
944 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
945   "Highlight the signature.
946 Valid values are nil, t, `head', `last', an integer or a predicate.
947 See Info node `(gnus)Customizing Articles'."
948   :group 'gnus-article-treat
949   :link '(custom-manual "(gnus)Customizing Articles")
950   :type gnus-article-treat-custom)
951 (put 'gnus-treat-highlight-signature 'highlight t)
952
953 (defcustom gnus-treat-buttonize 100000
954   "Add buttons.
955 Valid values are nil, t, `head', `last', an integer or a predicate.
956 See Info node `(gnus)Customizing Articles'."
957   :group 'gnus-article-treat
958   :link '(custom-manual "(gnus)Customizing Articles")
959   :type gnus-article-treat-custom)
960 (put 'gnus-treat-buttonize 'highlight t)
961
962 (defcustom gnus-treat-buttonize-head 'head
963   "Add buttons to the head.
964 Valid values are nil, t, `head', `last', an integer or a predicate.
965 See Info node `(gnus)Customizing Articles' for details."
966   :group 'gnus-article-treat
967   :link '(custom-manual "(gnus)Customizing Articles")
968   :type gnus-article-treat-head-custom)
969 (put 'gnus-treat-buttonize-head 'highlight t)
970
971 (defcustom gnus-treat-emphasize
972   (and (or window-system
973            (featurep 'xemacs))
974        50000)
975   "Emphasize text.
976 Valid values are nil, t, `head', `last', an integer or a predicate.
977 See Info node `(gnus)Customizing Articles' for details."
978   :group 'gnus-article-treat
979   :link '(custom-manual "(gnus)Customizing Articles")
980   :type gnus-article-treat-custom)
981 (put 'gnus-treat-emphasize 'highlight t)
982
983 (defcustom gnus-treat-strip-cr nil
984   "Remove carriage returns.
985 Valid values are nil, t, `head', `last', an integer or a predicate.
986 See Info node `(gnus)Customizing Articles' for details."
987   :version "22.1"
988   :group 'gnus-article-treat
989   :link '(custom-manual "(gnus)Customizing Articles")
990   :type gnus-article-treat-custom)
991
992 (defcustom gnus-treat-unsplit-urls nil
993   "Remove newlines from within URLs.
994 Valid values are nil, t, `head', `last', an integer or a predicate.
995 See Info node `(gnus)Customizing Articles' for details."
996   :version "22.1"
997   :group 'gnus-article-treat
998   :link '(custom-manual "(gnus)Customizing Articles")
999   :type gnus-article-treat-custom)
1000
1001 (defcustom gnus-treat-leading-whitespace nil
1002   "Remove leading whitespace in headers.
1003 Valid values are nil, t, `head', `last', an integer or a predicate.
1004 See Info node `(gnus)Customizing Articles' for details."
1005   :version "22.1"
1006   :group 'gnus-article-treat
1007   :link '(custom-manual "(gnus)Customizing Articles")
1008   :type gnus-article-treat-custom)
1009
1010 (defcustom gnus-treat-hide-headers 'head
1011   "Hide headers.
1012 Valid values are nil, t, `head', `last', an integer or a predicate.
1013 See Info node `(gnus)Customizing Articles' for details."
1014   :group 'gnus-article-treat
1015   :link '(custom-manual "(gnus)Customizing Articles")
1016   :type gnus-article-treat-head-custom)
1017
1018 (defcustom gnus-treat-hide-boring-headers nil
1019   "Hide boring headers.
1020 Valid values are nil, t, `head', `last', an integer or a predicate.
1021 See Info node `(gnus)Customizing Articles' for details."
1022   :group 'gnus-article-treat
1023   :link '(custom-manual "(gnus)Customizing Articles")
1024   :type gnus-article-treat-head-custom)
1025
1026 (defcustom gnus-treat-hide-signature nil
1027   "Hide the signature.
1028 Valid values are nil, t, `head', `last', an integer or a predicate.
1029 See Info node `(gnus)Customizing Articles' for details."
1030   :group 'gnus-article-treat
1031   :link '(custom-manual "(gnus)Customizing Articles")
1032   :type gnus-article-treat-custom)
1033
1034 (defcustom gnus-treat-fill-article nil
1035   "Fill the article.
1036 Valid values are nil, t, `head', `last', an integer or a predicate.
1037 See Info node `(gnus)Customizing Articles' for details."
1038   :group 'gnus-article-treat
1039   :link '(custom-manual "(gnus)Customizing Articles")
1040   :type gnus-article-treat-custom)
1041
1042 (defcustom gnus-treat-hide-citation nil
1043   "Hide cited text.
1044 Valid values are nil, t, `head', `last', an integer or a predicate.
1045 See Info node `(gnus)Customizing Articles' for details."
1046   :group 'gnus-article-treat
1047   :link '(custom-manual "(gnus)Customizing Articles")
1048   :type gnus-article-treat-custom)
1049
1050 (defcustom gnus-treat-hide-citation-maybe nil
1051   "Hide cited text.
1052 Valid values are nil, t, `head', `last', an integer or a predicate.
1053 See Info node `(gnus)Customizing Articles' for details."
1054   :group 'gnus-article-treat
1055   :link '(custom-manual "(gnus)Customizing Articles")
1056   :type gnus-article-treat-custom)
1057
1058 (defcustom gnus-treat-strip-list-identifiers 'head
1059   "Strip list identifiers from `gnus-list-identifiers`.
1060 Valid values are nil, t, `head', `last', an integer or a predicate.
1061 See Info node `(gnus)Customizing Articles' for details."
1062   :version "21.1"
1063   :group 'gnus-article-treat
1064   :link '(custom-manual "(gnus)Customizing Articles")
1065   :type gnus-article-treat-custom)
1066
1067 (make-obsolete-variable 'gnus-treat-strip-pgp
1068                         "This option is obsolete in Gnus 5.10.")
1069
1070 (defcustom gnus-treat-strip-pem nil
1071   "Strip PEM signatures.
1072 Valid values are nil, t, `head', `last', an integer or a predicate.
1073 See Info node `(gnus)Customizing Articles' for details."
1074   :group 'gnus-article-treat
1075   :link '(custom-manual "(gnus)Customizing Articles")
1076   :type gnus-article-treat-custom)
1077
1078 (defcustom gnus-treat-strip-banner t
1079   "Strip banners from articles.
1080 The banner to be stripped is specified in the `banner' group parameter.
1081 Valid values are nil, t, `head', `last', an integer or a predicate.
1082 See Info node `(gnus)Customizing Articles' for details."
1083   :group 'gnus-article-treat
1084   :link '(custom-manual "(gnus)Customizing Articles")
1085   :type gnus-article-treat-custom)
1086
1087 (defcustom gnus-treat-highlight-headers 'head
1088   "Highlight the headers.
1089 Valid values are nil, t, `head', `last', an integer or a predicate.
1090 See Info node `(gnus)Customizing Articles' for details."
1091   :group 'gnus-article-treat
1092   :link '(custom-manual "(gnus)Customizing Articles")
1093   :type gnus-article-treat-head-custom)
1094 (put 'gnus-treat-highlight-headers 'highlight t)
1095
1096 (defcustom gnus-treat-highlight-citation t
1097   "Highlight cited text.
1098 Valid values are nil, t, `head', `last', an integer or a predicate.
1099 See Info node `(gnus)Customizing Articles' for details."
1100   :group 'gnus-article-treat
1101   :link '(custom-manual "(gnus)Customizing Articles")
1102   :type gnus-article-treat-custom)
1103 (put 'gnus-treat-highlight-citation 'highlight t)
1104
1105 (defcustom gnus-treat-date-ut nil
1106   "Display the Date in UT (GMT).
1107 Valid values are nil, t, `head', `last', an integer or a predicate.
1108 See Info node `(gnus)Customizing Articles' for details."
1109   :group 'gnus-article-treat
1110   :link '(custom-manual "(gnus)Customizing Articles")
1111   :type gnus-article-treat-head-custom)
1112
1113 (defcustom gnus-treat-date-local nil
1114   "Display the Date in the local timezone.
1115 Valid values are nil, t, `head', `last', an integer or a predicate.
1116 See Info node `(gnus)Customizing Articles' for details."
1117   :group 'gnus-article-treat
1118   :link '(custom-manual "(gnus)Customizing Articles")
1119   :type gnus-article-treat-head-custom)
1120
1121 (defcustom gnus-treat-date-english nil
1122   "Display the Date in a format that can be read aloud in English.
1123 Valid values are nil, t, `head', `last', an integer or a predicate.
1124 See Info node `(gnus)Customizing Articles' for details."
1125   :version "22.1"
1126   :group 'gnus-article-treat
1127   :link '(custom-manual "(gnus)Customizing Articles")
1128   :type gnus-article-treat-head-custom)
1129
1130 (defcustom gnus-treat-date-lapsed nil
1131   "Display the Date header in a way that says how much time has elapsed.
1132 Valid values are nil, t, `head', `last', an integer or a predicate.
1133 See Info node `(gnus)Customizing Articles' for details."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-head-custom)
1137
1138 (defcustom gnus-treat-date-original nil
1139   "Display the date in the original timezone.
1140 Valid values are nil, t, `head', `last', an integer or a predicate.
1141 See Info node `(gnus)Customizing Articles' for details."
1142   :group 'gnus-article-treat
1143   :link '(custom-manual "(gnus)Customizing Articles")
1144   :type gnus-article-treat-head-custom)
1145
1146 (defcustom gnus-treat-date-iso8601 nil
1147   "Display the date in the ISO8601 format.
1148 Valid values are nil, t, `head', `last', an integer or a predicate.
1149 See Info node `(gnus)Customizing Articles' for details."
1150   :version "21.1"
1151   :group 'gnus-article-treat
1152   :link '(custom-manual "(gnus)Customizing Articles")
1153   :type gnus-article-treat-head-custom)
1154
1155 (defcustom gnus-treat-date-user-defined nil
1156   "Display the date in a user-defined format.
1157 The format is defined by the `gnus-article-time-format' variable.
1158 Valid values are nil, t, `head', `last', an integer or a predicate.
1159 See Info node `(gnus)Customizing Articles' for details."
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-head-custom)
1163
1164 (defcustom gnus-treat-strip-headers-in-body t
1165   "Strip the X-No-Archive header line from the beginning of the body.
1166 Valid values are nil, t, `head', `last', an integer or a predicate.
1167 See Info node `(gnus)Customizing Articles' for details."
1168   :version "21.1"
1169   :group 'gnus-article-treat
1170   :link '(custom-manual "(gnus)Customizing Articles")
1171   :type gnus-article-treat-custom)
1172
1173 (defcustom gnus-treat-strip-trailing-blank-lines nil
1174   "Strip trailing blank lines.
1175 Valid values are nil, t, `head', `last', an integer or a predicate.
1176 See Info node `(gnus)Customizing Articles' for details."
1177   :group 'gnus-article-treat
1178   :link '(custom-manual "(gnus)Customizing Articles")
1179   :type gnus-article-treat-custom)
1180
1181 (defcustom gnus-treat-strip-leading-blank-lines nil
1182   "Strip leading blank lines.
1183 Valid values are nil, t, `head', `last', an integer or a predicate.
1184 See Info node `(gnus)Customizing Articles' for details."
1185   :group 'gnus-article-treat
1186   :link '(custom-manual "(gnus)Customizing Articles")
1187   :type gnus-article-treat-custom)
1188
1189 (defcustom gnus-treat-strip-multiple-blank-lines nil
1190   "Strip multiple blank lines.
1191 Valid values are nil, t, `head', `last', an integer or a predicate.
1192 See Info node `(gnus)Customizing Articles' for details."
1193   :group 'gnus-article-treat
1194   :link '(custom-manual "(gnus)Customizing Articles")
1195   :type gnus-article-treat-custom)
1196
1197 (defcustom gnus-treat-unfold-headers 'head
1198   "Unfold folded header lines.
1199 Valid values are nil, t, `head', `last', an integer or a predicate.
1200 See Info node `(gnus)Customizing Articles' for details."
1201   :version "22.1"
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-fold-headers nil
1207   "Fold headers.
1208 Valid values are nil, t, `head', `last', an integer or a predicate.
1209 See Info node `(gnus)Customizing Articles' for details."
1210   :version "22.1"
1211   :group 'gnus-article-treat
1212   :link '(custom-manual "(gnus)Customizing Articles")
1213   :type gnus-article-treat-custom)
1214
1215 (defcustom gnus-treat-fold-newsgroups 'head
1216   "Fold the Newsgroups and Followup-To headers.
1217 Valid values are nil, t, `head', `last', an integer or a predicate.
1218 See Info node `(gnus)Customizing Articles' for details."
1219   :version "22.1"
1220   :group 'gnus-article-treat
1221   :link '(custom-manual "(gnus)Customizing Articles")
1222   :type gnus-article-treat-custom)
1223
1224 (defcustom gnus-treat-overstrike t
1225   "Treat overstrike highlighting.
1226 Valid values are nil, t, `head', `last', an integer or a predicate.
1227 See Info node `(gnus)Customizing Articles' for details."
1228   :group 'gnus-article-treat
1229   :link '(custom-manual "(gnus)Customizing Articles")
1230   :type gnus-article-treat-custom)
1231 (put 'gnus-treat-overstrike 'highlight t)
1232
1233 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1234   "Treat ANSI SGR control sequences.
1235 Valid values are nil, t, `head', `last', an integer or a predicate.
1236 See Info node `(gnus)Customizing Articles' for details."
1237   :group 'gnus-article-treat
1238   :link '(custom-manual "(gnus)Customizing Articles")
1239   :type gnus-article-treat-custom)
1240
1241 (make-obsolete-variable 'gnus-treat-display-xface
1242                         'gnus-treat-display-x-face)
1243
1244 (defcustom gnus-treat-display-x-face
1245   (and (not noninteractive)
1246        (or (and (fboundp 'image-type-available-p)
1247                 (image-type-available-p 'xbm)
1248                 (string-match "^0x" (shell-command-to-string "uncompface"))
1249                 (executable-find "icontopbm"))
1250            (and (featurep 'xemacs)
1251                 (featurep 'xface)))
1252        'head)
1253   "Display X-Face headers.
1254 Valid values are nil, t, `head', `last', an integer or a predicate.
1255 See Info node `(gnus)Customizing Articles' and Info node
1256 `(gnus)X-Face' for details."
1257   :group 'gnus-article-treat
1258   :version "21.1"
1259   :link '(custom-manual "(gnus)Customizing Articles")
1260   :link '(custom-manual "(gnus)X-Face")
1261   :type gnus-article-treat-head-custom
1262   :set (lambda (symbol value)
1263          (set-default
1264           symbol
1265           (cond ((or (boundp symbol) (get symbol 'saved-value))
1266                  value)
1267                 ((boundp 'gnus-treat-display-xface)
1268                  (message "\
1269 ** gnus-treat-display-xface is an obsolete variable;\
1270  use gnus-treat-display-x-face instead")
1271                  (default-value 'gnus-treat-display-xface))
1272                 ((get 'gnus-treat-display-xface 'saved-value)
1273                  (message "\
1274 ** gnus-treat-display-xface is an obsolete variable;\
1275  use gnus-treat-display-x-face instead")
1276                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1277                 (t
1278                  value)))))
1279 (put 'gnus-treat-display-x-face 'highlight t)
1280
1281 (defcustom gnus-treat-display-face
1282   (and (not noninteractive)
1283        (or (and (fboundp 'image-type-available-p)
1284                 (image-type-available-p 'png))
1285            (and (featurep 'xemacs)
1286                 (featurep 'png)))
1287        'head)
1288   "Display Face headers.
1289 Valid values are nil, t, `head', `last', an integer or a predicate.
1290 See Info node `(gnus)Customizing Articles' and Info node
1291 `(gnus)X-Face' for details."
1292   :group 'gnus-article-treat
1293   :version "22.1"
1294   :link '(custom-manual "(gnus)Customizing Articles")
1295   :link '(custom-manual "(gnus)X-Face")
1296   :type gnus-article-treat-head-custom)
1297 (put 'gnus-treat-display-face 'highlight t)
1298
1299 (defcustom gnus-treat-display-smileys
1300   (if (or (and (featurep 'xemacs)
1301                (featurep 'xpm))
1302           (and (fboundp 'image-type-available-p)
1303                (image-type-available-p 'pbm)))
1304       t nil)
1305   "Display smileys.
1306 Valid values are nil, t, `head', `last', an integer or a predicate.
1307 See Info node `(gnus)Customizing Articles' and Info node
1308 `(gnus)Smileys' for details."
1309   :group 'gnus-article-treat
1310   :version "21.1"
1311   :link '(custom-manual "(gnus)Customizing Articles")
1312   :link '(custom-manual "(gnus)Smileys")
1313   :type gnus-article-treat-custom)
1314 (put 'gnus-treat-display-smileys 'highlight t)
1315
1316 (defcustom gnus-treat-from-picon
1317   (if (and (gnus-image-type-available-p 'xpm)
1318            (gnus-picons-installed-p))
1319       'head nil)
1320   "Display picons in the From header.
1321 Valid values are nil, t, `head', `last', an integer or a predicate.
1322 See Info node `(gnus)Customizing Articles' and Info node
1323 `(gnus)Picons' for details."
1324   :version "22.1"
1325   :group 'gnus-article-treat
1326   :group 'gnus-picon
1327   :link '(custom-manual "(gnus)Customizing Articles")
1328   :link '(custom-manual "(gnus)Picons")
1329   :type gnus-article-treat-head-custom)
1330 (put 'gnus-treat-from-picon 'highlight t)
1331
1332 (defcustom gnus-treat-mail-picon
1333   (if (and (gnus-image-type-available-p 'xpm)
1334            (gnus-picons-installed-p))
1335       'head nil)
1336   "Display picons in To and Cc headers.
1337 Valid values are nil, t, `head', `last', an integer or a predicate.
1338 See Info node `(gnus)Customizing Articles' and Info node
1339 `(gnus)Picons' for details."
1340   :version "22.1"
1341   :group 'gnus-article-treat
1342   :group 'gnus-picon
1343   :link '(custom-manual "(gnus)Customizing Articles")
1344   :link '(custom-manual "(gnus)Picons")
1345   :type gnus-article-treat-head-custom)
1346 (put 'gnus-treat-mail-picon 'highlight t)
1347
1348 (defcustom gnus-treat-newsgroups-picon
1349   (if (and (gnus-image-type-available-p 'xpm)
1350            (gnus-picons-installed-p))
1351       'head nil)
1352   "Display picons in the Newsgroups and Followup-To headers.
1353 Valid values are nil, t, `head', `last', an integer or a predicate.
1354 See Info node `(gnus)Customizing Articles' and Info node
1355 `(gnus)Picons' for details."
1356   :version "22.1"
1357   :group 'gnus-article-treat
1358   :group 'gnus-picon
1359   :link '(custom-manual "(gnus)Customizing Articles")
1360   :link '(custom-manual "(gnus)Picons")
1361   :type gnus-article-treat-head-custom)
1362 (put 'gnus-treat-newsgroups-picon 'highlight t)
1363
1364 (defcustom gnus-treat-body-boundary
1365   (if (and (eq window-system 'x)
1366            (or gnus-treat-newsgroups-picon
1367                gnus-treat-mail-picon
1368                gnus-treat-from-picon))
1369       'head nil)
1370   "Draw a boundary at the end of the headers.
1371 Valid values are nil and `head'.
1372 See Info node `(gnus)Customizing Articles' for details."
1373   :version "22.1"
1374   :group 'gnus-article-treat
1375   :link '(custom-manual "(gnus)Customizing Articles")
1376   :type gnus-article-treat-head-custom)
1377
1378 (defcustom gnus-treat-capitalize-sentences nil
1379   "Capitalize sentence-starting words.
1380 Valid values are nil, t, `head', `last', an integer or a predicate.
1381 See Info node `(gnus)Customizing Articles' for details."
1382   :version "21.1"
1383   :group 'gnus-article-treat
1384   :link '(custom-manual "(gnus)Customizing Articles")
1385   :type gnus-article-treat-custom)
1386
1387 (defcustom gnus-treat-wash-html nil
1388   "Format as HTML.
1389 Valid values are nil, t, `head', `last', an integer or a predicate.
1390 See Info node `(gnus)Customizing Articles' for details."
1391   :version "22.1"
1392   :group 'gnus-article-treat
1393   :link '(custom-manual "(gnus)Customizing Articles")
1394   :type gnus-article-treat-custom)
1395
1396 (defcustom gnus-treat-fill-long-lines nil
1397   "Fill long lines.
1398 Valid values are nil, t, `head', `last', an integer or a predicate.
1399 See Info node `(gnus)Customizing Articles' for details."
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403
1404 (defcustom gnus-treat-play-sounds nil
1405   "Play sounds.
1406 Valid values are nil, t, `head', `last', an integer or a predicate.
1407 See Info node `(gnus)Customizing Articles' for details."
1408   :version "21.1"
1409   :group 'gnus-article-treat
1410   :link '(custom-manual "(gnus)Customizing Articles")
1411   :type gnus-article-treat-custom)
1412
1413 (defcustom gnus-treat-translate nil
1414   "Translate articles from one language to another.
1415 Valid values are nil, t, `head', `last', an integer or a predicate.
1416 See Info node `(gnus)Customizing Articles' for details."
1417   :version "21.1"
1418   :group 'gnus-article-treat
1419   :link '(custom-manual "(gnus)Customizing Articles")
1420   :type gnus-article-treat-custom)
1421
1422 (defcustom gnus-treat-x-pgp-sig nil
1423   "Verify X-PGP-Sig.
1424 To automatically treat X-PGP-Sig, set it to head.
1425 Valid values are nil, t, `head', `last', an integer or a predicate.
1426 See Info node `(gnus)Customizing Articles' for details."
1427   :version "22.1"
1428   :group 'gnus-article-treat
1429   :group 'mime-security
1430   :link '(custom-manual "(gnus)Customizing Articles")
1431   :type gnus-article-treat-custom)
1432
1433 (defvar gnus-article-encrypt-protocol-alist
1434   '(("PGP" . mml2015-self-encrypt)))
1435
1436 ;; Set to nil if more than one protocol added to
1437 ;; gnus-article-encrypt-protocol-alist.
1438 (defcustom gnus-article-encrypt-protocol "PGP"
1439   "The protocol used for encrypt articles.
1440 It is a string, such as \"PGP\". If nil, ask user."
1441   :version "22.1"
1442   :type 'string
1443   :group 'mime-security)
1444
1445 (defvar gnus-article-wash-function nil
1446   "Function used for converting HTML into text.")
1447
1448 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1449                               (mm-coding-system-p 'utf-8)
1450                               (executable-find idna-program))
1451   "Whether IDNA decoding of headers is used when viewing messages.
1452 This requires GNU Libidn, and by default only enabled if it is found."
1453   :version "22.1"
1454   :group 'gnus-article-headers
1455   :type 'boolean)
1456
1457 (defcustom gnus-article-over-scroll nil
1458   "If non-nil, allow scrolling the article buffer even when there no more text."
1459   :version "22.1"
1460   :group 'gnus-article
1461   :type 'boolean)
1462
1463 ;;; Internal variables
1464
1465 (defvar gnus-english-month-names
1466   '("January" "February" "March" "April" "May" "June" "July" "August"
1467     "September" "October" "November" "December"))
1468
1469 (defvar gnus-button-regexp nil)
1470 (defvar gnus-button-marker-list nil)
1471 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1472
1473 (defvar gnus-button-last nil)
1474 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1475
1476 (defvar article-goto-body-goes-to-point-min-p nil)
1477 (defvar gnus-article-wash-types nil)
1478 (defvar gnus-article-emphasis-alist nil)
1479 (defvar gnus-article-image-alist nil)
1480
1481 (defvar gnus-article-mime-handle-alist-1 nil)
1482 (defvar gnus-treatment-function-alist
1483   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1484     (gnus-treat-strip-banner gnus-article-strip-banner)
1485     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1486     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1487     (gnus-treat-buttonize gnus-article-add-buttons)
1488     (gnus-treat-fill-article gnus-article-fill-cited-article)
1489     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1490     (gnus-treat-strip-cr gnus-article-remove-cr)
1491     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1492     (gnus-treat-date-ut gnus-article-date-ut)
1493     (gnus-treat-date-local gnus-article-date-local)
1494     (gnus-treat-date-english gnus-article-date-english)
1495     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1496     (gnus-treat-date-original gnus-article-date-original)
1497     (gnus-treat-date-user-defined gnus-article-date-user)
1498     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1499     (gnus-treat-display-x-face gnus-article-display-x-face)
1500     (gnus-treat-display-face gnus-article-display-face)
1501     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1502     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1503     (gnus-treat-hide-signature gnus-article-hide-signature)
1504     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1505     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1506     (gnus-treat-strip-pem gnus-article-hide-pem)
1507     (gnus-treat-from-picon gnus-treat-from-picon)
1508     (gnus-treat-mail-picon gnus-treat-mail-picon)
1509     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1510     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1511     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1512     (gnus-treat-strip-trailing-blank-lines
1513      gnus-article-remove-trailing-blank-lines)
1514     (gnus-treat-strip-leading-blank-lines
1515      gnus-article-strip-leading-blank-lines)
1516     (gnus-treat-strip-multiple-blank-lines
1517      gnus-article-strip-multiple-blank-lines)
1518     (gnus-treat-overstrike gnus-article-treat-overstrike)
1519     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1520     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1521     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1522     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1523     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1524     (gnus-treat-display-smileys gnus-treat-smiley)
1525     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1526     (gnus-treat-wash-html gnus-article-wash-html)
1527     (gnus-treat-emphasize gnus-article-emphasize)
1528     (gnus-treat-hide-citation gnus-article-hide-citation)
1529     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1530     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1531     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1532     (gnus-treat-play-sounds gnus-earcon-display)))
1533
1534 (defvar gnus-article-mime-handle-alist nil)
1535 (defvar article-lapsed-timer nil)
1536 (defvar gnus-article-current-summary nil)
1537
1538 (defvar gnus-article-mode-syntax-table
1539   (let ((table (copy-syntax-table text-mode-syntax-table)))
1540     ;; This causes the citation match run O(2^n).
1541     ;; (modify-syntax-entry ?- "w" table)
1542     (modify-syntax-entry ?> ")<" table)
1543     (modify-syntax-entry ?< "(>" table)
1544     ;; make M-. in article buffers work for `foo' strings
1545     (modify-syntax-entry ?' " " table)
1546     (modify-syntax-entry ?` " " table)
1547     table)
1548   "Syntax table used in article mode buffers.
1549 Initialized from `text-mode-syntax-table.")
1550
1551 (defvar gnus-save-article-buffer nil)
1552
1553 (defvar gnus-article-mode-line-format-alist
1554   (nconc '((?w (gnus-article-wash-status) ?s)
1555            (?m (gnus-article-mime-part-status) ?s))
1556          gnus-summary-mode-line-format-alist))
1557
1558 (defvar gnus-number-of-articles-to-be-saved nil)
1559
1560 (defvar gnus-inhibit-hiding nil)
1561
1562 (defvar gnus-article-edit-mode nil)
1563
1564 ;;; Macros for dealing with the article buffer.
1565
1566 (defmacro gnus-with-article-headers (&rest forms)
1567   `(save-excursion
1568      (set-buffer gnus-article-buffer)
1569      (save-restriction
1570        (let ((inhibit-read-only t)
1571              (inhibit-point-motion-hooks t)
1572              (case-fold-search t))
1573          (article-narrow-to-head)
1574          ,@forms))))
1575
1576 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1577 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1578
1579 (defmacro gnus-with-article-buffer (&rest forms)
1580   `(save-excursion
1581      (set-buffer gnus-article-buffer)
1582      (let ((inhibit-read-only t))
1583        ,@forms)))
1584
1585 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1586 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1587
1588 (defun gnus-article-goto-header (header)
1589   "Go to HEADER, which is a regular expression."
1590   (re-search-forward (concat "^\\(" header "\\):") nil t))
1591
1592 (defsubst gnus-article-hide-text (b e props)
1593   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1594   (gnus-add-text-properties-when 'article-type nil b e props)
1595   (when (memq 'intangible props)
1596     (put-text-property
1597      (max (1- b) (point-min))
1598      b 'intangible (cddr (memq 'intangible props)))))
1599
1600 (defsubst gnus-article-unhide-text (b e)
1601   "Remove hidden text properties from region between B and E."
1602   (remove-text-properties b e gnus-hidden-properties)
1603   (when (memq 'intangible gnus-hidden-properties)
1604     (put-text-property (max (1- b) (point-min))
1605                        b 'intangible nil)))
1606
1607 (defun gnus-article-hide-text-type (b e type)
1608   "Hide text of TYPE between B and E."
1609   (gnus-add-wash-type type)
1610   (gnus-article-hide-text
1611    b e (cons 'article-type (cons type gnus-hidden-properties))))
1612
1613 (defun gnus-article-unhide-text-type (b e type)
1614   "Unhide text of TYPE between B and E."
1615   (gnus-delete-wash-type type)
1616   (remove-text-properties
1617    b e (cons 'article-type (cons type gnus-hidden-properties)))
1618   (when (memq 'intangible gnus-hidden-properties)
1619     (put-text-property (max (1- b) (point-min))
1620                        b 'intangible nil)))
1621
1622 (defun gnus-article-hide-text-of-type (type)
1623   "Hide text of TYPE in the current buffer."
1624   (save-excursion
1625     (let ((b (point-min))
1626           (e (point-max)))
1627       (while (setq b (text-property-any b e 'article-type type))
1628         (add-text-properties b (incf b) gnus-hidden-properties)))))
1629
1630 (defun gnus-article-delete-text-of-type (type)
1631   "Delete text of TYPE in the current buffer."
1632   (save-excursion
1633     (let ((b (point-min)))
1634       (while (setq b (text-property-any b (point-max) 'article-type type))
1635         (delete-region
1636          b (or (text-property-not-all b (point-max) 'article-type type)
1637                (point-max)))))))
1638
1639 (defun gnus-article-delete-invisible-text ()
1640   "Delete all invisible text in the current buffer."
1641   (save-excursion
1642     (let ((b (point-min)))
1643       (while (setq b (text-property-any b (point-max) 'invisible t))
1644         (delete-region
1645          b (or (text-property-not-all b (point-max) 'invisible t)
1646                (point-max)))))))
1647
1648 (defun gnus-article-text-type-exists-p (type)
1649   "Say whether any text of type TYPE exists in the buffer."
1650   (text-property-any (point-min) (point-max) 'article-type type))
1651
1652 (defsubst gnus-article-header-rank ()
1653   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1654   (let ((list gnus-sorted-header-list)
1655         (i 1))
1656     (while list
1657       (if (looking-at (car list))
1658           (setq list nil)
1659         (setq list (cdr list))
1660         (incf i)))
1661       i))
1662
1663 (defun article-hide-headers (&optional arg delete)
1664   "Hide unwanted headers and possibly sort them as well."
1665   (interactive)
1666   ;; This function might be inhibited.
1667   (unless gnus-inhibit-hiding
1668     (let ((inhibit-read-only nil)
1669           (case-fold-search t)
1670           (max (1+ (length gnus-sorted-header-list)))
1671           (inhibit-point-motion-hooks t)
1672           (cur (current-buffer))
1673           ignored visible beg)
1674       (save-excursion
1675         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1676         ;; group parameters, so we should go to the summary buffer.
1677         (when (prog1
1678                   (condition-case nil
1679                       (progn (set-buffer gnus-summary-buffer) t)
1680                     (error nil))
1681                 (setq ignored (when (not gnus-visible-headers)
1682                                 (cond ((stringp gnus-ignored-headers)
1683                                        gnus-ignored-headers)
1684                                       ((listp gnus-ignored-headers)
1685                                        (mapconcat 'identity
1686                                                   gnus-ignored-headers
1687                                                   "\\|"))))
1688                       visible (cond ((stringp gnus-visible-headers)
1689                                      gnus-visible-headers)
1690                                     ((and gnus-visible-headers
1691                                           (listp gnus-visible-headers))
1692                                      (mapconcat 'identity
1693                                                 gnus-visible-headers
1694                                                 "\\|")))))
1695           (set-buffer cur))
1696         (save-restriction
1697           ;; First we narrow to just the headers.
1698           (article-narrow-to-head)
1699           ;; Hide any "From " lines at the beginning of (mail) articles.
1700           (while (looking-at "From ")
1701             (forward-line 1))
1702           (unless (bobp)
1703             (delete-region (point-min) (point)))
1704           ;; Then treat the rest of the header lines.
1705           ;; Then we use the two regular expressions
1706           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1707           ;; select which header lines is to remain visible in the
1708           ;; article buffer.
1709           (while (re-search-forward "^[^ \t:]*:" nil t)
1710             (beginning-of-line)
1711             ;; Mark the rank of the header.
1712             (put-text-property
1713              (point) (1+ (point)) 'message-rank
1714              (if (or (and visible (looking-at visible))
1715                      (and ignored
1716                           (not (looking-at ignored))))
1717                  (gnus-article-header-rank)
1718                (+ 2 max)))
1719             (forward-line 1))
1720           (message-sort-headers-1)
1721           (when (setq beg (text-property-any
1722                            (point-min) (point-max) 'message-rank (+ 2 max)))
1723             ;; We delete the unwanted headers.
1724             (gnus-add-wash-type 'headers)
1725             (add-text-properties (point-min) (+ 5 (point-min))
1726                                  '(article-type headers dummy-invisible t))
1727             (delete-region beg (point-max))))))))
1728
1729 (defun article-hide-boring-headers (&optional arg)
1730   "Toggle hiding of headers that aren't very interesting.
1731 If given a negative prefix, always show; if given a positive prefix,
1732 always hide."
1733   (interactive (gnus-article-hidden-arg))
1734   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1735              (not gnus-show-all-headers))
1736     (save-excursion
1737       (save-restriction
1738         (let ((inhibit-read-only t)
1739               (inhibit-point-motion-hooks t))
1740           (article-narrow-to-head)
1741           (dolist (elem gnus-boring-article-headers)
1742             (goto-char (point-min))
1743             (cond
1744              ;; Hide empty headers.
1745              ((eq elem 'empty)
1746               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1747                 (forward-line -1)
1748                 (gnus-article-hide-text-type
1749                  (point-at-bol)
1750                  (progn
1751                    (end-of-line)
1752                    (if (re-search-forward "^[^ \t]" nil t)
1753                        (match-beginning 0)
1754                      (point-max)))
1755                  'boring-headers)))
1756              ;; Hide boring Newsgroups header.
1757              ((eq elem 'newsgroups)
1758               (when (gnus-string-equal
1759                      (gnus-fetch-field "newsgroups")
1760                      (gnus-group-real-name
1761                       (if (boundp 'gnus-newsgroup-name)
1762                           gnus-newsgroup-name
1763                         "")))
1764                 (gnus-article-hide-header "newsgroups")))
1765              ((eq elem 'to-address)
1766               (let ((to (message-fetch-field "to"))
1767                     (to-address
1768                      (gnus-parameter-to-address
1769                       (if (boundp 'gnus-newsgroup-name)
1770                           gnus-newsgroup-name ""))))
1771                 (when (and to to-address
1772                            (ignore-errors
1773                              (gnus-string-equal
1774                               ;; only one address in To
1775                               (nth 1 (mail-extract-address-components to))
1776                               to-address)))
1777                   (gnus-article-hide-header "to"))))
1778              ((eq elem 'to-list)
1779               (let ((to (message-fetch-field "to"))
1780                     (to-list
1781                      (gnus-parameter-to-list
1782                       (if (boundp 'gnus-newsgroup-name)
1783                           gnus-newsgroup-name ""))))
1784                 (when (and to to-list
1785                            (ignore-errors
1786                              (gnus-string-equal
1787                               ;; only one address in To
1788                               (nth 1 (mail-extract-address-components to))
1789                               to-list)))
1790                   (gnus-article-hide-header "to"))))
1791              ((eq elem 'cc-list)
1792               (let ((cc (message-fetch-field "cc"))
1793                     (to-list
1794                      (gnus-parameter-to-list
1795                       (if (boundp 'gnus-newsgroup-name)
1796                           gnus-newsgroup-name ""))))
1797                 (when (and cc to-list
1798                            (ignore-errors
1799                              (gnus-string-equal
1800                               ;; only one address in CC
1801                               (nth 1 (mail-extract-address-components cc))
1802                               to-list)))
1803                   (gnus-article-hide-header "cc"))))
1804              ((eq elem 'followup-to)
1805               (when (gnus-string-equal
1806                      (message-fetch-field "followup-to")
1807                      (message-fetch-field "newsgroups"))
1808                 (gnus-article-hide-header "followup-to")))
1809              ((eq elem 'reply-to)
1810               (if (gnus-group-find-parameter
1811                    gnus-newsgroup-name 'broken-reply-to)
1812                   (gnus-article-hide-header "reply-to")
1813                 (let ((from (message-fetch-field "from"))
1814                       (reply-to (message-fetch-field "reply-to")))
1815                   (when
1816                       (and
1817                        from reply-to
1818                        (ignore-errors
1819                          (equal
1820                           (sort (mapcar
1821                                  (lambda (x) (downcase (cadr x)))
1822                                  (mail-extract-address-components from t))
1823                                 'string<)
1824                           (sort (mapcar
1825                                  (lambda (x) (downcase (cadr x)))
1826                                  (mail-extract-address-components reply-to t))
1827                                 'string<))))
1828                     (gnus-article-hide-header "reply-to")))))
1829              ((eq elem 'date)
1830               (let ((date (message-fetch-field "date")))
1831                 (when (and date
1832                            (< (days-between (current-time-string) date)
1833                               4))
1834                   (gnus-article-hide-header "date"))))
1835              ((eq elem 'long-to)
1836               (let ((to (message-fetch-field "to"))
1837                     (cc (message-fetch-field "cc")))
1838                 (when (> (length to) 1024)
1839                   (gnus-article-hide-header "to"))
1840                 (when (> (length cc) 1024)
1841                   (gnus-article-hide-header "cc"))))
1842              ((eq elem 'many-to)
1843               (let ((to-count 0)
1844                     (cc-count 0))
1845                 (goto-char (point-min))
1846                 (while (re-search-forward "^to:" nil t)
1847                   (setq to-count (1+ to-count)))
1848                 (when (> to-count 1)
1849                   (while (> to-count 0)
1850                     (goto-char (point-min))
1851                     (save-restriction
1852                       (re-search-forward "^to:" nil nil to-count)
1853                       (forward-line -1)
1854                       (narrow-to-region (point) (point-max))
1855                       (gnus-article-hide-header "to"))
1856                     (setq to-count (1- to-count))))
1857                 (goto-char (point-min))
1858                 (while (re-search-forward "^cc:" nil t)
1859                   (setq cc-count (1+ cc-count)))
1860                 (when (> cc-count 1)
1861                   (while (> cc-count 0)
1862                     (goto-char (point-min))
1863                     (save-restriction
1864                       (re-search-forward "^cc:" nil nil cc-count)
1865                       (forward-line -1)
1866                       (narrow-to-region (point) (point-max))
1867                       (gnus-article-hide-header "cc"))
1868                     (setq cc-count (1- cc-count)))))))))))))
1869
1870 (defun gnus-article-hide-header (header)
1871   (save-excursion
1872     (goto-char (point-min))
1873     (when (re-search-forward (concat "^" header ":") nil t)
1874       (gnus-article-hide-text-type
1875        (point-at-bol)
1876        (progn
1877          (end-of-line)
1878          (if (re-search-forward "^[^ \t]" nil t)
1879              (match-beginning 0)
1880            (point-max)))
1881        'boring-headers))))
1882
1883 (defvar gnus-article-normalized-header-length 40
1884   "Length of normalized headers.")
1885
1886 (defun article-normalize-headers ()
1887   "Make all header lines 40 characters long."
1888   (interactive)
1889   (let ((inhibit-read-only t)
1890         column)
1891     (save-excursion
1892       (save-restriction
1893         (article-narrow-to-head)
1894         (while (not (eobp))
1895           (cond
1896            ((< (setq column (- (point-at-eol) (point)))
1897                gnus-article-normalized-header-length)
1898             (end-of-line)
1899             (insert (make-string
1900                      (- gnus-article-normalized-header-length column)
1901                      ? )))
1902            ((> column gnus-article-normalized-header-length)
1903             (gnus-put-text-property
1904              (progn
1905                (forward-char gnus-article-normalized-header-length)
1906                (point))
1907              (point-at-eol)
1908              'invisible t))
1909            (t
1910             ;; Do nothing.
1911             ))
1912           (forward-line 1))))))
1913
1914 (defun article-treat-dumbquotes ()
1915   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
1916 Note that this function guesses whether a character is a sm*rtq**t* or
1917 not, so it should only be used interactively.
1918
1919 Sm*rtq**t*s are M****s***'s unilateral extension to the
1920 iso-8859-1 character map in an attempt to provide more quoting
1921 characters.  If you see something like \\222 or \\264 where
1922 you're expecting some kind of apostrophe or quotation mark, then
1923 try this wash."
1924   (interactive)
1925   (article-translate-strings gnus-article-dumbquotes-map))
1926
1927 (defun article-translate-characters (from to)
1928   "Translate all characters in the body of the article according to FROM and TO.
1929 FROM is a string of characters to translate from; to is a string of
1930 characters to translate to."
1931   (save-excursion
1932     (when (article-goto-body)
1933       (let ((inhibit-read-only t)
1934             (x (make-string 225 ?x))
1935             (i -1))
1936         (while (< (incf i) (length x))
1937           (aset x i i))
1938         (setq i 0)
1939         (while (< i (length from))
1940           (aset x (aref from i) (aref to i))
1941           (incf i))
1942         (translate-region (point) (point-max) x)))))
1943
1944 (defun article-translate-strings (map)
1945   "Translate all string in the body of the article according to MAP.
1946 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1947   (save-excursion
1948     (when (article-goto-body)
1949       (let ((inhibit-read-only t))
1950         (dolist (elem map)
1951           (save-excursion
1952             (while (search-forward (car elem) nil t)
1953               (replace-match (cadr elem)))))))))
1954
1955 (defun article-treat-overstrike ()
1956   "Translate overstrikes into bold text."
1957   (interactive)
1958   (save-excursion
1959     (when (article-goto-body)
1960       (let ((inhibit-read-only t))
1961         (while (search-forward "\b" nil t)
1962           (let ((next (char-after))
1963                 (previous (char-after (- (point) 2))))
1964             ;; We do the boldification/underlining by hiding the
1965             ;; overstrikes and putting the proper text property
1966             ;; on the letters.
1967             (cond
1968              ((eq next previous)
1969               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1970               (put-text-property (point) (1+ (point)) 'face 'bold))
1971              ((eq next ?_)
1972               (gnus-article-hide-text-type
1973                (1- (point)) (1+ (point)) 'overstrike)
1974               (put-text-property
1975                (- (point) 2) (1- (point)) 'face 'underline))
1976              ((eq previous ?_)
1977               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1978               (put-text-property
1979                (point) (1+ (point)) 'face 'underline)))))))))
1980
1981 (defun article-treat-ansi-sequences ()
1982   "Translate ANSI SGR control sequences into overlays or extents."
1983   (interactive)
1984   (save-excursion
1985     (when (article-goto-body)
1986       (let ((inhibit-read-only t))
1987         (ansi-color-apply-on-region (point) (point-max))))))
1988
1989 (defun gnus-article-treat-unfold-headers ()
1990   "Unfold folded message headers.
1991 Only the headers that fit into the current window width will be
1992 unfolded."
1993   (interactive)
1994   (gnus-with-article-headers
1995     (let (length)
1996       (while (not (eobp))
1997         (save-restriction
1998           (mail-header-narrow-to-field)
1999           (let ((header (buffer-string)))
2000             (with-temp-buffer
2001               (insert header)
2002               (goto-char (point-min))
2003               (while (re-search-forward "\n[\t ]" nil t)
2004                 (replace-match " " t t)))
2005             (setq length (- (point-max) (point-min) 1)))
2006           (when (< length (window-width))
2007             (while (re-search-forward "\n[\t ]" nil t)
2008               (replace-match " " t t)))
2009           (goto-char (point-max)))))))
2010
2011 (defun gnus-article-treat-fold-headers ()
2012   "Fold message headers."
2013   (interactive)
2014   (gnus-with-article-headers
2015     (while (not (eobp))
2016       (save-restriction
2017         (mail-header-narrow-to-field)
2018         (mail-header-fold-field)
2019         (goto-char (point-max))))))
2020
2021 (defun gnus-treat-smiley ()
2022   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2023   (interactive)
2024   (gnus-with-article-buffer
2025     (if (memq 'smiley gnus-article-wash-types)
2026         (gnus-delete-images 'smiley)
2027       (article-goto-body)
2028       (let ((images (smiley-region (point) (point-max))))
2029         (when images
2030           (gnus-add-wash-type 'smiley)
2031           (dolist (image images)
2032             (gnus-add-image 'smiley image)))))))
2033
2034 (defun gnus-article-remove-images ()
2035   "Remove all images from the article buffer."
2036   (interactive)
2037   (gnus-with-article-buffer
2038     (dolist (elem gnus-article-image-alist)
2039       (gnus-delete-images (car elem)))))
2040
2041 (defun gnus-article-treat-fold-newsgroups ()
2042   "Unfold folded message headers.
2043 Only the headers that fit into the current window width will be
2044 unfolded."
2045   (interactive)
2046   (gnus-with-article-headers
2047     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2048       (save-restriction
2049         (mail-header-narrow-to-field)
2050         (while (re-search-forward ", *" nil t)
2051           (replace-match ", " t t))
2052         (mail-header-fold-field)
2053         (goto-char (point-max))))))
2054
2055 (defun gnus-article-treat-body-boundary ()
2056   "Place a boundary line at the end of the headers."
2057   (interactive)
2058   (when (and gnus-body-boundary-delimiter
2059              (> (length gnus-body-boundary-delimiter) 0))
2060     (gnus-with-article-headers
2061       (goto-char (point-max))
2062       (let ((start (point)))
2063         (insert "X-Boundary: ")
2064         (gnus-add-text-properties start (point) '(invisible t intangible t))
2065         (insert (let (str)
2066                   (while (>= (1- (window-width)) (length str))
2067                     (setq str (concat str gnus-body-boundary-delimiter)))
2068                   (substring str 0 (1- (window-width))))
2069                 "\n")
2070         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2071
2072 (defun article-fill-long-lines ()
2073   "Fill lines that are wider than the window width."
2074   (interactive)
2075   (save-excursion
2076     (let ((inhibit-read-only t)
2077           (width (window-width (get-buffer-window (current-buffer)))))
2078       (save-restriction
2079         (article-goto-body)
2080         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2081           (while (not (eobp))
2082             (end-of-line)
2083             (when (>= (current-column) (min fill-column width))
2084               (narrow-to-region (min (1+ (point)) (point-max))
2085                                 (point-at-bol))
2086               (let ((goback (point-marker)))
2087                 (fill-paragraph nil)
2088                 (goto-char (marker-position goback)))
2089               (widen))
2090             (forward-line 1)))))))
2091
2092 (defun article-capitalize-sentences ()
2093   "Capitalize the first word in each sentence."
2094   (interactive)
2095   (save-excursion
2096     (let ((inhibit-read-only t)
2097           (paragraph-start "^[\n\^L]"))
2098       (article-goto-body)
2099       (while (not (eobp))
2100         (capitalize-word 1)
2101         (forward-sentence)))))
2102
2103 (defun article-remove-cr ()
2104   "Remove trailing CRs and then translate remaining CRs into LFs."
2105   (interactive)
2106   (save-excursion
2107     (let ((inhibit-read-only t))
2108       (goto-char (point-min))
2109       (while (re-search-forward "\r+$" nil t)
2110         (replace-match "" t t))
2111       (goto-char (point-min))
2112       (while (search-forward "\r" nil t)
2113         (replace-match "\n" t t)))))
2114
2115 (defun article-remove-trailing-blank-lines ()
2116   "Remove all trailing blank lines from the article."
2117   (interactive)
2118   (save-excursion
2119     (let ((inhibit-read-only t))
2120       (goto-char (point-max))
2121       (delete-region
2122        (point)
2123        (progn
2124          (while (and (not (bobp))
2125                      (looking-at "^[ \t]*$")
2126                      (not (gnus-annotation-in-region-p
2127                            (point) (point-at-eol))))
2128            (forward-line -1))
2129          (forward-line 1)
2130          (point))))))
2131
2132 (eval-when-compile
2133   (defvar gnus-face-properties-alist))
2134
2135 (defun article-display-face ()
2136   "Display any Face headers in the header."
2137   (interactive)
2138   (let ((wash-face-p buffer-read-only))
2139     (gnus-with-article-headers
2140       ;; When displaying parts, this function can be called several times on
2141       ;; the same article, without any intended toggle semantic (as typing `W
2142       ;; D d' would have). So face deletion must occur only when we come from
2143       ;; an interactive command, that is when the *Article* buffer is
2144       ;; read-only.
2145       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2146           (gnus-delete-images 'face)
2147         (let (face faces)
2148           (save-excursion
2149             (when (and wash-face-p
2150                        (progn
2151                          (goto-char (point-min))
2152                          (not (re-search-forward "^Face:[\t ]*" nil t)))
2153                        (gnus-buffer-live-p gnus-original-article-buffer))
2154               (set-buffer gnus-original-article-buffer))
2155             (save-restriction
2156               (mail-narrow-to-head)
2157               (while (gnus-article-goto-header "Face")
2158                 (setq faces (nconc faces (list (mail-header-field-value)))))))
2159           (dolist (face faces)
2160             (let ((png (gnus-convert-face-to-png face))
2161                   image)
2162               (when png
2163                 (setq image
2164                       (apply 'gnus-create-image png 'png t
2165                              (cdr (assq 'png gnus-face-properties-alist))))
2166                 (gnus-article-goto-header "from")
2167                 (when (bobp)
2168                   (insert "From: [no `from' set]\n")
2169                   (forward-char -17))
2170                 (gnus-add-wash-type 'face)
2171                 (gnus-add-image 'face image)
2172                 (gnus-put-image image nil 'face))))))
2173       )))
2174
2175 (defun article-display-x-face (&optional force)
2176   "Look for an X-Face header and display it if present."
2177   (interactive (list 'force))
2178   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2179     (gnus-with-article-headers
2180       ;; Delete the old process, if any.
2181       (when (process-status "article-x-face")
2182         (delete-process "article-x-face"))
2183       ;; See the comment in `article-display-face'.
2184       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2185           ;; We have already displayed X-Faces, so we remove them
2186           ;; instead.
2187           (gnus-delete-images 'xface)
2188         ;; Display X-Faces.
2189         (let (x-faces from face)
2190           (save-excursion
2191             (when (and wash-face-p
2192                        (progn
2193                          (goto-char (point-min))
2194                          (not (re-search-forward
2195                                "^X-Face\\(-[0-9]+\\)?:[\t ]*" nil t)))
2196                        (gnus-buffer-live-p gnus-original-article-buffer))
2197               ;; If type `W f', use gnus-original-article-buffer,
2198               ;; otherwise use the current buffer because displaying
2199               ;; RFC822 parts calls this function too.
2200               (set-buffer gnus-original-article-buffer))
2201             (save-restriction
2202               (mail-narrow-to-head)
2203               (while (gnus-article-goto-header "X-Face")
2204                 (push (mail-header-field-value) x-faces))
2205               (setq from (message-fetch-field "from"))))
2206           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2207           ;; single external face.
2208           (when (stringp gnus-article-x-face-command)
2209             (setq x-faces (list (car x-faces))))
2210           (while (and (setq face (pop x-faces))
2211                       gnus-article-x-face-command
2212                       (or force
2213                           ;; Check whether this face is censored.
2214                           (not gnus-article-x-face-too-ugly)
2215                           (and gnus-article-x-face-too-ugly from
2216                                (not (string-match gnus-article-x-face-too-ugly
2217                                                   from)))))
2218             ;; We display the face.
2219             (cond ((stringp gnus-article-x-face-command)
2220                    ;; The command is a string, so we interpret the command
2221                    ;; as a, well, command, and fork it off.
2222                    (let ((process-connection-type nil))
2223                      (process-kill-without-query
2224                       (start-process
2225                        "article-x-face" nil shell-file-name
2226                        shell-command-switch gnus-article-x-face-command))
2227                      (with-temp-buffer
2228                        (insert face)
2229                        (process-send-region "article-x-face"
2230                                             (point-min) (point-max)))
2231                      (process-send-eof "article-x-face")))
2232                   ((functionp gnus-article-x-face-command)
2233                    ;; The command is a lisp function, so we call it.
2234                    (funcall gnus-article-x-face-command face))
2235                   (t
2236                    (error "%s is not a function"
2237                           gnus-article-x-face-command)))))))))
2238
2239 (defun article-decode-mime-words ()
2240   "Decode all MIME-encoded words in the article."
2241   (interactive)
2242   (gnus-with-article-buffer
2243     (let ((inhibit-point-motion-hooks t)
2244           (mail-parse-charset gnus-newsgroup-charset)
2245           (mail-parse-ignored-charsets
2246            (with-current-buffer gnus-summary-buffer
2247              gnus-newsgroup-ignored-charsets)))
2248       (mail-decode-encoded-word-region (point-min) (point-max)))))
2249
2250 (defun article-decode-charset (&optional prompt)
2251   "Decode charset-encoded text in the article.
2252 If PROMPT (the prefix), prompt for a coding system to use."
2253   (interactive "P")
2254   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2255         (inhibit-read-only t)
2256         (mail-parse-charset gnus-newsgroup-charset)
2257         (mail-parse-ignored-charsets
2258          (save-excursion (condition-case nil
2259                              (set-buffer gnus-summary-buffer)
2260                            (error))
2261                          gnus-newsgroup-ignored-charsets))
2262         ct cte ctl charset format)
2263   (save-excursion
2264     (save-restriction
2265       (article-narrow-to-head)
2266       (setq ct (message-fetch-field "Content-Type" t)
2267             cte (message-fetch-field "Content-Transfer-Encoding" t)
2268             ctl (and ct (ignore-errors
2269                           (mail-header-parse-content-type ct)))
2270             charset (cond
2271                      (prompt
2272                       (mm-read-coding-system "Charset to decode: "))
2273                      (ctl
2274                       (mail-content-type-get ctl 'charset)))
2275             format (and ctl (mail-content-type-get ctl 'format)))
2276       (when cte
2277         (setq cte (mail-header-strip cte)))
2278       (if (and ctl (not (string-match "/" (car ctl))))
2279           (setq ctl nil))
2280       (goto-char (point-max)))
2281     (forward-line 1)
2282     (save-restriction
2283       (narrow-to-region (point) (point-max))
2284       (when (and (eq mail-parse-charset 'gnus-decoded)
2285                  (eq (mm-body-7-or-8) '8bit))
2286         ;; The text code could have been decoded.
2287         (setq charset mail-parse-charset))
2288       (when (and (or (not ctl)
2289                      (equal (car ctl) "text/plain"))
2290                  (not format)) ;; article with format will decode later.
2291         (mm-decode-body
2292          charset (and cte (intern (downcase
2293                                    (gnus-strip-whitespace cte))))
2294          (car ctl)))))))
2295
2296 (defun article-decode-encoded-words ()
2297   "Remove encoded-word encoding from headers."
2298   (let ((inhibit-point-motion-hooks t)
2299         (mail-parse-charset gnus-newsgroup-charset)
2300         (mail-parse-ignored-charsets
2301          (save-excursion (condition-case nil
2302                              (set-buffer gnus-summary-buffer)
2303                            (error))
2304                          gnus-newsgroup-ignored-charsets))
2305         (inhibit-read-only t))
2306     (save-restriction
2307       (article-narrow-to-head)
2308       (funcall gnus-decode-header-function (point-min) (point-max)))))
2309
2310 (defun article-decode-group-name ()
2311   "Decode group names in `Newsgroups:'."
2312   (let ((inhibit-point-motion-hooks t)
2313         (inhibit-read-only t)
2314         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2315     (when (and (or gnus-group-name-charset-method-alist
2316                    gnus-group-name-charset-group-alist)
2317                (gnus-buffer-live-p gnus-original-article-buffer))
2318       (save-restriction
2319         (article-narrow-to-head)
2320         (with-current-buffer gnus-original-article-buffer
2321           (goto-char (point-min)))
2322         (while (re-search-forward
2323                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2324           (replace-match (save-match-data
2325                            (gnus-decode-newsgroups
2326                             ;; XXX how to use data in article buffer?
2327                             (with-current-buffer gnus-original-article-buffer
2328                               (re-search-forward
2329                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2330                                nil t)
2331                               (match-string 1))
2332                             gnus-newsgroup-name method))
2333                          t t nil 1))
2334         (goto-char (point-min))
2335         (with-current-buffer gnus-original-article-buffer
2336           (goto-char (point-min)))
2337         (while (re-search-forward
2338                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2339           (replace-match (save-match-data
2340                            (gnus-decode-newsgroups
2341                             ;; XXX how to use data in article buffer?
2342                             (with-current-buffer gnus-original-article-buffer
2343                               (re-search-forward
2344                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2345                                nil t)
2346                               (match-string 1))
2347                             gnus-newsgroup-name method))
2348                          t t nil 1))))))
2349
2350 (autoload 'idna-to-unicode "idna")
2351
2352 (defun article-decode-idna-rhs ()
2353   "Decode IDNA strings in RHS in From:, To: and Cc: headers in current buffer."
2354   (when gnus-use-idna
2355     (save-restriction
2356       (let ((inhibit-point-motion-hooks t)
2357             (inhibit-read-only t))
2358         (article-narrow-to-head)
2359         (goto-char (point-min))
2360         (while (re-search-forward "@.*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2361           (let (ace unicode)
2362             (when (save-match-data
2363                     (and (setq ace (match-string 1))
2364                          (save-excursion
2365                            (and (re-search-backward "^[^ \t]" nil t)
2366                                 (looking-at "From\\|To\\|Cc")))
2367                          (setq unicode (idna-to-unicode ace))))
2368               (unless (string= ace unicode)
2369                 (replace-match unicode nil nil nil 1)))))))))
2370
2371 (defun article-de-quoted-unreadable (&optional force read-charset)
2372   "Translate a quoted-printable-encoded article.
2373 If FORCE, decode the article whether it is marked as quoted-printable
2374 or not.
2375 If READ-CHARSET, ask for a coding system."
2376   (interactive (list 'force current-prefix-arg))
2377   (save-excursion
2378     (let ((inhibit-read-only t) type charset)
2379       (if (gnus-buffer-live-p gnus-original-article-buffer)
2380           (with-current-buffer gnus-original-article-buffer
2381             (setq type
2382                   (gnus-fetch-field "content-transfer-encoding"))
2383             (let* ((ct (gnus-fetch-field "content-type"))
2384                    (ctl (and ct
2385                              (ignore-errors
2386                                (mail-header-parse-content-type ct)))))
2387               (setq charset (and ctl
2388                                  (mail-content-type-get ctl 'charset)))
2389               (if (stringp charset)
2390                   (setq charset (intern (downcase charset)))))))
2391       (if read-charset
2392           (setq charset (mm-read-coding-system "Charset: " charset)))
2393       (unless charset
2394         (setq charset gnus-newsgroup-charset))
2395       (when (or force
2396                 (and type (let ((case-fold-search t))
2397                             (string-match "quoted-printable" type))))
2398         (article-goto-body)
2399         (quoted-printable-decode-region
2400          (point) (point-max) (mm-charset-to-coding-system charset))))))
2401
2402 (defun article-de-base64-unreadable (&optional force read-charset)
2403   "Translate a base64 article.
2404 If FORCE, decode the article whether it is marked as base64 not.
2405 If READ-CHARSET, ask for a coding system."
2406   (interactive (list 'force current-prefix-arg))
2407   (save-excursion
2408     (let ((inhibit-read-only t) type charset)
2409       (if (gnus-buffer-live-p gnus-original-article-buffer)
2410           (with-current-buffer gnus-original-article-buffer
2411             (setq type
2412                   (gnus-fetch-field "content-transfer-encoding"))
2413             (let* ((ct (gnus-fetch-field "content-type"))
2414                    (ctl (and ct
2415                              (ignore-errors
2416                                (mail-header-parse-content-type ct)))))
2417               (setq charset (and ctl
2418                                  (mail-content-type-get ctl 'charset)))
2419               (if (stringp charset)
2420                   (setq charset (intern (downcase charset)))))))
2421       (if read-charset
2422           (setq charset (mm-read-coding-system "Charset: " charset)))
2423       (unless charset
2424         (setq charset gnus-newsgroup-charset))
2425       (when (or force
2426                 (and type (let ((case-fold-search t))
2427                             (string-match "base64" type))))
2428         (article-goto-body)
2429         (save-restriction
2430           (narrow-to-region (point) (point-max))
2431           (base64-decode-region (point-min) (point-max))
2432           (mm-decode-coding-region
2433            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2434
2435 (eval-when-compile
2436   (require 'rfc1843))
2437
2438 (defun article-decode-HZ ()
2439   "Translate a HZ-encoded article."
2440   (interactive)
2441   (require 'rfc1843)
2442   (save-excursion
2443     (let ((inhibit-read-only t))
2444       (rfc1843-decode-region (point-min) (point-max)))))
2445
2446 (defun article-unsplit-urls ()
2447   "Remove the newlines that some other mailers insert into URLs."
2448   (interactive)
2449   (save-excursion
2450     (let ((inhibit-read-only t))
2451       (goto-char (point-min))
2452       (while (re-search-forward
2453               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2454         (replace-match "\\1\\3" t)))
2455     (when (interactive-p)
2456       (gnus-treat-article nil))))
2457
2458
2459 (defun article-wash-html (&optional read-charset)
2460   "Format an HTML article.
2461 If READ-CHARSET, ask for a coding system."
2462   (interactive "P")
2463   (save-excursion
2464     (let ((inhibit-read-only t)
2465           charset)
2466       (when (gnus-buffer-live-p gnus-original-article-buffer)
2467         (with-current-buffer gnus-original-article-buffer
2468           (let* ((ct (gnus-fetch-field "content-type"))
2469                  (ctl (and ct
2470                            (ignore-errors
2471                              (mail-header-parse-content-type ct)))))
2472             (setq charset (and ctl
2473                                (mail-content-type-get ctl 'charset)))
2474             (when (stringp charset)
2475               (setq charset (intern (downcase charset)))))))
2476       (when read-charset
2477         (setq charset (mm-read-coding-system "Charset: " charset)))
2478       (unless charset
2479         (setq charset gnus-newsgroup-charset))
2480       (article-goto-body)
2481       (save-window-excursion
2482         (save-restriction
2483           (narrow-to-region (point) (point-max))
2484           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2485                  (entry (assq func mm-text-html-washer-alist)))
2486             (when entry
2487               (setq func (cdr entry)))
2488             (cond
2489              ((functionp func)
2490               (funcall func))
2491              (t
2492               (apply (car func) (cdr func))))))))))
2493
2494 (defun gnus-article-wash-html-with-w3 ()
2495   "Wash the current buffer with w3."
2496   (mm-setup-w3)
2497   (let ((w3-strict-width (window-width))
2498         (url-standalone-mode t)
2499         (url-gateway-unplugged t)
2500         (w3-honor-stylesheets nil))
2501     (condition-case ()
2502         (w3-region (point-min) (point-max))
2503       (error))))
2504
2505 (defun gnus-article-wash-html-with-w3m ()
2506   "Wash the current buffer with emacs-w3m."
2507   (mm-setup-w3m)
2508   (save-restriction
2509     (narrow-to-region (point) (point-max))
2510     (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2511           w3m-force-redisplay)
2512       (w3m-region (point-min) (point-max)))
2513     (when (and mm-inline-text-html-with-w3m-keymap
2514                (boundp 'w3m-minor-mode-map)
2515                w3m-minor-mode-map)
2516       (add-text-properties
2517        (point-min) (point-max)
2518        (list 'keymap w3m-minor-mode-map
2519              ;; Put the mark meaning this part was rendered by emacs-w3m.
2520              'mm-inline-text-html-with-w3m t)))))
2521
2522 (defun article-hide-list-identifiers ()
2523   "Remove list identifies from the Subject header.
2524 The `gnus-list-identifiers' variable specifies what to do."
2525   (interactive)
2526   (let ((inhibit-point-motion-hooks t)
2527         (regexp (if (consp gnus-list-identifiers)
2528                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2529                   gnus-list-identifiers))
2530         (inhibit-read-only t))
2531     (when regexp
2532       (save-excursion
2533         (save-restriction
2534           (article-narrow-to-head)
2535           (goto-char (point-min))
2536           (while (re-search-forward
2537                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2538                   nil t)
2539             (delete-region (match-beginning 2) (match-end 0))
2540             (beginning-of-line))
2541           (when (re-search-forward
2542                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2543             (delete-region (match-beginning 1) (match-end 1))))))))
2544
2545 (defun article-hide-pem (&optional arg)
2546   "Toggle hiding of any PEM headers and signatures in the current article.
2547 If given a negative prefix, always show; if given a positive prefix,
2548 always hide."
2549   (interactive (gnus-article-hidden-arg))
2550   (unless (gnus-article-check-hidden-text 'pem arg)
2551     (save-excursion
2552       (let ((inhibit-read-only t) end)
2553         (goto-char (point-min))
2554         ;; Hide the horrendously ugly "header".
2555         (when (and (search-forward
2556                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2557                     nil t)
2558                    (setq end (1+ (match-beginning 0))))
2559           (gnus-add-wash-type 'pem)
2560           (gnus-article-hide-text-type
2561            end
2562            (if (search-forward "\n\n" nil t)
2563                (match-end 0)
2564              (point-max))
2565            'pem)
2566           ;; Hide the trailer as well
2567           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2568                                 nil t)
2569             (gnus-article-hide-text-type
2570              (match-beginning 0) (match-end 0) 'pem)))))))
2571
2572 (defun article-strip-banner ()
2573   "Strip the banners specified by the `banner' group parameter and by
2574 `gnus-article-address-banner-alist'."
2575   (interactive)
2576   (save-excursion
2577     (save-restriction
2578       (let ((inhibit-point-motion-hooks t))
2579         (when (gnus-parameter-banner gnus-newsgroup-name)
2580           (article-really-strip-banner
2581            (gnus-parameter-banner gnus-newsgroup-name)))
2582         (when gnus-article-address-banner-alist
2583           ;; It is necessary to encode from fields before checking,
2584           ;; because `mail-header-parse-addresses' does not work
2585           ;; (reliably) on decoded headers.  And more, it is
2586           ;; impossible to use `gnus-fetch-original-field' here,
2587           ;; because `article-strip-banner' may be called in draft
2588           ;; buffers to preview them.
2589           (let ((from (save-restriction
2590                         (widen)
2591                         (article-narrow-to-head)
2592                         (mail-fetch-field "from"))))
2593             (when (and from
2594                        (setq from
2595                              (caar (mail-header-parse-addresses
2596                                     (mail-encode-encoded-word-string from)))))
2597               (catch 'found
2598                 (dolist (pair gnus-article-address-banner-alist)
2599                   (when (string-match (car pair) from)
2600                     (throw 'found
2601                            (article-really-strip-banner (cdr pair)))))))))))))
2602
2603 (defun article-really-strip-banner (banner)
2604   "Strip the banner specified by the argument."
2605   (save-excursion
2606     (save-restriction
2607       (let ((inhibit-point-motion-hooks t)
2608             (gnus-signature-limit nil)
2609             (inhibit-read-only t))
2610         (article-goto-body)
2611         (cond
2612          ((eq banner 'signature)
2613           (when (gnus-article-narrow-to-signature)
2614             (widen)
2615             (forward-line -1)
2616             (delete-region (point) (point-max))))
2617          ((symbolp banner)
2618           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2619               (while (re-search-forward banner nil t)
2620                 (delete-region (match-beginning 0) (match-end 0)))))
2621          ((stringp banner)
2622           (while (re-search-forward banner nil t)
2623             (delete-region (match-beginning 0) (match-end 0)))))))))
2624
2625 (defun article-babel ()
2626   "Translate article using an online translation service."
2627   (interactive)
2628   (require 'babel)
2629   (gnus-with-article-buffer
2630     (when (article-goto-body)
2631       (let* ((start (point))
2632              (end (point-max))
2633              (orig (buffer-substring start end))
2634              (trans (babel-as-string orig)))
2635         (save-restriction
2636           (narrow-to-region start end)
2637           (delete-region start end)
2638           (insert trans))))))
2639
2640 (defun article-hide-signature (&optional arg)
2641   "Hide the signature in the current article.
2642 If given a negative prefix, always show; if given a positive prefix,
2643 always hide."
2644   (interactive (gnus-article-hidden-arg))
2645   (unless (gnus-article-check-hidden-text 'signature arg)
2646     (save-excursion
2647       (save-restriction
2648         (let ((inhibit-read-only t))
2649           (when (gnus-article-narrow-to-signature)
2650             (gnus-article-hide-text-type
2651              (point-min) (point-max) 'signature))))))
2652   (gnus-set-mode-line 'article))
2653
2654 (defun article-strip-headers-in-body ()
2655   "Strip offensive headers from bodies."
2656   (interactive)
2657   (save-excursion
2658     (article-goto-body)
2659     (let ((case-fold-search t))
2660       (when (looking-at "x-no-archive:")
2661         (gnus-delete-line)))))
2662
2663 (defun article-strip-leading-blank-lines ()
2664   "Remove all blank lines from the beginning of the article."
2665   (interactive)
2666   (save-excursion
2667     (let ((inhibit-point-motion-hooks t)
2668           (inhibit-read-only t))
2669       (when (article-goto-body)
2670         (while (and (not (eobp))
2671                     (looking-at "[ \t]*$"))
2672           (gnus-delete-line))))))
2673
2674 (defun article-narrow-to-head ()
2675   "Narrow the buffer to the head of the message.
2676 Point is left at the beginning of the narrowed-to region."
2677   (narrow-to-region
2678    (goto-char (point-min))
2679    (if (search-forward "\n\n" nil 1)
2680        (1- (point))
2681      (point-max)))
2682   (goto-char (point-min)))
2683
2684 (defun article-goto-body ()
2685   "Place point at the start of the body."
2686   (goto-char (point-min))
2687   (cond
2688    ;; This variable is only bound when dealing with separate
2689    ;; MIME body parts.
2690    (article-goto-body-goes-to-point-min-p
2691     t)
2692    ((search-forward "\n\n" nil t)
2693     t)
2694    (t
2695     (goto-char (point-max))
2696     nil)))
2697
2698 (defun article-strip-multiple-blank-lines ()
2699   "Replace consecutive blank lines with one empty line."
2700   (interactive)
2701   (save-excursion
2702     (let ((inhibit-point-motion-hooks t)
2703           (inhibit-read-only t))
2704       ;; First make all blank lines empty.
2705       (article-goto-body)
2706       (while (re-search-forward "^[ \t]+$" nil t)
2707         (unless (gnus-annotation-in-region-p
2708                  (match-beginning 0) (match-end 0))
2709           (replace-match "" nil t)))
2710       ;; Then replace multiple empty lines with a single empty line.
2711       (article-goto-body)
2712       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2713         (unless (gnus-annotation-in-region-p
2714                  (match-beginning 0) (match-end 0))
2715           (delete-region (match-beginning 1) (match-end 1)))))))
2716
2717 (defun article-strip-leading-space ()
2718   "Remove all white space from the beginning of the lines in the article."
2719   (interactive)
2720   (save-excursion
2721     (let ((inhibit-point-motion-hooks t)
2722           (inhibit-read-only t))
2723       (article-goto-body)
2724       (while (re-search-forward "^[ \t]+" nil t)
2725         (replace-match "" t t)))))
2726
2727 (defun article-strip-trailing-space ()
2728   "Remove all white space from the end of the lines in the article."
2729   (interactive)
2730   (save-excursion
2731     (let ((inhibit-point-motion-hooks t)
2732           (inhibit-read-only t))
2733       (article-goto-body)
2734       (while (re-search-forward "[ \t]+$" nil t)
2735         (replace-match "" t t)))))
2736
2737 (defun article-strip-blank-lines ()
2738   "Strip leading, trailing and multiple blank lines."
2739   (interactive)
2740   (article-strip-leading-blank-lines)
2741   (article-remove-trailing-blank-lines)
2742   (article-strip-multiple-blank-lines))
2743
2744 (defun article-strip-all-blank-lines ()
2745   "Strip all blank lines."
2746   (interactive)
2747   (save-excursion
2748     (let ((inhibit-point-motion-hooks t)
2749           (inhibit-read-only t))
2750       (article-goto-body)
2751       (while (re-search-forward "^[ \t]*\n" nil t)
2752         (replace-match "" t t)))))
2753
2754 (defun gnus-article-narrow-to-signature ()
2755   "Narrow to the signature; return t if a signature is found, else nil."
2756   (let ((inhibit-point-motion-hooks t))
2757     (when (gnus-article-search-signature)
2758       (forward-line 1)
2759       ;; Check whether we have some limits to what we consider
2760       ;; to be a signature.
2761       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2762                       (list gnus-signature-limit)))
2763             limit limited)
2764         (while (setq limit (pop limits))
2765           (if (or (and (integerp limit)
2766                        (< (- (point-max) (point)) limit))
2767                   (and (floatp limit)
2768                        (< (count-lines (point) (point-max)) limit))
2769                   (and (functionp limit)
2770                        (funcall limit))
2771                   (and (stringp limit)
2772                        (not (re-search-forward limit nil t))))
2773               ()                        ; This limit did not succeed.
2774             (setq limited t
2775                   limits nil)))
2776         (unless limited
2777           (narrow-to-region (point) (point-max))
2778           t)))))
2779
2780 (defun gnus-article-search-signature ()
2781   "Search the current buffer for the signature separator.
2782 Put point at the beginning of the signature separator."
2783   (let ((cur (point)))
2784     (goto-char (point-max))
2785     (if (if (stringp gnus-signature-separator)
2786             (re-search-backward gnus-signature-separator nil t)
2787           (let ((seps gnus-signature-separator))
2788             (while (and seps
2789                         (not (re-search-backward (car seps) nil t)))
2790               (pop seps))
2791             seps))
2792         t
2793       (goto-char cur)
2794       nil)))
2795
2796 (defun gnus-article-hidden-arg ()
2797   "Return the current prefix arg as a number, or 0 if no prefix."
2798   (list (if current-prefix-arg
2799             (prefix-numeric-value current-prefix-arg)
2800           0)))
2801
2802 (defun gnus-article-check-hidden-text (type arg)
2803   "Return nil if hiding is necessary.
2804 Arg can be nil or a number.  nil and positive means hide, negative
2805 means show, 0 means toggle."
2806   (save-excursion
2807     (save-restriction
2808       (let ((hide (gnus-article-hidden-text-p type)))
2809         (cond
2810          ((or (null arg)
2811               (> arg 0))
2812           nil)
2813          ((< arg 0)
2814           (gnus-article-show-hidden-text type)
2815           t)
2816          (t
2817           (if (eq hide 'hidden)
2818               (progn
2819                 (gnus-article-show-hidden-text type)
2820                 t)
2821             nil)))))))
2822
2823 (defun gnus-article-hidden-text-p (type)
2824   "Say whether the current buffer contains hidden text of type TYPE."
2825   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2826     (while (and pos
2827                 (not (get-text-property pos 'invisible))
2828                 (not (get-text-property pos 'dummy-invisible)))
2829       (setq pos
2830             (text-property-any (1+ pos) (point-max) 'article-type type)))
2831     (if pos
2832         'hidden
2833       nil)))
2834
2835 (defun gnus-article-show-hidden-text (type &optional dummy)
2836   "Show all hidden text of type TYPE.
2837 Originally it is hide instead of DUMMY."
2838   (let ((inhibit-read-only t)
2839         (inhibit-point-motion-hooks t))
2840     (gnus-remove-text-properties-when
2841      'article-type type
2842      (point-min) (point-max)
2843      (cons 'article-type (cons type
2844                                gnus-hidden-properties)))
2845     (gnus-delete-wash-type type)))
2846
2847 (defconst article-time-units
2848   `((year . ,(* 365.25 24 60 60))
2849     (week . ,(* 7 24 60 60))
2850     (day . ,(* 24 60 60))
2851     (hour . ,(* 60 60))
2852     (minute . 60)
2853     (second . 1))
2854   "Mapping from time units to seconds.")
2855
2856 (defun gnus-article-forward-header ()
2857   "Move point to the start of the next header.
2858 If the current header is a continuation header, this can be several
2859 lines forward."
2860   (let ((ended nil))
2861     (while (not ended)
2862       (forward-line 1)
2863       (if (looking-at "[ \t]+[^ \t]")
2864           (forward-line 1)
2865         (setq ended t)))))
2866
2867 (defun article-date-ut (&optional type highlight)
2868   "Convert DATE date to universal time in the current article.
2869 If TYPE is `local', convert to local time; if it is `lapsed', output
2870 how much time has lapsed since DATE.  For `lapsed', the value of
2871 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2872 should replace the \"Date:\" one, or should be added below it."
2873   (interactive (list 'ut t))
2874   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
2875          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
2876                              tdate-regexp)
2877                             ((eq type 'lapsed)
2878                              "^X-Sent:[ \t]")
2879                             (article-lapsed-timer
2880                              "^Date:[ \t]")
2881                             (t
2882                              tdate-regexp)))
2883          (case-fold-search t)
2884          (inhibit-read-only t)
2885          (inhibit-point-motion-hooks t)
2886          pos date bface eface)
2887     (save-excursion
2888       (save-restriction
2889         (widen)
2890         (goto-char (point-min))
2891         (while (or (setq date (get-text-property (setq pos (point))
2892                                                  'original-date))
2893                    (when (setq pos (next-single-property-change
2894                                     (point) 'original-date))
2895                      (setq date (get-text-property pos 'original-date))
2896                      t))
2897           (narrow-to-region pos (or (text-property-any pos (point-max)
2898                                                        'original-date nil)
2899                                     (point-max)))
2900           (goto-char (point-min))
2901           (when (re-search-forward tdate-regexp nil t)
2902             (setq bface (get-text-property (point-at-bol) 'face)
2903                   eface (get-text-property (1- (point-at-eol)) 'face)))
2904           (goto-char (point-min))
2905           (setq pos nil)
2906           ;; Delete any old Date headers.
2907           (while (re-search-forward date-regexp nil t)
2908             (if pos
2909                 (delete-region (point-at-bol) (progn
2910                                                 (gnus-article-forward-header)
2911                                                 (point)))
2912               (delete-region (point-at-bol) (progn
2913                                               (gnus-article-forward-header)
2914                                               (forward-char -1)
2915                                               (point)))
2916               (setq pos (point))))
2917           (when (and (not pos)
2918                      (re-search-forward tdate-regexp nil t))
2919             (forward-line 1))
2920           (gnus-goto-char pos)
2921           (insert (article-make-date-line date (or type 'ut)))
2922           (unless pos
2923             (insert "\n")
2924             (forward-line -1))
2925           ;; Do highlighting.
2926           (beginning-of-line)
2927           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2928             (put-text-property (match-beginning 1) (1+ (match-end 1))
2929                                'face bface)
2930             (put-text-property (match-beginning 2) (match-end 2)
2931                                'face eface))
2932           (put-text-property (point-min) (1- (point-max)) 'original-date date)
2933           (goto-char (point-max))
2934           (widen))))))
2935
2936 (defun article-make-date-line (date type)
2937   "Return a DATE line of TYPE."
2938   (unless (memq type '(local ut original user iso8601 lapsed english))
2939     (error "Unknown conversion type: %s" type))
2940   (condition-case ()
2941       (let ((time (date-to-time date)))
2942         (cond
2943          ;; Convert to the local timezone.
2944          ((eq type 'local)
2945           (concat "Date: " (message-make-date time)))
2946          ;; Convert to Universal Time.
2947          ((eq type 'ut)
2948           (concat "Date: "
2949                   (substring
2950                    (message-make-date
2951                     (let* ((e (parse-time-string date))
2952                            (tm (apply 'encode-time e))
2953                            (ms (car tm))
2954                            (ls (- (cadr tm) (car (current-time-zone time)))))
2955                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2956                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
2957                             (t (list ms ls)))))
2958                    0 -5)
2959                   "UT"))
2960          ;; Get the original date from the article.
2961          ((eq type 'original)
2962           (concat "Date: " (if (string-match "\n+$" date)
2963                                (substring date 0 (match-beginning 0))
2964                              date)))
2965          ;; Let the user define the format.
2966          ((eq type 'user)
2967           (let ((format (or (condition-case nil
2968                                 (with-current-buffer gnus-summary-buffer
2969                                   gnus-article-time-format)
2970                               (error nil))
2971                             gnus-article-time-format)))
2972             (if (functionp format)
2973                 (funcall format time)
2974               (concat "Date: " (format-time-string format time)))))
2975          ;; ISO 8601.
2976          ((eq type 'iso8601)
2977           (let ((tz (car (current-time-zone time))))
2978             (concat
2979              "Date: "
2980              (format-time-string "%Y%m%dT%H%M%S" time)
2981              (format "%s%02d%02d"
2982                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2983                      (/ (% (abs tz) 3600) 60)))))
2984          ;; Do an X-Sent lapsed format.
2985          ((eq type 'lapsed)
2986           ;; If the date is seriously mangled, the timezone functions are
2987           ;; liable to bug out, so we ignore all errors.
2988           (let* ((now (current-time))
2989                  (real-time (subtract-time now time))
2990                  (real-sec (and real-time
2991                                 (+ (* (float (car real-time)) 65536)
2992                                    (cadr real-time))))
2993                  (sec (and real-time (abs real-sec)))
2994                  num prev)
2995             (cond
2996              ((null real-time)
2997               "X-Sent: Unknown")
2998              ((zerop sec)
2999               "X-Sent: Now")
3000              (t
3001               (concat
3002                "X-Sent: "
3003                ;; This is a bit convoluted, but basically we go
3004                ;; through the time units for years, weeks, etc,
3005                ;; and divide things to see whether that results
3006                ;; in positive answers.
3007                (mapconcat
3008                 (lambda (unit)
3009                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3010                       ;; The (remaining) seconds are too few to
3011                       ;; be divided into this time unit.
3012                       ""
3013                     ;; It's big enough, so we output it.
3014                     (setq sec (- sec (* num (cdr unit))))
3015                     (prog1
3016                         (concat (if prev ", " "") (int-to-string
3017                                                    (floor num))
3018                                 " " (symbol-name (car unit))
3019                                 (if (> num 1) "s" ""))
3020                       (setq prev t))))
3021                 article-time-units "")
3022                ;; If dates are odd, then it might appear like the
3023                ;; article was sent in the future.
3024                (if (> real-sec 0)
3025                    " ago"
3026                  " in the future"))))))
3027          ;; Display the date in proper English
3028          ((eq type 'english)
3029           (let ((dtime (decode-time time)))
3030             (concat
3031              "Date: the "
3032              (number-to-string (nth 3 dtime))
3033              (let ((digit (% (nth 3 dtime) 10)))
3034                (cond
3035                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3036                 ((= digit 1) "st")
3037                 ((= digit 2) "nd")
3038                 ((= digit 3) "rd")
3039                 (t "th")))
3040              " of "
3041              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3042              " "
3043              (number-to-string (nth 5 dtime))
3044              " at "
3045              (format "%02d" (nth 2 dtime))
3046              ":"
3047              (format "%02d" (nth 1 dtime)))))))
3048     (error
3049      (format "Date: %s (from Gnus)" date))))
3050
3051 (defun article-date-local (&optional highlight)
3052   "Convert the current article date to the local timezone."
3053   (interactive (list t))
3054   (article-date-ut 'local highlight))
3055
3056 (defun article-date-english (&optional highlight)
3057   "Convert the current article date to something that is proper English."
3058   (interactive (list t))
3059   (article-date-ut 'english highlight))
3060
3061 (defun article-date-original (&optional highlight)
3062   "Convert the current article date to what it was originally.
3063 This is only useful if you have used some other date conversion
3064 function and want to see what the date was before converting."
3065   (interactive (list t))
3066   (article-date-ut 'original highlight))
3067
3068 (defun article-date-lapsed (&optional highlight)
3069   "Convert the current article date to time lapsed since it was sent."
3070   (interactive (list t))
3071   (article-date-ut 'lapsed highlight))
3072
3073 (defun article-update-date-lapsed ()
3074   "Function to be run from a timer to update the lapsed time line."
3075   (let (deactivate-mark)
3076     (save-excursion
3077       (ignore-errors
3078         (walk-windows
3079          (lambda (w)
3080            (set-buffer (window-buffer w))
3081            (when (eq major-mode 'gnus-article-mode)
3082              (let ((mark (point-marker)))
3083                (goto-char (point-min))
3084                (when (re-search-forward "^X-Sent:" nil t)
3085                  (article-date-lapsed t))
3086                (goto-char (marker-position mark))
3087                (move-marker mark nil))))
3088          nil 'visible)))))
3089
3090 (defun gnus-start-date-timer (&optional n)
3091   "Start a timer to update the X-Sent header in the article buffers.
3092 The numerical prefix says how frequently (in seconds) the function
3093 is to run."
3094   (interactive "p")
3095   (unless n
3096     (setq n 1))
3097   (gnus-stop-date-timer)
3098   (setq article-lapsed-timer
3099         (run-at-time 1 n 'article-update-date-lapsed)))
3100
3101 (defun gnus-stop-date-timer ()
3102   "Stop the X-Sent timer."
3103   (interactive)
3104   (when article-lapsed-timer
3105     (nnheader-cancel-timer article-lapsed-timer)
3106     (setq article-lapsed-timer nil)))
3107
3108 (defun article-date-user (&optional highlight)
3109   "Convert the current article date to the user-defined format.
3110 This format is defined by the `gnus-article-time-format' variable."
3111   (interactive (list t))
3112   (article-date-ut 'user highlight))
3113
3114 (defun article-date-iso8601 (&optional highlight)
3115   "Convert the current article date to ISO8601."
3116   (interactive (list t))
3117   (article-date-ut 'iso8601 highlight))
3118
3119 (defmacro gnus-article-save-original-date (&rest forms)
3120   "Save the original date as a text property and evaluate FORMS."
3121   `(let* ((case-fold-search t)
3122           (start (progn
3123                    (goto-char (point-min))
3124                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3125                               (not (bolp)))
3126                      (match-end 0))))
3127           (date (when (and start
3128                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3129                                               nil t))
3130                   (buffer-substring-no-properties start
3131                                                   (match-beginning 0)))))
3132      (goto-char (point-max))
3133      (skip-chars-backward "\n")
3134      (put-text-property (point-min) (point) 'original-date date)
3135      ,@forms
3136      (goto-char (point-max))
3137      (skip-chars-backward "\n")
3138      (put-text-property (point-min) (point) 'original-date date)))
3139
3140 ;; (defun article-show-all ()
3141 ;;   "Show all hidden text in the article buffer."
3142 ;;   (interactive)
3143 ;;   (save-excursion
3144 ;;     (let ((inhibit-read-only t))
3145 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3146
3147 (defun article-remove-leading-whitespace ()
3148   "Remove excessive whitespace from all headers."
3149   (interactive)
3150   (save-excursion
3151     (save-restriction
3152       (let ((inhibit-read-only t))
3153         (article-narrow-to-head)
3154         (goto-char (point-min))
3155         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3156           (delete-region (match-beginning 1) (match-end 1)))))))
3157
3158 (defun article-emphasize (&optional arg)
3159   "Emphasize text according to `gnus-emphasis-alist'."
3160   (interactive (gnus-article-hidden-arg))
3161   (unless (gnus-article-check-hidden-text 'emphasis arg)
3162     (save-excursion
3163       (let ((alist (or
3164                     (condition-case nil
3165                         (with-current-buffer gnus-summary-buffer
3166                           gnus-article-emphasis-alist)
3167                       (error))
3168                     gnus-emphasis-alist))
3169             (inhibit-read-only t)
3170             (props (append '(article-type emphasis)
3171                            gnus-hidden-properties))
3172             regexp elem beg invisible visible face)
3173         (article-goto-body)
3174         (setq beg (point))
3175         (while (setq elem (pop alist))
3176           (goto-char beg)
3177           (setq regexp (car elem)
3178                 invisible (nth 1 elem)
3179                 visible (nth 2 elem)
3180                 face (nth 3 elem))
3181           (while (re-search-forward regexp nil t)
3182             (when (and (match-beginning visible) (match-beginning invisible))
3183               (gnus-article-hide-text
3184                (match-beginning invisible) (match-end invisible) props)
3185               (gnus-article-unhide-text-type
3186                (match-beginning visible) (match-end visible) 'emphasis)
3187               (gnus-put-overlay-excluding-newlines
3188                (match-beginning visible) (match-end visible) 'face face)
3189               (gnus-add-wash-type 'emphasis)
3190               (goto-char (match-end invisible)))))))))
3191
3192 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3193   "Setup newsgroup emphasis alist."
3194   (unless gnus-article-emphasis-alist
3195     (let ((name (and gnus-newsgroup-name
3196                      (gnus-group-real-name gnus-newsgroup-name))))
3197       (make-local-variable 'gnus-article-emphasis-alist)
3198       (setq gnus-article-emphasis-alist
3199             (nconc
3200              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3201                (while (setq elem (pop alist))
3202                  (when (and name (string-match (car elem) name))
3203                    (setq alist nil
3204                          highlight (copy-sequence (cdr elem)))))
3205                highlight)
3206              (copy-sequence highlight-words)
3207              (if gnus-newsgroup-name
3208                  (copy-sequence (gnus-group-find-parameter
3209                                  gnus-newsgroup-name 'highlight-words t)))
3210              gnus-emphasis-alist)))))
3211
3212 (eval-when-compile
3213   (defvar gnus-summary-article-menu)
3214   (defvar gnus-summary-post-menu))
3215
3216 ;;; Saving functions.
3217
3218 (defun gnus-article-save (save-buffer file &optional num)
3219   "Save the currently selected article."
3220   (unless gnus-save-all-headers
3221     ;; Remove headers according to `gnus-saved-headers'.
3222     (let ((gnus-visible-headers
3223            (or gnus-saved-headers gnus-visible-headers))
3224           (gnus-article-buffer save-buffer))
3225       (save-excursion
3226         (set-buffer save-buffer)
3227         (article-hide-headers 1 t))))
3228   (save-window-excursion
3229     (if (not gnus-default-article-saver)
3230         (error "No default saver is defined")
3231       ;; !!! Magic!  The saving functions all save
3232       ;; `gnus-save-article-buffer' (or so they think), but we
3233       ;; bind that variable to our save-buffer.
3234       (set-buffer gnus-article-buffer)
3235       (let* ((gnus-save-article-buffer save-buffer)
3236              (filename
3237               (cond
3238                ((not gnus-prompt-before-saving) 'default)
3239                ((eq gnus-prompt-before-saving 'always) nil)
3240                (t file)))
3241              (gnus-number-of-articles-to-be-saved
3242               (when (eq gnus-prompt-before-saving t)
3243                 num)))                  ; Magic
3244         (set-buffer gnus-article-current-summary)
3245         (funcall gnus-default-article-saver filename)))))
3246
3247 (defun gnus-read-save-file-name (prompt &optional filename
3248                                         function group headers variable)
3249   (let ((default-name
3250           (funcall function group headers (symbol-value variable)))
3251         result)
3252     (setq result
3253           (expand-file-name
3254            (cond
3255             ((eq filename 'default)
3256              default-name)
3257             ((eq filename t)
3258              default-name)
3259             (filename filename)
3260             (t
3261              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3262                     (prompt
3263                      (format prompt
3264                              (if (and gnus-number-of-articles-to-be-saved
3265                                       (> gnus-number-of-articles-to-be-saved 1))
3266                                  (format "these %d articles"
3267                                          gnus-number-of-articles-to-be-saved)
3268                                "this article")))
3269                     (file
3270                      ;; Let the split methods have their say.
3271                      (cond
3272                       ;; No split name was found.
3273                       ((null split-name)
3274                        (read-file-name
3275                         (concat prompt " (default "
3276                                 (file-name-nondirectory default-name) ") ")
3277                         (file-name-directory default-name)
3278                         default-name))
3279                       ;; A single group name is returned.
3280                       ((stringp split-name)
3281                        (setq default-name
3282                              (funcall function split-name headers
3283                                       (symbol-value variable)))
3284                        (read-file-name
3285                         (concat prompt " (default "
3286                                 (file-name-nondirectory default-name) ") ")
3287                         (file-name-directory default-name)
3288                         default-name))
3289                       ;; A single split name was found
3290                       ((= 1 (length split-name))
3291                        (let* ((name (expand-file-name
3292                                      (car split-name)
3293                                      gnus-article-save-directory))
3294                               (dir (cond ((file-directory-p name)
3295                                           (file-name-as-directory name))
3296                                          ((file-exists-p name) name)
3297                                          (t gnus-article-save-directory))))
3298                          (read-file-name
3299                           (concat prompt " (default " name ") ")
3300                           dir name)))
3301                       ;; A list of splits was found.
3302                       (t
3303                        (setq split-name (nreverse split-name))
3304                        (let (result)
3305                          (let ((file-name-history
3306                                 (nconc split-name file-name-history)))
3307                            (setq result
3308                                  (expand-file-name
3309                                   (read-file-name
3310                                    (concat prompt " (`M-p' for defaults) ")
3311                                    gnus-article-save-directory
3312                                    (car split-name))
3313                                   gnus-article-save-directory)))
3314                          (car (push result file-name-history)))))))
3315                ;; Create the directory.
3316                (gnus-make-directory (file-name-directory file))
3317                ;; If we have read a directory, we append the default file name.
3318                (when (file-directory-p file)
3319                  (setq file (expand-file-name (file-name-nondirectory
3320                                                default-name)
3321                                               (file-name-as-directory file))))
3322                ;; Possibly translate some characters.
3323                (nnheader-translate-file-chars file))))))
3324     (gnus-make-directory (file-name-directory result))
3325     (set variable result)))
3326
3327 (defun gnus-article-archive-name (group)
3328   "Return the first instance of an \"Archive-name\" in the current buffer."
3329   (let ((case-fold-search t))
3330     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3331       (nnheader-concat gnus-article-save-directory
3332                        (match-string 1)))))
3333
3334 (defun gnus-article-nndoc-name (group)
3335   "If GROUP is an nndoc group, return the name of the parent group."
3336   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3337     (gnus-group-get-parameter group 'save-article-group)))
3338
3339 (defun gnus-summary-save-in-rmail (&optional filename)
3340   "Append this article to Rmail file.
3341 Optional argument FILENAME specifies file name.
3342 Directory to save to is default to `gnus-article-save-directory'."
3343   (setq filename (gnus-read-save-file-name
3344                   "Save %s in rmail file:" filename
3345                   gnus-rmail-save-name gnus-newsgroup-name
3346                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3347   (gnus-eval-in-buffer-window gnus-save-article-buffer
3348     (save-excursion
3349       (save-restriction
3350         (widen)
3351         (gnus-output-to-rmail filename))))
3352   filename)
3353
3354 (defun gnus-summary-save-in-mail (&optional filename)
3355   "Append this article to Unix mail file.
3356 Optional argument FILENAME specifies file name.
3357 Directory to save to is default to `gnus-article-save-directory'."
3358   (setq filename (gnus-read-save-file-name
3359                   "Save %s in Unix mail file:" filename
3360                   gnus-mail-save-name gnus-newsgroup-name
3361                   gnus-current-headers 'gnus-newsgroup-last-mail))
3362   (gnus-eval-in-buffer-window gnus-save-article-buffer
3363     (save-excursion
3364       (save-restriction
3365         (widen)
3366         (if (and (file-readable-p filename)
3367                  (file-regular-p filename)
3368                  (mail-file-babyl-p filename))
3369             (rmail-output-to-rmail-file filename t)
3370           (gnus-output-to-mail filename)))))
3371   filename)
3372
3373 (defun gnus-summary-save-in-file (&optional filename overwrite)
3374   "Append this article to file.
3375 Optional argument FILENAME specifies file name.
3376 Directory to save to is default to `gnus-article-save-directory'."
3377   (setq filename (gnus-read-save-file-name
3378                   "Save %s in file:" filename
3379                   gnus-file-save-name gnus-newsgroup-name
3380                   gnus-current-headers 'gnus-newsgroup-last-file))
3381   (gnus-eval-in-buffer-window gnus-save-article-buffer
3382     (save-excursion
3383       (save-restriction
3384         (widen)
3385         (when (and overwrite
3386                    (file-exists-p filename))
3387           (delete-file filename))
3388         (gnus-output-to-file filename))))
3389   filename)
3390
3391 (defun gnus-summary-write-to-file (&optional filename)
3392   "Write this article to a file, overwriting it if the file exists.
3393 Optional argument FILENAME specifies file name.
3394 The directory to save in defaults to `gnus-article-save-directory'."
3395   (gnus-summary-save-in-file nil t))
3396
3397 (defun gnus-summary-save-body-in-file (&optional filename)
3398   "Append this article body to a file.
3399 Optional argument FILENAME specifies file name.
3400 The directory to save in defaults to `gnus-article-save-directory'."
3401   (setq filename (gnus-read-save-file-name
3402                   "Save %s body in file:" filename
3403                   gnus-file-save-name gnus-newsgroup-name
3404                   gnus-current-headers 'gnus-newsgroup-last-file))
3405   (gnus-eval-in-buffer-window gnus-save-article-buffer
3406     (save-excursion
3407       (save-restriction
3408         (widen)
3409         (when (article-goto-body)
3410           (narrow-to-region (point) (point-max)))
3411         (gnus-output-to-file filename))))
3412   filename)
3413
3414 (defun gnus-summary-save-in-pipe (&optional command)
3415   "Pipe this article to subprocess."
3416   (setq command
3417         (cond ((and (eq command 'default)
3418                     gnus-last-shell-command)
3419                gnus-last-shell-command)
3420               ((stringp command)
3421                command)
3422               (t (read-string
3423                   (format
3424                    "Shell command on %s: "
3425                    (if (and gnus-number-of-articles-to-be-saved
3426                             (> gnus-number-of-articles-to-be-saved 1))
3427                        (format "these %d articles"
3428                                gnus-number-of-articles-to-be-saved)
3429                      "this article"))
3430                   gnus-last-shell-command))))
3431   (when (string-equal command "")
3432     (if gnus-last-shell-command
3433         (setq command gnus-last-shell-command)
3434       (error "A command is required")))
3435   (gnus-eval-in-buffer-window gnus-article-buffer
3436     (save-restriction
3437       (widen)
3438       (shell-command-on-region (point-min) (point-max) command nil)))
3439   (setq gnus-last-shell-command command))
3440
3441 (defun gnus-summary-pipe-to-muttprint (&optional command)
3442   "Pipe this article to muttprint."
3443   (setq command (read-string
3444                  "Print using command: " gnus-summary-muttprint-program
3445                  nil gnus-summary-muttprint-program))
3446   (gnus-summary-save-in-pipe command))
3447
3448 ;;; Article file names when saving.
3449
3450 (defun gnus-capitalize-newsgroup (newsgroup)
3451   "Capitalize NEWSGROUP name."
3452   (when (not (zerop (length newsgroup)))
3453     (concat (char-to-string (upcase (aref newsgroup 0)))
3454             (substring newsgroup 1))))
3455
3456 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3457   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3458 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3459 Otherwise, it is like ~/News/news/group/num."
3460   (let ((default
3461           (expand-file-name
3462            (concat (if (gnus-use-long-file-name 'not-save)
3463                        (gnus-capitalize-newsgroup newsgroup)
3464                      (gnus-newsgroup-directory-form newsgroup))
3465                    "/" (int-to-string (mail-header-number headers)))
3466            gnus-article-save-directory)))
3467     (if (and last-file
3468              (string-equal (file-name-directory default)
3469                            (file-name-directory last-file))
3470              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3471         default
3472       (or last-file default))))
3473
3474 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3475   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3476 If variable `gnus-use-long-file-name' is non-nil, it is
3477 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3478   (let ((default
3479           (expand-file-name
3480            (concat (if (gnus-use-long-file-name 'not-save)
3481                        newsgroup
3482                      (gnus-newsgroup-directory-form newsgroup))
3483                    "/" (int-to-string (mail-header-number headers)))
3484            gnus-article-save-directory)))
3485     (if (and last-file
3486              (string-equal (file-name-directory default)
3487                            (file-name-directory last-file))
3488              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3489         default
3490       (or last-file default))))
3491
3492 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3493   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3494 If variable `gnus-use-long-file-name' is non-nil, it is
3495 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3496   (or last-file
3497       (expand-file-name
3498        (if (gnus-use-long-file-name 'not-save)
3499            newsgroup
3500          (file-relative-name
3501           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3502           default-directory))
3503        gnus-article-save-directory)))
3504
3505 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3506   "Generate file name from sender."
3507   (let ((from (mail-header-from headers)))
3508     (expand-file-name
3509      (if (and from (string-match "\\([^ <]+\\)@" from))
3510          (match-string 1 from)
3511        "nobody")
3512      gnus-article-save-directory)))
3513
3514 (defun article-verify-x-pgp-sig ()
3515   "Verify X-PGP-Sig."
3516   (interactive)
3517   (if (gnus-buffer-live-p gnus-original-article-buffer)
3518       (let ((sig (with-current-buffer gnus-original-article-buffer
3519                    (gnus-fetch-field "X-PGP-Sig")))
3520             items info headers)
3521         (when (and sig
3522                    mml2015-use
3523                    (mml2015-clear-verify-function))
3524           (with-temp-buffer
3525             (insert-buffer-substring gnus-original-article-buffer)
3526             (setq items (split-string sig))
3527             (message-narrow-to-head)
3528             (let ((inhibit-point-motion-hooks t)
3529                   (case-fold-search t))
3530               ;; Don't verify multiple headers.
3531               (setq headers (mapconcat (lambda (header)
3532                                          (concat header ": "
3533                                                  (mail-fetch-field header)
3534                                                  "\n"))
3535                                        (split-string (nth 1 items) ",") "")))
3536             (delete-region (point-min) (point-max))
3537             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3538             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3539             (insert headers)
3540             (widen)
3541             (forward-line)
3542             (while (not (eobp))
3543               (if (looking-at "^-")
3544                   (insert "- "))
3545               (forward-line))
3546             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3547             (insert "Version: " (car items) "\n\n")
3548             (insert (mapconcat 'identity (cddr items) "\n"))
3549             (insert "\n-----END PGP SIGNATURE-----\n")
3550             (let ((mm-security-handle (list (format "multipart/signed"))))
3551               (mml2015-clean-buffer)
3552               (let ((coding-system-for-write (or gnus-newsgroup-charset
3553                                                  'iso-8859-1)))
3554                 (funcall (mml2015-clear-verify-function)))
3555               (setq info
3556                     (or (mm-handle-multipart-ctl-parameter
3557                          mm-security-handle 'gnus-details)
3558                         (mm-handle-multipart-ctl-parameter
3559                          mm-security-handle 'gnus-info)))))
3560           (when info
3561             (let ((inhibit-read-only t) bface eface)
3562               (save-restriction
3563                 (message-narrow-to-head)
3564                 (goto-char (point-max))
3565                 (forward-line -1)
3566                 (setq bface (get-text-property (point-at-bol) 'face)
3567                       eface (get-text-property (1- (point-at-eol)) 'face))
3568                 (message-remove-header "X-Gnus-PGP-Verify")
3569                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3570                     (forward-line)
3571                   (goto-char (point-max)))
3572                 (narrow-to-region (point) (point))
3573                 (insert "X-Gnus-PGP-Verify: " info "\n")
3574                 (goto-char (point-min))
3575                 (forward-line)
3576                 (while (not (eobp))
3577                   (if (not (looking-at "^[ \t]"))
3578                       (insert " "))
3579                   (forward-line))
3580                 ;; Do highlighting.
3581                 (goto-char (point-min))
3582                 (when (looking-at "\\([^:]+\\): *")
3583                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3584                                      'face bface)
3585                   (put-text-property (match-end 0) (point-max)
3586                                      'face eface)))))))))
3587
3588 (defun article-verify-cancel-lock ()
3589   "Verify Cancel-Lock header."
3590   (interactive)
3591   (if (gnus-buffer-live-p gnus-original-article-buffer)
3592       (canlock-verify gnus-original-article-buffer)))
3593
3594 (eval-and-compile
3595   (mapcar
3596    (lambda (func)
3597      (let (afunc gfunc)
3598        (if (consp func)
3599            (setq afunc (car func)
3600                  gfunc (cdr func))
3601          (setq afunc func
3602                gfunc (intern (format "gnus-%s" func))))
3603        (defalias gfunc
3604          (when (fboundp afunc)
3605            `(lambda (&optional interactive &rest args)
3606               ,(documentation afunc t)
3607               (interactive (list t))
3608               (save-excursion
3609                 (set-buffer gnus-article-buffer)
3610                 (if interactive
3611                     (call-interactively ',afunc)
3612                   (apply ',afunc args))))))))
3613    '(article-hide-headers
3614      article-verify-x-pgp-sig
3615      article-verify-cancel-lock
3616      article-hide-boring-headers
3617      article-treat-overstrike
3618      article-treat-ansi-sequences
3619      article-fill-long-lines
3620      article-capitalize-sentences
3621      article-remove-cr
3622      article-remove-leading-whitespace
3623      article-display-x-face
3624      article-display-face
3625      article-de-quoted-unreadable
3626      article-de-base64-unreadable
3627      article-decode-HZ
3628      article-wash-html
3629      article-unsplit-urls
3630      article-hide-list-identifiers
3631      article-strip-banner
3632      article-babel
3633      article-hide-pem
3634      article-hide-signature
3635      article-strip-headers-in-body
3636      article-remove-trailing-blank-lines
3637      article-strip-leading-blank-lines
3638      article-strip-multiple-blank-lines
3639      article-strip-leading-space
3640      article-strip-trailing-space
3641      article-strip-blank-lines
3642      article-strip-all-blank-lines
3643      article-date-local
3644      article-date-english
3645      article-date-iso8601
3646      article-date-original
3647      article-date-ut
3648      article-decode-mime-words
3649      article-decode-charset
3650      article-decode-encoded-words
3651      article-date-user
3652      article-date-lapsed
3653      article-emphasize
3654      article-treat-dumbquotes
3655      article-normalize-headers
3656 ;;     (article-show-all . gnus-article-show-all-headers)
3657      )))
3658 \f
3659 ;;;
3660 ;;; Gnus article mode
3661 ;;;
3662
3663 (put 'gnus-article-mode 'mode-class 'special)
3664
3665 (set-keymap-parent gnus-article-mode-map widget-keymap)
3666
3667 (gnus-define-keys gnus-article-mode-map
3668   " " gnus-article-goto-next-page
3669   "\177" gnus-article-goto-prev-page
3670   [delete] gnus-article-goto-prev-page
3671   [backspace] gnus-article-goto-prev-page
3672   "\C-c^" gnus-article-refer-article
3673   "h" gnus-article-show-summary
3674   "s" gnus-article-show-summary
3675   "\C-c\C-m" gnus-article-mail
3676   "?" gnus-article-describe-briefly
3677   "e" gnus-summary-edit-article
3678   "<" beginning-of-buffer
3679   ">" end-of-buffer
3680   "\C-c\C-i" gnus-info-find-node
3681   "\C-c\C-b" gnus-bug
3682   "R" gnus-article-reply-with-original
3683   "F" gnus-article-followup-with-original
3684   "\C-hk" gnus-article-describe-key
3685   "\C-hc" gnus-article-describe-key-briefly
3686
3687   "\C-d" gnus-article-read-summary-keys
3688   "\M-*" gnus-article-read-summary-keys
3689   "\M-#" gnus-article-read-summary-keys
3690   "\M-^" gnus-article-read-summary-keys
3691   "\M-g" gnus-article-read-summary-keys)
3692
3693 (substitute-key-definition
3694  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
3695
3696 (defun gnus-article-make-menu-bar ()
3697   (unless (boundp 'gnus-article-commands-menu)
3698     (gnus-summary-make-menu-bar))
3699   (gnus-turn-off-edit-menu 'article)
3700   (unless (boundp 'gnus-article-article-menu)
3701     (easy-menu-define
3702      gnus-article-article-menu gnus-article-mode-map ""
3703      '("Article"
3704        ["Scroll forwards" gnus-article-goto-next-page t]
3705        ["Scroll backwards" gnus-article-goto-prev-page t]
3706        ["Show summary" gnus-article-show-summary t]
3707        ["Fetch Message-ID at point" gnus-article-refer-article t]
3708        ["Mail to address at point" gnus-article-mail t]
3709        ["Send a bug report" gnus-bug t]))
3710
3711     (easy-menu-define
3712      gnus-article-treatment-menu gnus-article-mode-map ""
3713      ;; Fixme: this should use :active (and maybe :visible).
3714      '("Treatment"
3715        ["Hide headers" gnus-article-hide-headers t]
3716        ["Hide signature" gnus-article-hide-signature t]
3717        ["Hide citation" gnus-article-hide-citation t]
3718        ["Treat overstrike" gnus-article-treat-overstrike t]
3719        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3720        ["Remove carriage return" gnus-article-remove-cr t]
3721        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3722        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
3723        ["Remove base64" gnus-article-de-base64-unreadable t]
3724        ["Treat html" gnus-article-wash-html t]
3725        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3726        ["Decode HZ" gnus-article-decode-HZ t]))
3727
3728     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3729
3730     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3731
3732     (gnus-run-hooks 'gnus-article-menu-hook)))
3733
3734 (defun gnus-article-mode ()
3735   "Major mode for displaying an article.
3736
3737 All normal editing commands are switched off.
3738
3739 The following commands are available in addition to all summary mode
3740 commands:
3741 \\<gnus-article-mode-map>
3742 \\[gnus-article-next-page]\t Scroll the article one page forwards
3743 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3744 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3745 \\[gnus-article-show-summary]\t Display the summary buffer
3746 \\[gnus-article-mail]\t Send a reply to the address near point
3747 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3748 \\[gnus-info-find-node]\t Go to the Gnus info node"
3749   (interactive)
3750   (gnus-simplify-mode-line)
3751   (setq mode-name "Article")
3752   (setq major-mode 'gnus-article-mode)
3753   (make-local-variable 'minor-mode-alist)
3754   (use-local-map gnus-article-mode-map)
3755   (when (gnus-visual-p 'article-menu 'menu)
3756     (gnus-article-make-menu-bar)
3757     (when gnus-summary-tool-bar-map
3758       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3759   (gnus-update-format-specifications nil 'article-mode)
3760   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3761   (set (make-local-variable 'gnus-page-broken) nil)
3762   (make-local-variable 'gnus-button-marker-list)
3763   (make-local-variable 'gnus-article-current-summary)
3764   (make-local-variable 'gnus-article-mime-handles)
3765   (make-local-variable 'gnus-article-decoded-p)
3766   (make-local-variable 'gnus-article-mime-handle-alist)
3767   (make-local-variable 'gnus-article-wash-types)
3768   (make-local-variable 'gnus-article-image-alist)
3769   (make-local-variable 'gnus-article-charset)
3770   (make-local-variable 'gnus-article-ignored-charsets)
3771   ;; Prevent recent Emacsen from displaying non-break space as "\ ".
3772   (set (make-local-variable 'show-nonbreak-escape) nil)
3773   (gnus-set-default-directory)
3774   (buffer-disable-undo)
3775   (setq buffer-read-only t
3776         show-trailing-whitespace nil)
3777   (set-syntax-table gnus-article-mode-syntax-table)
3778   (mm-enable-multibyte)
3779   (gnus-run-mode-hooks 'gnus-article-mode-hook))
3780
3781 (defun gnus-article-setup-buffer ()
3782   "Initialize the article buffer."
3783   (let* ((name (if gnus-single-article-buffer "*Article*"
3784                  (concat "*Article " gnus-newsgroup-name "*")))
3785          (original
3786           (progn (string-match "\\*Article" name)
3787                  (concat " *Original Article"
3788                          (substring name (match-end 0))))))
3789     (setq gnus-article-buffer name)
3790     (setq gnus-original-article-buffer original)
3791     (setq gnus-article-mime-handle-alist nil)
3792     ;; This might be a variable local to the summary buffer.
3793     (unless gnus-single-article-buffer
3794       (save-excursion
3795         (set-buffer gnus-summary-buffer)
3796         (setq gnus-article-buffer name)
3797         (setq gnus-original-article-buffer original)
3798         (gnus-set-global-variables)))
3799     (gnus-article-setup-highlight-words)
3800     ;; Init original article buffer.
3801     (save-excursion
3802       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3803       (mm-enable-multibyte)
3804       (setq major-mode 'gnus-original-article-mode)
3805       (make-local-variable 'gnus-original-article))
3806     (if (and (get-buffer name)
3807              (with-current-buffer name
3808                (if gnus-article-edit-mode
3809                    (if (y-or-n-p "Article mode edit in progress; discard? ")
3810                        (progn
3811                          (set-buffer-modified-p nil)
3812                          (gnus-kill-buffer name)
3813                          (message "")
3814                          nil)
3815                      (error "Action aborted"))
3816                  t)))
3817         (save-excursion
3818           (set-buffer name)
3819           (set (make-local-variable 'gnus-article-edit-mode) nil)
3820           (when gnus-article-mime-handles
3821             (mm-destroy-parts gnus-article-mime-handles)
3822             (setq gnus-article-mime-handles nil))
3823           ;; Set it to nil in article-buffer!
3824           (setq gnus-article-mime-handle-alist nil)
3825           (buffer-disable-undo)
3826           (setq buffer-read-only t)
3827           ;; This list just keeps growing if we don't reset it.
3828           (setq gnus-button-marker-list nil)
3829           (unless (eq major-mode 'gnus-article-mode)
3830             (gnus-article-mode))
3831           (current-buffer))
3832       (save-excursion
3833         (set-buffer (gnus-get-buffer-create name))
3834         (gnus-article-mode)
3835         (make-local-variable 'gnus-summary-buffer)
3836         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3837         (current-buffer)))))
3838
3839 ;; Set article window start at LINE, where LINE is the number of lines
3840 ;; from the head of the article.
3841 (defun gnus-article-set-window-start (&optional line)
3842   (set-window-start
3843    (gnus-get-buffer-window gnus-article-buffer t)
3844    (save-excursion
3845      (set-buffer gnus-article-buffer)
3846      (goto-char (point-min))
3847      (if (not line)
3848          (point-min)
3849        (gnus-message 6 "Moved to bookmark")
3850        (search-forward "\n\n" nil t)
3851        (forward-line line)
3852        (point)))))
3853
3854 (defun gnus-article-prepare (article &optional all-headers header)
3855   "Prepare ARTICLE in article mode buffer.
3856 ARTICLE should either be an article number or a Message-ID.
3857 If ARTICLE is an id, HEADER should be the article headers.
3858 If ALL-HEADERS is non-nil, no headers are hidden."
3859   (save-excursion
3860     ;; Make sure we start in a summary buffer.
3861     (unless (eq major-mode 'gnus-summary-mode)
3862       (set-buffer gnus-summary-buffer))
3863     (setq gnus-summary-buffer (current-buffer))
3864     (let* ((gnus-article (if header (mail-header-number header) article))
3865            (summary-buffer (current-buffer))
3866            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3867            (group gnus-newsgroup-name)
3868            result)
3869       (save-excursion
3870         (gnus-article-setup-buffer)
3871         (set-buffer gnus-article-buffer)
3872         ;; Deactivate active regions.
3873         (when (and (boundp 'transient-mark-mode)
3874                    transient-mark-mode)
3875           (setq mark-active nil))
3876         (if (not (setq result (let ((inhibit-read-only t))
3877                                 (gnus-request-article-this-buffer
3878                                  article group))))
3879             ;; There is no such article.
3880             (save-excursion
3881               (when (and (numberp article)
3882                          (not (memq article gnus-newsgroup-sparse)))
3883                 (setq gnus-article-current
3884                       (cons gnus-newsgroup-name article))
3885                 (set-buffer gnus-summary-buffer)
3886                 (setq gnus-current-article article)
3887                 (if (and (memq article gnus-newsgroup-undownloaded)
3888                          (not (gnus-online (gnus-find-method-for-group
3889                                             gnus-newsgroup-name))))
3890                     (progn
3891                       (gnus-summary-set-agent-mark article)
3892                       (message "Message marked for downloading"))
3893                   (gnus-summary-mark-article article gnus-canceled-mark)
3894                   (unless (memq article gnus-newsgroup-sparse)
3895                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3896           (if (or (eq result 'pseudo)
3897                   (eq result 'nneething))
3898               (progn
3899                 (save-excursion
3900                   (set-buffer summary-buffer)
3901                   (push article gnus-newsgroup-history)
3902                   (setq gnus-last-article gnus-current-article
3903                         gnus-current-article 0
3904                         gnus-current-headers nil
3905                         gnus-article-current nil)
3906                   (if (eq result 'nneething)
3907                       (gnus-configure-windows 'summary)
3908                     (gnus-configure-windows 'article))
3909                   (gnus-set-global-variables))
3910                 (let ((gnus-article-mime-handle-alist-1
3911                        gnus-article-mime-handle-alist))
3912                   (gnus-set-mode-line 'article)))
3913             ;; The result from the `request' was an actual article -
3914             ;; or at least some text that is now displayed in the
3915             ;; article buffer.
3916             (when (and (numberp article)
3917                        (not (eq article gnus-current-article)))
3918               ;; Seems like a new article has been selected.
3919               ;; `gnus-current-article' must be an article number.
3920               (save-excursion
3921                 (set-buffer summary-buffer)
3922                 (push article gnus-newsgroup-history)
3923                 (setq gnus-last-article gnus-current-article
3924                       gnus-current-article article
3925                       gnus-current-headers
3926                       (gnus-summary-article-header gnus-current-article)
3927                       gnus-article-current
3928                       (cons gnus-newsgroup-name gnus-current-article))
3929                 (unless (vectorp gnus-current-headers)
3930                   (setq gnus-current-headers nil))
3931                 (gnus-summary-goto-subject gnus-current-article)
3932                 (when (gnus-summary-show-thread)
3933                   ;; If the summary buffer really was folded, the
3934                   ;; previous goto may not actually have gone to
3935                   ;; the right article, but the thread root instead.
3936                   ;; So we go again.
3937                   (gnus-summary-goto-subject gnus-current-article))
3938                 (gnus-run-hooks 'gnus-mark-article-hook)
3939                 (gnus-set-mode-line 'summary)
3940                 (when (gnus-visual-p 'article-highlight 'highlight)
3941                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3942                 ;; Set the global newsgroup variables here.
3943                 (gnus-set-global-variables)
3944                 (setq gnus-have-all-headers
3945                       (or all-headers gnus-show-all-headers))))
3946             (save-excursion
3947               (gnus-configure-windows 'article))
3948             (when (or (numberp article)
3949                       (stringp article))
3950               (gnus-article-prepare-display)
3951               ;; Do page break.
3952               (goto-char (point-min))
3953               (when gnus-break-pages
3954                 (gnus-narrow-to-page)))
3955             (let ((gnus-article-mime-handle-alist-1
3956                    gnus-article-mime-handle-alist))
3957               (gnus-set-mode-line 'article))
3958             (article-goto-body)
3959             (unless (bobp)
3960               (forward-line -1))
3961             (set-window-point (get-buffer-window (current-buffer)) (point))
3962             (gnus-configure-windows 'article)
3963             t))))))
3964
3965 ;;;###autoload
3966 (defun gnus-article-prepare-display ()
3967   "Make the current buffer look like a nice article."
3968   ;; Hooks for getting information from the article.
3969   ;; This hook must be called before being narrowed.
3970   (let ((gnus-article-buffer (current-buffer))
3971         buffer-read-only
3972         (inhibit-read-only t))
3973     (unless (eq major-mode 'gnus-article-mode)
3974       (gnus-article-mode))
3975     (setq buffer-read-only nil
3976           gnus-article-wash-types nil
3977           gnus-article-image-alist nil)
3978     (gnus-run-hooks 'gnus-tmp-internal-hook)
3979     (when gnus-display-mime-function
3980       (funcall gnus-display-mime-function))
3981     (gnus-run-hooks 'gnus-article-prepare-hook)))
3982
3983 ;;;
3984 ;;; Gnus MIME viewing functions
3985 ;;;
3986
3987 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3988   "Format of the MIME buttons.
3989
3990 Valid specifiers include:
3991 %t  The MIME type
3992 %T  MIME type, along with additional info
3993 %n  The `name' parameter
3994 %d  The description, if any
3995 %l  The length of the encoded part
3996 %p  The part identifier number
3997 %e  Dots if the part isn't displayed
3998
3999 General format specifiers can also be used.  See Info node
4000 `(gnus)Formatting Variables'.")
4001
4002 (defvar gnus-mime-button-line-format-alist
4003   '((?t gnus-tmp-type ?s)
4004     (?T gnus-tmp-type-long ?s)
4005     (?n gnus-tmp-name ?s)
4006     (?d gnus-tmp-description ?s)
4007     (?p gnus-tmp-id ?s)
4008     (?l gnus-tmp-length ?d)
4009     (?e gnus-tmp-dots ?s)))
4010
4011 (defvar gnus-mime-button-commands
4012   '((gnus-article-press-button "\r" "Toggle Display")
4013     (gnus-mime-view-part "v" "View Interactively...")
4014     (gnus-mime-view-part-as-type "t" "View As Type...")
4015     (gnus-mime-view-part-as-charset "C" "View As charset...")
4016     (gnus-mime-save-part "o" "Save...")
4017     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4018     (gnus-mime-delete-part "d" "Delete part")
4019     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4020     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4021     (gnus-mime-view-part-internally "E" "View Internally")
4022     (gnus-mime-view-part-externally "e" "View Externally")
4023     (gnus-mime-print-part "p" "Print")
4024     (gnus-mime-pipe-part "|" "Pipe To Command...")
4025     (gnus-mime-action-on-part "." "Take action on the part...")))
4026
4027 (defun gnus-article-mime-part-status ()
4028   (if gnus-article-mime-handle-alist-1
4029       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4030           " (1 part)"
4031         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4032     ""))
4033
4034 (defvar gnus-mime-button-map
4035   (let ((map (make-sparse-keymap)))
4036     (define-key map gnus-mouse-2 'gnus-article-push-button)
4037     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4038     (dolist (c gnus-mime-button-commands)
4039       (define-key map (cadr c) (car c)))
4040     map))
4041
4042 (easy-menu-define
4043   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4044   `("MIME Part"
4045     ,@(mapcar (lambda (c)
4046                 (vector (caddr c) (car c) :enable t))
4047               gnus-mime-button-commands)))
4048
4049 (defun gnus-mime-button-menu (event prefix)
4050  "Construct a context-sensitive menu of MIME commands."
4051  (interactive "e\nP")
4052  (save-window-excursion
4053    (let ((pos (event-start event)))
4054      (select-window (posn-window pos))
4055      (goto-char (posn-point pos))
4056      (gnus-article-check-buffer)
4057      (popup-menu gnus-mime-button-menu nil prefix))))
4058
4059 (defun gnus-mime-view-all-parts (&optional handles)
4060   "View all the MIME parts."
4061   (interactive)
4062   (with-current-buffer gnus-article-buffer
4063     (let ((handles (or handles gnus-article-mime-handles))
4064           (mail-parse-charset gnus-newsgroup-charset)
4065           (mail-parse-ignored-charsets
4066            (with-current-buffer gnus-summary-buffer
4067              gnus-newsgroup-ignored-charsets)))
4068       (when handles
4069         (mm-remove-parts handles)
4070         (goto-char (point-min))
4071         (or (search-forward "\n\n") (goto-char (point-max)))
4072         (let ((inhibit-read-only t))
4073           (delete-region (point) (point-max))
4074           (mm-display-parts handles))))))
4075
4076 (eval-when-compile
4077   (defsubst gnus-article-edit-part (handles)
4078     "Edit an article in order to delete a mime part.
4079 This function is exclusively used by `gnus-mime-save-part-and-strip'
4080 and `gnus-mime-delete-part', and not provided at run-time normally."
4081     (gnus-article-edit-article
4082      `(lambda ()
4083         (buffer-disable-undo)
4084         (erase-buffer)
4085         (let ((mail-parse-charset (or gnus-article-charset
4086                                       ',gnus-newsgroup-charset))
4087               (mail-parse-ignored-charsets
4088                (or gnus-article-ignored-charsets
4089                    ',gnus-newsgroup-ignored-charsets))
4090               (mbl mml-buffer-list))
4091           (setq mml-buffer-list nil)
4092           (insert-buffer gnus-original-article-buffer)
4093           (mime-to-mml ',handles)
4094           (setq gnus-article-mime-handles nil)
4095           (let ((mbl1 mml-buffer-list))
4096             (setq mml-buffer-list mbl)
4097             (set (make-local-variable 'mml-buffer-list) mbl1))
4098           (gnus-make-local-hook 'kill-buffer-hook)
4099           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4100      `(lambda (no-highlight)
4101         (let ((mail-parse-charset (or gnus-article-charset
4102                                       ',gnus-newsgroup-charset))
4103               (message-options message-options)
4104               (message-options-set-recipient)
4105               (mail-parse-ignored-charsets
4106                (or gnus-article-ignored-charsets
4107                    ',gnus-newsgroup-ignored-charsets)))
4108           (mml-to-mime)
4109           (mml-destroy-buffers)
4110           (remove-hook 'kill-buffer-hook
4111                        'mml-destroy-buffers t)
4112           (kill-local-variable 'mml-buffer-list))
4113         (gnus-summary-edit-article-done
4114          ,(or (mail-header-references gnus-current-headers) "")
4115          ,(gnus-group-read-only-p)
4116          ,gnus-summary-buffer no-highlight)))
4117     (gnus-article-edit-done)
4118     (gnus-summary-expand-window)
4119     (gnus-summary-show-article)))
4120
4121 (defun gnus-mime-save-part-and-strip ()
4122   "Save the MIME part under point then replace it with an external body."
4123   (interactive)
4124   (gnus-article-check-buffer)
4125   (when (gnus-group-read-only-p)
4126     (error "The current group does not support deleting of parts"))
4127   (when (mm-complicated-handles gnus-article-mime-handles)
4128     (error "\
4129 The current article has a complicated MIME structure, giving up..."))
4130   (when (gnus-yes-or-no-p "\
4131 Deleting parts may malfunction or destroy the article; continue? ")
4132     (let* ((data (get-text-property (point) 'gnus-data))
4133            file param
4134            (handles gnus-article-mime-handles))
4135       (setq file (and data (mm-save-part data)))
4136       (when file
4137         (with-current-buffer (mm-handle-buffer data)
4138           (erase-buffer)
4139           (insert "Content-Type: " (mm-handle-media-type data))
4140           (mml-insert-parameter-string (cdr (mm-handle-type data))
4141                                        '(charset))
4142           (insert "\n")
4143           (insert "Content-ID: " (message-make-message-id) "\n")
4144           (insert "Content-Transfer-Encoding: binary\n")
4145           (insert "\n"))
4146         (setcdr data
4147                 (cdr (mm-make-handle nil
4148                                      `("message/external-body"
4149                                        (access-type . "LOCAL-FILE")
4150                                        (name . ,file)))))
4151         (set-buffer gnus-summary-buffer)
4152         (gnus-article-edit-part handles)))))
4153
4154 (defun gnus-mime-delete-part ()
4155   "Delete the MIME part under point.
4156 Replace it with some information about the removed part."
4157   (interactive)
4158   (gnus-article-check-buffer)
4159   (when (gnus-group-read-only-p)
4160     (error "The current group does not support deleting of parts"))
4161   (when (mm-complicated-handles gnus-article-mime-handles)
4162     (error "\
4163 The current article has a complicated MIME structure, giving up..."))
4164   (when (gnus-yes-or-no-p "\
4165 Deleting parts may malfunction or destroy the article; continue? ")
4166     (let* ((data (get-text-property (point) 'gnus-data))
4167            (handles gnus-article-mime-handles)
4168            (none "(none)")
4169            (description
4170             (or
4171              (mail-decode-encoded-word-string (or (mm-handle-description data)
4172                                                   none))))
4173            (filename
4174             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4175                 none))
4176            (type (mm-handle-media-type data)))
4177       (unless data
4178         (error "No MIME part under point"))
4179       (with-current-buffer (mm-handle-buffer data)
4180         (let ((bsize (format "%s" (buffer-size))))
4181           (erase-buffer)
4182           (insert
4183            (concat
4184             ",----\n"
4185             "| The following attachment has been deleted:\n"
4186             "|\n"
4187             "| Type:           " type "\n"
4188             "| Filename:       " filename "\n"
4189             "| Size (encoded): " bsize " Byte\n"
4190             "| Description:    " description "\n"
4191             "`----\n"))
4192           (setcdr data
4193                   (cdr (mm-make-handle
4194                         nil `("text/plain") nil nil
4195                         (list "attachment")
4196                         (format "Deleted attachment (%s bytes)" bsize))))))
4197       (set-buffer gnus-summary-buffer)
4198       (gnus-article-edit-part handles))))
4199
4200 (defun gnus-mime-save-part ()
4201   "Save the MIME part under point."
4202   (interactive)
4203   (gnus-article-check-buffer)
4204   (let ((data (get-text-property (point) 'gnus-data)))
4205     (when data
4206       (mm-save-part data))))
4207
4208 (defun gnus-mime-pipe-part ()
4209   "Pipe the MIME part under point to a process."
4210   (interactive)
4211   (gnus-article-check-buffer)
4212   (let ((data (get-text-property (point) 'gnus-data)))
4213     (when data
4214       (mm-pipe-part data))))
4215
4216 (defun gnus-mime-view-part ()
4217   "Interactively choose a viewing method for the MIME part under point."
4218   (interactive)
4219   (gnus-article-check-buffer)
4220   (let ((data (get-text-property (point) 'gnus-data)))
4221     (when data
4222       (setq gnus-article-mime-handles
4223             (mm-merge-handles
4224              gnus-article-mime-handles (setq data (copy-sequence data))))
4225       (mm-interactively-view-part data))))
4226
4227 (defun gnus-mime-view-part-as-type-internal ()
4228   (gnus-article-check-buffer)
4229   (let* ((name (mail-content-type-get
4230                 (mm-handle-type (get-text-property (point) 'gnus-data))
4231                 'name))
4232          (def-type (and name (mm-default-file-encoding name))))
4233     (and def-type (cons def-type 0))))
4234
4235 (defun gnus-mime-view-part-as-type (&optional mime-type)
4236   "Choose a MIME media type, and view the part as such."
4237   (interactive)
4238   (unless mime-type
4239     (setq mime-type (completing-read
4240                      "View as MIME type: "
4241                      (mapcar #'list (mailcap-mime-types))
4242                      nil nil
4243                      (gnus-mime-view-part-as-type-internal))))
4244   (gnus-article-check-buffer)
4245   (let ((handle (get-text-property (point) 'gnus-data)))
4246     (when handle
4247       (setq handle
4248             (mm-make-handle (mm-handle-buffer handle)
4249                             (cons mime-type (cdr (mm-handle-type handle)))
4250                             (mm-handle-encoding handle)
4251                             (mm-handle-undisplayer handle)
4252                             (mm-handle-disposition handle)
4253                             (mm-handle-description handle)
4254                             nil
4255                             (mm-handle-id handle)))
4256       (setq gnus-article-mime-handles
4257             (mm-merge-handles gnus-article-mime-handles handle))
4258       (gnus-mm-display-part handle))))
4259
4260 (defun gnus-mime-copy-part (&optional handle arg)
4261   "Put the MIME part under point into a new buffer.
4262 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4263 are decompressed."
4264   (interactive (list nil current-prefix-arg))
4265   (gnus-article-check-buffer)
4266   (unless handle
4267     (setq handle (get-text-property (point) 'gnus-data)))
4268   (when handle
4269     (let ((filename (or (mail-content-type-get (mm-handle-disposition handle)
4270                                                'name)
4271                         (mail-content-type-get (mm-handle-disposition handle)
4272                                                'filename)))
4273           contents dont-decode charset coding-system)
4274       (mm-with-unibyte-buffer
4275         (mm-insert-part handle)
4276         (setq contents (or (condition-case nil
4277                                (mm-decompress-buffer filename nil 'sig)
4278                              (error
4279                               (setq dont-decode t)
4280                               nil))
4281                            (buffer-string))))
4282       (setq filename (cond (filename (file-name-nondirectory filename))
4283                            (dont-decode "*raw data*")
4284                            (t "*decoded*")))
4285       (cond
4286        (dont-decode)
4287        ((not arg)
4288         (unless (setq charset (mail-content-type-get
4289                                (mm-handle-type handle) 'charset))
4290           (unless (setq coding-system (mm-with-unibyte-buffer
4291                                         (insert contents)
4292                                         (mm-find-buffer-file-coding-system)))
4293             (setq charset gnus-newsgroup-charset))))
4294        ((numberp arg)
4295         (setq charset (or (cdr (assq arg
4296                                      gnus-summary-show-article-charset-alist))
4297                           (mm-read-coding-system "Charset: ")))))
4298       (switch-to-buffer (generate-new-buffer filename))
4299       (if (or coding-system
4300               (and charset
4301                    (setq coding-system (mm-charset-to-coding-system charset))
4302                    (not (eq charset 'ascii))))
4303           (progn
4304             (mm-enable-multibyte)
4305             (insert (mm-decode-coding-string contents coding-system))
4306             (setq buffer-file-coding-system
4307                   (if (boundp 'last-coding-system-used)
4308                       (symbol-value 'last-coding-system-used)
4309                     coding-system)))
4310         (mm-disable-multibyte)
4311         (insert contents)
4312         (setq buffer-file-coding-system mm-binary-coding-system))
4313       ;; We do it this way to make `normal-mode' set the appropriate mode.
4314       (unwind-protect
4315           (progn
4316             (setq buffer-file-name (expand-file-name filename))
4317             (normal-mode))
4318         (setq buffer-file-name nil))
4319       (goto-char (point-min)))))
4320
4321 (defun gnus-mime-print-part (&optional handle filename)
4322   "Print the MIME part under point."
4323   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4324   (gnus-article-check-buffer)
4325   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4326          (contents (and handle (mm-get-part handle)))
4327          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4328          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4329     (when contents
4330         (if printer
4331             (unwind-protect
4332                 (progn
4333                   (mm-save-part-to-file handle file)
4334                   (call-process shell-file-name nil
4335                                 (generate-new-buffer " *mm*")
4336                                 nil
4337                                 shell-command-switch
4338                                 (mm-mailcap-command
4339                                  printer file (mm-handle-type handle))))
4340               (delete-file file))
4341           (with-temp-buffer
4342             (insert contents)
4343             (gnus-print-buffer))
4344           (ps-despool filename)))))
4345
4346 (defun gnus-mime-inline-part (&optional handle arg)
4347   "Insert the MIME part under point into the current buffer.
4348 Compressed files like .gz and .bz2 are decompressed."
4349   (interactive (list nil current-prefix-arg))
4350   (gnus-article-check-buffer)
4351   (unless handle
4352     (setq handle (get-text-property (point) 'gnus-data)))
4353   (when handle
4354     (let ((b (point))
4355           (inhibit-read-only t)
4356           contents charset coding-system)
4357       (if (and (not arg) (mm-handle-undisplayer handle))
4358           (mm-remove-part handle)
4359         (mm-with-unibyte-buffer
4360           (mm-insert-part handle)
4361           (setq contents
4362                 (or (mm-decompress-buffer
4363                      (or (mail-content-type-get (mm-handle-disposition handle)
4364                                                 'name)
4365                          (mail-content-type-get (mm-handle-disposition handle)
4366                                                 'filename))
4367                      nil t)
4368                     (buffer-string))))
4369         (cond
4370          ((not arg)
4371           (unless (setq charset (mail-content-type-get
4372                                  (mm-handle-type handle) 'charset))
4373             (unless (setq coding-system
4374                           (mm-with-unibyte-buffer
4375                             (insert contents)
4376                             (mm-find-buffer-file-coding-system)))
4377               (setq charset gnus-newsgroup-charset))))
4378          ((numberp arg)
4379           (if (mm-handle-undisplayer handle)
4380               (mm-remove-part handle))
4381           (setq charset
4382                 (or (cdr (assq arg
4383                                gnus-summary-show-article-charset-alist))
4384                     (mm-read-coding-system "Charset: "))))
4385          (t
4386           (if (mm-handle-undisplayer handle)
4387               (mm-remove-part handle))))
4388         (forward-line 2)
4389         (mm-insert-inline
4390          handle
4391          (if (or coding-system
4392                  (and charset
4393                       (setq coding-system
4394                             (mm-charset-to-coding-system charset))
4395                       (not (eq charset 'ascii))))
4396              (mm-decode-coding-string contents coding-system)
4397            (mm-string-to-multibyte contents)))
4398         (goto-char b)))))
4399
4400 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4401   "Insert the MIME part under point into the current buffer using the
4402 specified charset."
4403   (interactive (list nil current-prefix-arg))
4404   (gnus-article-check-buffer)
4405   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4406          contents charset
4407          (b (point))
4408          (inhibit-read-only t))
4409     (when handle
4410       (if (mm-handle-undisplayer handle)
4411           (mm-remove-part handle))
4412       (let ((gnus-newsgroup-charset
4413              (or (cdr (assq arg
4414                             gnus-summary-show-article-charset-alist))
4415                  (mm-read-coding-system "Charset: ")))
4416           (gnus-newsgroup-ignored-charsets 'gnus-all))
4417         (gnus-article-press-button)))))
4418
4419 (defun gnus-mime-view-part-externally (&optional handle)
4420   "View the MIME part under point with an external viewer."
4421   (interactive)
4422   (gnus-article-check-buffer)
4423   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4424          (mm-user-display-methods nil)
4425          (mm-inlined-types nil)
4426          (mail-parse-charset gnus-newsgroup-charset)
4427          (mail-parse-ignored-charsets
4428           (with-current-buffer gnus-summary-buffer
4429             gnus-newsgroup-ignored-charsets)))
4430     (when handle
4431       (if (mm-handle-undisplayer handle)
4432           (mm-remove-part handle)
4433         (mm-display-part handle)))))
4434
4435 (defun gnus-mime-view-part-internally (&optional handle)
4436   "View the MIME part under point with an internal viewer.
4437 If no internal viewer is available, use an external viewer."
4438   (interactive)
4439   (gnus-article-check-buffer)
4440   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4441          (mm-inlined-types '(".*"))
4442          (mm-inline-large-images t)
4443          (mail-parse-charset gnus-newsgroup-charset)
4444          (mail-parse-ignored-charsets
4445           (with-current-buffer gnus-summary-buffer
4446             gnus-newsgroup-ignored-charsets))
4447          (inhibit-read-only t))
4448     (when handle
4449       (if (mm-handle-undisplayer handle)
4450           (mm-remove-part handle)
4451         (mm-display-part handle)))))
4452
4453 (defun gnus-mime-action-on-part (&optional action)
4454   "Do something with the MIME attachment at \(point\)."
4455   (interactive
4456    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4457   (gnus-article-check-buffer)
4458   (let ((action-pair (assoc action gnus-mime-action-alist)))
4459     (if action-pair
4460         (funcall (cdr action-pair)))))
4461
4462 (defun gnus-article-part-wrapper (n function)
4463   (with-current-buffer gnus-article-buffer
4464     (when (> n (length gnus-article-mime-handle-alist))
4465       (error "No such part"))
4466     (gnus-article-goto-part n)
4467     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4468       (funcall function handle))))
4469
4470 (defun gnus-article-pipe-part (n)
4471   "Pipe MIME part N, which is the numerical prefix."
4472   (interactive "p")
4473   (gnus-article-part-wrapper n 'mm-pipe-part))
4474
4475 (defun gnus-article-save-part (n)
4476   "Save MIME part N, which is the numerical prefix."
4477   (interactive "p")
4478   (gnus-article-part-wrapper n 'mm-save-part))
4479
4480 (defun gnus-article-interactively-view-part (n)
4481   "View MIME part N interactively, which is the numerical prefix."
4482   (interactive "p")
4483   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4484
4485 (defun gnus-article-copy-part (n)
4486   "Copy MIME part N, which is the numerical prefix."
4487   (interactive "p")
4488   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4489
4490 (defun gnus-article-view-part-as-charset (n)
4491   "View MIME part N using a specified charset.
4492 N is the numerical prefix."
4493   (interactive "p")
4494   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4495
4496 (defun gnus-article-view-part-externally (n)
4497   "View MIME part N externally, which is the numerical prefix."
4498   (interactive "p")
4499   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4500
4501 (defun gnus-article-inline-part (n)
4502   "Inline MIME part N, which is the numerical prefix."
4503   (interactive "p")
4504   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4505
4506 (defun gnus-article-mime-match-handle-first (condition)
4507   (if condition
4508       (let (n)
4509         (dolist (ihandle gnus-article-mime-handle-alist)
4510           (if (and (cond
4511                     ((functionp condition)
4512                      (funcall condition (cdr ihandle)))
4513                     ((eq condition 'undisplayed)
4514                      (not (or (mm-handle-undisplayer (cdr ihandle))
4515                               (equal (mm-handle-media-type (cdr ihandle))
4516                                      "multipart/alternative"))))
4517                     ((eq condition 'undisplayed-alternative)
4518                      (not (mm-handle-undisplayer (cdr ihandle))))
4519                     (t t))
4520                    (gnus-article-goto-part (car ihandle))
4521                    (or (not n) (< (car ihandle) n)))
4522               (setq n (car ihandle))))
4523         (or n 1))
4524     1))
4525
4526 (defun gnus-article-view-part (&optional n)
4527   "View MIME part N, which is the numerical prefix."
4528   (interactive "P")
4529   (with-current-buffer gnus-article-buffer
4530     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4531                              gnus-article-mime-match-handle-function)))
4532     (when (> n (length gnus-article-mime-handle-alist))
4533       (error "No such part"))
4534     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4535       (when (gnus-article-goto-part n)
4536         (if (equal (car handle) "multipart/alternative")
4537             (gnus-article-press-button)
4538           (when (eq (gnus-mm-display-part handle) 'internal)
4539             (gnus-set-window-start)))))))
4540
4541 (defsubst gnus-article-mime-total-parts ()
4542   (if (bufferp (car gnus-article-mime-handles))
4543       1 ;; single part
4544     (1- (length gnus-article-mime-handles))))
4545
4546 (defun gnus-mm-display-part (handle)
4547   "Display HANDLE and fix MIME button."
4548   (let ((id (get-text-property (point) 'gnus-part))
4549         (point (point))
4550         (inhibit-read-only t))
4551     (forward-line 1)
4552     (prog1
4553         (let ((window (selected-window))
4554               (mail-parse-charset gnus-newsgroup-charset)
4555               (mail-parse-ignored-charsets
4556                (if (gnus-buffer-live-p gnus-summary-buffer)
4557                    (with-current-buffer gnus-summary-buffer
4558                      gnus-newsgroup-ignored-charsets)
4559                  nil)))
4560           (save-excursion
4561             (unwind-protect
4562                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4563                       (beg (point)))
4564                   (when win
4565                     (select-window win))
4566                   (goto-char point)
4567                   (forward-line)
4568                   (if (mm-handle-displayed-p handle)
4569                       ;; This will remove the part.
4570                       (mm-display-part handle)
4571                     (save-restriction
4572                       (narrow-to-region (point)
4573                                         (if (eobp) (point) (1+ (point))))
4574                       (mm-display-part handle)
4575                       ;; We narrow to the part itself and
4576                       ;; then call the treatment functions.
4577                       (goto-char (point-min))
4578                       (forward-line 1)
4579                       (narrow-to-region (point) (point-max))
4580                       (gnus-treat-article
4581                        nil id
4582                        (gnus-article-mime-total-parts)
4583                        (mm-handle-media-type handle)))))
4584               (if (window-live-p window)
4585                   (select-window window)))))
4586       (goto-char point)
4587       (gnus-delete-line)
4588       (gnus-insert-mime-button
4589        handle id (list (mm-handle-displayed-p handle)))
4590       (goto-char point))))
4591
4592 (defun gnus-article-goto-part (n)
4593   "Go to MIME part N."
4594   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4595
4596 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4597   (let ((gnus-tmp-name
4598          (or (mail-content-type-get (mm-handle-type handle) 'name)
4599              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4600              (mail-content-type-get (mm-handle-type handle) 'url)
4601              ""))
4602         (gnus-tmp-type (mm-handle-media-type handle))
4603         (gnus-tmp-description
4604          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4605                                               "")))
4606         (gnus-tmp-dots
4607          (if (if displayed (car displayed)
4608                (mm-handle-displayed-p handle))
4609              "" "..."))
4610         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4611                            (buffer-size)))
4612         gnus-tmp-type-long b e)
4613     (when (string-match ".*/" gnus-tmp-name)
4614       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4615     (setq gnus-tmp-type-long (concat gnus-tmp-type
4616                                      (and (not (equal gnus-tmp-name ""))
4617                                           (concat "; " gnus-tmp-name))))
4618     (unless (equal gnus-tmp-description "")
4619       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4620     (unless (bolp)
4621       (insert "\n"))
4622     (setq b (point))
4623     (gnus-eval-format
4624      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4625      `(keymap ,gnus-mime-button-map
4626               gnus-callback gnus-mm-display-part
4627               gnus-part ,gnus-tmp-id
4628               article-type annotation
4629               gnus-data ,handle))
4630     (setq e (if (bolp)
4631                 ;; Exclude a newline.
4632                 (1- (point))
4633               (point)))
4634     (widget-convert-button
4635      'link b e
4636      :mime-handle handle
4637      :action 'gnus-widget-press-button
4638      :button-keymap gnus-mime-button-map
4639      :help-echo
4640      (lambda (widget/window &optional overlay pos)
4641        ;; Needed to properly clear the message due to a bug in
4642        ;; wid-edit (XEmacs only).
4643        (if (boundp 'help-echo-owns-message)
4644            (setq help-echo-owns-message t))
4645        (format
4646         "%S: %s the MIME part; %S: more options"
4647         (aref gnus-mouse-2 0)
4648         ;; XEmacs will get a single widget arg; Emacs 21 will get
4649         ;; window, overlay, position.
4650         (if (mm-handle-displayed-p
4651              (if overlay
4652                  (with-current-buffer (gnus-overlay-buffer overlay)
4653                    (widget-get (widget-at (gnus-overlay-start overlay))
4654                                :mime-handle))
4655                (widget-get widget/window :mime-handle)))
4656             "hide" "show")
4657         (aref gnus-down-mouse-3 0))))))
4658
4659 (defun gnus-widget-press-button (elems el)
4660   (goto-char (widget-get elems :from))
4661   (gnus-article-press-button))
4662
4663 (defvar gnus-displaying-mime nil)
4664
4665 (defun gnus-display-mime (&optional ihandles)
4666   "Display the MIME parts."
4667   (save-excursion
4668     (save-selected-window
4669       (let ((window (get-buffer-window gnus-article-buffer))
4670             (point (point)))
4671         (when window
4672           (select-window window)
4673           ;; We have to do this since selecting the window
4674           ;; may change the point.  So we set the window point.
4675           (set-window-point window point)))
4676       (let ((handles ihandles)
4677             (inhibit-read-only t)
4678             handle)
4679         (cond (handles)
4680               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
4681                (when gnus-article-emulate-mime
4682                  (mm-uu-dissect-text-parts handles)))
4683               (gnus-article-emulate-mime
4684                (setq handles (mm-uu-dissect))))
4685         (when (and (not ihandles)
4686                    (not gnus-displaying-mime))
4687           ;; Top-level call; we clean up.
4688           (when gnus-article-mime-handles
4689             (mm-destroy-parts gnus-article-mime-handles)
4690             (setq gnus-article-mime-handle-alist nil));; A trick.
4691           (setq gnus-article-mime-handles handles)
4692           ;; We allow users to glean info from the handles.
4693           (when gnus-article-mime-part-function
4694             (gnus-mime-part-function handles)))
4695         (if (and handles
4696                  (or (not (stringp (car handles)))
4697                      (cdr handles)))
4698             (progn
4699               (when (and (not ihandles)
4700                          (not gnus-displaying-mime))
4701                 ;; Clean up for mime parts.
4702                 (article-goto-body)
4703                 (delete-region (point) (point-max)))
4704               (let ((gnus-displaying-mime t))
4705                 (gnus-mime-display-part handles)))
4706           (save-restriction
4707             (article-goto-body)
4708             (narrow-to-region (point) (point-max))
4709             (gnus-treat-article nil 1 1)
4710             (widen)))
4711         (unless ihandles
4712           ;; Highlight the headers.
4713           (save-excursion
4714             (save-restriction
4715               (article-goto-body)
4716               (narrow-to-region (point-min) (point))
4717               (gnus-article-save-original-date
4718                (gnus-treat-article 'head)))))))))
4719
4720 (defcustom gnus-mime-display-multipart-as-mixed nil
4721   "Display \"multipart\" parts as  \"multipart/mixed\".
4722
4723 If t, it overrides nil values of
4724 `gnus-mime-display-multipart-alternative-as-mixed' and
4725 `gnus-mime-display-multipart-related-as-mixed'."
4726   :group 'gnus-article-mime
4727   :type 'boolean)
4728
4729 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
4730   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
4731   :version "22.1"
4732   :group 'gnus-article-mime
4733   :type 'boolean)
4734
4735 (defcustom gnus-mime-display-multipart-related-as-mixed nil
4736   "Display \"multipart/related\" parts as  \"multipart/mixed\".
4737
4738 If displaying \"text/html\" is discouraged \(see
4739 `mm-discouraged-alternatives'\) images or other material inside a
4740 \"multipart/related\" part might be overlooked when this variable is nil."
4741   :version "22.1"
4742   :group 'gnus-article-mime
4743   :type 'boolean)
4744
4745 (defun gnus-mime-display-part (handle)
4746   (cond
4747    ;; Single part.
4748    ((not (stringp (car handle)))
4749     (gnus-mime-display-single handle))
4750    ;; User-defined multipart
4751    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4752     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4753              handle))
4754    ;; multipart/alternative
4755    ((and (equal (car handle) "multipart/alternative")
4756          (not (or gnus-mime-display-multipart-as-mixed
4757                   gnus-mime-display-multipart-alternative-as-mixed)))
4758     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4759       (push (cons id handle) gnus-article-mime-handle-alist)
4760       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4761    ;; multipart/related
4762    ((and (equal (car handle) "multipart/related")
4763          (not (or gnus-mime-display-multipart-as-mixed
4764                   gnus-mime-display-multipart-related-as-mixed)))
4765     ;;;!!!We should find the start part, but we just default
4766     ;;;!!!to the first part.
4767     ;;(gnus-mime-display-part (cadr handle))
4768     ;;;!!! Most multipart/related is an HTML message plus images.
4769     ;;;!!! Unfortunately we are unable to let W3 display those
4770     ;;;!!! included images, so we just display it as a mixed multipart.
4771     ;;(gnus-mime-display-mixed (cdr handle))
4772     ;;;!!! No, w3 can display everything just fine.
4773     (gnus-mime-display-part (cadr handle)))
4774    ((equal (car handle) "multipart/signed")
4775     (gnus-add-wash-type 'signed)
4776     (gnus-mime-display-security handle))
4777    ((equal (car handle) "multipart/encrypted")
4778     (gnus-add-wash-type 'encrypted)
4779     (gnus-mime-display-security handle))
4780    ;; Other multiparts are handled like multipart/mixed.
4781    (t
4782     (gnus-mime-display-mixed (cdr handle)))))
4783
4784 (defun gnus-mime-part-function (handles)
4785   (if (stringp (car handles))
4786       (mapcar 'gnus-mime-part-function (cdr handles))
4787     (funcall gnus-article-mime-part-function handles)))
4788
4789 (defun gnus-mime-display-mixed (handles)
4790   (mapcar 'gnus-mime-display-part handles))
4791
4792 (defun gnus-mime-display-single (handle)
4793   (let ((type (mm-handle-media-type handle))
4794         (ignored gnus-ignored-mime-types)
4795         (not-attachment t)
4796         (move nil)
4797         display text)
4798     (catch 'ignored
4799       (progn
4800         (while ignored
4801           (when (string-match (pop ignored) type)
4802             (throw 'ignored nil)))
4803         (if (and (setq not-attachment
4804                        (and (not (mm-inline-override-p handle))
4805                             (or (not (mm-handle-disposition handle))
4806                                 (equal (car (mm-handle-disposition handle))
4807                                        "inline")
4808                                 (mm-attachment-override-p handle))))
4809                  (mm-automatic-display-p handle)
4810                  (or (and
4811                       (mm-inlinable-p handle)
4812                       (mm-inlined-p handle))
4813                      (mm-automatic-external-display-p type)))
4814             (setq display t)
4815           (when (equal (mm-handle-media-supertype handle) "text")
4816             (setq text t)))
4817         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4818               beg)
4819           (push (cons id handle) gnus-article-mime-handle-alist)
4820           (when (or (not display)
4821                     (not (gnus-unbuttonized-mime-type-p type)))
4822             (gnus-insert-mime-button
4823              handle id (list (or display (and not-attachment text))))
4824             (gnus-article-insert-newline)
4825             ;; Remember modify the number of forward lines.
4826             (setq move t))
4827           (setq beg (point))
4828           (cond
4829            (display
4830             (when move
4831               (forward-line -1)
4832               (setq beg (point)))
4833             (let ((mail-parse-charset gnus-newsgroup-charset)
4834                   (mail-parse-ignored-charsets
4835                    (save-excursion (condition-case ()
4836                                        (set-buffer gnus-summary-buffer)
4837                                      (error))
4838                                    gnus-newsgroup-ignored-charsets)))
4839               (mm-display-part handle t))
4840             (goto-char (point-max)))
4841            ((and text not-attachment)
4842             (when move
4843               (forward-line -1)
4844               (setq beg (point)))
4845             (gnus-article-insert-newline)
4846             (mm-display-inline handle)
4847             (goto-char (point-max))))
4848           ;; Do highlighting.
4849           (save-excursion
4850             (save-restriction
4851               (narrow-to-region beg (point))
4852               (gnus-treat-article
4853                nil id
4854                (gnus-article-mime-total-parts)
4855                (mm-handle-media-type handle)))))))))
4856
4857 (defun gnus-unbuttonized-mime-type-p (type)
4858   "Say whether TYPE is to be unbuttonized."
4859   (unless gnus-inhibit-mime-unbuttonizing
4860     (when (catch 'found
4861             (let ((types gnus-unbuttonized-mime-types))
4862               (while types
4863                 (when (string-match (pop types) type)
4864                   (throw 'found t)))))
4865       (not (catch 'found
4866              (let ((types gnus-buttonized-mime-types))
4867                (while types
4868                  (when (string-match (pop types) type)
4869                    (throw 'found t)))))))))
4870
4871 (defun gnus-article-insert-newline ()
4872   "Insert a newline, but mark it as undeletable."
4873   (gnus-put-text-property
4874    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4875
4876 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4877   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4878          (ihandles handles)
4879          (point (point))
4880          handle (inhibit-read-only t) from props begend not-pref)
4881     (save-window-excursion
4882       (save-restriction
4883         (when ibegend
4884           (narrow-to-region (car ibegend)
4885                             (or (cdr ibegend)
4886                                 (progn
4887                                   (goto-char (car ibegend))
4888                                   (forward-line 2)
4889                                   (point))))
4890           (delete-region (point-min) (point-max))
4891           (mm-remove-parts handles))
4892         (setq begend (list (point-marker)))
4893         ;; Do the toggle.
4894         (unless (setq not-pref (cadr (member preferred ihandles)))
4895           (setq not-pref (car ihandles)))
4896         (when (or ibegend
4897                   (not preferred)
4898                   (not (gnus-unbuttonized-mime-type-p
4899                         "multipart/alternative")))
4900           (gnus-add-text-properties
4901            (setq from (point))
4902            (progn
4903              (insert (format "%d.  " id))
4904              (point))
4905            `(gnus-callback
4906              (lambda (handles)
4907                (unless ,(not ibegend)
4908                  (setq gnus-article-mime-handle-alist
4909                        ',gnus-article-mime-handle-alist))
4910                (gnus-mime-display-alternative
4911                 ',ihandles ',not-pref ',begend ,id))
4912              keymap ,gnus-mime-button-map
4913              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4914              face ,gnus-article-button-face
4915              gnus-part ,id
4916              gnus-data ,handle))
4917           (widget-convert-button 'link from (point)
4918                                  :action 'gnus-widget-press-button
4919                                  :button-keymap gnus-widget-button-keymap)
4920           ;; Do the handles
4921           (while (setq handle (pop handles))
4922             (gnus-add-text-properties
4923              (setq from (point))
4924              (progn
4925                (insert (format "(%c) %-18s"
4926                                (if (equal handle preferred) ?* ? )
4927                                (mm-handle-media-type handle)))
4928                (point))
4929              `(gnus-callback
4930                (lambda (handles)
4931                  (unless ,(not ibegend)
4932                    (setq gnus-article-mime-handle-alist
4933                          ',gnus-article-mime-handle-alist))
4934                  (gnus-mime-display-alternative
4935                   ',ihandles ',handle ',begend ,id))
4936                keymap ,gnus-mime-button-map
4937                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4938                face ,gnus-article-button-face
4939                gnus-part ,id
4940                gnus-data ,handle))
4941             (widget-convert-button 'link from (point)
4942                                    :action 'gnus-widget-press-button
4943                                    :button-keymap gnus-widget-button-keymap)
4944             (insert "  "))
4945           (insert "\n\n"))
4946         (when preferred
4947           (if (stringp (car preferred))
4948               (gnus-display-mime preferred)
4949             (let ((mail-parse-charset gnus-newsgroup-charset)
4950                   (mail-parse-ignored-charsets
4951                    (with-current-buffer gnus-summary-buffer
4952                      gnus-newsgroup-ignored-charsets)))
4953               (mm-display-part preferred)
4954               ;; Do highlighting.
4955               (save-excursion
4956                 (save-restriction
4957                   (narrow-to-region (car begend) (point-max))
4958                   (gnus-treat-article
4959                    nil (length gnus-article-mime-handle-alist)
4960                    (gnus-article-mime-total-parts)
4961                    (mm-handle-media-type handle))))))
4962           (goto-char (point-max))
4963           (setcdr begend (point-marker)))))
4964     (when ibegend
4965       (goto-char point))))
4966
4967 (defconst gnus-article-wash-status-strings
4968   (let ((alist '((cite "c" "Possible hidden citation text"
4969                        " " "All citation text visible")
4970                  (headers "h" "Hidden headers"
4971                           " " "All headers visible.")
4972                  (pgp "p" "Encrypted or signed message status hidden"
4973                       " " "No hidden encryption nor digital signature status")
4974                  (signature "s" "Signature has been hidden"
4975                             " " "Signature is visible")
4976                  (overstrike "o" "Overstrike (^H) characters applied"
4977                              " " "No overstrike characters applied")
4978                  (emphasis "e" "/*_Emphasis_*/ characters applied"
4979                            " " "No /*_emphasis_*/ characters applied")))
4980         result)
4981     (dolist (entry alist result)
4982       (let ((key (nth 0 entry))
4983             (on (copy-sequence (nth 1 entry)))
4984             (on-help (nth 2 entry))
4985             (off (copy-sequence (nth 3 entry)))
4986             (off-help (nth 4 entry)))
4987         (put-text-property 0 1 'help-echo on-help on)
4988         (put-text-property 0 1 'help-echo off-help off)
4989         (push (list key on off) result))))
4990   "Alist of strings describing wash status in the mode line.
4991 Each entry has the form (KEY ON OF), where the KEY is a symbol
4992 representing the particular washing function, ON is the string to use
4993 in the article mode line when the washing function is active, and OFF
4994 is the string to use when it is inactive.")
4995
4996 (defun gnus-article-wash-status-entry (key value)
4997   (let ((entry (assoc key gnus-article-wash-status-strings)))
4998     (if value (nth 1 entry) (nth 2 entry))))
4999
5000 (defun gnus-article-wash-status ()
5001   "Return a string which display status of article washing."
5002   (with-current-buffer gnus-article-buffer
5003     (let ((cite (memq 'cite gnus-article-wash-types))
5004           (headers (memq 'headers gnus-article-wash-types))
5005           (boring (memq 'boring-headers gnus-article-wash-types))
5006           (pgp (memq 'pgp gnus-article-wash-types))
5007           (pem (memq 'pem gnus-article-wash-types))
5008           (signed (memq 'signed gnus-article-wash-types))
5009           (encrypted (memq 'encrypted gnus-article-wash-types))
5010           (signature (memq 'signature gnus-article-wash-types))
5011           (overstrike (memq 'overstrike gnus-article-wash-types))
5012           (emphasis (memq 'emphasis gnus-article-wash-types)))
5013       (concat
5014        (gnus-article-wash-status-entry 'cite cite)
5015        (gnus-article-wash-status-entry 'headers (or headers boring))
5016        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5017        (gnus-article-wash-status-entry 'signature signature)
5018        (gnus-article-wash-status-entry 'overstrike overstrike)
5019        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5020
5021 (defun gnus-add-wash-type (type)
5022   "Add a washing of TYPE to the current status."
5023   (add-to-list 'gnus-article-wash-types type))
5024
5025 (defun gnus-delete-wash-type (type)
5026   "Add a washing of TYPE to the current status."
5027   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5028
5029 (defun gnus-add-image (category image)
5030   "Add IMAGE of CATEGORY to the list of displayed images."
5031   (let ((entry (assq category gnus-article-image-alist)))
5032     (unless entry
5033       (setq entry (list category))
5034       (push entry gnus-article-image-alist))
5035     (nconc entry (list image))))
5036
5037 (defun gnus-delete-images (category)
5038   "Delete all images in CATEGORY."
5039   (let ((entry (assq category gnus-article-image-alist)))
5040     (dolist (image (cdr entry))
5041       (gnus-remove-image image category))
5042     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5043     (gnus-delete-wash-type category)))
5044
5045 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5046
5047 (defun gnus-article-maybe-hide-headers ()
5048   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5049 Provided for backwards compatibility."
5050   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5051                  (not (with-current-buffer gnus-summary-buffer
5052                         gnus-have-all-headers)))
5053              (not gnus-inhibit-hiding))
5054     (gnus-article-hide-headers)))
5055
5056 ;;; Article savers.
5057
5058 (defun gnus-output-to-file (file-name)
5059   "Append the current article to a file named FILE-NAME."
5060   (let ((artbuf (current-buffer)))
5061     (with-temp-buffer
5062       (insert-buffer-substring artbuf)
5063       ;; Append newline at end of the buffer as separator, and then
5064       ;; save it to file.
5065       (goto-char (point-max))
5066       (insert "\n")
5067       (let ((file-name-coding-system nnmail-pathname-coding-system))
5068         (mm-append-to-file (point-min) (point-max) file-name))
5069       t)))
5070
5071 (defun gnus-narrow-to-page (&optional arg)
5072   "Narrow the article buffer to a page.
5073 If given a numerical ARG, move forward ARG pages."
5074   (interactive "P")
5075   (setq arg (if arg (prefix-numeric-value arg) 0))
5076   (save-excursion
5077     (set-buffer gnus-article-buffer)
5078     (goto-char (point-min))
5079     (widen)
5080     ;; Remove any old next/prev buttons.
5081     (when (gnus-visual-p 'page-marker)
5082       (let ((inhibit-read-only t))
5083         (gnus-remove-text-with-property 'gnus-prev)
5084         (gnus-remove-text-with-property 'gnus-next)))
5085     (if
5086         (cond ((< arg 0)
5087                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5088               ((> arg 0)
5089                (re-search-forward page-delimiter nil 'move arg)))
5090         (goto-char (match-end 0))
5091       (save-excursion
5092         (goto-char (point-min))
5093         (setq gnus-page-broken
5094               (and (re-search-forward page-delimiter nil t) t))))
5095     (when gnus-page-broken
5096       (narrow-to-region
5097        (point)
5098        (if (re-search-forward page-delimiter nil 'move)
5099            (match-beginning 0)
5100          (point)))
5101       (when (and (gnus-visual-p 'page-marker)
5102                  (> (point-min) (save-restriction (widen) (point-min))))
5103         (save-excursion
5104           (goto-char (point-min))
5105           (gnus-insert-prev-page-button)))
5106       (when (and (gnus-visual-p 'page-marker)
5107                  (< (point-max) (save-restriction (widen) (point-max))))
5108         (save-excursion
5109           (goto-char (point-max))
5110           (gnus-insert-next-page-button))))))
5111
5112 ;; Article mode commands
5113
5114 (defun gnus-article-goto-next-page ()
5115   "Show the next page of the article."
5116   (interactive)
5117   (when (gnus-article-next-page)
5118     (goto-char (point-min))
5119     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5120
5121
5122 (defun gnus-article-goto-prev-page ()
5123   "Show the previous page of the article."
5124   (interactive)
5125   (if (bobp)
5126       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5127     (gnus-article-prev-page nil)))
5128
5129 ;; This is cleaner but currently breaks `gnus-pick-mode':
5130 ;;
5131 ;; (defun gnus-article-goto-next-page ()
5132 ;;   "Show the next page of the article."
5133 ;;   (interactive)
5134 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5135 ;;     (gnus-summary-next-page)))
5136 ;;
5137 ;; (defun gnus-article-goto-prev-page ()
5138 ;;   "Show the next page of the article."
5139 ;;   (interactive)
5140 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5141 ;;     (gnus-summary-prev-page)))
5142
5143 (defun gnus-article-next-page (&optional lines)
5144   "Show the next page of the current article.
5145 If end of article, return non-nil.  Otherwise return nil.
5146 Argument LINES specifies lines to be scrolled up."
5147   (interactive "p")
5148   (move-to-window-line -1)
5149   (if (save-excursion
5150         (end-of-line)
5151         (and (pos-visible-in-window-p)  ;Not continuation line.
5152              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5153       ;; Nothing in this page.
5154       (if (or (not gnus-page-broken)
5155               (save-excursion
5156                 (save-restriction
5157                   (widen)
5158                   (forward-line)
5159                   (eobp)))) ;Real end-of-buffer?
5160           (progn
5161             (when gnus-article-over-scroll
5162               (gnus-article-next-page-1 lines))
5163             t)                  ;Nothing more.
5164         (gnus-narrow-to-page 1)         ;Go to next page.
5165         nil)
5166     ;; More in this page.
5167     (gnus-article-next-page-1 lines)
5168     nil))
5169
5170 (defun gnus-article-next-page-1 (lines)
5171   (let ((scroll-in-place nil))
5172     (condition-case ()
5173         (scroll-up lines)
5174       (end-of-buffer
5175        ;; Long lines may cause an end-of-buffer error.
5176        (goto-char (point-max)))))
5177   (move-to-window-line 0))
5178
5179 (defun gnus-article-prev-page (&optional lines)
5180   "Show previous page of current article.
5181 Argument LINES specifies lines to be scrolled down."
5182   (interactive "p")
5183   (move-to-window-line 0)
5184   (if (and gnus-page-broken
5185            (bobp)
5186            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5187       (progn
5188         (gnus-narrow-to-page -1)        ;Go to previous page.
5189         (goto-char (point-max))
5190         (recenter -1))
5191     (let ((scroll-in-place nil))
5192       (prog1
5193           (condition-case ()
5194               (scroll-down lines)
5195             (beginning-of-buffer
5196              (goto-char (point-min))))
5197         (move-to-window-line 0)))))
5198
5199 (defun gnus-article-only-boring-p ()
5200   "Decide whether there is only boring text remaining in the article.
5201 Something \"interesting\" is a word of at least two letters that does
5202 not have a face in `gnus-article-boring-faces'."
5203   (when (and gnus-article-skip-boring
5204              (boundp 'gnus-article-boring-faces)
5205              (symbol-value 'gnus-article-boring-faces))
5206     (save-excursion
5207       (catch 'only-boring
5208         (while (re-search-forward "\\b\\w\\w" nil t)
5209           (forward-char -1)
5210           (when (not (gnus-intersection
5211                       (gnus-faces-at (point))
5212                       (symbol-value 'gnus-article-boring-faces)))
5213             (throw 'only-boring nil)))
5214         (throw 'only-boring t)))))
5215
5216 (defun gnus-article-refer-article ()
5217   "Read article specified by message-id around point."
5218   (interactive)
5219   (save-excursion
5220     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
5221     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
5222     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
5223         (let ((msg-id (concat "<" (match-string 0) ">")))
5224           (set-buffer gnus-summary-buffer)
5225           (gnus-summary-refer-article msg-id))
5226       (error "No references around point"))))
5227
5228 (defun gnus-article-show-summary ()
5229   "Reconfigure windows to show summary buffer."
5230   (interactive)
5231   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5232       (error "There is no summary buffer for this article buffer")
5233     (gnus-article-set-globals)
5234     (gnus-configure-windows 'article)
5235     (gnus-summary-goto-subject gnus-current-article)
5236     (gnus-summary-position-point)))
5237
5238 (defun gnus-article-describe-briefly ()
5239   "Describe article mode commands briefly."
5240   (interactive)
5241   (gnus-message 6 (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")))
5242
5243 (defun gnus-article-summary-command ()
5244   "Execute the last keystroke in the summary buffer."
5245   (interactive)
5246   (let ((obuf (current-buffer))
5247         (owin (current-window-configuration))
5248         func)
5249     (switch-to-buffer gnus-article-current-summary 'norecord)
5250     (setq func (lookup-key (current-local-map) (this-command-keys)))
5251     (call-interactively func)
5252     (set-buffer obuf)
5253     (set-window-configuration owin)
5254     (set-window-point (get-buffer-window (current-buffer)) (point))))
5255
5256 (defun gnus-article-summary-command-nosave ()
5257   "Execute the last keystroke in the summary buffer."
5258   (interactive)
5259   (let (func)
5260     (pop-to-buffer gnus-article-current-summary 'norecord)
5261     (setq func (lookup-key (current-local-map) (this-command-keys)))
5262     (call-interactively func)))
5263
5264 (defun gnus-article-check-buffer ()
5265   "Beep if not in an article buffer."
5266   (unless (equal major-mode 'gnus-article-mode)
5267     (error "Command invoked outside of a Gnus article buffer")))
5268
5269 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5270   "Read a summary buffer key sequence and execute it from the article buffer."
5271   (interactive "P")
5272   (gnus-article-check-buffer)
5273   (let ((nosaves
5274          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5275            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5276            "=" "^" "\M-^" "|"))
5277         (nosave-but-article
5278          '("A\r"))
5279         (nosave-in-article
5280          '("\C-d"))
5281         (up-to-top
5282          '("n" "Gn" "p" "Gp"))
5283         keys new-sum-point)
5284     (save-excursion
5285       (set-buffer gnus-article-current-summary)
5286       (let (gnus-pick-mode)
5287         (push (or key last-command-event) unread-command-events)
5288         (setq keys (if (featurep 'xemacs)
5289                        (events-to-keys (read-key-sequence nil))
5290                      (read-key-sequence nil)))))
5291
5292     (message "")
5293
5294     (if (or (member keys nosaves)
5295             (member keys nosave-but-article)
5296             (member keys nosave-in-article))
5297         (let (func)
5298           (save-window-excursion
5299             (pop-to-buffer gnus-article-current-summary 'norecord)
5300             ;; We disable the pick minor mode commands.
5301             (let (gnus-pick-mode)
5302               (setq func (lookup-key (current-local-map) keys))))
5303           (if (or (not func)
5304                   (numberp func))
5305               (ding)
5306             (unless (member keys nosave-in-article)
5307               (set-buffer gnus-article-current-summary))
5308             (call-interactively func)
5309             (setq new-sum-point (point)))
5310           (when (member keys nosave-but-article)
5311             (pop-to-buffer gnus-article-buffer 'norecord)))
5312       ;; These commands should restore window configuration.
5313       (let ((obuf (current-buffer))
5314             (owin (current-window-configuration))
5315             (opoint (point))
5316             win func in-buffer selected new-sum-start new-sum-hscroll)
5317         (cond (not-restore-window
5318                (pop-to-buffer gnus-article-current-summary 'norecord))
5319               ((setq win (get-buffer-window gnus-article-current-summary))
5320                (select-window win))
5321               (t
5322                (switch-to-buffer gnus-article-current-summary 'norecord)))
5323         (setq in-buffer (current-buffer))
5324         ;; We disable the pick minor mode commands.
5325         (if (and (setq func (let (gnus-pick-mode)
5326                               (lookup-key (current-local-map) keys)))
5327                  (functionp func))
5328             (progn
5329               (call-interactively func)
5330               (when (eq win (selected-window))
5331                 (setq new-sum-point (point)
5332                       new-sum-start (window-start win)
5333                       new-sum-hscroll (window-hscroll win)))
5334               (when (eq in-buffer (current-buffer))
5335                 (setq selected (gnus-summary-select-article))
5336                 (set-buffer obuf)
5337                 (unless not-restore-window
5338                   (set-window-configuration owin))
5339                 (when (eq selected 'old)
5340                   (article-goto-body)
5341                   (set-window-start (get-buffer-window (current-buffer))
5342                                     1)
5343                   (set-window-point (get-buffer-window (current-buffer))
5344                                     (point)))
5345                 (when (and (not not-restore-window)
5346                            new-sum-point)
5347                   (set-window-point win new-sum-point)
5348                   (set-window-start win new-sum-start)
5349                   (set-window-hscroll win new-sum-hscroll))))
5350           (set-window-configuration owin)
5351           (ding))))))
5352
5353 (defun gnus-article-describe-key (key)
5354   "Display documentation of the function invoked by KEY.  KEY is a string."
5355   (interactive "kDescribe key: ")
5356   (gnus-article-check-buffer)
5357   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5358       (save-excursion
5359         (set-buffer gnus-article-current-summary)
5360         (let (gnus-pick-mode)
5361           (if (featurep 'xemacs)
5362               (progn
5363                 (push (elt key 0) unread-command-events)
5364                 (setq key (events-to-keys
5365                            (read-key-sequence "Describe key: "))))
5366             (setq unread-command-events
5367                   (mapcar
5368                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5369                    (string-to-list key)))
5370             (setq key (read-key-sequence "Describe key: "))))
5371         (describe-key key))
5372     (describe-key key)))
5373
5374 (defun gnus-article-describe-key-briefly (key &optional insert)
5375   "Display documentation of the function invoked by KEY.  KEY is a string."
5376   (interactive "kDescribe key: \nP")
5377   (gnus-article-check-buffer)
5378   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5379       (save-excursion
5380         (set-buffer gnus-article-current-summary)
5381         (let (gnus-pick-mode)
5382           (if (featurep 'xemacs)
5383               (progn
5384                 (push (elt key 0) unread-command-events)
5385                 (setq key (events-to-keys
5386                            (read-key-sequence "Describe key: "))))
5387             (setq unread-command-events
5388                   (mapcar
5389                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5390                    (string-to-list key)))
5391             (setq key (read-key-sequence "Describe key: "))))
5392         (describe-key-briefly key insert))
5393     (describe-key-briefly key insert)))
5394
5395 (defun gnus-article-reply-with-original (&optional wide)
5396   "Start composing a reply mail to the current message.
5397 The text in the region will be yanked.  If the region isn't active,
5398 the entire article will be yanked."
5399   (interactive "P")
5400   (let ((article (cdr gnus-article-current))
5401         contents)
5402     (if (not (gnus-region-active-p))
5403         (with-current-buffer gnus-summary-buffer
5404           (gnus-summary-reply (list (list article)) wide))
5405       (setq contents (buffer-substring (point) (mark t)))
5406       ;; Deactivate active regions.
5407       (when (and (boundp 'transient-mark-mode)
5408                  transient-mark-mode)
5409         (setq mark-active nil))
5410       (with-current-buffer gnus-summary-buffer
5411         (gnus-summary-reply
5412          (list (list article contents)) wide)))))
5413
5414 (defun gnus-article-followup-with-original ()
5415   "Compose a followup to the current article.
5416 The text in the region will be yanked.  If the region isn't active,
5417 the entire article will be yanked."
5418   (interactive)
5419   (let ((article (cdr gnus-article-current))
5420         contents)
5421       (if (not (gnus-region-active-p))
5422           (with-current-buffer gnus-summary-buffer
5423             (gnus-summary-followup (list (list article))))
5424         (setq contents (buffer-substring (point) (mark t)))
5425         ;; Deactivate active regions.
5426         (when (and (boundp 'transient-mark-mode)
5427                    transient-mark-mode)
5428           (setq mark-active nil))
5429         (with-current-buffer gnus-summary-buffer
5430           (gnus-summary-followup
5431            (list (list article contents)))))))
5432
5433 (defun gnus-article-hide (&optional arg force)
5434   "Hide all the gruft in the current article.
5435 This means that signatures, cited text and (some) headers will be
5436 hidden.
5437 If given a prefix, show the hidden text instead."
5438   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5439   (gnus-article-hide-headers arg)
5440   (gnus-article-hide-list-identifiers arg)
5441   (gnus-article-hide-citation-maybe arg force)
5442   (gnus-article-hide-signature arg))
5443
5444 (defun gnus-article-maybe-highlight ()
5445   "Do some article highlighting if article highlighting is requested."
5446   (when (gnus-visual-p 'article-highlight 'highlight)
5447     (gnus-article-highlight-some)))
5448
5449 (defun gnus-check-group-server ()
5450   ;; Make sure the connection to the server is alive.
5451   (unless (gnus-server-opened
5452            (gnus-find-method-for-group gnus-newsgroup-name))
5453     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5454     (gnus-request-group gnus-newsgroup-name t)))
5455
5456 (eval-when-compile
5457   (autoload 'nneething-get-file-name "nneething"))
5458
5459 (defun gnus-request-article-this-buffer (article group)
5460   "Get an article and insert it into this buffer."
5461   (let (do-update-line sparse-header)
5462     (prog1
5463         (save-excursion
5464           (erase-buffer)
5465           (gnus-kill-all-overlays)
5466           (setq group (or group gnus-newsgroup-name))
5467
5468           ;; Using `gnus-request-article' directly will insert the article into
5469           ;; `nntp-server-buffer' - so we'll save some time by not having to
5470           ;; copy it from the server buffer into the article buffer.
5471
5472           ;; We only request an article by message-id when we do not have the
5473           ;; headers for it, so we'll have to get those.
5474           (when (stringp article)
5475             (gnus-read-header article))
5476
5477           ;; If the article number is negative, that means that this article
5478           ;; doesn't belong in this newsgroup (possibly), so we find its
5479           ;; message-id and request it by id instead of number.
5480           (when (and (numberp article)
5481                      gnus-summary-buffer
5482                      (get-buffer gnus-summary-buffer)
5483                      (gnus-buffer-exists-p gnus-summary-buffer))
5484             (save-excursion
5485               (set-buffer gnus-summary-buffer)
5486               (let ((header (gnus-summary-article-header article)))
5487                 (when (< article 0)
5488                   (cond
5489                    ((memq article gnus-newsgroup-sparse)
5490                     ;; This is a sparse gap article.
5491                     (setq do-update-line article)
5492                     (setq article (mail-header-id header))
5493                     (setq sparse-header (gnus-read-header article))
5494                     (setq gnus-newsgroup-sparse
5495                           (delq article gnus-newsgroup-sparse)))
5496                    ((vectorp header)
5497                     ;; It's a real article.
5498                     (setq article (mail-header-id header)))
5499                    (t
5500                     ;; It is an extracted pseudo-article.
5501                     (setq article 'pseudo)
5502                     (gnus-request-pseudo-article header))))
5503
5504                 (let ((method (gnus-find-method-for-group
5505                                gnus-newsgroup-name)))
5506                   (when (and (eq (car method) 'nneething)
5507                              (vectorp header))
5508                     (let ((dir (nneething-get-file-name
5509                                 (mail-header-id header))))
5510                       (when (and (stringp dir)
5511                                  (file-directory-p dir))
5512                         (setq article 'nneething)
5513                         (gnus-group-enter-directory dir))))))))
5514
5515           (cond
5516            ;; Refuse to select canceled articles.
5517            ((and (numberp article)
5518                  gnus-summary-buffer
5519                  (get-buffer gnus-summary-buffer)
5520                  (gnus-buffer-exists-p gnus-summary-buffer)
5521                  (eq (cdr (with-current-buffer gnus-summary-buffer
5522                             (assq article gnus-newsgroup-reads)))
5523                      gnus-canceled-mark))
5524             nil)
5525            ;; We first check `gnus-original-article-buffer'.
5526            ((and (get-buffer gnus-original-article-buffer)
5527                  (numberp article)
5528                  (with-current-buffer gnus-original-article-buffer
5529                    (and (equal (car gnus-original-article) group)
5530                         (eq (cdr gnus-original-article) article))))
5531             (insert-buffer-substring gnus-original-article-buffer)
5532             'article)
5533            ;; Check the backlog.
5534            ((and gnus-keep-backlog
5535                  (gnus-backlog-request-article group article (current-buffer)))
5536             'article)
5537            ;; Check asynchronous pre-fetch.
5538            ((gnus-async-request-fetched-article group article (current-buffer))
5539             (gnus-async-prefetch-next group article gnus-summary-buffer)
5540             (when (and (numberp article) gnus-keep-backlog)
5541               (gnus-backlog-enter-article group article (current-buffer)))
5542             'article)
5543            ;; Check the cache.
5544            ((and gnus-use-cache
5545                  (numberp article)
5546                  (gnus-cache-request-article article group))
5547             'article)
5548            ;; Check the agent cache.
5549            ((gnus-agent-request-article article group)
5550             'article)
5551            ;; Get the article and put into the article buffer.
5552            ((or (stringp article)
5553                 (numberp article))
5554             (let ((gnus-override-method gnus-override-method)
5555                   (methods (and (stringp article)
5556                                 gnus-refer-article-method))
5557                   (backend (car (gnus-find-method-for-group
5558                                  gnus-newsgroup-name)))
5559                   result
5560                   (inhibit-read-only t))
5561               (if (or (not (listp methods))
5562                       (and (symbolp (car methods))
5563                            (assq (car methods) nnoo-definition-alist)))
5564                   (setq methods (list methods)))
5565               (when (and (null gnus-override-method)
5566                          methods)
5567                 (setq gnus-override-method (pop methods)))
5568               (while (not result)
5569                 (when (eq gnus-override-method 'current)
5570                   (setq gnus-override-method
5571                         (with-current-buffer gnus-summary-buffer
5572                           gnus-current-select-method)))
5573                 (erase-buffer)
5574                 (gnus-kill-all-overlays)
5575                 (let ((gnus-newsgroup-name group))
5576                   (gnus-check-group-server))
5577                 (cond
5578                  ((gnus-request-article article group (current-buffer))
5579                   (when (numberp article)
5580                     (gnus-async-prefetch-next group article
5581                                               gnus-summary-buffer)
5582                     (when gnus-keep-backlog
5583                       (gnus-backlog-enter-article
5584                        group article (current-buffer))))
5585                   (setq result 'article))
5586                  (methods
5587                   (setq gnus-override-method (pop methods)))
5588                  ((not (string-match "^400 "
5589                                      (nnheader-get-report backend)))
5590                   ;; If we get 400 server disconnect, reconnect and
5591                   ;; retry; otherwise, assume the article has expired.
5592                   (setq result 'done))))
5593               (and (eq result 'article) 'article)))
5594            ;; It was a pseudo.
5595            (t article)))
5596
5597       ;; Associate this article with the current summary buffer.
5598       (setq gnus-article-current-summary gnus-summary-buffer)
5599
5600       ;; Take the article from the original article buffer
5601       ;; and place it in the buffer it's supposed to be in.
5602       (when (and (get-buffer gnus-article-buffer)
5603                  (equal (buffer-name (current-buffer))
5604                         (buffer-name (get-buffer gnus-article-buffer))))
5605         (save-excursion
5606           (if (get-buffer gnus-original-article-buffer)
5607               (set-buffer gnus-original-article-buffer)
5608             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5609             (buffer-disable-undo)
5610             (setq major-mode 'gnus-original-article-mode)
5611             (setq buffer-read-only t))
5612           (let ((inhibit-read-only t))
5613             (erase-buffer)
5614             (insert-buffer-substring gnus-article-buffer))
5615           (setq gnus-original-article (cons group article)))
5616
5617         ;; Decode charsets.
5618         (run-hooks 'gnus-article-decode-hook)
5619         ;; Mark article as decoded or not.
5620         (setq gnus-article-decoded-p gnus-article-decode-hook))
5621
5622       ;; Update sparse articles.
5623       (when (and do-update-line
5624                  (or (numberp article)
5625                      (stringp article)))
5626         (let ((buf (current-buffer)))
5627           (set-buffer gnus-summary-buffer)
5628           (gnus-summary-update-article do-update-line sparse-header)
5629           (gnus-summary-goto-subject do-update-line nil t)
5630           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5631                             (point))
5632           (set-buffer buf))))))
5633
5634 ;;;
5635 ;;; Article editing
5636 ;;;
5637
5638 (defcustom gnus-article-edit-mode-hook nil
5639   "Hook run in article edit mode buffers."
5640   :group 'gnus-article-various
5641   :type 'hook)
5642
5643 (defvar gnus-article-edit-done-function nil)
5644
5645 (defvar gnus-article-edit-mode-map nil)
5646
5647 ;; Should we be using derived.el for this?
5648 (unless gnus-article-edit-mode-map
5649   (setq gnus-article-edit-mode-map (make-keymap))
5650   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5651
5652   (gnus-define-keys gnus-article-edit-mode-map
5653     "\C-c?"    describe-mode
5654     "\C-c\C-c" gnus-article-edit-done
5655     "\C-c\C-k" gnus-article-edit-exit
5656     "\C-c\C-f\C-t" message-goto-to
5657     "\C-c\C-f\C-o" message-goto-from
5658     "\C-c\C-f\C-b" message-goto-bcc
5659     ;;"\C-c\C-f\C-w" message-goto-fcc
5660     "\C-c\C-f\C-c" message-goto-cc
5661     "\C-c\C-f\C-s" message-goto-subject
5662     "\C-c\C-f\C-r" message-goto-reply-to
5663     "\C-c\C-f\C-n" message-goto-newsgroups
5664     "\C-c\C-f\C-d" message-goto-distribution
5665     "\C-c\C-f\C-f" message-goto-followup-to
5666     "\C-c\C-f\C-m" message-goto-mail-followup-to
5667     "\C-c\C-f\C-k" message-goto-keywords
5668     "\C-c\C-f\C-u" message-goto-summary
5669     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5670     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
5671     "\C-c\C-b" message-goto-body
5672     "\C-c\C-i" message-goto-signature
5673
5674     "\C-c\C-t" message-insert-to
5675     "\C-c\C-n" message-insert-newsgroups
5676     "\C-c\C-o" message-sort-headers
5677     "\C-c\C-e" message-elide-region
5678     "\C-c\C-v" message-delete-not-region
5679     "\C-c\C-z" message-kill-to-signature
5680     "\M-\r" message-newline-and-reformat
5681     "\C-c\C-a" mml-attach-file
5682     "\C-a" message-beginning-of-line
5683     "\t" message-tab
5684     "\M-;" comment-region)
5685
5686   (gnus-define-keys (gnus-article-edit-wash-map
5687                      "\C-c\C-w" gnus-article-edit-mode-map)
5688     "f" gnus-article-edit-full-stops))
5689
5690 (easy-menu-define
5691   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5692   '("Field"
5693     ["Fetch To" message-insert-to t]
5694     ["Fetch Newsgroups" message-insert-newsgroups t]
5695     "----"
5696     ["To" message-goto-to t]
5697     ["From" message-goto-from t]
5698     ["Subject" message-goto-subject t]
5699     ["Cc" message-goto-cc t]
5700     ["Reply-To" message-goto-reply-to t]
5701     ["Summary" message-goto-summary t]
5702     ["Keywords" message-goto-keywords t]
5703     ["Newsgroups" message-goto-newsgroups t]
5704     ["Followup-To" message-goto-followup-to t]
5705     ["Mail-Followup-To" message-goto-mail-followup-to t]
5706     ["Distribution" message-goto-distribution t]
5707     ["Body" message-goto-body t]
5708     ["Signature" message-goto-signature t]))
5709
5710 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
5711   "Major mode for editing articles.
5712 This is an extended text-mode.
5713
5714 \\{gnus-article-edit-mode-map}"
5715   (make-local-variable 'gnus-article-edit-done-function)
5716   (make-local-variable 'gnus-prev-winconf)
5717   (set (make-local-variable 'font-lock-defaults)
5718        '(message-font-lock-keywords t))
5719   (set (make-local-variable 'mail-header-separator) "")
5720   (set (make-local-variable 'gnus-article-edit-mode) t)
5721   (easy-menu-add message-mode-field-menu message-mode-map)
5722   (mml-mode)
5723   (setq buffer-read-only nil)
5724   (buffer-enable-undo)
5725   (widen))
5726
5727 (defun gnus-article-edit (&optional force)
5728   "Edit the current article.
5729 This will have permanent effect only in mail groups.
5730 If FORCE is non-nil, allow editing of articles even in read-only
5731 groups."
5732   (interactive "P")
5733   (when (and (not force)
5734              (gnus-group-read-only-p))
5735     (error "The current newsgroup does not support article editing"))
5736   (gnus-article-date-original)
5737   (gnus-article-edit-article
5738    'ignore
5739    `(lambda (no-highlight)
5740       'ignore
5741       (gnus-summary-edit-article-done
5742        ,(or (mail-header-references gnus-current-headers) "")
5743        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5744
5745 (defun gnus-article-edit-article (start-func exit-func)
5746   "Start editing the contents of the current article buffer."
5747   (let ((winconf (current-window-configuration)))
5748     (set-buffer gnus-article-buffer)
5749     (let ((message-auto-save-directory
5750            ;; Don't associate the article buffer with a draft file.
5751            nil))
5752       (gnus-article-edit-mode))
5753     (funcall start-func)
5754     (set-buffer-modified-p nil)
5755     (gnus-configure-windows 'edit-article)
5756     (setq gnus-article-edit-done-function exit-func)
5757     (setq gnus-prev-winconf winconf)
5758     (gnus-message 6 "C-c C-c to end edits")))
5759
5760 (defun gnus-article-edit-done (&optional arg)
5761   "Update the article edits and exit."
5762   (interactive "P")
5763   (let ((func gnus-article-edit-done-function)
5764         (buf (current-buffer))
5765         (start (window-start))
5766         (p (point))
5767         (winconf gnus-prev-winconf))
5768     (widen) ;; Widen it in case that users narrowed the buffer.
5769     (funcall func arg)
5770     (set-buffer buf)
5771     ;; The cache and backlog have to be flushed somewhat.
5772     (when gnus-keep-backlog
5773       (gnus-backlog-remove-article
5774        (car gnus-article-current) (cdr gnus-article-current)))
5775     ;; Flush original article as well.
5776     (save-excursion
5777       (when (get-buffer gnus-original-article-buffer)
5778         (set-buffer gnus-original-article-buffer)
5779         (setq gnus-original-article nil)))
5780     (when gnus-use-cache
5781       (gnus-cache-update-article
5782        (car gnus-article-current) (cdr gnus-article-current)))
5783     ;; We remove all text props from the article buffer.
5784     (kill-all-local-variables)
5785     (set-text-properties (point-min) (point-max) nil)
5786     (gnus-article-mode)
5787     (set-window-configuration winconf)
5788     (set-buffer buf)
5789     (set-window-start (get-buffer-window buf) start)
5790     (set-window-point (get-buffer-window buf) (point)))
5791   (gnus-summary-show-article))
5792
5793 (defun gnus-article-edit-exit ()
5794   "Exit the article editing without updating."
5795   (interactive)
5796   (when (or (not (buffer-modified-p))
5797             (yes-or-no-p "Article modified; kill anyway? "))
5798     (let ((curbuf (current-buffer))
5799           (p (point))
5800           (window-start (window-start)))
5801       (erase-buffer)
5802       (if (gnus-buffer-live-p gnus-original-article-buffer)
5803           (insert-buffer gnus-original-article-buffer))
5804       (let ((winconf gnus-prev-winconf))
5805         (kill-all-local-variables)
5806         (gnus-article-mode)
5807         (set-window-configuration winconf)
5808         ;; Tippy-toe some to make sure that point remains where it was.
5809         (save-current-buffer
5810           (set-buffer curbuf)
5811           (set-window-start (get-buffer-window (current-buffer)) window-start)
5812           (goto-char p))))
5813     (gnus-summary-show-article)))
5814
5815 (defun gnus-article-edit-full-stops ()
5816   "Interactively repair spacing at end of sentences."
5817   (interactive)
5818   (save-excursion
5819     (goto-char (point-min))
5820     (search-forward-regexp "^$" nil t)
5821     (let ((case-fold-search nil))
5822       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5823
5824 ;;;
5825 ;;; Article highlights
5826 ;;;
5827
5828 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5829
5830 ;;; Internal Variables:
5831
5832 (defcustom gnus-button-url-regexp
5833   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
5834       "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?[-a-z0-9_=!?#$@~%&*+\\/:;.,[:word:]]+[-a-z0-9_=#$@~%&*+\\/[:word:]]\\)"
5835     "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)\\)")
5836   "Regular expression that matches URLs."
5837   :group 'gnus-article-buttons
5838   :type 'regexp)
5839
5840 (defcustom gnus-button-valid-fqdn-regexp
5841   message-valid-fqdn-regexp
5842   "Regular expression that matches a valid FQDN."
5843   :version "22.1"
5844   :group 'gnus-article-buttons
5845   :type 'regexp)
5846
5847 (defcustom gnus-button-man-handler 'manual-entry
5848   "Function to use for displaying man pages.
5849 The function must take at least one argument with a string naming the
5850 man page."
5851   :version "22.1"
5852   :type '(choice (function-item :tag "Man" manual-entry)
5853                  (function-item :tag "Woman" woman)
5854                  (function :tag "Other"))
5855   :group 'gnus-article-buttons)
5856
5857 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
5858   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
5859 If the default site is too slow, try to find a CTAN mirror, see
5860 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
5861 the variable `gnus-button-handle-ctan'."
5862   :version "22.1"
5863   :group 'gnus-article-buttons
5864   :link '(custom-manual "(gnus)Group Parameters")
5865   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
5866                  (const "http://tug.ctan.org/tex-archive/")
5867                  (const "http://www.dante.de/CTAN/")
5868                  (string :tag "Other")))
5869
5870 (defcustom gnus-button-ctan-handler 'browse-url
5871   "Function to use for displaying CTAN links.
5872 The function must take one argument, the string naming the URL."
5873   :version "22.1"
5874   :type '(choice (function-item :tag "Browse Url" browse-url)
5875                  (function :tag "Other"))
5876   :group 'gnus-article-buttons)
5877
5878 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
5879   "Bogus strings removed from CTAN URLs."
5880   :version "22.1"
5881   :group 'gnus-article-buttons
5882   :type '(choice (const "^/?tex-archive/\\|/")
5883                  (regexp :tag "Other")))
5884
5885 (defcustom gnus-button-ctan-directory-regexp
5886   (concat
5887    "\\(?:"
5888    "biblio\\|digests\\|dviware\\|fonts\\|graphics\\|help\\|"
5889    "indexing\\|info\\|language\\|macros\\|support\\|systems\\|"
5890    "tds\\|tools\\|usergrps\\|web\\|nonfree\\|obsolete"
5891    "\\)")
5892   "Regular expression for ctan directories.
5893 It should match all directories in the top level of `gnus-ctan-url'."
5894   :version "22.1"
5895   :group 'gnus-article-buttons
5896   :type 'regexp)
5897
5898 (defcustom gnus-button-mid-or-mail-regexp
5899   (concat "\\b\\(<?[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t ]*@"
5900           ;; Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
5901           gnus-button-valid-fqdn-regexp
5902           ">?\\)\\b")
5903   "Regular expression that matches a message ID or a mail address."
5904   :version "22.1"
5905   :group 'gnus-article-buttons
5906   :type 'regexp)
5907
5908 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
5909   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
5910 Strings like this can be either a message ID or a mail address.  If it is one
5911 of the symbols `mid' or `mail', Gnus will always assume that the string is a
5912 message ID or a mail address, respectively.  If this variable is set to the
5913 symbol `ask', always query the user what do do.  If it is a function, this
5914 function will be called with the string as it's only argument.  The function
5915 must return `mid', `mail', `invalid' or `ask'."
5916   :version "22.1"
5917   :group 'gnus-article-buttons
5918   :type '(choice (function-item :tag "Heuristic function"
5919                                 gnus-button-mid-or-mail-heuristic)
5920                  (const ask)
5921                  (const mid)
5922                  (const mail)))
5923
5924 (defcustom gnus-button-mid-or-mail-heuristic-alist
5925   '((-10.0 . ".+\\$.+@")
5926     (-10.0 . "#")
5927     (-10.0 . "\\*")
5928     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
5929     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
5930     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
5931     (-1.0  . "^[^a-z]+@")
5932     ;;
5933     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
5934     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
5935     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
5936     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
5937     ;;
5938     (-2.0  . "^[0-9]")
5939     (-1.0  . "^[0-9][0-9]")
5940     ;;
5941     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
5942     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
5943     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
5944     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
5945     ;;
5946     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
5947     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
5948     ;;       "[0-9]{8,}.*\@"
5949     (-3.0
5950      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
5951     ;; "[0-9]{12,}.*\@"
5952     ;; compensation for TDMA dated mail addresses:
5953     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
5954     ;;
5955     (-20.0 . "\\.fsf@") ;; Gnus
5956     (-20.0 . "^slrn")
5957     (-20.0 . "^Pine")
5958     (-20.0 . "_-_") ;; Subject change in thread
5959     ;;
5960     (-20.0 . "\\.ln@") ;; leafnode
5961     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
5962     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
5963     ;;
5964     ;; (5.0 . "") ;; $local_part_len <= 7
5965     (10.0  . "^[^0-9]+@")
5966     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
5967     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
5968     (3.0   . "\@stud")
5969     ;;
5970     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
5971     ;;
5972     (0.5   . "^[A-Z][a-z]")
5973     (0.5   . "^[A-Z][a-z][a-z]")
5974     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
5975     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
5976   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
5977
5978 A negative RATE indicates a message IDs, whereas a positive indicates a mail
5979 address.  The REGEXP is processed with `case-fold-search' set to nil."
5980   :version "22.1"
5981   :group 'gnus-article-buttons
5982   :type '(repeat (cons (number :tag "Rate")
5983                        (regexp :tag "Regexp"))))
5984
5985 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
5986   "Guess whether MID-OR-MAIL is a message ID or a mail address.
5987 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
5988 address, `ask' if unsure and `invalid' if the string is invalid."
5989   (let ((case-fold-search nil)
5990         (list gnus-button-mid-or-mail-heuristic-alist)
5991         (result 0) rate regexp lpartlen elem)
5992     (setq lpartlen
5993           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
5994     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
5995     ;; Certain special cases...
5996     (when (string-match
5997            (concat
5998             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
5999             "^[0-9]+\\.[0-9]+@compuserve\\|"
6000             "@public\\.gmane\\.org")
6001            mid-or-mail)
6002       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
6003       (setq result 'mail))
6004     (when (string-match "@.*@\\| " mid-or-mail)
6005       (gnus-message 8 "`%s' is invalid." mid-or-mail)
6006       (setq result 'invalid))
6007     ;; Nothing more to do, if result is not a number here...
6008     (when (numberp result)
6009       (while list
6010         (setq elem (car list)
6011               rate (car elem)
6012               regexp (cdr elem)
6013               list (cdr list))
6014         (when (string-match regexp mid-or-mail)
6015           (setq result (+ result rate))
6016           (gnus-message
6017            9 "`%s' matched `%s', rate `%s', result `%s'."
6018            mid-or-mail regexp rate result)))
6019       (when (<= lpartlen 7)
6020         (setq result (+ result 5.0))
6021         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6022                       mid-or-mail result))
6023       (when (>= lpartlen 12)
6024         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6025         (cond
6026          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6027           ;; Long local part should contain realname if e-mail address,
6028           ;; too many digits: message-id.
6029           ;; $score -= 5.0 + 0.1 * $local_part_len;
6030           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6031           (setq result (+ result rate))
6032           (gnus-message
6033            9 "Many digits in `%s', rate `%s', result `%s'."
6034            mid-or-mail rate result))
6035          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6036                         mid-or-mail)
6037           ;; Too few vowels [^aeiouy]{4,}.*\@
6038           (setq result (+ result -5.0))
6039           (gnus-message
6040            9 "Few vowels in `%s', rate `%s', result `%s'."
6041            mid-or-mail -5.0 result))
6042          (t
6043           (setq result (+ result 5.0))
6044           (gnus-message
6045            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6046     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6047     ;; Maybe we should make this a customizable alist: (condition . 'result)
6048     (cond
6049      ((symbolp result) result)
6050      ;; Now convert number into proper results:
6051      ((< result -10.0) 'mid)
6052      ((> result  10.0) 'mail)
6053      (t 'ask))))
6054
6055 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6056   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6057          (url-mid (concat "news" ":" mid-or-mail))
6058          (url-mailto (concat "mailto" ":" mid-or-mail)))
6059     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6060     (when (fboundp pref)
6061       (setq guessed
6062             ;; get rid of surrounding angles...
6063             (funcall pref
6064                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6065       (if (or (eq 'mid guessed) (eq 'mail guessed))
6066           (setq pref guessed)
6067         (setq pref 'ask)))
6068     (if (eq pref 'ask)
6069         (save-window-excursion
6070           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6071               (setq pref 'mail)
6072             (setq pref 'mid))))
6073     (cond ((eq pref 'mid)
6074            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6075            (gnus-button-handle-news url-mid))
6076           ((eq pref 'mail)
6077            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6078            (gnus-url-mailto url-mailto))
6079           (t (gnus-message 3 "Invalid string.")))))
6080
6081 (defun gnus-button-handle-custom (url)
6082   "Follow a Custom URL."
6083   (customize-apropos (gnus-url-unhex-string url)))
6084
6085 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6086
6087 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6088 ;; stuff?
6089
6090 (defun gnus-button-handle-describe-function (url)
6091   "Call `describe-function' when pushing the corresponding URL button."
6092   (describe-function
6093    (intern
6094     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6095
6096 (defun gnus-button-handle-describe-variable (url)
6097   "Call `describe-variable' when pushing the corresponding URL button."
6098   (describe-variable
6099    (intern
6100     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6101
6102 (defun gnus-button-handle-symbol (url)
6103 "Display help on variable or function.
6104 Calls `describe-variable' or `describe-function'."
6105   (let ((sym (intern url)))
6106     (cond
6107      ((fboundp sym) (describe-function sym))
6108      ((boundp sym) (describe-variable sym))
6109      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6110
6111 (defun gnus-button-handle-describe-key (url)
6112   "Call `describe-key' when pushing the corresponding URL button."
6113   (let* ((key-string
6114           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6115          (keys (ignore-errors (eval `(kbd ,key-string)))))
6116     (if keys
6117         (describe-key keys)
6118       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6119
6120 (defun gnus-button-handle-apropos (url)
6121   "Call `apropos' when pushing the corresponding URL button."
6122   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6123
6124 (defun gnus-button-handle-apropos-command (url)
6125   "Call `apropos' when pushing the corresponding URL button."
6126   (apropos-command
6127    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6128
6129 (defun gnus-button-handle-apropos-variable (url)
6130   "Call `apropos' when pushing the corresponding URL button."
6131   (funcall
6132    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6133    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6134
6135 (defun gnus-button-handle-apropos-documentation (url)
6136   "Call `apropos' when pushing the corresponding URL button."
6137   (funcall
6138    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6139    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6140
6141 (defun gnus-button-handle-library (url)
6142   "Call `locate-library' when pushing the corresponding URL button."
6143   (gnus-message 9 "url=`%s'" url)
6144   (let* ((lib (locate-library url))
6145          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6146     (if (not lib)
6147         (gnus-message 1 "Cannot locale library `%s'." url)
6148       (find-file-read-only file))))
6149
6150 (defun gnus-button-handle-ctan (url)
6151   "Call `browse-url' when pushing a CTAN URL button."
6152   (funcall
6153    gnus-button-ctan-handler
6154    (concat
6155     gnus-ctan-url
6156     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6157
6158 (defcustom gnus-button-tex-level 5
6159   "*Integer that says how many TeX-related buttons Gnus will show.
6160 The higher the number, the more buttons will appear and the more false
6161 positives are possible.  Note that you can set this variable local to
6162 specific groups.  Setting it higher in TeX groups is probably a good idea.
6163 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6164 how to set variables in specific groups."
6165   :version "22.1"
6166   :group 'gnus-article-buttons
6167   :link '(custom-manual "(gnus)Group Parameters")
6168   :type 'integer)
6169
6170 (defcustom gnus-button-man-level 5
6171   "*Integer that says how many man-related buttons Gnus will show.
6172 The higher the number, the more buttons will appear and the more false
6173 positives are possible.  Note that you can set this variable local to
6174 specific groups.  Setting it higher in Unix groups is probably a good idea.
6175 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6176 how to set variables in specific groups."
6177   :version "22.1"
6178   :group 'gnus-article-buttons
6179   :link '(custom-manual "(gnus)Group Parameters")
6180   :type 'integer)
6181
6182 (defcustom gnus-button-emacs-level 5
6183   "*Integer that says how many emacs-related buttons Gnus will show.
6184 The higher the number, the more buttons will appear and the more false
6185 positives are possible.  Note that you can set this variable local to
6186 specific groups.  Setting it higher in Emacs or Gnus related groups is
6187 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6188 `gnus-parameters' on how to set variables in specific groups."
6189   :version "22.1"
6190   :group 'gnus-article-buttons
6191   :link '(custom-manual "(gnus)Group Parameters")
6192   :type 'integer)
6193
6194 (defcustom gnus-button-message-level 5
6195   "*Integer that says how many buttons for news or mail messages will appear.
6196 The higher the number, the more buttons will appear and the more false
6197 positives are possible."
6198   ;; mail addresses, MIDs, URLs for news, ...
6199   :version "22.1"
6200   :group 'gnus-article-buttons
6201   :type 'integer)
6202
6203 (defcustom gnus-button-browse-level 5
6204   "*Integer that says how many buttons for browsing will appear.
6205 The higher the number, the more buttons will appear and the more false
6206 positives are possible."
6207   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6208   :version "22.1"
6209   :group 'gnus-article-buttons
6210   :type 'integer)
6211
6212 (defcustom gnus-button-alist
6213   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6214      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6215     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
6216      gnus-button-handle-news 2)
6217     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6218      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6219     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6220      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6221     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
6222     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
6223      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
6224     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6225      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6226     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6227      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6228     ;; RFC 2368 (The mailto URL scheme)
6229     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6230      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6231     ("\\bmailto:\\([^ \n\t]+\\)"
6232      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6233     ;; CTAN
6234     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6235              gnus-button-ctan-directory-regexp
6236              "[^][>)!;:,'\n\t ]+\\)")
6237      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6238     ((concat "\\btex-archive/\\("
6239              gnus-button-ctan-directory-regexp
6240              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6241      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6242     ((concat
6243       "\\b\\("
6244       gnus-button-ctan-directory-regexp
6245       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6246      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6247     ;; This is info (home-grown style) <info://foo/bar+baz>
6248     ("\\binfo://\\([^'\">\n\t ]+\\)"
6249      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
6250     ;; Info GNOME style <info:foo#bar_baz>
6251     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
6252      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
6253     ;; Info KDE style <info:(foo)bar baz>
6254     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
6255      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
6256     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6257      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6258     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6259      ;; Info links like `C-h i d m CC Mode RET'
6260      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6261     ;; This is custom
6262     ("\\bcustom:\\(//\\)?\\([^'\">\n\t ]+\\)"
6263      0 (>= gnus-button-emacs-level 5) gnus-button-handle-custom 2)
6264     ("M-x[ \t\n]customize-[^ ]+[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6265      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1)
6266     ;; Emacs help commands
6267     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6268      ;; regexp doesn't match arguments containing ` '.
6269      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6270     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6271      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6272     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6273      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6274     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6275      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6276     ;; The following entries may lead to many false positives so don't enable
6277     ;; them by default (use a high button level).
6278     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
6279      ;; Exclude [.?] for URLs in gmane.emacs.cvs
6280      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6281     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6282      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6283     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6284      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6285     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6286      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6287     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6288      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6289     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6290      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
6291     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6292      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6293     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6294      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6295     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6296      ;; Unlike the other regexps we really have to require quoting
6297      ;; here to determine where it ends.
6298      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6299     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
6300     ("<URL: *\\([^<>]*\\)>"
6301      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6302     ;; RFC 2396 (2.4.3., delims) ...
6303     ("\"URL: *\\([^\"]*\\)\""
6304      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6305     ;; RFC 2396 (2.4.3., delims) ...
6306     ("\"URL: *\\([^\"]*\\)\""
6307      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6308     ;; Raw URLs.
6309     (gnus-button-url-regexp
6310      0 (>= gnus-button-browse-level 0) browse-url 0)
6311     ;; man pages
6312     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
6313      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6314      gnus-button-handle-man 1)
6315     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6316     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
6317      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6318      gnus-button-handle-man 1)
6319     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6320     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6321     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
6322      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6323     ;; MID or mail: To avoid too many false positives we don't try to catch
6324     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6325     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6326     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6327     ;; so that non-ambiguous entries (see above) match first.
6328     (gnus-button-mid-or-mail-regexp
6329      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6330   "*Alist of regexps matching buttons in article bodies.
6331
6332 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6333 REGEXP: is the string (case insensitive) matching text around the button (can
6334 also be Lisp expression evaluating to a string),
6335 BUTTON: is the number of the regexp grouping actually matching the button,
6336 FORM: is a Lisp expression which must eval to true for the button to
6337 be added,
6338 CALLBACK: is the function to call when the user push this button, and each
6339 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6340
6341 CALLBACK can also be a variable, in that case the value of that
6342 variable it the real callback function."
6343   :group 'gnus-article-buttons
6344   :type '(repeat (list (choice regexp variable sexp)
6345                        (integer :tag "Button")
6346                        (sexp :tag "Form")
6347                        (function :tag "Callback")
6348                        (repeat :tag "Par"
6349                                :inline t
6350                                (integer :tag "Regexp group")))))
6351
6352 (defcustom gnus-header-button-alist
6353   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
6354      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6355     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6356      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6357     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6358      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
6359     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6360      0 (>= gnus-button-browse-level 0) browse-url 0)
6361     ("^Subject:" gnus-button-url-regexp
6362      0 (>= gnus-button-browse-level 0) browse-url 0)
6363     ("^[^:]+:" gnus-button-url-regexp
6364      0 (>= gnus-button-browse-level 0) browse-url 0)
6365     ("^OpenPGP:.*url=" gnus-button-url-regexp
6366      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
6367     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6368      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6369     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6370      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6371   "*Alist of headers and regexps to match buttons in article heads.
6372
6373 This alist is very similar to `gnus-button-alist', except that each
6374 alist has an additional HEADER element first in each entry:
6375
6376 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6377
6378 HEADER is a regexp to match a header.  For a fuller explanation, see
6379 `gnus-button-alist'."
6380   :group 'gnus-article-buttons
6381   :group 'gnus-article-headers
6382   :type '(repeat (list (regexp :tag "Header")
6383                        (choice regexp variable)
6384                        (integer :tag "Button")
6385                        (sexp :tag "Form")
6386                        (function :tag "Callback")
6387                        (repeat :tag "Par"
6388                                :inline t
6389                                (integer :tag "Regexp group")))))
6390
6391 ;;; Commands:
6392
6393 (defun gnus-article-push-button (event)
6394   "Check text under the mouse pointer for a callback function.
6395 If the text under the mouse pointer has a `gnus-callback' property,
6396 call it with the value of the `gnus-data' text property."
6397   (interactive "e")
6398   (set-buffer (window-buffer (posn-window (event-start event))))
6399   (let* ((pos (posn-point (event-start event)))
6400          (data (get-text-property pos 'gnus-data))
6401          (fun (get-text-property pos 'gnus-callback)))
6402     (goto-char pos)
6403     (when fun
6404       (funcall fun data))))
6405
6406 (defun gnus-article-press-button ()
6407   "Check text at point for a callback function.
6408 If the text at point has a `gnus-callback' property,
6409 call it with the value of the `gnus-data' text property."
6410   (interactive)
6411   (let ((data (get-text-property (point) 'gnus-data))
6412         (fun (get-text-property (point) 'gnus-callback)))
6413     (when fun
6414       (funcall fun data))))
6415
6416 (defun gnus-article-highlight (&optional force)
6417   "Highlight current article.
6418 This function calls `gnus-article-highlight-headers',
6419 `gnus-article-highlight-citation',
6420 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6421 do the highlighting.  See the documentation for those functions."
6422   (interactive (list 'force))
6423   (gnus-article-highlight-headers)
6424   (gnus-article-highlight-citation force)
6425   (gnus-article-highlight-signature)
6426   (gnus-article-add-buttons force)
6427   (gnus-article-add-buttons-to-head))
6428
6429 (defun gnus-article-highlight-some (&optional force)
6430   "Highlight current article.
6431 This function calls `gnus-article-highlight-headers',
6432 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6433 do the highlighting.  See the documentation for those functions."
6434   (interactive (list 'force))
6435   (gnus-article-highlight-headers)
6436   (gnus-article-highlight-signature)
6437   (gnus-article-add-buttons))
6438
6439 (defun gnus-article-highlight-headers ()
6440   "Highlight article headers as specified by `gnus-header-face-alist'."
6441   (interactive)
6442   (gnus-with-article-headers
6443     (let (regexp header-face field-face from hpoints fpoints)
6444       (dolist (entry gnus-header-face-alist)
6445         (goto-char (point-min))
6446         (setq regexp (concat "^\\("
6447                              (if (string-equal "" (nth 0 entry))
6448                                  "[^\t ]"
6449                                (nth 0 entry))
6450                              "\\)")
6451               header-face (nth 1 entry)
6452               field-face (nth 2 entry))
6453         (while (and (re-search-forward regexp nil t)
6454                     (not (eobp)))
6455           (beginning-of-line)
6456           (setq from (point))
6457           (unless (search-forward ":" nil t)
6458             (forward-char 1))
6459           (when (and header-face
6460                      (not (memq (point) hpoints)))
6461             (push (point) hpoints)
6462             (gnus-put-text-property from (point) 'face header-face))
6463           (when (and field-face
6464                      (not (memq (setq from (point)) fpoints)))
6465             (push from fpoints)
6466             (if (re-search-forward "^[^ \t]" nil t)
6467                 (forward-char -2)
6468               (goto-char (point-max)))
6469             (gnus-put-text-property from (point) 'face field-face)))))))
6470
6471 (defun gnus-article-highlight-signature ()
6472   "Highlight the signature in an article.
6473 It does this by highlighting everything after
6474 `gnus-signature-separator' using `gnus-signature-face'."
6475   (interactive)
6476   (gnus-with-article-buffer
6477     (let ((inhibit-point-motion-hooks t))
6478       (save-restriction
6479         (when (and gnus-signature-face
6480                    (gnus-article-narrow-to-signature))
6481           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6482                             'face gnus-signature-face)
6483           (widen)
6484           (gnus-article-search-signature)
6485           (let ((start (match-beginning 0))
6486                 (end (set-marker (make-marker) (1+ (match-end 0)))))
6487             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
6488                                      end)))))))
6489
6490 (defun gnus-button-in-region-p (b e prop)
6491   "Say whether PROP exists in the region."
6492   (text-property-not-all b e prop nil))
6493
6494 (defun gnus-article-add-buttons (&optional force)
6495   "Find external references in the article and make buttons of them.
6496 \"External references\" are things like Message-IDs and URLs, as
6497 specified by `gnus-button-alist'."
6498   (interactive (list 'force))
6499   (gnus-with-article-buffer
6500     (let ((inhibit-point-motion-hooks t)
6501           (case-fold-search t)
6502           (alist gnus-button-alist)
6503           beg entry regexp)
6504       ;; Remove all old markers.
6505       (let (marker entry new-list)
6506         (while (setq marker (pop gnus-button-marker-list))
6507           (if (or (< marker (point-min)) (>= marker (point-max)))
6508               (push marker new-list)
6509             (goto-char marker)
6510             (when (setq entry (gnus-button-entry))
6511               (put-text-property (match-beginning (nth 1 entry))
6512                                  (match-end (nth 1 entry))
6513                                  'gnus-callback nil))
6514             (set-marker marker nil)))
6515         (setq gnus-button-marker-list new-list))
6516       ;; We skip the headers.
6517       (article-goto-body)
6518       (setq beg (point))
6519       (while (setq entry (pop alist))
6520         (setq regexp (eval (car entry)))
6521         (goto-char beg)
6522         (while (re-search-forward regexp nil t)
6523           (let* ((start (and entry (match-beginning (nth 1 entry))))
6524                  (end (and entry (match-end (nth 1 entry))))
6525                  (from (match-beginning 0)))
6526             (when (and (or (eq t (nth 2 entry))
6527                            (eval (nth 2 entry)))
6528                        (not (gnus-button-in-region-p
6529                              start end 'gnus-callback)))
6530               ;; That optional form returned non-nil, so we add the
6531               ;; button.
6532               (gnus-article-add-button
6533                start end 'gnus-button-push
6534                (car (push (set-marker (make-marker) from)
6535                           gnus-button-marker-list))))))))))
6536
6537 ;; Add buttons to the head of an article.
6538 (defun gnus-article-add-buttons-to-head ()
6539   "Add buttons to the head of the article."
6540   (interactive)
6541   (gnus-with-article-headers
6542     (let (beg end)
6543       (dolist (entry gnus-header-button-alist)
6544         ;; Each alist entry.
6545         (goto-char (point-min))
6546         (while (re-search-forward (car entry) nil t)
6547           ;; Each header matching the entry.
6548           (setq beg (match-beginning 0))
6549           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6550                              (match-beginning 0))
6551                         (point-max)))
6552           (goto-char beg)
6553           (while (re-search-forward (eval (nth 1 entry)) end t)
6554             ;; Each match within a header.
6555             (let* ((entry (cdr entry))
6556                    (start (match-beginning (nth 1 entry)))
6557                    (end (match-end (nth 1 entry)))
6558                    (form (nth 2 entry)))
6559               (goto-char (match-end 0))
6560               (when (eval form)
6561                 (gnus-article-add-button
6562                  start end (nth 3 entry)
6563                  (buffer-substring (match-beginning (nth 4 entry))
6564                                    (match-end (nth 4 entry)))))))
6565           (goto-char end))))))
6566
6567 ;;; External functions:
6568
6569 (defun gnus-article-add-button (from to fun &optional data)
6570   "Create a button between FROM and TO with callback FUN and data DATA."
6571   (when gnus-article-button-face
6572     (gnus-overlay-put (gnus-make-overlay from to)
6573                       'face gnus-article-button-face))
6574   (gnus-add-text-properties
6575    from to
6576    (nconc (and gnus-article-mouse-face
6577                (list gnus-mouse-face-prop gnus-article-mouse-face))
6578           (list 'gnus-callback fun)
6579           (and data (list 'gnus-data data))))
6580   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6581                          :button-keymap gnus-widget-button-keymap))
6582
6583 ;;; Internal functions:
6584
6585 (defun gnus-article-set-globals ()
6586   (with-current-buffer gnus-summary-buffer
6587     (gnus-set-global-variables)))
6588
6589 (defun gnus-signature-toggle (end)
6590   (gnus-with-article-buffer
6591     (let ((inhibit-point-motion-hooks t))
6592       (if (text-property-any end (point-max) 'article-type 'signature)
6593           (progn
6594             (gnus-delete-wash-type 'signature)
6595             (gnus-remove-text-properties-when
6596              'article-type 'signature end (point-max)
6597              (cons 'article-type (cons 'signature
6598                                        gnus-hidden-properties))))
6599         (gnus-add-wash-type 'signature)
6600         (gnus-add-text-properties-when
6601          'article-type nil end (point-max)
6602          (cons 'article-type (cons 'signature
6603                                    gnus-hidden-properties)))))
6604     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6605       (gnus-set-mode-line 'article))))
6606
6607 (defun gnus-button-entry ()
6608   ;; Return the first entry in `gnus-button-alist' matching this place.
6609   (let ((alist gnus-button-alist)
6610         (entry nil))
6611     (while alist
6612       (setq entry (pop alist))
6613       (if (looking-at (eval (car entry)))
6614           (setq alist nil)
6615         (setq entry nil)))
6616     entry))
6617
6618 (defun gnus-button-push (marker)
6619   ;; Push button starting at MARKER.
6620   (save-excursion
6621     (goto-char marker)
6622     (let* ((entry (gnus-button-entry))
6623            (inhibit-point-motion-hooks t)
6624            (fun (nth 3 entry))
6625            (args (mapcar (lambda (group)
6626                            (let ((string (match-string group)))
6627                              (set-text-properties
6628                               0 (length string) nil string)
6629                              string))
6630                          (nthcdr 4 entry))))
6631       (cond
6632        ((fboundp fun)
6633         (apply fun args))
6634        ((and (boundp fun)
6635              (fboundp (symbol-value fun)))
6636         (apply (symbol-value fun) args))
6637        (t
6638         (gnus-message 1 "You must define `%S' to use this button"
6639                       (cons fun args)))))))
6640
6641 (defun gnus-parse-news-url (url)
6642   (let (scheme server port group message-id articles)
6643     (with-temp-buffer
6644       (insert url)
6645       (goto-char (point-min))
6646       (when (looking-at "\\([A-Za-z]+\\):")
6647         (setq scheme (match-string 1))
6648         (goto-char (match-end 0)))
6649       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
6650         (setq server (match-string 1))
6651         (setq port (if (stringp (match-string 3))
6652                        (string-to-number (match-string 3))
6653                      (match-string 3)))
6654         (goto-char (match-end 0)))
6655
6656       (cond
6657        ((looking-at "\\(.*@.*\\)")
6658         (setq message-id (match-string 1)))
6659        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6660         (setq group (match-string 1)
6661               articles (split-string (match-string 2) "-")))
6662        ((looking-at "\\([^/]+\\)/?")
6663         (setq group (match-string 1)))
6664        (t
6665         (error "Unknown news URL syntax"))))
6666     (list scheme server port group message-id articles)))
6667
6668 (defun gnus-button-handle-news (url)
6669   "Fetch a news URL."
6670   (destructuring-bind (scheme server port group message-id articles)
6671       (gnus-parse-news-url url)
6672     (cond
6673      (message-id
6674       (save-excursion
6675         (set-buffer gnus-summary-buffer)
6676         (if server
6677             (let ((gnus-refer-article-method
6678                    (nconc (list (list 'nntp server))
6679                           gnus-refer-article-method))
6680                   (nntp-port-number (or port "nntp")))
6681               (gnus-message 7 "Fetching %s with %s"
6682                             message-id gnus-refer-article-method)
6683               (gnus-summary-refer-article message-id))
6684           (gnus-summary-refer-article message-id))))
6685      (group
6686       (gnus-button-fetch-group url)))))
6687
6688 (defun gnus-button-handle-man (url)
6689   "Fetch a man page."
6690   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
6691   (when (eq gnus-button-man-handler 'woman)
6692     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
6693   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
6694   (funcall gnus-button-man-handler url))
6695
6696 (defun gnus-button-handle-info-url (url)
6697   "Fetch an info URL."
6698   (setq url (mm-subst-char-in-string ?+ ?\  url))
6699   (cond
6700    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
6701     (gnus-info-find-node
6702      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
6703                      "Gnus")
6704              ")" (gnus-url-unhex-string (match-string 2 url)))))
6705    ((string-match "([^)\"]+)[^\"]+" url)
6706     (setq url
6707           (gnus-replace-in-string
6708            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
6709     (gnus-info-find-node url))
6710    (t (error "Can't parse %s" url))))
6711
6712 (defun gnus-button-handle-info-url-gnome (url)
6713   "Fetch GNOME style info URL."
6714   (setq url (mm-subst-char-in-string ?_ ?\  url))
6715   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
6716       (gnus-info-find-node
6717        (concat "("
6718                (gnus-url-unhex-string
6719                  (match-string 1 url))
6720                ")"
6721                (or (gnus-url-unhex-string
6722                     (match-string 2 url))
6723                    "Top")))
6724     (error "Can't parse %s" url)))
6725
6726 (defun gnus-button-handle-info-url-kde (url)
6727   "Fetch KDE style info URL."
6728   (gnus-info-find-node (gnus-url-unhex-string url)))
6729
6730 (defun gnus-button-handle-info-keystrokes (url)
6731   "Call `info' when pushing the corresponding URL button."
6732   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
6733   (info)
6734   (Info-directory)
6735   (Info-menu url))
6736
6737 (defun gnus-button-openpgp (url)
6738   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
6739   (with-temp-buffer
6740     (mm-url-insert-file-contents-external url)
6741     (pgg-snarf-keys-region (point-min) (point-max))
6742     (pgg-display-output-buffer nil nil nil)))
6743
6744 (defun gnus-button-message-id (message-id)
6745   "Fetch MESSAGE-ID."
6746   (with-current-buffer gnus-summary-buffer
6747     (gnus-summary-refer-article message-id)))
6748
6749 (defun gnus-button-fetch-group (address)
6750   "Fetch GROUP specified by ADDRESS."
6751   (if (not (string-match "[:/]" address))
6752       ;; This is just a simple group url.
6753       (gnus-group-read-ephemeral-group address gnus-select-method)
6754     (if (not
6755          (string-match
6756           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
6757           address))
6758         (error "Can't parse %s" address)
6759       (gnus-group-read-ephemeral-group
6760        (match-string 4 address)
6761        `(nntp ,(match-string 1 address)
6762               (nntp-address ,(match-string 1 address))
6763               (nntp-port-number ,(if (match-end 3)
6764                                      (match-string 3 address)
6765                                    "nntp")))
6766        nil nil nil
6767        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
6768
6769 (defun gnus-url-parse-query-string (query &optional downcase)
6770   (let (retval pairs cur key val)
6771     (setq pairs (split-string query "&"))
6772     (while pairs
6773       (setq cur (car pairs)
6774             pairs (cdr pairs))
6775       (if (not (string-match "=" cur))
6776           nil                           ; Grace
6777         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
6778               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
6779         (if downcase
6780             (setq key (downcase key)))
6781         (setq cur (assoc key retval))
6782         (if cur
6783             (setcdr cur (cons val (cdr cur)))
6784           (setq retval (cons (list key val) retval)))))
6785     retval))
6786
6787 (defun gnus-url-mailto (url)
6788   ;; Send mail to someone
6789   (when (string-match "mailto:/*\\(.*\\)" url)
6790     (setq url (substring url (match-beginning 1) nil)))
6791   (let (to args subject func)
6792     (setq args (gnus-url-parse-query-string
6793                 (if (string-match "^\\?" url)
6794                     (substring url 1)
6795                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
6796                       (concat "to=" (match-string 1 url) "&"
6797                               (match-string 2 url))
6798                     (concat "to=" url)))
6799                 t)
6800           subject (cdr-safe (assoc "subject" args)))
6801     (gnus-msg-mail)
6802     (while args
6803       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
6804       (if (fboundp func)
6805           (funcall func)
6806         (message-position-on-field (caar args)))
6807       (insert (gnus-replace-in-string
6808                (mapconcat 'identity (reverse (cdar args)) ", ")
6809                "\r\n" "\n" t))
6810       (setq args (cdr args)))
6811     (if subject
6812         (message-goto-body)
6813       (message-goto-subject))))
6814
6815 (defun gnus-button-embedded-url (address)
6816   "Activate ADDRESS with `browse-url'."
6817   (browse-url (gnus-strip-whitespace address)))
6818
6819 ;;; Next/prev buttons in the article buffer.
6820
6821 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
6822 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
6823
6824 (defvar gnus-prev-page-map
6825   (let ((map (make-sparse-keymap)))
6826     (define-key map gnus-mouse-2 'gnus-button-prev-page)
6827     (define-key map "\r" 'gnus-button-prev-page)
6828     map))
6829
6830 (defvar gnus-next-page-map
6831   (let ((map (make-sparse-keymap)))
6832     (unless (>= emacs-major-version 21)
6833       ;; XEmacs doesn't care.
6834       (set-keymap-parent map gnus-article-mode-map))
6835     (define-key map gnus-mouse-2 'gnus-button-next-page)
6836     (define-key map "\r" 'gnus-button-next-page)
6837     map))
6838
6839 (defun gnus-insert-prev-page-button ()
6840   (let ((b (point))
6841         (inhibit-read-only t))
6842     (gnus-eval-format
6843      gnus-prev-page-line-format nil
6844      `(keymap ,gnus-prev-page-map
6845          gnus-prev t
6846          gnus-callback gnus-article-button-prev-page
6847          article-type annotation))
6848     (widget-convert-button
6849      'link b (if (bolp)
6850                  ;; Exclude a newline.
6851                  (1- (point))
6852                (point))
6853      :action 'gnus-button-prev-page
6854      :button-keymap gnus-prev-page-map)))
6855
6856 (defun gnus-button-next-page (&optional args more-args)
6857   "Go to the next page."
6858   (interactive)
6859   (let ((win (selected-window)))
6860     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6861     (gnus-article-next-page)
6862     (select-window win)))
6863
6864 (defun gnus-button-prev-page (&optional args more-args)
6865   "Go to the prev page."
6866   (interactive)
6867   (let ((win (selected-window)))
6868     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6869     (gnus-article-prev-page)
6870     (select-window win)))
6871
6872 (defun gnus-insert-next-page-button ()
6873   (let ((b (point))
6874         (inhibit-read-only t))
6875     (gnus-eval-format gnus-next-page-line-format nil
6876                       `(keymap ,gnus-next-page-map
6877                           gnus-next t
6878                           gnus-callback gnus-article-button-next-page
6879                           article-type annotation))
6880     (widget-convert-button
6881      'link b (if (bolp)
6882                  ;; Exclude a newline.
6883                  (1- (point))
6884                (point))
6885      :action 'gnus-button-next-page
6886      :button-keymap gnus-next-page-map)))
6887
6888 (defun gnus-article-button-next-page (arg)
6889   "Go to the next page."
6890   (interactive "P")
6891   (let ((win (selected-window)))
6892     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6893     (gnus-article-next-page)
6894     (select-window win)))
6895
6896 (defun gnus-article-button-prev-page (arg)
6897   "Go to the prev page."
6898   (interactive "P")
6899   (let ((win (selected-window)))
6900     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6901     (gnus-article-prev-page)
6902     (select-window win)))
6903
6904 (defvar gnus-decode-header-methods
6905   '(mail-decode-encoded-word-region)
6906   "List of methods used to decode headers.
6907
6908 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
6909 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
6910 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
6911 whose names match REGEXP.
6912
6913 For example:
6914 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
6915  mail-decode-encoded-word-region
6916  (\"chinese\" . rfc1843-decode-region))
6917 ")
6918
6919 (defvar gnus-decode-header-methods-cache nil)
6920
6921 (defun gnus-multi-decode-header (start end)
6922   "Apply the functions from `gnus-encoded-word-methods' that match."
6923   (unless (and gnus-decode-header-methods-cache
6924                (eq gnus-newsgroup-name
6925                    (car gnus-decode-header-methods-cache)))
6926     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
6927     (mapcar (lambda (x)
6928               (if (symbolp x)
6929                   (nconc gnus-decode-header-methods-cache (list x))
6930                 (if (and gnus-newsgroup-name
6931                          (string-match (car x) gnus-newsgroup-name))
6932                     (nconc gnus-decode-header-methods-cache
6933                            (list (cdr x))))))
6934           gnus-decode-header-methods))
6935   (let ((xlist gnus-decode-header-methods-cache))
6936     (pop xlist)
6937     (save-restriction
6938       (narrow-to-region start end)
6939       (while xlist
6940         (funcall (pop xlist) (point-min) (point-max))))))
6941
6942 ;;;
6943 ;;; Treatment top-level handling.
6944 ;;;
6945
6946 (defun gnus-treat-article (condition &optional part-number total-parts type)
6947   (let ((length (- (point-max) (point-min)))
6948         (alist gnus-treatment-function-alist)
6949         (article-goto-body-goes-to-point-min-p t)
6950         (treated-type
6951          (or (not type)
6952              (catch 'found
6953                (let ((list gnus-article-treat-types))
6954                  (while list
6955                    (when (string-match (pop list) type)
6956                      (throw 'found t)))))))
6957         (highlightp (gnus-visual-p 'article-highlight 'highlight))
6958         val elem)
6959     (gnus-run-hooks 'gnus-part-display-hook)
6960     (dolist (elem alist)
6961       (setq val
6962             (save-excursion
6963               (when (gnus-buffer-live-p gnus-summary-buffer)
6964                 (set-buffer gnus-summary-buffer))
6965               (symbol-value (car elem))))
6966       (when (and (or (consp val)
6967                      treated-type)
6968                  (gnus-treat-predicate val)
6969                  (or (not (get (car elem) 'highlight))
6970                      highlightp))
6971         (save-restriction
6972           (funcall (cadr elem)))))))
6973
6974 ;; Dynamic variables.
6975 (eval-when-compile
6976   (defvar part-number)
6977   (defvar total-parts)
6978   (defvar type)
6979   (defvar condition)
6980   (defvar length))
6981
6982 (defun gnus-treat-predicate (val)
6983   (cond
6984    ((null val)
6985     nil)
6986    (condition
6987     (eq condition val))
6988    ((and (listp val)
6989          (stringp (car val)))
6990     (apply 'gnus-or (mapcar `(lambda (s)
6991                                (string-match s ,(or gnus-newsgroup-name "")))
6992                             val)))
6993    ((listp val)
6994     (let ((pred (pop val)))
6995       (cond
6996        ((eq pred 'or)
6997         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
6998        ((eq pred 'and)
6999         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7000        ((eq pred 'not)
7001         (not (gnus-treat-predicate (car val))))
7002        ((eq pred 'typep)
7003         (equal (car val) type))
7004        (t
7005         (error "%S is not a valid predicate" pred)))))
7006    ((eq val t)
7007     t)
7008    ((eq val 'head)
7009     nil)
7010    ((eq val 'last)
7011     (eq part-number total-parts))
7012    ((numberp val)
7013     (< length val))
7014    (t
7015     (error "%S is not a valid value" val))))
7016
7017 (defun gnus-article-encrypt-body (protocol &optional n)
7018   "Encrypt the article body."
7019   (interactive
7020    (list
7021     (or gnus-article-encrypt-protocol
7022         (completing-read "Encrypt protocol: "
7023                          gnus-article-encrypt-protocol-alist
7024                          nil t))
7025     current-prefix-arg))
7026   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7027     (unless func
7028       (error (format "Can't find the encrypt protocol %s" protocol)))
7029     (if (member gnus-newsgroup-name '("nndraft:delayed"
7030                                       "nndraft:drafts"
7031                                       "nndraft:queue"))
7032         (error "Can't encrypt the article in group %s"
7033                gnus-newsgroup-name))
7034     (gnus-summary-iterate n
7035       (save-excursion
7036         (set-buffer gnus-summary-buffer)
7037         (let ((mail-parse-charset gnus-newsgroup-charset)
7038               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7039               (summary-buffer gnus-summary-buffer)
7040               references point)
7041           (gnus-set-global-variables)
7042           (when (gnus-group-read-only-p)
7043             (error "The current newsgroup does not support article encrypt"))
7044           (gnus-summary-show-article t)
7045           (setq references
7046               (or (mail-header-references gnus-current-headers) ""))
7047           (set-buffer gnus-article-buffer)
7048           (let* ((inhibit-read-only t)
7049                  (headers
7050                   (mapcar (lambda (field)
7051                             (and (save-restriction
7052                                    (message-narrow-to-head)
7053                                    (goto-char (point-min))
7054                                    (search-forward field nil t))
7055                                  (prog2
7056                                      (message-narrow-to-field)
7057                                      (buffer-string)
7058                                    (delete-region (point-min) (point-max))
7059                                    (widen))))
7060                           '("Content-Type:" "Content-Transfer-Encoding:"
7061                             "Content-Disposition:"))))
7062             (message-narrow-to-head)
7063             (message-remove-header "MIME-Version")
7064             (goto-char (point-max))
7065             (setq point (point))
7066             (insert (apply 'concat headers))
7067             (widen)
7068             (narrow-to-region point (point-max))
7069             (let ((message-options message-options))
7070               (message-options-set 'message-sender user-mail-address)
7071               (message-options-set 'message-recipients user-mail-address)
7072               (message-options-set 'message-sign-encrypt 'not)
7073               (funcall func))
7074             (goto-char (point-min))
7075             (insert "MIME-Version: 1.0\n")
7076             (widen)
7077             (gnus-summary-edit-article-done
7078              references nil summary-buffer t))
7079           (when gnus-keep-backlog
7080             (gnus-backlog-remove-article
7081              (car gnus-article-current) (cdr gnus-article-current)))
7082           (save-excursion
7083             (when (get-buffer gnus-original-article-buffer)
7084               (set-buffer gnus-original-article-buffer)
7085               (setq gnus-original-article nil)))
7086           (when gnus-use-cache
7087             (gnus-cache-update-article
7088              (car gnus-article-current) (cdr gnus-article-current))))))))
7089
7090 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7091   "The following specs can be used:
7092 %t  The security MIME type
7093 %i  Additional info
7094 %d  Details
7095 %D  Details if button is pressed")
7096
7097 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7098   "The following specs can be used:
7099 %t  The security MIME type
7100 %i  Additional info
7101 %d  Details
7102 %D  Details if button is pressed")
7103
7104 (defvar gnus-mime-security-button-line-format-alist
7105   '((?t gnus-tmp-type ?s)
7106     (?i gnus-tmp-info ?s)
7107     (?d gnus-tmp-details ?s)
7108     (?D gnus-tmp-pressed-details ?s)))
7109
7110 (defvar gnus-mime-security-button-map
7111   (let ((map (make-sparse-keymap)))
7112     (define-key map gnus-mouse-2 'gnus-article-push-button)
7113     (define-key map "\r" 'gnus-article-press-button)
7114     map))
7115
7116 (defvar gnus-mime-security-details-buffer nil)
7117
7118 (defvar gnus-mime-security-button-pressed nil)
7119
7120 (defvar gnus-mime-security-show-details-inline t
7121   "If non-nil, show details in the article buffer.")
7122
7123 (defun gnus-mime-security-verify-or-decrypt (handle)
7124   (mm-remove-parts (cdr handle))
7125   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7126         point (inhibit-read-only t))
7127     (if region
7128         (goto-char (car region)))
7129     (save-restriction
7130       (narrow-to-region (point) (point))
7131       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7132         (let* ((mm-verify-option 'known)
7133                (mm-decrypt-option 'known)
7134                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7135           (unless (eq nparts (cdr handle))
7136             (mm-destroy-parts (cdr handle))
7137             (setcdr handle nparts))))
7138       (setq point (point))
7139       (gnus-mime-display-security handle)
7140       (goto-char (point-max)))
7141     (when region
7142       (delete-region (point) (cdr region))
7143       (set-marker (car region) nil)
7144       (set-marker (cdr region) nil))
7145     (goto-char point)))
7146
7147 (defun gnus-mime-security-show-details (handle)
7148   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7149     (if (not details)
7150         (gnus-message 5 "No details.")
7151       (if gnus-mime-security-show-details-inline
7152           (let ((gnus-mime-security-button-pressed
7153                  (not (get-text-property (point) 'gnus-mime-details)))
7154                 (gnus-mime-security-button-line-format
7155                  (get-text-property (point) 'gnus-line-format))
7156                 (inhibit-read-only t))
7157             (forward-char -1)
7158             (while (eq (get-text-property (point) 'gnus-line-format)
7159                        gnus-mime-security-button-line-format)
7160               (forward-char -1))
7161             (forward-char)
7162             (save-restriction
7163               (narrow-to-region (point) (point))
7164               (gnus-insert-mime-security-button handle))
7165             (delete-region (point)
7166                            (or (text-property-not-all
7167                                 (point) (point-max)
7168                                 'gnus-line-format
7169                                 gnus-mime-security-button-line-format)
7170                                (point-max))))
7171         ;; Not inlined.
7172         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7173             (with-current-buffer gnus-mime-security-details-buffer
7174               (erase-buffer)
7175               t)
7176           (setq gnus-mime-security-details-buffer
7177                 (gnus-get-buffer-create "*MIME Security Details*")))
7178         (with-current-buffer gnus-mime-security-details-buffer
7179           (insert details)
7180           (goto-char (point-min)))
7181         (pop-to-buffer gnus-mime-security-details-buffer)))))
7182
7183 (defun gnus-mime-security-press-button (handle)
7184   (save-excursion
7185     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7186         (gnus-mime-security-show-details handle)
7187       (gnus-mime-security-verify-or-decrypt handle))))
7188
7189 (defun gnus-insert-mime-security-button (handle &optional displayed)
7190   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7191          (gnus-tmp-type
7192           (concat
7193            (or (nth 2 (assoc protocol mm-verify-function-alist))
7194                (nth 2 (assoc protocol mm-decrypt-function-alist))
7195                "Unknown")
7196            (if (equal (car handle) "multipart/signed")
7197                " Signed" " Encrypted")
7198            " Part"))
7199          (gnus-tmp-info
7200           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7201               "Undecided"))
7202          (gnus-tmp-details
7203           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7204          gnus-tmp-pressed-details
7205          b e)
7206     (setq gnus-tmp-details
7207           (if gnus-tmp-details
7208               (concat "\n" gnus-tmp-details)
7209             ""))
7210     (setq gnus-tmp-pressed-details
7211           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7212     (unless (bolp)
7213       (insert "\n"))
7214     (setq b (point))
7215     (gnus-eval-format
7216      gnus-mime-security-button-line-format
7217      gnus-mime-security-button-line-format-alist
7218      `(keymap ,gnus-mime-security-button-map
7219          gnus-callback gnus-mime-security-press-button
7220          gnus-line-format ,gnus-mime-security-button-line-format
7221          gnus-mime-details ,gnus-mime-security-button-pressed
7222          article-type annotation
7223          gnus-data ,handle))
7224     (setq e (if (bolp)
7225                 ;; Exclude a newline.
7226                 (1- (point))
7227               (point)))
7228     (widget-convert-button
7229      'link b e
7230      :mime-handle handle
7231      :action 'gnus-widget-press-button
7232      :button-keymap gnus-mime-security-button-map
7233      :help-echo
7234      (lambda (widget/window &optional overlay pos)
7235        ;; Needed to properly clear the message due to a bug in
7236        ;; wid-edit (XEmacs only).
7237        (when (boundp 'help-echo-owns-message)
7238          (setq help-echo-owns-message t))
7239        (format
7240         "%S: show detail"
7241         (aref gnus-mouse-2 0))))))
7242
7243 (defun gnus-mime-display-security (handle)
7244   (save-restriction
7245     (narrow-to-region (point) (point))
7246     (unless (gnus-unbuttonized-mime-type-p (car handle))
7247       (gnus-insert-mime-security-button handle))
7248     (gnus-mime-display-mixed (cdr handle))
7249     (unless (bolp)
7250       (insert "\n"))
7251     (unless (gnus-unbuttonized-mime-type-p (car handle))
7252       (let ((gnus-mime-security-button-line-format
7253              gnus-mime-security-button-end-line-format))
7254         (gnus-insert-mime-security-button handle)))
7255     (mm-set-handle-multipart-parameter
7256      handle 'gnus-region
7257      (cons (set-marker (make-marker) (point-min))
7258            (set-marker (make-marker) (point-max))))))
7259
7260 (gnus-ems-redefine)
7261
7262 (provide 'gnus-art)
7263
7264 (run-hooks 'gnus-art-load-hook)
7265
7266 ;;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
7267 ;;; gnus-art.el ends here