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