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