Merge from emacs--devo--0
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
38 (eval-and-compile
39   (let ((recursive-load-depth-limit 100))
40     (require 'gnus-sum)))
41 (require 'gnus-spec)
42 (require 'gnus-int)
43 (require 'gnus-win)
44 (require 'mm-bodies)
45 (require 'mail-parse)
46 (require 'mm-decode)
47 (require 'mm-view)
48 (require 'wid-edit)
49 (require 'mm-uu)
50 (require 'message)
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
55 (autoload 'parse-time-string "parse-time" nil nil)
56 (autoload 'ansi-color-apply-on-region "ansi-color")
57 (autoload 'mm-url-insert-file-contents-external "mm-url")
58 (autoload 'mm-extern-cache-contents "mm-extern")
59
60 (defgroup gnus-article nil
61   "Article display."
62   :link '(custom-manual "(gnus)Article Buffer")
63   :group 'gnus)
64
65 (defgroup gnus-article-treat nil
66   "Treating article parts."
67   :link '(custom-manual "(gnus)Article Hiding")
68   :group 'gnus-article)
69
70 (defgroup gnus-article-hiding nil
71   "Hiding article parts."
72   :link '(custom-manual "(gnus)Article Hiding")
73   :group 'gnus-article)
74
75 (defgroup gnus-article-highlight nil
76   "Article highlighting."
77   :link '(custom-manual "(gnus)Article Highlighting")
78   :group 'gnus-article
79   :group 'gnus-visual)
80
81 (defgroup gnus-article-signature nil
82   "Article signatures."
83   :link '(custom-manual "(gnus)Article Signature")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-headers nil
87   "Article headers."
88   :link '(custom-manual "(gnus)Hiding Headers")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-washing nil
92   "Special commands on articles."
93   :link '(custom-manual "(gnus)Article Washing")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-emphasis nil
97   "Fontisizing articles."
98   :link '(custom-manual "(gnus)Article Fontisizing")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-saving nil
102   "Saving articles."
103   :link '(custom-manual "(gnus)Saving Articles")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-mime nil
107   "Worshiping the MIME wonder."
108   :link '(custom-manual "(gnus)Using MIME")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-buttons nil
112   "Pushable buttons in the article buffer."
113   :link '(custom-manual "(gnus)Article Buttons")
114   :group 'gnus-article)
115
116 (defgroup gnus-article-various nil
117   "Other article options."
118   :link '(custom-manual "(gnus)Misc Article")
119   :group 'gnus-article)
120
121 (defcustom gnus-ignored-headers
122   (mapcar
123    (lambda (header)
124      (concat "^" header ":"))
125    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
126      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
127      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
128      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
129      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
130      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
131      "X-Attribution" "X-Originating-IP" "Delivered-To"
132      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
133      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
134      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
135      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
136      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
137      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
138      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
139      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
140      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
141      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
142      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
143      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
144      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
145      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
146      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
147      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
148      "List-[A-Za-z]+" "X-Listprocessor-Version"
149      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
150      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
151      "X-Received" "Content-length" "X-precedence"
152      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
153      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
154      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
155      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
156      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
157      "X-Content-length" "X-Posting-Agent" "Original-Received"
158      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
159      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
160      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
161      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
162      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
163      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
164      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
165      "Envelope-Sender" "Envelope-Recipients"))
166   "*All headers that start with this regexp will be hidden.
167 This variable can also be a list of regexps of headers to be ignored.
168 If `gnus-visible-headers' is non-nil, this variable will be ignored."
169   :type '(choice :custom-show nil
170                  regexp
171                  (repeat regexp))
172   :group 'gnus-article-hiding)
173
174 (defcustom gnus-visible-headers
175   "^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:"
176   "*All headers that do not match this regexp will be hidden.
177 This variable can also be a list of regexp of headers to remain visible.
178 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
179   :type '(choice
180           (repeat :value-to-internal (lambda (widget value)
181                                        (custom-split-regexp-maybe value))
182                   :match (lambda (widget value)
183                            (or (stringp value)
184                                (widget-editable-list-match widget value)))
185                   regexp)
186           (const :tag "Use gnus-ignored-headers" nil)
187           regexp)
188   :group 'gnus-article-hiding)
189
190 (defcustom gnus-sorted-header-list
191   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
192     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
193   "*This variable is a list of regular expressions.
194 If it is non-nil, headers that match the regular expressions will
195 be placed first in the article buffer in the sequence specified by
196 this list."
197   :type '(repeat regexp)
198   :group 'gnus-article-hiding)
199
200 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
201   "Headers that are only to be displayed if they have interesting data.
202 Possible values in this list are:
203
204   'empty       Headers with no content.
205   'newsgroups  Newsgroup identical to Gnus group.
206   'to-address  To identical to To-address.
207   'to-list     To identical to To-list.
208   'cc-list     CC identical to To-list.
209   'followup-to Followup-to identical to Newsgroups.
210   'reply-to    Reply-to identical to From.
211   'date        Date less than four days old.
212   'long-to     To and/or Cc longer than 1024 characters.
213   'many-to     Multiple To and/or Cc."
214   :type '(set (const :tag "Headers with no content." empty)
215               (const :tag "Newsgroups identical to Gnus group." newsgroups)
216               (const :tag "To identical to To-address." to-address)
217               (const :tag "To identical to To-list." to-list)
218               (const :tag "CC identical to To-list." cc-list)
219               (const :tag "Followup-to identical to Newsgroups." followup-to)
220               (const :tag "Reply-to identical to From." reply-to)
221               (const :tag "Date less than four days old." date)
222               (const :tag "To and/or Cc longer than 1024 characters." long-to)
223               (const :tag "Multiple To and/or Cc headers." many-to))
224   :group 'gnus-article-hiding)
225
226 (defcustom gnus-article-skip-boring nil
227   "Skip over text that is not worth reading.
228 By default, if you set this t, then Gnus will display citations and
229 signatures, but will never scroll down to show you a page consisting
230 only of boring text.  Boring text is controlled by
231 `gnus-article-boring-faces'."
232   :version "22.1"
233   :type 'boolean
234   :group 'gnus-article-hiding)
235
236 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
237   "Regexp matching signature separator.
238 This can also be a list of regexps.  In that case, it will be checked
239 from head to tail looking for a separator.  Searches will be done from
240 the end of the buffer."
241   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
242                  (regexp)
243                  (repeat :tag "List of regexp" regexp))
244   :group 'gnus-article-signature)
245
246 (defcustom gnus-signature-limit nil
247   "Provide a limit to what is considered a signature.
248 If it is a number, no signature may not be longer (in characters) than
249 that number.  If it is a floating point number, no signature may be
250 longer (in lines) than that number.  If it is a function, the function
251 will be called without any parameters, and if it returns nil, there is
252 no signature in the buffer.  If it is a string, it will be used as a
253 regexp.  If it matches, the text in question is not a signature.
254
255 This can also be a list of the above values."
256   :type '(choice (const nil)
257                  (integer :value 200)
258                  (number :value 4.0)
259                  function
260                  (regexp :value ".*"))
261   :group 'gnus-article-signature)
262
263 (defcustom gnus-hidden-properties '(invisible t intangible t)
264   "Property list to use for hiding text."
265   :type 'sexp
266   :group 'gnus-article-hiding)
267
268 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
269 ;; frames in a session.
270 (defcustom gnus-article-x-face-command
271   (if (featurep 'xemacs)
272       (if (or (gnus-image-type-available-p 'xface)
273               (gnus-image-type-available-p 'pbm))
274           'gnus-display-x-face-in-from
275         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
276     (if (gnus-image-type-available-p 'pbm)
277         'gnus-display-x-face-in-from
278       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
279 display -"))
280   "*String or function to be executed to display an X-Face header.
281 If it is a string, the command will be executed in a sub-shell
282 asynchronously.  The compressed face will be piped to this command."
283   :type `(choice string
284                  (function-item gnus-display-x-face-in-from)
285                  function)
286   :version "21.1"
287   :group 'gnus-picon
288   :group 'gnus-article-washing)
289
290 (defcustom gnus-article-x-face-too-ugly nil
291   "Regexp matching posters whose face shouldn't be shown automatically."
292   :type '(choice regexp (const nil))
293   :group 'gnus-article-washing)
294
295 (defcustom gnus-article-banner-alist nil
296   "Banner alist for stripping.
297 For example,
298      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
299   :version "21.1"
300   :type '(repeat (cons symbol regexp))
301   :group 'gnus-article-washing)
302
303 (gnus-define-group-parameter
304  banner
305  :variable-document
306  "Alist of regexps (to match group names) and banner."
307  :variable-group gnus-article-washing
308  :parameter-type
309  '(choice :tag "Banner"
310           :value nil
311           (const :tag "Remove signature" signature)
312           (symbol :tag "Item in `gnus-article-banner-alist'" none)
313           regexp
314           (const :tag "None" nil))
315  :parameter-document
316  "If non-nil, specify how to remove `banners' from articles.
317
318 Symbol `signature' means to remove signatures delimited by
319 `gnus-signature-separator'.  Any other symbol is used to look up a
320 regular expression to match the banner in `gnus-article-banner-alist'.
321 A string is used as a regular expression to match the banner
322 directly.")
323
324 (defcustom gnus-article-address-banner-alist nil
325   "Alist of mail addresses and banners.
326 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
327 to match a mail address in the From: header, BANNER is one of a symbol
328 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
329 If ADDRESS matches author's mail address, it will remove things like
330 advertisements.  For example:
331
332 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
333 "
334   :type '(repeat
335           (cons
336            (regexp :tag "Address")
337            (choice :tag "Banner" :value nil
338                    (const :tag "Remove signature" signature)
339                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
340                    regexp
341                    (const :tag "None" nil))))
342   :version "22.1"
343   :group 'gnus-article-washing)
344
345 (defmacro gnus-emphasis-custom-with-format (&rest body)
346   `(let ((format "\
347 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
348 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
349      ,@body))
350
351 (defun gnus-emphasis-custom-value-to-external (value)
352   (gnus-emphasis-custom-with-format
353    (if (consp (car value))
354        (list (format format (car (car value)) (cdr (car value)))
355              2
356              (if (nth 1 value) 2 3)
357              (nth 2 value))
358      value)))
359
360 (defun gnus-emphasis-custom-value-to-internal (value)
361   (gnus-emphasis-custom-with-format
362    (let ((regexp (concat "\\`"
363                          (format (regexp-quote format)
364                                  "\\([^()]+\\)" "\\([^()]+\\)")
365                          "\\'"))
366          pattern)
367      (if (string-match regexp (setq pattern (car value)))
368          (list (cons (match-string 1 pattern) (match-string 2 pattern))
369                (= (nth 2 value) 2)
370                (nth 3 value))
371        value))))
372
373 (defcustom gnus-emphasis-alist
374   (let ((types
375          '(("\\*" "\\*" bold nil 2)
376            ("_" "_" underline)
377            ("/" "/" italic)
378            ("_/" "/_" underline-italic)
379            ("_\\*" "\\*_" underline-bold)
380            ("\\*/" "/\\*" bold-italic)
381            ("_\\*/" "/\\*_" underline-bold-italic))))
382     (nconc
383      (gnus-emphasis-custom-with-format
384       (mapcar (lambda (spec)
385                 (list (format format (car spec) (cadr spec))
386                       (or (nth 3 spec) 2)
387                       (or (nth 4 spec) 3)
388                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
389               types))
390      '(;; I've never seen anyone use this strikethru convention whereas I've
391        ;; several times seen it triggered by normal text.  --Stef
392        ;; Miles suggests that this form is sometimes used but for italics,
393        ;; so maybe we should map it to `italic'.
394        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
395        ;; 2 3 gnus-emphasis-strikethru)
396        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
397         2 3 gnus-emphasis-underline))))
398   "*Alist that says how to fontify certain phrases.
399 Each item looks like this:
400
401   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
402
403 The first element is a regular expression to be matched.  The second
404 is a number that says what regular expression grouping used to find
405 the entire emphasized word.  The third is a number that says what
406 regexp grouping should be displayed and highlighted.  The fourth
407 is the face used for highlighting."
408   :type
409   '(repeat
410     (menu-choice
411      :format "%[Customizing Style%]\n%v"
412      :indent 2
413      (group :tag "Default"
414             :value ("" 0 0 default)
415             :value-create
416             (lambda (widget)
417               (let ((value (widget-get
418                             (cadr (widget-get (widget-get widget :parent)
419                                               :args))
420                             :value)))
421                 (if (not (eq (nth 2 value) 'default))
422                     (widget-put
423                      widget
424                      :value
425                      (gnus-emphasis-custom-value-to-external value))))
426               (widget-group-value-create widget))
427             regexp
428             (integer :format "Match group: %v")
429             (integer :format "Emphasize group: %v")
430             face)
431      (group :tag "Simple"
432             :value (("_" . "_") nil default)
433             (cons :format "%v"
434                   (regexp :format "Start regexp: %v")
435                   (regexp :format "End regexp: %v"))
436             (boolean :format "Show start and end patterns: %[%v%]\n"
437                      :on " On " :off " Off ")
438             face)))
439   :get (lambda (symbol)
440          (mapcar 'gnus-emphasis-custom-value-to-internal
441                  (default-value symbol)))
442   :set (lambda (symbol value)
443          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
444                                      value)))
445   :group 'gnus-article-emphasis)
446
447 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
448   "A regexp to describe whitespace which should not be emphasized.
449 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
450 The former avoids underlining of leading and trailing whitespace,
451 and the latter avoids underlining any whitespace at all."
452   :version "21.1"
453   :group 'gnus-article-emphasis
454   :type 'regexp)
455
456 (defface gnus-emphasis-bold '((t (:bold t)))
457   "Face used for displaying strong emphasized text (*word*)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-italic '((t (:italic t)))
461   "Face used for displaying italic emphasized text (/word/)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline '((t (:underline t)))
465   "Face used for displaying underlined emphasized text (_word_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
469   "Face used for displaying underlined bold emphasized text (_*word*_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
473   "Face used for displaying underlined italic emphasized text (_/word/_)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
477   "Face used for displaying bold italic emphasized text (/*word*/)."
478   :group 'gnus-article-emphasis)
479
480 (defface gnus-emphasis-underline-bold-italic
481   '((t (:bold t :italic t :underline t)))
482   "Face used for displaying underlined bold italic emphasized text.
483 Example: (_/*word*/_)."
484   :group 'gnus-article-emphasis)
485
486 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
487                                       '((t (:strikethru t)))
488                                     '((t (:strike-through t))))
489   "Face used for displaying strike-through text (-word-)."
490   :group 'gnus-article-emphasis)
491
492 (defface gnus-emphasis-highlight-words
493   '((t (:background "black" :foreground "yellow")))
494   "Face used for displaying highlighted words."
495   :group 'gnus-article-emphasis)
496
497 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
498   "Format for display of Date headers in article bodies.
499 See `format-time-string' for the possible values.
500
501 The variable can also be function, which should return a complete Date
502 header.  The function is called with one argument, the time, which can
503 be fed to `format-time-string'."
504   :type '(choice string function)
505   :link '(custom-manual "(gnus)Article Date")
506   :group 'gnus-article-washing)
507
508 (defcustom gnus-save-all-headers t
509   "*If non-nil, don't remove any headers before saving.
510 This will be overridden by the `:headers' property that the symbol of
511 the saver function, which is specified by `gnus-default-article-saver',
512 might have."
513   :group 'gnus-article-saving
514   :type 'boolean)
515
516 (defcustom gnus-prompt-before-saving 'always
517   "*This variable says how much prompting is to be done when saving articles.
518 If it is nil, no prompting will be done, and the articles will be
519 saved to the default files.  If this variable is `always', each and
520 every article that is saved will be preceded by a prompt, even when
521 saving large batches of articles.  If this variable is neither nil not
522 `always', there the user will be prompted once for a file name for
523 each invocation of the saving commands."
524   :group 'gnus-article-saving
525   :type '(choice (item always)
526                  (item :tag "never" nil)
527                  (sexp :tag "once" :format "%t\n" :value t)))
528
529 (defcustom gnus-saved-headers gnus-visible-headers
530   "Headers to keep if `gnus-save-all-headers' is nil.
531 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
532 If that variable is nil, however, all headers that match this regexp
533 will be kept while the rest will be deleted before saving.  This and
534 `gnus-save-all-headers' will be overridden by the `:headers' property
535 that the symbol of the saver function, which is specified by
536 `gnus-default-article-saver', might have."
537   :group 'gnus-article-saving
538   :type 'regexp)
539
540 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
541   "A function to save articles in your favourite format.
542 The function will be called by way of the `gnus-summary-save-article'
543 command, and friends such as `gnus-summary-save-article-rmail'.
544
545 Gnus provides the following functions:
546
547 * gnus-summary-save-in-rmail (Rmail format)
548 * gnus-summary-save-in-mail (Unix mail format)
549 * gnus-summary-save-in-folder (MH folder)
550 * gnus-summary-save-in-file (article format)
551 * gnus-summary-save-body-in-file (article body)
552 * gnus-summary-save-in-vm (use VM's folder format)
553 * gnus-summary-write-to-file (article format -- overwrite)
554 * gnus-summary-write-body-to-file (article body -- overwrite)
555 * gnus-summary-save-in-pipe (article format)
556
557 The symbol of each function may have the following properties:
558
559 * :decode
560 The value non-nil means save decoded articles.  This is meaningful
561 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
562 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
563 `gnus-summary-save-in-pipe'.
564
565 * :function
566 The value specifies an alternative function which appends, not
567 overwrites, articles to a file.  This implies that when saving many
568 articles at a time, `gnus-prompt-before-saving' is bound to t and all
569 articles are saved in a single file.  This is meaningful only with
570 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
571
572 * :headers
573 The value specifies the symbol of a variable of which the value
574 specifies headers to be saved.  If it is omitted,
575 `gnus-save-all-headers' and `gnus-saved-headers' control what
576 headers should be saved."
577   :group 'gnus-article-saving
578   :type '(radio (function-item gnus-summary-save-in-rmail)
579                 (function-item gnus-summary-save-in-mail)
580                 (function-item gnus-summary-save-in-folder)
581                 (function-item gnus-summary-save-in-file)
582                 (function-item gnus-summary-save-body-in-file)
583                 (function-item gnus-summary-save-in-vm)
584                 (function-item gnus-summary-write-to-file)
585                 (function-item gnus-summary-write-body-to-file)
586                 (function-item gnus-summary-save-in-pipe)
587                 (function)))
588
589 (defcustom gnus-article-save-coding-system
590   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
591       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
592       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
593       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
594   "Coding system used to save decoded articles to a file.
595
596 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
597 which can safely encode any characters in text.  This is used by the
598 commands including:
599
600 * gnus-summary-save-article-file
601 * gnus-summary-save-article-body-file
602 * gnus-summary-write-article-file
603 * gnus-summary-write-article-body-file
604
605 and the functions to which you may set `gnus-default-article-saver':
606
607 * gnus-summary-save-in-file
608 * gnus-summary-save-body-in-file
609 * gnus-summary-write-to-file
610 * gnus-summary-write-body-to-file
611
612 Those commands and functions save just text displayed in the article
613 buffer to a file if the value of this variable is non-nil.  Note that
614 buttonized MIME parts will be lost in a saved file in that case.
615 Otherwise, raw articles will be saved."
616   :group 'gnus-article-saving
617   :type `(choice
618           :format "%{%t%}:\n %[Value Menu%] %v"
619           (const :tag "Save raw articles" nil)
620           ,@(delq nil
621                   (mapcar
622                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
623                    '((const :tag "UTF-8" utf-8)
624                      (const :tag "iso-2022-7bit" iso-2022-7bit)
625                      (const :tag "Emacs internal" emacs-mule)
626                      (const :tag "escape-quoted" escape-quoted))))
627           (symbol :tag "Coding system")))
628
629 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
630   "A function generating a file name to save articles in Rmail format.
631 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
632   :group 'gnus-article-saving
633   :type 'function)
634
635 (defcustom gnus-mail-save-name 'gnus-plain-save-name
636   "A function generating a file name to save articles in Unix mail format.
637 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
638   :group 'gnus-article-saving
639   :type 'function)
640
641 (defcustom gnus-folder-save-name 'gnus-folder-save-name
642   "A function generating a file name to save articles in MH folder.
643 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
644   :group 'gnus-article-saving
645   :type 'function)
646
647 (defcustom gnus-file-save-name 'gnus-numeric-save-name
648   "A function generating a file name to save articles in article format.
649 The function is called with NEWSGROUP, HEADERS, and optional
650 LAST-FILE."
651   :group 'gnus-article-saving
652   :type 'function)
653
654 (defcustom gnus-split-methods
655   '((gnus-article-archive-name)
656     (gnus-article-nndoc-name))
657   "*Variable used to suggest where articles are to be saved.
658 For instance, if you would like to save articles related to Gnus in
659 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
660 you could set this variable to something like:
661
662  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
663    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
664
665 This variable is an alist where the key is the match and the
666 value is a list of possible files to save in if the match is
667 non-nil.
668
669 If the match is a string, it is used as a regexp match on the
670 article.  If the match is a symbol, that symbol will be funcalled
671 from the buffer of the article to be saved with the newsgroup as the
672 parameter.  If it is a list, it will be evaled in the same buffer.
673
674 If this form or function returns a string, this string will be used as a
675 possible file name; and if it returns a non-nil list, that list will be
676 used as possible file names."
677   :group 'gnus-article-saving
678   :type '(repeat (choice (list :value (fun) function)
679                          (cons :value ("" "") regexp (repeat string))
680                          (sexp :value nil))))
681
682 (defcustom gnus-page-delimiter "^\^L"
683   "*Regexp describing what to use as article page delimiters.
684 The default value is \"^\^L\", which is a form linefeed at the
685 beginning of a line."
686   :type 'regexp
687   :group 'gnus-article-various)
688
689 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
690   "*The format specification for the article mode line.
691 See `gnus-summary-mode-line-format' for a closer description.
692
693 The following additional specs are available:
694
695 %w  The article washing status.
696 %m  The number of MIME parts in the article."
697   :type 'string
698   :group 'gnus-article-various)
699
700 (defcustom gnus-article-mode-hook nil
701   "*A hook for Gnus article mode."
702   :type 'hook
703   :group 'gnus-article-various)
704
705 (when (featurep 'xemacs)
706   ;; Extracted from gnus-xmas-define in order to preserve user settings
707   (when (fboundp 'turn-off-scroll-in-place)
708     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
709   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
710   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
711
712 (defcustom gnus-article-menu-hook nil
713   "*Hook run after the creation of the article mode menu."
714   :type 'hook
715   :group 'gnus-article-various)
716
717 (defcustom gnus-article-prepare-hook nil
718   "*A hook called after an article has been prepared in the article buffer."
719   :type 'hook
720   :group 'gnus-article-various)
721
722 (defcustom gnus-copy-article-ignored-headers nil
723   "List of headers to be removed when copying an article.
724 Each element is a regular expression."
725   :version "23.1" ;; No Gnus
726   :type '(repeat regexp)
727   :group 'gnus-article-various)
728
729 (make-obsolete-variable 'gnus-article-hide-pgp-hook
730                         "This variable is obsolete in Gnus 5.10.")
731
732 (defface gnus-button
733   '((t (:weight bold)))
734   "Face used for highlighting a button in the article buffer."
735   :group 'gnus-article-buttons)
736
737 (defcustom gnus-article-button-face 'gnus-button
738   "Face used for highlighting buttons in the article buffer.
739
740 An article button is a piece of text that you can activate by pressing
741 `RET' or `mouse-2' above it."
742   :type 'face
743   :group 'gnus-article-buttons)
744
745 (defcustom gnus-article-mouse-face 'highlight
746   "Face used for mouse highlighting in the article buffer.
747
748 Article buttons will be displayed in this face when the cursor is
749 above them."
750   :type 'face
751   :group 'gnus-article-buttons)
752
753 (defcustom gnus-signature-face 'gnus-signature
754   "Face used for highlighting a signature in the article buffer.
755 Obsolete; use the face `gnus-signature' for customizations instead."
756   :type 'face
757   :group 'gnus-article-highlight
758   :group 'gnus-article-signature)
759
760 (defface gnus-signature
761   '((t
762      (:italic t)))
763   "Face used for highlighting a signature in the article buffer."
764   :group 'gnus-article-highlight
765   :group 'gnus-article-signature)
766 ;; backward-compatibility alias
767 (put 'gnus-signature-face 'face-alias 'gnus-signature)
768
769 (defface gnus-header-from
770   '((((class color)
771       (background dark))
772      (:foreground "PaleGreen1"))
773     (((class color)
774       (background light))
775      (:foreground "red3"))
776     (t
777      (:italic t)))
778   "Face used for displaying from headers."
779   :group 'gnus-article-headers
780   :group 'gnus-article-highlight)
781 ;; backward-compatibility alias
782 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
783
784 (defface gnus-header-subject
785   '((((class color)
786       (background dark))
787      (:foreground "SeaGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red4"))
791     (t
792      (:bold t :italic t)))
793   "Face used for displaying subject headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
798
799 (defface gnus-header-newsgroups
800   '((((class color)
801       (background dark))
802      (:foreground "yellow" :italic t))
803     (((class color)
804       (background light))
805      (:foreground "MidnightBlue" :italic t))
806     (t
807      (:italic t)))
808   "Face used for displaying newsgroups headers.
809 In the default setup this face is only used for crossposted
810 articles."
811   :group 'gnus-article-headers
812   :group 'gnus-article-highlight)
813 ;; backward-compatibility alias
814 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
815
816 (defface gnus-header-name
817   '((((class color)
818       (background dark))
819      (:foreground "SpringGreen2"))
820     (((class color)
821       (background light))
822      (:foreground "maroon"))
823     (t
824      (:bold t)))
825   "Face used for displaying header names."
826   :group 'gnus-article-headers
827   :group 'gnus-article-highlight)
828 ;; backward-compatibility alias
829 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
830
831 (defface gnus-header-content
832   '((((class color)
833       (background dark))
834      (:foreground "SpringGreen1" :italic t))
835     (((class color)
836       (background light))
837      (:foreground "indianred4" :italic t))
838     (t
839      (:italic t)))  "Face used for displaying header content."
840   :group 'gnus-article-headers
841   :group 'gnus-article-highlight)
842 ;; backward-compatibility alias
843 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
844
845 (defcustom gnus-header-face-alist
846   '(("From" nil gnus-header-from)
847     ("Subject" nil gnus-header-subject)
848     ("Newsgroups:.*," nil gnus-header-newsgroups)
849     ("" gnus-header-name gnus-header-content))
850   "*Controls highlighting of article headers.
851
852 An alist of the form (HEADER NAME CONTENT).
853
854 HEADER is a regular expression which should match the name of a
855 header and NAME and CONTENT are either face names or nil.
856
857 The name of each header field will be displayed using the face
858 specified by the first element in the list where HEADER matches
859 the header name and NAME is non-nil.  Similarly, the content will
860 be displayed by the first non-nil matching CONTENT face."
861   :group 'gnus-article-headers
862   :group 'gnus-article-highlight
863   :type '(repeat (list (regexp :tag "Header")
864                        (choice :tag "Name"
865                                (item :tag "skip" nil)
866                                (face :value default))
867                        (choice :tag "Content"
868                                (item :tag "skip" nil)
869                                (face :value default)))))
870
871 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
872                                           '((xface . (:face gnus-x-face)))
873                                         '((pbm . (:face gnus-x-face))
874                                           (png . nil)))
875   "Alist of image types and properties applied to Face and X-Face images.
876 Here are examples:
877
878 ;; Specify the altitude of Face images in the From header.
879 \(setq gnus-face-properties-alist
880       '((pbm . (:face gnus-x-face :ascent 80))
881         (png . (:ascent 80))))
882
883 ;; Show Face images as pressed buttons.
884 \(setq gnus-face-properties-alist
885       '((pbm . (:face gnus-x-face :relief -2))
886         (png . (:relief -2))))
887
888 See the manual for the valid properties for various image types.
889 Currently, `pbm' is used for X-Face images and `png' is used for Face
890 images in Emacs.  Only the `:face' property is effective on the `xface'
891 image type in XEmacs if it is built with the libcompface library."
892   :version "23.1" ;; No Gnus
893   :group 'gnus-article-headers
894   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
895
896 (defcustom gnus-article-decode-hook
897   '(article-decode-charset article-decode-encoded-words
898                            article-decode-group-name article-decode-idna-rhs)
899   "*Hook run to decode charsets in articles."
900   :group 'gnus-article-headers
901   :type 'hook)
902
903 (defcustom gnus-display-mime-function 'gnus-display-mime
904   "Function to display MIME articles."
905   :group 'gnus-article-mime
906   :type 'function)
907
908 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
909   "Function used to decode headers.")
910
911 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
912   "Function used to decode addresses.")
913
914 (defvar gnus-article-dumbquotes-map
915   '(("\200" "EUR")
916     ("\202" ",")
917     ("\203" "f")
918     ("\204" ",,")
919     ("\205" "...")
920     ("\213" "<")
921     ("\214" "OE")
922     ("\221" "`")
923     ("\222" "'")
924     ("\223" "``")
925     ("\224" "\"")
926     ("\225" "*")
927     ("\226" "-")
928     ("\227" "--")
929     ("\230" "~")
930     ("\231" "(TM)")
931     ("\233" ">")
932     ("\234" "oe")
933     ("\264" "'"))
934   "Table for MS-to-Latin1 translation.")
935
936 (defcustom gnus-ignored-mime-types nil
937   "List of MIME types that should be ignored by Gnus."
938   :version "21.1"
939   :group 'gnus-article-mime
940   :type '(repeat regexp))
941
942 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
943   "List of MIME types that should not be given buttons when rendered inline.
944 See also `gnus-buttonized-mime-types' which may override this variable.
945 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
946   :version "21.1"
947   :group 'gnus-article-mime
948   :type '(repeat regexp))
949
950 (defcustom gnus-buttonized-mime-types nil
951   "List of MIME types that should be given buttons when rendered inline.
952 If set, this variable overrides `gnus-unbuttonized-mime-types'.
953 To see e.g. security buttons you could set this to
954 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
955 this list to display radio buttons that allow you to choose one of two
956 media types those mails include.  See also `mm-discouraged-alternatives'.
957 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
958   :version "22.1"
959   :group 'gnus-article-mime
960   :type '(repeat regexp))
961
962 (defcustom gnus-inhibit-mime-unbuttonizing nil
963   "If non-nil, all MIME parts get buttons.
964 When nil (the default value), then some MIME parts do not get buttons,
965 as described by the variables `gnus-buttonized-mime-types' and
966 `gnus-unbuttonized-mime-types'."
967   :version "22.1"
968   :group 'gnus-article-mime
969   :type 'boolean)
970
971 (defcustom gnus-body-boundary-delimiter "_"
972   "String used to delimit header and body.
973 This variable is used by `gnus-article-treat-body-boundary' which can
974 be controlled by `gnus-treat-body-boundary'."
975   :version "22.1"
976   :group 'gnus-article-various
977   :type '(choice (item :tag "None" :value nil)
978                  string))
979
980 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
981                                   "/usr/share/picons")
982   "Defines the location of the faces database.
983 For information on obtaining this database of pretty pictures, please
984 see http://www.cs.indiana.edu/picons/ftp/index.html"
985   :version "22.1"
986   :type '(repeat directory)
987   :link '(url-link :tag "download"
988                    "http://www.cs.indiana.edu/picons/ftp/index.html")
989   :link '(custom-manual "(gnus)Picons")
990   :group 'gnus-picon)
991
992 (defun gnus-picons-installed-p ()
993   "Say whether picons are installed on your machine."
994   (let ((installed nil))
995     (dolist (database gnus-picon-databases)
996       (when (file-exists-p database)
997         (setq installed t)))
998     installed))
999
1000 (defcustom gnus-article-mime-part-function nil
1001   "Function called with a MIME handle as the argument.
1002 This is meant for people who want to do something automatic based
1003 on parts -- for instance, adding Vcard info to a database."
1004   :group 'gnus-article-mime
1005   :type '(choice (const nil)
1006                  function))
1007
1008 (defcustom gnus-mime-multipart-functions nil
1009   "An alist of MIME types to functions to display them."
1010   :version "21.1"
1011   :group 'gnus-article-mime
1012   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1013
1014 (defcustom gnus-article-date-lapsed-new-header nil
1015   "Whether the X-Sent and Date headers can coexist.
1016 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1017 either replace the old \"Date:\" header (if this variable is nil), or
1018 be added below it (otherwise)."
1019   :version "21.1"
1020   :group 'gnus-article-headers
1021   :type 'boolean)
1022
1023 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1024   "Function called with a MIME handle as the argument.
1025 This is meant for people who want to view first matched part.
1026 For `undisplayed-alternative' (default), the first undisplayed
1027 part or alternative part is used.  For `undisplayed', the first
1028 undisplayed part is used.  For a function, the first part which
1029 the function return t is used.  For nil, the first part is
1030 used."
1031   :version "21.1"
1032   :group 'gnus-article-mime
1033   :type '(choice
1034           (item :tag "first" :value nil)
1035           (item :tag "undisplayed" :value undisplayed)
1036           (item :tag "undisplayed or alternative"
1037                 :value undisplayed-alternative)
1038           (function)))
1039
1040 (defcustom gnus-mime-action-alist
1041   '(("save to file" . gnus-mime-save-part)
1042     ("save and strip" . gnus-mime-save-part-and-strip)
1043     ("replace with file" . gnus-mime-replace-part)
1044     ("delete part" . gnus-mime-delete-part)
1045     ("display as text" . gnus-mime-inline-part)
1046     ("view the part" . gnus-mime-view-part)
1047     ("pipe to command" . gnus-mime-pipe-part)
1048     ("toggle display" . gnus-article-press-button)
1049     ("toggle display" . gnus-article-view-part-as-charset)
1050     ("view as type" . gnus-mime-view-part-as-type)
1051     ("view internally" . gnus-mime-view-part-internally)
1052     ("view externally" . gnus-mime-view-part-externally))
1053   "An alist of actions that run on the MIME attachment."
1054   :group 'gnus-article-mime
1055   :type '(repeat (cons (string :tag "name")
1056                        (function))))
1057
1058 (defcustom gnus-auto-select-part 1
1059   "Advance to next MIME part when deleting or stripping parts.
1060
1061 When 0, point will be placed on the same part as before.  When
1062 positive (negative), move point forward (backwards) this many
1063 parts.  When nil, redisplay article."
1064   :version "23.1" ;; No Gnus
1065   :group 'gnus-article-mime
1066   :type '(choice (const nil :tag "Redisplay article.")
1067                  (const 1 :tag "Next part.")
1068                  (const 0 :tag "Current part.")
1069                  integer))
1070
1071 ;;;
1072 ;;; The treatment variables
1073 ;;;
1074
1075 (defvar gnus-part-display-hook nil
1076   "Hook called on parts that are to receive treatment.")
1077
1078 (defvar gnus-article-treat-custom
1079   '(choice (const :tag "Off" nil)
1080            (const :tag "On" t)
1081            (const :tag "Header" head)
1082            (const :tag "First" first)
1083            (const :tag "Last" last)
1084            (integer :tag "Less")
1085            (repeat :tag "Groups" regexp)
1086            (sexp :tag "Predicate")))
1087
1088 (defvar gnus-article-treat-head-custom
1089   '(choice (const :tag "Off" nil)
1090            (const :tag "Header" head)))
1091
1092 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1093                                    "text/x-patch")
1094   "Parts to treat.")
1095
1096 (defvar gnus-inhibit-treatment nil
1097   "Whether to inhibit treatment.")
1098
1099 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1100   "Highlight the signature.
1101 Valid values are nil, t, `head', `first', `last', an integer or a
1102 predicate.  See Info node `(gnus)Customizing Articles'."
1103   :group 'gnus-article-treat
1104   :link '(custom-manual "(gnus)Customizing Articles")
1105   :type gnus-article-treat-custom)
1106 (put 'gnus-treat-highlight-signature 'highlight t)
1107
1108 (defcustom gnus-treat-buttonize 100000
1109   "Add buttons.
1110 Valid values are nil, t, `head', `first', `last', an integer or a
1111 predicate.  See Info node `(gnus)Customizing Articles'."
1112   :group 'gnus-article-treat
1113   :link '(custom-manual "(gnus)Customizing Articles")
1114   :type gnus-article-treat-custom)
1115 (put 'gnus-treat-buttonize 'highlight t)
1116
1117 (defcustom gnus-treat-buttonize-head 'head
1118   "Add buttons to the head.
1119 Valid values are nil, t, `head', `first', `last', an integer or a
1120 predicate.  See Info node `(gnus)Customizing Articles'."
1121   :group 'gnus-article-treat
1122   :link '(custom-manual "(gnus)Customizing Articles")
1123   :type gnus-article-treat-head-custom)
1124 (put 'gnus-treat-buttonize-head 'highlight t)
1125
1126 (defcustom gnus-treat-emphasize 50000
1127   "Emphasize text.
1128 Valid values are nil, t, `head', `first', `last', an integer or a
1129 predicate.  See Info node `(gnus)Customizing Articles'."
1130   :group 'gnus-article-treat
1131   :link '(custom-manual "(gnus)Customizing Articles")
1132   :type gnus-article-treat-custom)
1133 (put 'gnus-treat-emphasize 'highlight t)
1134
1135 (defcustom gnus-treat-strip-cr nil
1136   "Remove carriage returns.
1137 Valid values are nil, t, `head', `first', `last', an integer or a
1138 predicate.  See Info node `(gnus)Customizing Articles'."
1139   :version "22.1"
1140   :group 'gnus-article-treat
1141   :link '(custom-manual "(gnus)Customizing Articles")
1142   :type gnus-article-treat-custom)
1143
1144 (defcustom gnus-treat-unsplit-urls nil
1145   "Remove newlines from within URLs.
1146 Valid values are nil, t, `head', `first', `last', an integer or a
1147 predicate.  See Info node `(gnus)Customizing Articles'."
1148   :version "22.1"
1149   :group 'gnus-article-treat
1150   :link '(custom-manual "(gnus)Customizing Articles")
1151   :type gnus-article-treat-custom)
1152
1153 (defcustom gnus-treat-leading-whitespace nil
1154   "Remove leading whitespace in headers.
1155 Valid values are nil, t, `head', `first', `last', an integer or a
1156 predicate.  See Info node `(gnus)Customizing Articles'."
1157   :version "22.1"
1158   :group 'gnus-article-treat
1159   :link '(custom-manual "(gnus)Customizing Articles")
1160   :type gnus-article-treat-custom)
1161
1162 (defcustom gnus-treat-hide-headers 'head
1163   "Hide headers.
1164 Valid values are nil, t, `head', `first', `last', an integer or a
1165 predicate.  See Info node `(gnus)Customizing Articles'."
1166   :group 'gnus-article-treat
1167   :link '(custom-manual "(gnus)Customizing Articles")
1168   :type gnus-article-treat-head-custom)
1169
1170 (defcustom gnus-treat-hide-boring-headers nil
1171   "Hide boring headers.
1172 Valid values are nil, t, `head', `first', `last', an integer or a
1173 predicate.  See Info node `(gnus)Customizing Articles'."
1174   :group 'gnus-article-treat
1175   :link '(custom-manual "(gnus)Customizing Articles")
1176   :type gnus-article-treat-head-custom)
1177
1178 (defcustom gnus-treat-hide-signature nil
1179   "Hide the signature.
1180 Valid values are nil, t, `head', `first', `last', an integer or a
1181 predicate.  See Info node `(gnus)Customizing Articles'."
1182   :group 'gnus-article-treat
1183   :link '(custom-manual "(gnus)Customizing Articles")
1184   :type gnus-article-treat-custom)
1185
1186 (defcustom gnus-treat-fill-article nil
1187   "Fill the article.
1188 Valid values are nil, t, `head', `first', `last', an integer or a
1189 predicate.  See Info node `(gnus)Customizing Articles'."
1190   :group 'gnus-article-treat
1191   :link '(custom-manual "(gnus)Customizing Articles")
1192   :type gnus-article-treat-custom)
1193
1194 (defcustom gnus-treat-hide-citation nil
1195   "Hide cited text.
1196 Valid values are nil, t, `head', `first', `last', an integer or a
1197 predicate.  See Info node `(gnus)Customizing Articles'."
1198   :group 'gnus-article-treat
1199   :link '(custom-manual "(gnus)Customizing Articles")
1200   :type gnus-article-treat-custom)
1201
1202 (defcustom gnus-treat-hide-citation-maybe nil
1203   "Hide cited text.
1204 Valid values are nil, t, `head', `first', `last', an integer or a
1205 predicate.  See Info node `(gnus)Customizing Articles'."
1206   :group 'gnus-article-treat
1207   :link '(custom-manual "(gnus)Customizing Articles")
1208   :type gnus-article-treat-custom)
1209
1210 (defcustom gnus-treat-strip-list-identifiers 'head
1211   "Strip list identifiers from `gnus-list-identifiers`.
1212 Valid values are nil, t, `head', `first', `last', an integer or a
1213 predicate.  See Info node `(gnus)Customizing Articles'."
1214   :version "21.1"
1215   :group 'gnus-article-treat
1216   :link '(custom-manual "(gnus)Customizing Articles")
1217   :type gnus-article-treat-custom)
1218
1219 (make-obsolete-variable 'gnus-treat-strip-pgp
1220                         "This option is obsolete in Gnus 5.10.")
1221
1222 (defcustom gnus-treat-strip-pem nil
1223   "Strip PEM signatures.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-strip-banner t
1231   "Strip banners from articles.
1232 The banner to be stripped is specified in the `banner' group parameter.
1233 Valid values are nil, t, `head', `first', `last', an integer or a
1234 predicate.  See Info node `(gnus)Customizing Articles'."
1235   :group 'gnus-article-treat
1236   :link '(custom-manual "(gnus)Customizing Articles")
1237   :type gnus-article-treat-custom)
1238
1239 (defcustom gnus-treat-highlight-headers 'head
1240   "Highlight the headers.
1241 Valid values are nil, t, `head', `first', `last', an integer or a
1242 predicate.  See Info node `(gnus)Customizing Articles'."
1243   :group 'gnus-article-treat
1244   :link '(custom-manual "(gnus)Customizing Articles")
1245   :type gnus-article-treat-head-custom)
1246 (put 'gnus-treat-highlight-headers 'highlight t)
1247
1248 (defcustom gnus-treat-highlight-citation t
1249   "Highlight cited text.
1250 Valid values are nil, t, `head', `first', `last', an integer or a
1251 predicate.  See Info node `(gnus)Customizing Articles'."
1252   :group 'gnus-article-treat
1253   :link '(custom-manual "(gnus)Customizing Articles")
1254   :type gnus-article-treat-custom)
1255 (put 'gnus-treat-highlight-citation 'highlight t)
1256
1257 (defcustom gnus-treat-date-ut nil
1258   "Display the Date in UT (GMT).
1259 Valid values are nil, t, `head', `first', `last', an integer or a
1260 predicate.  See Info node `(gnus)Customizing Articles'."
1261   :group 'gnus-article-treat
1262   :link '(custom-manual "(gnus)Customizing Articles")
1263   :type gnus-article-treat-head-custom)
1264
1265 (defcustom gnus-treat-date-local nil
1266   "Display the Date in the local timezone.
1267 Valid values are nil, t, `head', `first', `last', an integer or a
1268 predicate.  See Info node `(gnus)Customizing Articles'."
1269   :group 'gnus-article-treat
1270   :link '(custom-manual "(gnus)Customizing Articles")
1271   :type gnus-article-treat-head-custom)
1272
1273 (defcustom gnus-treat-date-english nil
1274   "Display the Date in a format that can be read aloud in English.
1275 Valid values are nil, t, `head', `first', `last', an integer or a
1276 predicate.  See Info node `(gnus)Customizing Articles'."
1277   :version "22.1"
1278   :group 'gnus-article-treat
1279   :link '(custom-manual "(gnus)Customizing Articles")
1280   :type gnus-article-treat-head-custom)
1281
1282 (defcustom gnus-treat-date-lapsed nil
1283   "Display the Date header in a way that says how much time has elapsed.
1284 Valid values are nil, t, `head', `first', `last', an integer or a
1285 predicate.  See Info node `(gnus)Customizing Articles'."
1286   :group 'gnus-article-treat
1287   :link '(custom-manual "(gnus)Customizing Articles")
1288   :type gnus-article-treat-head-custom)
1289
1290 (defcustom gnus-treat-date-original nil
1291   "Display the date in the original timezone.
1292 Valid values are nil, t, `head', `first', `last', an integer or a
1293 predicate.  See Info node `(gnus)Customizing Articles'."
1294   :group 'gnus-article-treat
1295   :link '(custom-manual "(gnus)Customizing Articles")
1296   :type gnus-article-treat-head-custom)
1297
1298 (defcustom gnus-treat-date-iso8601 nil
1299   "Display the date in the ISO8601 format.
1300 Valid values are nil, t, `head', `first', `last', an integer or a
1301 predicate.  See Info node `(gnus)Customizing Articles'."
1302   :version "21.1"
1303   :group 'gnus-article-treat
1304   :link '(custom-manual "(gnus)Customizing Articles")
1305   :type gnus-article-treat-head-custom)
1306
1307 (defcustom gnus-treat-date-user-defined nil
1308   "Display the date in a user-defined format.
1309 The format is defined by the `gnus-article-time-format' variable.
1310 Valid values are nil, t, `head', `first', `last', an integer or a
1311 predicate.  See Info node `(gnus)Customizing Articles'."
1312   :group 'gnus-article-treat
1313   :link '(custom-manual "(gnus)Customizing Articles")
1314   :type gnus-article-treat-head-custom)
1315
1316 (defcustom gnus-treat-strip-headers-in-body t
1317   "Strip the X-No-Archive header line from the beginning of the body.
1318 Valid values are nil, t, `head', `first', `last', an integer or a
1319 predicate.  See Info node `(gnus)Customizing Articles'."
1320   :version "21.1"
1321   :group 'gnus-article-treat
1322   :link '(custom-manual "(gnus)Customizing Articles")
1323   :type gnus-article-treat-custom)
1324
1325 (defcustom gnus-treat-strip-trailing-blank-lines nil
1326   "Strip trailing blank lines.
1327 Valid values are nil, t, `head', `first', `last', an integer or a
1328 predicate.  See Info node `(gnus)Customizing Articles'.
1329
1330 When set to t, it also strips trailing blanks in all MIME parts.
1331 Consider to use `last' instead."
1332   :group 'gnus-article-treat
1333   :link '(custom-manual "(gnus)Customizing Articles")
1334   :type gnus-article-treat-custom)
1335
1336 (defcustom gnus-treat-strip-leading-blank-lines nil
1337   "Strip leading blank lines.
1338 Valid values are nil, t, `head', `first', `last', an integer or a
1339 predicate.  See Info node `(gnus)Customizing Articles'.
1340
1341 When set to t, it also strips trailing blanks in all MIME parts."
1342   :group 'gnus-article-treat
1343   :link '(custom-manual "(gnus)Customizing Articles")
1344   :type gnus-article-treat-custom)
1345
1346 (defcustom gnus-treat-strip-multiple-blank-lines nil
1347   "Strip multiple blank lines.
1348 Valid values are nil, t, `head', `first', `last', an integer or a
1349 predicate.  See Info node `(gnus)Customizing Articles'."
1350   :group 'gnus-article-treat
1351   :link '(custom-manual "(gnus)Customizing Articles")
1352   :type gnus-article-treat-custom)
1353
1354 (defcustom gnus-treat-unfold-headers 'head
1355   "Unfold folded header lines.
1356 Valid values are nil, t, `head', `first', `last', an integer or a
1357 predicate.  See Info node `(gnus)Customizing Articles'."
1358   :version "22.1"
1359   :group 'gnus-article-treat
1360   :link '(custom-manual "(gnus)Customizing Articles")
1361   :type gnus-article-treat-custom)
1362
1363 (defcustom gnus-article-unfold-long-headers nil
1364   "If non-nil, allow unfolding headers even if the header is long.
1365 If it is a regexp, only long headers matching this regexp are unfolded.
1366 If it is t, all long headers are unfolded.
1367
1368 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1369   :version "23.1" ;; No Gnus
1370   :group 'gnus-article-treat
1371   :type '(choice (const nil)
1372                  (const :tag "all" t)
1373                  (regexp)))
1374
1375 (defcustom gnus-treat-fold-headers nil
1376   "Fold headers.
1377 Valid values are nil, t, `head', `first', `last', an integer or a
1378 predicate.  See Info node `(gnus)Customizing Articles'."
1379   :version "22.1"
1380   :group 'gnus-article-treat
1381   :link '(custom-manual "(gnus)Customizing Articles")
1382   :type gnus-article-treat-custom)
1383
1384 (defcustom gnus-treat-fold-newsgroups 'head
1385   "Fold the Newsgroups and Followup-To headers.
1386 Valid values are nil, t, `head', `first', `last', an integer or a
1387 predicate.  See Info node `(gnus)Customizing Articles'."
1388   :version "22.1"
1389   :group 'gnus-article-treat
1390   :link '(custom-manual "(gnus)Customizing Articles")
1391   :type gnus-article-treat-custom)
1392
1393 (defcustom gnus-treat-overstrike t
1394   "Treat overstrike highlighting.
1395 Valid values are nil, t, `head', `first', `last', an integer or a
1396 predicate.  See Info node `(gnus)Customizing Articles'."
1397   :group 'gnus-article-treat
1398   :link '(custom-manual "(gnus)Customizing Articles")
1399   :type gnus-article-treat-custom)
1400 (put 'gnus-treat-overstrike 'highlight t)
1401
1402 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1403   "Treat ANSI SGR control sequences.
1404 Valid values are nil, t, `head', `first', `last', an integer or a
1405 predicate.  See Info node `(gnus)Customizing Articles'."
1406   :group 'gnus-article-treat
1407   :link '(custom-manual "(gnus)Customizing Articles")
1408   :type gnus-article-treat-custom)
1409
1410 (make-obsolete-variable 'gnus-treat-display-xface
1411                         'gnus-treat-display-x-face)
1412
1413 (defcustom gnus-treat-display-x-face
1414   (and (not noninteractive)
1415        (gnus-image-type-available-p 'xbm)
1416        (if (featurep 'xemacs)
1417            (featurep 'xface)
1418          (and (string-match "^0x" (shell-command-to-string "uncompface"))
1419               (executable-find "icontopbm")))
1420        'head)
1421   "Display X-Face headers.
1422 Valid values are nil and `head'.
1423 See Info node `(gnus)Customizing Articles' and Info node
1424 `(gnus)X-Face' for details."
1425   :group 'gnus-article-treat
1426   :version "21.1"
1427   :link '(custom-manual "(gnus)Customizing Articles")
1428   :link '(custom-manual "(gnus)X-Face")
1429   :type gnus-article-treat-head-custom
1430   :set (lambda (symbol value)
1431          (set-default
1432           symbol
1433           (cond ((or (boundp symbol) (get symbol 'saved-value))
1434                  value)
1435                 ((boundp 'gnus-treat-display-xface)
1436                  (message "\
1437 ** gnus-treat-display-xface is an obsolete variable;\
1438  use gnus-treat-display-x-face instead")
1439                  (default-value 'gnus-treat-display-xface))
1440                 ((get 'gnus-treat-display-xface 'saved-value)
1441                  (message "\
1442 ** gnus-treat-display-xface is an obsolete variable;\
1443  use gnus-treat-display-x-face instead")
1444                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1445                 (t
1446                  value)))))
1447 (put 'gnus-treat-display-x-face 'highlight t)
1448
1449 (defcustom gnus-treat-display-face
1450   (and (not noninteractive)
1451        (gnus-image-type-available-p 'png)
1452        'head)
1453   "Display Face headers.
1454 Valid values are nil, t, `head', `first', `last', an integer or a
1455 predicate.  See Info node `(gnus)Customizing Articles' and Info
1456 node `(gnus)Face' for details."
1457   :group 'gnus-article-treat
1458   :version "22.1"
1459   :link '(custom-manual "(gnus)Customizing Articles")
1460   :link '(custom-manual "(gnus)X-Face")
1461   :type gnus-article-treat-head-custom)
1462 (put 'gnus-treat-display-face 'highlight t)
1463
1464 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1465   "Display smileys.
1466 Valid values are nil, t, `head', `first', `last', an integer or a
1467 predicate.  See Info node `(gnus)Customizing Articles' and Info
1468 node `(gnus)Smileys' for details."
1469   :group 'gnus-article-treat
1470   :version "21.1"
1471   :link '(custom-manual "(gnus)Customizing Articles")
1472   :link '(custom-manual "(gnus)Smileys")
1473   :type gnus-article-treat-custom)
1474 (put 'gnus-treat-display-smileys 'highlight t)
1475
1476 (defcustom gnus-treat-from-picon
1477   (if (and (gnus-image-type-available-p 'xpm)
1478            (gnus-picons-installed-p))
1479       'head nil)
1480   "Display picons in the From header.
1481 Valid values are nil, t, `head', `first', `last', an integer or a
1482 predicate.  See Info node `(gnus)Customizing Articles' and Info
1483 node `(gnus)Picons' for details."
1484   :version "22.1"
1485   :group 'gnus-article-treat
1486   :group 'gnus-picon
1487   :link '(custom-manual "(gnus)Customizing Articles")
1488   :link '(custom-manual "(gnus)Picons")
1489   :type gnus-article-treat-head-custom)
1490 (put 'gnus-treat-from-picon 'highlight t)
1491
1492 (defcustom gnus-treat-mail-picon
1493   (if (and (gnus-image-type-available-p 'xpm)
1494            (gnus-picons-installed-p))
1495       'head nil)
1496   "Display picons in To and Cc headers.
1497 Valid values are nil, t, `head', `first', `last', an integer or a
1498 predicate.  See Info node `(gnus)Customizing Articles' and Info
1499 node `(gnus)Picons' for details."
1500   :version "22.1"
1501   :group 'gnus-article-treat
1502   :group 'gnus-picon
1503   :link '(custom-manual "(gnus)Customizing Articles")
1504   :link '(custom-manual "(gnus)Picons")
1505   :type gnus-article-treat-head-custom)
1506 (put 'gnus-treat-mail-picon 'highlight t)
1507
1508 (defcustom gnus-treat-newsgroups-picon
1509   (if (and (gnus-image-type-available-p 'xpm)
1510            (gnus-picons-installed-p))
1511       'head nil)
1512   "Display picons in the Newsgroups and Followup-To headers.
1513 Valid values are nil, t, `head', `first', `last', an integer or a
1514 predicate.  See Info node `(gnus)Customizing Articles' and Info
1515 node `(gnus)Picons' for details."
1516   :version "22.1"
1517   :group 'gnus-article-treat
1518   :group 'gnus-picon
1519   :link '(custom-manual "(gnus)Customizing Articles")
1520   :link '(custom-manual "(gnus)Picons")
1521   :type gnus-article-treat-head-custom)
1522 (put 'gnus-treat-newsgroups-picon 'highlight t)
1523
1524 (defcustom gnus-treat-body-boundary
1525   (if (or gnus-treat-newsgroups-picon
1526           gnus-treat-mail-picon
1527           gnus-treat-from-picon)
1528       ;; If there's much decoration, the user might prefer a boundery.
1529       'head
1530     nil)
1531   "Draw a boundary at the end of the headers.
1532 Valid values are nil and `head'.
1533 See Info node `(gnus)Customizing Articles' for details."
1534   :version "22.1"
1535   :group 'gnus-article-treat
1536   :link '(custom-manual "(gnus)Customizing Articles")
1537   :type gnus-article-treat-head-custom)
1538
1539 (defcustom gnus-treat-capitalize-sentences nil
1540   "Capitalize sentence-starting words.
1541 Valid values are nil, t, `head', `first', `last', an integer or a
1542 predicate.  See Info node `(gnus)Customizing Articles'."
1543   :version "21.1"
1544   :group 'gnus-article-treat
1545   :link '(custom-manual "(gnus)Customizing Articles")
1546   :type gnus-article-treat-custom)
1547
1548 (defcustom gnus-treat-wash-html nil
1549   "Format as HTML.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles'."
1552   :version "22.1"
1553   :group 'gnus-article-treat
1554   :link '(custom-manual "(gnus)Customizing Articles")
1555   :type gnus-article-treat-custom)
1556
1557 (defcustom gnus-treat-fill-long-lines nil
1558   "Fill long lines.
1559 Valid values are nil, t, `head', `first', `last', an integer or a
1560 predicate.  See Info node `(gnus)Customizing Articles'."
1561   :group 'gnus-article-treat
1562   :link '(custom-manual "(gnus)Customizing Articles")
1563   :type gnus-article-treat-custom)
1564
1565 (defcustom gnus-treat-play-sounds nil
1566   "Play sounds.
1567 Valid values are nil, t, `head', `first', `last', an integer or a
1568 predicate.  See Info node `(gnus)Customizing Articles'."
1569   :version "21.1"
1570   :group 'gnus-article-treat
1571   :link '(custom-manual "(gnus)Customizing Articles")
1572   :type gnus-article-treat-custom)
1573
1574 (defcustom gnus-treat-translate nil
1575   "Translate articles from one language to another.
1576 Valid values are nil, t, `head', `first', `last', an integer or a
1577 predicate.  See Info node `(gnus)Customizing Articles'."
1578   :version "21.1"
1579   :group 'gnus-article-treat
1580   :link '(custom-manual "(gnus)Customizing Articles")
1581   :type gnus-article-treat-custom)
1582
1583 (defcustom gnus-treat-x-pgp-sig nil
1584   "Verify X-PGP-Sig.
1585 To automatically treat X-PGP-Sig, set it to head.
1586 Valid values are nil, t, `head', `first', `last', an integer or a
1587 predicate.  See Info node `(gnus)Customizing Articles'."
1588   :version "22.1"
1589   :group 'gnus-article-treat
1590   :group 'mime-security
1591   :link '(custom-manual "(gnus)Customizing Articles")
1592   :type gnus-article-treat-custom)
1593
1594 (defvar gnus-article-encrypt-protocol-alist
1595   '(("PGP" . mml2015-self-encrypt)))
1596
1597 ;; Set to nil if more than one protocol added to
1598 ;; gnus-article-encrypt-protocol-alist.
1599 (defcustom gnus-article-encrypt-protocol "PGP"
1600   "The protocol used for encrypt articles.
1601 It is a string, such as \"PGP\". If nil, ask user."
1602   :version "22.1"
1603   :type 'string
1604   :group 'mime-security)
1605
1606 (defvar gnus-article-wash-function nil
1607   "Function used for converting HTML into text.")
1608
1609 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1610                               (mm-coding-system-p 'utf-8)
1611                               (executable-find idna-program))
1612   "Whether IDNA decoding of headers is used when viewing messages.
1613 This requires GNU Libidn, and by default only enabled if it is found."
1614   :version "22.1"
1615   :group 'gnus-article-headers
1616   :type 'boolean)
1617
1618 (defcustom gnus-article-over-scroll nil
1619   "If non-nil, allow scrolling the article buffer even when there no more text."
1620   :version "22.1"
1621   :group 'gnus-article
1622   :type 'boolean)
1623
1624 ;;; Internal variables
1625
1626 (defvar gnus-english-month-names
1627   '("January" "February" "March" "April" "May" "June" "July" "August"
1628     "September" "October" "November" "December"))
1629
1630 (defvar article-goto-body-goes-to-point-min-p nil)
1631 (defvar gnus-article-wash-types nil)
1632 (defvar gnus-article-emphasis-alist nil)
1633 (defvar gnus-article-image-alist nil)
1634
1635 (defvar gnus-article-mime-handle-alist-1 nil)
1636 (defvar gnus-treatment-function-alist
1637   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1638     (gnus-treat-strip-banner gnus-article-strip-banner)
1639     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1640     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1641     (gnus-treat-buttonize gnus-article-add-buttons)
1642     (gnus-treat-fill-article gnus-article-fill-cited-article)
1643     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1644     (gnus-treat-strip-cr gnus-article-remove-cr)
1645     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1646     (gnus-treat-date-ut gnus-article-date-ut)
1647     (gnus-treat-date-local gnus-article-date-local)
1648     (gnus-treat-date-english gnus-article-date-english)
1649     (gnus-treat-date-original gnus-article-date-original)
1650     (gnus-treat-date-user-defined gnus-article-date-user)
1651     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1652     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1653     (gnus-treat-display-x-face gnus-article-display-x-face)
1654     (gnus-treat-display-face gnus-article-display-face)
1655     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1656     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1657     (gnus-treat-hide-signature gnus-article-hide-signature)
1658     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1659     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1660     (gnus-treat-strip-pem gnus-article-hide-pem)
1661     (gnus-treat-from-picon gnus-treat-from-picon)
1662     (gnus-treat-mail-picon gnus-treat-mail-picon)
1663     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1664     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1665     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1666     (gnus-treat-strip-trailing-blank-lines
1667      gnus-article-remove-trailing-blank-lines)
1668     (gnus-treat-strip-leading-blank-lines
1669      gnus-article-strip-leading-blank-lines)
1670     (gnus-treat-strip-multiple-blank-lines
1671      gnus-article-strip-multiple-blank-lines)
1672     (gnus-treat-overstrike gnus-article-treat-overstrike)
1673     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1674     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1675     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1676     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1677     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1678     (gnus-treat-display-smileys gnus-treat-smiley)
1679     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1680     (gnus-treat-wash-html gnus-article-wash-html)
1681     (gnus-treat-emphasize gnus-article-emphasize)
1682     (gnus-treat-hide-citation gnus-article-hide-citation)
1683     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1684     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1685     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1686     (gnus-treat-play-sounds gnus-earcon-display)))
1687
1688 (defvar gnus-article-mime-handle-alist nil)
1689 (defvar article-lapsed-timer nil)
1690 (defvar gnus-article-current-summary nil)
1691
1692 (defvar gnus-article-mode-syntax-table
1693   (let ((table (copy-syntax-table text-mode-syntax-table)))
1694     ;; This causes the citation match run O(2^n).
1695     ;; (modify-syntax-entry ?- "w" table)
1696     (modify-syntax-entry ?> ")<" table)
1697     (modify-syntax-entry ?< "(>" table)
1698     ;; make M-. in article buffers work for `foo' strings
1699     (modify-syntax-entry ?' " " table)
1700     (modify-syntax-entry ?` " " table)
1701     table)
1702   "Syntax table used in article mode buffers.
1703 Initialized from `text-mode-syntax-table.")
1704
1705 (defvar gnus-save-article-buffer nil)
1706
1707 (defvar gnus-number-of-articles-to-be-saved nil)
1708
1709 (defvar gnus-inhibit-hiding nil)
1710
1711 (defvar gnus-article-edit-mode nil)
1712
1713 ;;; Macros for dealing with the article buffer.
1714
1715 (defmacro gnus-with-article-headers (&rest forms)
1716   `(with-current-buffer gnus-article-buffer
1717      (save-restriction
1718        (let ((inhibit-read-only t)
1719              (inhibit-point-motion-hooks t)
1720              (case-fold-search t))
1721          (article-narrow-to-head)
1722          ,@forms))))
1723
1724 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1725 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1726
1727 (defmacro gnus-with-article-buffer (&rest forms)
1728   `(with-current-buffer gnus-article-buffer
1729      (let ((inhibit-read-only t))
1730        ,@forms)))
1731
1732 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1733 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1734
1735 (defun gnus-article-goto-header (header)
1736   "Go to HEADER, which is a regular expression."
1737   (re-search-forward (concat "^\\(" header "\\):") nil t))
1738
1739 (defsubst gnus-article-hide-text (b e props)
1740   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1741   (gnus-add-text-properties-when 'article-type nil b e props)
1742   (when (memq 'intangible props)
1743     (put-text-property
1744      (max (1- b) (point-min))
1745      b 'intangible (cddr (memq 'intangible props)))))
1746
1747 (defsubst gnus-article-unhide-text (b e)
1748   "Remove hidden text properties from region between B and E."
1749   (remove-text-properties b e gnus-hidden-properties)
1750   (when (memq 'intangible gnus-hidden-properties)
1751     (put-text-property (max (1- b) (point-min))
1752                        b 'intangible nil)))
1753
1754 (defun gnus-article-hide-text-type (b e type)
1755   "Hide text of TYPE between B and E."
1756   (gnus-add-wash-type type)
1757   (gnus-article-hide-text
1758    b e (cons 'article-type (cons type gnus-hidden-properties))))
1759
1760 (defun gnus-article-unhide-text-type (b e type)
1761   "Unhide text of TYPE between B and E."
1762   (gnus-delete-wash-type type)
1763   (remove-text-properties
1764    b e (cons 'article-type (cons type gnus-hidden-properties)))
1765   (when (memq 'intangible gnus-hidden-properties)
1766     (put-text-property (max (1- b) (point-min))
1767                        b 'intangible nil)))
1768
1769 (defun gnus-article-hide-text-of-type (type)
1770   "Hide text of TYPE in the current buffer."
1771   (save-excursion
1772     (let ((b (point-min))
1773           (e (point-max)))
1774       (while (setq b (text-property-any b e 'article-type type))
1775         (add-text-properties b (incf b) gnus-hidden-properties)))))
1776
1777 (defun gnus-article-delete-text-of-type (type)
1778   "Delete text of TYPE in the current buffer."
1779   (save-excursion
1780     (let ((b (point-min)))
1781       (if (eq type 'multipart)
1782           ;; Remove MIME buttons associated with multipart/alternative parts.
1783           (progn
1784             (goto-char b)
1785             (while (if (get-text-property (point) 'gnus-part)
1786                        (setq b (point))
1787                      (when (setq b (next-single-property-change (point)
1788                                                                 'gnus-part))
1789                        (goto-char b)
1790                        t))
1791               (end-of-line)
1792               (skip-chars-forward "\n")
1793               (when (eq (get-text-property b 'article-type) 'multipart)
1794                 (delete-region b (point)))))
1795         (while (setq b (text-property-any b (point-max) 'article-type type))
1796           (delete-region
1797            b (or (text-property-not-all b (point-max) 'article-type type)
1798                  (point-max))))))))
1799
1800 (defun gnus-article-delete-invisible-text ()
1801   "Delete all invisible text in the current buffer."
1802   (save-excursion
1803     (let ((b (point-min)))
1804       (while (setq b (text-property-any b (point-max) 'invisible t))
1805         (delete-region
1806          b (or (text-property-not-all b (point-max) 'invisible t)
1807                (point-max)))))))
1808
1809 (defun gnus-article-text-type-exists-p (type)
1810   "Say whether any text of type TYPE exists in the buffer."
1811   (text-property-any (point-min) (point-max) 'article-type type))
1812
1813 (defsubst gnus-article-header-rank ()
1814   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1815   (let ((list gnus-sorted-header-list)
1816         (i 1))
1817     (while list
1818       (if (looking-at (car list))
1819           (setq list nil)
1820         (setq list (cdr list))
1821         (incf i)))
1822       i))
1823
1824 (defun article-hide-headers (&optional arg delete)
1825   "Hide unwanted headers and possibly sort them as well."
1826   (interactive)
1827   ;; This function might be inhibited.
1828   (unless gnus-inhibit-hiding
1829     (let ((inhibit-read-only t)
1830           (case-fold-search t)
1831           (max (1+ (length gnus-sorted-header-list)))
1832           (inhibit-point-motion-hooks t)
1833           (cur (current-buffer))
1834           ignored visible beg)
1835       (save-excursion
1836         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1837         ;; group parameters, so we should go to the summary buffer.
1838         (when (prog1
1839                   (condition-case nil
1840                       (progn (set-buffer gnus-summary-buffer) t)
1841                     (error nil))
1842                 (setq ignored (when (not gnus-visible-headers)
1843                                 (cond ((stringp gnus-ignored-headers)
1844                                        gnus-ignored-headers)
1845                                       ((listp gnus-ignored-headers)
1846                                        (mapconcat 'identity
1847                                                   gnus-ignored-headers
1848                                                   "\\|"))))
1849                       visible (cond ((stringp gnus-visible-headers)
1850                                      gnus-visible-headers)
1851                                     ((and gnus-visible-headers
1852                                           (listp gnus-visible-headers))
1853                                      (mapconcat 'identity
1854                                                 gnus-visible-headers
1855                                                 "\\|")))))
1856           (set-buffer cur))
1857         (save-restriction
1858           ;; First we narrow to just the headers.
1859           (article-narrow-to-head)
1860           ;; Hide any "From " lines at the beginning of (mail) articles.
1861           (while (looking-at "From ")
1862             (forward-line 1))
1863           (unless (bobp)
1864             (delete-region (point-min) (point)))
1865           ;; Then treat the rest of the header lines.
1866           ;; Then we use the two regular expressions
1867           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1868           ;; select which header lines is to remain visible in the
1869           ;; article buffer.
1870           (while (re-search-forward "^[^ \t:]*:" nil t)
1871             (beginning-of-line)
1872             ;; Mark the rank of the header.
1873             (put-text-property
1874              (point) (1+ (point)) 'message-rank
1875              (if (or (and visible (looking-at visible))
1876                      (and ignored
1877                           (not (looking-at ignored))))
1878                  (gnus-article-header-rank)
1879                (+ 2 max)))
1880             (forward-line 1))
1881           (message-sort-headers-1)
1882           (when (setq beg (text-property-any
1883                            (point-min) (point-max) 'message-rank (+ 2 max)))
1884             ;; We delete the unwanted headers.
1885             (gnus-add-wash-type 'headers)
1886             (add-text-properties (point-min) (+ 5 (point-min))
1887                                  '(article-type headers dummy-invisible t))
1888             (delete-region beg (point-max))))))))
1889
1890 (defun article-hide-boring-headers (&optional arg)
1891   "Toggle hiding of headers that aren't very interesting.
1892 If given a negative prefix, always show; if given a positive prefix,
1893 always hide."
1894   (interactive (gnus-article-hidden-arg))
1895   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1896              (not gnus-show-all-headers))
1897     (save-excursion
1898       (save-restriction
1899         (let ((inhibit-read-only t)
1900               (inhibit-point-motion-hooks t))
1901           (article-narrow-to-head)
1902           (dolist (elem gnus-boring-article-headers)
1903             (goto-char (point-min))
1904             (cond
1905              ;; Hide empty headers.
1906              ((eq elem 'empty)
1907               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1908                 (forward-line -1)
1909                 (gnus-article-hide-text-type
1910                  (point-at-bol)
1911                  (progn
1912                    (end-of-line)
1913                    (if (re-search-forward "^[^ \t]" nil t)
1914                        (match-beginning 0)
1915                      (point-max)))
1916                  'boring-headers)))
1917              ;; Hide boring Newsgroups header.
1918              ((eq elem 'newsgroups)
1919               (when (gnus-string-equal
1920                      (gnus-fetch-field "newsgroups")
1921                      (gnus-group-real-name
1922                       (if (boundp 'gnus-newsgroup-name)
1923                           gnus-newsgroup-name
1924                         "")))
1925                 (gnus-article-hide-header "newsgroups")))
1926              ((eq elem 'to-address)
1927               (let ((to (message-fetch-field "to"))
1928                     (to-address
1929                      (gnus-parameter-to-address
1930                       (if (boundp 'gnus-newsgroup-name)
1931                           gnus-newsgroup-name ""))))
1932                 (when (and to to-address
1933                            (ignore-errors
1934                              (gnus-string-equal
1935                               ;; only one address in To
1936                               (nth 1 (mail-extract-address-components to))
1937                               to-address)))
1938                   (gnus-article-hide-header "to"))))
1939              ((eq elem 'to-list)
1940               (let ((to (message-fetch-field "to"))
1941                     (to-list
1942                      (gnus-parameter-to-list
1943                       (if (boundp 'gnus-newsgroup-name)
1944                           gnus-newsgroup-name ""))))
1945                 (when (and to to-list
1946                            (ignore-errors
1947                              (gnus-string-equal
1948                               ;; only one address in To
1949                               (nth 1 (mail-extract-address-components to))
1950                               to-list)))
1951                   (gnus-article-hide-header "to"))))
1952              ((eq elem 'cc-list)
1953               (let ((cc (message-fetch-field "cc"))
1954                     (to-list
1955                      (gnus-parameter-to-list
1956                       (if (boundp 'gnus-newsgroup-name)
1957                           gnus-newsgroup-name ""))))
1958                 (when (and cc to-list
1959                            (ignore-errors
1960                              (gnus-string-equal
1961                               ;; only one address in CC
1962                               (nth 1 (mail-extract-address-components cc))
1963                               to-list)))
1964                   (gnus-article-hide-header "cc"))))
1965              ((eq elem 'followup-to)
1966               (when (gnus-string-equal
1967                      (message-fetch-field "followup-to")
1968                      (message-fetch-field "newsgroups"))
1969                 (gnus-article-hide-header "followup-to")))
1970              ((eq elem 'reply-to)
1971               (if (gnus-group-find-parameter
1972                    gnus-newsgroup-name 'broken-reply-to)
1973                   (gnus-article-hide-header "reply-to")
1974                 (let ((from (message-fetch-field "from"))
1975                       (reply-to (message-fetch-field "reply-to")))
1976                   (when
1977                       (and
1978                        from reply-to
1979                        (ignore-errors
1980                          (equal
1981                           (sort (mapcar
1982                                  (lambda (x) (downcase (cadr x)))
1983                                  (mail-extract-address-components from t))
1984                                 'string<)
1985                           (sort (mapcar
1986                                  (lambda (x) (downcase (cadr x)))
1987                                  (mail-extract-address-components reply-to t))
1988                                 'string<))))
1989                     (gnus-article-hide-header "reply-to")))))
1990              ((eq elem 'date)
1991               (let ((date (with-current-buffer gnus-original-article-buffer
1992                             ;; If date in `gnus-article-buffer' is localized
1993                             ;; (`gnus-treat-date-user-defined'),
1994                             ;; `days-between' might fail.
1995                             (message-fetch-field "date"))))
1996                 (when (and date
1997                            (< (days-between (current-time-string) date)
1998                               4))
1999                   (gnus-article-hide-header "date"))))
2000              ((eq elem 'long-to)
2001               (let ((to (message-fetch-field "to"))
2002                     (cc (message-fetch-field "cc")))
2003                 (when (> (length to) 1024)
2004                   (gnus-article-hide-header "to"))
2005                 (when (> (length cc) 1024)
2006                   (gnus-article-hide-header "cc"))))
2007              ((eq elem 'many-to)
2008               (let ((to-count 0)
2009                     (cc-count 0))
2010                 (goto-char (point-min))
2011                 (while (re-search-forward "^to:" nil t)
2012                   (setq to-count (1+ to-count)))
2013                 (when (> to-count 1)
2014                   (while (> to-count 0)
2015                     (goto-char (point-min))
2016                     (save-restriction
2017                       (re-search-forward "^to:" nil nil to-count)
2018                       (forward-line -1)
2019                       (narrow-to-region (point) (point-max))
2020                       (gnus-article-hide-header "to"))
2021                     (setq to-count (1- to-count))))
2022                 (goto-char (point-min))
2023                 (while (re-search-forward "^cc:" nil t)
2024                   (setq cc-count (1+ cc-count)))
2025                 (when (> cc-count 1)
2026                   (while (> cc-count 0)
2027                     (goto-char (point-min))
2028                     (save-restriction
2029                       (re-search-forward "^cc:" nil nil cc-count)
2030                       (forward-line -1)
2031                       (narrow-to-region (point) (point-max))
2032                       (gnus-article-hide-header "cc"))
2033                     (setq cc-count (1- cc-count)))))))))))))
2034
2035 (defun gnus-article-hide-header (header)
2036   (save-excursion
2037     (goto-char (point-min))
2038     (when (re-search-forward (concat "^" header ":") nil t)
2039       (gnus-article-hide-text-type
2040        (point-at-bol)
2041        (progn
2042          (end-of-line)
2043          (if (re-search-forward "^[^ \t]" nil t)
2044              (match-beginning 0)
2045            (point-max)))
2046        'boring-headers))))
2047
2048 (defvar gnus-article-normalized-header-length 40
2049   "Length of normalized headers.")
2050
2051 (defun article-normalize-headers ()
2052   "Make all header lines 40 characters long."
2053   (interactive)
2054   (let ((inhibit-read-only t)
2055         column)
2056     (save-excursion
2057       (save-restriction
2058         (article-narrow-to-head)
2059         (while (not (eobp))
2060           (cond
2061            ((< (setq column (- (point-at-eol) (point)))
2062                gnus-article-normalized-header-length)
2063             (end-of-line)
2064             (insert (make-string
2065                      (- gnus-article-normalized-header-length column)
2066                      ? )))
2067            ((> column gnus-article-normalized-header-length)
2068             (gnus-put-text-property
2069              (progn
2070                (forward-char gnus-article-normalized-header-length)
2071                (point))
2072              (point-at-eol)
2073              'invisible t))
2074            (t
2075             ;; Do nothing.
2076             ))
2077           (forward-line 1))))))
2078
2079 (defun article-treat-dumbquotes ()
2080   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2081 Note that this function guesses whether a character is a sm*rtq**t* or
2082 not, so it should only be used interactively.
2083
2084 Sm*rtq**t*s are M****s***'s unilateral extension to the
2085 iso-8859-1 character map in an attempt to provide more quoting
2086 characters.  If you see something like \\222 or \\264 where
2087 you're expecting some kind of apostrophe or quotation mark, then
2088 try this wash."
2089   (interactive)
2090   (article-translate-strings gnus-article-dumbquotes-map))
2091
2092 (defun article-translate-characters (from to)
2093   "Translate all characters in the body of the article according to FROM and TO.
2094 FROM is a string of characters to translate from; to is a string of
2095 characters to translate to."
2096   (save-excursion
2097     (when (article-goto-body)
2098       (let ((inhibit-read-only t)
2099             (x (make-string 225 ?x))
2100             (i -1))
2101         (while (< (incf i) (length x))
2102           (aset x i i))
2103         (setq i 0)
2104         (while (< i (length from))
2105           (aset x (aref from i) (aref to i))
2106           (incf i))
2107         (translate-region (point) (point-max) x)))))
2108
2109 (defun article-translate-strings (map)
2110   "Translate all string in the body of the article according to MAP.
2111 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2112   (save-excursion
2113     (when (article-goto-body)
2114       (let ((inhibit-read-only t))
2115         (dolist (elem map)
2116           (save-excursion
2117             (while (search-forward (car elem) nil t)
2118               (replace-match (cadr elem)))))))))
2119
2120 (defun article-treat-overstrike ()
2121   "Translate overstrikes into bold text."
2122   (interactive)
2123   (save-excursion
2124     (when (article-goto-body)
2125       (let ((inhibit-read-only t))
2126         (while (search-forward "\b" nil t)
2127           (let ((next (char-after))
2128                 (previous (char-after (- (point) 2))))
2129             ;; We do the boldification/underlining by hiding the
2130             ;; overstrikes and putting the proper text property
2131             ;; on the letters.
2132             (cond
2133              ((eq next previous)
2134               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2135               (put-text-property (point) (1+ (point)) 'face 'bold))
2136              ((eq next ?_)
2137               (gnus-article-hide-text-type
2138                (1- (point)) (1+ (point)) 'overstrike)
2139               (put-text-property
2140                (- (point) 2) (1- (point)) 'face 'underline))
2141              ((eq previous ?_)
2142               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2143               (put-text-property
2144                (point) (1+ (point)) 'face 'underline)))))))))
2145
2146 (defun article-treat-ansi-sequences ()
2147   "Translate ANSI SGR control sequences into overlays or extents."
2148   (interactive)
2149   (save-excursion
2150     (when (article-goto-body)
2151       (let ((inhibit-read-only t))
2152         (ansi-color-apply-on-region (point) (point-max))))))
2153
2154 (defun gnus-article-treat-unfold-headers ()
2155   "Unfold folded message headers.
2156 Only the headers that fit into the current window width will be
2157 unfolded."
2158   (interactive)
2159   (gnus-with-article-headers
2160     (let (length)
2161       (while (not (eobp))
2162         (save-restriction
2163           (mail-header-narrow-to-field)
2164           (let* ((header (buffer-string))
2165                  (unfoldable
2166                   (or (equal gnus-article-unfold-long-headers t)
2167                       (and (stringp gnus-article-unfold-long-headers)
2168                            (string-match gnus-article-unfold-long-headers header)))))
2169             (with-temp-buffer
2170               (insert header)
2171               (goto-char (point-min))
2172               (while (re-search-forward "\n[\t ]" nil t)
2173                 (replace-match " " t t)))
2174             (setq length (- (point-max) (point-min) 1))
2175             (when (or unfoldable
2176                       (< length (window-width)))
2177               (while (re-search-forward "\n[\t ]" nil t)
2178                 (replace-match " " t t))))
2179           (goto-char (point-max)))))))
2180
2181 (defun gnus-article-treat-fold-headers ()
2182   "Fold message headers."
2183   (interactive)
2184   (gnus-with-article-headers
2185     (while (not (eobp))
2186       (save-restriction
2187         (mail-header-narrow-to-field)
2188         (mail-header-fold-field)
2189         (goto-char (point-max))))))
2190
2191 (defun gnus-treat-smiley ()
2192   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2193   (interactive)
2194   (gnus-with-article-buffer
2195     (if (memq 'smiley gnus-article-wash-types)
2196         (gnus-delete-images 'smiley)
2197       (article-goto-body)
2198       (let ((images (smiley-region (point) (point-max))))
2199         (when images
2200           (gnus-add-wash-type 'smiley)
2201           (dolist (image images)
2202             (gnus-add-image 'smiley image)))))))
2203
2204 (defun gnus-article-remove-images ()
2205   "Remove all images from the article buffer."
2206   (interactive)
2207   (gnus-with-article-buffer
2208     (dolist (elem gnus-article-image-alist)
2209       (gnus-delete-images (car elem)))))
2210
2211 (defun gnus-article-treat-fold-newsgroups ()
2212   "Unfold folded message headers.
2213 Only the headers that fit into the current window width will be
2214 unfolded."
2215   (interactive)
2216   (gnus-with-article-headers
2217     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2218       (save-restriction
2219         (mail-header-narrow-to-field)
2220         (while (re-search-forward ", *" nil t)
2221           (replace-match ", " t t))
2222         (mail-header-fold-field)
2223         (goto-char (point-max))))))
2224
2225 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2226   "Value of `truncate-lines' in Gnus Article buffer.
2227 Valid values are nil, t, `head', `first', `last', an integer or a
2228 predicate.  See Info node `(gnus)Customizing Articles'."
2229   :version "23.1" ;; No Gnus
2230   :group 'gnus-article
2231   ;; :link '(custom-manual "(gnus)Customizing Articles")
2232   :type 'boolean)
2233
2234 (defun gnus-article-toggle-truncate-lines (&optional arg)
2235   "Toggle whether to fold or truncate long lines in article the buffer.
2236 If ARG is non-nil and not a number, toggle
2237 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2238 long lines if and only if arg is positive."
2239   (interactive "P")
2240   (cond
2241    ((and (numberp arg) (> arg 0))
2242     (setq gnus-article-truncate-lines t))
2243    ((numberp arg)
2244     (setq gnus-article-truncate-lines nil))
2245    (arg
2246     (setq gnus-article-truncate-lines
2247           (not gnus-article-truncate-lines))))
2248   (gnus-with-article-buffer
2249     (cond
2250      ((and (numberp arg) (> arg 0))
2251       (setq truncate-lines nil))
2252      ((numberp arg)
2253       (setq truncate-lines t)))
2254     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2255     ;; `toggle-truncate-lines' needs an argument.
2256     (toggle-truncate-lines)))
2257
2258 (defun gnus-article-treat-body-boundary ()
2259   "Place a boundary line at the end of the headers."
2260   (interactive)
2261   (when (and gnus-body-boundary-delimiter
2262              (> (length gnus-body-boundary-delimiter) 0))
2263     (gnus-with-article-headers
2264       (goto-char (point-max))
2265       (let ((start (point)))
2266         (insert "X-Boundary: ")
2267         (gnus-add-text-properties start (point) '(invisible t intangible t))
2268         (insert (let (str)
2269                   (while (>= (1- (window-width)) (length str))
2270                     (setq str (concat str gnus-body-boundary-delimiter)))
2271                   (substring str 0 (1- (window-width))))
2272                 "\n")
2273         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2274
2275 (defun article-fill-long-lines ()
2276   "Fill lines that are wider than the window width."
2277   (interactive)
2278   (save-excursion
2279     (let ((inhibit-read-only t)
2280           (width (window-width (get-buffer-window (current-buffer)))))
2281       (save-restriction
2282         (article-goto-body)
2283         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2284           (while (not (eobp))
2285             (end-of-line)
2286             (when (>= (current-column) (min fill-column width))
2287               (narrow-to-region (min (1+ (point)) (point-max))
2288                                 (point-at-bol))
2289               (let ((goback (point-marker)))
2290                 (fill-paragraph nil)
2291                 (goto-char (marker-position goback)))
2292               (widen))
2293             (forward-line 1)))))))
2294
2295 (defun article-capitalize-sentences ()
2296   "Capitalize the first word in each sentence."
2297   (interactive)
2298   (save-excursion
2299     (let ((inhibit-read-only t)
2300           (paragraph-start "^[\n\^L]"))
2301       (article-goto-body)
2302       (while (not (eobp))
2303         (capitalize-word 1)
2304         (forward-sentence)))))
2305
2306 (defun article-remove-cr ()
2307   "Remove trailing CRs and then translate remaining CRs into LFs."
2308   (interactive)
2309   (save-excursion
2310     (let ((inhibit-read-only t))
2311       (goto-char (point-min))
2312       (while (re-search-forward "\r+$" nil t)
2313         (replace-match "" t t))
2314       (goto-char (point-min))
2315       (while (search-forward "\r" nil t)
2316         (replace-match "\n" t t)))))
2317
2318 (defun article-remove-trailing-blank-lines ()
2319   "Remove all trailing blank lines from the article."
2320   (interactive)
2321   (save-excursion
2322     (let ((inhibit-read-only t))
2323       (goto-char (point-max))
2324       (delete-region
2325        (point)
2326        (progn
2327          (while (and (not (bobp))
2328                      (looking-at "^[ \t]*$")
2329                      (not (gnus-annotation-in-region-p
2330                            (point) (point-at-eol))))
2331            (forward-line -1))
2332          (forward-line 1)
2333          (point))))))
2334
2335 (defvar gnus-face-properties-alist)
2336
2337 (defun article-display-face (&optional force)
2338   "Display any Face headers in the header."
2339   (interactive (list 'force))
2340   (let ((wash-face-p buffer-read-only))
2341     (gnus-with-article-headers
2342       ;; When displaying parts, this function can be called several times on
2343       ;; the same article, without any intended toggle semantic (as typing `W
2344       ;; D d' would have). So face deletion must occur only when we come from
2345       ;; an interactive command, that is when the *Article* buffer is
2346       ;; read-only.
2347       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2348           (gnus-delete-images 'face)
2349         (let ((from (message-fetch-field "from"))
2350               face faces)
2351           (save-current-buffer
2352             (when (and wash-face-p
2353                        (gnus-buffer-live-p gnus-original-article-buffer)
2354                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2355               (set-buffer gnus-original-article-buffer))
2356             (save-restriction
2357               (mail-narrow-to-head)
2358               (when (or force
2359                         ;; Check whether this face is censored.
2360                         (not (and gnus-article-x-face-too-ugly
2361                                   (or from
2362                                       (setq from (message-fetch-field "from")))
2363                                   (string-match gnus-article-x-face-too-ugly
2364                                                 from))))
2365                 (while (gnus-article-goto-header "Face")
2366                   (push (mail-header-field-value) faces)))))
2367           (when faces
2368             (goto-char (point-min))
2369             (let (png image)
2370               (unless (setq from (gnus-article-goto-header "from"))
2371                 (insert "From:")
2372                 (setq from (point))
2373                 (insert " [no `from' set]\n"))
2374               (while faces
2375                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2376                   (setq image
2377                         (apply 'gnus-create-image png 'png t
2378                                (cdr (assq 'png gnus-face-properties-alist))))
2379                   (goto-char from)
2380                   (gnus-add-wash-type 'face)
2381                   (gnus-add-image 'face image)
2382                   (gnus-put-image image nil 'face))))))))))
2383
2384 (defun article-display-x-face (&optional force)
2385   "Look for an X-Face header and display it if present."
2386   (interactive (list 'force))
2387   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2388     (gnus-with-article-headers
2389       ;; Delete the old process, if any.
2390       (when (process-status "article-x-face")
2391         (delete-process "article-x-face"))
2392       ;; See the comment in `article-display-face'.
2393       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2394           ;; We have already displayed X-Faces, so we remove them
2395           ;; instead.
2396           (gnus-delete-images 'xface)
2397         ;; Display X-Faces.
2398         (let ((from (message-fetch-field "from"))
2399               x-faces face)
2400           (save-current-buffer
2401             (when (and wash-face-p
2402                        (gnus-buffer-live-p gnus-original-article-buffer)
2403                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2404               ;; If type `W f', use gnus-original-article-buffer,
2405               ;; otherwise use the current buffer because displaying
2406               ;; RFC822 parts calls this function too.
2407               (set-buffer gnus-original-article-buffer))
2408             (save-restriction
2409               (mail-narrow-to-head)
2410               (and gnus-article-x-face-command
2411                    (or force
2412                        ;; Check whether this face is censored.
2413                        (not (and gnus-article-x-face-too-ugly
2414                                  (or from
2415                                      (setq from (message-fetch-field "from")))
2416                                  (string-match gnus-article-x-face-too-ugly
2417                                                from))))
2418                    (while (gnus-article-goto-header "X-Face")
2419                      (push (mail-header-field-value) x-faces)))))
2420           (when x-faces
2421             ;; We display the face.
2422             (cond ((functionp gnus-article-x-face-command)
2423                    ;; The command is a lisp function, so we call it.
2424                    (mapc gnus-article-x-face-command x-faces))
2425                   ((stringp gnus-article-x-face-command)
2426                    ;; The command is a string, so we interpret the command
2427                    ;; as a, well, command, and fork it off.
2428                    (let ((process-connection-type nil))
2429                      (gnus-set-process-query-on-exit-flag
2430                       (start-process
2431                        "article-x-face" nil shell-file-name
2432                        shell-command-switch gnus-article-x-face-command)
2433                       nil)
2434                      ;; Sending multiple EOFs to xv doesn't work,
2435                      ;; so we only do a single external face.
2436                      (with-temp-buffer
2437                        (insert (car x-faces))
2438                        (process-send-region "article-x-face"
2439                                             (point-min) (point-max)))
2440                      (process-send-eof "article-x-face")))
2441                   (t
2442                    (error "`%s' set to `%s' is not a function"
2443                           gnus-article-x-face-command
2444                           'gnus-article-x-face-command)))))))))
2445
2446 (defun article-decode-mime-words ()
2447   "Decode all MIME-encoded words in the article."
2448   (interactive)
2449   (gnus-with-article-buffer
2450     (let ((inhibit-point-motion-hooks t)
2451           (mail-parse-charset gnus-newsgroup-charset)
2452           (mail-parse-ignored-charsets
2453            (with-current-buffer gnus-summary-buffer
2454              gnus-newsgroup-ignored-charsets)))
2455       (mail-decode-encoded-word-region (point-min) (point-max)))))
2456
2457 (defun article-decode-charset (&optional prompt)
2458   "Decode charset-encoded text in the article.
2459 If PROMPT (the prefix), prompt for a coding system to use."
2460   (interactive "P")
2461   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2462         (inhibit-read-only t)
2463         (mail-parse-charset gnus-newsgroup-charset)
2464         (mail-parse-ignored-charsets
2465          (save-excursion (condition-case nil
2466                              (set-buffer gnus-summary-buffer)
2467                            (error))
2468                          gnus-newsgroup-ignored-charsets))
2469         ct cte ctl charset format)
2470     (save-excursion
2471       (save-restriction
2472         (article-narrow-to-head)
2473         (setq ct (message-fetch-field "Content-Type" t)
2474               cte (message-fetch-field "Content-Transfer-Encoding" t)
2475               ctl (and ct (mail-header-parse-content-type ct))
2476               charset (cond
2477                        (prompt
2478                         (mm-read-coding-system "Charset to decode: "))
2479                        (ctl
2480                         (mail-content-type-get ctl 'charset)))
2481               format (and ctl (mail-content-type-get ctl 'format)))
2482         (when cte
2483           (setq cte (mail-header-strip cte)))
2484         (if (and ctl (not (string-match "/" (car ctl))))
2485             (setq ctl nil))
2486         (goto-char (point-max)))
2487       (forward-line 1)
2488       (save-restriction
2489         (narrow-to-region (point) (point-max))
2490         (when (and (eq mail-parse-charset 'gnus-decoded)
2491                    (eq (mm-body-7-or-8) '8bit))
2492           ;; The text code could have been decoded.
2493           (setq charset mail-parse-charset))
2494         (when (and (or (not ctl)
2495                        (equal (car ctl) "text/plain"))
2496                    (not format)) ;; article with format will decode later.
2497           (mm-decode-body
2498            charset (and cte (intern (downcase
2499                                      (gnus-strip-whitespace cte))))
2500            (car ctl)))))))
2501
2502 (defun article-decode-encoded-words ()
2503   "Remove encoded-word encoding from headers."
2504   (let ((inhibit-point-motion-hooks t)
2505         (mail-parse-charset gnus-newsgroup-charset)
2506         (mail-parse-ignored-charsets
2507          (save-excursion (condition-case nil
2508                              (set-buffer gnus-summary-buffer)
2509                            (error))
2510                          gnus-newsgroup-ignored-charsets))
2511         (inhibit-read-only t)
2512         end start)
2513     (goto-char (point-min))
2514     (when (search-forward "\n\n" nil 'move)
2515       (forward-line -1))
2516     (setq end (point))
2517     (while (not (bobp))
2518       (while (progn
2519                (forward-line -1)
2520                (and (not (bobp))
2521                     (memq (char-after) '(?\t ? )))))
2522       (setq start (point))
2523       (if (looking-at "\
2524 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2525 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2526           (funcall gnus-decode-address-function start end)
2527         (funcall gnus-decode-header-function start end))
2528       (goto-char (setq end start)))))
2529
2530 (defun article-decode-group-name ()
2531   "Decode group names in Newsgroups, Followup-To and Xref headers."
2532   (let ((inhibit-point-motion-hooks t)
2533         (inhibit-read-only t)
2534         (method (gnus-find-method-for-group gnus-newsgroup-name))
2535         regexp)
2536     (when (and (or gnus-group-name-charset-method-alist
2537                    gnus-group-name-charset-group-alist)
2538                (gnus-buffer-live-p gnus-original-article-buffer))
2539       (save-restriction
2540         (article-narrow-to-head)
2541         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2542           (with-current-buffer gnus-original-article-buffer
2543             (goto-char (point-min)))
2544           (setq regexp (concat "^" header
2545                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2546           (while (re-search-forward regexp nil t)
2547             (replace-match (save-match-data
2548                              (gnus-decode-newsgroups
2549                               ;; XXX how to use data in article buffer?
2550                               (with-current-buffer gnus-original-article-buffer
2551                                 (re-search-forward regexp nil t)
2552                                 (match-string 1))
2553                               gnus-newsgroup-name method))
2554                            t t nil 1))
2555           (goto-char (point-min)))))))
2556
2557 (autoload 'idna-to-unicode "idna")
2558
2559 (defun article-decode-idna-rhs ()
2560   "Decode IDNA strings in RHS in various headers in current buffer.
2561 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2562 Mail-Reply-To: and Mail-Followup-To:."
2563   (when gnus-use-idna
2564     (save-restriction
2565       (let ((inhibit-point-motion-hooks t)
2566             (inhibit-read-only t))
2567         (article-narrow-to-head)
2568         (goto-char (point-min))
2569         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2570           (let (ace unicode)
2571             (when (save-match-data
2572                     (and (setq ace (match-string 1))
2573                          (save-excursion
2574                            (and (re-search-backward "^[^ \t]" nil t)
2575                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2576                          (setq unicode (idna-to-unicode ace))))
2577               (unless (string= ace unicode)
2578                 (replace-match unicode nil nil nil 1)))))))))
2579
2580 (defun article-de-quoted-unreadable (&optional force read-charset)
2581   "Translate a quoted-printable-encoded article.
2582 If FORCE, decode the article whether it is marked as quoted-printable
2583 or not.
2584 If READ-CHARSET, ask for a coding system."
2585   (interactive (list 'force current-prefix-arg))
2586   (save-excursion
2587     (let ((inhibit-read-only t) type charset)
2588       (if (gnus-buffer-live-p gnus-original-article-buffer)
2589           (with-current-buffer gnus-original-article-buffer
2590             (setq type
2591                   (gnus-fetch-field "content-transfer-encoding"))
2592             (let* ((ct (gnus-fetch-field "content-type"))
2593                    (ctl (and ct (mail-header-parse-content-type ct))))
2594               (setq charset (and ctl
2595                                  (mail-content-type-get ctl 'charset)))
2596               (if (stringp charset)
2597                   (setq charset (intern (downcase charset)))))))
2598       (if read-charset
2599           (setq charset (mm-read-coding-system "Charset: " charset)))
2600       (unless charset
2601         (setq charset gnus-newsgroup-charset))
2602       (when (or force
2603                 (and type (let ((case-fold-search t))
2604                             (string-match "quoted-printable" type))))
2605         (article-goto-body)
2606         (quoted-printable-decode-region
2607          (point) (point-max) (mm-charset-to-coding-system charset))))))
2608
2609 (defun article-de-base64-unreadable (&optional force read-charset)
2610   "Translate a base64 article.
2611 If FORCE, decode the article whether it is marked as base64 not.
2612 If READ-CHARSET, ask for a coding system."
2613   (interactive (list 'force current-prefix-arg))
2614   (save-excursion
2615     (let ((inhibit-read-only t) type charset)
2616       (if (gnus-buffer-live-p gnus-original-article-buffer)
2617           (with-current-buffer gnus-original-article-buffer
2618             (setq type
2619                   (gnus-fetch-field "content-transfer-encoding"))
2620             (let* ((ct (gnus-fetch-field "content-type"))
2621                    (ctl (and ct (mail-header-parse-content-type ct))))
2622               (setq charset (and ctl
2623                                  (mail-content-type-get ctl 'charset)))
2624               (if (stringp charset)
2625                   (setq charset (intern (downcase charset)))))))
2626       (if read-charset
2627           (setq charset (mm-read-coding-system "Charset: " charset)))
2628       (unless charset
2629         (setq charset gnus-newsgroup-charset))
2630       (when (or force
2631                 (and type (let ((case-fold-search t))
2632                             (string-match "base64" type))))
2633         (article-goto-body)
2634         (save-restriction
2635           (narrow-to-region (point) (point-max))
2636           (base64-decode-region (point-min) (point-max))
2637           (mm-decode-coding-region
2638            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2639
2640 (eval-when-compile
2641   (require 'rfc1843))
2642
2643 (defun article-decode-HZ ()
2644   "Translate a HZ-encoded article."
2645   (interactive)
2646   (require 'rfc1843)
2647   (save-excursion
2648     (let ((inhibit-read-only t))
2649       (rfc1843-decode-region (point-min) (point-max)))))
2650
2651 (defun article-unsplit-urls ()
2652   "Remove the newlines that some other mailers insert into URLs."
2653   (interactive)
2654   (save-excursion
2655     (let ((inhibit-read-only t))
2656       (goto-char (point-min))
2657       (while (re-search-forward
2658               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2659         (replace-match "\\1\\3" t)))
2660     (when (interactive-p)
2661       (gnus-treat-article nil))))
2662
2663
2664 (defun article-wash-html (&optional read-charset)
2665   "Format an HTML article.
2666 If READ-CHARSET, ask for a coding system.  If it is a number, the
2667 charset defined in `gnus-summary-show-article-charset-alist' is used."
2668   (interactive "P")
2669   (save-excursion
2670     (let ((inhibit-read-only t)
2671           charset)
2672       (if read-charset
2673           (if (or (and (numberp read-charset)
2674                        (setq charset
2675                              (cdr
2676                               (assq read-charset
2677                                     gnus-summary-show-article-charset-alist))))
2678                   (setq charset (mm-read-coding-system "Charset: ")))
2679               (let ((gnus-summary-show-article-charset-alist
2680                      (list (cons 1 charset))))
2681                 (with-current-buffer gnus-summary-buffer
2682                   (gnus-summary-show-article 1)))
2683             (error "No charset is given"))
2684         (when (gnus-buffer-live-p gnus-original-article-buffer)
2685           (with-current-buffer gnus-original-article-buffer
2686             (let* ((ct (gnus-fetch-field "content-type"))
2687                    (ctl (and ct (mail-header-parse-content-type ct))))
2688               (setq charset (and ctl
2689                                  (mail-content-type-get ctl 'charset)))
2690               (when (stringp charset)
2691                 (setq charset (intern (downcase charset)))))))
2692         (unless charset
2693           (setq charset gnus-newsgroup-charset)))
2694       (article-goto-body)
2695       (save-window-excursion
2696         (save-restriction
2697           (narrow-to-region (point) (point-max))
2698           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2699                  (entry (assq func mm-text-html-washer-alist)))
2700             (when entry
2701               (setq func (cdr entry)))
2702             (cond
2703              ((functionp func)
2704               (funcall func))
2705              (t
2706               (apply (car func) (cdr func))))))))))
2707
2708 ;; External.
2709 (declare-function w3-region "ext:w3-display" (st nd))
2710
2711 (defun gnus-article-wash-html-with-w3 ()
2712   "Wash the current buffer with w3."
2713   (mm-setup-w3)
2714   (let ((w3-strict-width (window-width))
2715         (url-standalone-mode t)
2716         (url-gateway-unplugged t)
2717         (w3-honor-stylesheets nil))
2718     (condition-case ()
2719         (w3-region (point-min) (point-max))
2720       (error))))
2721
2722 ;; External.
2723 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2724
2725 (defun gnus-article-wash-html-with-w3m ()
2726   "Wash the current buffer with emacs-w3m."
2727   (mm-setup-w3m)
2728   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2729         w3m-force-redisplay)
2730     (w3m-region (point-min) (point-max)))
2731   ;; Put the mark meaning this part was rendered by emacs-w3m.
2732   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2733   (when (and mm-inline-text-html-with-w3m-keymap
2734              (boundp 'w3m-minor-mode-map)
2735              w3m-minor-mode-map)
2736     (if (and (boundp 'w3m-link-map)
2737              w3m-link-map)
2738         (let* ((start (point-min))
2739                (end (point-max))
2740                (on (get-text-property start 'w3m-href-anchor))
2741                (map (copy-keymap w3m-link-map))
2742                next)
2743           (set-keymap-parent map w3m-minor-mode-map)
2744           (while (< start end)
2745             (if on
2746                 (progn
2747                   (setq next (or (text-property-any start end
2748                                                     'w3m-href-anchor nil)
2749                                  end))
2750                   (put-text-property start next 'keymap map))
2751               (setq next (or (text-property-not-all start end
2752                                                     'w3m-href-anchor nil)
2753                              end))
2754               (put-text-property start next 'keymap w3m-minor-mode-map))
2755             (setq start next
2756                   on (not on))))
2757       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2758
2759 (defvar charset) ;; Bound by `article-wash-html'.
2760
2761 (defun gnus-article-wash-html-with-w3m-standalone ()
2762   "Wash the current buffer with w3m."
2763   (if (mm-w3m-standalone-supports-m17n-p)
2764       (progn
2765         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2766           ;; The default.
2767           (setq charset 'iso-8859-1))
2768         (let ((coding-system-for-write charset)
2769               (coding-system-for-read charset))
2770           (call-process-region
2771            (point-min) (point-max)
2772            "w3m" t t nil "-dump" "-T" "text/html"
2773            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2774     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2775
2776 (defvar gnus-article-browse-html-temp-list nil
2777   "List of temporary files created by `gnus-article-browse-html-parts'.
2778 Internal variable.")
2779
2780 (defcustom gnus-article-browse-delete-temp 'ask
2781   "What to do with temporary files from `gnus-article-browse-html-parts'.
2782 If nil, don't delete temporary files.  If it is t, delete them on
2783 exit from the summary buffer.  If it is the symbol `file', query
2784 on each file, if it is `ask' ask once when exiting from the
2785 summary buffer."
2786   :group 'gnus-article
2787   :version "23.1" ;; No Gnus
2788   :type '(choice (const :tag "Don't delete" nil)
2789                  (const :tag "Don't ask" t)
2790                  (const :tag "Ask" ask)
2791                  (const :tag "Ask for each file" file)))
2792
2793 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2794
2795 (defun gnus-article-browse-delete-temp-files (&optional how)
2796   "Delete temp-files created by `gnus-article-browse-html-parts'."
2797   (when (and gnus-article-browse-html-temp-list
2798              (or how
2799                  (setq how gnus-article-browse-delete-temp)))
2800     (when (and (eq how 'ask)
2801                (gnus-y-or-n-p (format
2802                                "Delete all %s temporary HTML file(s)? "
2803                                (length gnus-article-browse-html-temp-list)))
2804                (setq how t)))
2805     (dolist (file gnus-article-browse-html-temp-list)
2806       (when (and (file-exists-p file)
2807                  (or (eq how t)
2808                      ;; `how' is neither `nil', `ask' nor `t' (i.e. `file'):
2809                      (gnus-y-or-n-p
2810                       (format "Delete temporary HTML file `%s'? " file))))
2811         (delete-file file)))
2812     ;; Also remove file from the list when not deleted or if file doesn't
2813     ;; exist anymore.
2814     (setq gnus-article-browse-html-temp-list nil))
2815   gnus-article-browse-html-temp-list)
2816
2817 (defun gnus-article-browse-html-parts (list &optional header)
2818   "View all \"text/html\" parts from LIST.
2819 Recurse into multiparts.  The optional HEADER that should be a decoded
2820 message header will be added to the bodies of the \"text/html\" parts."
2821   ;; Internal function used by `gnus-article-browse-html-article'.
2822   (let (type file charset tmp-file showed)
2823     ;; Find and show the html-parts.
2824     (dolist (handle list)
2825       ;; If HTML, show it:
2826       (cond ((not (listp handle)))
2827             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2828                  (and (equal (car type) "message/external-body")
2829                       (or header
2830                           (setq file (or (mail-content-type-get type 'name)
2831                                          (mail-content-type-get
2832                                           (mm-handle-disposition handle)
2833                                           'filename))))
2834                       (or (mm-handle-cache handle)
2835                           (condition-case code
2836                               (progn (mm-extern-cache-contents handle) t)
2837                             (error
2838                              (gnus-message 3 "%s" (error-message-string code))
2839                              (when (>= gnus-verbose 3) (sit-for 2))
2840                              nil)))
2841                       (progn
2842                         (setq handle (mm-handle-cache handle)
2843                               type (mm-handle-type handle))
2844                         (equal (car type) "text/html"))))
2845              (when (or (setq charset (mail-content-type-get type 'charset))
2846                        header
2847                        (not file))
2848                (setq tmp-file (mm-make-temp-file
2849                                ;; Do we need to care for 8.3 filenames?
2850                                "mm-" nil ".html")))
2851              ;; Add a meta html tag to specify charset and a header.
2852              (cond
2853               (header
2854                (let (title eheader body hcharset coding)
2855                  (with-temp-buffer
2856                    (mm-enable-multibyte)
2857                    (setq case-fold-search t)
2858                    (insert header "\n")
2859                    (setq title (message-fetch-field "subject"))
2860                    (goto-char (point-min))
2861                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2862                      (replace-match (cond ((match-beginning 1) "&lt;")
2863                                           ((match-beginning 2) "&gt;")
2864                                           (t "&amp;"))))
2865                    (goto-char (point-min))
2866                    (insert "<pre>\n")
2867                    (goto-char (point-max))
2868                    (insert "</pre>\n<hr>\n")
2869                    ;; We have to examine charset one by one since
2870                    ;; charset specified in parts might be different.
2871                    (if (eq charset 'gnus-decoded)
2872                        (setq charset 'utf-8
2873                              eheader (mm-encode-coding-string (buffer-string)
2874                                                               charset)
2875                              title (when title
2876                                      (mm-encode-coding-string title charset))
2877                              body (mm-encode-coding-string (mm-get-part handle)
2878                                                            charset))
2879                      (setq hcharset (mm-find-mime-charset-region (point-min)
2880                                                                  (point-max)))
2881                      (cond ((= (length hcharset) 1)
2882                             (setq hcharset (car hcharset)
2883                                   coding (mm-charset-to-coding-system
2884                                           hcharset)))
2885                            ((> (length hcharset) 1)
2886                             (setq hcharset 'utf-8
2887                                   coding hcharset)))
2888                      (if coding
2889                          (if charset
2890                              (progn
2891                                (setq body
2892                                      (mm-charset-to-coding-system charset))
2893                                (if (eq coding body)
2894                                    (setq eheader (mm-encode-coding-string
2895                                                   (buffer-string) coding)
2896                                          title (when title
2897                                                  (mm-encode-coding-string
2898                                                   title coding))
2899                                          body (mm-get-part handle))
2900                                  (setq charset 'utf-8
2901                                        eheader (mm-encode-coding-string
2902                                                 (buffer-string) charset)
2903                                        title (when title
2904                                                (mm-encode-coding-string
2905                                                 title charset))
2906                                        body (mm-encode-coding-string
2907                                              (mm-decode-coding-string
2908                                               (mm-get-part handle) body)
2909                                              charset))))
2910                            (setq charset hcharset
2911                                  eheader (mm-encode-coding-string
2912                                           (buffer-string) coding)
2913                                  title (when title
2914                                          (mm-encode-coding-string
2915                                           title coding))
2916                                  body (mm-get-part handle)))
2917                        (setq eheader (mm-string-as-unibyte (buffer-string))
2918                              body (mm-get-part handle))))
2919                    (erase-buffer)
2920                    (mm-disable-multibyte)
2921                    (insert body)
2922                    (when charset
2923                      (mm-add-meta-html-tag handle charset))
2924                    (when title
2925                      (goto-char (point-min))
2926                      (unless (search-forward "<title>" nil t)
2927                        (re-search-forward "<head>\\s-*" nil t)
2928                        (insert "<title>" title "</title>\n")))
2929                    (goto-char (point-min))
2930                    (or (re-search-forward
2931                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2932                        (re-search-forward
2933                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2934                    (insert eheader)
2935                    (mm-write-region (point-min) (point-max)
2936                                     tmp-file nil nil nil 'binary t))))
2937               (charset
2938                (mm-with-unibyte-buffer
2939                  (insert (if (eq charset 'gnus-decoded)
2940                              (mm-encode-coding-string
2941                               (mm-get-part handle)
2942                               (setq charset 'utf-8))
2943                            (mm-get-part handle)))
2944                  (if (or (mm-add-meta-html-tag handle charset)
2945                          (not file))
2946                      (mm-write-region (point-min) (point-max)
2947                                       tmp-file nil nil nil 'binary t)
2948                    (setq tmp-file nil))))
2949               (tmp-file
2950                (mm-save-part-to-file handle tmp-file)))
2951              (when tmp-file
2952                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2953              (add-hook 'gnus-summary-prepare-exit-hook
2954                        'gnus-article-browse-delete-temp-files)
2955              (add-hook 'gnus-exit-gnus-hook
2956                        (lambda  ()
2957                          (gnus-article-browse-delete-temp-files t)))
2958              ;; FIXME: Warn if there's an <img> tag?
2959              (browse-url-of-file (or tmp-file (expand-file-name file)))
2960              (setq showed t))
2961             ;; If multipart, recurse
2962             ((equal (mm-handle-media-supertype handle) "multipart")
2963              (when (gnus-article-browse-html-parts handle header)
2964                (setq showed t)))
2965             ((equal (mm-handle-media-type handle) "message/rfc822")
2966              (mm-with-multibyte-buffer
2967                (mm-insert-part handle)
2968                (setq handle (mm-dissect-buffer t t))
2969                (when (and (bufferp (car handle))
2970                           (stringp (car (mm-handle-type handle))))
2971                  (setq handle (list handle)))
2972                (when header
2973                  (article-decode-encoded-words)
2974                  (let ((gnus-visible-headers
2975                         (or (get 'gnus-visible-headers 'standard-value)
2976                             gnus-visible-headers)))
2977                    (article-hide-headers))
2978                  (goto-char (point-min))
2979                  (search-forward "\n\n" nil 'move)
2980                  (skip-chars-backward "\t\n ")
2981                  (setq header (buffer-substring (point-min) (point)))))
2982              (when (prog1
2983                        (gnus-article-browse-html-parts handle header)
2984                      (mm-destroy-parts handle))
2985                (setq showed t)))))
2986     showed))
2987
2988 (defun gnus-article-browse-html-article (&optional arg)
2989   "View \"text/html\" parts of the current article with a WWW browser.
2990 The message header is added to the beginning of every html part unless
2991 the prefix argument ARG is given.
2992
2993 Warning: Spammers use links to images in HTML articles to verify
2994 whether you have read the message.  As
2995 `gnus-article-browse-html-article' passes the HTML content to the
2996 browser without eliminating these \"web bugs\" you should only
2997 use it for mails from trusted senders.
2998
2999 If you always want to display HTML parts in the browser, set
3000 `mm-text-html-renderer' to nil."
3001   ;; Cf. `mm-w3m-safe-url-regexp'
3002   (interactive "P")
3003   (if arg
3004       (gnus-summary-show-article)
3005     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3006                                     gnus-visible-headers))
3007           ;; As we insert a <hr>, there's no need for the body boundary.
3008           (gnus-treat-body-boundary nil))
3009       (gnus-summary-show-article)))
3010   (with-current-buffer gnus-article-buffer
3011     (let ((header (unless arg
3012                     (save-restriction
3013                       (widen)
3014                       (buffer-substring-no-properties
3015                        (goto-char (point-min))
3016                        (if (search-forward "\n\n" nil t)
3017                            (match-beginning 0)
3018                          (goto-char (point-max))
3019                          (skip-chars-backward "\t\n ")
3020                          (point))))))
3021           parts)
3022       (set-buffer gnus-original-article-buffer)
3023       (setq parts (mm-dissect-buffer t t))
3024       ;; If singlepart, enforce a list.
3025       (when (and (bufferp (car parts))
3026                  (stringp (car (mm-handle-type parts))))
3027         (setq parts (list parts)))
3028       ;; Process the list
3029       (unless (gnus-article-browse-html-parts parts header)
3030         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3031       (mm-destroy-parts parts)
3032       (unless arg
3033         (gnus-summary-show-article)))))
3034
3035 (defun article-hide-list-identifiers ()
3036   "Remove list identifies from the Subject header.
3037 The `gnus-list-identifiers' variable specifies what to do."
3038   (interactive)
3039   (let ((inhibit-point-motion-hooks t)
3040         (regexp (if (consp gnus-list-identifiers)
3041                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3042                   gnus-list-identifiers))
3043         (inhibit-read-only t))
3044     (when regexp
3045       (save-excursion
3046         (save-restriction
3047           (article-narrow-to-head)
3048           (goto-char (point-min))
3049           (while (re-search-forward
3050                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3051                   nil t)
3052             (delete-region (match-beginning 2) (match-end 0))
3053             (beginning-of-line))
3054           (when (re-search-forward
3055                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3056             (delete-region (match-beginning 1) (match-end 1))))))))
3057
3058 (defun article-hide-pem (&optional arg)
3059   "Toggle hiding of any PEM headers and signatures in the current article.
3060 If given a negative prefix, always show; if given a positive prefix,
3061 always hide."
3062   (interactive (gnus-article-hidden-arg))
3063   (unless (gnus-article-check-hidden-text 'pem arg)
3064     (save-excursion
3065       (let ((inhibit-read-only t) end)
3066         (goto-char (point-min))
3067         ;; Hide the horrendously ugly "header".
3068         (when (and (search-forward
3069                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3070                     nil t)
3071                    (setq end (1+ (match-beginning 0))))
3072           (gnus-add-wash-type 'pem)
3073           (gnus-article-hide-text-type
3074            end
3075            (if (search-forward "\n\n" nil t)
3076                (match-end 0)
3077              (point-max))
3078            'pem)
3079           ;; Hide the trailer as well
3080           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3081                                 nil t)
3082             (gnus-article-hide-text-type
3083              (match-beginning 0) (match-end 0) 'pem)))))))
3084
3085 (defun article-strip-banner ()
3086   "Strip the banners specified by the `banner' group parameter and by
3087 `gnus-article-address-banner-alist'."
3088   (interactive)
3089   (save-excursion
3090     (save-restriction
3091       (let ((inhibit-point-motion-hooks t))
3092         (when (gnus-parameter-banner gnus-newsgroup-name)
3093           (article-really-strip-banner
3094            (gnus-parameter-banner gnus-newsgroup-name)))
3095         (when gnus-article-address-banner-alist
3096           ;; Note that the From header is decoded here, so it is
3097           ;; required that the *-extract-address-components function
3098           ;; supports non-ASCII text.
3099           (let ((from (save-restriction
3100                         (widen)
3101                         (article-narrow-to-head)
3102                         (mail-fetch-field "from"))))
3103             (when (and from
3104                        (setq from
3105                              (cadr (funcall gnus-extract-address-components
3106                                             from))))
3107               (catch 'found
3108                 (dolist (pair gnus-article-address-banner-alist)
3109                   (when (string-match (car pair) from)
3110                     (throw 'found
3111                            (article-really-strip-banner (cdr pair)))))))))))))
3112
3113 (defun article-really-strip-banner (banner)
3114   "Strip the banner specified by the argument."
3115   (save-excursion
3116     (save-restriction
3117       (let ((inhibit-point-motion-hooks t)
3118             (gnus-signature-limit nil)
3119             (inhibit-read-only t))
3120         (article-goto-body)
3121         (cond
3122          ((eq banner 'signature)
3123           (when (gnus-article-narrow-to-signature)
3124             (widen)
3125             (forward-line -1)
3126             (delete-region (point) (point-max))))
3127          ((symbolp banner)
3128           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3129               (while (re-search-forward banner nil t)
3130                 (delete-region (match-beginning 0) (match-end 0)))))
3131          ((stringp banner)
3132           (while (re-search-forward banner nil t)
3133             (delete-region (match-beginning 0) (match-end 0)))))))))
3134
3135 (defun article-babel ()
3136   "Translate article using an online translation service."
3137   (interactive)
3138   (require 'babel)
3139   (gnus-with-article-buffer
3140     (when (article-goto-body)
3141       (let* ((start (point))
3142              (end (point-max))
3143              (orig (buffer-substring start end))
3144              (trans (babel-as-string orig)))
3145         (save-restriction
3146           (narrow-to-region start end)
3147           (delete-region start end)
3148           (insert trans))))))
3149
3150 (defun article-hide-signature (&optional arg)
3151   "Hide the signature in the current article.
3152 If given a negative prefix, always show; if given a positive prefix,
3153 always hide."
3154   (interactive (gnus-article-hidden-arg))
3155   (unless (gnus-article-check-hidden-text 'signature arg)
3156     (save-excursion
3157       (save-restriction
3158         (let ((inhibit-read-only t))
3159           (when (gnus-article-narrow-to-signature)
3160             (gnus-article-hide-text-type
3161              (point-min) (point-max) 'signature))))))
3162   (gnus-set-mode-line 'article))
3163
3164 (defun article-strip-headers-in-body ()
3165   "Strip offensive headers from bodies."
3166   (interactive)
3167   (save-excursion
3168     (article-goto-body)
3169     (let ((case-fold-search t))
3170       (when (looking-at "x-no-archive:")
3171         (gnus-delete-line)))))
3172
3173 (defun article-strip-leading-blank-lines ()
3174   "Remove all blank lines from the beginning of the article."
3175   (interactive)
3176   (save-excursion
3177     (let ((inhibit-point-motion-hooks t)
3178           (inhibit-read-only t))
3179       (when (article-goto-body)
3180         (while (and (not (eobp))
3181                     (looking-at "[ \t]*$"))
3182           (gnus-delete-line))))))
3183
3184 (defun article-narrow-to-head ()
3185   "Narrow the buffer to the head of the message.
3186 Point is left at the beginning of the narrowed-to region."
3187   (narrow-to-region
3188    (goto-char (point-min))
3189    (if (search-forward "\n\n" nil 1)
3190        (1- (point))
3191      (point-max)))
3192   (goto-char (point-min)))
3193
3194 (defun article-goto-body ()
3195   "Place point at the start of the body."
3196   (goto-char (point-min))
3197   (cond
3198    ;; This variable is only bound when dealing with separate
3199    ;; MIME body parts.
3200    (article-goto-body-goes-to-point-min-p
3201     t)
3202    ((search-forward "\n\n" nil t)
3203     t)
3204    (t
3205     (goto-char (point-max))
3206     nil)))
3207
3208 (defun article-strip-multiple-blank-lines ()
3209   "Replace consecutive blank lines with one empty line."
3210   (interactive)
3211   (save-excursion
3212     (let ((inhibit-point-motion-hooks t)
3213           (inhibit-read-only t))
3214       ;; First make all blank lines empty.
3215       (article-goto-body)
3216       (while (re-search-forward "^[ \t]+$" nil t)
3217         (unless (gnus-annotation-in-region-p
3218                  (match-beginning 0) (match-end 0))
3219           (replace-match "" nil t)))
3220       ;; Then replace multiple empty lines with a single empty line.
3221       (article-goto-body)
3222       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3223         (unless (gnus-annotation-in-region-p
3224                  (match-beginning 0) (match-end 0))
3225           (delete-region (match-beginning 1) (match-end 1)))))))
3226
3227 (defun article-strip-leading-space ()
3228   "Remove all white space from the beginning of the lines in the article."
3229   (interactive)
3230   (save-excursion
3231     (let ((inhibit-point-motion-hooks t)
3232           (inhibit-read-only t))
3233       (article-goto-body)
3234       (while (re-search-forward "^[ \t]+" nil t)
3235         (replace-match "" t t)))))
3236
3237 (defun article-strip-trailing-space ()
3238   "Remove all white space from the end of the lines in the article."
3239   (interactive)
3240   (save-excursion
3241     (let ((inhibit-point-motion-hooks t)
3242           (inhibit-read-only t))
3243       (article-goto-body)
3244       (while (re-search-forward "[ \t]+$" nil t)
3245         (replace-match "" t t)))))
3246
3247 (defun article-strip-blank-lines ()
3248   "Strip leading, trailing and multiple blank lines."
3249   (interactive)
3250   (article-strip-leading-blank-lines)
3251   (article-remove-trailing-blank-lines)
3252   (article-strip-multiple-blank-lines))
3253
3254 (defun article-strip-all-blank-lines ()
3255   "Strip all blank lines."
3256   (interactive)
3257   (save-excursion
3258     (let ((inhibit-point-motion-hooks t)
3259           (inhibit-read-only t))
3260       (article-goto-body)
3261       (while (re-search-forward "^[ \t]*\n" nil t)
3262         (replace-match "" t t)))))
3263
3264 (defun gnus-article-narrow-to-signature ()
3265   "Narrow to the signature; return t if a signature is found, else nil."
3266   (let ((inhibit-point-motion-hooks t))
3267     (when (gnus-article-search-signature)
3268       (forward-line 1)
3269       ;; Check whether we have some limits to what we consider
3270       ;; to be a signature.
3271       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3272                       (list gnus-signature-limit)))
3273             limit limited)
3274         (while (setq limit (pop limits))
3275           (if (or (and (integerp limit)
3276                        (< (- (point-max) (point)) limit))
3277                   (and (floatp limit)
3278                        (< (count-lines (point) (point-max)) limit))
3279                   (and (functionp limit)
3280                        (funcall limit))
3281                   (and (stringp limit)
3282                        (not (re-search-forward limit nil t))))
3283               ()                        ; This limit did not succeed.
3284             (setq limited t
3285                   limits nil)))
3286         (unless limited
3287           (narrow-to-region (point) (point-max))
3288           t)))))
3289
3290 (defun gnus-article-search-signature ()
3291   "Search the current buffer for the signature separator.
3292 Put point at the beginning of the signature separator."
3293   (let ((cur (point)))
3294     (goto-char (point-max))
3295     (if (if (stringp gnus-signature-separator)
3296             (re-search-backward gnus-signature-separator nil t)
3297           (let ((seps gnus-signature-separator))
3298             (while (and seps
3299                         (not (re-search-backward (car seps) nil t)))
3300               (pop seps))
3301             seps))
3302         t
3303       (goto-char cur)
3304       nil)))
3305
3306 (defun gnus-article-hidden-arg ()
3307   "Return the current prefix arg as a number, or 0 if no prefix."
3308   (list (if current-prefix-arg
3309             (prefix-numeric-value current-prefix-arg)
3310           0)))
3311
3312 (defun gnus-article-check-hidden-text (type arg)
3313   "Return nil if hiding is necessary.
3314 Arg can be nil or a number.  nil and positive means hide, negative
3315 means show, 0 means toggle."
3316   (save-excursion
3317     (save-restriction
3318       (let ((hide (gnus-article-hidden-text-p type)))
3319         (cond
3320          ((or (null arg)
3321               (> arg 0))
3322           nil)
3323          ((< arg 0)
3324           (gnus-article-show-hidden-text type)
3325           t)
3326          (t
3327           (if (eq hide 'hidden)
3328               (progn
3329                 (gnus-article-show-hidden-text type)
3330                 t)
3331             nil)))))))
3332
3333 (defun gnus-article-hidden-text-p (type)
3334   "Say whether the current buffer contains hidden text of type TYPE."
3335   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3336     (while (and pos
3337                 (not (get-text-property pos 'invisible))
3338                 (not (get-text-property pos 'dummy-invisible)))
3339       (setq pos
3340             (text-property-any (1+ pos) (point-max) 'article-type type)))
3341     (if pos
3342         'hidden
3343       nil)))
3344
3345 (defun gnus-article-show-hidden-text (type &optional dummy)
3346   "Show all hidden text of type TYPE.
3347 Originally it is hide instead of DUMMY."
3348   (let ((inhibit-read-only t)
3349         (inhibit-point-motion-hooks t))
3350     (gnus-remove-text-properties-when
3351      'article-type type
3352      (point-min) (point-max)
3353      (cons 'article-type (cons type
3354                                gnus-hidden-properties)))
3355     (gnus-delete-wash-type type)))
3356
3357 (defconst article-time-units
3358   `((year . ,(* 365.25 24 60 60))
3359     (week . ,(* 7 24 60 60))
3360     (day . ,(* 24 60 60))
3361     (hour . ,(* 60 60))
3362     (minute . 60)
3363     (second . 1))
3364   "Mapping from time units to seconds.")
3365
3366 (defun gnus-article-forward-header ()
3367   "Move point to the start of the next header.
3368 If the current header is a continuation header, this can be several
3369 lines forward."
3370   (let ((ended nil))
3371     (while (not ended)
3372       (forward-line 1)
3373       (if (looking-at "[ \t]+[^ \t]")
3374           (forward-line 1)
3375         (setq ended t)))))
3376
3377 (defun article-date-ut (&optional type highlight)
3378   "Convert DATE date to universal time in the current article.
3379 If TYPE is `local', convert to local time; if it is `lapsed', output
3380 how much time has lapsed since DATE.  For `lapsed', the value of
3381 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3382 should replace the \"Date:\" one, or should be added below it."
3383   (interactive (list 'ut t))
3384   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3385          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3386                              tdate-regexp)
3387                             ((eq type 'lapsed)
3388                              "^X-Sent:[ \t]")
3389                             (article-lapsed-timer
3390                              "^Date:[ \t]")
3391                             (t
3392                              tdate-regexp)))
3393          (case-fold-search t)
3394          (inhibit-read-only t)
3395          (inhibit-point-motion-hooks t)
3396          pos date bface eface)
3397     (save-excursion
3398       (save-restriction
3399         (widen)
3400         (goto-char (point-min))
3401         (while (or (setq date (get-text-property (setq pos (point))
3402                                                  'original-date))
3403                    (when (setq pos (next-single-property-change
3404                                     (point) 'original-date))
3405                      (setq date (get-text-property pos 'original-date))
3406                      t))
3407           (narrow-to-region
3408            pos (if (setq pos (text-property-any pos (point-max)
3409                                                 'original-date nil))
3410                    (progn
3411                      (goto-char pos)
3412                      (if (or (bolp) (eobp))
3413                          (point)
3414                        (1+ (point))))
3415                  (point-max)))
3416           (goto-char (point-min))
3417           (when (re-search-forward tdate-regexp nil t)
3418             (setq bface (get-text-property (point-at-bol) 'face)
3419                   eface (get-text-property (1- (point-at-eol)) 'face)))
3420           (goto-char (point-min))
3421           (setq pos nil)
3422           ;; Delete any old Date headers.
3423           (while (re-search-forward date-regexp nil t)
3424             (if pos
3425                 (delete-region (point-at-bol) (progn
3426                                                 (gnus-article-forward-header)
3427                                                 (point)))
3428               (delete-region (point-at-bol) (progn
3429                                               (gnus-article-forward-header)
3430                                               (forward-char -1)
3431                                               (point)))
3432               (setq pos (point))))
3433           (when (and (not pos)
3434                      (re-search-forward tdate-regexp nil t))
3435             (forward-line 1))
3436           (gnus-goto-char pos)
3437           (insert (article-make-date-line date (or type 'ut)))
3438           (unless pos
3439             (insert "\n")
3440             (forward-line -1))
3441           ;; Do highlighting.
3442           (beginning-of-line)
3443           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3444             (put-text-property (match-beginning 1) (1+ (match-end 1))
3445                                'face bface)
3446             (put-text-property (match-beginning 2) (match-end 2)
3447                                'face eface))
3448           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3449           (goto-char (point-max))
3450           (widen))))))
3451
3452 (defun article-make-date-line (date type)
3453   "Return a DATE line of TYPE."
3454   (unless (memq type '(local ut original user iso8601 lapsed english))
3455     (error "Unknown conversion type: %s" type))
3456   (condition-case ()
3457       (let ((time (date-to-time date)))
3458         (cond
3459          ;; Convert to the local timezone.
3460          ((eq type 'local)
3461           (concat "Date: " (message-make-date time)))
3462          ;; Convert to Universal Time.
3463          ((eq type 'ut)
3464           (concat "Date: "
3465                   (substring
3466                    (message-make-date
3467                     (let* ((e (parse-time-string date))
3468                            (tm (apply 'encode-time e))
3469                            (ms (car tm))
3470                            (ls (- (cadr tm) (car (current-time-zone time)))))
3471                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3472                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3473                             (t (list ms ls)))))
3474                    0 -5)
3475                   "UT"))
3476          ;; Get the original date from the article.
3477          ((eq type 'original)
3478           (concat "Date: " (if (string-match "\n+$" date)
3479                                (substring date 0 (match-beginning 0))
3480                              date)))
3481          ;; Let the user define the format.
3482          ((eq type 'user)
3483           (let ((format (or (condition-case nil
3484                                 (with-current-buffer gnus-summary-buffer
3485                                   gnus-article-time-format)
3486                               (error nil))
3487                             gnus-article-time-format)))
3488             (if (functionp format)
3489                 (funcall format time)
3490               (concat "Date: " (format-time-string format time)))))
3491          ;; ISO 8601.
3492          ((eq type 'iso8601)
3493           (let ((tz (car (current-time-zone time))))
3494             (concat
3495              "Date: "
3496              (format-time-string "%Y%m%dT%H%M%S" time)
3497              (format "%s%02d%02d"
3498                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3499                      (/ (% (abs tz) 3600) 60)))))
3500          ;; Do an X-Sent lapsed format.
3501          ((eq type 'lapsed)
3502           ;; If the date is seriously mangled, the timezone functions are
3503           ;; liable to bug out, so we ignore all errors.
3504           (let* ((now (current-time))
3505                  (real-time (subtract-time now time))
3506                  (real-sec (and real-time
3507                                 (+ (* (float (car real-time)) 65536)
3508                                    (cadr real-time))))
3509                  (sec (and real-time (abs real-sec)))
3510                  num prev)
3511             (cond
3512              ((null real-time)
3513               "X-Sent: Unknown")
3514              ((zerop sec)
3515               "X-Sent: Now")
3516              (t
3517               (concat
3518                "X-Sent: "
3519                ;; This is a bit convoluted, but basically we go
3520                ;; through the time units for years, weeks, etc,
3521                ;; and divide things to see whether that results
3522                ;; in positive answers.
3523                (mapconcat
3524                 (lambda (unit)
3525                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3526                       ;; The (remaining) seconds are too few to
3527                       ;; be divided into this time unit.
3528                       ""
3529                     ;; It's big enough, so we output it.
3530                     (setq sec (- sec (* num (cdr unit))))
3531                     (prog1
3532                         (concat (if prev ", " "") (int-to-string
3533                                                    (floor num))
3534                                 " " (symbol-name (car unit))
3535                                 (if (> num 1) "s" ""))
3536                       (setq prev t))))
3537                 article-time-units "")
3538                ;; If dates are odd, then it might appear like the
3539                ;; article was sent in the future.
3540                (if (> real-sec 0)
3541                    " ago"
3542                  " in the future"))))))
3543          ;; Display the date in proper English
3544          ((eq type 'english)
3545           (let ((dtime (decode-time time)))
3546             (concat
3547              "Date: the "
3548              (number-to-string (nth 3 dtime))
3549              (let ((digit (% (nth 3 dtime) 10)))
3550                (cond
3551                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3552                 ((= digit 1) "st")
3553                 ((= digit 2) "nd")
3554                 ((= digit 3) "rd")
3555                 (t "th")))
3556              " of "
3557              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3558              " "
3559              (number-to-string (nth 5 dtime))
3560              " at "
3561              (format "%02d" (nth 2 dtime))
3562              ":"
3563              (format "%02d" (nth 1 dtime)))))))
3564     (error
3565      (format "Date: %s (from Gnus)" date))))
3566
3567 (defun article-date-local (&optional highlight)
3568   "Convert the current article date to the local timezone."
3569   (interactive (list t))
3570   (article-date-ut 'local highlight))
3571
3572 (defun article-date-english (&optional highlight)
3573   "Convert the current article date to something that is proper English."
3574   (interactive (list t))
3575   (article-date-ut 'english highlight))
3576
3577 (defun article-date-original (&optional highlight)
3578   "Convert the current article date to what it was originally.
3579 This is only useful if you have used some other date conversion
3580 function and want to see what the date was before converting."
3581   (interactive (list t))
3582   (article-date-ut 'original highlight))
3583
3584 (defun article-date-lapsed (&optional highlight)
3585   "Convert the current article date to time lapsed since it was sent."
3586   (interactive (list t))
3587   (article-date-ut 'lapsed highlight))
3588
3589 (defun article-update-date-lapsed ()
3590   "Function to be run from a timer to update the lapsed time line."
3591   (save-match-data
3592     (let (deactivate-mark)
3593       (save-excursion
3594         (ignore-errors
3595          (walk-windows
3596           (lambda (w)
3597             (set-buffer (window-buffer w))
3598             (when (eq major-mode 'gnus-article-mode)
3599               (let ((mark (point-marker)))
3600                 (goto-char (point-min))
3601                 (when (re-search-forward "^X-Sent:" nil t)
3602                   (article-date-lapsed t))
3603                 (goto-char (marker-position mark))
3604                 (move-marker mark nil))))
3605           nil 'visible))))))
3606
3607 (defun gnus-start-date-timer (&optional n)
3608   "Start a timer to update the X-Sent header in the article buffers.
3609 The numerical prefix says how frequently (in seconds) the function
3610 is to run."
3611   (interactive "p")
3612   (unless n
3613     (setq n 1))
3614   (gnus-stop-date-timer)
3615   (setq article-lapsed-timer
3616         (run-at-time 1 n 'article-update-date-lapsed)))
3617
3618 (defun gnus-stop-date-timer ()
3619   "Stop the X-Sent timer."
3620   (interactive)
3621   (when article-lapsed-timer
3622     (nnheader-cancel-timer article-lapsed-timer)
3623     (setq article-lapsed-timer nil)))
3624
3625 (defun article-date-user (&optional highlight)
3626   "Convert the current article date to the user-defined format.
3627 This format is defined by the `gnus-article-time-format' variable."
3628   (interactive (list t))
3629   (article-date-ut 'user highlight))
3630
3631 (defun article-date-iso8601 (&optional highlight)
3632   "Convert the current article date to ISO8601."
3633   (interactive (list t))
3634   (article-date-ut 'iso8601 highlight))
3635
3636 (defmacro gnus-article-save-original-date (&rest forms)
3637   "Save the original date as a text property and evaluate FORMS."
3638   `(let* ((case-fold-search t)
3639           (start (progn
3640                    (goto-char (point-min))
3641                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3642                               (not (bolp)))
3643                      (match-end 0))))
3644           (date (when (and start
3645                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3646                                               nil t))
3647                   (buffer-substring-no-properties start
3648                                                   (match-beginning 0)))))
3649      (goto-char (point-max))
3650      (skip-chars-backward "\n")
3651      (put-text-property (point-min) (point) 'original-date date)
3652      ,@forms
3653      (goto-char (point-max))
3654      (skip-chars-backward "\n")
3655      (put-text-property (point-min) (point) 'original-date date)))
3656
3657 ;; (defun article-show-all ()
3658 ;;   "Show all hidden text in the article buffer."
3659 ;;   (interactive)
3660 ;;   (save-excursion
3661 ;;     (let ((inhibit-read-only t))
3662 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3663
3664 (defun article-remove-leading-whitespace ()
3665   "Remove excessive whitespace from all headers."
3666   (interactive)
3667   (save-excursion
3668     (save-restriction
3669       (let ((inhibit-read-only t))
3670         (article-narrow-to-head)
3671         (goto-char (point-min))
3672         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3673           (delete-region (match-beginning 1) (match-end 1)))))))
3674
3675 (defun article-emphasize (&optional arg)
3676   "Emphasize text according to `gnus-emphasis-alist'."
3677   (interactive (gnus-article-hidden-arg))
3678   (unless (gnus-article-check-hidden-text 'emphasis arg)
3679     (save-excursion
3680       (let ((alist (or
3681                     (condition-case nil
3682                         (with-current-buffer gnus-summary-buffer
3683                           gnus-article-emphasis-alist)
3684                       (error))
3685                     gnus-emphasis-alist))
3686             (inhibit-read-only t)
3687             (props (append '(article-type emphasis)
3688                            gnus-hidden-properties))
3689             regexp elem beg invisible visible face)
3690         (article-goto-body)
3691         (setq beg (point))
3692         (while (setq elem (pop alist))
3693           (goto-char beg)
3694           (setq regexp (car elem)
3695                 invisible (nth 1 elem)
3696                 visible (nth 2 elem)
3697                 face (nth 3 elem))
3698           (while (re-search-forward regexp nil t)
3699             (when (and (match-beginning visible) (match-beginning invisible))
3700               (gnus-article-hide-text
3701                (match-beginning invisible) (match-end invisible) props)
3702               (gnus-article-unhide-text-type
3703                (match-beginning visible) (match-end visible) 'emphasis)
3704               (gnus-put-overlay-excluding-newlines
3705                (match-beginning visible) (match-end visible) 'face face)
3706               (gnus-add-wash-type 'emphasis)
3707               (goto-char (match-end invisible)))))))))
3708
3709 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3710   "Setup newsgroup emphasis alist."
3711   (unless gnus-article-emphasis-alist
3712     (let ((name (and gnus-newsgroup-name
3713                      (gnus-group-real-name gnus-newsgroup-name))))
3714       (make-local-variable 'gnus-article-emphasis-alist)
3715       (setq gnus-article-emphasis-alist
3716             (nconc
3717              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3718                (while (setq elem (pop alist))
3719                  (when (and name (string-match (car elem) name))
3720                    (setq alist nil
3721                          highlight (copy-sequence (cdr elem)))))
3722                highlight)
3723              (copy-sequence highlight-words)
3724              (if gnus-newsgroup-name
3725                  (copy-sequence (gnus-group-find-parameter
3726                                  gnus-newsgroup-name 'highlight-words t)))
3727              gnus-emphasis-alist)))))
3728
3729 (defvar gnus-summary-article-menu)
3730 (defvar gnus-summary-post-menu)
3731
3732 ;;; Saving functions.
3733
3734 (defun gnus-article-save (save-buffer file &optional num)
3735   "Save the currently selected article."
3736   (when (or (get gnus-default-article-saver :headers)
3737             (not gnus-save-all-headers))
3738     ;; Remove headers according to `gnus-saved-headers' or the value
3739     ;; of the `:headers' property that the saver function might have.
3740     (let ((gnus-visible-headers
3741            (or (symbol-value (get gnus-default-article-saver :headers))
3742                gnus-saved-headers gnus-visible-headers))
3743           ;; Ignore group parameter.  See `article-hide-headers'.
3744           (gnus-summary-buffer nil))
3745       (with-current-buffer save-buffer
3746         (article-hide-headers 1 t))))
3747   (save-window-excursion
3748     (if (not gnus-default-article-saver)
3749         (error "No default saver is defined")
3750       ;; !!! Magic!  The saving functions all save
3751       ;; `gnus-save-article-buffer' (or so they think), but we
3752       ;; bind that variable to our save-buffer.
3753       (set-buffer gnus-article-buffer)
3754       (let* ((gnus-save-article-buffer save-buffer)
3755              (filename
3756               (cond
3757                ((not gnus-prompt-before-saving) 'default)
3758                ((eq gnus-prompt-before-saving 'always) nil)
3759                (t file)))
3760              (gnus-number-of-articles-to-be-saved
3761               (when (eq gnus-prompt-before-saving t)
3762                 num)))                  ; Magic
3763         (set-buffer gnus-article-current-summary)
3764         (funcall gnus-default-article-saver filename)))))
3765
3766 (defun gnus-read-save-file-name (prompt &optional filename
3767                                         function group headers variable
3768                                         dir-var)
3769   (let ((default-name
3770           (funcall function group headers (symbol-value variable)))
3771         result)
3772     (setq result
3773           (expand-file-name
3774            (cond
3775             ((eq filename 'default)
3776              default-name)
3777             ((eq filename t)
3778              default-name)
3779             (filename filename)
3780             (t
3781              (when (symbol-value dir-var)
3782                (setq default-name (expand-file-name
3783                                    (file-name-nondirectory default-name)
3784                                    (symbol-value dir-var))))
3785              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3786                     (prompt
3787                      (format prompt
3788                              (if (and gnus-number-of-articles-to-be-saved
3789                                       (> gnus-number-of-articles-to-be-saved 1))
3790                                  (format "these %d articles"
3791                                          gnus-number-of-articles-to-be-saved)
3792                                "this article")))
3793                     (file
3794                      ;; Let the split methods have their say.
3795                      (cond
3796                       ;; No split name was found.
3797                       ((null split-name)
3798                        (read-file-name
3799                         (concat prompt " (default "
3800                                 (file-name-nondirectory default-name) "): ")
3801                         (file-name-directory default-name)
3802                         default-name))
3803                       ;; A single group name is returned.
3804                       ((stringp split-name)
3805                        (setq default-name
3806                              (funcall function split-name headers
3807                                       (symbol-value variable)))
3808                        (read-file-name
3809                         (concat prompt " (default "
3810                                 (file-name-nondirectory default-name) "): ")
3811                         (file-name-directory default-name)
3812                         default-name))
3813                       ;; A single split name was found
3814                       ((= 1 (length split-name))
3815                        (let* ((name (expand-file-name
3816                                      (car split-name)
3817                                      gnus-article-save-directory))
3818                               (dir (cond ((file-directory-p name)
3819                                           (file-name-as-directory name))
3820                                          ((file-exists-p name) name)
3821                                          (t gnus-article-save-directory))))
3822                          (read-file-name
3823                           (concat prompt " (default " name "): ")
3824                           dir name)))
3825                       ;; A list of splits was found.
3826                       (t
3827                        (setq split-name (nreverse split-name))
3828                        (let (result)
3829                          (let ((file-name-history
3830                                 (nconc split-name file-name-history)))
3831                            (setq result
3832                                  (expand-file-name
3833                                   (read-file-name
3834                                    (concat prompt " (`M-p' for defaults): ")
3835                                    gnus-article-save-directory
3836                                    (car split-name))
3837                                   gnus-article-save-directory)))
3838                          (car (push result file-name-history)))))))
3839                ;; Create the directory.
3840                (gnus-make-directory (file-name-directory file))
3841                ;; If we have read a directory, we append the default file name.
3842                (when (file-directory-p file)
3843                  (setq file (expand-file-name (file-name-nondirectory
3844                                                default-name)
3845                                               (file-name-as-directory file))))
3846                ;; Possibly translate some characters.
3847                (nnheader-translate-file-chars file))))))
3848     (gnus-make-directory (file-name-directory result))
3849     (when variable
3850       (set variable result))
3851     (when dir-var
3852       (set dir-var (file-name-directory result)))
3853     result))
3854
3855 (defun gnus-article-archive-name (group)
3856   "Return the first instance of an \"Archive-name\" in the current buffer."
3857   (let ((case-fold-search t))
3858     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3859       (nnheader-concat gnus-article-save-directory
3860                        (match-string 1)))))
3861
3862 (defun gnus-article-nndoc-name (group)
3863   "If GROUP is an nndoc group, return the name of the parent group."
3864   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3865     (gnus-group-get-parameter group 'save-article-group)))
3866
3867 (defun gnus-summary-save-in-rmail (&optional filename)
3868   "Append this article to Rmail file.
3869 Optional argument FILENAME specifies file name.
3870 Directory to save to is default to `gnus-article-save-directory'."
3871   (setq filename (gnus-read-save-file-name
3872                   "Save %s in rmail file" filename
3873                   gnus-rmail-save-name gnus-newsgroup-name
3874                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3875   (gnus-eval-in-buffer-window gnus-save-article-buffer
3876     (save-excursion
3877       (save-restriction
3878         (widen)
3879         (gnus-output-to-rmail filename))))
3880   filename)
3881
3882 (defun gnus-summary-save-in-mail (&optional filename)
3883   "Append this article to Unix mail file.
3884 Optional argument FILENAME specifies file name.
3885 Directory to save to is default to `gnus-article-save-directory'."
3886   (setq filename (gnus-read-save-file-name
3887                   "Save %s in Unix mail file" filename
3888                   gnus-mail-save-name gnus-newsgroup-name
3889                   gnus-current-headers 'gnus-newsgroup-last-mail))
3890   (gnus-eval-in-buffer-window gnus-save-article-buffer
3891     (save-excursion
3892       (save-restriction
3893         (widen)
3894         (if (and (file-readable-p filename)
3895                  (file-regular-p filename)
3896                  (mail-file-babyl-p filename))
3897             (rmail-output-to-rmail-file filename t)
3898           (gnus-output-to-mail filename)))))
3899   filename)
3900
3901 (put 'gnus-summary-save-in-file :decode t)
3902 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3903 (defun gnus-summary-save-in-file (&optional filename overwrite)
3904   "Append this article to file.
3905 Optional argument FILENAME specifies file name.
3906 Directory to save to is default to `gnus-article-save-directory'."
3907   (setq filename (gnus-read-save-file-name
3908                   "Save %s in file" filename
3909                   gnus-file-save-name gnus-newsgroup-name
3910                   gnus-current-headers 'gnus-newsgroup-last-file))
3911   (gnus-eval-in-buffer-window gnus-save-article-buffer
3912     (save-excursion
3913       (save-restriction
3914         (widen)
3915         (when (and overwrite
3916                    (file-exists-p filename))
3917           (delete-file filename))
3918         (gnus-output-to-file filename))))
3919   filename)
3920
3921 (put 'gnus-summary-write-to-file :decode t)
3922 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3923 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3924 (defun gnus-summary-write-to-file (&optional filename)
3925   "Write this article to a file, overwriting it if the file exists.
3926 Optional argument FILENAME specifies file name.
3927 The directory to save in defaults to `gnus-article-save-directory'."
3928   (setq filename (gnus-read-save-file-name
3929                   "Save %s in file" filename
3930                   gnus-file-save-name gnus-newsgroup-name
3931                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3932   (gnus-summary-save-in-file filename t))
3933
3934 (put 'gnus-summary-save-body-in-file :decode t)
3935 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3936   "Append this article body to a file.
3937 Optional argument FILENAME specifies file name.
3938 The directory to save in defaults to `gnus-article-save-directory'."
3939   (setq filename (gnus-read-save-file-name
3940                   "Save %s body in file" filename
3941                   gnus-file-save-name gnus-newsgroup-name
3942                   gnus-current-headers 'gnus-newsgroup-last-file))
3943   (gnus-eval-in-buffer-window gnus-save-article-buffer
3944     (save-excursion
3945       (save-restriction
3946         (widen)
3947         (when (article-goto-body)
3948           (narrow-to-region (point) (point-max)))
3949         (when (and overwrite
3950                    (file-exists-p filename))
3951           (delete-file filename))
3952         (gnus-output-to-file filename))))
3953   filename)
3954
3955 (put 'gnus-summary-write-body-to-file :decode t)
3956 (put 'gnus-summary-write-body-to-file
3957      :function 'gnus-summary-save-body-in-file)
3958 (defun gnus-summary-write-body-to-file (&optional filename)
3959   "Write this article body to a file, overwriting it if the file exists.
3960 Optional argument FILENAME specifies file name.
3961 The directory to save in defaults to `gnus-article-save-directory'."
3962   (setq filename (gnus-read-save-file-name
3963                   "Save %s body in file" filename
3964                   gnus-file-save-name gnus-newsgroup-name
3965                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3966   (gnus-summary-save-body-in-file filename t))
3967
3968 (put 'gnus-summary-save-in-pipe :decode t)
3969 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
3970 (defun gnus-summary-save-in-pipe (&optional command raw)
3971   "Pipe this article to subprocess COMMAND.
3972 Valid values for COMMAND include:
3973   a string
3974     The executable command name and possibly arguments.
3975   nil
3976     You will be prompted for the command in the minibuffer.
3977   the symbol `default'
3978     It will be replaced with the command which the variable
3979     `gnus-summary-pipe-output-default-command' holds or the command
3980     last used for saving.
3981 Non-nil value for RAW overrides `:decode' and `:headers' properties
3982 and the raw article including all headers will be piped."
3983   (let ((article (gnus-summary-article-number))
3984         (decode (unless raw
3985                   (get 'gnus-summary-save-in-pipe :decode)))
3986         save-buffer default)
3987     (if article
3988         (if (vectorp (gnus-summary-article-header article))
3989             (save-current-buffer
3990               (gnus-summary-select-article decode decode nil article)
3991               (insert-buffer-substring
3992                (prog1
3993                    (if decode
3994                        gnus-article-buffer
3995                      gnus-original-article-buffer)
3996                  (setq save-buffer
3997                        (nnheader-set-temp-buffer " *Gnus Save*"))))
3998               ;; Remove unwanted headers.
3999               (when (and (not raw)
4000                          (or (get 'gnus-summary-save-in-pipe :headers)
4001                              (not gnus-save-all-headers)))
4002                 (let ((gnus-visible-headers
4003                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4004                                               :headers))
4005                            gnus-saved-headers gnus-visible-headers))
4006                       (gnus-summary-buffer nil))
4007                   (article-hide-headers 1 t))))
4008           (error "%d is not a real article" article))
4009       (error "No article to pipe"))
4010     (setq default (or gnus-summary-pipe-output-default-command
4011                       gnus-last-shell-command))
4012     (unless (stringp command)
4013       (setq command
4014             (if (and (eq command 'default) default)
4015                 default
4016               (gnus-read-shell-command "Shell command on this article: "
4017                                        default))))
4018     (when (string-equal command "")
4019       (if default
4020           (setq command default)
4021         (error "A command is required")))
4022     (gnus-eval-in-buffer-window save-buffer
4023       (save-restriction
4024         (widen)
4025         (shell-command-on-region (point-min) (point-max) command nil)))
4026     (gnus-kill-buffer save-buffer))
4027   (setq gnus-summary-pipe-output-default-command command))
4028
4029 (defun gnus-summary-pipe-to-muttprint (&optional command)
4030   "Pipe this article to muttprint."
4031   (unless (stringp command)
4032     (setq command (read-string
4033                    "Print using command: " gnus-summary-muttprint-program
4034                    nil gnus-summary-muttprint-program)))
4035   (let ((gnus-summary-pipe-output-default-command
4036          gnus-summary-pipe-output-default-command))
4037     (gnus-summary-save-in-pipe command))
4038   (setq gnus-summary-muttprint-program command))
4039
4040 ;;; Article file names when saving.
4041
4042 (defun gnus-capitalize-newsgroup (newsgroup)
4043   "Capitalize NEWSGROUP name."
4044   (when (not (zerop (length newsgroup)))
4045     (concat (char-to-string (upcase (aref newsgroup 0)))
4046             (substring newsgroup 1))))
4047
4048 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4049   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4050 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4051 Otherwise, it is like ~/News/news/group/num."
4052   (let ((default
4053           (expand-file-name
4054            (concat (if (gnus-use-long-file-name 'not-save)
4055                        (gnus-capitalize-newsgroup newsgroup)
4056                      (gnus-newsgroup-directory-form newsgroup))
4057                    "/" (int-to-string (mail-header-number headers)))
4058            gnus-article-save-directory)))
4059     (if (and last-file
4060              (string-equal (file-name-directory default)
4061                            (file-name-directory last-file))
4062              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4063         default
4064       (or last-file default))))
4065
4066 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4067   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4068 If variable `gnus-use-long-file-name' is non-nil, it is
4069 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4070   (let ((default
4071           (expand-file-name
4072            (concat (if (gnus-use-long-file-name 'not-save)
4073                        newsgroup
4074                      (gnus-newsgroup-directory-form newsgroup))
4075                    "/" (int-to-string (mail-header-number headers)))
4076            gnus-article-save-directory)))
4077     (if (and last-file
4078              (string-equal (file-name-directory default)
4079                            (file-name-directory last-file))
4080              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4081         default
4082       (or last-file default))))
4083
4084 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4085   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4086 If variable `gnus-use-long-file-name' is non-nil, it is
4087 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4088   (or last-file
4089       (expand-file-name
4090        (if (gnus-use-long-file-name 'not-save)
4091            newsgroup
4092          (file-relative-name
4093           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4094           default-directory))
4095        gnus-article-save-directory)))
4096
4097 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4098   "Generate file name from sender."
4099   (let ((from (mail-header-from headers)))
4100     (expand-file-name
4101      (if (and from (string-match "\\([^ <]+\\)@" from))
4102          (match-string 1 from)
4103        "nobody")
4104      gnus-article-save-directory)))
4105
4106 (defun article-verify-x-pgp-sig ()
4107   "Verify X-PGP-Sig."
4108   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4109   (interactive)
4110   (if (gnus-buffer-live-p gnus-original-article-buffer)
4111       (let ((sig (with-current-buffer gnus-original-article-buffer
4112                    (gnus-fetch-field "X-PGP-Sig")))
4113             items info headers)
4114         (when (and sig
4115                    mml2015-use
4116                    (mml2015-clear-verify-function))
4117           (with-temp-buffer
4118             (insert-buffer-substring gnus-original-article-buffer)
4119             (setq items (split-string sig))
4120             (message-narrow-to-head)
4121             (let ((inhibit-point-motion-hooks t)
4122                   (case-fold-search t))
4123               ;; Don't verify multiple headers.
4124               (setq headers (mapconcat (lambda (header)
4125                                          (concat header ": "
4126                                                  (mail-fetch-field header)
4127                                                  "\n"))
4128                                        (split-string (nth 1 items) ",") "")))
4129             (delete-region (point-min) (point-max))
4130             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4131             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4132             (insert headers)
4133             (widen)
4134             (forward-line)
4135             (while (not (eobp))
4136               (if (looking-at "^-")
4137                   (insert "- "))
4138               (forward-line))
4139             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4140             (insert "Version: " (car items) "\n\n")
4141             (insert (mapconcat 'identity (cddr items) "\n"))
4142             (insert "\n-----END PGP SIGNATURE-----\n")
4143             (let ((mm-security-handle (list (format "multipart/signed"))))
4144               (mml2015-clean-buffer)
4145               (let ((coding-system-for-write (or gnus-newsgroup-charset
4146                                                  'iso-8859-1)))
4147                 (funcall (mml2015-clear-verify-function)))
4148               (setq info
4149                     (or (mm-handle-multipart-ctl-parameter
4150                          mm-security-handle 'gnus-details)
4151                         (mm-handle-multipart-ctl-parameter
4152                          mm-security-handle 'gnus-info)))))
4153           (when info
4154             (let ((inhibit-read-only t) bface eface)
4155               (save-restriction
4156                 (message-narrow-to-head)
4157                 (goto-char (point-max))
4158                 (forward-line -1)
4159                 (setq bface (get-text-property (point-at-bol) 'face)
4160                       eface (get-text-property (1- (point-at-eol)) 'face))
4161                 (message-remove-header "X-Gnus-PGP-Verify")
4162                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4163                     (forward-line)
4164                   (goto-char (point-max)))
4165                 (narrow-to-region (point) (point))
4166                 (insert "X-Gnus-PGP-Verify: " info "\n")
4167                 (goto-char (point-min))
4168                 (forward-line)
4169                 (while (not (eobp))
4170                   (if (not (looking-at "^[ \t]"))
4171                       (insert " "))
4172                   (forward-line))
4173                 ;; Do highlighting.
4174                 (goto-char (point-min))
4175                 (when (looking-at "\\([^:]+\\): *")
4176                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4177                                      'face bface)
4178                   (put-text-property (match-end 0) (point-max)
4179                                      'face eface)))))))))
4180
4181 (defun article-verify-cancel-lock ()
4182   "Verify Cancel-Lock header."
4183   (interactive)
4184   (if (gnus-buffer-live-p gnus-original-article-buffer)
4185       (canlock-verify gnus-original-article-buffer)))
4186
4187 (eval-and-compile
4188   (mapc
4189    (lambda (func)
4190      (let (afunc gfunc)
4191        (if (consp func)
4192            (setq afunc (car func)
4193                  gfunc (cdr func))
4194          (setq afunc func
4195                gfunc (intern (format "gnus-%s" func))))
4196        (defalias gfunc
4197          (when (fboundp afunc)
4198            `(lambda (&optional interactive &rest args)
4199               ,(documentation afunc t)
4200               (interactive (list t))
4201               (with-current-buffer gnus-article-buffer
4202                 (if interactive
4203                     (call-interactively ',afunc)
4204                   (apply ',afunc args))))))))
4205    '(article-hide-headers
4206      article-verify-x-pgp-sig
4207      article-verify-cancel-lock
4208      article-hide-boring-headers
4209      article-treat-overstrike
4210      article-treat-ansi-sequences
4211      article-fill-long-lines
4212      article-capitalize-sentences
4213      article-remove-cr
4214      article-remove-leading-whitespace
4215      article-display-x-face
4216      article-display-face
4217      article-de-quoted-unreadable
4218      article-de-base64-unreadable
4219      article-decode-HZ
4220      article-wash-html
4221      article-unsplit-urls
4222      article-hide-list-identifiers
4223      article-strip-banner
4224      article-babel
4225      article-hide-pem
4226      article-hide-signature
4227      article-strip-headers-in-body
4228      article-remove-trailing-blank-lines
4229      article-strip-leading-blank-lines
4230      article-strip-multiple-blank-lines
4231      article-strip-leading-space
4232      article-strip-trailing-space
4233      article-strip-blank-lines
4234      article-strip-all-blank-lines
4235      article-date-local
4236      article-date-english
4237      article-date-iso8601
4238      article-date-original
4239      article-date-ut
4240      article-decode-mime-words
4241      article-decode-charset
4242      article-decode-encoded-words
4243      article-date-user
4244      article-date-lapsed
4245      article-emphasize
4246      article-treat-dumbquotes
4247      article-normalize-headers
4248      ;;(article-show-all . gnus-article-show-all-headers)
4249      )))
4250 \f
4251 ;;;
4252 ;;; Gnus article mode
4253 ;;;
4254
4255 (put 'gnus-article-mode 'mode-class 'special)
4256
4257 (set-keymap-parent gnus-article-mode-map widget-keymap)
4258
4259 (gnus-define-keys gnus-article-mode-map
4260   " " gnus-article-goto-next-page
4261   "\177" gnus-article-goto-prev-page
4262   [delete] gnus-article-goto-prev-page
4263   [backspace] gnus-article-goto-prev-page
4264   "\C-c^" gnus-article-refer-article
4265   "h" gnus-article-show-summary
4266   "s" gnus-article-show-summary
4267   "\C-c\C-m" gnus-article-mail
4268   "?" gnus-article-describe-briefly
4269   "e" gnus-summary-edit-article
4270   "<" beginning-of-buffer
4271   ">" end-of-buffer
4272   "\C-c\C-i" gnus-info-find-node
4273   "\C-c\C-b" gnus-bug
4274   "R" gnus-article-reply-with-original
4275   "F" gnus-article-followup-with-original
4276   "\C-hk" gnus-article-describe-key
4277   "\C-hc" gnus-article-describe-key-briefly
4278   "\C-hb" gnus-article-describe-bindings
4279
4280   "\C-d" gnus-article-read-summary-keys
4281   "\M-*" gnus-article-read-summary-keys
4282   "\M-#" gnus-article-read-summary-keys
4283   "\M-^" gnus-article-read-summary-keys
4284   "\M-g" gnus-article-read-summary-keys)
4285
4286 (substitute-key-definition
4287  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4288
4289 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4290   "W" gnus-article-wide-reply-with-original)
4291 (if (featurep 'xemacs)
4292     (set-keymap-default-binding gnus-article-send-map
4293                                 'gnus-article-read-summary-send-keys)
4294   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4295
4296 (defun gnus-article-make-menu-bar ()
4297   (unless (boundp 'gnus-article-commands-menu)
4298     (gnus-summary-make-menu-bar))
4299   (gnus-turn-off-edit-menu 'article)
4300   (unless (boundp 'gnus-article-article-menu)
4301     (easy-menu-define
4302      gnus-article-article-menu gnus-article-mode-map ""
4303      '("Article"
4304        ["Scroll forwards" gnus-article-goto-next-page t]
4305        ["Scroll backwards" gnus-article-goto-prev-page t]
4306        ["Show summary" gnus-article-show-summary t]
4307        ["Fetch Message-ID at point" gnus-article-refer-article t]
4308        ["Mail to address at point" gnus-article-mail t]
4309        ["Send a bug report" gnus-bug t]))
4310
4311     (easy-menu-define
4312      gnus-article-treatment-menu gnus-article-mode-map ""
4313      ;; Fixme: this should use :active (and maybe :visible).
4314      '("Treatment"
4315        ["Hide headers" gnus-article-hide-headers t]
4316        ["Hide signature" gnus-article-hide-signature t]
4317        ["Hide citation" gnus-article-hide-citation t]
4318        ["Treat overstrike" gnus-article-treat-overstrike t]
4319        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4320        ["Remove carriage return" gnus-article-remove-cr t]
4321        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4322        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4323        ["Remove base64" gnus-article-de-base64-unreadable t]
4324        ["Treat html" gnus-article-wash-html t]
4325        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4326        ["Decode HZ" gnus-article-decode-HZ t]))
4327
4328     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4329
4330     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4331
4332     (gnus-run-hooks 'gnus-article-menu-hook)))
4333
4334 (defun gnus-article-mode ()
4335   "Major mode for displaying an article.
4336
4337 All normal editing commands are switched off.
4338
4339 The following commands are available in addition to all summary mode
4340 commands:
4341 \\<gnus-article-mode-map>
4342 \\[gnus-article-next-page]\t Scroll the article one page forwards
4343 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4344 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4345 \\[gnus-article-show-summary]\t Display the summary buffer
4346 \\[gnus-article-mail]\t Send a reply to the address near point
4347 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4348 \\[gnus-info-find-node]\t Go to the Gnus info node"
4349   (interactive)
4350   (kill-all-local-variables)
4351   (gnus-simplify-mode-line)
4352   (setq mode-name "Article")
4353   (setq major-mode 'gnus-article-mode)
4354   (make-local-variable 'minor-mode-alist)
4355   (use-local-map gnus-article-mode-map)
4356   (when (gnus-visual-p 'article-menu 'menu)
4357     (gnus-article-make-menu-bar)
4358     (when gnus-summary-tool-bar-map
4359       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4360   (gnus-update-format-specifications nil 'article-mode)
4361   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4362   (set (make-local-variable 'gnus-page-broken) nil)
4363   (make-local-variable 'gnus-button-marker-list)
4364   (make-local-variable 'gnus-article-current-summary)
4365   (make-local-variable 'gnus-article-mime-handles)
4366   (make-local-variable 'gnus-article-decoded-p)
4367   (make-local-variable 'gnus-article-mime-handle-alist)
4368   (make-local-variable 'gnus-article-wash-types)
4369   (make-local-variable 'gnus-article-image-alist)
4370   (make-local-variable 'gnus-article-charset)
4371   (make-local-variable 'gnus-article-ignored-charsets)
4372   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4373   ;; face.
4374   (set (make-local-variable 'nobreak-char-display) nil)
4375   (setq cursor-in-non-selected-windows nil)
4376   (setq truncate-lines gnus-article-truncate-lines)
4377   (gnus-set-default-directory)
4378   (buffer-disable-undo)
4379   (setq buffer-read-only t
4380         show-trailing-whitespace nil)
4381   (set-syntax-table gnus-article-mode-syntax-table)
4382   (mm-enable-multibyte)
4383   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4384
4385 (defvar gnus-button-marker-list nil
4386   "Regexp matching any of the regexps from `gnus-button-alist'.
4387 Internal variable.")
4388
4389 (defun gnus-article-setup-buffer ()
4390   "Initialize the article buffer."
4391   (let* ((name (if gnus-single-article-buffer "*Article*"
4392                  (concat "*Article " gnus-newsgroup-name "*")))
4393          (original
4394           (progn (string-match "\\*Article" name)
4395                  (concat " *Original Article"
4396                          (substring name (match-end 0))))))
4397     (setq gnus-article-buffer name)
4398     (setq gnus-original-article-buffer original)
4399     (setq gnus-article-mime-handle-alist nil)
4400     (with-current-buffer gnus-summary-buffer
4401       ;; This might be a variable local to the summary buffer.
4402       (unless gnus-single-article-buffer
4403         (setq gnus-article-buffer name)
4404         (setq gnus-original-article-buffer original)
4405         (gnus-set-global-variables)))
4406     (gnus-article-setup-highlight-words)
4407     ;; Init original article buffer.
4408     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4409       (mm-enable-multibyte)
4410       (setq major-mode 'gnus-original-article-mode)
4411       (make-local-variable 'gnus-original-article))
4412     (if (and (get-buffer name)
4413              (with-current-buffer name
4414                (if gnus-article-edit-mode
4415                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4416                        (progn
4417                          (set-buffer-modified-p nil)
4418                          (gnus-kill-buffer name)
4419                          (message "")
4420                          nil)
4421                      (error "Action aborted"))
4422                  t)))
4423         (with-current-buffer name
4424           (set (make-local-variable 'gnus-article-edit-mode) nil)
4425           (when gnus-article-mime-handles
4426             (mm-destroy-parts gnus-article-mime-handles)
4427             (setq gnus-article-mime-handles nil))
4428           ;; Set it to nil in article-buffer!
4429           (setq gnus-article-mime-handle-alist nil)
4430           (buffer-disable-undo)
4431           (setq buffer-read-only t)
4432           ;; This list just keeps growing if we don't reset it.
4433           (setq gnus-button-marker-list nil)
4434           (unless (eq major-mode 'gnus-article-mode)
4435             (gnus-article-mode))
4436           (current-buffer))
4437       (with-current-buffer (gnus-get-buffer-create name)
4438         (gnus-article-mode)
4439         (make-local-variable 'gnus-summary-buffer)
4440         (setq gnus-summary-buffer
4441               (gnus-summary-buffer-name gnus-newsgroup-name))
4442         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4443         (current-buffer)))))
4444
4445 ;; Set article window start at LINE, where LINE is the number of lines
4446 ;; from the head of the article.
4447 (defun gnus-article-set-window-start (&optional line)
4448   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4449     (when article-window
4450       (set-window-start
4451        article-window
4452        (with-current-buffer gnus-article-buffer
4453          (goto-char (point-min))
4454          (if (not line)
4455              (point-min)
4456            (gnus-message 6 "Moved to bookmark")
4457            (search-forward "\n\n" nil t)
4458            (forward-line line)
4459            (point)))))))
4460
4461 (defun gnus-article-prepare (article &optional all-headers header)
4462   "Prepare ARTICLE in article mode buffer.
4463 ARTICLE should either be an article number or a Message-ID.
4464 If ARTICLE is an id, HEADER should be the article headers.
4465 If ALL-HEADERS is non-nil, no headers are hidden."
4466   (save-excursion
4467     ;; Make sure we start in a summary buffer.
4468     (unless (eq major-mode 'gnus-summary-mode)
4469       (set-buffer gnus-summary-buffer))
4470     (setq gnus-summary-buffer (current-buffer))
4471     (let* ((gnus-article (if header (mail-header-number header) article))
4472            (summary-buffer (current-buffer))
4473            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4474            (group gnus-newsgroup-name)
4475            result)
4476       (save-excursion
4477         (gnus-article-setup-buffer)
4478         (set-buffer gnus-article-buffer)
4479         ;; Deactivate active regions.
4480         (when (and (boundp 'transient-mark-mode)
4481                    transient-mark-mode)
4482           (setq mark-active nil))
4483         (if (not (setq result (let ((inhibit-read-only t))
4484                                 (gnus-request-article-this-buffer
4485                                  article group))))
4486             ;; There is no such article.
4487             (save-excursion
4488               (when (and (numberp article)
4489                          (not (memq article gnus-newsgroup-sparse)))
4490                 (setq gnus-article-current
4491                       (cons gnus-newsgroup-name article))
4492                 (set-buffer gnus-summary-buffer)
4493                 (setq gnus-current-article article)
4494                 (if (and (memq article gnus-newsgroup-undownloaded)
4495                          (not (gnus-online (gnus-find-method-for-group
4496                                             gnus-newsgroup-name))))
4497                     (progn
4498                       (gnus-summary-set-agent-mark article)
4499                       (message "Message marked for downloading"))
4500                   (gnus-summary-mark-article article gnus-canceled-mark)
4501                   (unless (memq article gnus-newsgroup-sparse)
4502                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4503           (if (or (eq result 'pseudo)
4504                   (eq result 'nneething))
4505               (progn
4506                 (with-current-buffer summary-buffer
4507                   (push article gnus-newsgroup-history)
4508                   (setq gnus-last-article gnus-current-article
4509                         gnus-current-article 0
4510                         gnus-current-headers nil
4511                         gnus-article-current nil)
4512                   (if (eq result 'nneething)
4513                       (gnus-configure-windows 'summary)
4514                     (gnus-configure-windows 'article))
4515                   (gnus-set-global-variables))
4516                 (let ((gnus-article-mime-handle-alist-1
4517                        gnus-article-mime-handle-alist))
4518                   (gnus-set-mode-line 'article)))
4519             ;; The result from the `request' was an actual article -
4520             ;; or at least some text that is now displayed in the
4521             ;; article buffer.
4522             (when (and (numberp article)
4523                        (not (eq article gnus-current-article)))
4524               ;; Seems like a new article has been selected.
4525               ;; `gnus-current-article' must be an article number.
4526               (with-current-buffer summary-buffer
4527                 (push article gnus-newsgroup-history)
4528                 (setq gnus-last-article gnus-current-article
4529                       gnus-current-article article
4530                       gnus-current-headers
4531                       (gnus-summary-article-header gnus-current-article)
4532                       gnus-article-current
4533                       (cons gnus-newsgroup-name gnus-current-article))
4534                 (unless (vectorp gnus-current-headers)
4535                   (setq gnus-current-headers nil))
4536                 (gnus-summary-goto-subject gnus-current-article)
4537                 (when (gnus-summary-show-thread)
4538                   ;; If the summary buffer really was folded, the
4539                   ;; previous goto may not actually have gone to
4540                   ;; the right article, but the thread root instead.
4541                   ;; So we go again.
4542                   (gnus-summary-goto-subject gnus-current-article))
4543                 (gnus-run-hooks 'gnus-mark-article-hook)
4544                 (gnus-set-mode-line 'summary)
4545                 (when (gnus-visual-p 'article-highlight 'highlight)
4546                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4547                 ;; Set the global newsgroup variables here.
4548                 (gnus-set-global-variables)
4549                 (setq gnus-have-all-headers
4550                       (or all-headers gnus-show-all-headers))))
4551             (save-excursion
4552               (gnus-configure-windows 'article))
4553             (when (or (numberp article)
4554                       (stringp article))
4555               (gnus-article-prepare-display)
4556               ;; Do page break.
4557               (goto-char (point-min))
4558               (when gnus-break-pages
4559                 (gnus-narrow-to-page)))
4560             (let ((gnus-article-mime-handle-alist-1
4561                    gnus-article-mime-handle-alist))
4562               (gnus-set-mode-line 'article))
4563             (article-goto-body)
4564             (unless (bobp)
4565               (forward-line -1))
4566             (set-window-point (get-buffer-window (current-buffer)) (point))
4567             (gnus-configure-windows 'article)
4568             t))))))
4569
4570 ;;;###autoload
4571 (defun gnus-article-prepare-display ()
4572   "Make the current buffer look like a nice article."
4573   ;; Hooks for getting information from the article.
4574   ;; This hook must be called before being narrowed.
4575   (let ((gnus-article-buffer (current-buffer))
4576         buffer-read-only
4577         (inhibit-read-only t))
4578     (unless (eq major-mode 'gnus-article-mode)
4579       (gnus-article-mode))
4580     (setq buffer-read-only nil
4581           gnus-article-wash-types nil
4582           gnus-article-image-alist nil)
4583     (gnus-run-hooks 'gnus-tmp-internal-hook)
4584     (when gnus-display-mime-function
4585       (funcall gnus-display-mime-function))
4586     (gnus-run-hooks 'gnus-article-prepare-hook)))
4587
4588 ;;;
4589 ;;; Gnus Sticky Article Mode
4590 ;;;
4591
4592 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4593   "Mode for sticky articles."
4594   ;; Release bindings that won't work.
4595   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4596                              gnus-sticky-article-mode-map)
4597   (substitute-key-definition 'gnus-article-refer-article 'undefined
4598                              gnus-sticky-article-mode-map)
4599   (dolist (k '("e" "h" "s" "F" "R"))
4600     (define-key gnus-sticky-article-mode-map k nil))
4601   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4602   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4603   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4604   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4605
4606 (defun gnus-sticky-article (arg)
4607   "Make the current article sticky.
4608 If a prefix ARG is given, ask for a name for this sticky article buffer."
4609   (interactive "P")
4610   (gnus-summary-show-thread)
4611   (gnus-summary-select-article nil nil 'pseudo)
4612   (let (new-art-buf-name)
4613     (gnus-eval-in-buffer-window gnus-article-buffer
4614       (setq new-art-buf-name
4615             (concat
4616              "*Sticky Article: "
4617              (if arg
4618                  (read-from-minibuffer "Sticky article buffer name: ")
4619                (gnus-with-article-headers
4620                  (gnus-article-goto-header "subject")
4621                  (setq new-art-buf-name
4622                        (buffer-substring-no-properties
4623                         (line-beginning-position) (line-end-position)))
4624                  (goto-char (point-min))
4625                  (gnus-article-goto-header "from")
4626                  (setq new-art-buf-name
4627                        (concat
4628                         new-art-buf-name ", "
4629                         (buffer-substring-no-properties
4630                          (line-beginning-position) (line-end-position))))
4631                  (goto-char (point-min))
4632                  (gnus-article-goto-header "date")
4633                  (setq new-art-buf-name
4634                        (concat
4635                         new-art-buf-name ", "
4636                         (buffer-substring-no-properties
4637                          (line-beginning-position) (line-end-position))))))
4638              "*"))
4639       (if (and (gnus-buffer-live-p new-art-buf-name)
4640                (with-current-buffer new-art-buf-name
4641                  (eq major-mode 'gnus-sticky-article-mode)))
4642           (switch-to-buffer new-art-buf-name)
4643         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4644       (gnus-sticky-article-mode))
4645     (setq gnus-article-buffer new-art-buf-name))
4646   (gnus-summary-recenter)
4647   (gnus-summary-position-point))
4648
4649 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4650   "Kill the given sticky article BUFFER.
4651 If none is given, assume the current buffer and kill it if it has
4652 `gnus-sticky-article-mode'."
4653   (interactive)
4654   (unless buffer
4655     (setq buffer (current-buffer)))
4656   (with-current-buffer buffer
4657     (when (eq major-mode 'gnus-sticky-article-mode)
4658       (gnus-kill-buffer buffer))))
4659
4660 (defun gnus-kill-sticky-article-buffers (arg)
4661   "Kill all sticky article buffers.
4662 If a prefix ARG is given, ask for confirmation."
4663   (interactive "P")
4664   (dolist (buf (gnus-buffers))
4665     (with-current-buffer buf
4666       (when (eq major-mode 'gnus-sticky-article-mode)
4667         (if (not arg)
4668             (gnus-kill-buffer buf)
4669           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4670             (gnus-kill-buffer buf)))))))
4671
4672 ;;;
4673 ;;; Gnus MIME viewing functions
4674 ;;;
4675
4676 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4677   "Format of the MIME buttons.
4678
4679 Valid specifiers include:
4680 %t  The MIME type
4681 %T  MIME type, along with additional info
4682 %n  The `name' parameter
4683 %d  The description, if any
4684 %l  The length of the encoded part
4685 %p  The part identifier number
4686 %e  Dots if the part isn't displayed
4687
4688 General format specifiers can also be used.  See Info node
4689 `(gnus)Formatting Variables'.")
4690
4691 (defvar gnus-mime-button-line-format-alist
4692   '((?t gnus-tmp-type ?s)
4693     (?T gnus-tmp-type-long ?s)
4694     (?n gnus-tmp-name ?s)
4695     (?d gnus-tmp-description ?s)
4696     (?p gnus-tmp-id ?s)
4697     (?l gnus-tmp-length ?d)
4698     (?e gnus-tmp-dots ?s)))
4699
4700 (defvar gnus-mime-button-commands
4701   '((gnus-article-press-button "\r" "Toggle Display")
4702     (gnus-mime-view-part "v" "View Interactively...")
4703     (gnus-mime-view-part-as-type "t" "View As Type...")
4704     (gnus-mime-view-part-as-charset "C" "View As charset...")
4705     (gnus-mime-save-part "o" "Save...")
4706     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4707     (gnus-mime-replace-part "r" "Replace part")
4708     (gnus-mime-delete-part "d" "Delete part")
4709     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4710     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4711     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4712     (gnus-mime-view-part-externally "e" "View Externally")
4713     (gnus-mime-print-part "p" "Print")
4714     (gnus-mime-pipe-part "|" "Pipe To Command...")
4715     (gnus-mime-action-on-part "." "Take action on the part...")))
4716
4717 (defun gnus-article-mime-part-status ()
4718   (if gnus-article-mime-handle-alist-1
4719       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4720           " (1 part)"
4721         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4722     ""))
4723
4724 (defvar gnus-mime-button-map
4725   (let ((map (make-sparse-keymap)))
4726     (define-key map gnus-mouse-2 'gnus-article-push-button)
4727     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4728     (dolist (c gnus-mime-button-commands)
4729       (define-key map (cadr c) (car c)))
4730     map))
4731
4732 (easy-menu-define
4733   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4734   `("MIME Part"
4735     ,@(mapcar (lambda (c)
4736                 (vector (caddr c) (car c) :active t))
4737               gnus-mime-button-commands)))
4738
4739 (defun gnus-mime-button-menu (event prefix)
4740  "Construct a context-sensitive menu of MIME commands."
4741  (interactive "e\nP")
4742  (save-window-excursion
4743    (let ((pos (event-start event)))
4744      (select-window (posn-window pos))
4745      (goto-char (posn-point pos))
4746      (gnus-article-check-buffer)
4747      (popup-menu gnus-mime-button-menu nil prefix))))
4748
4749 (defun gnus-mime-view-all-parts (&optional handles)
4750   "View all the MIME parts."
4751   (interactive)
4752   (with-current-buffer gnus-article-buffer
4753     (let ((handles (or handles gnus-article-mime-handles))
4754           (mail-parse-charset gnus-newsgroup-charset)
4755           (mail-parse-ignored-charsets
4756            (with-current-buffer gnus-summary-buffer
4757              gnus-newsgroup-ignored-charsets)))
4758       (when handles
4759         (mm-remove-parts handles)
4760         (goto-char (point-min))
4761         (or (search-forward "\n\n") (goto-char (point-max)))
4762         (let ((inhibit-read-only t))
4763           (delete-region (point) (point-max))
4764           (mm-display-parts handles))))))
4765
4766 (defun gnus-article-jump-to-part (n)
4767   "Jump to MIME part N."
4768   (interactive "P")
4769   (pop-to-buffer gnus-article-buffer)
4770   ;; FIXME: why is it necessary?
4771   (sit-for 0)
4772   (let ((parts (length gnus-article-mime-handle-alist)))
4773     (or n (setq n
4774                 (string-to-number
4775                  (read-string ;; Emacs 21 doesn't have `read-number'.
4776                   (format "Jump to part (2..%s): " parts)))))
4777     (unless (and (integerp n) (<= n parts) (>= n 1))
4778       (setq n
4779             (progn
4780               (gnus-message 7 "Invalid part `%s', using %s instead."
4781                             n parts)
4782               parts)))
4783     (gnus-message 9 "Jumping to part %s." n)
4784     (cond ((>= gnus-auto-select-part 1)
4785            (while (and (<= n parts)
4786                        (not (gnus-article-goto-part n)))
4787              (setq n (1+ n))))
4788           ((< gnus-auto-select-part 0)
4789            (while (and (>= n 1)
4790                        (not (gnus-article-goto-part n)))
4791              (setq n (1- n))))
4792           (t
4793            (gnus-article-goto-part n)))))
4794
4795 (eval-when-compile
4796   (defsubst gnus-article-edit-part (handles &optional current-id)
4797     "Edit an article in order to delete a mime part.
4798 This function is exclusively used by `gnus-mime-save-part-and-strip'
4799 and `gnus-mime-delete-part', and not provided at run-time normally."
4800     (gnus-article-edit-article
4801      `(lambda ()
4802         (buffer-disable-undo)
4803         (erase-buffer)
4804         (let ((mail-parse-charset (or gnus-article-charset
4805                                       ',gnus-newsgroup-charset))
4806               (mail-parse-ignored-charsets
4807                (or gnus-article-ignored-charsets
4808                    ',gnus-newsgroup-ignored-charsets))
4809               (mbl mml-buffer-list))
4810           (setq mml-buffer-list nil)
4811           (insert-buffer-substring gnus-original-article-buffer)
4812           (mime-to-mml ',handles)
4813           (setq gnus-article-mime-handles nil)
4814           (let ((mbl1 mml-buffer-list))
4815             (setq mml-buffer-list mbl)
4816             (set (make-local-variable 'mml-buffer-list) mbl1))
4817           (gnus-make-local-hook 'kill-buffer-hook)
4818           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4819      `(lambda (no-highlight)
4820         (let ((mail-parse-charset (or gnus-article-charset
4821                                       ',gnus-newsgroup-charset))
4822               (message-options message-options)
4823               (message-options-set-recipient)
4824               (mail-parse-ignored-charsets
4825                (or gnus-article-ignored-charsets
4826                    ',gnus-newsgroup-ignored-charsets)))
4827           (mml-to-mime)
4828           (mml-destroy-buffers)
4829           (remove-hook 'kill-buffer-hook
4830                        'mml-destroy-buffers t)
4831           (kill-local-variable 'mml-buffer-list))
4832         (gnus-summary-edit-article-done
4833          ,(or (mail-header-references gnus-current-headers) "")
4834          ,(gnus-group-read-only-p)
4835          ,gnus-summary-buffer no-highlight))
4836      t)
4837     (gnus-article-edit-done)
4838     (gnus-summary-expand-window)
4839     (gnus-summary-show-article)
4840     (when (and current-id (integerp gnus-auto-select-part))
4841       (gnus-article-jump-to-part
4842        (if (text-property-any (point-min) (point-max)
4843                               'gnus-part (+ current-id gnus-auto-select-part))
4844            (+ current-id gnus-auto-select-part)
4845          (with-current-buffer gnus-article-buffer
4846            (length gnus-article-mime-handle-alist)))))))
4847
4848 (defun gnus-mime-replace-part (file)
4849   "Replace MIME part under point with an external body."
4850   ;; Useful if file has already been saved to disk
4851   (interactive
4852    (list
4853     (read-file-name "Replace MIME part with file: "
4854                     (or mm-default-directory default-directory)
4855                     nil nil)))
4856   (gnus-mime-save-part-and-strip file))
4857
4858 (defun gnus-mime-save-part-and-strip (&optional file)
4859   "Save the MIME part under point then replace it with an external body.
4860 If FILE is given, use it for the external part."
4861   (interactive)
4862   (gnus-article-check-buffer)
4863   (when (gnus-group-read-only-p)
4864     (error "The current group does not support deleting of parts"))
4865   (when (mm-complicated-handles gnus-article-mime-handles)
4866     (error "\
4867 The current article has a complicated MIME structure, giving up..."))
4868   (let* ((data (get-text-property (point) 'gnus-data))
4869          (id (get-text-property (point) 'gnus-part))
4870          param
4871          (handles gnus-article-mime-handles))
4872     (unless file
4873       (setq file
4874             (and data (mm-save-part data "Delete MIME part and save to: "))))
4875     (when file
4876       (with-current-buffer (mm-handle-buffer data)
4877         (erase-buffer)
4878         (insert "Content-Type: " (mm-handle-media-type data))
4879         (mml-insert-parameter-string (cdr (mm-handle-type data))
4880                                      '(charset))
4881         ;; Add a filename for the sake of saving the part again.
4882         (mml-insert-parameter
4883          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4884         (insert "\n")
4885         (insert "Content-ID: " (message-make-message-id) "\n")
4886         (insert "Content-Transfer-Encoding: binary\n")
4887         (insert "\n"))
4888       (setcdr data
4889               (cdr (mm-make-handle nil
4890                                    `("message/external-body"
4891                                      (access-type . "LOCAL-FILE")
4892                                      (name . ,file)))))
4893       ;; (set-buffer gnus-summary-buffer)
4894       (gnus-article-edit-part handles id))))
4895
4896 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4897 ;; parts...>') but with stripping would be nice.
4898
4899 (defun gnus-mime-delete-part ()
4900   "Delete the MIME part under point.
4901 Replace it with some information about the removed part."
4902   (interactive)
4903   (gnus-article-check-buffer)
4904   (when (gnus-group-read-only-p)
4905     (error "The current group does not support deleting of parts"))
4906   (when (mm-complicated-handles gnus-article-mime-handles)
4907     (error "\
4908 The current article has a complicated MIME structure, giving up..."))
4909   (when (or gnus-expert-user
4910             (gnus-yes-or-no-p "\
4911 Deleting parts may malfunction or destroy the article; continue? "))
4912     (let* ((data (get-text-property (point) 'gnus-data))
4913            (id (get-text-property (point) 'gnus-part))
4914            (handles gnus-article-mime-handles)
4915            (none "(none)")
4916            (description
4917             (let ((desc (mm-handle-description data)))
4918               (when desc
4919                 (mail-decode-encoded-word-string desc))))
4920            (filename
4921             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4922                 none))
4923            (type (mm-handle-media-type data)))
4924       (unless data
4925         (error "No MIME part under point"))
4926       (with-current-buffer (mm-handle-buffer data)
4927         (let ((bsize (format "%s" (buffer-size))))
4928           (erase-buffer)
4929           (insert
4930            (concat
4931             ",----\n"
4932             "| The following attachment has been deleted:\n"
4933             "|\n"
4934             "| Type:           " type "\n"
4935             "| Filename:       " filename "\n"
4936             "| Size (encoded): " bsize " Byte\n"
4937             (when description
4938               (concat    "| Description:    " description "\n"))
4939             "`----\n"))
4940           (setcdr data
4941                   (cdr (mm-make-handle
4942                         nil `("text/plain") nil nil
4943                         (list "attachment")
4944                         (format "Deleted attachment (%s bytes)" bsize))))))
4945       ;; (set-buffer gnus-summary-buffer)
4946       (gnus-article-edit-part handles id))))
4947
4948 (defun gnus-mime-save-part ()
4949   "Save the MIME part under point."
4950   (interactive)
4951   (gnus-article-check-buffer)
4952   (let ((data (get-text-property (point) 'gnus-data)))
4953     (when data
4954       (mm-save-part data))))
4955
4956 (defun gnus-mime-pipe-part ()
4957   "Pipe the MIME part under point to a process."
4958   (interactive)
4959   (gnus-article-check-buffer)
4960   (let ((data (get-text-property (point) 'gnus-data)))
4961     (when data
4962       (mm-pipe-part data))))
4963
4964 (defun gnus-mime-view-part ()
4965   "Interactively choose a viewing method for the MIME part under point."
4966   (interactive)
4967   (gnus-article-check-buffer)
4968   (let ((data (get-text-property (point) 'gnus-data)))
4969     (when data
4970       (setq gnus-article-mime-handles
4971             (mm-merge-handles
4972              gnus-article-mime-handles (setq data (copy-sequence data))))
4973       (mm-interactively-view-part data))))
4974
4975 (defun gnus-mime-view-part-as-type-internal ()
4976   (gnus-article-check-buffer)
4977   (let* ((handle (get-text-property (point) 'gnus-data))
4978          (name (or
4979                 ;; Content-Type: foo/bar; name=...
4980                 (mail-content-type-get (mm-handle-type handle) 'name)
4981                 ;; Content-Disposition: attachment; filename=...
4982                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4983          (def-type (and name (mm-default-file-encoding name))))
4984     (or (and def-type (cons def-type 0))
4985         (and handle
4986              (equal (mm-handle-media-supertype handle) "text")
4987              '("text/plain" . 0))
4988         '("application/octet-stream" . 0))))
4989
4990 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
4991   "Choose a MIME media type, and view the part as such.
4992 If non-nil, PRED is a predicate to use during completion to limit the
4993 available media-types."
4994   (interactive)
4995   (unless mime-type
4996     (setq mime-type
4997           (let ((default (gnus-mime-view-part-as-type-internal)))
4998             (completing-read
4999              (format "View as MIME type (default %s): "
5000                      (car default))
5001              (mapcar #'list (mailcap-mime-types))
5002              pred nil nil nil
5003              (car default)))))
5004   (gnus-article-check-buffer)
5005   (let ((handle (get-text-property (point) 'gnus-data)))
5006     (when handle
5007       (when (equal (mm-handle-media-type handle) "message/external-body")
5008         (unless (mm-handle-cache handle)
5009           (mm-extern-cache-contents handle))
5010         (setq handle (mm-handle-cache handle)))
5011       (setq handle
5012             (mm-make-handle (mm-handle-buffer handle)
5013                             (cons mime-type (cdr (mm-handle-type handle)))
5014                             (mm-handle-encoding handle)
5015                             (mm-handle-undisplayer handle)
5016                             (mm-handle-disposition handle)
5017                             (mm-handle-description handle)
5018                             nil
5019                             (mm-handle-id handle)))
5020       (setq gnus-article-mime-handles
5021             (mm-merge-handles gnus-article-mime-handles handle))
5022       (when (mm-handle-displayed-p handle)
5023         (mm-remove-part handle))
5024       (gnus-mm-display-part handle))))
5025
5026 (defun gnus-mime-copy-part (&optional handle arg)
5027   "Put the MIME part under point into a new buffer.
5028 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5029 are decompressed."
5030   (interactive (list nil current-prefix-arg))
5031   (gnus-article-check-buffer)
5032   (unless handle
5033     (setq handle (get-text-property (point) 'gnus-data)))
5034   (when handle
5035     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5036                                                'name)
5037                         (mail-content-type-get (mm-handle-disposition handle)
5038                                                'filename)))
5039           contents dont-decode charset coding-system)
5040       (mm-with-unibyte-buffer
5041         (mm-insert-part handle)
5042         (setq contents (or (condition-case nil
5043                                (mm-decompress-buffer filename nil 'sig)
5044                              (error
5045                               (setq dont-decode t)
5046                               nil))
5047                            (buffer-string))))
5048       (setq filename (cond (filename (file-name-nondirectory filename))
5049                            (dont-decode "*raw data*")
5050                            (t "*decoded*")))
5051       (cond
5052        (dont-decode)
5053        ((not arg)
5054         (unless (setq charset (mail-content-type-get
5055                                (mm-handle-type handle) 'charset))
5056           (unless (setq coding-system (mm-with-unibyte-buffer
5057                                         (insert contents)
5058                                         (mm-find-buffer-file-coding-system)))
5059             (setq charset gnus-newsgroup-charset))))
5060        ((numberp arg)
5061         (setq charset (or (cdr (assq arg
5062                                      gnus-summary-show-article-charset-alist))
5063                           (mm-read-coding-system "Charset: ")))))
5064       (switch-to-buffer (generate-new-buffer filename))
5065       (if (or coding-system
5066               (and charset
5067                    (setq coding-system (mm-charset-to-coding-system charset))
5068                    (not (eq charset 'ascii))))
5069           (progn
5070             (mm-enable-multibyte)
5071             (insert (mm-decode-coding-string contents coding-system))
5072             (setq buffer-file-coding-system
5073                   (if (boundp 'last-coding-system-used)
5074                       (symbol-value 'last-coding-system-used)
5075                     coding-system)))
5076         (mm-disable-multibyte)
5077         (insert contents)
5078         (setq buffer-file-coding-system mm-binary-coding-system))
5079       ;; We do it this way to make `normal-mode' set the appropriate mode.
5080       (unwind-protect
5081           (progn
5082             (setq buffer-file-name (expand-file-name filename))
5083             (normal-mode))
5084         (setq buffer-file-name nil))
5085       (goto-char (point-min)))))
5086
5087 (defun gnus-mime-print-part (&optional handle filename)
5088   "Print the MIME part under point."
5089   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5090   (gnus-article-check-buffer)
5091   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5092          (contents (and handle (mm-get-part handle)))
5093          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5094          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5095     (when contents
5096         (if printer
5097             (unwind-protect
5098                 (progn
5099                   (mm-save-part-to-file handle file)
5100                   (call-process shell-file-name nil
5101                                 (generate-new-buffer " *mm*")
5102                                 nil
5103                                 shell-command-switch
5104                                 (mm-mailcap-command
5105                                  printer file (mm-handle-type handle))))
5106               (delete-file file))
5107           (with-temp-buffer
5108             (insert contents)
5109             (gnus-print-buffer))
5110           (ps-despool filename)))))
5111
5112 (defun gnus-mime-inline-part (&optional handle arg)
5113   "Insert the MIME part under point into the current buffer.
5114 Compressed files like .gz and .bz2 are decompressed."
5115   (interactive (list nil current-prefix-arg))
5116   (gnus-article-check-buffer)
5117   (unless handle
5118     (setq handle (get-text-property (point) 'gnus-data)))
5119   (when handle
5120     (let ((b (point))
5121           (inhibit-read-only t)
5122           contents charset coding-system)
5123       (if (and (not arg) (mm-handle-undisplayer handle))
5124           (mm-remove-part handle)
5125         (mm-with-unibyte-buffer
5126           (mm-insert-part handle)
5127           (setq contents
5128                 (or (mm-decompress-buffer
5129                      (or (mail-content-type-get (mm-handle-type handle)
5130                                                 'name)
5131                          (mail-content-type-get (mm-handle-disposition handle)
5132                                                 'filename))
5133                      nil t)
5134                     (buffer-string))))
5135         (cond
5136          ((not arg)
5137           (unless (setq charset (mail-content-type-get
5138                                  (mm-handle-type handle) 'charset))
5139             (unless (setq coding-system
5140                           (mm-with-unibyte-buffer
5141                             (insert contents)
5142                             (mm-find-buffer-file-coding-system)))
5143               (setq charset gnus-newsgroup-charset))))
5144          ((numberp arg)
5145           (if (mm-handle-undisplayer handle)
5146               (mm-remove-part handle))
5147           (setq charset
5148                 (or (cdr (assq arg
5149                                gnus-summary-show-article-charset-alist))
5150                     (mm-read-coding-system "Charset: "))))
5151          (t
5152           (if (mm-handle-undisplayer handle)
5153               (mm-remove-part handle))))
5154         (forward-line 2)
5155         (mm-insert-inline
5156          handle
5157          (if (or coding-system
5158                  (and charset
5159                       (setq coding-system
5160                             (mm-charset-to-coding-system charset))
5161                       (not (eq coding-system 'ascii))))
5162              (mm-decode-coding-string contents coding-system)
5163            (mm-string-to-multibyte contents)))
5164         (goto-char b)))))
5165
5166 (defun gnus-mime-set-charset-parameters (handle charset)
5167   "Set CHARSET to parameters in HANDLE.
5168 CHARSET may either be a string or a symbol."
5169   (unless (stringp charset)
5170     (setq charset (symbol-name charset)))
5171   (if (stringp (car handle))
5172       (dolist (h (cdr handle))
5173         (gnus-mime-set-charset-parameters h charset))
5174     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5175                                             "message/external-body")
5176                                      (progn
5177                                        (unless (mm-handle-cache handle)
5178                                          (mm-extern-cache-contents handle))
5179                                        (mm-handle-cache handle))
5180                                    handle)))
5181            (param (assq 'charset (cdr type))))
5182       (if param
5183           (setcdr param charset)
5184         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5185
5186 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5187   "Insert the MIME part under point into the current buffer using the
5188 specified charset."
5189   (interactive (list nil current-prefix-arg))
5190   (gnus-article-check-buffer)
5191   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5192         (fun (get-text-property (point) 'gnus-callback))
5193         (gnus-newsgroup-ignored-charsets 'gnus-all)
5194         charset form preferred parts)
5195     (when handle
5196       (when (prog1
5197                 (and fun
5198                      (setq charset
5199                            (or (cdr (assq
5200                                      arg
5201                                      gnus-summary-show-article-charset-alist))
5202                                (mm-read-coding-system "Charset: "))))
5203               (if (mm-handle-undisplayer handle)
5204                   (mm-remove-part handle)))
5205         (gnus-mime-set-charset-parameters handle charset)
5206         (when (and (consp (setq form (cdr-safe fun)))
5207                    (setq form (ignore-errors
5208                                 (assq 'gnus-mime-display-alternative form)))
5209                    (setq preferred (caddr form))
5210                    (progn
5211                      (when (eq (car preferred) 'quote)
5212                        (setq preferred (cadr preferred)))
5213                      (not (equal preferred
5214                                  (get-text-property (point) 'gnus-data))))
5215                    (setq parts (get-text-property (point) 'gnus-part))
5216                    (setq parts (cdr (assq parts
5217                                           gnus-article-mime-handle-alist)))
5218                    (equal (mm-handle-media-type parts) "multipart/alternative")
5219                    (setq parts (reverse (cdr parts))))
5220           (setcar (cddr form)
5221                   (list 'quote (or (cadr (member preferred parts))
5222                                    (car parts)))))
5223         (funcall fun handle)))))
5224
5225 (defun gnus-mime-view-part-externally (&optional handle)
5226   "View the MIME part under point with an external viewer."
5227   (interactive)
5228   (gnus-article-check-buffer)
5229   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5230          (mm-user-display-methods nil)
5231          (mm-inlined-types nil)
5232          (mail-parse-charset gnus-newsgroup-charset)
5233          (mail-parse-ignored-charsets
5234           (with-current-buffer gnus-summary-buffer
5235             gnus-newsgroup-ignored-charsets))
5236          (type (mm-handle-media-type handle))
5237          (method (mailcap-mime-info type))
5238          (mm-enable-external t))
5239     (if (not (stringp method))
5240         (gnus-mime-view-part-as-type
5241          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5242       (when handle
5243         (if (mm-handle-undisplayer handle)
5244             (mm-remove-part handle)
5245           (mm-display-part handle))))))
5246
5247 (defun gnus-mime-view-part-internally (&optional handle)
5248   "View the MIME part under point with an internal viewer.
5249 If no internal viewer is available, use an external viewer."
5250   (interactive)
5251   (gnus-article-check-buffer)
5252   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5253          (mm-inlined-types '(".*"))
5254          (mm-inline-large-images t)
5255          (mail-parse-charset gnus-newsgroup-charset)
5256          (mail-parse-ignored-charsets
5257           (with-current-buffer gnus-summary-buffer
5258             gnus-newsgroup-ignored-charsets))
5259          (inhibit-read-only t))
5260     (if (not (mm-inlinable-p handle))
5261         (gnus-mime-view-part-as-type
5262          nil (lambda (types) (mm-inlinable-p handle (car types))))
5263       (when handle
5264         (if (mm-handle-undisplayer handle)
5265             (mm-remove-part handle)
5266           (mm-display-part handle))))))
5267
5268 (defun gnus-mime-action-on-part (&optional action)
5269   "Do something with the MIME attachment at \(point\)."
5270   (interactive
5271    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5272   (gnus-article-check-buffer)
5273   (let ((action-pair (assoc action gnus-mime-action-alist)))
5274     (if action-pair
5275         (funcall (cdr action-pair)))))
5276
5277 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5278   "Call FUNCTION on MIME part N.
5279 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5280 If INTERACTIVE, call FUNCTION interactivly."
5281   (let (window frame)
5282     ;; Check whether the article is displayed.
5283     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5284                  (setq window (get-buffer-window gnus-article-buffer t))
5285                  (frame-visible-p (setq frame (window-frame window))))
5286       (error "No article is displayed"))
5287     (with-current-buffer gnus-article-buffer
5288       ;; Check whether the article displays the right contents.
5289       (unless (with-current-buffer gnus-summary-buffer
5290                 (eq gnus-current-article (gnus-summary-article-number)))
5291         (error "You should select the right article first"))
5292       (if n
5293           (setq n (prefix-numeric-value n))
5294         (let ((pt (point)))
5295           (setq n (or (get-text-property pt 'gnus-part)
5296                       (and (not (bobp))
5297                            (get-text-property (1- pt) 'gnus-part))
5298                       (get-text-property (prog2
5299                                              (forward-line 1)
5300                                              (point)
5301                                            (goto-char pt))
5302                                          'gnus-part)
5303                       (get-text-property
5304                        (or (and (setq pt (previous-single-property-change
5305                                           pt 'gnus-part))
5306                                 (1- pt))
5307                            (next-single-property-change (point) 'gnus-part)
5308                            (point))
5309                        'gnus-part)
5310                       1))))
5311       ;; Check whether the specified part exists.
5312       (when (> n (length gnus-article-mime-handle-alist))
5313         (error "No such part")))
5314     (unless
5315         (progn
5316           ;; To select the window is needed so that the cursor
5317           ;; might be visible on the MIME button.
5318           (select-window (prog1
5319                              window
5320                            (setq window (selected-window))
5321                            ;; Article may be displayed in the other frame.
5322                            (gnus-select-frame-set-input-focus
5323                             (prog1
5324                                 frame
5325                               (setq frame (selected-frame))))))
5326           (when (gnus-article-goto-part n)
5327             ;; We point the cursor and the arrow at the MIME button
5328             ;; when the `function' prompt the user for something.
5329             (let ((cursor-in-non-selected-windows t)
5330                   (overlay-arrow-string "=>")
5331                   (overlay-arrow-position (point-marker)))
5332               (unwind-protect
5333                   (cond
5334                    ((and no-handle interactive)
5335                     (call-interactively function))
5336                    (no-handle
5337                     (funcall function))
5338                    (interactive
5339                     (call-interactively
5340                      function
5341                      (cdr (assq n gnus-article-mime-handle-alist))))
5342                    (t
5343                     (funcall function
5344                              (cdr (assq n gnus-article-mime-handle-alist)))))
5345                 (set-marker overlay-arrow-position nil)
5346                 (unless gnus-auto-select-part
5347                   (gnus-select-frame-set-input-focus frame)
5348                   (select-window window))))
5349             t))
5350       (if gnus-inhibit-mime-unbuttonizing
5351           ;; This is the default though the program shouldn't reach here.
5352           (error "No such part")
5353         ;; The part which doesn't have the MIME button is selected.
5354         ;; So, we display all the buttons and redo it.
5355         (let ((gnus-inhibit-mime-unbuttonizing t))
5356           (gnus-summary-show-article)
5357           (gnus-article-part-wrapper n function no-handle))))))
5358
5359 (defun gnus-article-pipe-part (n)
5360   "Pipe MIME part N, which is the numerical prefix."
5361   (interactive "P")
5362   (gnus-article-part-wrapper n 'mm-pipe-part))
5363
5364 (defun gnus-article-save-part (n)
5365   "Save MIME part N, which is the numerical prefix."
5366   (interactive "P")
5367   (gnus-article-part-wrapper n 'mm-save-part))
5368
5369 (defun gnus-article-interactively-view-part (n)
5370   "View MIME part N interactively, which is the numerical prefix."
5371   (interactive "P")
5372   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5373
5374 (defun gnus-article-copy-part (n)
5375   "Copy MIME part N, which is the numerical prefix."
5376   (interactive "P")
5377   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5378
5379 (defun gnus-article-view-part-as-charset (n)
5380   "View MIME part N using a specified charset.
5381 N is the numerical prefix."
5382   (interactive "P")
5383   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5384
5385 (defun gnus-article-view-part-externally (n)
5386   "View MIME part N externally, which is the numerical prefix."
5387   (interactive "P")
5388   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5389
5390 (defun gnus-article-inline-part (n)
5391   "Inline MIME part N, which is the numerical prefix."
5392   (interactive "P")
5393   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5394
5395 (defun gnus-article-save-part-and-strip (n)
5396   "Save MIME part N and replace it with an external body.
5397 N is the numerical prefix."
5398   (interactive "P")
5399   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5400
5401 (defun gnus-article-replace-part (n)
5402   "Replace MIME part N with an external body.
5403 N is the numerical prefix."
5404   (interactive "P")
5405   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5406
5407 (defun gnus-article-delete-part (n)
5408   "Delete MIME part N and add some information about the removed part.
5409 N is the numerical prefix."
5410   (interactive "P")
5411   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5412
5413 (defun gnus-article-view-part-as-type (n)
5414   "Choose a MIME media type, and view part N as such.
5415 N is the numerical prefix."
5416   (interactive "P")
5417   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5418
5419 (defun gnus-article-mime-match-handle-first (condition)
5420   (if condition
5421       (let (n)
5422         (dolist (ihandle gnus-article-mime-handle-alist)
5423           (if (and (cond
5424                     ((functionp condition)
5425                      (funcall condition (cdr ihandle)))
5426                     ((eq condition 'undisplayed)
5427                      (not (or (mm-handle-undisplayer (cdr ihandle))
5428                               (equal (mm-handle-media-type (cdr ihandle))
5429                                      "multipart/alternative"))))
5430                     ((eq condition 'undisplayed-alternative)
5431                      (not (mm-handle-undisplayer (cdr ihandle))))
5432                     (t t))
5433                    (gnus-article-goto-part (car ihandle))
5434                    (or (not n) (< (car ihandle) n)))
5435               (setq n (car ihandle))))
5436         (or n 1))
5437     1))
5438
5439 (defun gnus-article-view-part (&optional n)
5440   "View MIME part N, which is the numerical prefix."
5441   (interactive "P")
5442   (with-current-buffer gnus-article-buffer
5443     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5444                              gnus-article-mime-match-handle-function)))
5445     (when (> n (length gnus-article-mime-handle-alist))
5446       (error "No such part"))
5447     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5448       (when (gnus-article-goto-part n)
5449         (if (equal (car handle) "multipart/alternative")
5450             (gnus-article-press-button)
5451           (when (eq (gnus-mm-display-part handle) 'internal)
5452             (gnus-set-window-start)))))))
5453
5454 (defsubst gnus-article-mime-total-parts ()
5455   (if (bufferp (car gnus-article-mime-handles))
5456       1 ;; single part
5457     (1- (length gnus-article-mime-handles))))
5458
5459 (defun gnus-mm-display-part (handle)
5460   "Display HANDLE and fix MIME button."
5461   (let ((id (get-text-property (point) 'gnus-part))
5462         (point (point))
5463         (inhibit-read-only t))
5464     (forward-line 1)
5465     (prog1
5466         (let ((window (selected-window))
5467               (mail-parse-charset gnus-newsgroup-charset)
5468               (mail-parse-ignored-charsets
5469                (if (gnus-buffer-live-p gnus-summary-buffer)
5470                    (with-current-buffer gnus-summary-buffer
5471                      gnus-newsgroup-ignored-charsets)
5472                  nil)))
5473           (save-excursion
5474             (unwind-protect
5475                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5476                       (beg (point)))
5477                   (when win
5478                     (select-window win))
5479                   (goto-char point)
5480                   (forward-line)
5481                   (if (mm-handle-displayed-p handle)
5482                       ;; This will remove the part.
5483                       (mm-display-part handle)
5484                     (save-restriction
5485                       (narrow-to-region (point)
5486                                         (if (eobp) (point) (1+ (point))))
5487                       (mm-display-part handle)
5488                       ;; We narrow to the part itself and
5489                       ;; then call the treatment functions.
5490                       (goto-char (point-min))
5491                       (forward-line 1)
5492                       (narrow-to-region (point) (point-max))
5493                       (gnus-treat-article
5494                        nil id
5495                        (gnus-article-mime-total-parts)
5496                        (mm-handle-media-type handle)))))
5497               (if (window-live-p window)
5498                   (select-window window)))))
5499       (goto-char point)
5500       (gnus-delete-line)
5501       (gnus-insert-mime-button
5502        handle id (list (mm-handle-displayed-p handle)))
5503       (goto-char point))))
5504
5505 (defun gnus-article-goto-part (n)
5506   "Go to MIME part N."
5507   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5508
5509 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5510   (let ((gnus-tmp-name
5511          (or (mail-content-type-get (mm-handle-type handle) 'name)
5512              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5513              (mail-content-type-get (mm-handle-type handle) 'url)
5514              ""))
5515         (gnus-tmp-type (mm-handle-media-type handle))
5516         (gnus-tmp-description (or (mm-handle-description handle) ""))
5517         (gnus-tmp-dots
5518          (if (if displayed (car displayed)
5519                (mm-handle-displayed-p handle))
5520              "" "..."))
5521         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5522                            (buffer-size)))
5523         gnus-tmp-type-long b e)
5524     (when (string-match ".*/" gnus-tmp-name)
5525       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5526     (setq gnus-tmp-type-long (concat gnus-tmp-type
5527                                      (and (not (equal gnus-tmp-name ""))
5528                                           (concat "; " gnus-tmp-name))))
5529     (unless (equal gnus-tmp-description "")
5530       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5531     (unless (bolp)
5532       (insert "\n"))
5533     (setq b (point))
5534     (gnus-eval-format
5535      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5536      `(keymap ,gnus-mime-button-map
5537               gnus-callback gnus-mm-display-part
5538               gnus-part ,gnus-tmp-id
5539               article-type annotation
5540               gnus-data ,handle))
5541     (setq e (if (bolp)
5542                 ;; Exclude a newline.
5543                 (1- (point))
5544               (point)))
5545     (when gnus-article-button-face
5546       (gnus-overlay-put (gnus-make-overlay b e nil t)
5547                         'face gnus-article-button-face))
5548     (widget-convert-button
5549      'link b e
5550      :mime-handle handle
5551      :action 'gnus-widget-press-button
5552      :button-keymap gnus-mime-button-map
5553      :help-echo
5554      (lambda (widget/window &optional overlay pos)
5555        ;; Needed to properly clear the message due to a bug in
5556        ;; wid-edit (XEmacs only).
5557        (if (boundp 'help-echo-owns-message)
5558            (setq help-echo-owns-message t))
5559        (format
5560         "%S: %s the MIME part; %S: more options"
5561         (aref gnus-mouse-2 0)
5562         ;; XEmacs will get a single widget arg; Emacs 21 will get
5563         ;; window, overlay, position.
5564         (if (mm-handle-displayed-p
5565              (if overlay
5566                  (with-current-buffer (gnus-overlay-buffer overlay)
5567                    (widget-get (widget-at (gnus-overlay-start overlay))
5568                                :mime-handle))
5569                (widget-get widget/window :mime-handle)))
5570             "hide" "show")
5571         (aref gnus-down-mouse-3 0))))))
5572
5573 (defun gnus-widget-press-button (elems el)
5574   (goto-char (widget-get elems :from))
5575   (gnus-article-press-button))
5576
5577 (defvar gnus-displaying-mime nil)
5578
5579 (defun gnus-display-mime (&optional ihandles)
5580   "Display the MIME parts."
5581   (save-excursion
5582     (save-selected-window
5583       (let ((window (get-buffer-window gnus-article-buffer))
5584             (point (point)))
5585         (when window
5586           (select-window window)
5587           ;; We have to do this since selecting the window
5588           ;; may change the point.  So we set the window point.
5589           (set-window-point window point)))
5590       (let ((handles ihandles)
5591             (inhibit-read-only t)
5592             handle)
5593         (cond (handles)
5594               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5595                (when gnus-article-emulate-mime
5596                  (mm-uu-dissect-text-parts handles)))
5597               (gnus-article-emulate-mime
5598                (setq handles (mm-uu-dissect))))
5599         (when (and (not ihandles)
5600                    (not gnus-displaying-mime))
5601           ;; Top-level call; we clean up.
5602           (when gnus-article-mime-handles
5603             (mm-destroy-parts gnus-article-mime-handles)
5604             (setq gnus-article-mime-handle-alist nil));; A trick.
5605           (setq gnus-article-mime-handles handles)
5606           ;; We allow users to glean info from the handles.
5607           (when gnus-article-mime-part-function
5608             (gnus-mime-part-function handles)))
5609         (if (and handles
5610                  (or (not (stringp (car handles)))
5611                      (cdr handles)))
5612             (progn
5613               (when (and (not ihandles)
5614                          (not gnus-displaying-mime))
5615                 ;; Clean up for mime parts.
5616                 (article-goto-body)
5617                 (delete-region (point) (point-max)))
5618               (let ((gnus-displaying-mime t))
5619                 (gnus-mime-display-part handles)))
5620           (save-restriction
5621             (article-goto-body)
5622             (narrow-to-region (point) (point-max))
5623             (gnus-treat-article nil 1 1)
5624             (widen)))
5625         (unless ihandles
5626           ;; Highlight the headers.
5627           (save-excursion
5628             (save-restriction
5629               (article-goto-body)
5630               (narrow-to-region (point-min) (point))
5631               (gnus-article-save-original-date
5632                (gnus-treat-article 'head)))))))
5633     ;; Cope with broken MIME messages.
5634     (goto-char (point-max))
5635     (unless (bolp)
5636       (insert "\n"))))
5637
5638 (defcustom gnus-mime-display-multipart-as-mixed nil
5639   "Display \"multipart\" parts as  \"multipart/mixed\".
5640
5641 If t, it overrides nil values of
5642 `gnus-mime-display-multipart-alternative-as-mixed' and
5643 `gnus-mime-display-multipart-related-as-mixed'."
5644   :group 'gnus-article-mime
5645   :type 'boolean)
5646
5647 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5648   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5649   :version "22.1"
5650   :group 'gnus-article-mime
5651   :type 'boolean)
5652
5653 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5654   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5655
5656 If displaying \"text/html\" is discouraged \(see
5657 `mm-discouraged-alternatives'\) images or other material inside a
5658 \"multipart/related\" part might be overlooked when this variable is nil."
5659   :version "22.1"
5660   :group 'gnus-article-mime
5661   :type 'boolean)
5662
5663 (defun gnus-mime-display-part (handle)
5664   (cond
5665    ;; Maybe a broken MIME message.
5666    ((null handle))
5667    ;; Single part.
5668    ((not (stringp (car handle)))
5669     (gnus-mime-display-single handle))
5670    ;; User-defined multipart
5671    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5672     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5673              handle))
5674    ;; multipart/alternative
5675    ((and (equal (car handle) "multipart/alternative")
5676          (not (or gnus-mime-display-multipart-as-mixed
5677                   gnus-mime-display-multipart-alternative-as-mixed)))
5678     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5679       (push (cons id handle) gnus-article-mime-handle-alist)
5680       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5681    ;; multipart/related
5682    ((and (equal (car handle) "multipart/related")
5683          (not (or gnus-mime-display-multipart-as-mixed
5684                   gnus-mime-display-multipart-related-as-mixed)))
5685     ;;;!!!We should find the start part, but we just default
5686     ;;;!!!to the first part.
5687     ;;(gnus-mime-display-part (cadr handle))
5688     ;;;!!! Most multipart/related is an HTML message plus images.
5689     ;;;!!! Unfortunately we are unable to let W3 display those
5690     ;;;!!! included images, so we just display it as a mixed multipart.
5691     ;;(gnus-mime-display-mixed (cdr handle))
5692     ;;;!!! No, w3 can display everything just fine.
5693     (gnus-mime-display-part (cadr handle)))
5694    ((equal (car handle) "multipart/signed")
5695     (gnus-add-wash-type 'signed)
5696     (gnus-mime-display-security handle))
5697    ((equal (car handle) "multipart/encrypted")
5698     (gnus-add-wash-type 'encrypted)
5699     (gnus-mime-display-security handle))
5700    ;; Other multiparts are handled like multipart/mixed.
5701    (t
5702     (gnus-mime-display-mixed (cdr handle)))))
5703
5704 (defun gnus-mime-part-function (handles)
5705   (if (stringp (car handles))
5706       (mapcar 'gnus-mime-part-function (cdr handles))
5707     (funcall gnus-article-mime-part-function handles)))
5708
5709 (defun gnus-mime-display-mixed (handles)
5710   (mapcar 'gnus-mime-display-part handles))
5711
5712 (defun gnus-mime-display-single (handle)
5713   (let ((type (mm-handle-media-type handle))
5714         (ignored gnus-ignored-mime-types)
5715         (not-attachment t)
5716         (move nil)
5717         display text)
5718     (catch 'ignored
5719       (progn
5720         (while ignored
5721           (when (string-match (pop ignored) type)
5722             (throw 'ignored nil)))
5723         (if (and (setq not-attachment
5724                        (and (not (mm-inline-override-p handle))
5725                             (or (not (mm-handle-disposition handle))
5726                                 (equal (car (mm-handle-disposition handle))
5727                                        "inline")
5728                                 (mm-attachment-override-p handle))))
5729                  (mm-automatic-display-p handle)
5730                  (or (and
5731                       (mm-inlinable-p handle)
5732                       (mm-inlined-p handle))
5733                      (mm-automatic-external-display-p type)))
5734             (setq display t)
5735           (when (equal (mm-handle-media-supertype handle) "text")
5736             (setq text t)))
5737         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5738               beg)
5739           (push (cons id handle) gnus-article-mime-handle-alist)
5740           (when (and display
5741                      (equal (mm-handle-media-supertype handle) "message"))
5742             (insert-char
5743              ?\n
5744              (cond ((not (bolp)) 2)
5745                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5746                    (t 1))))
5747           (when (or (not display)
5748                     (not (gnus-unbuttonized-mime-type-p type)))
5749             (gnus-insert-mime-button
5750              handle id (list (or display (and not-attachment text))))
5751             (gnus-article-insert-newline)
5752             ;; Remember modify the number of forward lines.
5753             (setq move t))
5754           (setq beg (point))
5755           (cond
5756            (display
5757             (when move
5758               (forward-line -1)
5759               (setq beg (point)))
5760             (let ((mail-parse-charset gnus-newsgroup-charset)
5761                   (mail-parse-ignored-charsets
5762                    (save-excursion (condition-case ()
5763                                        (set-buffer gnus-summary-buffer)
5764                                      (error))
5765                                    gnus-newsgroup-ignored-charsets)))
5766               (mm-display-part handle t))
5767             (goto-char (point-max)))
5768            ((and text not-attachment)
5769             (when move
5770               (forward-line -1)
5771               (setq beg (point)))
5772             (gnus-article-insert-newline)
5773             (mm-insert-inline
5774              handle
5775              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5776                                                        'charset)
5777                                 (and (equal type "text/calendar") 'utf-8))))
5778                (cond ((not charset)
5779                       (mm-string-as-multibyte (mm-get-part handle)))
5780                      ((eq charset 'gnus-decoded)
5781                       (with-current-buffer (mm-handle-buffer handle)
5782                         (buffer-string)))
5783                      (t
5784                       (mm-decode-string (mm-get-part handle) charset)))))
5785             (goto-char (point-max))))
5786           ;; Do highlighting.
5787           (save-excursion
5788             (save-restriction
5789               (narrow-to-region beg (point))
5790               (if (eq handle gnus-article-mime-handles)
5791                   ;; The format=flowed case.
5792                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5793                 ;; Don't count signature parts that are never displayed.
5794                 ;; The part number should be re-calculated supposing this
5795                 ;; might be a message/rfc822 part.
5796                 (let (handles)
5797                   (dolist (part gnus-article-mime-handles)
5798                     (unless (or (stringp part)
5799                                 (equal (car (mm-handle-type part))
5800                                        "application/pgp-signature"))
5801                       (push part handles)))
5802                   (gnus-treat-article
5803                    nil (length (memq handle handles)) (length handles)
5804                    (mm-handle-media-type handle)))))))))))
5805
5806 (defun gnus-unbuttonized-mime-type-p (type)
5807   "Say whether TYPE is to be unbuttonized."
5808   (unless gnus-inhibit-mime-unbuttonizing
5809     (when (catch 'found
5810             (let ((types gnus-unbuttonized-mime-types))
5811               (while types
5812                 (when (string-match (pop types) type)
5813                   (throw 'found t)))))
5814       (not (catch 'found
5815              (let ((types gnus-buttonized-mime-types))
5816                (while types
5817                  (when (string-match (pop types) type)
5818                    (throw 'found t)))))))))
5819
5820 (defun gnus-article-insert-newline ()
5821   "Insert a newline, but mark it as undeletable."
5822   (gnus-put-text-property
5823    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5824
5825 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5826   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5827          (ihandles handles)
5828          (point (point))
5829          handle (inhibit-read-only t) from props begend not-pref)
5830     (save-window-excursion
5831       (save-restriction
5832         (when ibegend
5833           (narrow-to-region (car ibegend)
5834                             (or (cdr ibegend)
5835                                 (progn
5836                                   (goto-char (car ibegend))
5837                                   (forward-line 2)
5838                                   (point))))
5839           (delete-region (point-min) (point-max))
5840           (mm-remove-parts handles))
5841         (setq begend (list (point-marker)))
5842         ;; Do the toggle.
5843         (unless (setq not-pref (cadr (member preferred ihandles)))
5844           (setq not-pref (car ihandles)))
5845         (when (or ibegend
5846                   (not preferred)
5847                   (not (gnus-unbuttonized-mime-type-p
5848                         "multipart/alternative")))
5849           (gnus-add-text-properties
5850            (setq from (point))
5851            (progn
5852              (insert (format "%d.  " id))
5853              (point))
5854            `(gnus-callback
5855              (lambda (handles)
5856                (unless ,(not ibegend)
5857                  (setq gnus-article-mime-handle-alist
5858                        ',gnus-article-mime-handle-alist))
5859                (gnus-mime-display-alternative
5860                 ',ihandles ',not-pref ',begend ,id))
5861              keymap ,gnus-mime-button-map
5862              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5863              face ,gnus-article-button-face
5864              gnus-part ,id
5865              article-type multipart))
5866           (widget-convert-button 'link from (point)
5867                                  :action 'gnus-widget-press-button
5868                                  :button-keymap gnus-widget-button-keymap)
5869           ;; Do the handles
5870           (while (setq handle (pop handles))
5871             (gnus-add-text-properties
5872              (setq from (point))
5873              (progn
5874                (insert (format "(%c) %-18s"
5875                                (if (equal handle preferred) ?* ? )
5876                                (mm-handle-media-type handle)))
5877                (point))
5878              `(gnus-callback
5879                (lambda (handles)
5880                  (unless ,(not ibegend)
5881                    (setq gnus-article-mime-handle-alist
5882                          ',gnus-article-mime-handle-alist))
5883                  (gnus-mime-display-alternative
5884                   ',ihandles ',handle ',begend ,id))
5885                keymap ,gnus-mime-button-map
5886                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5887                face ,gnus-article-button-face
5888                gnus-part ,id
5889                gnus-data ,handle))
5890             (widget-convert-button 'link from (point)
5891                                    :action 'gnus-widget-press-button
5892                                    :button-keymap gnus-widget-button-keymap)
5893             (insert "  "))
5894           (insert "\n\n"))
5895         (when preferred
5896           (if (stringp (car preferred))
5897               (gnus-display-mime preferred)
5898             (let ((mail-parse-charset gnus-newsgroup-charset)
5899                   (mail-parse-ignored-charsets
5900                    (with-current-buffer gnus-summary-buffer
5901                      gnus-newsgroup-ignored-charsets)))
5902               (mm-display-part preferred)
5903               ;; Do highlighting.
5904               (save-excursion
5905                 (save-restriction
5906                   (narrow-to-region (car begend) (point-max))
5907                   (gnus-treat-article
5908                    nil (length gnus-article-mime-handle-alist)
5909                    (gnus-article-mime-total-parts)
5910                    (mm-handle-media-type handle))))))
5911           (goto-char (point-max))
5912           (setcdr begend (point-marker)))))
5913     (when ibegend
5914       (goto-char point))))
5915
5916 (defconst gnus-article-wash-status-strings
5917   (let ((alist '((cite "c" "Possible hidden citation text"
5918                        " " "All citation text visible")
5919                  (headers "h" "Hidden headers"
5920                           " " "All headers visible.")
5921                  (pgp "p" "Encrypted or signed message status hidden"
5922                       " " "No hidden encryption nor digital signature status")
5923                  (signature "s" "Signature has been hidden"
5924                             " " "Signature is visible")
5925                  (overstrike "o" "Overstrike (^H) characters applied"
5926                              " " "No overstrike characters applied")
5927                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5928                            " " "No /*_emphasis_*/ characters applied")))
5929         result)
5930     (dolist (entry alist result)
5931       (let ((key (nth 0 entry))
5932             (on (copy-sequence (nth 1 entry)))
5933             (on-help (nth 2 entry))
5934             (off (copy-sequence (nth 3 entry)))
5935             (off-help (nth 4 entry)))
5936         (put-text-property 0 1 'help-echo on-help on)
5937         (put-text-property 0 1 'help-echo off-help off)
5938         (push (list key on off) result))))
5939   "Alist of strings describing wash status in the mode line.
5940 Each entry has the form (KEY ON OF), where the KEY is a symbol
5941 representing the particular washing function, ON is the string to use
5942 in the article mode line when the washing function is active, and OFF
5943 is the string to use when it is inactive.")
5944
5945 (defun gnus-article-wash-status-entry (key value)
5946   (let ((entry (assoc key gnus-article-wash-status-strings)))
5947     (if value (nth 1 entry) (nth 2 entry))))
5948
5949 (defun gnus-article-wash-status ()
5950   "Return a string which display status of article washing."
5951   (with-current-buffer gnus-article-buffer
5952     (let ((cite (memq 'cite gnus-article-wash-types))
5953           (headers (memq 'headers gnus-article-wash-types))
5954           (boring (memq 'boring-headers gnus-article-wash-types))
5955           (pgp (memq 'pgp gnus-article-wash-types))
5956           (pem (memq 'pem gnus-article-wash-types))
5957           (signed (memq 'signed gnus-article-wash-types))
5958           (encrypted (memq 'encrypted gnus-article-wash-types))
5959           (signature (memq 'signature gnus-article-wash-types))
5960           (overstrike (memq 'overstrike gnus-article-wash-types))
5961           (emphasis (memq 'emphasis gnus-article-wash-types)))
5962       (concat
5963        (gnus-article-wash-status-entry 'cite cite)
5964        (gnus-article-wash-status-entry 'headers (or headers boring))
5965        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5966        (gnus-article-wash-status-entry 'signature signature)
5967        (gnus-article-wash-status-entry 'overstrike overstrike)
5968        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5969
5970 (defun gnus-add-wash-type (type)
5971   "Add a washing of TYPE to the current status."
5972   (add-to-list 'gnus-article-wash-types type))
5973
5974 (defun gnus-delete-wash-type (type)
5975   "Add a washing of TYPE to the current status."
5976   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5977
5978 (defun gnus-add-image (category image)
5979   "Add IMAGE of CATEGORY to the list of displayed images."
5980   (let ((entry (assq category gnus-article-image-alist)))
5981     (unless entry
5982       (setq entry (list category))
5983       (push entry gnus-article-image-alist))
5984     (nconc entry (list image))))
5985
5986 (defun gnus-delete-images (category)
5987   "Delete all images in CATEGORY."
5988   (let ((entry (assq category gnus-article-image-alist)))
5989     (dolist (image (cdr entry))
5990       (gnus-remove-image image category))
5991     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5992     (gnus-delete-wash-type category)))
5993
5994 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5995
5996 (defun gnus-article-maybe-hide-headers ()
5997   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5998 Provided for backwards compatibility."
5999   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6000                  (not (with-current-buffer gnus-summary-buffer
6001                         gnus-have-all-headers)))
6002              (not gnus-inhibit-hiding))
6003     (gnus-article-hide-headers)))
6004
6005 ;;; Article savers.
6006
6007 (defun gnus-output-to-file (file-name)
6008   "Append the current article to a file named FILE-NAME.
6009 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6010 text and used as the value of the coding cookie which is added to the
6011 top of a file.  Otherwise, this function saves a raw article without
6012 the coding cookie."
6013   (let* ((artbuf (current-buffer))
6014          (file-name-coding-system nnmail-pathname-coding-system)
6015          (coding gnus-article-save-coding-system)
6016          (coding-system-for-read (if coding
6017                                      nil ;; Rely on the coding cookie.
6018                                    mm-text-coding-system))
6019          (coding-system-for-write (or coding
6020                                       mm-text-coding-system-for-write
6021                                       mm-text-coding-system))
6022          (exists (file-exists-p file-name)))
6023     (with-temp-buffer
6024       (when exists
6025         (insert-file-contents file-name)
6026         (goto-char (point-min))
6027         ;; Remove the existing coding cookie.
6028         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6029           (delete-region (match-beginning 0) (match-end 0))))
6030       (goto-char (point-max))
6031       (insert-buffer-substring artbuf)
6032       ;; Append newline at end of the buffer as separator, and then
6033       ;; save it to file.
6034       (goto-char (point-max))
6035       (insert "\n")
6036       (when coding
6037         ;; If the coding system is not suitable to encode the text,
6038         ;; ask a user for a proper one.
6039         (when (fboundp 'select-safe-coding-system)
6040           (setq coding (coding-system-base
6041                         (save-window-excursion
6042                           (select-safe-coding-system (point-min) (point-max)
6043                                                      coding))))
6044           (setq coding-system-for-write
6045                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6046                     coding)))
6047         (goto-char (point-min))
6048         ;; Add the coding cookie.
6049         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6050                         coding-system-for-write)))
6051       (if exists
6052           (progn
6053             (write-region (point-min) (point-max) file-name nil 'no-message)
6054             (message "Appended to %s" file-name))
6055         (write-region (point-min) (point-max) file-name))))
6056   t)
6057
6058 (defun gnus-narrow-to-page (&optional arg)
6059   "Narrow the article buffer to a page.
6060 If given a numerical ARG, move forward ARG pages."
6061   (interactive "P")
6062   (setq arg (if arg (prefix-numeric-value arg) 0))
6063   (with-current-buffer gnus-article-buffer
6064     (widen)
6065     ;; Remove any old next/prev buttons.
6066     (when (gnus-visual-p 'page-marker)
6067       (let ((inhibit-read-only t))
6068         (gnus-remove-text-with-property 'gnus-prev)
6069         (gnus-remove-text-with-property 'gnus-next)))
6070     (let (st nd pt)
6071       (when (save-excursion
6072               (cond ((< arg 0)
6073                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6074                          (prog1
6075                              (setq nd (match-beginning 0)
6076                                    pt nd)
6077                            (when (re-search-backward page-delimiter nil t)
6078                              (setq st (match-end 0))))
6079                        (when (re-search-forward page-delimiter nil t)
6080                          (setq nd (match-beginning 0)
6081                                pt (point-min)))))
6082                     ((> arg 0)
6083                      (if (re-search-forward page-delimiter nil 'move arg)
6084                          (prog1
6085                              (setq st (match-end 0)
6086                                    pt st)
6087                            (when (re-search-forward page-delimiter nil t)
6088                              (setq nd (match-beginning 0))))
6089                        (when (re-search-backward page-delimiter nil t)
6090                          (setq st (match-end 0)
6091                                pt (point-max)))))
6092                     (t
6093                      (when (re-search-backward page-delimiter nil t)
6094                        (goto-char (setq st (match-end 0))))
6095                      (when (re-search-forward page-delimiter nil t)
6096                        (setq nd (match-beginning 0)))
6097                      (or st nd))))
6098         (setq gnus-page-broken t)
6099         (when pt (goto-char pt))
6100         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6101         (when (gnus-visual-p 'page-marker)
6102           (save-excursion
6103             (when nd
6104               (goto-char nd)
6105               (gnus-insert-next-page-button))
6106             (when st
6107               (goto-char st)
6108               (gnus-insert-prev-page-button))))))))
6109
6110 ;; Article mode commands
6111
6112 (defun gnus-article-goto-next-page ()
6113   "Show the next page of the article."
6114   (interactive)
6115   (when (gnus-article-next-page)
6116     (goto-char (point-min))
6117     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6118
6119
6120 (defun gnus-article-goto-prev-page ()
6121   "Show the previous page of the article."
6122   (interactive)
6123   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6124       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6125     (gnus-article-prev-page nil)))
6126
6127 ;; This is cleaner but currently breaks `gnus-pick-mode':
6128 ;;
6129 ;; (defun gnus-article-goto-next-page ()
6130 ;;   "Show the next page of the article."
6131 ;;   (interactive)
6132 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6133 ;;     (gnus-summary-next-page)))
6134 ;;
6135 ;; (defun gnus-article-goto-prev-page ()
6136 ;;   "Show the next page of the article."
6137 ;;   (interactive)
6138 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6139 ;;     (gnus-summary-prev-page)))
6140
6141 (defun gnus-article-next-page (&optional lines)
6142   "Show the next page of the current article.
6143 If end of article, return non-nil.  Otherwise return nil.
6144 Argument LINES specifies lines to be scrolled up."
6145   (interactive "p")
6146   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6147   (if (and (not (and gnus-article-over-scroll
6148                      (> (count-lines (window-start) (point-max))
6149                         (if (featurep 'xemacs)
6150                             (or lines (1- (window-height)))
6151                           (+ (or lines (1- (window-height))) scroll-margin)))))
6152            (save-excursion
6153              (end-of-line)
6154              (and (pos-visible-in-window-p)     ;Not continuation line.
6155                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6156       ;; Nothing in this page.
6157       (if (or (not gnus-page-broken)
6158               (save-excursion
6159                 (save-restriction
6160                   (widen)
6161                   (forward-line)
6162                   (eobp)))) ;Real end-of-buffer?
6163           (progn
6164             (when gnus-article-over-scroll
6165               (gnus-article-next-page-1 lines))
6166             t)                  ;Nothing more.
6167         (gnus-narrow-to-page 1)         ;Go to next page.
6168         nil)
6169     ;; More in this page.
6170     (gnus-article-next-page-1 lines)
6171     nil))
6172
6173 (defmacro gnus-article-beginning-of-window ()
6174   "Move point to the beginning of the window.
6175 In Emacs, the point is placed at the line number which `scroll-margin'
6176 specifies."
6177   (if (featurep 'xemacs)
6178       '(move-to-window-line 0)
6179     '(move-to-window-line
6180       (min (max 0 scroll-margin)
6181            (max 1 (- (window-height)
6182                      (if mode-line-format 1 0)
6183                      (if header-line-format 1 0)
6184                      2))))))
6185
6186 (defun gnus-article-next-page-1 (lines)
6187   (unless (featurep 'xemacs)
6188     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
6189     ;; too many number of lines if `scroll-margin' is set as two or greater.
6190     (when (and (numberp lines)
6191                (> lines 0)
6192                (> scroll-margin 0))
6193       (setq lines (min lines
6194                        (max 0 (- (count-lines (window-start) (point-max))
6195                                  scroll-margin))))))
6196   (condition-case ()
6197       (let ((scroll-in-place nil))
6198         (scroll-up lines))
6199     (end-of-buffer
6200      ;; Long lines may cause an end-of-buffer error.
6201      (goto-char (point-max))))
6202   (gnus-article-beginning-of-window))
6203
6204 (defun gnus-article-prev-page (&optional lines)
6205   "Show previous page of current article.
6206 Argument LINES specifies lines to be scrolled down."
6207   (interactive "p")
6208   (move-to-window-line 0)
6209   (if (and gnus-page-broken
6210            (bobp)
6211            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6212       (progn
6213         (gnus-narrow-to-page -1)        ;Go to previous page.
6214         (goto-char (point-max))
6215         (recenter (if gnus-article-over-scroll
6216                       (if lines
6217                           (max (if (featurep 'xemacs)
6218                                    lines
6219                                  (+ lines scroll-margin))
6220                                3)
6221                         (- (window-height) 2))
6222                     -1)))
6223     (prog1
6224         (condition-case ()
6225             (let ((scroll-in-place nil))
6226               (scroll-down lines))
6227           (beginning-of-buffer
6228            (goto-char (point-min))))
6229       (gnus-article-beginning-of-window))))
6230
6231 (defun gnus-article-only-boring-p ()
6232   "Decide whether there is only boring text remaining in the article.
6233 Something \"interesting\" is a word of at least two letters that does
6234 not have a face in `gnus-article-boring-faces'."
6235   (when (and gnus-article-skip-boring
6236              (boundp 'gnus-article-boring-faces)
6237              (symbol-value 'gnus-article-boring-faces))
6238     (save-excursion
6239       (let ((inhibit-point-motion-hooks t))
6240         (catch 'only-boring
6241           (while (re-search-forward "\\b\\w\\w" nil t)
6242             (forward-char -1)
6243             (when (not (gnus-intersection
6244                         (gnus-faces-at (point))
6245                         (symbol-value 'gnus-article-boring-faces)))
6246               (throw 'only-boring nil)))
6247           (throw 'only-boring t))))))
6248
6249 (defun gnus-article-refer-article ()
6250   "Read article specified by message-id around point."
6251   (interactive)
6252   (save-excursion
6253     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6254     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6255     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6256         (let ((msg-id (concat "<" (match-string 0) ">")))
6257           (set-buffer gnus-summary-buffer)
6258           (gnus-summary-refer-article msg-id))
6259       (error "No references around point"))))
6260
6261 (defun gnus-article-show-summary ()
6262   "Reconfigure windows to show summary buffer."
6263   (interactive)
6264   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6265       (error "There is no summary buffer for this article buffer")
6266     (gnus-article-set-globals)
6267     (gnus-configure-windows 'article)
6268     (gnus-summary-goto-subject gnus-current-article)
6269     (gnus-summary-position-point)))
6270
6271 (defun gnus-article-describe-briefly ()
6272   "Describe article mode commands briefly."
6273   (interactive)
6274   (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")))
6275
6276 (defun gnus-article-summary-command ()
6277   "Execute the last keystroke in the summary buffer."
6278   (interactive)
6279   (let ((obuf (current-buffer))
6280         (owin (current-window-configuration))
6281         func)
6282     (switch-to-buffer gnus-article-current-summary 'norecord)
6283     (setq func (lookup-key (current-local-map) (this-command-keys)))
6284     (call-interactively func)
6285     (set-buffer obuf)
6286     (set-window-configuration owin)
6287     (set-window-point (get-buffer-window (current-buffer)) (point))))
6288
6289 (defun gnus-article-summary-command-nosave ()
6290   "Execute the last keystroke in the summary buffer."
6291   (interactive)
6292   (let (func)
6293     (pop-to-buffer gnus-article-current-summary)
6294     (setq func (lookup-key (current-local-map) (this-command-keys)))
6295     (call-interactively func)))
6296
6297 (defun gnus-article-check-buffer ()
6298   "Beep if not in an article buffer."
6299   (unless (equal major-mode 'gnus-article-mode)
6300     (error "Command invoked outside of a Gnus article buffer")))
6301
6302 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6303   "Read a summary buffer key sequence and execute it from the article buffer."
6304   (interactive "P")
6305   (gnus-article-check-buffer)
6306   (let ((nosaves
6307          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6308            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6309            "=" "^" "\M-^" "|"))
6310         (nosave-but-article
6311          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6312            "An" "Ap" [?A (meta return)] [?A delete]))
6313         (nosave-in-article
6314          '("AS" "\C-d"))
6315         (up-to-top
6316          '("n" "Gn" "p" "Gp"))
6317         keys new-sum-point)
6318     (with-current-buffer gnus-article-current-summary
6319       (let (gnus-pick-mode)
6320         (setq unread-command-events (nconc unread-command-events
6321                                            (list (or key last-command-event)))
6322               keys (if (featurep 'xemacs)
6323                        (events-to-keys (read-key-sequence nil t))
6324                      (read-key-sequence nil t)))))
6325
6326     (message "")
6327
6328     (cond
6329      ((eq (aref keys (1- (length keys))) ?\C-h)
6330       (gnus-article-describe-bindings (substring keys 0 -1)))
6331      ((or (member keys nosaves)
6332           (member keys nosave-but-article)
6333           (member keys nosave-in-article))
6334       (let (func)
6335         (save-window-excursion
6336           (pop-to-buffer gnus-article-current-summary)
6337           ;; We disable the pick minor mode commands.
6338           (let (gnus-pick-mode)
6339             (setq func (lookup-key (current-local-map) keys))))
6340         (if (or (not func)
6341                 (numberp func))
6342             (ding)
6343           (unless (member keys nosave-in-article)
6344             (set-buffer gnus-article-current-summary))
6345           (call-interactively func)
6346           (setq new-sum-point (point)))
6347         (when (member keys nosave-but-article)
6348           (pop-to-buffer gnus-article-buffer))))
6349      (t
6350       ;; These commands should restore window configuration.
6351       (let ((obuf (current-buffer))
6352             (owin (current-window-configuration))
6353             win func in-buffer selected new-sum-start new-sum-hscroll err)
6354         (cond (not-restore-window
6355                (pop-to-buffer gnus-article-current-summary)
6356                (setq win (selected-window)))
6357               ((setq win (get-buffer-window gnus-article-current-summary))
6358                (select-window win))
6359               (t
6360                (let ((summary-buffer gnus-article-current-summary))
6361                  (gnus-configure-windows 'article)
6362                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6363                    (let ((gnus-buffer-configuration
6364                           '(article ((vertical 1.0
6365                                                (summary 0.25 point)
6366                                                (article 1.0))))))
6367                      (gnus-configure-windows 'article))
6368                    (setq win (get-buffer-window summary-buffer 'visible)))
6369                  (gnus-select-frame-set-input-focus (window-frame win))
6370                  (select-window win))))
6371         (setq in-buffer (current-buffer))
6372         ;; We disable the pick minor mode commands.
6373         (if (and (setq func (let (gnus-pick-mode)
6374                               (lookup-key (current-local-map) keys)))
6375                  (functionp func)
6376                  (condition-case code
6377                      (progn
6378                        (call-interactively func)
6379                        t)
6380                    (error
6381                     (setq err code)
6382                     nil)))
6383             (progn
6384               (when (eq win (selected-window))
6385                 (setq new-sum-point (point)
6386                       new-sum-start (window-start win)
6387                       new-sum-hscroll (window-hscroll win)))
6388               (when (or (eq in-buffer (current-buffer))
6389                         (when (eq obuf (current-buffer))
6390                           (set-buffer in-buffer)
6391                           t))
6392                 (setq selected (gnus-summary-select-article))
6393                 (set-buffer obuf)
6394                 (unless not-restore-window
6395                   (set-window-configuration owin))
6396                 (when (and (eq selected 'old)
6397                            new-sum-point)
6398                   (set-window-start (get-buffer-window (current-buffer))
6399                                     1)
6400                   (set-window-point (get-buffer-window (current-buffer))
6401                                     (if (article-goto-body)
6402                                         (1- (point))
6403                                       (point))))
6404                 (when (and (not not-restore-window)
6405                            new-sum-point
6406                            (window-live-p win)
6407                            (with-current-buffer (window-buffer win)
6408                              (eq major-mode 'gnus-summary-mode)))
6409                   (set-window-point win new-sum-point)
6410                   (set-window-start win new-sum-start)
6411                   (set-window-hscroll win new-sum-hscroll))))
6412           (set-window-configuration owin)
6413           (if err
6414               (signal (car err) (cdr err))
6415             (ding))))))))
6416
6417 (defun gnus-article-read-summary-send-keys ()
6418   (interactive)
6419   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6420     (gnus-article-read-summary-keys)))
6421
6422 (defun gnus-article-describe-key (key)
6423   "Display documentation of the function invoked by KEY.
6424 KEY is a string or a vector."
6425   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6426                        (read-key-sequence "Describe key: "))))
6427   (gnus-article-check-buffer)
6428   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6429                                   gnus-article-read-summary-send-keys))
6430       (with-current-buffer gnus-article-current-summary
6431         (setq unread-command-events
6432               (if (featurep 'xemacs)
6433                   (append key nil)
6434                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6435                                         (list 'meta (- x 128))
6436                                       x))
6437                         key)))
6438         (let ((cursor-in-echo-area t)
6439               gnus-pick-mode)
6440           (describe-key (read-key-sequence nil t))))
6441     (describe-key key)))
6442
6443 (defun gnus-article-describe-key-briefly (key &optional insert)
6444   "Display documentation of the function invoked by KEY.
6445 KEY is a string or a vector."
6446   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6447                        (read-key-sequence "Describe key: "))
6448                      current-prefix-arg))
6449   (gnus-article-check-buffer)
6450   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6451                                   gnus-article-read-summary-send-keys))
6452       (with-current-buffer gnus-article-current-summary
6453         (setq unread-command-events
6454               (if (featurep 'xemacs)
6455                   (append key nil)
6456                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6457                                         (list 'meta (- x 128))
6458                                       x))
6459                         key)))
6460         (let ((cursor-in-echo-area t)
6461               gnus-pick-mode)
6462           (describe-key-briefly (read-key-sequence nil t) insert)))
6463     (describe-key-briefly key insert)))
6464
6465 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6466 (defvar gnus-agent-summary-mode)
6467 (defvar gnus-draft-mode)
6468
6469 (defun gnus-article-describe-bindings (&optional prefix)
6470   "Show a list of all defined keys, and their definitions.
6471 The optional argument PREFIX, if non-nil, should be a key sequence;
6472 then we display only bindings that start with that prefix."
6473   (interactive)
6474   (gnus-article-check-buffer)
6475   (let ((keymap (copy-keymap gnus-article-mode-map))
6476         (map (copy-keymap gnus-article-send-map))
6477         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6478         agent draft)
6479     (define-key keymap "S" map)
6480     (define-key map [t] nil)
6481     (with-current-buffer gnus-article-current-summary
6482       (set-keymap-parent map (key-binding "S"))
6483       (let (key def gnus-pick-mode)
6484         (while sumkeys
6485           (setq key (pop sumkeys))
6486           (cond ((and (vectorp key) (= (length key) 1)
6487                       (consp (setq def (aref key 0)))
6488                       (numberp (car def)) (numberp (cdr def)))
6489                  (when (< (max (car def) (cdr def)) 128)
6490                    (setq sumkeys
6491                          (append (mapcar
6492                                   #'vector
6493                                   (nreverse (gnus-uncompress-range def)))
6494                                  sumkeys))))
6495                 ((setq def (key-binding key))
6496                  (unless (eq def 'undefined)
6497                    (define-key keymap key def))))))
6498       (when (boundp 'gnus-agent-summary-mode)
6499         (setq agent gnus-agent-summary-mode))
6500       (when (boundp 'gnus-draft-mode)
6501         (setq draft gnus-draft-mode)))
6502     (with-temp-buffer
6503       (use-local-map keymap)
6504       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6505       (set (make-local-variable 'gnus-draft-mode) draft)
6506       (describe-bindings prefix))
6507     (let ((item `((lambda (prefix)
6508                     (with-current-buffer ,(current-buffer)
6509                       (gnus-article-describe-bindings prefix)))
6510                   ,prefix)))
6511       (with-current-buffer (if (fboundp 'help-buffer)
6512                                (let (help-xref-following) (help-buffer))
6513                              "*Help*") ;; Emacs 21
6514         (setq help-xref-stack-item item)))))
6515
6516 (defun gnus-article-reply-with-original (&optional wide)
6517   "Start composing a reply mail to the current message.
6518 The text in the region will be yanked.  If the region isn't active,
6519 the entire article will be yanked."
6520   (interactive)
6521   (let ((article (cdr gnus-article-current))
6522         contents)
6523     (if (not (gnus-region-active-p))
6524         (with-current-buffer gnus-summary-buffer
6525           (gnus-summary-reply (list (list article)) wide))
6526       (setq contents (buffer-substring (point) (mark t)))
6527       ;; Deactivate active regions.
6528       (when (and (boundp 'transient-mark-mode)
6529                  transient-mark-mode)
6530         (setq mark-active nil))
6531       (with-current-buffer gnus-summary-buffer
6532         (gnus-summary-reply
6533          (list (list article contents)) wide)))))
6534
6535 (defun gnus-article-wide-reply-with-original ()
6536   "Start composing a wide reply mail to the current message.
6537 The text in the region will be yanked.  If the region isn't active,
6538 the entire article will be yanked."
6539   (interactive)
6540   (gnus-article-reply-with-original t))
6541
6542 (defun gnus-article-followup-with-original ()
6543   "Compose a followup to the current article.
6544 The text in the region will be yanked.  If the region isn't active,
6545 the entire article will be yanked."
6546   (interactive)
6547   (let ((article (cdr gnus-article-current))
6548         contents)
6549       (if (not (gnus-region-active-p))
6550           (with-current-buffer gnus-summary-buffer
6551             (gnus-summary-followup (list (list article))))
6552         (setq contents (buffer-substring (point) (mark t)))
6553         ;; Deactivate active regions.
6554         (when (and (boundp 'transient-mark-mode)
6555                    transient-mark-mode)
6556           (setq mark-active nil))
6557         (with-current-buffer gnus-summary-buffer
6558           (gnus-summary-followup
6559            (list (list article contents)))))))
6560
6561 (defun gnus-article-hide (&optional arg force)
6562   "Hide all the gruft in the current article.
6563 This means that signatures, cited text and (some) headers will be
6564 hidden.
6565 If given a prefix, show the hidden text instead."
6566   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6567   (gnus-article-hide-headers arg)
6568   (gnus-article-hide-list-identifiers arg)
6569   (gnus-article-hide-citation-maybe arg force)
6570   (gnus-article-hide-signature arg))
6571
6572 (defun gnus-article-maybe-highlight ()
6573   "Do some article highlighting if article highlighting is requested."
6574   (when (gnus-visual-p 'article-highlight 'highlight)
6575     (gnus-article-highlight-some)))
6576
6577 (defun gnus-check-group-server ()
6578   ;; Make sure the connection to the server is alive.
6579   (unless (gnus-server-opened
6580            (gnus-find-method-for-group gnus-newsgroup-name))
6581     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6582     (gnus-request-group gnus-newsgroup-name t)))
6583
6584 (eval-when-compile
6585   (autoload 'nneething-get-file-name "nneething"))
6586
6587 (defun gnus-request-article-this-buffer (article group)
6588   "Get an article and insert it into this buffer."
6589   (let (do-update-line sparse-header)
6590     (prog1
6591         (save-excursion
6592           (erase-buffer)
6593           (gnus-kill-all-overlays)
6594           (setq group (or group gnus-newsgroup-name))
6595
6596           ;; Using `gnus-request-article' directly will insert the article into
6597           ;; `nntp-server-buffer' - so we'll save some time by not having to
6598           ;; copy it from the server buffer into the article buffer.
6599
6600           ;; We only request an article by message-id when we do not have the
6601           ;; headers for it, so we'll have to get those.
6602           (when (stringp article)
6603             (gnus-read-header article))
6604
6605           ;; If the article number is negative, that means that this article
6606           ;; doesn't belong in this newsgroup (possibly), so we find its
6607           ;; message-id and request it by id instead of number.
6608           (when (and (numberp article)
6609                      gnus-summary-buffer
6610                      (get-buffer gnus-summary-buffer)
6611                      (gnus-buffer-exists-p gnus-summary-buffer))
6612             (with-current-buffer gnus-summary-buffer
6613               (let ((header (gnus-summary-article-header article)))
6614                 (when (< article 0)
6615                   (cond
6616                    ((memq article gnus-newsgroup-sparse)
6617                     ;; This is a sparse gap article.
6618                     (setq do-update-line article)
6619                     (setq article (mail-header-id header))
6620                     (setq sparse-header (gnus-read-header article))
6621                     (setq gnus-newsgroup-sparse
6622                           (delq article gnus-newsgroup-sparse)))
6623                    ((vectorp header)
6624                     ;; It's a real article.
6625                     (setq article (mail-header-id header)))
6626                    (t
6627                     ;; It is an extracted pseudo-article.
6628                     (setq article 'pseudo)
6629                     (gnus-request-pseudo-article header))))
6630
6631                 (let ((method (gnus-find-method-for-group
6632                                gnus-newsgroup-name)))
6633                   (when (and (eq (car method) 'nneething)
6634                              (vectorp header))
6635                     (let ((dir (nneething-get-file-name
6636                                 (mail-header-id header))))
6637                       (when (and (stringp dir)
6638                                  (file-directory-p dir))
6639                         (setq article 'nneething)
6640                         (gnus-group-enter-directory dir))))))))
6641
6642           (cond
6643            ;; Refuse to select canceled articles.
6644            ((and (numberp article)
6645                  gnus-summary-buffer
6646                  (get-buffer gnus-summary-buffer)
6647                  (gnus-buffer-exists-p gnus-summary-buffer)
6648                  (eq (cdr (with-current-buffer gnus-summary-buffer
6649                             (assq article gnus-newsgroup-reads)))
6650                      gnus-canceled-mark))
6651             nil)
6652            ;; We first check `gnus-original-article-buffer'.
6653            ((and (get-buffer gnus-original-article-buffer)
6654                  (numberp article)
6655                  (with-current-buffer gnus-original-article-buffer
6656                    (and (equal (car gnus-original-article) group)
6657                         (eq (cdr gnus-original-article) article))))
6658             ;; `insert-buffer-substring' would incorrectly use the
6659             ;; equivalent of string-make-multibyte which amount to decoding
6660             ;; with locale-coding-system, causing failure of
6661             ;; subsequent decoding.
6662             (insert (mm-string-to-multibyte
6663                      (with-current-buffer gnus-original-article-buffer
6664                        (buffer-substring (point-min) (point-max)))))
6665             'article)
6666            ;; Check the backlog.
6667            ((and gnus-keep-backlog
6668                  (gnus-backlog-request-article group article (current-buffer)))
6669             'article)
6670            ;; Check asynchronous pre-fetch.
6671            ((gnus-async-request-fetched-article group article (current-buffer))
6672             (gnus-async-prefetch-next group article gnus-summary-buffer)
6673             (when (and (numberp article) gnus-keep-backlog)
6674               (gnus-backlog-enter-article group article (current-buffer)))
6675             'article)
6676            ;; Check the cache.
6677            ((and gnus-use-cache
6678                  (numberp article)
6679                  (gnus-cache-request-article article group))
6680             'article)
6681            ;; Check the agent cache.
6682            ((gnus-agent-request-article article group)
6683             'article)
6684            ;; Get the article and put into the article buffer.
6685            ((or (stringp article)
6686                 (numberp article))
6687             (let ((gnus-override-method gnus-override-method)
6688                   (methods (and (stringp article)
6689                                 gnus-refer-article-method))
6690                   (backend (car (gnus-find-method-for-group
6691                                  gnus-newsgroup-name)))
6692                   result
6693                   (inhibit-read-only t))
6694               (if (or (not (listp methods))
6695                       (and (symbolp (car methods))
6696                            (assq (car methods) nnoo-definition-alist)))
6697                   (setq methods (list methods)))
6698               (when (and (null gnus-override-method)
6699                          methods)
6700                 (setq gnus-override-method (pop methods)))
6701               (while (not result)
6702                 (when (eq gnus-override-method 'current)
6703                   (setq gnus-override-method
6704                         (with-current-buffer gnus-summary-buffer
6705                           gnus-current-select-method)))
6706                 (erase-buffer)
6707                 (gnus-kill-all-overlays)
6708                 (let ((gnus-newsgroup-name group))
6709                   (gnus-check-group-server))
6710                 (cond
6711                  ((gnus-request-article article group (current-buffer))
6712                   (when (numberp article)
6713                     (gnus-async-prefetch-next group article
6714                                               gnus-summary-buffer)
6715                     (when gnus-keep-backlog
6716                       (gnus-backlog-enter-article
6717                        group article (current-buffer))))
6718                   (setq result 'article))
6719                  (methods
6720                   (setq gnus-override-method (pop methods)))
6721                  ((not (string-match "^400 "
6722                                      (nnheader-get-report backend)))
6723                   ;; If we get 400 server disconnect, reconnect and
6724                   ;; retry; otherwise, assume the article has expired.
6725                   (setq result 'done))))
6726               (and (eq result 'article) 'article)))
6727            ;; It was a pseudo.
6728            (t article)))
6729
6730       ;; Associate this article with the current summary buffer.
6731       (setq gnus-article-current-summary gnus-summary-buffer)
6732
6733       ;; Take the article from the original article buffer
6734       ;; and place it in the buffer it's supposed to be in.
6735       (when (and (get-buffer gnus-article-buffer)
6736                  (equal (buffer-name (current-buffer))
6737                         (buffer-name (get-buffer gnus-article-buffer))))
6738         (save-excursion
6739           (if (get-buffer gnus-original-article-buffer)
6740               (set-buffer gnus-original-article-buffer)
6741             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6742             (buffer-disable-undo)
6743             (setq major-mode 'gnus-original-article-mode)
6744             (setq buffer-read-only t))
6745           (let ((inhibit-read-only t))
6746             (erase-buffer)
6747             (insert-buffer-substring gnus-article-buffer))
6748           (setq gnus-original-article (cons group article)))
6749
6750         ;; Decode charsets.
6751         (run-hooks 'gnus-article-decode-hook)
6752         ;; Mark article as decoded or not.
6753         (setq gnus-article-decoded-p gnus-article-decode-hook))
6754
6755       ;; Update sparse articles.
6756       (when (and do-update-line
6757                  (or (numberp article)
6758                      (stringp article)))
6759         (let ((buf (current-buffer)))
6760           (set-buffer gnus-summary-buffer)
6761           (gnus-summary-update-article do-update-line sparse-header)
6762           (gnus-summary-goto-subject do-update-line nil t)
6763           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6764                             (point))
6765           (set-buffer buf))))))
6766
6767 ;;;
6768 ;;; Article editing
6769 ;;;
6770
6771 (defcustom gnus-article-edit-mode-hook nil
6772   "Hook run in article edit mode buffers."
6773   :group 'gnus-article-various
6774   :type 'hook)
6775
6776 (defvar gnus-article-edit-done-function nil)
6777
6778 (defvar gnus-article-edit-mode-map nil)
6779
6780 ;; Should we be using derived.el for this?
6781 (unless gnus-article-edit-mode-map
6782   (setq gnus-article-edit-mode-map (make-keymap))
6783   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6784
6785   (gnus-define-keys gnus-article-edit-mode-map
6786     "\C-c?"    describe-mode
6787     "\C-c\C-c" gnus-article-edit-done
6788     "\C-c\C-k" gnus-article-edit-exit
6789     "\C-c\C-f\C-t" message-goto-to
6790     "\C-c\C-f\C-o" message-goto-from
6791     "\C-c\C-f\C-b" message-goto-bcc
6792     ;;"\C-c\C-f\C-w" message-goto-fcc
6793     "\C-c\C-f\C-c" message-goto-cc
6794     "\C-c\C-f\C-s" message-goto-subject
6795     "\C-c\C-f\C-r" message-goto-reply-to
6796     "\C-c\C-f\C-n" message-goto-newsgroups
6797     "\C-c\C-f\C-d" message-goto-distribution
6798     "\C-c\C-f\C-f" message-goto-followup-to
6799     "\C-c\C-f\C-m" message-goto-mail-followup-to
6800     "\C-c\C-f\C-k" message-goto-keywords
6801     "\C-c\C-f\C-u" message-goto-summary
6802     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6803     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6804     "\C-c\C-b" message-goto-body
6805     "\C-c\C-i" message-goto-signature
6806
6807     "\C-c\C-t" message-insert-to
6808     "\C-c\C-n" message-insert-newsgroups
6809     "\C-c\C-o" message-sort-headers
6810     "\C-c\C-e" message-elide-region
6811     "\C-c\C-v" message-delete-not-region
6812     "\C-c\C-z" message-kill-to-signature
6813     "\M-\r" message-newline-and-reformat
6814     "\C-c\C-a" mml-attach-file
6815     "\C-a" message-beginning-of-line
6816     "\t" message-tab
6817     "\M-;" comment-region)
6818
6819   (gnus-define-keys (gnus-article-edit-wash-map
6820                      "\C-c\C-w" gnus-article-edit-mode-map)
6821     "f" gnus-article-edit-full-stops))
6822
6823 (easy-menu-define
6824   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6825   '("Field"
6826     ["Fetch To" message-insert-to t]
6827     ["Fetch Newsgroups" message-insert-newsgroups t]
6828     "----"
6829     ["To" message-goto-to t]
6830     ["From" message-goto-from t]
6831     ["Subject" message-goto-subject t]
6832     ["Cc" message-goto-cc t]
6833     ["Reply-To" message-goto-reply-to t]
6834     ["Summary" message-goto-summary t]
6835     ["Keywords" message-goto-keywords t]
6836     ["Newsgroups" message-goto-newsgroups t]
6837     ["Followup-To" message-goto-followup-to t]
6838     ["Mail-Followup-To" message-goto-mail-followup-to t]
6839     ["Distribution" message-goto-distribution t]
6840     ["Body" message-goto-body t]
6841     ["Signature" message-goto-signature t]))
6842
6843 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6844   "Major mode for editing articles.
6845 This is an extended text-mode.
6846
6847 \\{gnus-article-edit-mode-map}"
6848   (make-local-variable 'gnus-article-edit-done-function)
6849   (make-local-variable 'gnus-prev-winconf)
6850   (set (make-local-variable 'font-lock-defaults)
6851        '(message-font-lock-keywords t))
6852   (set (make-local-variable 'mail-header-separator) "")
6853   (set (make-local-variable 'gnus-article-edit-mode) t)
6854   (easy-menu-add message-mode-field-menu message-mode-map)
6855   (mml-mode)
6856   (setq buffer-read-only nil)
6857   (buffer-enable-undo)
6858   (widen))
6859
6860 (defun gnus-article-edit (&optional force)
6861   "Edit the current article.
6862 This will have permanent effect only in mail groups.
6863 If FORCE is non-nil, allow editing of articles even in read-only
6864 groups."
6865   (interactive "P")
6866   (when (and (not force)
6867              (gnus-group-read-only-p))
6868     (error "The current newsgroup does not support article editing"))
6869   (gnus-article-date-original)
6870   (gnus-article-edit-article
6871    'ignore
6872    `(lambda (no-highlight)
6873       'ignore
6874       (gnus-summary-edit-article-done
6875        ,(or (mail-header-references gnus-current-headers) "")
6876        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6877
6878 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6879   "Start editing the contents of the current article buffer."
6880   (let ((winconf (current-window-configuration)))
6881     (set-buffer gnus-article-buffer)
6882     (let ((message-auto-save-directory
6883            ;; Don't associate the article buffer with a draft file.
6884            nil))
6885       (gnus-article-edit-mode))
6886     (funcall start-func)
6887     (set-buffer-modified-p nil)
6888     (gnus-configure-windows 'edit-article)
6889     (setq gnus-article-edit-done-function exit-func)
6890     (setq gnus-prev-winconf winconf)
6891     (unless quiet
6892       (gnus-message 6 "C-c C-c to end edits"))))
6893
6894 (defun gnus-article-edit-done (&optional arg)
6895   "Update the article edits and exit."
6896   (interactive "P")
6897   (let ((func gnus-article-edit-done-function)
6898         (buf (current-buffer))
6899         (start (window-start))
6900         (p (point))
6901         (winconf gnus-prev-winconf))
6902     (widen) ;; Widen it in case that users narrowed the buffer.
6903     (funcall func arg)
6904     (set-buffer buf)
6905     ;; The cache and backlog have to be flushed somewhat.
6906     (when gnus-keep-backlog
6907       (gnus-backlog-remove-article
6908        (car gnus-article-current) (cdr gnus-article-current)))
6909     ;; Flush original article as well.
6910     (when (get-buffer gnus-original-article-buffer)
6911       (with-current-buffer gnus-original-article-buffer
6912         (setq gnus-original-article nil)))
6913     (when gnus-use-cache
6914       (gnus-cache-update-article
6915        (car gnus-article-current) (cdr gnus-article-current)))
6916     ;; We remove all text props from the article buffer.
6917     (kill-all-local-variables)
6918     (set-text-properties (point-min) (point-max) nil)
6919     (gnus-article-mode)
6920     (set-window-configuration winconf)
6921     (set-buffer buf)
6922     (set-window-start (get-buffer-window buf) start)
6923     (set-window-point (get-buffer-window buf) (point)))
6924   (gnus-summary-show-article))
6925
6926 (defun gnus-article-edit-exit ()
6927   "Exit the article editing without updating."
6928   (interactive)
6929   (when (or (not (buffer-modified-p))
6930             (yes-or-no-p "Article modified; kill anyway? "))
6931     (let ((curbuf (current-buffer))
6932           (p (point))
6933           (window-start (window-start)))
6934       (erase-buffer)
6935       (if (gnus-buffer-live-p gnus-original-article-buffer)
6936           (insert-buffer-substring gnus-original-article-buffer))
6937       (let ((winconf gnus-prev-winconf))
6938         (kill-all-local-variables)
6939         (gnus-article-mode)
6940         (set-window-configuration winconf)
6941         ;; Tippy-toe some to make sure that point remains where it was.
6942         (save-current-buffer
6943           (set-buffer curbuf)
6944           (set-window-start (get-buffer-window (current-buffer)) window-start)
6945           (goto-char p))))
6946     (gnus-summary-show-article)))
6947
6948 (defun gnus-article-edit-full-stops ()
6949   "Interactively repair spacing at end of sentences."
6950   (interactive)
6951   (save-excursion
6952     (goto-char (point-min))
6953     (search-forward-regexp "^$" nil t)
6954     (let ((case-fold-search nil))
6955       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6956
6957 ;;;
6958 ;;; Article highlights
6959 ;;;
6960
6961 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6962
6963 ;;; Internal Variables:
6964
6965 (defcustom gnus-button-url-regexp
6966   (concat
6967    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
6968    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
6969    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
6970    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
6971        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
6972              (punct "!?:;.,"))
6973          (concat
6974           "\\(?:"
6975           ;; Match paired parentheses, e.g. in Wikipedia URLs:
6976           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
6977           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
6978           "\\|"
6979           "[" chars punct     "]+" "[" chars "]"
6980           "\\)"))
6981      (concat ;; XEmacs 21.4 doesn't support POSIX.
6982       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
6983       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
6984    "\\)")
6985   "Regular expression that matches URLs."
6986   :group 'gnus-article-buttons
6987   :type 'regexp)
6988
6989 (defcustom gnus-button-valid-fqdn-regexp
6990   message-valid-fqdn-regexp
6991   "Regular expression that matches a valid FQDN."
6992   :version "22.1"
6993   :group 'gnus-article-buttons
6994   :type 'regexp)
6995
6996 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6997 (defcustom gnus-button-valid-localpart-regexp
6998   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
6999   "Regular expression that matches a localpart of mail addresses or MIDs."
7000   :version "22.1"
7001   :group 'gnus-article-buttons
7002   :type 'regexp)
7003
7004 (defcustom gnus-button-man-handler 'manual-entry
7005   "Function to use for displaying man pages.
7006 The function must take at least one argument with a string naming the
7007 man page."
7008   :version "22.1"
7009   :type '(choice (function-item :tag "Man" manual-entry)
7010                  (function-item :tag "Woman" woman)
7011                  (function :tag "Other"))
7012   :group 'gnus-article-buttons)
7013
7014 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
7015   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
7016 If the default site is too slow, try to find a CTAN mirror, see
7017 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
7018 the variable `gnus-button-handle-ctan'."
7019   :version "22.1"
7020   :group 'gnus-article-buttons
7021   :link '(custom-manual "(gnus)Group Parameters")
7022   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
7023                  (const "http://tug.ctan.org/tex-archive/")
7024                  (const "http://www.dante.de/CTAN/")
7025                  (string :tag "Other")))
7026
7027 (defcustom gnus-button-ctan-handler 'browse-url
7028   "Function to use for displaying CTAN links.
7029 The function must take one argument, the string naming the URL."
7030   :version "22.1"
7031   :type '(choice (function-item :tag "Browse Url" browse-url)
7032                  (function :tag "Other"))
7033   :group 'gnus-article-buttons)
7034
7035 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
7036   "Bogus strings removed from CTAN URLs."
7037   :version "22.1"
7038   :group 'gnus-article-buttons
7039   :type '(choice (const "^/?tex-archive/\\|/")
7040                  (regexp :tag "Other")))
7041
7042 (defcustom gnus-button-ctan-directory-regexp
7043   (regexp-opt
7044    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
7045          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
7046          "languages" "macros" "nonfree" "obsolete" "support" "systems"
7047          "tds" "tools" "usergrps" "web") t)
7048   "Regular expression for ctan directories.
7049 It should match all directories in the top level of `gnus-ctan-url'."
7050   :version "22.1"
7051   :group 'gnus-article-buttons
7052   :type 'regexp)
7053
7054 (defcustom gnus-button-mid-or-mail-regexp
7055   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7056           gnus-button-valid-fqdn-regexp
7057           ">?\\)\\b")
7058   "Regular expression that matches a message ID or a mail address."
7059   :version "22.1"
7060   :group 'gnus-article-buttons
7061   :type 'regexp)
7062
7063 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7064   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7065 Strings like this can be either a message ID or a mail address.  If it is one
7066 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7067 message ID or a mail address, respectively.  If this variable is set to the
7068 symbol `ask', always query the user what do do.  If it is a function, this
7069 function will be called with the string as its only argument.  The function
7070 must return `mid', `mail', `invalid' or `ask'."
7071   :version "22.1"
7072   :group 'gnus-article-buttons
7073   :type '(choice (function-item :tag "Heuristic function"
7074                                 gnus-button-mid-or-mail-heuristic)
7075                  (const ask)
7076                  (const mid)
7077                  (const mail)))
7078
7079 (defcustom gnus-button-mid-or-mail-heuristic-alist
7080   '((-10.0 . ".+\\$.+@")
7081     (-10.0 . "#")
7082     (-10.0 . "\\*")
7083     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7084     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7085     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7086     (-1.0  . "^[^a-z]+@")
7087     ;;
7088     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7089     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7090     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7091     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7092     ;;
7093     (-2.0  . "^[0-9]")
7094     (-1.0  . "^[0-9][0-9]")
7095     ;;
7096     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7097     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7098     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7099     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7100     ;;
7101     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7102     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7103     ;;       "[0-9]{8,}.*\@"
7104     (-3.0
7105      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7106     ;; "[0-9]{12,}.*\@"
7107     ;; compensation for TDMA dated mail addresses:
7108     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7109     ;;
7110     (-20.0 . "\\.fsf@") ;; Gnus
7111     (-20.0 . "^slrn")
7112     (-20.0 . "^Pine")
7113     (-20.0 . "^alpine\\.")
7114     (-20.0 . "_-_") ;; Subject change in thread
7115     ;;
7116     (-20.0 . "\\.ln@") ;; leafnode
7117     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7118     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7119     ;;
7120     ;; (5.0 . "") ;; $local_part_len <= 7
7121     (10.0  . "^[^0-9]+@")
7122     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7123     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7124     (3.0   . "\@stud")
7125     ;;
7126     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7127     ;;
7128     (0.5   . "^[A-Z][a-z]")
7129     (0.5   . "^[A-Z][a-z][a-z]")
7130     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7131     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7132   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7133
7134 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7135 address.  The REGEXP is processed with `case-fold-search' set to nil."
7136   :version "22.1"
7137   :group 'gnus-article-buttons
7138   :type '(repeat (cons (number :tag "Rate")
7139                        (regexp :tag "Regexp"))))
7140
7141 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7142   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7143 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7144 address, `ask' if unsure and `invalid' if the string is invalid."
7145   (let ((case-fold-search nil)
7146         (list gnus-button-mid-or-mail-heuristic-alist)
7147         (result 0) rate regexp lpartlen elem)
7148     (setq lpartlen
7149           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7150     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7151     ;; Certain special cases...
7152     (when (string-match
7153            (concat
7154             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7155             "^[0-9]+\\.[0-9]+@compuserve\\|"
7156             "@public\\.gmane\\.org")
7157            mid-or-mail)
7158       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7159       (setq result 'mail))
7160     (when (string-match "@.*@\\| " mid-or-mail)
7161       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7162       (setq result 'invalid))
7163     ;; Nothing more to do, if result is not a number here...
7164     (when (numberp result)
7165       (while list
7166         (setq elem (car list)
7167               rate (car elem)
7168               regexp (cdr elem)
7169               list (cdr list))
7170         (when (string-match regexp mid-or-mail)
7171           (setq result (+ result rate))
7172           (gnus-message
7173            9 "`%s' matched `%s', rate `%s', result `%s'."
7174            mid-or-mail regexp rate result)))
7175       (when (<= lpartlen 7)
7176         (setq result (+ result 5.0))
7177         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7178                       mid-or-mail result))
7179       (when (>= lpartlen 12)
7180         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7181         (cond
7182          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7183           ;; Long local part should contain realname if e-mail address,
7184           ;; too many digits: message-id.
7185           ;; $score -= 5.0 + 0.1 * $local_part_len;
7186           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7187           (setq result (+ result rate))
7188           (gnus-message
7189            9 "Many digits in `%s', rate `%s', result `%s'."
7190            mid-or-mail rate result))
7191          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7192                         mid-or-mail)
7193           ;; Too few vowels [^aeiouy]{4,}.*\@
7194           (setq result (+ result -5.0))
7195           (gnus-message
7196            9 "Few vowels in `%s', rate `%s', result `%s'."
7197            mid-or-mail -5.0 result))
7198          (t
7199           (setq result (+ result 5.0))
7200           (gnus-message
7201            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7202     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7203     ;; Maybe we should make this a customizable alist: (condition . 'result)
7204     (cond
7205      ((symbolp result) result)
7206      ;; Now convert number into proper results:
7207      ((< result -10.0) 'mid)
7208      ((> result  10.0) 'mail)
7209      (t 'ask))))
7210
7211 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7212   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7213          (url-mid (concat "news" ":" mid-or-mail))
7214          (url-mailto (concat "mailto" ":" mid-or-mail)))
7215     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7216     (when (fboundp pref)
7217       (setq guessed
7218             ;; get rid of surrounding angles...
7219             (funcall pref
7220                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7221       (if (or (eq 'mid guessed) (eq 'mail guessed))
7222           (setq pref guessed)
7223         (setq pref 'ask)))
7224     (if (eq pref 'ask)
7225         (save-window-excursion
7226           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7227               (setq pref 'mail)
7228             (setq pref 'mid))))
7229     (cond ((eq pref 'mid)
7230            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7231            (gnus-button-handle-news url-mid))
7232           ((eq pref 'mail)
7233            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7234            (gnus-url-mailto url-mailto))
7235           (t (gnus-message 3 "Invalid string.")))))
7236
7237 (defun gnus-button-handle-custom (fun arg)
7238   "Call function FUN on argument ARG.
7239 Both FUN and ARG are supposed to be strings.  ARG will be passed
7240 as a symbol to FUN."
7241   (funcall (intern fun)
7242            (if (string-match "^customize-apropos" fun)
7243                arg
7244              (intern arg))))
7245
7246 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7247
7248 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7249 ;; stuff?
7250
7251 (defun gnus-button-handle-describe-function (url)
7252   "Call `describe-function' when pushing the corresponding URL button."
7253   (describe-function
7254    (intern
7255     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7256
7257 (defun gnus-button-handle-describe-variable (url)
7258   "Call `describe-variable' when pushing the corresponding URL button."
7259   (describe-variable
7260    (intern
7261     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7262
7263 (defun gnus-button-handle-symbol (url)
7264 "Display help on variable or function.
7265 Calls `describe-variable' or `describe-function'."
7266   (let ((sym (intern url)))
7267     (cond
7268      ((fboundp sym) (describe-function sym))
7269      ((boundp sym) (describe-variable sym))
7270      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7271
7272 (defun gnus-button-handle-describe-key (url)
7273   "Call `describe-key' when pushing the corresponding URL button."
7274   (let* ((key-string
7275           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7276          (keys (ignore-errors (eval `(kbd ,key-string)))))
7277     (if keys
7278         (describe-key keys)
7279       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7280
7281 (defun gnus-button-handle-apropos (url)
7282   "Call `apropos' when pushing the corresponding URL button."
7283   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7284
7285 (defun gnus-button-handle-apropos-command (url)
7286   "Call `apropos' when pushing the corresponding URL button."
7287   (apropos-command
7288    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7289
7290 (defun gnus-button-handle-apropos-variable (url)
7291   "Call `apropos' when pushing the corresponding URL button."
7292   (funcall
7293    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7294    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7295
7296 (defun gnus-button-handle-apropos-documentation (url)
7297   "Call `apropos' when pushing the corresponding URL button."
7298   (funcall
7299    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7300    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7301
7302 (defun gnus-button-handle-library (url)
7303   "Call `locate-library' when pushing the corresponding URL button."
7304   (gnus-message 9 "url=`%s'" url)
7305   (let* ((lib (locate-library url))
7306          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7307     (if (not lib)
7308         (gnus-message 1 "Cannot locale library `%s'." url)
7309       (find-file-read-only file))))
7310
7311 (defun gnus-button-handle-ctan (url)
7312   "Call `browse-url' when pushing a CTAN URL button."
7313   (funcall
7314    gnus-button-ctan-handler
7315    (concat
7316     gnus-ctan-url
7317     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7318
7319 (defcustom gnus-button-tex-level 5
7320   "*Integer that says how many TeX-related buttons Gnus will show.
7321 The higher the number, the more buttons will appear and the more false
7322 positives are possible.  Note that you can set this variable local to
7323 specific groups.  Setting it higher in TeX groups is probably a good idea.
7324 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7325 how to set variables in specific groups."
7326   :version "22.1"
7327   :group 'gnus-article-buttons
7328   :link '(custom-manual "(gnus)Group Parameters")
7329   :type 'integer)
7330
7331 (defcustom gnus-button-man-level 5
7332   "*Integer that says how many man-related buttons Gnus will show.
7333 The higher the number, the more buttons will appear and the more false
7334 positives are possible.  Note that you can set this variable local to
7335 specific groups.  Setting it higher in Unix groups is probably a good idea.
7336 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7337 how to set variables in specific groups."
7338   :version "22.1"
7339   :group 'gnus-article-buttons
7340   :link '(custom-manual "(gnus)Group Parameters")
7341   :type 'integer)
7342
7343 (defcustom gnus-button-emacs-level 5
7344   "*Integer that says how many emacs-related buttons Gnus will show.
7345 The higher the number, the more buttons will appear and the more false
7346 positives are possible.  Note that you can set this variable local to
7347 specific groups.  Setting it higher in Emacs or Gnus related groups is
7348 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7349 `gnus-parameters' on how to set variables in specific groups."
7350   :version "22.1"
7351   :group 'gnus-article-buttons
7352   :link '(custom-manual "(gnus)Group Parameters")
7353   :type 'integer)
7354
7355 (defcustom gnus-button-message-level 5
7356   "*Integer that says how many buttons for news or mail messages will appear.
7357 The higher the number, the more buttons will appear and the more false
7358 positives are possible."
7359   ;; mail addresses, MIDs, URLs for news, ...
7360   :version "22.1"
7361   :group 'gnus-article-buttons
7362   :type 'integer)
7363
7364 (defcustom gnus-button-browse-level 5
7365   "*Integer that says how many buttons for browsing will appear.
7366 The higher the number, the more buttons will appear and the more false
7367 positives are possible."
7368   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7369   :version "22.1"
7370   :group 'gnus-article-buttons
7371   :type 'integer)
7372
7373 (defcustom gnus-button-alist
7374   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7375      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7376     ((concat "\\b\\(nntp\\|news\\):\\("
7377              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7378      0 t gnus-button-handle-news 2)
7379     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7380      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7381     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7382      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7383     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7384     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7385      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7386     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7387      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7388     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7389      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7390     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7391      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7392     ;; RFC 2368 (The mailto URL scheme)
7393     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7394      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7395     ("\\bmailto:\\([^ \n\t]+\\)"
7396      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7397     ;; CTAN
7398     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7399              gnus-button-ctan-directory-regexp
7400              "[^][>)!;:,'\n\t ]+\\)")
7401      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7402     ((concat "\\btex-archive/\\("
7403              gnus-button-ctan-directory-regexp
7404              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7405      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7406     ((concat
7407       "\\b\\("
7408       gnus-button-ctan-directory-regexp
7409       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7410      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7411     ;; This is info (home-grown style) <info://foo/bar+baz>
7412     ("\\binfo://\\([^'\">\n\t ]+\\)"
7413      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7414     ;; Info GNOME style <info:foo#bar_baz>
7415     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7416      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7417     ;; Info KDE style <info:(foo)bar baz>
7418     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7419      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7420     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7421      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7422     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7423      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7424      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7425     ;; This is custom
7426     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7427      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7428     ;; Emacs help commands
7429     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7430      ;; regexp doesn't match arguments containing ` '.
7431      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7432     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7433      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7434     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7435      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7436     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7437      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7438     ;; The following entries may lead to many false positives so don't enable
7439     ;; them by default (use a high button level).
7440     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7441      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7442      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7443     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7444      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7445     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7446      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7447     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7448      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7449     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7450      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7451     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7452      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7453     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7454      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7455     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7456      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7457     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7458      ;; Unlike the other regexps we really have to require quoting
7459      ;; here to determine where it ends.
7460      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7461     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7462     ("<URL: *\\([^\n<>]*\\)>"
7463      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7464     ;; RFC 2396 (2.4.3., delims) ...
7465     ("\"URL: *\\([^\n\"]*\\)\""
7466      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7467     ;; Raw URLs.
7468     (gnus-button-url-regexp
7469      0 (>= gnus-button-browse-level 0) browse-url 0)
7470     ;; man pages
7471     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7472      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7473      gnus-button-handle-man 1)
7474     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7475     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7476      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7477      gnus-button-handle-man 1)
7478     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7479     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7480     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7481      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7482     ;; Recognizing patches to .el files.  This is somewhat obscure,
7483     ;; but considering the percentage of Gnus users who hack Emacs
7484     ;; Lisp files...
7485     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7486      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7487     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7488      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7489     ;; MID or mail: To avoid too many false positives we don't try to catch
7490     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7491     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7492     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7493     ;; so that non-ambiguous entries (see above) match first.
7494     (gnus-button-mid-or-mail-regexp
7495      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7496   "*Alist of regexps matching buttons in article bodies.
7497
7498 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7499 REGEXP: is the string (case insensitive) matching text around the button (can
7500 also be Lisp expression evaluating to a string),
7501 BUTTON: is the number of the regexp grouping actually matching the button,
7502 FORM: is a Lisp expression which must eval to true for the button to
7503 be added,
7504 CALLBACK: is the function to call when the user push this button, and each
7505 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7506
7507 CALLBACK can also be a variable, in that case the value of that
7508 variable it the real callback function."
7509   :group 'gnus-article-buttons
7510   :type '(repeat (list (choice regexp variable sexp)
7511                        (integer :tag "Button")
7512                        (sexp :tag "Form")
7513                        (function :tag "Callback")
7514                        (repeat :tag "Par"
7515                                :inline t
7516                                (integer :tag "Regexp group")))))
7517 (put 'gnus-button-alist 'risky-local-variable t)
7518
7519 (defcustom gnus-header-button-alist
7520   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7521      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7522     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7523      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7524     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7525      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7526     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7527      0 (>= gnus-button-browse-level 0) browse-url 0)
7528     ("^Subject:" gnus-button-url-regexp
7529      0 (>= gnus-button-browse-level 0) browse-url 0)
7530     ("^[^:]+:" gnus-button-url-regexp
7531      0 (>= gnus-button-browse-level 0) browse-url 0)
7532     ("^OpenPGP:.*url=" gnus-button-url-regexp
7533      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7534     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7535      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7536     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7537      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7538   "*Alist of headers and regexps to match buttons in article heads.
7539
7540 This alist is very similar to `gnus-button-alist', except that each
7541 alist has an additional HEADER element first in each entry:
7542
7543 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7544
7545 HEADER is a regexp to match a header.  For a fuller explanation, see
7546 `gnus-button-alist'."
7547   :group 'gnus-article-buttons
7548   :group 'gnus-article-headers
7549   :type '(repeat (list (regexp :tag "Header")
7550                        (choice regexp variable)
7551                        (integer :tag "Button")
7552                        (sexp :tag "Form")
7553                        (function :tag "Callback")
7554                        (repeat :tag "Par"
7555                                :inline t
7556                                (integer :tag "Regexp group")))))
7557 (put 'gnus-header-button-alist 'risky-local-variable t)
7558
7559 ;;; Commands:
7560
7561 (defun gnus-article-push-button (event)
7562   "Check text under the mouse pointer for a callback function.
7563 If the text under the mouse pointer has a `gnus-callback' property,
7564 call it with the value of the `gnus-data' text property."
7565   (interactive "e")
7566   (set-buffer (window-buffer (posn-window (event-start event))))
7567   (let* ((pos (posn-point (event-start event)))
7568          (data (get-text-property pos 'gnus-data))
7569          (fun (get-text-property pos 'gnus-callback)))
7570     (goto-char pos)
7571     (when fun
7572       (funcall fun data))))
7573
7574 (defun gnus-article-press-button ()
7575   "Check text at point for a callback function.
7576 If the text at point has a `gnus-callback' property,
7577 call it with the value of the `gnus-data' text property."
7578   (interactive)
7579   (let ((data (get-text-property (point) 'gnus-data))
7580         (fun (get-text-property (point) 'gnus-callback)))
7581     (when fun
7582       (funcall fun data))))
7583
7584 (defun gnus-article-highlight (&optional force)
7585   "Highlight current article.
7586 This function calls `gnus-article-highlight-headers',
7587 `gnus-article-highlight-citation',
7588 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7589 do the highlighting.  See the documentation for those functions."
7590   (interactive (list 'force))
7591   (gnus-article-highlight-headers)
7592   (gnus-article-highlight-citation force)
7593   (gnus-article-highlight-signature)
7594   (gnus-article-add-buttons force)
7595   (gnus-article-add-buttons-to-head))
7596
7597 (defun gnus-article-highlight-some (&optional force)
7598   "Highlight current article.
7599 This function calls `gnus-article-highlight-headers',
7600 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7601 do the highlighting.  See the documentation for those functions."
7602   (interactive (list 'force))
7603   (gnus-article-highlight-headers)
7604   (gnus-article-highlight-signature)
7605   (gnus-article-add-buttons))
7606
7607 (defun gnus-article-highlight-headers ()
7608   "Highlight article headers as specified by `gnus-header-face-alist'."
7609   (interactive)
7610   (gnus-with-article-headers
7611     (let (regexp header-face field-face from hpoints fpoints)
7612       (dolist (entry gnus-header-face-alist)
7613         (goto-char (point-min))
7614         (setq regexp (concat "^\\("
7615                              (if (string-equal "" (nth 0 entry))
7616                                  "[^\t ]"
7617                                (nth 0 entry))
7618                              "\\)")
7619               header-face (nth 1 entry)
7620               field-face (nth 2 entry))
7621         (while (and (re-search-forward regexp nil t)
7622                     (not (eobp)))
7623           (beginning-of-line)
7624           (setq from (point))
7625           (unless (search-forward ":" nil t)
7626             (forward-char 1))
7627           (when (and header-face
7628                      (not (memq (point) hpoints)))
7629             (push (point) hpoints)
7630             (gnus-put-text-property from (point) 'face header-face))
7631           (when (and field-face
7632                      (not (memq (setq from (point)) fpoints)))
7633             (push from fpoints)
7634             (if (re-search-forward "^[^ \t]" nil t)
7635                 (forward-char -2)
7636               (goto-char (point-max)))
7637             (gnus-put-text-property from (point) 'face field-face)))))))
7638
7639 (defun gnus-article-highlight-signature ()
7640   "Highlight the signature in an article.
7641 It does this by highlighting everything after
7642 `gnus-signature-separator' using the face `gnus-signature'."
7643   (interactive)
7644   (gnus-with-article-buffer
7645     (let ((inhibit-point-motion-hooks t))
7646       (save-restriction
7647         (when (and gnus-signature-face
7648                    (gnus-article-narrow-to-signature))
7649           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7650                             'face gnus-signature-face)
7651           (widen)
7652           (gnus-article-search-signature)
7653           (let ((start (match-beginning 0))
7654                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7655             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7656                                      end)))))))
7657
7658 (defun gnus-button-in-region-p (b e prop)
7659   "Say whether PROP exists in the region."
7660   (text-property-not-all b e prop nil))
7661
7662 (defun gnus-article-add-buttons (&optional force)
7663   "Find external references in the article and make buttons of them.
7664 \"External references\" are things like Message-IDs and URLs, as
7665 specified by `gnus-button-alist'."
7666   (interactive (list 'force))
7667   (gnus-with-article-buffer
7668     (let ((inhibit-point-motion-hooks t)
7669           (case-fold-search t)
7670           (alist gnus-button-alist)
7671           beg entry regexp)
7672       ;; Remove all old markers.
7673       (let (marker entry new-list)
7674         (while (setq marker (pop gnus-button-marker-list))
7675           (if (or (< marker (point-min)) (>= marker (point-max)))
7676               (push marker new-list)
7677             (goto-char marker)
7678             (when (setq entry (gnus-button-entry))
7679               (put-text-property (match-beginning (nth 1 entry))
7680                                  (match-end (nth 1 entry))
7681                                  'gnus-callback nil))
7682             (set-marker marker nil)))
7683         (setq gnus-button-marker-list new-list))
7684       ;; We skip the headers.
7685       (article-goto-body)
7686       (setq beg (point))
7687       (while (setq entry (pop alist))
7688         (setq regexp (eval (car entry)))
7689         (goto-char beg)
7690         (while (re-search-forward regexp nil t)
7691           (let ((start (match-beginning (nth 1 entry)))
7692                 (end (match-end (nth 1 entry)))
7693                 (from (match-beginning 0)))
7694             (when (and (or (eq t (nth 2 entry))
7695                            (eval (nth 2 entry)))
7696                        (not (gnus-button-in-region-p
7697                              start end 'gnus-callback)))
7698               ;; That optional form returned non-nil, so we add the
7699               ;; button.
7700               (setq from (set-marker (make-marker) from))
7701               (push from gnus-button-marker-list)
7702               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7703                            (gnus-article-extend-url-button from start end))
7704                 (gnus-article-add-button start end
7705                                          'gnus-button-push from)))))))))
7706
7707 (defun gnus-article-extend-url-button (beg start end)
7708   "Extend url button if url is folded into two or more lines.
7709 Return non-nil if button is extended.  BEG is a marker that points to
7710 the beginning position of a text containing url.  START and END are
7711 the endpoints of a url button before it is extended.  The concatenated
7712 url is put as the `gnus-button-url' overlay property on the button."
7713   (let ((opoint (point))
7714         (points (list start end))
7715         url delim regexp)
7716     (prog1
7717         (when (and (progn
7718                      (goto-char end)
7719                      (not (looking-at "[\t ]*[\">]")))
7720                    (progn
7721                      (goto-char start)
7722                      (string-match
7723                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7724                       (buffer-substring (point-at-bol) start)))
7725                    (progn
7726                      (setq url (list (buffer-substring start end))
7727                            delim (if (match-beginning 1) ">" "\""))
7728                      (beginning-of-line)
7729                      (setq regexp (concat
7730                                    (when (and (looking-at
7731                                                message-cite-prefix-regexp)
7732                                               (< (match-end 0) start))
7733                                      (regexp-quote (match-string 0)))
7734                                    "\
7735 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7736                                    delim "\\)"))
7737                      (while (progn
7738                               (forward-line 1)
7739                               (and (looking-at regexp)
7740                                    (prog1
7741                                        (match-beginning 1)
7742                                      (push (or (match-string 2)
7743                                                (match-string 1))
7744                                            url)
7745                                      (push (setq end (or (match-end 2)
7746                                                          (match-end 1)))
7747                                            points)
7748                                      (push (or (match-beginning 2)
7749                                                (match-beginning 1))
7750                                            points)))))
7751                      (match-beginning 2)))
7752           (let (gnus-article-mouse-face widget-mouse-face)
7753             (while points
7754               (gnus-article-add-button (pop points) (pop points)
7755                                        'gnus-button-push beg)))
7756           (let ((overlay (gnus-make-overlay start end)))
7757             (gnus-overlay-put overlay 'evaporate t)
7758             (gnus-overlay-put overlay 'gnus-button-url
7759                               (list (mapconcat 'identity (nreverse url) "")))
7760             (when gnus-article-mouse-face
7761               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7762           t)
7763       (goto-char opoint))))
7764
7765 ;; Add buttons to the head of an article.
7766 (defun gnus-article-add-buttons-to-head ()
7767   "Add buttons to the head of the article."
7768   (interactive)
7769   (gnus-with-article-headers
7770     (let (beg end)
7771       (dolist (entry gnus-header-button-alist)
7772         ;; Each alist entry.
7773         (goto-char (point-min))
7774         (while (re-search-forward (car entry) nil t)
7775           ;; Each header matching the entry.
7776           (setq beg (match-beginning 0))
7777           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7778                              (match-beginning 0))
7779                         (point-max)))
7780           (goto-char beg)
7781           (while (re-search-forward (eval (nth 1 entry)) end t)
7782             ;; Each match within a header.
7783             (let* ((entry (cdr entry))
7784                    (start (match-beginning (nth 1 entry)))
7785                    (end (match-end (nth 1 entry)))
7786                    (form (nth 2 entry)))
7787               (goto-char (match-end 0))
7788               (when (eval form)
7789                 (gnus-article-add-button
7790                  start end (nth 3 entry)
7791                  (buffer-substring (match-beginning (nth 4 entry))
7792                                    (match-end (nth 4 entry)))))))
7793           (goto-char end))))))
7794
7795 ;;; External functions:
7796
7797 (defun gnus-article-add-button (from to fun &optional data)
7798   "Create a button between FROM and TO with callback FUN and data DATA."
7799   (when gnus-article-button-face
7800     (gnus-overlay-put (gnus-make-overlay from to nil t)
7801                       'face gnus-article-button-face))
7802   (gnus-add-text-properties
7803    from to
7804    (nconc (and gnus-article-mouse-face
7805                (list gnus-mouse-face-prop gnus-article-mouse-face))
7806           (list 'gnus-callback fun)
7807           (and data (list 'gnus-data data))))
7808   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7809                          :button-keymap gnus-widget-button-keymap))
7810
7811 ;;; Internal functions:
7812
7813 (defun gnus-article-set-globals ()
7814   (with-current-buffer gnus-summary-buffer
7815     (gnus-set-global-variables)))
7816
7817 (defun gnus-signature-toggle (end)
7818   (gnus-with-article-buffer
7819     (let ((inhibit-point-motion-hooks t))
7820       (if (text-property-any end (point-max) 'article-type 'signature)
7821           (progn
7822             (gnus-delete-wash-type 'signature)
7823             (gnus-remove-text-properties-when
7824              'article-type 'signature end (point-max)
7825              (cons 'article-type (cons 'signature
7826                                        gnus-hidden-properties))))
7827         (gnus-add-wash-type 'signature)
7828         (gnus-add-text-properties-when
7829          'article-type nil end (point-max)
7830          (cons 'article-type (cons 'signature
7831                                    gnus-hidden-properties)))))
7832     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7833       (gnus-set-mode-line 'article))))
7834
7835 (defun gnus-button-entry ()
7836   ;; Return the first entry in `gnus-button-alist' matching this place.
7837   (let ((alist gnus-button-alist)
7838         (entry nil))
7839     (while alist
7840       (setq entry (pop alist))
7841       (if (looking-at (eval (car entry)))
7842           (setq alist nil)
7843         (setq entry nil)))
7844     entry))
7845
7846 (defun gnus-button-push (marker)
7847   ;; Push button starting at MARKER.
7848   (save-excursion
7849     (goto-char marker)
7850     (let* ((entry (gnus-button-entry))
7851            (inhibit-point-motion-hooks t)
7852            (fun (nth 3 entry))
7853            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7854                           (get-char-property marker 'gnus-button-url))
7855                      (mapcar (lambda (group)
7856                                (let ((string (match-string group)))
7857                                  (set-text-properties
7858                                   0 (length string) nil string)
7859                                  string))
7860                              (nthcdr 4 entry)))))
7861       (cond
7862        ((fboundp fun)
7863         (apply fun args))
7864        ((and (boundp fun)
7865              (fboundp (symbol-value fun)))
7866         (apply (symbol-value fun) args))
7867        (t
7868         (gnus-message 1 "You must define `%S' to use this button"
7869                       (cons fun args)))))))
7870
7871 (defun gnus-parse-news-url (url)
7872   (let (scheme server port group message-id articles)
7873     (with-temp-buffer
7874       (insert url)
7875       (goto-char (point-min))
7876       (when (looking-at "\\([A-Za-z]+\\):")
7877         (setq scheme (match-string 1))
7878         (goto-char (match-end 0)))
7879       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7880         (setq server (match-string 1))
7881         (setq port (if (stringp (match-string 3))
7882                        (string-to-number (match-string 3))
7883                      (match-string 3)))
7884         (goto-char (match-end 0)))
7885
7886       (cond
7887        ((looking-at "\\(.*@.*\\)")
7888         (setq message-id (match-string 1)))
7889        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7890         (setq group (match-string 1)
7891               articles (split-string (match-string 2) "-")))
7892        ((looking-at "\\([^/]+\\)/?")
7893         (setq group (match-string 1)))
7894        (t
7895         (error "Unknown news URL syntax"))))
7896     (list scheme server port group message-id articles)))
7897
7898 (defun gnus-button-handle-news (url)
7899   "Fetch a news URL."
7900   (destructuring-bind (scheme server port group message-id articles)
7901       (gnus-parse-news-url url)
7902     (cond
7903      (message-id
7904       (with-current-buffer gnus-summary-buffer
7905         (if server
7906             (let ((gnus-refer-article-method
7907                    (nconc (list (list 'nntp server))
7908                           gnus-refer-article-method))
7909                   (nntp-port-number (or port "nntp")))
7910               (gnus-message 7 "Fetching %s with %s"
7911                             message-id gnus-refer-article-method)
7912               (gnus-summary-refer-article message-id))
7913           (gnus-summary-refer-article message-id))))
7914      (group
7915       (gnus-button-fetch-group url)))))
7916
7917 (defun gnus-button-patch (library line)
7918   "Visit an Emacs Lisp library LIBRARY on line LINE."
7919   (interactive)
7920   (let ((file (locate-library (file-name-nondirectory library))))
7921     (unless file
7922       (error "Couldn't find library %s" library))
7923     (find-file file)
7924     (goto-line (string-to-number line))))
7925
7926 (defun gnus-button-handle-man (url)
7927   "Fetch a man page."
7928   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7929   (when (eq gnus-button-man-handler 'woman)
7930     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7931   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7932   (funcall gnus-button-man-handler url))
7933
7934 (defun gnus-button-handle-info-url (url)
7935   "Fetch an info URL."
7936   (setq url (mm-subst-char-in-string ?+ ?\  url))
7937   (cond
7938    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7939     (gnus-info-find-node
7940      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7941                      "Gnus")
7942              ")" (gnus-url-unhex-string (match-string 2 url)))))
7943    ((string-match "([^)\"]+)[^\"]+" url)
7944     (setq url
7945           (gnus-replace-in-string
7946            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7947     (gnus-info-find-node url))
7948    (t (error "Can't parse %s" url))))
7949
7950 (defun gnus-button-handle-info-url-gnome (url)
7951   "Fetch GNOME style info URL."
7952   (setq url (mm-subst-char-in-string ?_ ?\  url))
7953   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7954       (gnus-info-find-node
7955        (concat "("
7956                (gnus-url-unhex-string
7957                  (match-string 1 url))
7958                ")"
7959                (or (gnus-url-unhex-string
7960                     (match-string 2 url))
7961                    "Top")))
7962     (error "Can't parse %s" url)))
7963
7964 (defun gnus-button-handle-info-url-kde (url)
7965   "Fetch KDE style info URL."
7966   (gnus-info-find-node (gnus-url-unhex-string url)))
7967
7968 ;; (info) will autoload info.el
7969 (declare-function Info-menu "info" (menu-item &optional fork))
7970 (declare-function Info-index-next "info" (num))
7971
7972 (defun gnus-button-handle-info-keystrokes (url)
7973   "Call `info' when pushing the corresponding URL button."
7974   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
7975   (let (node indx comma)
7976     (if (string-match
7977          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
7978                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7979                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
7980                  "\\(?:[ \t\n,]*\\)\\)?")
7981          url)
7982         (setq node (match-string 2 url)
7983               indx (match-string 3 url))
7984       (error "Can't parse %s" url))
7985     (info)
7986     (Info-directory)
7987     (Info-menu node)
7988     (when (> (length indx) 0)
7989       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7990                             "\\([ \t\n,]*\\)")
7991                     indx)
7992       (setq comma (match-string 2 indx))
7993       (setq indx  (match-string 1 indx))
7994       (Info-index indx)
7995       (when comma
7996         (dotimes (i (with-temp-buffer
7997                       (insert comma)
7998                       ;; Note: the XEmacs version of `how-many' takes
7999                       ;; no optional argument.
8000                       (goto-char (point-min))
8001                       (how-many ",")))
8002           (Info-index-next 1)))
8003       nil)))
8004
8005 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8006 (declare-function pgg-display-output-buffer "pgg" (start end status))
8007
8008 (defun gnus-button-openpgp (url)
8009   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8010   (with-temp-buffer
8011     (mm-url-insert-file-contents-external url)
8012     (pgg-snarf-keys-region (point-min) (point-max))
8013     (pgg-display-output-buffer nil nil nil)))
8014
8015 (defun gnus-button-message-id (message-id)
8016   "Fetch MESSAGE-ID."
8017   (with-current-buffer gnus-summary-buffer
8018     (gnus-summary-refer-article message-id)))
8019
8020 (defun gnus-button-fetch-group (address &rest ignore)
8021   "Fetch GROUP specified by ADDRESS."
8022   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8023                       address)
8024     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8025     ;; for nntp:// and news://
8026     (setq address (match-string 3 address)))
8027   (if (not (string-match "[:/]" address))
8028       ;; This is just a simple group url.
8029       (gnus-group-read-ephemeral-group address gnus-select-method)
8030     (if (not
8031          (string-match
8032           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8033           address))
8034         (error "Can't parse %s" address)
8035       (gnus-group-read-ephemeral-group
8036        (match-string 4 address)
8037        `(nntp ,(match-string 1 address)
8038               (nntp-address ,(match-string 1 address))
8039               (nntp-port-number ,(if (match-end 3)
8040                                      (match-string 3 address)
8041                                    "nntp")))
8042        nil nil nil
8043        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8044
8045 (defun gnus-url-parse-query-string (query &optional downcase)
8046   (let (retval pairs cur key val)
8047     (setq pairs (split-string query "&"))
8048     (while pairs
8049       (setq cur (car pairs)
8050             pairs (cdr pairs))
8051       (if (not (string-match "=" cur))
8052           nil                           ; Grace
8053         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8054               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8055         (if downcase
8056             (setq key (downcase key)))
8057         (setq cur (assoc key retval))
8058         (if cur
8059             (setcdr cur (cons val (cdr cur)))
8060           (setq retval (cons (list key val) retval)))))
8061     retval))
8062
8063 (defun gnus-url-mailto (url)
8064   ;; Send mail to someone
8065   (when (string-match "mailto:/*\\(.*\\)" url)
8066     (setq url (substring url (match-beginning 1) nil)))
8067   (let (to args subject func)
8068     (setq args (gnus-url-parse-query-string
8069                 (if (string-match "^\\?" url)
8070                     (substring url 1)
8071                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8072                       (concat "to=" (match-string 1 url) "&"
8073                               (match-string 2 url))
8074                     (concat "to=" url)))
8075                 t)
8076           subject (cdr-safe (assoc "subject" args)))
8077     (gnus-msg-mail)
8078     (while args
8079       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8080       (if (fboundp func)
8081           (funcall func)
8082         (message-position-on-field (caar args)))
8083       (insert (gnus-replace-in-string
8084                (mapconcat 'identity (reverse (cdar args)) ", ")
8085                "\r\n" "\n" t))
8086       (setq args (cdr args)))
8087     (if subject
8088         (message-goto-body)
8089       (message-goto-subject))))
8090
8091 (defun gnus-button-embedded-url (address)
8092   "Activate ADDRESS with `browse-url'."
8093   (browse-url (gnus-strip-whitespace address)))
8094
8095 ;;; Next/prev buttons in the article buffer.
8096
8097 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8098 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8099
8100 (defvar gnus-prev-page-map
8101   (let ((map (make-sparse-keymap)))
8102     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8103     (define-key map "\r" 'gnus-button-prev-page)
8104     map))
8105
8106 (defvar gnus-next-page-map
8107   (let ((map (make-sparse-keymap)))
8108     (unless (>= emacs-major-version 21)
8109       ;; XEmacs doesn't care.
8110       (set-keymap-parent map gnus-article-mode-map))
8111     (define-key map gnus-mouse-2 'gnus-button-next-page)
8112     (define-key map "\r" 'gnus-button-next-page)
8113     map))
8114
8115 (defun gnus-insert-prev-page-button ()
8116   (let ((b (point)) e
8117         (inhibit-read-only t))
8118     (gnus-eval-format
8119      gnus-prev-page-line-format nil
8120      `(keymap ,gnus-prev-page-map
8121               gnus-prev t
8122               gnus-callback gnus-article-button-prev-page
8123               article-type annotation))
8124     (setq e (if (bolp)
8125                 ;; Exclude a newline.
8126                 (1- (point))
8127               (point)))
8128     (when gnus-article-button-face
8129       (gnus-overlay-put (gnus-make-overlay b e nil t)
8130                         'face gnus-article-button-face))
8131     (widget-convert-button
8132      'link b e
8133      :action 'gnus-button-prev-page
8134      :button-keymap gnus-prev-page-map)))
8135
8136 (defun gnus-button-next-page (&optional args more-args)
8137   "Go to the next page."
8138   (interactive)
8139   (let ((win (selected-window)))
8140     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8141     (gnus-article-next-page)
8142     (select-window win)))
8143
8144 (defun gnus-button-prev-page (&optional args more-args)
8145   "Go to the prev page."
8146   (interactive)
8147   (let ((win (selected-window)))
8148     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8149     (gnus-article-prev-page)
8150     (select-window win)))
8151
8152 (defun gnus-insert-next-page-button ()
8153   (let ((b (point)) e
8154         (inhibit-read-only t))
8155     (gnus-eval-format gnus-next-page-line-format nil
8156                       `(keymap ,gnus-next-page-map
8157                                gnus-next t
8158                                gnus-callback gnus-article-button-next-page
8159                                article-type annotation))
8160     (setq e (if (bolp)
8161                 ;; Exclude a newline.
8162                 (1- (point))
8163               (point)))
8164     (when gnus-article-button-face
8165       (gnus-overlay-put (gnus-make-overlay b e nil t)
8166                         'face gnus-article-button-face))
8167     (widget-convert-button
8168      'link b e
8169      :action 'gnus-button-next-page
8170      :button-keymap gnus-next-page-map)))
8171
8172 (defun gnus-article-button-next-page (arg)
8173   "Go to the next page."
8174   (interactive "P")
8175   (let ((win (selected-window)))
8176     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8177     (gnus-article-next-page)
8178     (select-window win)))
8179
8180 (defun gnus-article-button-prev-page (arg)
8181   "Go to the prev page."
8182   (interactive "P")
8183   (let ((win (selected-window)))
8184     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8185     (gnus-article-prev-page)
8186     (select-window win)))
8187
8188 (defvar gnus-decode-header-methods
8189   '(mail-decode-encoded-word-region)
8190   "List of methods used to decode headers.
8191
8192 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8193 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8194 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8195 whose names match REGEXP.
8196
8197 For example:
8198 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8199  mail-decode-encoded-word-region
8200  (\"chinese\" . rfc1843-decode-region))
8201 ")
8202
8203 (defvar gnus-decode-header-methods-cache nil)
8204
8205 (defun gnus-multi-decode-header (start end)
8206   "Apply the functions from `gnus-encoded-word-methods' that match."
8207   (unless (and gnus-decode-header-methods-cache
8208                (eq gnus-newsgroup-name
8209                    (car gnus-decode-header-methods-cache)))
8210     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8211     (dolist (x gnus-decode-header-methods)
8212       (if (symbolp x)
8213           (nconc gnus-decode-header-methods-cache (list x))
8214         (if (and gnus-newsgroup-name
8215                  (string-match (car x) gnus-newsgroup-name))
8216             (nconc gnus-decode-header-methods-cache
8217                    (list (cdr x)))))))
8218   (let ((xlist gnus-decode-header-methods-cache))
8219     (pop xlist)
8220     (save-restriction
8221       (narrow-to-region start end)
8222       (while xlist
8223         (funcall (pop xlist) (point-min) (point-max))))))
8224
8225 ;;;
8226 ;;; Treatment top-level handling.
8227 ;;;
8228
8229 (defun gnus-treat-article (condition &optional part-number total-parts type)
8230   (let ((length (- (point-max) (point-min)))
8231         (alist gnus-treatment-function-alist)
8232         (article-goto-body-goes-to-point-min-p t)
8233         (treated-type
8234          (or (not type)
8235              (catch 'found
8236                (let ((list gnus-article-treat-types))
8237                  (while list
8238                    (when (string-match (pop list) type)
8239                      (throw 'found t)))))))
8240         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8241         val elem)
8242     (gnus-run-hooks 'gnus-part-display-hook)
8243     (dolist (elem alist)
8244       (setq val
8245             (save-excursion
8246               (when (gnus-buffer-live-p gnus-summary-buffer)
8247                 (set-buffer gnus-summary-buffer))
8248               (symbol-value (car elem))))
8249       (when (and (or (consp val)
8250                      treated-type)
8251                  (gnus-treat-predicate val)
8252                  (or (not (get (car elem) 'highlight))
8253                      highlightp))
8254         (save-restriction
8255           (funcall (cadr elem)))))))
8256
8257 ;; Dynamic variables.
8258 (defvar part-number)
8259 (defvar total-parts)
8260 (defvar type)
8261 (defvar condition)
8262 (defvar length)
8263
8264 (defun gnus-treat-predicate (val)
8265   (cond
8266    ((null val)
8267     nil)
8268    (condition
8269     (eq condition val))
8270    ((and (listp val)
8271          (stringp (car val)))
8272     (apply 'gnus-or (mapcar `(lambda (s)
8273                                (string-match s ,(or gnus-newsgroup-name "")))
8274                             val)))
8275    ((listp val)
8276     (let ((pred (pop val)))
8277       (cond
8278        ((eq pred 'or)
8279         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8280        ((eq pred 'and)
8281         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8282        ((eq pred 'not)
8283         (not (gnus-treat-predicate (car val))))
8284        ((eq pred 'typep)
8285         (equal (car val) type))
8286        (t
8287         (error "%S is not a valid predicate" pred)))))
8288    ((eq val t)
8289     t)
8290    ((eq val 'head)
8291     nil)
8292    ((eq val 'first)
8293     (eq part-number 1))
8294    ((eq val 'last)
8295     (eq part-number total-parts))
8296    ((numberp val)
8297     (< length val))
8298    (t
8299     (error "%S is not a valid value" val))))
8300
8301 (defun gnus-article-encrypt-body (protocol &optional n)
8302   "Encrypt the article body."
8303   (interactive
8304    (list
8305     (or gnus-article-encrypt-protocol
8306         (completing-read "Encrypt protocol: "
8307                          gnus-article-encrypt-protocol-alist
8308                          nil t))
8309     current-prefix-arg))
8310   ;; User might hit `K E' instead of `K e', so prompt once.
8311   (when (and gnus-article-encrypt-protocol
8312              gnus-novice-user)
8313     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8314       (error "Encrypt aborted.")))
8315   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8316     (unless func
8317       (error "Can't find the encrypt protocol %s" protocol))
8318     (if (member gnus-newsgroup-name '("nndraft:delayed"
8319                                       "nndraft:drafts"
8320                                       "nndraft:queue"))
8321         (error "Can't encrypt the article in group %s"
8322                gnus-newsgroup-name))
8323     (gnus-summary-iterate n
8324       (with-current-buffer gnus-summary-buffer
8325         (let ((mail-parse-charset gnus-newsgroup-charset)
8326               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8327               (summary-buffer gnus-summary-buffer)
8328               references point)
8329           (gnus-set-global-variables)
8330           (when (gnus-group-read-only-p)
8331             (error "The current newsgroup does not support article encrypt"))
8332           (gnus-summary-show-article t)
8333           (setq references
8334               (or (mail-header-references gnus-current-headers) ""))
8335           (set-buffer gnus-article-buffer)
8336           (let* ((inhibit-read-only t)
8337                  (headers
8338                   (mapcar (lambda (field)
8339                             (and (save-restriction
8340                                    (message-narrow-to-head)
8341                                    (goto-char (point-min))
8342                                    (search-forward field nil t))
8343                                  (prog2
8344                                      (message-narrow-to-field)
8345                                      (buffer-string)
8346                                    (delete-region (point-min) (point-max))
8347                                    (widen))))
8348                           '("Content-Type:" "Content-Transfer-Encoding:"
8349                             "Content-Disposition:"))))
8350             (message-narrow-to-head)
8351             (message-remove-header "MIME-Version")
8352             (goto-char (point-max))
8353             (setq point (point))
8354             (insert (apply 'concat headers))
8355             (widen)
8356             (narrow-to-region point (point-max))
8357             (let ((message-options message-options))
8358               (message-options-set 'message-sender user-mail-address)
8359               (message-options-set 'message-recipients user-mail-address)
8360               (message-options-set 'message-sign-encrypt 'not)
8361               (funcall func))
8362             (goto-char (point-min))
8363             (insert "MIME-Version: 1.0\n")
8364             (widen)
8365             (gnus-summary-edit-article-done
8366              references nil summary-buffer t))
8367           (when gnus-keep-backlog
8368             (gnus-backlog-remove-article
8369              (car gnus-article-current) (cdr gnus-article-current)))
8370           (when (get-buffer gnus-original-article-buffer)
8371             (with-current-buffer gnus-original-article-buffer
8372               (setq gnus-original-article nil)))
8373           (when gnus-use-cache
8374             (gnus-cache-update-article
8375              (car gnus-article-current) (cdr gnus-article-current))))))))
8376
8377 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8378   "The following specs can be used:
8379 %t  The security MIME type
8380 %i  Additional info
8381 %d  Details
8382 %D  Details if button is pressed")
8383
8384 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8385   "The following specs can be used:
8386 %t  The security MIME type
8387 %i  Additional info
8388 %d  Details
8389 %D  Details if button is pressed")
8390
8391 (defvar gnus-mime-security-button-line-format-alist
8392   '((?t gnus-tmp-type ?s)
8393     (?i gnus-tmp-info ?s)
8394     (?d gnus-tmp-details ?s)
8395     (?D gnus-tmp-pressed-details ?s)))
8396
8397 (defvar gnus-mime-security-button-commands
8398   '((gnus-article-press-button "\r" "Show Detail")
8399     (undefined "v")
8400     (undefined "t")
8401     (undefined "C")
8402     (gnus-mime-security-save-part "o" "Save...")
8403     (undefined "\C-o")
8404     (undefined "r")
8405     (undefined "d")
8406     (undefined "c")
8407     (undefined "i")
8408     (undefined "E")
8409     (undefined "e")
8410     (undefined "p")
8411     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8412     (undefined ".")))
8413
8414 (defvar gnus-mime-security-button-map
8415   (let ((map (make-sparse-keymap)))
8416     (define-key map gnus-mouse-2 'gnus-article-push-button)
8417     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8418     (dolist (c gnus-mime-security-button-commands)
8419       (define-key map (cadr c) (car c)))
8420     map))
8421
8422 (easy-menu-define
8423   gnus-mime-security-button-menu gnus-mime-security-button-map
8424   "Security button menu."
8425   `("Security Part"
8426     ,@(delq nil
8427             (mapcar (lambda (c)
8428                       (unless (eq (car c) 'undefined)
8429                         (vector (caddr c) (car c) :active t)))
8430                     gnus-mime-security-button-commands))))
8431
8432 (defun gnus-mime-security-button-menu (event prefix)
8433   "Construct a context-sensitive menu of security commands."
8434   (interactive "e\nP")
8435   (save-window-excursion
8436     (let ((pos (event-start event)))
8437       (select-window (posn-window pos))
8438       (goto-char (posn-point pos))
8439       (gnus-article-check-buffer)
8440       (popup-menu gnus-mime-security-button-menu nil prefix))))
8441
8442 (defvar gnus-mime-security-details-buffer nil)
8443
8444 (defvar gnus-mime-security-button-pressed nil)
8445
8446 (defvar gnus-mime-security-show-details-inline t
8447   "If non-nil, show details in the article buffer.")
8448
8449 (defun gnus-mime-security-verify-or-decrypt (handle)
8450   (mm-remove-parts (cdr handle))
8451   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8452         point (inhibit-read-only t))
8453     (if region
8454         (goto-char (car region)))
8455     (setq point (point))
8456     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8457       (let* ((mm-verify-option 'known)
8458              (mm-decrypt-option 'known)
8459              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8460         (unless (eq nparts (cdr handle))
8461           (mm-destroy-parts (cdr handle))
8462           (setcdr handle nparts))))
8463     (gnus-mime-display-security handle)
8464     (when region
8465       (delete-region (point) (cdr region))
8466       (set-marker (car region) nil)
8467       (set-marker (cdr region) nil))
8468     (goto-char point)))
8469
8470 (defun gnus-mime-security-show-details (handle)
8471   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8472     (if (not details)
8473         (gnus-message 5 "No details.")
8474       (if gnus-mime-security-show-details-inline
8475           (let ((gnus-mime-security-button-pressed
8476                  (not (get-text-property (point) 'gnus-mime-details)))
8477                 (gnus-mime-security-button-line-format
8478                  (get-text-property (point) 'gnus-line-format))
8479                 (inhibit-read-only t))
8480             (forward-char -1)
8481             (while (eq (get-text-property (point) 'gnus-line-format)
8482                        gnus-mime-security-button-line-format)
8483               (forward-char -1))
8484             (forward-char)
8485             (save-restriction
8486               (narrow-to-region (point) (point))
8487               (gnus-insert-mime-security-button handle))
8488             (delete-region (point)
8489                            (or (text-property-not-all
8490                                 (point) (point-max)
8491                                 'gnus-line-format
8492                                 gnus-mime-security-button-line-format)
8493                                (point-max))))
8494         ;; Not inlined.
8495         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8496             (with-current-buffer gnus-mime-security-details-buffer
8497               (erase-buffer)
8498               t)
8499           (setq gnus-mime-security-details-buffer
8500                 (gnus-get-buffer-create "*MIME Security Details*")))
8501         (with-current-buffer gnus-mime-security-details-buffer
8502           (insert details)
8503           (goto-char (point-min)))
8504         (pop-to-buffer gnus-mime-security-details-buffer)))))
8505
8506 (defun gnus-mime-security-press-button (handle)
8507   (save-excursion
8508     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8509         (gnus-mime-security-show-details handle)
8510       (gnus-mime-security-verify-or-decrypt handle))))
8511
8512 (defun gnus-insert-mime-security-button (handle &optional displayed)
8513   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8514          (gnus-tmp-type
8515           (concat
8516            (or (nth 2 (assoc protocol mm-verify-function-alist))
8517                (nth 2 (assoc protocol mm-decrypt-function-alist))
8518                "Unknown")
8519            (if (equal (car handle) "multipart/signed")
8520                " Signed" " Encrypted")
8521            " Part"))
8522          (gnus-tmp-info
8523           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8524               "Undecided"))
8525          (gnus-tmp-details
8526           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8527          gnus-tmp-pressed-details
8528          b e)
8529     (setq gnus-tmp-details
8530           (if gnus-tmp-details
8531               (concat "\n" gnus-tmp-details)
8532             ""))
8533     (setq gnus-tmp-pressed-details
8534           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8535     (unless (bolp)
8536       (insert "\n"))
8537     (setq b (point))
8538     (gnus-eval-format
8539      gnus-mime-security-button-line-format
8540      gnus-mime-security-button-line-format-alist
8541      `(keymap ,gnus-mime-security-button-map
8542          gnus-callback gnus-mime-security-press-button
8543          gnus-line-format ,gnus-mime-security-button-line-format
8544          gnus-mime-details ,gnus-mime-security-button-pressed
8545          article-type annotation
8546          gnus-data ,handle))
8547     (setq e (if (bolp)
8548                 ;; Exclude a newline.
8549                 (1- (point))
8550               (point)))
8551     (when gnus-article-button-face
8552       (gnus-overlay-put (gnus-make-overlay b e nil t)
8553                         'face gnus-article-button-face))
8554     (widget-convert-button
8555      'link b e
8556      :mime-handle handle
8557      :action 'gnus-widget-press-button
8558      :button-keymap gnus-mime-security-button-map
8559      :help-echo
8560      (lambda (widget/window &optional overlay pos)
8561        ;; Needed to properly clear the message due to a bug in
8562        ;; wid-edit (XEmacs only).
8563        (when (boundp 'help-echo-owns-message)
8564          (setq help-echo-owns-message t))
8565        (format
8566         "%S: show detail; %S: more options"
8567         (aref gnus-mouse-2 0)
8568         (aref gnus-down-mouse-3 0))))))
8569
8570 (defun gnus-mime-display-security (handle)
8571   (save-restriction
8572     (narrow-to-region (point) (point))
8573     (unless (gnus-unbuttonized-mime-type-p (car handle))
8574       (gnus-insert-mime-security-button handle))
8575     (gnus-mime-display-part (cadr handle))
8576     (unless (bolp)
8577       (insert "\n"))
8578     (unless (gnus-unbuttonized-mime-type-p (car handle))
8579       (let ((gnus-mime-security-button-line-format
8580              gnus-mime-security-button-end-line-format))
8581         (gnus-insert-mime-security-button handle)))
8582     (mm-set-handle-multipart-parameter
8583      handle 'gnus-region
8584      (cons (set-marker (make-marker) (point-min))
8585            (set-marker (make-marker) (point-max))))
8586     (goto-char (point-max))))
8587
8588 (defun gnus-mime-security-run-function (function)
8589   "Run FUNCTION with the security part under point."
8590   (gnus-article-check-buffer)
8591   (let ((data (get-text-property (point) 'gnus-data))
8592         buffer handle)
8593     (when (and (stringp (car-safe data))
8594                (setq buffer (mm-handle-multipart-original-buffer data))
8595                (setq handle (cadr data)))
8596       (if (bufferp (mm-handle-buffer handle))
8597           (progn
8598             (setq handle (cons buffer (copy-sequence (cdr handle))))
8599             (mm-handle-set-undisplayer handle nil))
8600         (setq handle (mm-make-handle
8601                       buffer
8602                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8603                       nil nil nil nil nil nil)))
8604       (funcall function handle))))
8605
8606 (defun gnus-mime-security-save-part ()
8607   "Save the security part under point."
8608   (interactive)
8609   (gnus-mime-security-run-function 'mm-save-part))
8610
8611 (defun gnus-mime-security-pipe-part ()
8612   "Pipe the security part under point to a process."
8613   (interactive)
8614   (gnus-mime-security-run-function 'mm-pipe-part))
8615
8616 (gnus-ems-redefine)
8617
8618 (provide 'gnus-art)
8619
8620 (run-hooks 'gnus-art-load-hook)
8621
8622 ;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
8623 ;;; gnus-art.el ends here