Add Gravatar support
[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 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^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:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
273     (if (gnus-image-type-available-p 'pbm)
274         'gnus-display-x-face-in-from
275       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
276 display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favourite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evaled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :type 'string
696   :group 'gnus-article-various)
697
698 (defcustom gnus-article-mode-hook nil
699   "*A hook for Gnus article mode."
700   :type 'hook
701   :group 'gnus-article-various)
702
703 (when (featurep 'xemacs)
704   ;; Extracted from gnus-xmas-define in order to preserve user settings
705   (when (fboundp 'turn-off-scroll-in-place)
706     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
707   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
709
710 (defcustom gnus-article-menu-hook nil
711   "*Hook run after the creation of the article mode menu."
712   :type 'hook
713   :group 'gnus-article-various)
714
715 (defcustom gnus-article-prepare-hook nil
716   "*A hook called after an article has been prepared in the article buffer."
717   :type 'hook
718   :group 'gnus-article-various)
719
720 (defcustom gnus-copy-article-ignored-headers nil
721   "List of headers to be removed when copying an article.
722 Each element is a regular expression."
723   :version "23.1" ;; No Gnus
724   :type '(repeat regexp)
725   :group 'gnus-article-various)
726
727 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
728                         "Gnus 5.10 (Emacs-22.1)")
729
730 (defface gnus-button
731   '((t (:weight bold)))
732   "Face used for highlighting a button in the article buffer."
733   :group 'gnus-article-buttons)
734
735 (defcustom gnus-article-button-face 'gnus-button
736   "Face used for highlighting buttons in the article buffer.
737
738 An article button is a piece of text that you can activate by pressing
739 `RET' or `mouse-2' above it."
740   :type 'face
741   :group 'gnus-article-buttons)
742
743 (defcustom gnus-article-mouse-face 'highlight
744   "Face used for mouse highlighting in the article buffer.
745
746 Article buttons will be displayed in this face when the cursor is
747 above them."
748   :type 'face
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-signature-face 'gnus-signature
752   "Face used for highlighting a signature in the article buffer.
753 Obsolete; use the face `gnus-signature' for customizations instead."
754   :type 'face
755   :group 'gnus-article-highlight
756   :group 'gnus-article-signature)
757
758 (defface gnus-signature
759   '((t
760      (:italic t)))
761   "Face used for highlighting a signature in the article buffer."
762   :group 'gnus-article-highlight
763   :group 'gnus-article-signature)
764 ;; backward-compatibility alias
765 (put 'gnus-signature-face 'face-alias 'gnus-signature)
766 (put 'gnus-signature-face 'obsolete-face "22.1")
767
768 (defface gnus-header-from
769   '((((class color)
770       (background dark))
771      (:foreground "PaleGreen1"))
772     (((class color)
773       (background light))
774      (:foreground "red3"))
775     (t
776      (:italic t)))
777   "Face used for displaying from headers."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight)
780 ;; backward-compatibility alias
781 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
782 (put 'gnus-header-from-face 'obsolete-face "22.1")
783
784 (defface gnus-header-subject
785   '((((class color)
786       (background dark))
787      (:foreground "SeaGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red4"))
791     (t
792      (:bold t :italic t)))
793   "Face used for displaying subject headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
798 (put 'gnus-header-subject-face 'obsolete-face "22.1")
799
800 (defface gnus-header-newsgroups
801   '((((class color)
802       (background dark))
803      (:foreground "yellow" :italic t))
804     (((class color)
805       (background light))
806      (:foreground "MidnightBlue" :italic t))
807     (t
808      (:italic t)))
809   "Face used for displaying newsgroups headers.
810 In the default setup this face is only used for crossposted
811 articles."
812   :group 'gnus-article-headers
813   :group 'gnus-article-highlight)
814 ;; backward-compatibility alias
815 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
816 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
817
818 (defface gnus-header-name
819   '((((class color)
820       (background dark))
821      (:foreground "SpringGreen2"))
822     (((class color)
823       (background light))
824      (:foreground "maroon"))
825     (t
826      (:bold t)))
827   "Face used for displaying header names."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
832 (put 'gnus-header-name-face 'obsolete-face "22.1")
833
834 (defface gnus-header-content
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen1" :italic t))
838     (((class color)
839       (background light))
840      (:foreground "indianred4" :italic t))
841     (t
842      (:italic t)))  "Face used for displaying header content."
843   :group 'gnus-article-headers
844   :group 'gnus-article-highlight)
845 ;; backward-compatibility alias
846 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
847 (put 'gnus-header-content-face 'obsolete-face "22.1")
848
849 (defcustom gnus-header-face-alist
850   '(("From" nil gnus-header-from)
851     ("Subject" nil gnus-header-subject)
852     ("Newsgroups:.*," nil gnus-header-newsgroups)
853     ("" gnus-header-name gnus-header-content))
854   "*Controls highlighting of article headers.
855
856 An alist of the form (HEADER NAME CONTENT).
857
858 HEADER is a regular expression which should match the name of a
859 header and NAME and CONTENT are either face names or nil.
860
861 The name of each header field will be displayed using the face
862 specified by the first element in the list where HEADER matches
863 the header name and NAME is non-nil.  Similarly, the content will
864 be displayed by the first non-nil matching CONTENT face."
865   :group 'gnus-article-headers
866   :group 'gnus-article-highlight
867   :type '(repeat (list (regexp :tag "Header")
868                        (choice :tag "Name"
869                                (item :tag "skip" nil)
870                                (face :value default))
871                        (choice :tag "Content"
872                                (item :tag "skip" nil)
873                                (face :value default)))))
874
875 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
876                                           '((xface . (:face gnus-x-face)))
877                                         '((pbm . (:face gnus-x-face))
878                                           (png . nil)))
879   "Alist of image types and properties applied to Face and X-Face images.
880 Here are examples:
881
882 ;; Specify the altitude of Face images in the From header.
883 \(setq gnus-face-properties-alist
884       '((pbm . (:face gnus-x-face :ascent 80))
885         (png . (:ascent 80))))
886
887 ;; Show Face images as pressed buttons.
888 \(setq gnus-face-properties-alist
889       '((pbm . (:face gnus-x-face :relief -2))
890         (png . (:relief -2))))
891
892 See the manual for the valid properties for various image types.
893 Currently, `pbm' is used for X-Face images and `png' is used for Face
894 images in Emacs.  Only the `:face' property is effective on the `xface'
895 image type in XEmacs if it is built with the libcompface library."
896   :version "23.1" ;; No Gnus
897   :group 'gnus-article-headers
898   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
899
900 (defcustom gnus-article-decode-hook
901   '(article-decode-charset article-decode-encoded-words
902                            article-decode-group-name article-decode-idna-rhs)
903   "*Hook run to decode charsets in articles."
904   :group 'gnus-article-headers
905   :type 'hook)
906
907 (defcustom gnus-display-mime-function 'gnus-display-mime
908   "Function to display MIME articles."
909   :group 'gnus-article-mime
910   :type 'function)
911
912 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
913   "Function used to decode headers.")
914
915 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
916   "Function used to decode addresses.")
917
918 (defvar gnus-article-dumbquotes-map
919   '(("\200" "EUR")
920     ("\202" ",")
921     ("\203" "f")
922     ("\204" ",,")
923     ("\205" "...")
924     ("\213" "<")
925     ("\214" "OE")
926     ("\221" "`")
927     ("\222" "'")
928     ("\223" "``")
929     ("\224" "\"")
930     ("\225" "*")
931     ("\226" "-")
932     ("\227" "--")
933     ("\230" "~")
934     ("\231" "(TM)")
935     ("\233" ">")
936     ("\234" "oe")
937     ("\264" "'"))
938   "Table for MS-to-Latin1 translation.")
939
940 (defcustom gnus-ignored-mime-types nil
941   "List of MIME types that should be ignored by Gnus."
942   :version "21.1"
943   :group 'gnus-article-mime
944   :type '(repeat regexp))
945
946 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
947   "List of MIME types that should not be given buttons when rendered inline.
948 See also `gnus-buttonized-mime-types' which may override this variable.
949 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
950   :version "21.1"
951   :group 'gnus-article-mime
952   :type '(repeat regexp))
953
954 (defcustom gnus-buttonized-mime-types nil
955   "List of MIME types that should be given buttons when rendered inline.
956 If set, this variable overrides `gnus-unbuttonized-mime-types'.
957 To see e.g. security buttons you could set this to
958 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
959 this list to display radio buttons that allow you to choose one of two
960 media types those mails include.  See also `mm-discouraged-alternatives'.
961 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
962   :version "22.1"
963   :group 'gnus-article-mime
964   :type '(repeat regexp))
965
966 (defcustom gnus-inhibit-mime-unbuttonizing nil
967   "If non-nil, all MIME parts get buttons.
968 When nil (the default value), then some MIME parts do not get buttons,
969 as described by the variables `gnus-buttonized-mime-types' and
970 `gnus-unbuttonized-mime-types'."
971   :version "22.1"
972   :group 'gnus-article-mime
973   :type 'boolean)
974
975 (defcustom gnus-body-boundary-delimiter "_"
976   "String used to delimit header and body.
977 This variable is used by `gnus-article-treat-body-boundary' which can
978 be controlled by `gnus-treat-body-boundary'."
979   :version "22.1"
980   :group 'gnus-article-various
981   :type '(choice (item :tag "None" :value nil)
982                  string))
983
984 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
985                                   "/usr/share/picons")
986   "Defines the location of the faces database.
987 For information on obtaining this database of pretty pictures, please
988 see http://www.cs.indiana.edu/picons/ftp/index.html"
989   :version "22.1"
990   :type '(repeat directory)
991   :link '(url-link :tag "download"
992                    "http://www.cs.indiana.edu/picons/ftp/index.html")
993   :link '(custom-manual "(gnus)Picons")
994   :group 'gnus-picon)
995
996 (defun gnus-picons-installed-p ()
997   "Say whether picons are installed on your machine."
998   (let ((installed nil))
999     (dolist (database gnus-picon-databases)
1000       (when (file-exists-p database)
1001         (setq installed t)))
1002     installed))
1003
1004 (defcustom gnus-article-mime-part-function nil
1005   "Function called with a MIME handle as the argument.
1006 This is meant for people who want to do something automatic based
1007 on parts -- for instance, adding Vcard info to a database."
1008   :group 'gnus-article-mime
1009   :type '(choice (const nil)
1010                  function))
1011
1012 (defcustom gnus-mime-multipart-functions nil
1013   "An alist of MIME types to functions to display them."
1014   :version "21.1"
1015   :group 'gnus-article-mime
1016   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1017
1018 (defcustom gnus-article-date-lapsed-new-header nil
1019   "Whether the X-Sent and Date headers can coexist.
1020 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1021 either replace the old \"Date:\" header (if this variable is nil), or
1022 be added below it (otherwise)."
1023   :version "21.1"
1024   :group 'gnus-article-headers
1025   :type 'boolean)
1026
1027 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1028   "Function called with a MIME handle as the argument.
1029 This is meant for people who want to view first matched part.
1030 For `undisplayed-alternative' (default), the first undisplayed
1031 part or alternative part is used.  For `undisplayed', the first
1032 undisplayed part is used.  For a function, the first part which
1033 the function return t is used.  For nil, the first part is
1034 used."
1035   :version "21.1"
1036   :group 'gnus-article-mime
1037   :type '(choice
1038           (item :tag "first" :value nil)
1039           (item :tag "undisplayed" :value undisplayed)
1040           (item :tag "undisplayed or alternative"
1041                 :value undisplayed-alternative)
1042           (function)))
1043
1044 (defcustom gnus-mime-action-alist
1045   '(("save to file" . gnus-mime-save-part)
1046     ("save and strip" . gnus-mime-save-part-and-strip)
1047     ("replace with file" . gnus-mime-replace-part)
1048     ("delete part" . gnus-mime-delete-part)
1049     ("display as text" . gnus-mime-inline-part)
1050     ("view the part" . gnus-mime-view-part)
1051     ("pipe to command" . gnus-mime-pipe-part)
1052     ("toggle display" . gnus-article-press-button)
1053     ("toggle display" . gnus-article-view-part-as-charset)
1054     ("view as type" . gnus-mime-view-part-as-type)
1055     ("view internally" . gnus-mime-view-part-internally)
1056     ("view externally" . gnus-mime-view-part-externally))
1057   "An alist of actions that run on the MIME attachment."
1058   :group 'gnus-article-mime
1059   :type '(repeat (cons (string :tag "name")
1060                        (function))))
1061
1062 (defcustom gnus-auto-select-part 1
1063   "Advance to next MIME part when deleting or stripping parts.
1064
1065 When 0, point will be placed on the same part as before.  When
1066 positive (negative), move point forward (backwards) this many
1067 parts.  When nil, redisplay article."
1068   :version "23.1" ;; No Gnus
1069   :group 'gnus-article-mime
1070   :type '(choice (const nil :tag "Redisplay article.")
1071                  (const 1 :tag "Next part.")
1072                  (const 0 :tag "Current part.")
1073                  integer))
1074
1075 ;;;
1076 ;;; The treatment variables
1077 ;;;
1078
1079 (defvar gnus-part-display-hook nil
1080   "Hook called on parts that are to receive treatment.")
1081
1082 (defvar gnus-article-treat-custom
1083   '(choice (const :tag "Off" nil)
1084            (const :tag "On" t)
1085            (const :tag "Header" head)
1086            (const :tag "First" first)
1087            (const :tag "Last" last)
1088            (integer :tag "Less")
1089            (repeat :tag "Groups" regexp)
1090            (sexp :tag "Predicate")))
1091
1092 (defvar gnus-article-treat-head-custom
1093   '(choice (const :tag "Off" nil)
1094            (const :tag "Header" head)))
1095
1096 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1097                                    "text/x-patch")
1098   "Parts to treat.")
1099
1100 (defvar gnus-inhibit-treatment nil
1101   "Whether to inhibit treatment.")
1102
1103 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1104   "Highlight the signature.
1105 Valid values are nil, t, `head', `first', `last', an integer or a
1106 predicate.  See Info node `(gnus)Customizing Articles'."
1107   :group 'gnus-article-treat
1108   :link '(custom-manual "(gnus)Customizing Articles")
1109   :type gnus-article-treat-custom)
1110 (put 'gnus-treat-highlight-signature 'highlight t)
1111
1112 (defcustom gnus-treat-buttonize 100000
1113   "Add buttons.
1114 Valid values are nil, t, `head', `first', `last', an integer or a
1115 predicate.  See Info node `(gnus)Customizing Articles'."
1116   :group 'gnus-article-treat
1117   :link '(custom-manual "(gnus)Customizing Articles")
1118   :type gnus-article-treat-custom)
1119 (put 'gnus-treat-buttonize 'highlight t)
1120
1121 (defcustom gnus-treat-buttonize-head 'head
1122   "Add buttons to the head.
1123 Valid values are nil, t, `head', `first', `last', an integer or a
1124 predicate.  See Info node `(gnus)Customizing Articles'."
1125   :group 'gnus-article-treat
1126   :link '(custom-manual "(gnus)Customizing Articles")
1127   :type gnus-article-treat-head-custom)
1128 (put 'gnus-treat-buttonize-head 'highlight t)
1129
1130 (defcustom gnus-treat-emphasize 50000
1131   "Emphasize text.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-custom)
1137 (put 'gnus-treat-emphasize 'highlight t)
1138
1139 (defcustom gnus-treat-strip-cr nil
1140   "Remove carriage returns.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :version "22.1"
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147
1148 (defcustom gnus-treat-unsplit-urls nil
1149   "Remove newlines from within URLs.
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :version "22.1"
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-custom)
1156
1157 (defcustom gnus-treat-leading-whitespace nil
1158   "Remove leading whitespace in headers.
1159 Valid values are nil, t, `head', `first', `last', an integer or a
1160 predicate.  See Info node `(gnus)Customizing Articles'."
1161   :version "22.1"
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-custom)
1165
1166 (defcustom gnus-treat-hide-headers 'head
1167   "Hide headers.
1168 Valid values are nil, t, `head', `first', `last', an integer or a
1169 predicate.  See Info node `(gnus)Customizing Articles'."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-head-custom)
1173
1174 (defcustom gnus-treat-hide-boring-headers nil
1175   "Hide boring headers.
1176 Valid values are nil, t, `head', `first', `last', an integer or a
1177 predicate.  See Info node `(gnus)Customizing Articles'."
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-head-custom)
1181
1182 (defcustom gnus-treat-hide-signature nil
1183   "Hide the signature.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-custom)
1189
1190 (defcustom gnus-treat-fill-article nil
1191   "Fill the article.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197
1198 (defcustom gnus-treat-hide-citation nil
1199   "Hide cited text.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-hide-citation-maybe nil
1207   "Hide cited text.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-custom)
1213
1214 (defcustom gnus-treat-strip-list-identifiers 'head
1215   "Strip list identifiers from `gnus-list-identifiers`.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :version "21.1"
1219   :group 'gnus-article-treat
1220   :link '(custom-manual "(gnus)Customizing Articles")
1221   :type gnus-article-treat-custom)
1222
1223 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1224                         "Gnus 5.10 (Emacs 22.1)")
1225
1226 (defcustom gnus-treat-strip-pem nil
1227   "Strip PEM signatures.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'."
1230   :group 'gnus-article-treat
1231   :link '(custom-manual "(gnus)Customizing Articles")
1232   :type gnus-article-treat-custom)
1233
1234 (defcustom gnus-treat-strip-banner t
1235   "Strip banners from articles.
1236 The banner to be stripped is specified in the `banner' group parameter.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'."
1239   :group 'gnus-article-treat
1240   :link '(custom-manual "(gnus)Customizing Articles")
1241   :type gnus-article-treat-custom)
1242
1243 (defcustom gnus-treat-highlight-headers 'head
1244   "Highlight the headers.
1245 Valid values are nil, t, `head', `first', `last', an integer or a
1246 predicate.  See Info node `(gnus)Customizing Articles'."
1247   :group 'gnus-article-treat
1248   :link '(custom-manual "(gnus)Customizing Articles")
1249   :type gnus-article-treat-head-custom)
1250 (put 'gnus-treat-highlight-headers 'highlight t)
1251
1252 (defcustom gnus-treat-highlight-citation t
1253   "Highlight cited text.
1254 Valid values are nil, t, `head', `first', `last', an integer or a
1255 predicate.  See Info node `(gnus)Customizing Articles'."
1256   :group 'gnus-article-treat
1257   :link '(custom-manual "(gnus)Customizing Articles")
1258   :type gnus-article-treat-custom)
1259 (put 'gnus-treat-highlight-citation 'highlight t)
1260
1261 (defcustom gnus-treat-date-ut nil
1262   "Display the Date in UT (GMT).
1263 Valid values are nil, t, `head', `first', `last', an integer or a
1264 predicate.  See Info node `(gnus)Customizing Articles'."
1265   :group 'gnus-article-treat
1266   :link '(custom-manual "(gnus)Customizing Articles")
1267   :type gnus-article-treat-head-custom)
1268
1269 (defcustom gnus-treat-date-local nil
1270   "Display the Date in the local timezone.
1271 Valid values are nil, t, `head', `first', `last', an integer or a
1272 predicate.  See Info node `(gnus)Customizing Articles'."
1273   :group 'gnus-article-treat
1274   :link '(custom-manual "(gnus)Customizing Articles")
1275   :type gnus-article-treat-head-custom)
1276
1277 (defcustom gnus-treat-date-english nil
1278   "Display the Date in a format that can be read aloud in English.
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :version "22.1"
1282   :group 'gnus-article-treat
1283   :link '(custom-manual "(gnus)Customizing Articles")
1284   :type gnus-article-treat-head-custom)
1285
1286 (defcustom gnus-treat-date-lapsed nil
1287   "Display the Date header in a way that says how much time has elapsed.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-head-custom)
1293
1294 (defcustom gnus-treat-date-original nil
1295   "Display the date in the original timezone.
1296 Valid values are nil, t, `head', `first', `last', an integer or a
1297 predicate.  See Info node `(gnus)Customizing Articles'."
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301
1302 (defcustom gnus-treat-date-iso8601 nil
1303   "Display the date in the ISO8601 format.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :version "21.1"
1307   :group 'gnus-article-treat
1308   :link '(custom-manual "(gnus)Customizing Articles")
1309   :type gnus-article-treat-head-custom)
1310
1311 (defcustom gnus-treat-date-user-defined nil
1312   "Display the date in a user-defined format.
1313 The format is defined by the `gnus-article-time-format' variable.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :group 'gnus-article-treat
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :type gnus-article-treat-head-custom)
1319
1320 (defcustom gnus-treat-strip-headers-in-body t
1321   "Strip the X-No-Archive header line from the beginning of the body.
1322 Valid values are nil, t, `head', `first', `last', an integer or a
1323 predicate.  See Info node `(gnus)Customizing Articles'."
1324   :version "21.1"
1325   :group 'gnus-article-treat
1326   :link '(custom-manual "(gnus)Customizing Articles")
1327   :type gnus-article-treat-custom)
1328
1329 (defcustom gnus-treat-strip-trailing-blank-lines nil
1330   "Strip trailing blank lines.
1331 Valid values are nil, t, `head', `first', `last', an integer or a
1332 predicate.  See Info node `(gnus)Customizing Articles'.
1333
1334 When set to t, it also strips trailing blanks in all MIME parts.
1335 Consider to use `last' instead."
1336   :group 'gnus-article-treat
1337   :link '(custom-manual "(gnus)Customizing Articles")
1338   :type gnus-article-treat-custom)
1339
1340 (defcustom gnus-treat-strip-leading-blank-lines nil
1341   "Strip leading blank lines.
1342 Valid values are nil, t, `head', `first', `last', an integer or a
1343 predicate.  See Info node `(gnus)Customizing Articles'.
1344
1345 When set to t, it also strips trailing blanks in all MIME parts."
1346   :group 'gnus-article-treat
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :type gnus-article-treat-custom)
1349
1350 (defcustom gnus-treat-strip-multiple-blank-lines nil
1351   "Strip multiple blank lines.
1352 Valid values are nil, t, `head', `first', `last', an integer or a
1353 predicate.  See Info node `(gnus)Customizing Articles'."
1354   :group 'gnus-article-treat
1355   :link '(custom-manual "(gnus)Customizing Articles")
1356   :type gnus-article-treat-custom)
1357
1358 (defcustom gnus-treat-unfold-headers 'head
1359   "Unfold folded header lines.
1360 Valid values are nil, t, `head', `first', `last', an integer or a
1361 predicate.  See Info node `(gnus)Customizing Articles'."
1362   :version "22.1"
1363   :group 'gnus-article-treat
1364   :link '(custom-manual "(gnus)Customizing Articles")
1365   :type gnus-article-treat-custom)
1366
1367 (defcustom gnus-article-unfold-long-headers nil
1368   "If non-nil, allow unfolding headers even if the header is long.
1369 If it is a regexp, only long headers matching this regexp are unfolded.
1370 If it is t, all long headers are unfolded.
1371
1372 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1373   :version "23.1" ;; No Gnus
1374   :group 'gnus-article-treat
1375   :type '(choice (const nil)
1376                  (const :tag "all" t)
1377                  (regexp)))
1378
1379 (defcustom gnus-treat-fold-headers nil
1380   "Fold headers.
1381 Valid values are nil, t, `head', `first', `last', an integer or a
1382 predicate.  See Info node `(gnus)Customizing Articles'."
1383   :version "22.1"
1384   :group 'gnus-article-treat
1385   :link '(custom-manual "(gnus)Customizing Articles")
1386   :type gnus-article-treat-custom)
1387
1388 (defcustom gnus-treat-fold-newsgroups 'head
1389   "Fold the Newsgroups and Followup-To headers.
1390 Valid values are nil, t, `head', `first', `last', an integer or a
1391 predicate.  See Info node `(gnus)Customizing Articles'."
1392   :version "22.1"
1393   :group 'gnus-article-treat
1394   :link '(custom-manual "(gnus)Customizing Articles")
1395   :type gnus-article-treat-custom)
1396
1397 (defcustom gnus-treat-overstrike t
1398   "Treat overstrike highlighting.
1399 Valid values are nil, t, `head', `first', `last', an integer or a
1400 predicate.  See Info node `(gnus)Customizing Articles'."
1401   :group 'gnus-article-treat
1402   :link '(custom-manual "(gnus)Customizing Articles")
1403   :type gnus-article-treat-custom)
1404 (put 'gnus-treat-overstrike 'highlight t)
1405
1406 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1407   "Treat ANSI SGR control sequences.
1408 Valid values are nil, t, `head', `first', `last', an integer or a
1409 predicate.  See Info node `(gnus)Customizing Articles'."
1410   :group 'gnus-article-treat
1411   :link '(custom-manual "(gnus)Customizing Articles")
1412   :type gnus-article-treat-custom)
1413
1414 (make-obsolete-variable 'gnus-treat-display-xface
1415                         'gnus-treat-display-x-face "22.1")
1416
1417 (defcustom gnus-treat-display-x-face
1418   (and (not noninteractive)
1419        (gnus-image-type-available-p 'xbm)
1420        (if (featurep 'xemacs)
1421            (featurep 'xface)
1422          (condition-case nil
1423              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1424                   (executable-find "icontopbm"))
1425            ;; shell-command-to-string may signal an error, e.g. if
1426            ;; shell-file-name is not found.
1427            (error nil)))
1428        'head)
1429   "Display X-Face headers.
1430 Valid values are nil and `head'.
1431 See Info node `(gnus)Customizing Articles' and Info node
1432 `(gnus)X-Face' for details."
1433   :group 'gnus-article-treat
1434   :version "21.1"
1435   :link '(custom-manual "(gnus)Customizing Articles")
1436   :link '(custom-manual "(gnus)X-Face")
1437   :type gnus-article-treat-head-custom
1438   :set (lambda (symbol value)
1439          (set-default
1440           symbol
1441           (cond ((or (boundp symbol) (get symbol 'saved-value))
1442                  value)
1443                 ((boundp 'gnus-treat-display-xface)
1444                  (message "\
1445 ** gnus-treat-display-xface is an obsolete variable;\
1446  use gnus-treat-display-x-face instead")
1447                  (default-value 'gnus-treat-display-xface))
1448                 ((get 'gnus-treat-display-xface 'saved-value)
1449                  (message "\
1450 ** gnus-treat-display-xface is an obsolete variable;\
1451  use gnus-treat-display-x-face instead")
1452                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1453                 (t
1454                  value)))))
1455 (put 'gnus-treat-display-x-face 'highlight t)
1456
1457 (defcustom gnus-treat-display-face
1458   (and (not noninteractive)
1459        (gnus-image-type-available-p 'png)
1460        'head)
1461   "Display Face headers.
1462 Valid values are nil, t, `head', `first', `last', an integer or a
1463 predicate.  See Info node `(gnus)Customizing Articles' and Info
1464 node `(gnus)Face' for details."
1465   :group 'gnus-article-treat
1466   :version "22.1"
1467   :link '(custom-manual "(gnus)Customizing Articles")
1468   :link '(custom-manual "(gnus)X-Face")
1469   :type gnus-article-treat-head-custom)
1470 (put 'gnus-treat-display-face 'highlight t)
1471
1472 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1473   "Display smileys.
1474 Valid values are nil, t, `head', `first', `last', an integer or a
1475 predicate.  See Info node `(gnus)Customizing Articles' and Info
1476 node `(gnus)Smileys' for details."
1477   :group 'gnus-article-treat
1478   :version "21.1"
1479   :link '(custom-manual "(gnus)Customizing Articles")
1480   :link '(custom-manual "(gnus)Smileys")
1481   :type gnus-article-treat-custom)
1482 (put 'gnus-treat-display-smileys 'highlight t)
1483
1484 (defcustom gnus-treat-from-picon
1485   (if (and (gnus-image-type-available-p 'xpm)
1486            (gnus-picons-installed-p))
1487       'head nil)
1488   "Display picons in the From header.
1489 Valid values are nil, t, `head', `first', `last', an integer or a
1490 predicate.  See Info node `(gnus)Customizing Articles' and Info
1491 node `(gnus)Picons' for details."
1492   :version "22.1"
1493   :group 'gnus-article-treat
1494   :group 'gnus-picon
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Picons")
1497   :type gnus-article-treat-head-custom)
1498 (put 'gnus-treat-from-picon 'highlight t)
1499
1500 (defcustom gnus-treat-mail-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in To and Cc headers.
1505 Valid values are nil, t, `head', `first', `last', an integer or a
1506 predicate.  See Info node `(gnus)Customizing Articles' and Info
1507 node `(gnus)Picons' for details."
1508   :version "22.1"
1509   :group 'gnus-article-treat
1510   :group 'gnus-picon
1511   :link '(custom-manual "(gnus)Customizing Articles")
1512   :link '(custom-manual "(gnus)Picons")
1513   :type gnus-article-treat-head-custom)
1514 (put 'gnus-treat-mail-picon 'highlight t)
1515
1516 (defcustom gnus-treat-newsgroups-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in the Newsgroups and Followup-To headers.
1521 Valid values are nil, t, `head', `first', `last', an integer or a
1522 predicate.  See Info node `(gnus)Customizing Articles' and Info
1523 node `(gnus)Picons' for details."
1524   :version "22.1"
1525   :group 'gnus-article-treat
1526   :group 'gnus-picon
1527   :link '(custom-manual "(gnus)Customizing Articles")
1528   :link '(custom-manual "(gnus)Picons")
1529   :type gnus-article-treat-head-custom)
1530 (put 'gnus-treat-newsgroups-picon 'highlight t)
1531
1532 (defcustom gnus-treat-from-gravatar
1533   (when (display-images-p) 'head)
1534   "Display gravatars in the From header.
1535 Valid values are nil, t, `head', `first', `last', an integer or a
1536 predicate.  See Info node `(gnus)Customizing Articles' and Info
1537 node `(gnus)Gravatars' for details."
1538   :version "24.1"
1539   :group 'gnus-article-treat
1540   :group 'gnus-gravatar
1541   :link '(custom-manual "(gnus)Customizing Articles")
1542   :link '(custom-manual "(gnus)Gravatars")
1543   :type gnus-article-treat-head-custom)
1544 (put 'gnus-treat-from-gravatar 'highlight t)
1545
1546 (defcustom gnus-treat-mail-gravatar
1547   (when (display-images-p) 'head)
1548   "Display gravatars in To and Cc headers.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles' and Info
1551 node `(gnus)Gravatars' for details."
1552   :version "24.1"
1553   :group 'gnus-article-treat
1554   :group 'gnus-gravatar
1555   :link '(custom-manual "(gnus)Customizing Articles")
1556   :link '(custom-manual "(gnus)Gravatars")
1557   :type gnus-article-treat-head-custom)
1558 (put 'gnus-treat-mail-gravatar 'highlight t)
1559
1560 (defcustom gnus-treat-body-boundary
1561   (if (or gnus-treat-newsgroups-picon
1562           gnus-treat-mail-picon
1563           gnus-treat-from-picon
1564           gnus-treat-from-gravatar
1565           gnus-treat-mail-gravatar)
1566       ;; If there's much decoration, the user might prefer a boundery.
1567       'head
1568     nil)
1569   "Draw a boundary at the end of the headers.
1570 Valid values are nil and `head'.
1571 See Info node `(gnus)Customizing Articles' for details."
1572   :version "22.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-head-custom)
1576
1577 (defcustom gnus-treat-capitalize-sentences nil
1578   "Capitalize sentence-starting words.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "21.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-wash-html nil
1587   "Format as HTML.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "22.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-fill-long-lines nil
1596   "Fill long lines.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :group 'gnus-article-treat
1600   :link '(custom-manual "(gnus)Customizing Articles")
1601   :type gnus-article-treat-custom)
1602
1603 (defcustom gnus-treat-play-sounds nil
1604   "Play sounds.
1605 Valid values are nil, t, `head', `first', `last', an integer or a
1606 predicate.  See Info node `(gnus)Customizing Articles'."
1607   :version "21.1"
1608   :group 'gnus-article-treat
1609   :link '(custom-manual "(gnus)Customizing Articles")
1610   :type gnus-article-treat-custom)
1611
1612 (defcustom gnus-treat-translate nil
1613   "Translate articles from one language to another.
1614 Valid values are nil, t, `head', `first', `last', an integer or a
1615 predicate.  See Info node `(gnus)Customizing Articles'."
1616   :version "21.1"
1617   :group 'gnus-article-treat
1618   :link '(custom-manual "(gnus)Customizing Articles")
1619   :type gnus-article-treat-custom)
1620
1621 (defcustom gnus-treat-x-pgp-sig nil
1622   "Verify X-PGP-Sig.
1623 To automatically treat X-PGP-Sig, set it to head.
1624 Valid values are nil, t, `head', `first', `last', an integer or a
1625 predicate.  See Info node `(gnus)Customizing Articles'."
1626   :version "22.1"
1627   :group 'gnus-article-treat
1628   :group 'mime-security
1629   :link '(custom-manual "(gnus)Customizing Articles")
1630   :type gnus-article-treat-custom)
1631
1632 (defvar gnus-article-encrypt-protocol-alist
1633   '(("PGP" . mml2015-self-encrypt)))
1634
1635 ;; Set to nil if more than one protocol added to
1636 ;; gnus-article-encrypt-protocol-alist.
1637 (defcustom gnus-article-encrypt-protocol "PGP"
1638   "The protocol used for encrypt articles.
1639 It is a string, such as \"PGP\". If nil, ask user."
1640   :version "22.1"
1641   :type 'string
1642   :group 'mime-security)
1643
1644 (defvar gnus-article-wash-function nil
1645   "Function used for converting HTML into text.")
1646
1647 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1648                               (mm-coding-system-p 'utf-8)
1649                               (executable-find idna-program))
1650   "Whether IDNA decoding of headers is used when viewing messages.
1651 This requires GNU Libidn, and by default only enabled if it is found."
1652   :version "22.1"
1653   :group 'gnus-article-headers
1654   :type 'boolean)
1655
1656 (defcustom gnus-article-over-scroll nil
1657   "If non-nil, allow scrolling the article buffer even when there no more text."
1658   :version "22.1"
1659   :group 'gnus-article
1660   :type 'boolean)
1661
1662 ;;; Internal variables
1663
1664 (defvar gnus-english-month-names
1665   '("January" "February" "March" "April" "May" "June" "July" "August"
1666     "September" "October" "November" "December"))
1667
1668 (defvar article-goto-body-goes-to-point-min-p nil)
1669 (defvar gnus-article-wash-types nil)
1670 (defvar gnus-article-emphasis-alist nil)
1671 (defvar gnus-article-image-alist nil)
1672
1673 (defvar gnus-article-mime-handle-alist-1 nil)
1674 (defvar gnus-treatment-function-alist
1675   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1676     (gnus-treat-strip-banner gnus-article-strip-banner)
1677     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1678     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1679     (gnus-treat-buttonize gnus-article-add-buttons)
1680     (gnus-treat-fill-article gnus-article-fill-cited-article)
1681     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1682     (gnus-treat-strip-cr gnus-article-remove-cr)
1683     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1684     (gnus-treat-date-ut gnus-article-date-ut)
1685     (gnus-treat-date-local gnus-article-date-local)
1686     (gnus-treat-date-english gnus-article-date-english)
1687     (gnus-treat-date-original gnus-article-date-original)
1688     (gnus-treat-date-user-defined gnus-article-date-user)
1689     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1690     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1691     (gnus-treat-display-x-face gnus-article-display-x-face)
1692     (gnus-treat-display-face gnus-article-display-face)
1693     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1694     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1695     (gnus-treat-hide-signature gnus-article-hide-signature)
1696     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1697     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1698     (gnus-treat-strip-pem gnus-article-hide-pem)
1699     (gnus-treat-from-picon gnus-treat-from-picon)
1700     (gnus-treat-mail-picon gnus-treat-mail-picon)
1701     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1702     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1703     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1704     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1705     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1706     (gnus-treat-strip-trailing-blank-lines
1707      gnus-article-remove-trailing-blank-lines)
1708     (gnus-treat-strip-leading-blank-lines
1709      gnus-article-strip-leading-blank-lines)
1710     (gnus-treat-strip-multiple-blank-lines
1711      gnus-article-strip-multiple-blank-lines)
1712     (gnus-treat-overstrike gnus-article-treat-overstrike)
1713     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1714     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1715     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1716     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1717     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1718     (gnus-treat-display-smileys gnus-treat-smiley)
1719     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1720     (gnus-treat-wash-html gnus-article-wash-html)
1721     (gnus-treat-emphasize gnus-article-emphasize)
1722     (gnus-treat-hide-citation gnus-article-hide-citation)
1723     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1724     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1725     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1726     (gnus-treat-play-sounds gnus-earcon-display)))
1727
1728 (defvar gnus-article-mime-handle-alist nil)
1729 (defvar article-lapsed-timer nil)
1730 (defvar gnus-article-current-summary nil)
1731
1732 (defvar gnus-article-mode-syntax-table
1733   (let ((table (copy-syntax-table text-mode-syntax-table)))
1734     ;; This causes the citation match run O(2^n).
1735     ;; (modify-syntax-entry ?- "w" table)
1736     (modify-syntax-entry ?> ")<" table)
1737     (modify-syntax-entry ?< "(>" table)
1738     ;; make M-. in article buffers work for `foo' strings
1739     (modify-syntax-entry ?' " " table)
1740     (modify-syntax-entry ?` " " table)
1741     table)
1742   "Syntax table used in article mode buffers.
1743 Initialized from `text-mode-syntax-table.")
1744
1745 (defvar gnus-save-article-buffer nil)
1746
1747 (defvar gnus-number-of-articles-to-be-saved nil)
1748
1749 (defvar gnus-inhibit-hiding nil)
1750
1751 (defvar gnus-article-edit-mode nil)
1752
1753 ;;; Macros for dealing with the article buffer.
1754
1755 (defmacro gnus-with-article-headers (&rest forms)
1756   `(with-current-buffer gnus-article-buffer
1757      (save-restriction
1758        (let ((inhibit-read-only t)
1759              (inhibit-point-motion-hooks t)
1760              (case-fold-search t))
1761          (article-narrow-to-head)
1762          ,@forms))))
1763
1764 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1765 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1766
1767 (defmacro gnus-with-article-buffer (&rest forms)
1768   `(with-current-buffer gnus-article-buffer
1769      (let ((inhibit-read-only t))
1770        ,@forms)))
1771
1772 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1773 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1774
1775 (defun gnus-article-goto-header (header)
1776   "Go to HEADER, which is a regular expression."
1777   (re-search-forward (concat "^\\(" header "\\):") nil t))
1778
1779 (defsubst gnus-article-hide-text (b e props)
1780   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1781   (gnus-add-text-properties-when 'article-type nil b e props)
1782   (when (memq 'intangible props)
1783     (put-text-property
1784      (max (1- b) (point-min))
1785      b 'intangible (cddr (memq 'intangible props)))))
1786
1787 (defsubst gnus-article-unhide-text (b e)
1788   "Remove hidden text properties from region between B and E."
1789   (remove-text-properties b e gnus-hidden-properties)
1790   (when (memq 'intangible gnus-hidden-properties)
1791     (put-text-property (max (1- b) (point-min))
1792                        b 'intangible nil)))
1793
1794 (defun gnus-article-hide-text-type (b e type)
1795   "Hide text of TYPE between B and E."
1796   (gnus-add-wash-type type)
1797   (gnus-article-hide-text
1798    b e (cons 'article-type (cons type gnus-hidden-properties))))
1799
1800 (defun gnus-article-unhide-text-type (b e type)
1801   "Unhide text of TYPE between B and E."
1802   (gnus-delete-wash-type type)
1803   (remove-text-properties
1804    b e (cons 'article-type (cons type gnus-hidden-properties)))
1805   (when (memq 'intangible gnus-hidden-properties)
1806     (put-text-property (max (1- b) (point-min))
1807                        b 'intangible nil)))
1808
1809 (defun gnus-article-hide-text-of-type (type)
1810   "Hide text of TYPE in the current buffer."
1811   (save-excursion
1812     (let ((b (point-min))
1813           (e (point-max)))
1814       (while (setq b (text-property-any b e 'article-type type))
1815         (add-text-properties b (incf b) gnus-hidden-properties)))))
1816
1817 (defun gnus-article-delete-text-of-type (type)
1818   "Delete text of TYPE in the current buffer."
1819   (save-excursion
1820     (let ((b (point-min)))
1821       (if (eq type 'multipart)
1822           ;; Remove MIME buttons associated with multipart/alternative parts.
1823           (progn
1824             (goto-char b)
1825             (while (if (get-text-property (point) 'gnus-part)
1826                        (setq b (point))
1827                      (when (setq b (next-single-property-change (point)
1828                                                                 'gnus-part))
1829                        (goto-char b)
1830                        t))
1831               (end-of-line)
1832               (skip-chars-forward "\n")
1833               (when (eq (get-text-property b 'article-type) 'multipart)
1834                 (delete-region b (point)))))
1835         (while (setq b (text-property-any b (point-max) 'article-type type))
1836           (delete-region
1837            b (or (text-property-not-all b (point-max) 'article-type type)
1838                  (point-max))))))))
1839
1840 (defun gnus-article-delete-invisible-text ()
1841   "Delete all invisible text in the current buffer."
1842   (save-excursion
1843     (let ((b (point-min)))
1844       (while (setq b (text-property-any b (point-max) 'invisible t))
1845         (delete-region
1846          b (or (text-property-not-all b (point-max) 'invisible t)
1847                (point-max)))))))
1848
1849 (defun gnus-article-text-type-exists-p (type)
1850   "Say whether any text of type TYPE exists in the buffer."
1851   (text-property-any (point-min) (point-max) 'article-type type))
1852
1853 (defsubst gnus-article-header-rank ()
1854   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1855   (let ((list gnus-sorted-header-list)
1856         (i 1))
1857     (while list
1858       (if (looking-at (car list))
1859           (setq list nil)
1860         (setq list (cdr list))
1861         (incf i)))
1862       i))
1863
1864 (defun article-hide-headers (&optional arg delete)
1865   "Hide unwanted headers and possibly sort them as well."
1866   (interactive)
1867   ;; This function might be inhibited.
1868   (unless gnus-inhibit-hiding
1869     (let ((inhibit-read-only t)
1870           (case-fold-search t)
1871           (max (1+ (length gnus-sorted-header-list)))
1872           (inhibit-point-motion-hooks t)
1873           (cur (current-buffer))
1874           ignored visible beg)
1875       (save-excursion
1876         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1877         ;; group parameters, so we should go to the summary buffer.
1878         (when (prog1
1879                   (condition-case nil
1880                       (progn (set-buffer gnus-summary-buffer) t)
1881                     (error nil))
1882                 (setq ignored (when (not gnus-visible-headers)
1883                                 (cond ((stringp gnus-ignored-headers)
1884                                        gnus-ignored-headers)
1885                                       ((listp gnus-ignored-headers)
1886                                        (mapconcat 'identity
1887                                                   gnus-ignored-headers
1888                                                   "\\|"))))
1889                       visible (cond ((stringp gnus-visible-headers)
1890                                      gnus-visible-headers)
1891                                     ((and gnus-visible-headers
1892                                           (listp gnus-visible-headers))
1893                                      (mapconcat 'identity
1894                                                 gnus-visible-headers
1895                                                 "\\|")))))
1896           (set-buffer cur))
1897         (save-restriction
1898           ;; First we narrow to just the headers.
1899           (article-narrow-to-head)
1900           ;; Hide any "From " lines at the beginning of (mail) articles.
1901           (while (looking-at "From ")
1902             (forward-line 1))
1903           (unless (bobp)
1904             (delete-region (point-min) (point)))
1905           ;; Then treat the rest of the header lines.
1906           ;; Then we use the two regular expressions
1907           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1908           ;; select which header lines is to remain visible in the
1909           ;; article buffer.
1910           (while (re-search-forward "^[^ \t:]*:" nil t)
1911             (beginning-of-line)
1912             ;; Mark the rank of the header.
1913             (put-text-property
1914              (point) (1+ (point)) 'message-rank
1915              (if (or (and visible (looking-at visible))
1916                      (and ignored
1917                           (not (looking-at ignored))))
1918                  (gnus-article-header-rank)
1919                (+ 2 max)))
1920             (forward-line 1))
1921           (message-sort-headers-1)
1922           (when (setq beg (text-property-any
1923                            (point-min) (point-max) 'message-rank (+ 2 max)))
1924             ;; We delete the unwanted headers.
1925             (gnus-add-wash-type 'headers)
1926             (add-text-properties (point-min) (+ 5 (point-min))
1927                                  '(article-type headers dummy-invisible t))
1928             (delete-region beg (point-max))))))))
1929
1930 (defun article-hide-boring-headers (&optional arg)
1931   "Toggle hiding of headers that aren't very interesting.
1932 If given a negative prefix, always show; if given a positive prefix,
1933 always hide."
1934   (interactive (gnus-article-hidden-arg))
1935   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1936              (not gnus-show-all-headers))
1937     (save-excursion
1938       (save-restriction
1939         (let ((inhibit-read-only t)
1940               (inhibit-point-motion-hooks t))
1941           (article-narrow-to-head)
1942           (dolist (elem gnus-boring-article-headers)
1943             (goto-char (point-min))
1944             (cond
1945              ;; Hide empty headers.
1946              ((eq elem 'empty)
1947               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1948                 (forward-line -1)
1949                 (gnus-article-hide-text-type
1950                  (point-at-bol)
1951                  (progn
1952                    (end-of-line)
1953                    (if (re-search-forward "^[^ \t]" nil t)
1954                        (match-beginning 0)
1955                      (point-max)))
1956                  'boring-headers)))
1957              ;; Hide boring Newsgroups header.
1958              ((eq elem 'newsgroups)
1959               (when (gnus-string-equal
1960                      (gnus-fetch-field "newsgroups")
1961                      (gnus-group-real-name
1962                       (if (boundp 'gnus-newsgroup-name)
1963                           gnus-newsgroup-name
1964                         "")))
1965                 (gnus-article-hide-header "newsgroups")))
1966              ((eq elem 'to-address)
1967               (let ((to (message-fetch-field "to"))
1968                     (to-address
1969                      (gnus-parameter-to-address
1970                       (if (boundp 'gnus-newsgroup-name)
1971                           gnus-newsgroup-name ""))))
1972                 (when (and to to-address
1973                            (ignore-errors
1974                              (gnus-string-equal
1975                               ;; only one address in To
1976                               (nth 1 (mail-extract-address-components to))
1977                               to-address)))
1978                   (gnus-article-hide-header "to"))))
1979              ((eq elem 'to-list)
1980               (let ((to (message-fetch-field "to"))
1981                     (to-list
1982                      (gnus-parameter-to-list
1983                       (if (boundp 'gnus-newsgroup-name)
1984                           gnus-newsgroup-name ""))))
1985                 (when (and to to-list
1986                            (ignore-errors
1987                              (gnus-string-equal
1988                               ;; only one address in To
1989                               (nth 1 (mail-extract-address-components to))
1990                               to-list)))
1991                   (gnus-article-hide-header "to"))))
1992              ((eq elem 'cc-list)
1993               (let ((cc (message-fetch-field "cc"))
1994                     (to-list
1995                      (gnus-parameter-to-list
1996                       (if (boundp 'gnus-newsgroup-name)
1997                           gnus-newsgroup-name ""))))
1998                 (when (and cc to-list
1999                            (ignore-errors
2000                              (gnus-string-equal
2001                               ;; only one address in CC
2002                               (nth 1 (mail-extract-address-components cc))
2003                               to-list)))
2004                   (gnus-article-hide-header "cc"))))
2005              ((eq elem 'followup-to)
2006               (when (gnus-string-equal
2007                      (message-fetch-field "followup-to")
2008                      (message-fetch-field "newsgroups"))
2009                 (gnus-article-hide-header "followup-to")))
2010              ((eq elem 'reply-to)
2011               (if (gnus-group-find-parameter
2012                    gnus-newsgroup-name 'broken-reply-to)
2013                   (gnus-article-hide-header "reply-to")
2014                 (let ((from (message-fetch-field "from"))
2015                       (reply-to (message-fetch-field "reply-to")))
2016                   (when
2017                       (and
2018                        from reply-to
2019                        (ignore-errors
2020                          (equal
2021                           (sort (mapcar
2022                                  (lambda (x) (downcase (cadr x)))
2023                                  (mail-extract-address-components from t))
2024                                 'string<)
2025                           (sort (mapcar
2026                                  (lambda (x) (downcase (cadr x)))
2027                                  (mail-extract-address-components reply-to t))
2028                                 'string<))))
2029                     (gnus-article-hide-header "reply-to")))))
2030              ((eq elem 'date)
2031               (let ((date (with-current-buffer gnus-original-article-buffer
2032                             ;; If date in `gnus-article-buffer' is localized
2033                             ;; (`gnus-treat-date-user-defined'),
2034                             ;; `days-between' might fail.
2035                             (message-fetch-field "date"))))
2036                 (when (and date
2037                            (< (days-between (current-time-string) date)
2038                               4))
2039                   (gnus-article-hide-header "date"))))
2040              ((eq elem 'long-to)
2041               (let ((to (message-fetch-field "to"))
2042                     (cc (message-fetch-field "cc")))
2043                 (when (> (length to) 1024)
2044                   (gnus-article-hide-header "to"))
2045                 (when (> (length cc) 1024)
2046                   (gnus-article-hide-header "cc"))))
2047              ((eq elem 'many-to)
2048               (let ((to-count 0)
2049                     (cc-count 0))
2050                 (goto-char (point-min))
2051                 (while (re-search-forward "^to:" nil t)
2052                   (setq to-count (1+ to-count)))
2053                 (when (> to-count 1)
2054                   (while (> to-count 0)
2055                     (goto-char (point-min))
2056                     (save-restriction
2057                       (re-search-forward "^to:" nil nil to-count)
2058                       (forward-line -1)
2059                       (narrow-to-region (point) (point-max))
2060                       (gnus-article-hide-header "to"))
2061                     (setq to-count (1- to-count))))
2062                 (goto-char (point-min))
2063                 (while (re-search-forward "^cc:" nil t)
2064                   (setq cc-count (1+ cc-count)))
2065                 (when (> cc-count 1)
2066                   (while (> cc-count 0)
2067                     (goto-char (point-min))
2068                     (save-restriction
2069                       (re-search-forward "^cc:" nil nil cc-count)
2070                       (forward-line -1)
2071                       (narrow-to-region (point) (point-max))
2072                       (gnus-article-hide-header "cc"))
2073                     (setq cc-count (1- cc-count)))))))))))))
2074
2075 (defun gnus-article-hide-header (header)
2076   (save-excursion
2077     (goto-char (point-min))
2078     (when (re-search-forward (concat "^" header ":") nil t)
2079       (gnus-article-hide-text-type
2080        (point-at-bol)
2081        (progn
2082          (end-of-line)
2083          (if (re-search-forward "^[^ \t]" nil t)
2084              (match-beginning 0)
2085            (point-max)))
2086        'boring-headers))))
2087
2088 (defvar gnus-article-normalized-header-length 40
2089   "Length of normalized headers.")
2090
2091 (defun article-normalize-headers ()
2092   "Make all header lines 40 characters long."
2093   (interactive)
2094   (let ((inhibit-read-only t)
2095         column)
2096     (save-excursion
2097       (save-restriction
2098         (article-narrow-to-head)
2099         (while (not (eobp))
2100           (cond
2101            ((< (setq column (- (point-at-eol) (point)))
2102                gnus-article-normalized-header-length)
2103             (end-of-line)
2104             (insert (make-string
2105                      (- gnus-article-normalized-header-length column)
2106                      ? )))
2107            ((> column gnus-article-normalized-header-length)
2108             (gnus-put-text-property
2109              (progn
2110                (forward-char gnus-article-normalized-header-length)
2111                (point))
2112              (point-at-eol)
2113              'invisible t))
2114            (t
2115             ;; Do nothing.
2116             ))
2117           (forward-line 1))))))
2118
2119 (defun article-treat-dumbquotes ()
2120   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2121 Note that this function guesses whether a character is a sm*rtq**t* or
2122 not, so it should only be used interactively.
2123
2124 Sm*rtq**t*s are M****s***'s unilateral extension to the
2125 iso-8859-1 character map in an attempt to provide more quoting
2126 characters.  If you see something like \\222 or \\264 where
2127 you're expecting some kind of apostrophe or quotation mark, then
2128 try this wash."
2129   (interactive)
2130   (article-translate-strings gnus-article-dumbquotes-map))
2131
2132 (defun article-translate-characters (from to)
2133   "Translate all characters in the body of the article according to FROM and TO.
2134 FROM is a string of characters to translate from; to is a string of
2135 characters to translate to."
2136   (save-excursion
2137     (when (article-goto-body)
2138       (let ((inhibit-read-only t)
2139             (x (make-string 225 ?x))
2140             (i -1))
2141         (while (< (incf i) (length x))
2142           (aset x i i))
2143         (setq i 0)
2144         (while (< i (length from))
2145           (aset x (aref from i) (aref to i))
2146           (incf i))
2147         (translate-region (point) (point-max) x)))))
2148
2149 (defun article-translate-strings (map)
2150   "Translate all string in the body of the article according to MAP.
2151 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2152   (save-excursion
2153     (when (article-goto-body)
2154       (let ((inhibit-read-only t))
2155         (dolist (elem map)
2156           (save-excursion
2157             (while (search-forward (car elem) nil t)
2158               (replace-match (cadr elem)))))))))
2159
2160 (defun article-treat-overstrike ()
2161   "Translate overstrikes into bold text."
2162   (interactive)
2163   (save-excursion
2164     (when (article-goto-body)
2165       (let ((inhibit-read-only t))
2166         (while (search-forward "\b" nil t)
2167           (let ((next (char-after))
2168                 (previous (char-after (- (point) 2))))
2169             ;; We do the boldification/underlining by hiding the
2170             ;; overstrikes and putting the proper text property
2171             ;; on the letters.
2172             (cond
2173              ((eq next previous)
2174               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2175               (put-text-property (point) (1+ (point)) 'face 'bold))
2176              ((eq next ?_)
2177               (gnus-article-hide-text-type
2178                (1- (point)) (1+ (point)) 'overstrike)
2179               (put-text-property
2180                (- (point) 2) (1- (point)) 'face 'underline))
2181              ((eq previous ?_)
2182               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2183               (put-text-property
2184                (point) (1+ (point)) 'face 'underline)))))))))
2185
2186 (defun article-treat-ansi-sequences ()
2187   "Translate ANSI SGR control sequences into overlays or extents."
2188   (interactive)
2189   (save-excursion
2190     (when (article-goto-body)
2191       (let ((inhibit-read-only t))
2192         (ansi-color-apply-on-region (point) (point-max))))))
2193
2194 (defun gnus-article-treat-unfold-headers ()
2195   "Unfold folded message headers.
2196 Only the headers that fit into the current window width will be
2197 unfolded."
2198   (interactive)
2199   (gnus-with-article-headers
2200     (let (length)
2201       (while (not (eobp))
2202         (save-restriction
2203           (mail-header-narrow-to-field)
2204           (let* ((header (buffer-string))
2205                  (unfoldable
2206                   (or (equal gnus-article-unfold-long-headers t)
2207                       (and (stringp gnus-article-unfold-long-headers)
2208                            (string-match gnus-article-unfold-long-headers header)))))
2209             (with-temp-buffer
2210               (insert header)
2211               (goto-char (point-min))
2212               (while (re-search-forward "\n[\t ]" nil t)
2213                 (replace-match " " t t)))
2214             (setq length (- (point-max) (point-min) 1))
2215             (when (or unfoldable
2216                       (< length (window-width)))
2217               (while (re-search-forward "\n[\t ]" nil t)
2218                 (replace-match " " t t))))
2219           (goto-char (point-max)))))))
2220
2221 (defun gnus-article-treat-fold-headers ()
2222   "Fold message headers."
2223   (interactive)
2224   (gnus-with-article-headers
2225     (while (not (eobp))
2226       (save-restriction
2227         (mail-header-narrow-to-field)
2228         (mail-header-fold-field)
2229         (goto-char (point-max))))))
2230
2231 (defun gnus-treat-smiley ()
2232   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2233   (interactive)
2234   (gnus-with-article-buffer
2235     (if (memq 'smiley gnus-article-wash-types)
2236         (gnus-delete-images 'smiley)
2237       (article-goto-body)
2238       (let ((images (smiley-region (point) (point-max))))
2239         (when images
2240           (gnus-add-wash-type 'smiley)
2241           (dolist (image images)
2242             (gnus-add-image 'smiley image)))))))
2243
2244 (defun gnus-article-remove-images ()
2245   "Remove all images from the article buffer."
2246   (interactive)
2247   (gnus-with-article-buffer
2248     (dolist (elem gnus-article-image-alist)
2249       (gnus-delete-images (car elem)))))
2250
2251 (defun gnus-article-treat-fold-newsgroups ()
2252   "Unfold folded message headers.
2253 Only the headers that fit into the current window width will be
2254 unfolded."
2255   (interactive)
2256   (gnus-with-article-headers
2257     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2258       (save-restriction
2259         (mail-header-narrow-to-field)
2260         (while (re-search-forward ", *" nil t)
2261           (replace-match ", " t t))
2262         (mail-header-fold-field)
2263         (goto-char (point-max))))))
2264
2265 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2266   "Value of `truncate-lines' in Gnus Article buffer.
2267 Valid values are nil, t, `head', `first', `last', an integer or a
2268 predicate.  See Info node `(gnus)Customizing Articles'."
2269   :version "23.1" ;; No Gnus
2270   :group 'gnus-article
2271   ;; :link '(custom-manual "(gnus)Customizing Articles")
2272   :type 'boolean)
2273
2274 (defun gnus-article-toggle-truncate-lines (&optional arg)
2275   "Toggle whether to fold or truncate long lines in article the buffer.
2276 If ARG is non-nil and not a number, toggle
2277 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2278 long lines if and only if arg is positive."
2279   (interactive "P")
2280   (cond
2281    ((and (numberp arg) (> arg 0))
2282     (setq gnus-article-truncate-lines t))
2283    ((numberp arg)
2284     (setq gnus-article-truncate-lines nil))
2285    (arg
2286     (setq gnus-article-truncate-lines
2287           (not gnus-article-truncate-lines))))
2288   (gnus-with-article-buffer
2289     (cond
2290      ((and (numberp arg) (> arg 0))
2291       (setq truncate-lines nil))
2292      ((numberp arg)
2293       (setq truncate-lines t)))
2294     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2295     ;; `toggle-truncate-lines' needs an argument.
2296     (toggle-truncate-lines)))
2297
2298 (defun gnus-article-treat-body-boundary ()
2299   "Place a boundary line at the end of the headers."
2300   (interactive)
2301   (when (and gnus-body-boundary-delimiter
2302              (> (length gnus-body-boundary-delimiter) 0))
2303     (gnus-with-article-headers
2304       (goto-char (point-max))
2305       (let ((start (point)))
2306         (insert "X-Boundary: ")
2307         (gnus-add-text-properties start (point) '(invisible t intangible t))
2308         (insert (let (str)
2309                   (while (>= (1- (window-width)) (length str))
2310                     (setq str (concat str gnus-body-boundary-delimiter)))
2311                   (substring str 0 (1- (window-width))))
2312                 "\n")
2313         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2314
2315 (defun article-fill-long-lines ()
2316   "Fill lines that are wider than the window width."
2317   (interactive)
2318   (save-excursion
2319     (let ((inhibit-read-only t)
2320           (width (window-width (get-buffer-window (current-buffer)))))
2321       (save-restriction
2322         (article-goto-body)
2323         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2324           (while (not (eobp))
2325             (end-of-line)
2326             (when (>= (current-column) (min fill-column width))
2327               (narrow-to-region (min (1+ (point)) (point-max))
2328                                 (point-at-bol))
2329               (let ((goback (point-marker)))
2330                 (fill-paragraph nil)
2331                 (goto-char (marker-position goback)))
2332               (widen))
2333             (forward-line 1)))))))
2334
2335 (defun article-capitalize-sentences ()
2336   "Capitalize the first word in each sentence."
2337   (interactive)
2338   (save-excursion
2339     (let ((inhibit-read-only t)
2340           (paragraph-start "^[\n\^L]"))
2341       (article-goto-body)
2342       (while (not (eobp))
2343         (capitalize-word 1)
2344         (forward-sentence)))))
2345
2346 (defun article-remove-cr ()
2347   "Remove trailing CRs and then translate remaining CRs into LFs."
2348   (interactive)
2349   (save-excursion
2350     (let ((inhibit-read-only t))
2351       (goto-char (point-min))
2352       (while (re-search-forward "\r+$" nil t)
2353         (replace-match "" t t))
2354       (goto-char (point-min))
2355       (while (search-forward "\r" nil t)
2356         (replace-match "\n" t t)))))
2357
2358 (defun article-remove-trailing-blank-lines ()
2359   "Remove all trailing blank lines from the article."
2360   (interactive)
2361   (save-excursion
2362     (let ((inhibit-read-only t))
2363       (goto-char (point-max))
2364       (delete-region
2365        (point)
2366        (progn
2367          (while (and (not (bobp))
2368                      (looking-at "^[ \t]*$")
2369                      (not (gnus-annotation-in-region-p
2370                            (point) (point-at-eol))))
2371            (forward-line -1))
2372          (forward-line 1)
2373          (point))))))
2374
2375 (defvar gnus-face-properties-alist)
2376
2377 (defun article-display-face (&optional force)
2378   "Display any Face headers in the header."
2379   (interactive (list 'force))
2380   (let ((wash-face-p buffer-read-only))
2381     (gnus-with-article-headers
2382       ;; When displaying parts, this function can be called several times on
2383       ;; the same article, without any intended toggle semantic (as typing `W
2384       ;; D d' would have). So face deletion must occur only when we come from
2385       ;; an interactive command, that is when the *Article* buffer is
2386       ;; read-only.
2387       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2388           (gnus-delete-images 'face)
2389         (let ((from (message-fetch-field "from"))
2390               face faces)
2391           (save-current-buffer
2392             (when (and wash-face-p
2393                        (gnus-buffer-live-p gnus-original-article-buffer)
2394                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2395               (set-buffer gnus-original-article-buffer))
2396             (save-restriction
2397               (mail-narrow-to-head)
2398               (when (or force
2399                         ;; Check whether this face is censored.
2400                         (not (and gnus-article-x-face-too-ugly
2401                                   (or from
2402                                       (setq from (message-fetch-field "from")))
2403                                   (string-match gnus-article-x-face-too-ugly
2404                                                 from))))
2405                 (while (gnus-article-goto-header "Face")
2406                   (push (mail-header-field-value) faces)))))
2407           (when faces
2408             (goto-char (point-min))
2409             (let (png image)
2410               (unless (setq from (gnus-article-goto-header "from"))
2411                 (insert "From:")
2412                 (setq from (point))
2413                 (insert " [no `from' set]\n"))
2414               (while faces
2415                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2416                   (setq image
2417                         (apply 'gnus-create-image png 'png t
2418                                (cdr (assq 'png gnus-face-properties-alist))))
2419                   (goto-char from)
2420                   (gnus-add-wash-type 'face)
2421                   (gnus-add-image 'face image)
2422                   (gnus-put-image image nil 'face))))))))))
2423
2424 (defun article-display-x-face (&optional force)
2425   "Look for an X-Face header and display it if present."
2426   (interactive (list 'force))
2427   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2428     (gnus-with-article-headers
2429       ;; Delete the old process, if any.
2430       (when (process-status "article-x-face")
2431         (delete-process "article-x-face"))
2432       ;; See the comment in `article-display-face'.
2433       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2434           ;; We have already displayed X-Faces, so we remove them
2435           ;; instead.
2436           (gnus-delete-images 'xface)
2437         ;; Display X-Faces.
2438         (let ((from (message-fetch-field "from"))
2439               x-faces face)
2440           (save-current-buffer
2441             (when (and wash-face-p
2442                        (gnus-buffer-live-p gnus-original-article-buffer)
2443                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2444               ;; If type `W f', use gnus-original-article-buffer,
2445               ;; otherwise use the current buffer because displaying
2446               ;; RFC822 parts calls this function too.
2447               (set-buffer gnus-original-article-buffer))
2448             (save-restriction
2449               (mail-narrow-to-head)
2450               (and gnus-article-x-face-command
2451                    (or force
2452                        ;; Check whether this face is censored.
2453                        (not (and gnus-article-x-face-too-ugly
2454                                  (or from
2455                                      (setq from (message-fetch-field "from")))
2456                                  (string-match gnus-article-x-face-too-ugly
2457                                                from))))
2458                    (while (gnus-article-goto-header "X-Face")
2459                      (push (mail-header-field-value) x-faces)))))
2460           (when x-faces
2461             ;; We display the face.
2462             (cond ((functionp gnus-article-x-face-command)
2463                    ;; The command is a lisp function, so we call it.
2464                    (mapc gnus-article-x-face-command x-faces))
2465                   ((stringp gnus-article-x-face-command)
2466                    ;; The command is a string, so we interpret the command
2467                    ;; as a, well, command, and fork it off.
2468                    (let ((process-connection-type nil))
2469                      (gnus-set-process-query-on-exit-flag
2470                       (start-process
2471                        "article-x-face" nil shell-file-name
2472                        shell-command-switch gnus-article-x-face-command)
2473                       nil)
2474                      ;; Sending multiple EOFs to xv doesn't work,
2475                      ;; so we only do a single external face.
2476                      (with-temp-buffer
2477                        (insert (car x-faces))
2478                        (process-send-region "article-x-face"
2479                                             (point-min) (point-max)))
2480                      (process-send-eof "article-x-face")))
2481                   (t
2482                    (error "`%s' set to `%s' is not a function"
2483                           gnus-article-x-face-command
2484                           'gnus-article-x-face-command)))))))))
2485
2486 (defun article-decode-mime-words ()
2487   "Decode all MIME-encoded words in the article."
2488   (interactive)
2489   (gnus-with-article-buffer
2490     (let ((inhibit-point-motion-hooks t)
2491           (mail-parse-charset gnus-newsgroup-charset)
2492           (mail-parse-ignored-charsets
2493            (with-current-buffer gnus-summary-buffer
2494              gnus-newsgroup-ignored-charsets)))
2495       (mail-decode-encoded-word-region (point-min) (point-max)))))
2496
2497 (defun article-decode-charset (&optional prompt)
2498   "Decode charset-encoded text in the article.
2499 If PROMPT (the prefix), prompt for a coding system to use."
2500   (interactive "P")
2501   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2502         (inhibit-read-only t)
2503         (mail-parse-charset gnus-newsgroup-charset)
2504         (mail-parse-ignored-charsets
2505          (save-excursion (condition-case nil
2506                              (set-buffer gnus-summary-buffer)
2507                            (error))
2508                          gnus-newsgroup-ignored-charsets))
2509         ct cte ctl charset format)
2510     (save-excursion
2511       (save-restriction
2512         (article-narrow-to-head)
2513         (setq ct (message-fetch-field "Content-Type" t)
2514               cte (message-fetch-field "Content-Transfer-Encoding" t)
2515               ctl (and ct (mail-header-parse-content-type ct))
2516               charset (cond
2517                        (prompt
2518                         (mm-read-coding-system "Charset to decode: "))
2519                        (ctl
2520                         (mail-content-type-get ctl 'charset)))
2521               format (and ctl (mail-content-type-get ctl 'format)))
2522         (when cte
2523           (setq cte (mail-header-strip cte)))
2524         (if (and ctl (not (string-match "/" (car ctl))))
2525             (setq ctl nil))
2526         (goto-char (point-max)))
2527       (forward-line 1)
2528       (save-restriction
2529         (narrow-to-region (point) (point-max))
2530         (when (and (eq mail-parse-charset 'gnus-decoded)
2531                    (eq (mm-body-7-or-8) '8bit))
2532           ;; The text code could have been decoded.
2533           (setq charset mail-parse-charset))
2534         (when (and (or (not ctl)
2535                        (equal (car ctl) "text/plain"))
2536                    (not format)) ;; article with format will decode later.
2537           (mm-decode-body
2538            charset (and cte (intern (downcase
2539                                      (gnus-strip-whitespace cte))))
2540            (car ctl)))))))
2541
2542 (defun article-decode-encoded-words ()
2543   "Remove encoded-word encoding from headers."
2544   (let ((inhibit-point-motion-hooks t)
2545         (mail-parse-charset gnus-newsgroup-charset)
2546         (mail-parse-ignored-charsets
2547          (save-excursion (condition-case nil
2548                              (set-buffer gnus-summary-buffer)
2549                            (error))
2550                          gnus-newsgroup-ignored-charsets))
2551         (inhibit-read-only t)
2552         end start)
2553     (goto-char (point-min))
2554     (when (search-forward "\n\n" nil 'move)
2555       (forward-line -1))
2556     (setq end (point))
2557     (while (not (bobp))
2558       (while (progn
2559                (forward-line -1)
2560                (and (not (bobp))
2561                     (memq (char-after) '(?\t ? )))))
2562       (setq start (point))
2563       (if (looking-at "\
2564 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2565 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2566           (funcall gnus-decode-address-function start end)
2567         (funcall gnus-decode-header-function start end))
2568       (goto-char (setq end start)))))
2569
2570 (defun article-decode-group-name ()
2571   "Decode group names in Newsgroups, Followup-To and Xref headers."
2572   (let ((inhibit-point-motion-hooks t)
2573         (inhibit-read-only t)
2574         (method (gnus-find-method-for-group gnus-newsgroup-name))
2575         regexp)
2576     (when (and (or gnus-group-name-charset-method-alist
2577                    gnus-group-name-charset-group-alist)
2578                (gnus-buffer-live-p gnus-original-article-buffer))
2579       (save-restriction
2580         (article-narrow-to-head)
2581         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2582           (with-current-buffer gnus-original-article-buffer
2583             (goto-char (point-min)))
2584           (setq regexp (concat "^" header
2585                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2586           (while (re-search-forward regexp nil t)
2587             (replace-match (save-match-data
2588                              (gnus-decode-newsgroups
2589                               ;; XXX how to use data in article buffer?
2590                               (with-current-buffer gnus-original-article-buffer
2591                                 (re-search-forward regexp nil t)
2592                                 (match-string 1))
2593                               gnus-newsgroup-name method))
2594                            t t nil 1))
2595           (goto-char (point-min)))))))
2596
2597 (autoload 'idna-to-unicode "idna")
2598
2599 (defun article-decode-idna-rhs ()
2600   "Decode IDNA strings in RHS in various headers in current buffer.
2601 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2602 Mail-Reply-To: and Mail-Followup-To:."
2603   (when gnus-use-idna
2604     (save-restriction
2605       (let ((inhibit-point-motion-hooks t)
2606             (inhibit-read-only t))
2607         (article-narrow-to-head)
2608         (goto-char (point-min))
2609         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2610           (let (ace unicode)
2611             (when (save-match-data
2612                     (and (setq ace (match-string 1))
2613                          (save-excursion
2614                            (and (re-search-backward "^[^ \t]" nil t)
2615                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2616                          (setq unicode (idna-to-unicode ace))))
2617               (unless (string= ace unicode)
2618                 (replace-match unicode nil nil nil 1)))))))))
2619
2620 (defun article-de-quoted-unreadable (&optional force read-charset)
2621   "Translate a quoted-printable-encoded article.
2622 If FORCE, decode the article whether it is marked as quoted-printable
2623 or not.
2624 If READ-CHARSET, ask for a coding system."
2625   (interactive (list 'force current-prefix-arg))
2626   (save-excursion
2627     (let ((inhibit-read-only t) type charset)
2628       (if (gnus-buffer-live-p gnus-original-article-buffer)
2629           (with-current-buffer gnus-original-article-buffer
2630             (setq type
2631                   (gnus-fetch-field "content-transfer-encoding"))
2632             (let* ((ct (gnus-fetch-field "content-type"))
2633                    (ctl (and ct (mail-header-parse-content-type ct))))
2634               (setq charset (and ctl
2635                                  (mail-content-type-get ctl 'charset)))
2636               (if (stringp charset)
2637                   (setq charset (intern (downcase charset)))))))
2638       (if read-charset
2639           (setq charset (mm-read-coding-system "Charset: " charset)))
2640       (unless charset
2641         (setq charset gnus-newsgroup-charset))
2642       (when (or force
2643                 (and type (let ((case-fold-search t))
2644                             (string-match "quoted-printable" type))))
2645         (article-goto-body)
2646         (quoted-printable-decode-region
2647          (point) (point-max) (mm-charset-to-coding-system charset))))))
2648
2649 (defun article-de-base64-unreadable (&optional force read-charset)
2650   "Translate a base64 article.
2651 If FORCE, decode the article whether it is marked as base64 not.
2652 If READ-CHARSET, ask for a coding system."
2653   (interactive (list 'force current-prefix-arg))
2654   (save-excursion
2655     (let ((inhibit-read-only t) type charset)
2656       (if (gnus-buffer-live-p gnus-original-article-buffer)
2657           (with-current-buffer gnus-original-article-buffer
2658             (setq type
2659                   (gnus-fetch-field "content-transfer-encoding"))
2660             (let* ((ct (gnus-fetch-field "content-type"))
2661                    (ctl (and ct (mail-header-parse-content-type ct))))
2662               (setq charset (and ctl
2663                                  (mail-content-type-get ctl 'charset)))
2664               (if (stringp charset)
2665                   (setq charset (intern (downcase charset)))))))
2666       (if read-charset
2667           (setq charset (mm-read-coding-system "Charset: " charset)))
2668       (unless charset
2669         (setq charset gnus-newsgroup-charset))
2670       (when (or force
2671                 (and type (let ((case-fold-search t))
2672                             (string-match "base64" type))))
2673         (article-goto-body)
2674         (save-restriction
2675           (narrow-to-region (point) (point-max))
2676           (base64-decode-region (point-min) (point-max))
2677           (mm-decode-coding-region
2678            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2679
2680 (eval-when-compile
2681   (require 'rfc1843))
2682
2683 (defun article-decode-HZ ()
2684   "Translate a HZ-encoded article."
2685   (interactive)
2686   (require 'rfc1843)
2687   (save-excursion
2688     (let ((inhibit-read-only t))
2689       (rfc1843-decode-region (point-min) (point-max)))))
2690
2691 (defun article-unsplit-urls ()
2692   "Remove the newlines that some other mailers insert into URLs."
2693   (interactive)
2694   (save-excursion
2695     (let ((inhibit-read-only t))
2696       (goto-char (point-min))
2697       (while (re-search-forward
2698               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2699         (replace-match "\\1\\3" t)))
2700     (when (interactive-p)
2701       (gnus-treat-article nil))))
2702
2703
2704 (defun article-wash-html (&optional read-charset)
2705   "Format an HTML article.
2706 If READ-CHARSET, ask for a coding system.  If it is a number, the
2707 charset defined in `gnus-summary-show-article-charset-alist' is used."
2708   (interactive "P")
2709   (save-excursion
2710     (let ((inhibit-read-only t)
2711           charset)
2712       (if read-charset
2713           (if (or (and (numberp read-charset)
2714                        (setq charset
2715                              (cdr
2716                               (assq read-charset
2717                                     gnus-summary-show-article-charset-alist))))
2718                   (setq charset (mm-read-coding-system "Charset: ")))
2719               (let ((gnus-summary-show-article-charset-alist
2720                      (list (cons 1 charset))))
2721                 (with-current-buffer gnus-summary-buffer
2722                   (gnus-summary-show-article 1)))
2723             (error "No charset is given"))
2724         (when (gnus-buffer-live-p gnus-original-article-buffer)
2725           (with-current-buffer gnus-original-article-buffer
2726             (let* ((ct (gnus-fetch-field "content-type"))
2727                    (ctl (and ct (mail-header-parse-content-type ct))))
2728               (setq charset (and ctl
2729                                  (mail-content-type-get ctl 'charset)))
2730               (when (stringp charset)
2731                 (setq charset (intern (downcase charset)))))))
2732         (unless charset
2733           (setq charset gnus-newsgroup-charset)))
2734       (article-goto-body)
2735       (save-window-excursion
2736         (save-restriction
2737           (narrow-to-region (point) (point-max))
2738           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2739                  (entry (assq func mm-text-html-washer-alist)))
2740             (when entry
2741               (setq func (cdr entry)))
2742             (cond
2743              ((functionp func)
2744               (funcall func))
2745              (t
2746               (apply (car func) (cdr func))))))))))
2747
2748 ;; External.
2749 (declare-function w3-region "ext:w3-display" (st nd))
2750
2751 (defun gnus-article-wash-html-with-w3 ()
2752   "Wash the current buffer with w3."
2753   (mm-setup-w3)
2754   (let ((w3-strict-width (window-width))
2755         (url-standalone-mode t)
2756         (url-gateway-unplugged t)
2757         (w3-honor-stylesheets nil))
2758     (condition-case ()
2759         (w3-region (point-min) (point-max))
2760       (error))))
2761
2762 ;; External.
2763 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2764
2765 (defun gnus-article-wash-html-with-w3m ()
2766   "Wash the current buffer with emacs-w3m."
2767   (mm-setup-w3m)
2768   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2769         w3m-force-redisplay)
2770     (w3m-region (point-min) (point-max)))
2771   ;; Put the mark meaning this part was rendered by emacs-w3m.
2772   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2773   (when (and mm-inline-text-html-with-w3m-keymap
2774              (boundp 'w3m-minor-mode-map)
2775              w3m-minor-mode-map)
2776     (if (and (boundp 'w3m-link-map)
2777              w3m-link-map)
2778         (let* ((start (point-min))
2779                (end (point-max))
2780                (on (get-text-property start 'w3m-href-anchor))
2781                (map (copy-keymap w3m-link-map))
2782                next)
2783           (set-keymap-parent map w3m-minor-mode-map)
2784           (while (< start end)
2785             (if on
2786                 (progn
2787                   (setq next (or (text-property-any start end
2788                                                     'w3m-href-anchor nil)
2789                                  end))
2790                   (put-text-property start next 'keymap map))
2791               (setq next (or (text-property-not-all start end
2792                                                     'w3m-href-anchor nil)
2793                              end))
2794               (put-text-property start next 'keymap w3m-minor-mode-map))
2795             (setq start next
2796                   on (not on))))
2797       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2798
2799 (defvar charset) ;; Bound by `article-wash-html'.
2800
2801 (defun gnus-article-wash-html-with-w3m-standalone ()
2802   "Wash the current buffer with w3m."
2803   (if (mm-w3m-standalone-supports-m17n-p)
2804       (progn
2805         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2806           ;; The default.
2807           (setq charset 'iso-8859-1))
2808         (let ((coding-system-for-write charset)
2809               (coding-system-for-read charset))
2810           (call-process-region
2811            (point-min) (point-max)
2812            "w3m" t t nil "-dump" "-T" "text/html"
2813            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2814     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2815
2816 (defvar gnus-article-browse-html-temp-list nil
2817   "List of temporary files created by `gnus-article-browse-html-parts'.
2818 Internal variable.")
2819
2820 (defcustom gnus-article-browse-delete-temp 'ask
2821   "What to do with temporary files from `gnus-article-browse-html-parts'.
2822 If nil, don't delete temporary files.  If it is t, delete them on
2823 exit from the summary buffer.  If it is the symbol `file', query
2824 on each file, if it is `ask' ask once when exiting from the
2825 summary buffer."
2826   :group 'gnus-article
2827   :version "23.1" ;; No Gnus
2828   :type '(choice (const :tag "Don't delete" nil)
2829                  (const :tag "Don't ask" t)
2830                  (const :tag "Ask" ask)
2831                  (const :tag "Ask for each file" file)))
2832
2833 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2834
2835 (defun gnus-article-browse-delete-temp-files (&optional how)
2836   "Delete temp-files created by `gnus-article-browse-html-parts'."
2837   (when (and gnus-article-browse-html-temp-list
2838              (progn
2839                (or how (setq how gnus-article-browse-delete-temp))
2840                (if (eq how 'ask)
2841                    (let ((files (length gnus-article-browse-html-temp-list)))
2842                      (gnus-y-or-n-p (format
2843                                      "Delete all %s temporary HTML file%s? "
2844                                      files
2845                                      (if (> files 1) "s" ""))))
2846                  how)))
2847     (dolist (file gnus-article-browse-html-temp-list)
2848       (cond ((file-directory-p file)
2849              (when (or (not (eq how 'file))
2850                        (gnus-y-or-n-p
2851                         (format
2852                          "Delete temporary HTML file(s) in directory `%s'? "
2853                          (file-name-as-directory file))))
2854                (gnus-delete-directory file)))
2855             ((file-exists-p file)
2856              (when (or (not (eq how 'file))
2857                        (gnus-y-or-n-p
2858                         (format "Delete temporary HTML file `%s'? " file)))
2859                (delete-file file)))))
2860     ;; Also remove file from the list when not deleted or if file doesn't
2861     ;; exist anymore.
2862     (setq gnus-article-browse-html-temp-list nil))
2863   gnus-article-browse-html-temp-list)
2864
2865 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2866   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2867 Return file name."
2868   (save-match-data
2869     (let (file type)
2870       (catch 'found
2871         (dolist (handle handles)
2872           (cond
2873            ((not (listp handle)))
2874            ((equal (mm-handle-media-supertype handle) "multipart")
2875             (when (setq file (gnus-article-browse-html-save-cid-content
2876                               cid handle directory))
2877               (throw 'found file)))
2878            ((equal (concat "<" cid ">") (mm-handle-id handle))
2879             (setq file
2880                   (expand-file-name
2881                    (or (mail-content-type-get
2882                         (mm-handle-disposition handle) 'filename)
2883                        (mail-content-type-get
2884                         (setq type (mm-handle-type handle)) 'name)
2885                        (concat
2886                         (make-temp-name "cid")
2887                         (car (rassoc (car type) mailcap-mime-extensions))))
2888                    directory))
2889             (mm-save-part-to-file handle file)
2890             (throw 'found file))))))))
2891
2892 (defun gnus-article-browse-html-parts (list &optional header)
2893   "View all \"text/html\" parts from LIST.
2894 Recurse into multiparts.  The optional HEADER that should be a decoded
2895 message header will be added to the bodies of the \"text/html\" parts."
2896   ;; Internal function used by `gnus-article-browse-html-article'.
2897   (let (type file charset content cid-dir tmp-file showed)
2898     ;; Find and show the html-parts.
2899     (dolist (handle list)
2900       ;; If HTML, show it:
2901       (cond ((not (listp handle)))
2902             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2903                  (and (equal (car type) "message/external-body")
2904                       (or header
2905                           (setq file (or (mail-content-type-get type 'name)
2906                                          (mail-content-type-get
2907                                           (mm-handle-disposition handle)
2908                                           'filename))))
2909                       (or (mm-handle-cache handle)
2910                           (condition-case code
2911                               (progn (mm-extern-cache-contents handle) t)
2912                             (error
2913                              (gnus-message 3 "%s" (error-message-string code))
2914                              (when (>= gnus-verbose 3) (sit-for 2))
2915                              nil)))
2916                       (progn
2917                         (setq handle (mm-handle-cache handle)
2918                               type (mm-handle-type handle))
2919                         (equal (car type) "text/html"))))
2920              (setq charset (mail-content-type-get type 'charset)
2921                    content (mm-get-part handle))
2922              (with-temp-buffer
2923                (if (eq charset 'gnus-decoded)
2924                    (mm-enable-multibyte)
2925                  (mm-disable-multibyte))
2926                (insert content)
2927                ;; resolve cid contents
2928                (let ((case-fold-search t)
2929                      cid-file)
2930                  (goto-char (point-min))
2931                  (while (re-search-forward "\
2932 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2933                                            nil t)
2934                    (unless cid-dir
2935                      (setq cid-dir (mm-make-temp-file "cid" t))
2936                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2937                    (setq file nil
2938                          content nil)
2939                    (when (setq cid-file
2940                                (gnus-article-browse-html-save-cid-content
2941                                 (match-string 2)
2942                                 (with-current-buffer gnus-article-buffer
2943                                   gnus-article-mime-handles)
2944                                 cid-dir))
2945                      (replace-match (concat "file://" cid-file)
2946                                     nil nil nil 1))))
2947                (unless content (setq content (buffer-string))))
2948              (when (or charset header (not file))
2949                (setq tmp-file (mm-make-temp-file
2950                                ;; Do we need to care for 8.3 filenames?
2951                                "mm-" nil ".html")))
2952              ;; Add a meta html tag to specify charset and a header.
2953              (cond
2954               (header
2955                (let (title eheader body hcharset coding force-charset)
2956                  (with-temp-buffer
2957                    (mm-enable-multibyte)
2958                    (setq case-fold-search t)
2959                    (insert header "\n")
2960                    (setq title (message-fetch-field "subject"))
2961                    (goto-char (point-min))
2962                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2963                      (replace-match (cond ((match-beginning 1) "&lt;")
2964                                           ((match-beginning 2) "&gt;")
2965                                           (t "&amp;"))))
2966                    (goto-char (point-min))
2967                    (insert "<pre>\n")
2968                    (goto-char (point-max))
2969                    (insert "</pre>\n<hr>\n")
2970                    ;; We have to examine charset one by one since
2971                    ;; charset specified in parts might be different.
2972                    (if (eq charset 'gnus-decoded)
2973                        (setq charset 'utf-8
2974                              eheader (mm-encode-coding-string (buffer-string)
2975                                                               charset)
2976                              title (when title
2977                                      (mm-encode-coding-string title charset))
2978                              body (mm-encode-coding-string content charset)
2979                              force-charset t)
2980                      (setq hcharset (mm-find-mime-charset-region (point-min)
2981                                                                  (point-max)))
2982                      (cond ((= (length hcharset) 1)
2983                             (setq hcharset (car hcharset)
2984                                   coding (mm-charset-to-coding-system
2985                                           hcharset)))
2986                            ((> (length hcharset) 1)
2987                             (setq hcharset 'utf-8
2988                                   coding hcharset)))
2989                      (if coding
2990                          (if charset
2991                              (progn
2992                                (setq body
2993                                      (mm-charset-to-coding-system charset))
2994                                (if (eq coding body)
2995                                    (setq eheader (mm-encode-coding-string
2996                                                   (buffer-string) coding)
2997                                          title (when title
2998                                                  (mm-encode-coding-string
2999                                                   title coding))
3000                                          body content)
3001                                  (setq charset 'utf-8
3002                                        eheader (mm-encode-coding-string
3003                                                 (buffer-string) charset)
3004                                        title (when title
3005                                                (mm-encode-coding-string
3006                                                 title charset))
3007                                        body (mm-encode-coding-string
3008                                              (mm-decode-coding-string
3009                                               content body)
3010                                              charset)
3011                                        force-charset t)))
3012                            (setq charset hcharset
3013                                  eheader (mm-encode-coding-string
3014                                           (buffer-string) coding)
3015                                  title (when title
3016                                          (mm-encode-coding-string
3017                                           title coding))
3018                                  body content))
3019                        (setq eheader (mm-string-as-unibyte (buffer-string))
3020                              body content)))
3021                    (erase-buffer)
3022                    (mm-disable-multibyte)
3023                    (insert body)
3024                    (when charset
3025                      (mm-add-meta-html-tag handle charset force-charset))
3026                    (when title
3027                      (goto-char (point-min))
3028                      (unless (search-forward "<title>" nil t)
3029                        (re-search-forward "<head>\\s-*" nil t)
3030                        (insert "<title>" title "</title>\n")))
3031                    (goto-char (point-min))
3032                    (or (re-search-forward
3033                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3034                        (re-search-forward
3035                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3036                    (insert eheader)
3037                    (mm-write-region (point-min) (point-max)
3038                                     tmp-file nil nil nil 'binary t))))
3039               (charset
3040                (mm-with-unibyte-buffer
3041                  (insert (if (eq charset 'gnus-decoded)
3042                              (mm-encode-coding-string content
3043                                                       (setq charset 'utf-8))
3044                            content))
3045                  (if (or (mm-add-meta-html-tag handle charset)
3046                          (not file))
3047                      (mm-write-region (point-min) (point-max)
3048                                       tmp-file nil nil nil 'binary t)
3049                    (setq tmp-file nil))))
3050               (tmp-file
3051                (mm-save-part-to-file handle tmp-file)))
3052              (when tmp-file
3053                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3054              (add-hook 'gnus-summary-prepare-exit-hook
3055                        'gnus-article-browse-delete-temp-files)
3056              (add-hook 'gnus-exit-gnus-hook
3057                        (lambda  ()
3058                          (gnus-article-browse-delete-temp-files t)))
3059              ;; FIXME: Warn if there's an <img> tag?
3060              (browse-url-of-file (or tmp-file (expand-file-name file)))
3061              (setq showed t))
3062             ;; If multipart, recurse
3063             ((equal (mm-handle-media-supertype handle) "multipart")
3064              (when (gnus-article-browse-html-parts handle header)
3065                (setq showed t)))
3066             ((equal (mm-handle-media-type handle) "message/rfc822")
3067              (mm-with-multibyte-buffer
3068                (mm-insert-part handle)
3069                (setq handle (mm-dissect-buffer t t))
3070                (when (and (bufferp (car handle))
3071                           (stringp (car (mm-handle-type handle))))
3072                  (setq handle (list handle)))
3073                (when header
3074                  (article-decode-encoded-words)
3075                  (let ((gnus-visible-headers
3076                         (or (get 'gnus-visible-headers 'standard-value)
3077                             gnus-visible-headers)))
3078                    (article-hide-headers))
3079                  (goto-char (point-min))
3080                  (search-forward "\n\n" nil 'move)
3081                  (skip-chars-backward "\t\n ")
3082                  (setq header (buffer-substring (point-min) (point)))))
3083              (when (prog1
3084                        (gnus-article-browse-html-parts handle header)
3085                      (mm-destroy-parts handle))
3086                (setq showed t)))))
3087     showed))
3088
3089 (defun gnus-article-browse-html-article (&optional arg)
3090   "View \"text/html\" parts of the current article with a WWW browser.
3091 Inline images embedded in a message using the cid scheme, as they are
3092 generally considered to be safe, will be processed properly.
3093 The message header is added to the beginning of every html part unless
3094 the prefix argument ARG is given.
3095
3096 Warning: Spammers use links to images (using the http scheme) in HTML
3097 articles to verify whether you have read the message.  As
3098 `gnus-article-browse-html-article' passes the HTML content to the
3099 browser without eliminating these \"web bugs\" you should only
3100 use it for mails from trusted senders.
3101
3102 If you always want to display HTML parts in the browser, set
3103 `mm-text-html-renderer' to nil.
3104
3105 This command creates temporary files to pass HTML contents including
3106 images if any to the browser, and deletes them when exiting the group
3107 \(if you want)."
3108   ;; Cf. `mm-w3m-safe-url-regexp'
3109   (interactive "P")
3110   (if arg
3111       (gnus-summary-show-article)
3112     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3113                                     gnus-visible-headers))
3114           ;; As we insert a <hr>, there's no need for the body boundary.
3115           (gnus-treat-body-boundary nil))
3116       (gnus-summary-show-article)))
3117   (with-current-buffer gnus-article-buffer
3118     (let ((header (unless arg
3119                     (save-restriction
3120                       (widen)
3121                       (buffer-substring-no-properties
3122                        (goto-char (point-min))
3123                        (if (search-forward "\n\n" nil t)
3124                            (match-beginning 0)
3125                          (goto-char (point-max))
3126                          (skip-chars-backward "\t\n ")
3127                          (point))))))
3128           parts)
3129       (set-buffer gnus-original-article-buffer)
3130       (setq parts (mm-dissect-buffer t t))
3131       ;; If singlepart, enforce a list.
3132       (when (and (bufferp (car parts))
3133                  (stringp (car (mm-handle-type parts))))
3134         (setq parts (list parts)))
3135       ;; Process the list
3136       (unless (gnus-article-browse-html-parts parts header)
3137         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3138       (mm-destroy-parts parts)
3139       (unless arg
3140         (gnus-summary-show-article)))))
3141
3142 (defun article-hide-list-identifiers ()
3143   "Remove list identifies from the Subject header.
3144 The `gnus-list-identifiers' variable specifies what to do."
3145   (interactive)
3146   (let ((inhibit-point-motion-hooks t)
3147         (regexp (if (consp gnus-list-identifiers)
3148                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3149                   gnus-list-identifiers))
3150         (inhibit-read-only t))
3151     (when regexp
3152       (save-excursion
3153         (save-restriction
3154           (article-narrow-to-head)
3155           (goto-char (point-min))
3156           (while (re-search-forward
3157                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3158                   nil t)
3159             (delete-region (match-beginning 2) (match-end 0))
3160             (beginning-of-line))
3161           (when (re-search-forward
3162                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3163             (delete-region (match-beginning 1) (match-end 1))))))))
3164
3165 (defun article-hide-pem (&optional arg)
3166   "Toggle hiding of any PEM headers and signatures in the current article.
3167 If given a negative prefix, always show; if given a positive prefix,
3168 always hide."
3169   (interactive (gnus-article-hidden-arg))
3170   (unless (gnus-article-check-hidden-text 'pem arg)
3171     (save-excursion
3172       (let ((inhibit-read-only t) end)
3173         (goto-char (point-min))
3174         ;; Hide the horrendously ugly "header".
3175         (when (and (search-forward
3176                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3177                     nil t)
3178                    (setq end (1+ (match-beginning 0))))
3179           (gnus-add-wash-type 'pem)
3180           (gnus-article-hide-text-type
3181            end
3182            (if (search-forward "\n\n" nil t)
3183                (match-end 0)
3184              (point-max))
3185            'pem)
3186           ;; Hide the trailer as well
3187           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3188                                 nil t)
3189             (gnus-article-hide-text-type
3190              (match-beginning 0) (match-end 0) 'pem)))))))
3191
3192 (defun article-strip-banner ()
3193   "Strip the banners specified by the `banner' group parameter and by
3194 `gnus-article-address-banner-alist'."
3195   (interactive)
3196   (save-excursion
3197     (save-restriction
3198       (let ((inhibit-point-motion-hooks t))
3199         (when (gnus-parameter-banner gnus-newsgroup-name)
3200           (article-really-strip-banner
3201            (gnus-parameter-banner gnus-newsgroup-name)))
3202         (when gnus-article-address-banner-alist
3203           ;; Note that the From header is decoded here, so it is
3204           ;; required that the *-extract-address-components function
3205           ;; supports non-ASCII text.
3206           (let ((from (save-restriction
3207                         (widen)
3208                         (article-narrow-to-head)
3209                         (mail-fetch-field "from"))))
3210             (when (and from
3211                        (setq from
3212                              (cadr (funcall gnus-extract-address-components
3213                                             from))))
3214               (catch 'found
3215                 (dolist (pair gnus-article-address-banner-alist)
3216                   (when (string-match (car pair) from)
3217                     (throw 'found
3218                            (article-really-strip-banner (cdr pair)))))))))))))
3219
3220 (defun article-really-strip-banner (banner)
3221   "Strip the banner specified by the argument."
3222   (save-excursion
3223     (save-restriction
3224       (let ((inhibit-point-motion-hooks t)
3225             (gnus-signature-limit nil)
3226             (inhibit-read-only t))
3227         (article-goto-body)
3228         (cond
3229          ((eq banner 'signature)
3230           (when (gnus-article-narrow-to-signature)
3231             (widen)
3232             (forward-line -1)
3233             (delete-region (point) (point-max))))
3234          ((symbolp banner)
3235           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3236               (while (re-search-forward banner nil t)
3237                 (delete-region (match-beginning 0) (match-end 0)))))
3238          ((stringp banner)
3239           (while (re-search-forward banner nil t)
3240             (delete-region (match-beginning 0) (match-end 0)))))))))
3241
3242 (defun article-babel ()
3243   "Translate article using an online translation service."
3244   (interactive)
3245   (require 'babel)
3246   (gnus-with-article-buffer
3247     (when (article-goto-body)
3248       (let* ((start (point))
3249              (end (point-max))
3250              (orig (buffer-substring start end))
3251              (trans (babel-as-string orig)))
3252         (save-restriction
3253           (narrow-to-region start end)
3254           (delete-region start end)
3255           (insert trans))))))
3256
3257 (defun article-hide-signature (&optional arg)
3258   "Hide the signature in the current article.
3259 If given a negative prefix, always show; if given a positive prefix,
3260 always hide."
3261   (interactive (gnus-article-hidden-arg))
3262   (unless (gnus-article-check-hidden-text 'signature arg)
3263     (save-excursion
3264       (save-restriction
3265         (let ((inhibit-read-only t))
3266           (when (gnus-article-narrow-to-signature)
3267             (gnus-article-hide-text-type
3268              (point-min) (point-max) 'signature))))))
3269   (gnus-set-mode-line 'article))
3270
3271 (defun article-strip-headers-in-body ()
3272   "Strip offensive headers from bodies."
3273   (interactive)
3274   (save-excursion
3275     (article-goto-body)
3276     (let ((case-fold-search t))
3277       (when (looking-at "x-no-archive:")
3278         (gnus-delete-line)))))
3279
3280 (defun article-strip-leading-blank-lines ()
3281   "Remove all blank lines from the beginning of the article."
3282   (interactive)
3283   (save-excursion
3284     (let ((inhibit-point-motion-hooks t)
3285           (inhibit-read-only t))
3286       (when (article-goto-body)
3287         (while (and (not (eobp))
3288                     (looking-at "[ \t]*$"))
3289           (gnus-delete-line))))))
3290
3291 (defun article-narrow-to-head ()
3292   "Narrow the buffer to the head of the message.
3293 Point is left at the beginning of the narrowed-to region."
3294   (narrow-to-region
3295    (goto-char (point-min))
3296    (if (search-forward "\n\n" nil 1)
3297        (1- (point))
3298      (point-max)))
3299   (goto-char (point-min)))
3300
3301 (defun article-goto-body ()
3302   "Place point at the start of the body."
3303   (goto-char (point-min))
3304   (cond
3305    ;; This variable is only bound when dealing with separate
3306    ;; MIME body parts.
3307    (article-goto-body-goes-to-point-min-p
3308     t)
3309    ((search-forward "\n\n" nil t)
3310     t)
3311    (t
3312     (goto-char (point-max))
3313     nil)))
3314
3315 (defun article-strip-multiple-blank-lines ()
3316   "Replace consecutive blank lines with one empty line."
3317   (interactive)
3318   (save-excursion
3319     (let ((inhibit-point-motion-hooks t)
3320           (inhibit-read-only t))
3321       ;; First make all blank lines empty.
3322       (article-goto-body)
3323       (while (re-search-forward "^[ \t]+$" nil t)
3324         (unless (gnus-annotation-in-region-p
3325                  (match-beginning 0) (match-end 0))
3326           (replace-match "" nil t)))
3327       ;; Then replace multiple empty lines with a single empty line.
3328       (article-goto-body)
3329       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3330         (unless (gnus-annotation-in-region-p
3331                  (match-beginning 0) (match-end 0))
3332           (delete-region (match-beginning 1) (match-end 1)))))))
3333
3334 (defun article-strip-leading-space ()
3335   "Remove all white space from the beginning of the lines in the article."
3336   (interactive)
3337   (save-excursion
3338     (let ((inhibit-point-motion-hooks t)
3339           (inhibit-read-only t))
3340       (article-goto-body)
3341       (while (re-search-forward "^[ \t]+" nil t)
3342         (replace-match "" t t)))))
3343
3344 (defun article-strip-trailing-space ()
3345   "Remove all white space from the end of the lines in the article."
3346   (interactive)
3347   (save-excursion
3348     (let ((inhibit-point-motion-hooks t)
3349           (inhibit-read-only t))
3350       (article-goto-body)
3351       (while (re-search-forward "[ \t]+$" nil t)
3352         (replace-match "" t t)))))
3353
3354 (defun article-strip-blank-lines ()
3355   "Strip leading, trailing and multiple blank lines."
3356   (interactive)
3357   (article-strip-leading-blank-lines)
3358   (article-remove-trailing-blank-lines)
3359   (article-strip-multiple-blank-lines))
3360
3361 (defun article-strip-all-blank-lines ()
3362   "Strip all blank lines."
3363   (interactive)
3364   (save-excursion
3365     (let ((inhibit-point-motion-hooks t)
3366           (inhibit-read-only t))
3367       (article-goto-body)
3368       (while (re-search-forward "^[ \t]*\n" nil t)
3369         (replace-match "" t t)))))
3370
3371 (defun gnus-article-narrow-to-signature ()
3372   "Narrow to the signature; return t if a signature is found, else nil."
3373   (let ((inhibit-point-motion-hooks t))
3374     (when (gnus-article-search-signature)
3375       (forward-line 1)
3376       ;; Check whether we have some limits to what we consider
3377       ;; to be a signature.
3378       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3379                       (list gnus-signature-limit)))
3380             limit limited)
3381         (while (setq limit (pop limits))
3382           (if (or (and (integerp limit)
3383                        (< (- (point-max) (point)) limit))
3384                   (and (floatp limit)
3385                        (< (count-lines (point) (point-max)) limit))
3386                   (and (functionp limit)
3387                        (funcall limit))
3388                   (and (stringp limit)
3389                        (not (re-search-forward limit nil t))))
3390               ()                        ; This limit did not succeed.
3391             (setq limited t
3392                   limits nil)))
3393         (unless limited
3394           (narrow-to-region (point) (point-max))
3395           t)))))
3396
3397 (defun gnus-article-search-signature ()
3398   "Search the current buffer for the signature separator.
3399 Put point at the beginning of the signature separator."
3400   (let ((cur (point)))
3401     (goto-char (point-max))
3402     (if (if (stringp gnus-signature-separator)
3403             (re-search-backward gnus-signature-separator nil t)
3404           (let ((seps gnus-signature-separator))
3405             (while (and seps
3406                         (not (re-search-backward (car seps) nil t)))
3407               (pop seps))
3408             seps))
3409         t
3410       (goto-char cur)
3411       nil)))
3412
3413 (defun gnus-article-hidden-arg ()
3414   "Return the current prefix arg as a number, or 0 if no prefix."
3415   (list (if current-prefix-arg
3416             (prefix-numeric-value current-prefix-arg)
3417           0)))
3418
3419 (defun gnus-article-check-hidden-text (type arg)
3420   "Return nil if hiding is necessary.
3421 Arg can be nil or a number.  nil and positive means hide, negative
3422 means show, 0 means toggle."
3423   (save-excursion
3424     (save-restriction
3425       (let ((hide (gnus-article-hidden-text-p type)))
3426         (cond
3427          ((or (null arg)
3428               (> arg 0))
3429           nil)
3430          ((< arg 0)
3431           (gnus-article-show-hidden-text type)
3432           t)
3433          (t
3434           (if (eq hide 'hidden)
3435               (progn
3436                 (gnus-article-show-hidden-text type)
3437                 t)
3438             nil)))))))
3439
3440 (defun gnus-article-hidden-text-p (type)
3441   "Say whether the current buffer contains hidden text of type TYPE."
3442   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3443     (while (and pos
3444                 (not (get-text-property pos 'invisible))
3445                 (not (get-text-property pos 'dummy-invisible)))
3446       (setq pos
3447             (text-property-any (1+ pos) (point-max) 'article-type type)))
3448     (if pos
3449         'hidden
3450       nil)))
3451
3452 (defun gnus-article-show-hidden-text (type &optional dummy)
3453   "Show all hidden text of type TYPE.
3454 Originally it is hide instead of DUMMY."
3455   (let ((inhibit-read-only t)
3456         (inhibit-point-motion-hooks t))
3457     (gnus-remove-text-properties-when
3458      'article-type type
3459      (point-min) (point-max)
3460      (cons 'article-type (cons type
3461                                gnus-hidden-properties)))
3462     (gnus-delete-wash-type type)))
3463
3464 (defconst article-time-units
3465   `((year . ,(* 365.25 24 60 60))
3466     (week . ,(* 7 24 60 60))
3467     (day . ,(* 24 60 60))
3468     (hour . ,(* 60 60))
3469     (minute . 60)
3470     (second . 1))
3471   "Mapping from time units to seconds.")
3472
3473 (defun gnus-article-forward-header ()
3474   "Move point to the start of the next header.
3475 If the current header is a continuation header, this can be several
3476 lines forward."
3477   (let ((ended nil))
3478     (while (not ended)
3479       (forward-line 1)
3480       (if (looking-at "[ \t]+[^ \t]")
3481           (forward-line 1)
3482         (setq ended t)))))
3483
3484 (defun article-date-ut (&optional type highlight)
3485   "Convert DATE date to universal time in the current article.
3486 If TYPE is `local', convert to local time; if it is `lapsed', output
3487 how much time has lapsed since DATE.  For `lapsed', the value of
3488 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3489 should replace the \"Date:\" one, or should be added below it."
3490   (interactive (list 'ut t))
3491   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3492          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3493                              tdate-regexp)
3494                             ((eq type 'lapsed)
3495                              "^X-Sent:[ \t]")
3496                             (article-lapsed-timer
3497                              "^Date:[ \t]")
3498                             (t
3499                              tdate-regexp)))
3500          (case-fold-search t)
3501          (inhibit-read-only t)
3502          (inhibit-point-motion-hooks t)
3503          pos date bface eface)
3504     (save-excursion
3505       (save-restriction
3506         (widen)
3507         (goto-char (point-min))
3508         (while (or (setq date (get-text-property (setq pos (point))
3509                                                  'original-date))
3510                    (when (setq pos (next-single-property-change
3511                                     (point) 'original-date))
3512                      (setq date (get-text-property pos 'original-date))
3513                      t))
3514           (narrow-to-region
3515            pos (if (setq pos (text-property-any pos (point-max)
3516                                                 'original-date nil))
3517                    (progn
3518                      (goto-char pos)
3519                      (if (or (bolp) (eobp))
3520                          (point)
3521                        (1+ (point))))
3522                  (point-max)))
3523           (goto-char (point-min))
3524           (when (re-search-forward tdate-regexp nil t)
3525             (setq bface (get-text-property (point-at-bol) 'face)
3526                   eface (get-text-property (1- (point-at-eol)) 'face)))
3527           (goto-char (point-min))
3528           (setq pos nil)
3529           ;; Delete any old Date headers.
3530           (while (re-search-forward date-regexp nil t)
3531             (if pos
3532                 (delete-region (point-at-bol) (progn
3533                                                 (gnus-article-forward-header)
3534                                                 (point)))
3535               (delete-region (point-at-bol) (progn
3536                                               (gnus-article-forward-header)
3537                                               (forward-char -1)
3538                                               (point)))
3539               (setq pos (point))))
3540           (when (and (not pos)
3541                      (re-search-forward tdate-regexp nil t))
3542             (forward-line 1))
3543           (gnus-goto-char pos)
3544           (insert (article-make-date-line date (or type 'ut)))
3545           (unless pos
3546             (insert "\n")
3547             (forward-line -1))
3548           ;; Do highlighting.
3549           (beginning-of-line)
3550           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3551             (put-text-property (match-beginning 1) (1+ (match-end 1))
3552                                'face bface)
3553             (put-text-property (match-beginning 2) (match-end 2)
3554                                'face eface))
3555           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3556           (goto-char (point-max))
3557           (widen))))))
3558
3559 (defun article-make-date-line (date type)
3560   "Return a DATE line of TYPE."
3561   (unless (memq type '(local ut original user iso8601 lapsed english))
3562     (error "Unknown conversion type: %s" type))
3563   (condition-case ()
3564       (let ((time (date-to-time date)))
3565         (cond
3566          ;; Convert to the local timezone.
3567          ((eq type 'local)
3568           (concat "Date: " (message-make-date time)))
3569          ;; Convert to Universal Time.
3570          ((eq type 'ut)
3571           (concat "Date: "
3572                   (substring
3573                    (message-make-date
3574                     (let* ((e (parse-time-string date))
3575                            (tm (apply 'encode-time e))
3576                            (ms (car tm))
3577                            (ls (- (cadr tm) (car (current-time-zone time)))))
3578                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3579                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3580                             (t (list ms ls)))))
3581                    0 -5)
3582                   "UT"))
3583          ;; Get the original date from the article.
3584          ((eq type 'original)
3585           (concat "Date: " (if (string-match "\n+$" date)
3586                                (substring date 0 (match-beginning 0))
3587                              date)))
3588          ;; Let the user define the format.
3589          ((eq type 'user)
3590           (let ((format (or (condition-case nil
3591                                 (with-current-buffer gnus-summary-buffer
3592                                   gnus-article-time-format)
3593                               (error nil))
3594                             gnus-article-time-format)))
3595             (if (functionp format)
3596                 (funcall format time)
3597               (concat "Date: " (format-time-string format time)))))
3598          ;; ISO 8601.
3599          ((eq type 'iso8601)
3600           (let ((tz (car (current-time-zone time))))
3601             (concat
3602              "Date: "
3603              (format-time-string "%Y%m%dT%H%M%S" time)
3604              (format "%s%02d%02d"
3605                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3606                      (/ (% (abs tz) 3600) 60)))))
3607          ;; Do an X-Sent lapsed format.
3608          ((eq type 'lapsed)
3609           ;; If the date is seriously mangled, the timezone functions are
3610           ;; liable to bug out, so we ignore all errors.
3611           (let* ((now (current-time))
3612                  (real-time (subtract-time now time))
3613                  (real-sec (and real-time
3614                                 (+ (* (float (car real-time)) 65536)
3615                                    (cadr real-time))))
3616                  (sec (and real-time (abs real-sec)))
3617                  num prev)
3618             (cond
3619              ((null real-time)
3620               "X-Sent: Unknown")
3621              ((zerop sec)
3622               "X-Sent: Now")
3623              (t
3624               (concat
3625                "X-Sent: "
3626                ;; This is a bit convoluted, but basically we go
3627                ;; through the time units for years, weeks, etc,
3628                ;; and divide things to see whether that results
3629                ;; in positive answers.
3630                (mapconcat
3631                 (lambda (unit)
3632                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3633                       ;; The (remaining) seconds are too few to
3634                       ;; be divided into this time unit.
3635                       ""
3636                     ;; It's big enough, so we output it.
3637                     (setq sec (- sec (* num (cdr unit))))
3638                     (prog1
3639                         (concat (if prev ", " "") (int-to-string
3640                                                    (floor num))
3641                                 " " (symbol-name (car unit))
3642                                 (if (> num 1) "s" ""))
3643                       (setq prev t))))
3644                 article-time-units "")
3645                ;; If dates are odd, then it might appear like the
3646                ;; article was sent in the future.
3647                (if (> real-sec 0)
3648                    " ago"
3649                  " in the future"))))))
3650          ;; Display the date in proper English
3651          ((eq type 'english)
3652           (let ((dtime (decode-time time)))
3653             (concat
3654              "Date: the "
3655              (number-to-string (nth 3 dtime))
3656              (let ((digit (% (nth 3 dtime) 10)))
3657                (cond
3658                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3659                 ((= digit 1) "st")
3660                 ((= digit 2) "nd")
3661                 ((= digit 3) "rd")
3662                 (t "th")))
3663              " of "
3664              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3665              " "
3666              (number-to-string (nth 5 dtime))
3667              " at "
3668              (format "%02d" (nth 2 dtime))
3669              ":"
3670              (format "%02d" (nth 1 dtime)))))))
3671     (error
3672      (format "Date: %s (from Gnus)" date))))
3673
3674 (defun article-date-local (&optional highlight)
3675   "Convert the current article date to the local timezone."
3676   (interactive (list t))
3677   (article-date-ut 'local highlight))
3678
3679 (defun article-date-english (&optional highlight)
3680   "Convert the current article date to something that is proper English."
3681   (interactive (list t))
3682   (article-date-ut 'english highlight))
3683
3684 (defun article-date-original (&optional highlight)
3685   "Convert the current article date to what it was originally.
3686 This is only useful if you have used some other date conversion
3687 function and want to see what the date was before converting."
3688   (interactive (list t))
3689   (article-date-ut 'original highlight))
3690
3691 (defun article-date-lapsed (&optional highlight)
3692   "Convert the current article date to time lapsed since it was sent."
3693   (interactive (list t))
3694   (article-date-ut 'lapsed highlight))
3695
3696 (defun article-update-date-lapsed ()
3697   "Function to be run from a timer to update the lapsed time line."
3698   (save-match-data
3699     (let (deactivate-mark)
3700       (save-excursion
3701         (ignore-errors
3702          (walk-windows
3703           (lambda (w)
3704             (set-buffer (window-buffer w))
3705             (when (eq major-mode 'gnus-article-mode)
3706               (let ((mark (point-marker)))
3707                 (goto-char (point-min))
3708                 (when (re-search-forward "^X-Sent:" nil t)
3709                   (article-date-lapsed t))
3710                 (goto-char (marker-position mark))
3711                 (move-marker mark nil))))
3712           nil 'visible))))))
3713
3714 (defun gnus-start-date-timer (&optional n)
3715   "Start a timer to update the X-Sent header in the article buffers.
3716 The numerical prefix says how frequently (in seconds) the function
3717 is to run."
3718   (interactive "p")
3719   (unless n
3720     (setq n 1))
3721   (gnus-stop-date-timer)
3722   (setq article-lapsed-timer
3723         (run-at-time 1 n 'article-update-date-lapsed)))
3724
3725 (defun gnus-stop-date-timer ()
3726   "Stop the X-Sent timer."
3727   (interactive)
3728   (when article-lapsed-timer
3729     (nnheader-cancel-timer article-lapsed-timer)
3730     (setq article-lapsed-timer nil)))
3731
3732 (defun article-date-user (&optional highlight)
3733   "Convert the current article date to the user-defined format.
3734 This format is defined by the `gnus-article-time-format' variable."
3735   (interactive (list t))
3736   (article-date-ut 'user highlight))
3737
3738 (defun article-date-iso8601 (&optional highlight)
3739   "Convert the current article date to ISO8601."
3740   (interactive (list t))
3741   (article-date-ut 'iso8601 highlight))
3742
3743 (defmacro gnus-article-save-original-date (&rest forms)
3744   "Save the original date as a text property and evaluate FORMS."
3745   `(let* ((case-fold-search t)
3746           (start (progn
3747                    (goto-char (point-min))
3748                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3749                               (not (bolp)))
3750                      (match-end 0))))
3751           (date (when (and start
3752                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3753                                               nil t))
3754                   (buffer-substring-no-properties start
3755                                                   (match-beginning 0)))))
3756      (goto-char (point-max))
3757      (skip-chars-backward "\n")
3758      (put-text-property (point-min) (point) 'original-date date)
3759      ,@forms
3760      (goto-char (point-max))
3761      (skip-chars-backward "\n")
3762      (put-text-property (point-min) (point) 'original-date date)))
3763
3764 ;; (defun article-show-all ()
3765 ;;   "Show all hidden text in the article buffer."
3766 ;;   (interactive)
3767 ;;   (save-excursion
3768 ;;     (let ((inhibit-read-only t))
3769 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3770
3771 (defun article-remove-leading-whitespace ()
3772   "Remove excessive whitespace from all headers."
3773   (interactive)
3774   (save-excursion
3775     (save-restriction
3776       (let ((inhibit-read-only t))
3777         (article-narrow-to-head)
3778         (goto-char (point-min))
3779         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3780           (delete-region (match-beginning 1) (match-end 1)))))))
3781
3782 (defun article-emphasize (&optional arg)
3783   "Emphasize text according to `gnus-emphasis-alist'."
3784   (interactive (gnus-article-hidden-arg))
3785   (unless (gnus-article-check-hidden-text 'emphasis arg)
3786     (save-excursion
3787       (let ((alist (or
3788                     (condition-case nil
3789                         (with-current-buffer gnus-summary-buffer
3790                           gnus-article-emphasis-alist)
3791                       (error))
3792                     gnus-emphasis-alist))
3793             (inhibit-read-only t)
3794             (props (append '(article-type emphasis)
3795                            gnus-hidden-properties))
3796             regexp elem beg invisible visible face)
3797         (article-goto-body)
3798         (setq beg (point))
3799         (while (setq elem (pop alist))
3800           (goto-char beg)
3801           (setq regexp (car elem)
3802                 invisible (nth 1 elem)
3803                 visible (nth 2 elem)
3804                 face (nth 3 elem))
3805           (while (re-search-forward regexp nil t)
3806             (when (and (match-beginning visible) (match-beginning invisible))
3807               (gnus-article-hide-text
3808                (match-beginning invisible) (match-end invisible) props)
3809               (gnus-article-unhide-text-type
3810                (match-beginning visible) (match-end visible) 'emphasis)
3811               (gnus-put-overlay-excluding-newlines
3812                (match-beginning visible) (match-end visible) 'face face)
3813               (gnus-add-wash-type 'emphasis)
3814               (goto-char (match-end invisible)))))))))
3815
3816 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3817   "Setup newsgroup emphasis alist."
3818   (unless gnus-article-emphasis-alist
3819     (let ((name (and gnus-newsgroup-name
3820                      (gnus-group-real-name gnus-newsgroup-name))))
3821       (make-local-variable 'gnus-article-emphasis-alist)
3822       (setq gnus-article-emphasis-alist
3823             (nconc
3824              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3825                (while (setq elem (pop alist))
3826                  (when (and name (string-match (car elem) name))
3827                    (setq alist nil
3828                          highlight (copy-sequence (cdr elem)))))
3829                highlight)
3830              (copy-sequence highlight-words)
3831              (if gnus-newsgroup-name
3832                  (copy-sequence (gnus-group-find-parameter
3833                                  gnus-newsgroup-name 'highlight-words t)))
3834              gnus-emphasis-alist)))))
3835
3836 (defvar gnus-summary-article-menu)
3837 (defvar gnus-summary-post-menu)
3838
3839 ;;; Saving functions.
3840
3841 (defun gnus-article-save (save-buffer file &optional num)
3842   "Save the currently selected article."
3843   (when (or (get gnus-default-article-saver :headers)
3844             (not gnus-save-all-headers))
3845     ;; Remove headers according to `gnus-saved-headers' or the value
3846     ;; of the `:headers' property that the saver function might have.
3847     (let ((gnus-visible-headers
3848            (or (symbol-value (get gnus-default-article-saver :headers))
3849                gnus-saved-headers gnus-visible-headers))
3850           ;; Ignore group parameter.  See `article-hide-headers'.
3851           (gnus-summary-buffer nil))
3852       (with-current-buffer save-buffer
3853         (article-hide-headers 1 t))))
3854   (save-window-excursion
3855     (if (not gnus-default-article-saver)
3856         (error "No default saver is defined")
3857       ;; !!! Magic!  The saving functions all save
3858       ;; `gnus-save-article-buffer' (or so they think), but we
3859       ;; bind that variable to our save-buffer.
3860       (set-buffer gnus-article-buffer)
3861       (let* ((gnus-save-article-buffer save-buffer)
3862              (filename
3863               (cond
3864                ((not gnus-prompt-before-saving) 'default)
3865                ((eq gnus-prompt-before-saving 'always) nil)
3866                (t file)))
3867              (gnus-number-of-articles-to-be-saved
3868               (when (eq gnus-prompt-before-saving t)
3869                 num)))                  ; Magic
3870         (set-buffer gnus-article-current-summary)
3871         (funcall gnus-default-article-saver filename)))))
3872
3873 (defun gnus-read-save-file-name (prompt &optional filename
3874                                         function group headers variable
3875                                         dir-var)
3876   (let ((default-name
3877           (funcall function group headers (symbol-value variable)))
3878         result)
3879     (setq result
3880           (expand-file-name
3881            (cond
3882             ((eq filename 'default)
3883              default-name)
3884             ((eq filename t)
3885              default-name)
3886             (filename filename)
3887             (t
3888              (when (symbol-value dir-var)
3889                (setq default-name (expand-file-name
3890                                    (file-name-nondirectory default-name)
3891                                    (symbol-value dir-var))))
3892              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3893                     (prompt
3894                      (format prompt
3895                              (if (and gnus-number-of-articles-to-be-saved
3896                                       (> gnus-number-of-articles-to-be-saved 1))
3897                                  (format "these %d articles"
3898                                          gnus-number-of-articles-to-be-saved)
3899                                "this article")))
3900                     (file
3901                      ;; Let the split methods have their say.
3902                      (cond
3903                       ;; No split name was found.
3904                       ((null split-name)
3905                        (read-file-name
3906                         (concat prompt " (default "
3907                                 (file-name-nondirectory default-name) "): ")
3908                         (file-name-directory default-name)
3909                         default-name))
3910                       ;; A single group name is returned.
3911                       ((stringp split-name)
3912                        (setq default-name
3913                              (funcall function split-name headers
3914                                       (symbol-value variable)))
3915                        (read-file-name
3916                         (concat prompt " (default "
3917                                 (file-name-nondirectory default-name) "): ")
3918                         (file-name-directory default-name)
3919                         default-name))
3920                       ;; A single split name was found
3921                       ((= 1 (length split-name))
3922                        (let* ((name (expand-file-name
3923                                      (car split-name)
3924                                      gnus-article-save-directory))
3925                               (dir (cond ((file-directory-p name)
3926                                           (file-name-as-directory name))
3927                                          ((file-exists-p name) name)
3928                                          (t gnus-article-save-directory))))
3929                          (read-file-name
3930                           (concat prompt " (default " name "): ")
3931                           dir name)))
3932                       ;; A list of splits was found.
3933                       (t
3934                        (setq split-name (nreverse split-name))
3935                        (let (result)
3936                          (let ((file-name-history
3937                                 (nconc split-name file-name-history)))
3938                            (setq result
3939                                  (expand-file-name
3940                                   (read-file-name
3941                                    (concat prompt " (`M-p' for defaults): ")
3942                                    gnus-article-save-directory
3943                                    (car split-name))
3944                                   gnus-article-save-directory)))
3945                          (car (push result file-name-history)))))))
3946                ;; Create the directory.
3947                (gnus-make-directory (file-name-directory file))
3948                ;; If we have read a directory, we append the default file name.
3949                (when (file-directory-p file)
3950                  (setq file (expand-file-name (file-name-nondirectory
3951                                                default-name)
3952                                               (file-name-as-directory file))))
3953                ;; Possibly translate some characters.
3954                (nnheader-translate-file-chars file))))))
3955     (gnus-make-directory (file-name-directory result))
3956     (when variable
3957       (set variable result))
3958     (when dir-var
3959       (set dir-var (file-name-directory result)))
3960     result))
3961
3962 (defun gnus-article-archive-name (group)
3963   "Return the first instance of an \"Archive-name\" in the current buffer."
3964   (let ((case-fold-search t))
3965     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3966       (nnheader-concat gnus-article-save-directory
3967                        (match-string 1)))))
3968
3969 (defun gnus-article-nndoc-name (group)
3970   "If GROUP is an nndoc group, return the name of the parent group."
3971   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3972     (gnus-group-get-parameter group 'save-article-group)))
3973
3974 (defun gnus-summary-save-in-rmail (&optional filename)
3975   "Append this article to Rmail file.
3976 Optional argument FILENAME specifies file name.
3977 Directory to save to is default to `gnus-article-save-directory'."
3978   (setq filename (gnus-read-save-file-name
3979                   "Save %s in rmail file" filename
3980                   gnus-rmail-save-name gnus-newsgroup-name
3981                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3982   (gnus-eval-in-buffer-window gnus-save-article-buffer
3983     (save-excursion
3984       (save-restriction
3985         (widen)
3986         ;; Note that unlike gnus-summary-save-in-mail, there is no
3987         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3988         ;; not use Babyl.
3989         (gnus-output-to-rmail filename))))
3990   filename)
3991
3992 (defun gnus-summary-save-in-mail (&optional filename)
3993   "Append this article to Unix mail file.
3994 Optional argument FILENAME specifies file name.
3995 Directory to save to is default to `gnus-article-save-directory'."
3996   (setq filename (gnus-read-save-file-name
3997                   "Save %s in Unix mail file" filename
3998                   gnus-mail-save-name gnus-newsgroup-name
3999                   gnus-current-headers 'gnus-newsgroup-last-mail))
4000   (gnus-eval-in-buffer-window gnus-save-article-buffer
4001     (save-excursion
4002       (save-restriction
4003         (widen)
4004         (if (and (file-readable-p filename)
4005                  (file-regular-p filename)
4006                  (mail-file-babyl-p filename))
4007             (gnus-output-to-rmail filename)
4008           (gnus-output-to-mail filename)))))
4009   filename)
4010
4011 (put 'gnus-summary-save-in-file :decode t)
4012 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4013 (defun gnus-summary-save-in-file (&optional filename overwrite)
4014   "Append this article to file.
4015 Optional argument FILENAME specifies file name.
4016 Directory to save to is default to `gnus-article-save-directory'."
4017   (setq filename (gnus-read-save-file-name
4018                   "Save %s in file" filename
4019                   gnus-file-save-name gnus-newsgroup-name
4020                   gnus-current-headers 'gnus-newsgroup-last-file))
4021   (gnus-eval-in-buffer-window gnus-save-article-buffer
4022     (save-excursion
4023       (save-restriction
4024         (widen)
4025         (when (and overwrite
4026                    (file-exists-p filename))
4027           (delete-file filename))
4028         (gnus-output-to-file filename))))
4029   filename)
4030
4031 (put 'gnus-summary-write-to-file :decode t)
4032 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4033 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4034 (defun gnus-summary-write-to-file (&optional filename)
4035   "Write this article to a file, overwriting it if the file exists.
4036 Optional argument FILENAME specifies file name.
4037 The directory to save in defaults to `gnus-article-save-directory'."
4038   (setq filename (gnus-read-save-file-name
4039                   "Save %s in file" filename
4040                   gnus-file-save-name gnus-newsgroup-name
4041                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4042   (gnus-summary-save-in-file filename t))
4043
4044 (put 'gnus-summary-save-body-in-file :decode t)
4045 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4046   "Append this article body to a file.
4047 Optional argument FILENAME specifies file name.
4048 The directory to save in defaults to `gnus-article-save-directory'."
4049   (setq filename (gnus-read-save-file-name
4050                   "Save %s body in file" filename
4051                   gnus-file-save-name gnus-newsgroup-name
4052                   gnus-current-headers 'gnus-newsgroup-last-file))
4053   (gnus-eval-in-buffer-window gnus-save-article-buffer
4054     (save-excursion
4055       (save-restriction
4056         (widen)
4057         (when (article-goto-body)
4058           (narrow-to-region (point) (point-max)))
4059         (when (and overwrite
4060                    (file-exists-p filename))
4061           (delete-file filename))
4062         (gnus-output-to-file filename))))
4063   filename)
4064
4065 (put 'gnus-summary-write-body-to-file :decode t)
4066 (put 'gnus-summary-write-body-to-file
4067      :function 'gnus-summary-save-body-in-file)
4068 (defun gnus-summary-write-body-to-file (&optional filename)
4069   "Write this article body to a file, overwriting it if the file exists.
4070 Optional argument FILENAME specifies file name.
4071 The directory to save in defaults to `gnus-article-save-directory'."
4072   (setq filename (gnus-read-save-file-name
4073                   "Save %s body in file" filename
4074                   gnus-file-save-name gnus-newsgroup-name
4075                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4076   (gnus-summary-save-body-in-file filename t))
4077
4078 (put 'gnus-summary-save-in-pipe :decode t)
4079 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4080 (defun gnus-summary-save-in-pipe (&optional command raw)
4081   "Pipe this article to subprocess COMMAND.
4082 Valid values for COMMAND include:
4083   a string
4084     The executable command name and possibly arguments.
4085   nil
4086     You will be prompted for the command in the minibuffer.
4087   the symbol `default'
4088     It will be replaced with the command which the variable
4089     `gnus-summary-pipe-output-default-command' holds or the command
4090     last used for saving.
4091 Non-nil value for RAW overrides `:decode' and `:headers' properties
4092 and the raw article including all headers will be piped."
4093   (let ((article (gnus-summary-article-number))
4094         (decode (unless raw
4095                   (get 'gnus-summary-save-in-pipe :decode)))
4096         save-buffer default)
4097     (if article
4098         (if (vectorp (gnus-summary-article-header article))
4099             (save-current-buffer
4100               (gnus-summary-select-article decode decode nil article)
4101               (insert-buffer-substring
4102                (prog1
4103                    (if decode
4104                        gnus-article-buffer
4105                      gnus-original-article-buffer)
4106                  (setq save-buffer
4107                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4108               ;; Remove unwanted headers.
4109               (when (and (not raw)
4110                          (or (get 'gnus-summary-save-in-pipe :headers)
4111                              (not gnus-save-all-headers)))
4112                 (let ((gnus-visible-headers
4113                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4114                                               :headers))
4115                            gnus-saved-headers gnus-visible-headers))
4116                       (gnus-summary-buffer nil))
4117                   (article-hide-headers 1 t))))
4118           (error "%d is not a real article" article))
4119       (error "No article to pipe"))
4120     (setq default (or gnus-summary-pipe-output-default-command
4121                       gnus-last-shell-command))
4122     (unless (stringp command)
4123       (setq command
4124             (if (and (eq command 'default) default)
4125                 default
4126               (gnus-read-shell-command "Shell command on this article: "
4127                                        default))))
4128     (when (string-equal command "")
4129       (if default
4130           (setq command default)
4131         (error "A command is required")))
4132     (gnus-eval-in-buffer-window save-buffer
4133       (save-restriction
4134         (widen)
4135         (shell-command-on-region (point-min) (point-max) command nil)))
4136     (gnus-kill-buffer save-buffer))
4137   (setq gnus-summary-pipe-output-default-command command))
4138
4139 (defun gnus-summary-pipe-to-muttprint (&optional command)
4140   "Pipe this article to muttprint."
4141   (unless (stringp command)
4142     (setq command (read-string
4143                    "Print using command: " gnus-summary-muttprint-program
4144                    nil gnus-summary-muttprint-program)))
4145   (let ((gnus-summary-pipe-output-default-command
4146          gnus-summary-pipe-output-default-command))
4147     (gnus-summary-save-in-pipe command))
4148   (setq gnus-summary-muttprint-program command))
4149
4150 ;;; Article file names when saving.
4151
4152 (defun gnus-capitalize-newsgroup (newsgroup)
4153   "Capitalize NEWSGROUP name."
4154   (when (not (zerop (length newsgroup)))
4155     (concat (char-to-string (upcase (aref newsgroup 0)))
4156             (substring newsgroup 1))))
4157
4158 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4159   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4160 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4161 Otherwise, it is like ~/News/news/group/num."
4162   (let ((default
4163           (expand-file-name
4164            (concat (if (gnus-use-long-file-name 'not-save)
4165                        (gnus-capitalize-newsgroup newsgroup)
4166                      (gnus-newsgroup-directory-form newsgroup))
4167                    "/" (int-to-string (mail-header-number headers)))
4168            gnus-article-save-directory)))
4169     (if (and last-file
4170              (string-equal (file-name-directory default)
4171                            (file-name-directory last-file))
4172              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4173         default
4174       (or last-file default))))
4175
4176 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4177   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4178 If variable `gnus-use-long-file-name' is non-nil, it is
4179 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4180   (let ((default
4181           (expand-file-name
4182            (concat (if (gnus-use-long-file-name 'not-save)
4183                        newsgroup
4184                      (gnus-newsgroup-directory-form newsgroup))
4185                    "/" (int-to-string (mail-header-number headers)))
4186            gnus-article-save-directory)))
4187     (if (and last-file
4188              (string-equal (file-name-directory default)
4189                            (file-name-directory last-file))
4190              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4191         default
4192       (or last-file default))))
4193
4194 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4195   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4196 If variable `gnus-use-long-file-name' is non-nil, it is
4197 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4198   (or last-file
4199       (expand-file-name
4200        (if (gnus-use-long-file-name 'not-save)
4201            newsgroup
4202          (file-relative-name
4203           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4204           default-directory))
4205        gnus-article-save-directory)))
4206
4207 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4208   "Generate file name from sender."
4209   (let ((from (mail-header-from headers)))
4210     (expand-file-name
4211      (if (and from (string-match "\\([^ <]+\\)@" from))
4212          (match-string 1 from)
4213        "nobody")
4214      gnus-article-save-directory)))
4215
4216 (defun article-verify-x-pgp-sig ()
4217   "Verify X-PGP-Sig."
4218   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4219   (interactive)
4220   (if (gnus-buffer-live-p gnus-original-article-buffer)
4221       (let ((sig (with-current-buffer gnus-original-article-buffer
4222                    (gnus-fetch-field "X-PGP-Sig")))
4223             items info headers)
4224         (when (and sig
4225                    mml2015-use
4226                    (mml2015-clear-verify-function))
4227           (with-temp-buffer
4228             (insert-buffer-substring gnus-original-article-buffer)
4229             (setq items (split-string sig))
4230             (message-narrow-to-head)
4231             (let ((inhibit-point-motion-hooks t)
4232                   (case-fold-search t))
4233               ;; Don't verify multiple headers.
4234               (setq headers (mapconcat (lambda (header)
4235                                          (concat header ": "
4236                                                  (mail-fetch-field header)
4237                                                  "\n"))
4238                                        (split-string (nth 1 items) ",") "")))
4239             (delete-region (point-min) (point-max))
4240             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4241             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4242             (insert headers)
4243             (widen)
4244             (forward-line)
4245             (while (not (eobp))
4246               (if (looking-at "^-")
4247                   (insert "- "))
4248               (forward-line))
4249             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4250             (insert "Version: " (car items) "\n\n")
4251             (insert (mapconcat 'identity (cddr items) "\n"))
4252             (insert "\n-----END PGP SIGNATURE-----\n")
4253             (let ((mm-security-handle (list (format "multipart/signed"))))
4254               (mml2015-clean-buffer)
4255               (let ((coding-system-for-write (or gnus-newsgroup-charset
4256                                                  'iso-8859-1)))
4257                 (funcall (mml2015-clear-verify-function)))
4258               (setq info
4259                     (or (mm-handle-multipart-ctl-parameter
4260                          mm-security-handle 'gnus-details)
4261                         (mm-handle-multipart-ctl-parameter
4262                          mm-security-handle 'gnus-info)))))
4263           (when info
4264             (let ((inhibit-read-only t) bface eface)
4265               (save-restriction
4266                 (message-narrow-to-head)
4267                 (goto-char (point-max))
4268                 (forward-line -1)
4269                 (setq bface (get-text-property (point-at-bol) 'face)
4270                       eface (get-text-property (1- (point-at-eol)) 'face))
4271                 (message-remove-header "X-Gnus-PGP-Verify")
4272                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4273                     (forward-line)
4274                   (goto-char (point-max)))
4275                 (narrow-to-region (point) (point))
4276                 (insert "X-Gnus-PGP-Verify: " info "\n")
4277                 (goto-char (point-min))
4278                 (forward-line)
4279                 (while (not (eobp))
4280                   (if (not (looking-at "^[ \t]"))
4281                       (insert " "))
4282                   (forward-line))
4283                 ;; Do highlighting.
4284                 (goto-char (point-min))
4285                 (when (looking-at "\\([^:]+\\): *")
4286                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4287                                      'face bface)
4288                   (put-text-property (match-end 0) (point-max)
4289                                      'face eface)))))))))
4290
4291 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4292
4293 (defun article-verify-cancel-lock ()
4294   "Verify Cancel-Lock header."
4295   (interactive)
4296   (if (gnus-buffer-live-p gnus-original-article-buffer)
4297       (canlock-verify gnus-original-article-buffer)))
4298
4299 (eval-and-compile
4300   (mapc
4301    (lambda (func)
4302      (let (afunc gfunc)
4303        (if (consp func)
4304            (setq afunc (car func)
4305                  gfunc (cdr func))
4306          (setq afunc func
4307                gfunc (intern (format "gnus-%s" func))))
4308        (defalias gfunc
4309          (when (fboundp afunc)
4310            `(lambda (&optional interactive &rest args)
4311               ,(documentation afunc t)
4312               (interactive (list t))
4313               (with-current-buffer gnus-article-buffer
4314                 (if interactive
4315                     (call-interactively ',afunc)
4316                   (apply ',afunc args))))))))
4317    '(article-hide-headers
4318      article-verify-x-pgp-sig
4319      article-verify-cancel-lock
4320      article-hide-boring-headers
4321      article-treat-overstrike
4322      article-treat-ansi-sequences
4323      article-fill-long-lines
4324      article-capitalize-sentences
4325      article-remove-cr
4326      article-remove-leading-whitespace
4327      article-display-x-face
4328      article-display-face
4329      article-de-quoted-unreadable
4330      article-de-base64-unreadable
4331      article-decode-HZ
4332      article-wash-html
4333      article-unsplit-urls
4334      article-hide-list-identifiers
4335      article-strip-banner
4336      article-babel
4337      article-hide-pem
4338      article-hide-signature
4339      article-strip-headers-in-body
4340      article-remove-trailing-blank-lines
4341      article-strip-leading-blank-lines
4342      article-strip-multiple-blank-lines
4343      article-strip-leading-space
4344      article-strip-trailing-space
4345      article-strip-blank-lines
4346      article-strip-all-blank-lines
4347      article-date-local
4348      article-date-english
4349      article-date-iso8601
4350      article-date-original
4351      article-date-ut
4352      article-decode-mime-words
4353      article-decode-charset
4354      article-decode-encoded-words
4355      article-date-user
4356      article-date-lapsed
4357      article-emphasize
4358      article-treat-dumbquotes
4359      article-normalize-headers
4360      ;;(article-show-all . gnus-article-show-all-headers)
4361      )))
4362 \f
4363 ;;;
4364 ;;; Gnus article mode
4365 ;;;
4366
4367 (put 'gnus-article-mode 'mode-class 'special)
4368
4369 (set-keymap-parent gnus-article-mode-map widget-keymap)
4370
4371 (gnus-define-keys gnus-article-mode-map
4372   " " gnus-article-goto-next-page
4373   "\177" gnus-article-goto-prev-page
4374   [delete] gnus-article-goto-prev-page
4375   [backspace] gnus-article-goto-prev-page
4376   "\C-c^" gnus-article-refer-article
4377   "h" gnus-article-show-summary
4378   "s" gnus-article-show-summary
4379   "\C-c\C-m" gnus-article-mail
4380   "?" gnus-article-describe-briefly
4381   "<" beginning-of-buffer
4382   ">" end-of-buffer
4383   "\C-c\C-i" gnus-info-find-node
4384   "\C-c\C-b" gnus-bug
4385   "R" gnus-article-reply-with-original
4386   "F" gnus-article-followup-with-original
4387   "\C-hk" gnus-article-describe-key
4388   "\C-hc" gnus-article-describe-key-briefly
4389   "\C-hb" gnus-article-describe-bindings
4390
4391   "e" gnus-article-read-summary-keys
4392   "\C-d" gnus-article-read-summary-keys
4393   "\M-*" gnus-article-read-summary-keys
4394   "\M-#" gnus-article-read-summary-keys
4395   "\M-^" gnus-article-read-summary-keys
4396   "\M-g" gnus-article-read-summary-keys)
4397
4398 (substitute-key-definition
4399  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4400
4401 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4402   "W" gnus-article-wide-reply-with-original)
4403 (if (featurep 'xemacs)
4404     (set-keymap-default-binding gnus-article-send-map
4405                                 'gnus-article-read-summary-send-keys)
4406   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4407
4408 (defun gnus-article-make-menu-bar ()
4409   (unless (boundp 'gnus-article-commands-menu)
4410     (gnus-summary-make-menu-bar))
4411   (gnus-turn-off-edit-menu 'article)
4412   (unless (boundp 'gnus-article-article-menu)
4413     (easy-menu-define
4414      gnus-article-article-menu gnus-article-mode-map ""
4415      '("Article"
4416        ["Scroll forwards" gnus-article-goto-next-page t]
4417        ["Scroll backwards" gnus-article-goto-prev-page t]
4418        ["Show summary" gnus-article-show-summary t]
4419        ["Fetch Message-ID at point" gnus-article-refer-article t]
4420        ["Mail to address at point" gnus-article-mail t]
4421        ["Send a bug report" gnus-bug t]))
4422
4423     (easy-menu-define
4424      gnus-article-treatment-menu gnus-article-mode-map ""
4425      ;; Fixme: this should use :active (and maybe :visible).
4426      '("Treatment"
4427        ["Hide headers" gnus-article-hide-headers t]
4428        ["Hide signature" gnus-article-hide-signature t]
4429        ["Hide citation" gnus-article-hide-citation t]
4430        ["Treat overstrike" gnus-article-treat-overstrike t]
4431        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4432        ["Remove carriage return" gnus-article-remove-cr t]
4433        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4434        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4435        ["Remove base64" gnus-article-de-base64-unreadable t]
4436        ["Treat html" gnus-article-wash-html t]
4437        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4438        ["Decode HZ" gnus-article-decode-HZ t]))
4439
4440     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4441
4442     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4443
4444     (gnus-run-hooks 'gnus-article-menu-hook)))
4445
4446 (defvar bookmark-make-record-function)
4447
4448 (defun gnus-article-mode ()
4449   "Major mode for displaying an article.
4450
4451 All normal editing commands are switched off.
4452
4453 The following commands are available in addition to all summary mode
4454 commands:
4455 \\<gnus-article-mode-map>
4456 \\[gnus-article-next-page]\t Scroll the article one page forwards
4457 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4458 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4459 \\[gnus-article-show-summary]\t Display the summary buffer
4460 \\[gnus-article-mail]\t Send a reply to the address near point
4461 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4462 \\[gnus-info-find-node]\t Go to the Gnus info node"
4463   (interactive)
4464   (kill-all-local-variables)
4465   (gnus-simplify-mode-line)
4466   (setq mode-name "Article")
4467   (setq major-mode 'gnus-article-mode)
4468   (make-local-variable 'minor-mode-alist)
4469   (use-local-map gnus-article-mode-map)
4470   (when (gnus-visual-p 'article-menu 'menu)
4471     (gnus-article-make-menu-bar)
4472     (when gnus-summary-tool-bar-map
4473       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4474   (gnus-update-format-specifications nil 'article-mode)
4475   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4476   (set (make-local-variable 'gnus-page-broken) nil)
4477   (make-local-variable 'gnus-button-marker-list)
4478   (make-local-variable 'gnus-article-current-summary)
4479   (make-local-variable 'gnus-article-mime-handles)
4480   (make-local-variable 'gnus-article-decoded-p)
4481   (make-local-variable 'gnus-article-mime-handle-alist)
4482   (make-local-variable 'gnus-article-wash-types)
4483   (make-local-variable 'gnus-article-image-alist)
4484   (make-local-variable 'gnus-article-charset)
4485   (make-local-variable 'gnus-article-ignored-charsets)
4486   (set (make-local-variable 'bookmark-make-record-function)
4487        'gnus-summary-bookmark-make-record)
4488   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4489   ;; face.
4490   (set (make-local-variable 'nobreak-char-display) nil)
4491   (setq cursor-in-non-selected-windows nil)
4492   (setq truncate-lines gnus-article-truncate-lines)
4493   (gnus-set-default-directory)
4494   (buffer-disable-undo)
4495   (setq buffer-read-only t
4496         show-trailing-whitespace nil)
4497   (set-syntax-table gnus-article-mode-syntax-table)
4498   (mm-enable-multibyte)
4499   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4500
4501 (defvar gnus-button-marker-list nil
4502   "Regexp matching any of the regexps from `gnus-button-alist'.
4503 Internal variable.")
4504
4505 (defun gnus-article-setup-buffer ()
4506   "Initialize the article buffer."
4507   (let* ((name (if gnus-single-article-buffer "*Article*"
4508                  (concat "*Article " gnus-newsgroup-name "*")))
4509          (original
4510           (progn (string-match "\\*Article" name)
4511                  (concat " *Original Article"
4512                          (substring name (match-end 0))))))
4513     (setq gnus-article-buffer name)
4514     (setq gnus-original-article-buffer original)
4515     (setq gnus-article-mime-handle-alist nil)
4516     (with-current-buffer gnus-summary-buffer
4517       ;; This might be a variable local to the summary buffer.
4518       (unless gnus-single-article-buffer
4519         (setq gnus-article-buffer name)
4520         (setq gnus-original-article-buffer original)
4521         (gnus-set-global-variables)))
4522     (gnus-article-setup-highlight-words)
4523     ;; Init original article buffer.
4524     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4525       (mm-enable-multibyte)
4526       (setq major-mode 'gnus-original-article-mode)
4527       (make-local-variable 'gnus-original-article))
4528     (if (and (get-buffer name)
4529              (with-current-buffer name
4530                (if gnus-article-edit-mode
4531                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4532                        (progn
4533                          (set-buffer-modified-p nil)
4534                          (gnus-kill-buffer name)
4535                          (message "")
4536                          nil)
4537                      (error "Action aborted"))
4538                  t)))
4539         (with-current-buffer name
4540           (set (make-local-variable 'gnus-article-edit-mode) nil)
4541           (when gnus-article-mime-handles
4542             (mm-destroy-parts gnus-article-mime-handles)
4543             (setq gnus-article-mime-handles nil))
4544           ;; Set it to nil in article-buffer!
4545           (setq gnus-article-mime-handle-alist nil)
4546           (buffer-disable-undo)
4547           (setq buffer-read-only t)
4548           ;; This list just keeps growing if we don't reset it.
4549           (setq gnus-button-marker-list nil)
4550           (unless (eq major-mode 'gnus-article-mode)
4551             (gnus-article-mode))
4552           (current-buffer))
4553       (with-current-buffer (gnus-get-buffer-create name)
4554         (gnus-article-mode)
4555         (make-local-variable 'gnus-summary-buffer)
4556         (setq gnus-summary-buffer
4557               (gnus-summary-buffer-name gnus-newsgroup-name))
4558         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4559         (current-buffer)))))
4560
4561 ;; Set article window start at LINE, where LINE is the number of lines
4562 ;; from the head of the article.
4563 (defun gnus-article-set-window-start (&optional line)
4564   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4565     (when article-window
4566       (set-window-start
4567        article-window
4568        (with-current-buffer gnus-article-buffer
4569          (goto-char (point-min))
4570          (if (not line)
4571              (point-min)
4572            (gnus-message 6 "Moved to bookmark")
4573            (search-forward "\n\n" nil t)
4574            (forward-line line)
4575            (point)))))))
4576
4577 (defun gnus-article-prepare (article &optional all-headers header)
4578   "Prepare ARTICLE in article mode buffer.
4579 ARTICLE should either be an article number or a Message-ID.
4580 If ARTICLE is an id, HEADER should be the article headers.
4581 If ALL-HEADERS is non-nil, no headers are hidden."
4582   (save-excursion
4583     ;; Make sure we start in a summary buffer.
4584     (unless (eq major-mode 'gnus-summary-mode)
4585       (set-buffer gnus-summary-buffer))
4586     (setq gnus-summary-buffer (current-buffer))
4587     (let* ((gnus-article (if header (mail-header-number header) article))
4588            (summary-buffer (current-buffer))
4589            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4590            (group gnus-newsgroup-name)
4591            result)
4592       (save-excursion
4593         (gnus-article-setup-buffer)
4594         (set-buffer gnus-article-buffer)
4595         ;; Deactivate active regions.
4596         (when (and (boundp 'transient-mark-mode)
4597                    transient-mark-mode)
4598           (setq mark-active nil))
4599         (if (not (setq result (let ((inhibit-read-only t))
4600                                 (gnus-request-article-this-buffer
4601                                  article group))))
4602             ;; There is no such article.
4603             (save-excursion
4604               (when (and (numberp article)
4605                          (not (memq article gnus-newsgroup-sparse)))
4606                 (setq gnus-article-current
4607                       (cons gnus-newsgroup-name article))
4608                 (set-buffer gnus-summary-buffer)
4609                 (setq gnus-current-article article)
4610                 (if (and (memq article gnus-newsgroup-undownloaded)
4611                          (not (gnus-online (gnus-find-method-for-group
4612                                             gnus-newsgroup-name))))
4613                     (progn
4614                       (gnus-summary-set-agent-mark article)
4615                       (message "Message marked for downloading"))
4616                   (gnus-summary-mark-article article gnus-canceled-mark)
4617                   (unless (memq article gnus-newsgroup-sparse)
4618                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4619           (if (or (eq result 'pseudo)
4620                   (eq result 'nneething))
4621               (progn
4622                 (with-current-buffer summary-buffer
4623                   (push article gnus-newsgroup-history)
4624                   (setq gnus-last-article gnus-current-article
4625                         gnus-current-article 0
4626                         gnus-current-headers nil
4627                         gnus-article-current nil)
4628                   (if (eq result 'nneething)
4629                       (gnus-configure-windows 'summary)
4630                     (gnus-configure-windows 'article))
4631                   (gnus-set-global-variables))
4632                 (let ((gnus-article-mime-handle-alist-1
4633                        gnus-article-mime-handle-alist))
4634                   (gnus-set-mode-line 'article)))
4635             ;; The result from the `request' was an actual article -
4636             ;; or at least some text that is now displayed in the
4637             ;; article buffer.
4638             (when (and (numberp article)
4639                        (not (eq article gnus-current-article)))
4640               ;; Seems like a new article has been selected.
4641               ;; `gnus-current-article' must be an article number.
4642               (with-current-buffer summary-buffer
4643                 (push article gnus-newsgroup-history)
4644                 (setq gnus-last-article gnus-current-article
4645                       gnus-current-article article
4646                       gnus-current-headers
4647                       (gnus-summary-article-header gnus-current-article)
4648                       gnus-article-current
4649                       (cons gnus-newsgroup-name gnus-current-article))
4650                 (unless (vectorp gnus-current-headers)
4651                   (setq gnus-current-headers nil))
4652                 (gnus-summary-goto-subject gnus-current-article)
4653                 (when (gnus-summary-show-thread)
4654                   ;; If the summary buffer really was folded, the
4655                   ;; previous goto may not actually have gone to
4656                   ;; the right article, but the thread root instead.
4657                   ;; So we go again.
4658                   (gnus-summary-goto-subject gnus-current-article))
4659                 (gnus-run-hooks 'gnus-mark-article-hook)
4660                 (gnus-set-mode-line 'summary)
4661                 (when (gnus-visual-p 'article-highlight 'highlight)
4662                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4663                 ;; Set the global newsgroup variables here.
4664                 (gnus-set-global-variables)
4665                 (setq gnus-have-all-headers
4666                       (or all-headers gnus-show-all-headers))))
4667             (save-excursion
4668               (gnus-configure-windows 'article))
4669             (when (or (numberp article)
4670                       (stringp article))
4671               (gnus-article-prepare-display)
4672               ;; Do page break.
4673               (goto-char (point-min))
4674               (when gnus-break-pages
4675                 (gnus-narrow-to-page)))
4676             (let ((gnus-article-mime-handle-alist-1
4677                    gnus-article-mime-handle-alist))
4678               (gnus-set-mode-line 'article))
4679             (article-goto-body)
4680             (unless (bobp)
4681               (forward-line -1))
4682             (set-window-point (get-buffer-window (current-buffer)) (point))
4683             (gnus-configure-windows 'article)
4684             t))))))
4685
4686 ;;;###autoload
4687 (defun gnus-article-prepare-display ()
4688   "Make the current buffer look like a nice article."
4689   ;; Hooks for getting information from the article.
4690   ;; This hook must be called before being narrowed.
4691   (let ((gnus-article-buffer (current-buffer))
4692         buffer-read-only
4693         (inhibit-read-only t))
4694     (unless (eq major-mode 'gnus-article-mode)
4695       (gnus-article-mode))
4696     (setq buffer-read-only nil
4697           gnus-article-wash-types nil
4698           gnus-article-image-alist nil)
4699     (gnus-run-hooks 'gnus-tmp-internal-hook)
4700     (when gnus-display-mime-function
4701       (funcall gnus-display-mime-function))
4702     (gnus-run-hooks 'gnus-article-prepare-hook)))
4703
4704 ;;;
4705 ;;; Gnus Sticky Article Mode
4706 ;;;
4707
4708 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4709   "Mode for sticky articles."
4710   ;; Release bindings that won't work.
4711   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4712                              gnus-sticky-article-mode-map)
4713   (substitute-key-definition 'gnus-article-refer-article 'undefined
4714                              gnus-sticky-article-mode-map)
4715   (dolist (k '("e" "h" "s" "F" "R"))
4716     (define-key gnus-sticky-article-mode-map k nil))
4717   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4718   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4719   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4720   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4721
4722 (defun gnus-sticky-article (arg)
4723   "Make the current article sticky.
4724 If a prefix ARG is given, ask for a name for this sticky article buffer."
4725   (interactive "P")
4726   (gnus-summary-show-thread)
4727   (gnus-summary-select-article nil nil 'pseudo)
4728   (let (new-art-buf-name)
4729     (gnus-eval-in-buffer-window gnus-article-buffer
4730       (setq new-art-buf-name
4731             (concat
4732              "*Sticky Article: "
4733              (if arg
4734                  (read-from-minibuffer "Sticky article buffer name: ")
4735                (gnus-with-article-headers
4736                  (gnus-article-goto-header "subject")
4737                  (setq new-art-buf-name
4738                        (buffer-substring-no-properties
4739                         (line-beginning-position) (line-end-position)))
4740                  (goto-char (point-min))
4741                  (gnus-article-goto-header "from")
4742                  (setq new-art-buf-name
4743                        (concat
4744                         new-art-buf-name ", "
4745                         (buffer-substring-no-properties
4746                          (line-beginning-position) (line-end-position))))
4747                  (goto-char (point-min))
4748                  (gnus-article-goto-header "date")
4749                  (setq new-art-buf-name
4750                        (concat
4751                         new-art-buf-name ", "
4752                         (buffer-substring-no-properties
4753                          (line-beginning-position) (line-end-position))))))
4754              "*"))
4755       (if (and (gnus-buffer-live-p new-art-buf-name)
4756                (with-current-buffer new-art-buf-name
4757                  (eq major-mode 'gnus-sticky-article-mode)))
4758           (switch-to-buffer new-art-buf-name)
4759         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4760       (gnus-sticky-article-mode))
4761     (setq gnus-article-buffer new-art-buf-name))
4762   (gnus-summary-recenter)
4763   (gnus-summary-position-point))
4764
4765 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4766   "Kill the given sticky article BUFFER.
4767 If none is given, assume the current buffer and kill it if it has
4768 `gnus-sticky-article-mode'."
4769   (interactive)
4770   (unless buffer
4771     (setq buffer (current-buffer)))
4772   (with-current-buffer buffer
4773     (when (eq major-mode 'gnus-sticky-article-mode)
4774       (gnus-kill-buffer buffer))))
4775
4776 (defun gnus-kill-sticky-article-buffers (arg)
4777   "Kill all sticky article buffers.
4778 If a prefix ARG is given, ask for confirmation."
4779   (interactive "P")
4780   (dolist (buf (gnus-buffers))
4781     (with-current-buffer buf
4782       (when (eq major-mode 'gnus-sticky-article-mode)
4783         (if (not arg)
4784             (gnus-kill-buffer buf)
4785           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4786             (gnus-kill-buffer buf)))))))
4787
4788 ;;;
4789 ;;; Gnus MIME viewing functions
4790 ;;;
4791
4792 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4793   "Format of the MIME buttons.
4794
4795 Valid specifiers include:
4796 %t  The MIME type
4797 %T  MIME type, along with additional info
4798 %n  The `name' parameter
4799 %d  The description, if any
4800 %l  The length of the encoded part
4801 %p  The part identifier number
4802 %e  Dots if the part isn't displayed
4803
4804 General format specifiers can also be used.  See Info node
4805 `(gnus)Formatting Variables'.")
4806
4807 (defvar gnus-mime-button-line-format-alist
4808   '((?t gnus-tmp-type ?s)
4809     (?T gnus-tmp-type-long ?s)
4810     (?n gnus-tmp-name ?s)
4811     (?d gnus-tmp-description ?s)
4812     (?p gnus-tmp-id ?s)
4813     (?l gnus-tmp-length ?d)
4814     (?e gnus-tmp-dots ?s)))
4815
4816 (defvar gnus-mime-button-commands
4817   '((gnus-article-press-button "\r" "Toggle Display")
4818     (gnus-mime-view-part "v" "View Interactively...")
4819     (gnus-mime-view-part-as-type "t" "View As Type...")
4820     (gnus-mime-view-part-as-charset "C" "View As charset...")
4821     (gnus-mime-save-part "o" "Save...")
4822     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4823     (gnus-mime-replace-part "r" "Replace part")
4824     (gnus-mime-delete-part "d" "Delete part")
4825     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4826     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4827     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4828     (gnus-mime-view-part-externally "e" "View Externally")
4829     (gnus-mime-print-part "p" "Print")
4830     (gnus-mime-pipe-part "|" "Pipe To Command...")
4831     (gnus-mime-action-on-part "." "Take action on the part...")))
4832
4833 (defun gnus-article-mime-part-status ()
4834   (if gnus-article-mime-handle-alist-1
4835       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4836           " (1 part)"
4837         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4838     ""))
4839
4840 (defvar gnus-mime-button-map
4841   (let ((map (make-sparse-keymap)))
4842     (define-key map gnus-mouse-2 'gnus-article-push-button)
4843     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4844     (dolist (c gnus-mime-button-commands)
4845       (define-key map (cadr c) (car c)))
4846     map))
4847
4848 (easy-menu-define
4849   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4850   `("MIME Part"
4851     ,@(mapcar (lambda (c)
4852                 (vector (caddr c) (car c) :active t))
4853               gnus-mime-button-commands)))
4854
4855 (defvar gnus-url-button-commands
4856   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4857
4858 (defvar gnus-url-button-map
4859   (let ((map (make-sparse-keymap)))
4860     (dolist (c gnus-url-button-commands)
4861       (define-key map (cadr c) (car c)))
4862     map))
4863
4864 (easy-menu-define
4865   gnus-url-button-menu gnus-url-button-map "URL button menu."
4866   `("Url Button"
4867     ,@(mapcar (lambda (c)
4868                 (vector (caddr c) (car c) :active t))
4869               gnus-url-button-commands)))
4870
4871 (defmacro gnus-bind-safe-url-regexp (&rest body)
4872   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4873   `(let ((mm-w3m-safe-url-regexp
4874           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4875                                 (gnus-buffer-live-p
4876                                  gnus-article-current-summary))
4877                            (with-current-buffer gnus-article-current-summary
4878                              gnus-newsgroup-name)
4879                          gnus-newsgroup-name)))
4880             (if (cond ((not group)
4881                        ;; Maybe we're in a mml-preview buffer
4882                        ;; and no group is selected.
4883                        t)
4884                       ((stringp gnus-safe-html-newsgroups)
4885                        (string-match gnus-safe-html-newsgroups group))
4886                       ((consp gnus-safe-html-newsgroups)
4887                        (member group gnus-safe-html-newsgroups)))
4888                 nil
4889               mm-w3m-safe-url-regexp))))
4890      ,@body))
4891
4892 (defun gnus-mime-button-menu (event prefix)
4893  "Construct a context-sensitive menu of MIME commands."
4894  (interactive "e\nP")
4895  (save-window-excursion
4896    (let ((pos (event-start event)))
4897      (select-window (posn-window pos))
4898      (goto-char (posn-point pos))
4899      (gnus-article-check-buffer)
4900      (popup-menu gnus-mime-button-menu nil prefix))))
4901
4902 (defun gnus-mime-view-all-parts (&optional handles)
4903   "View all the MIME parts."
4904   (interactive)
4905   (with-current-buffer gnus-article-buffer
4906     (let ((handles (or handles gnus-article-mime-handles))
4907           (mail-parse-charset gnus-newsgroup-charset)
4908           (mail-parse-ignored-charsets
4909            (with-current-buffer gnus-summary-buffer
4910              gnus-newsgroup-ignored-charsets)))
4911       (when handles
4912         (mm-remove-parts handles)
4913         (goto-char (point-min))
4914         (or (search-forward "\n\n") (goto-char (point-max)))
4915         (let ((inhibit-read-only t))
4916           (delete-region (point) (point-max))
4917           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4918
4919 (defun gnus-article-jump-to-part (n)
4920   "Jump to MIME part N."
4921   (interactive "P")
4922   (pop-to-buffer gnus-article-buffer)
4923   ;; FIXME: why is it necessary?
4924   (sit-for 0)
4925   (let ((parts (length gnus-article-mime-handle-alist)))
4926     (or n (setq n (read-number (format "Jump to part (2..%s): " parts))))
4927     (unless (and (integerp n) (<= n parts) (>= n 1))
4928       (setq n
4929             (progn
4930               (gnus-message 7 "Invalid part `%s', using %s instead."
4931                             n parts)
4932               parts)))
4933     (gnus-message 9 "Jumping to part %s." n)
4934     (cond ((>= gnus-auto-select-part 1)
4935            (while (and (<= n parts)
4936                        (not (gnus-article-goto-part n)))
4937              (setq n (1+ n))))
4938           ((< gnus-auto-select-part 0)
4939            (while (and (>= n 1)
4940                        (not (gnus-article-goto-part n)))
4941              (setq n (1- n))))
4942           (t
4943            (gnus-article-goto-part n)))))
4944
4945 (defvar gnus-mime-buttonized-part-id nil
4946   "ID of a mime part that should be buttonized.
4947 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4948
4949 (eval-when-compile
4950   (defsubst gnus-article-edit-part (handles &optional current-id)
4951     "Edit an article in order to delete a mime part.
4952 This function is exclusively used by `gnus-mime-save-part-and-strip'
4953 and `gnus-mime-delete-part', and not provided at run-time normally."
4954     (gnus-article-edit-article
4955      `(lambda ()
4956         (buffer-disable-undo)
4957         (erase-buffer)
4958         (let ((mail-parse-charset (or gnus-article-charset
4959                                       ',gnus-newsgroup-charset))
4960               (mail-parse-ignored-charsets
4961                (or gnus-article-ignored-charsets
4962                    ',gnus-newsgroup-ignored-charsets))
4963               (mbl mml-buffer-list))
4964           (setq mml-buffer-list nil)
4965           (insert-buffer-substring gnus-original-article-buffer)
4966           (mime-to-mml ',handles)
4967           (setq gnus-article-mime-handles nil)
4968           (let ((mbl1 mml-buffer-list))
4969             (setq mml-buffer-list mbl)
4970             (set (make-local-variable 'mml-buffer-list) mbl1))
4971           (gnus-make-local-hook 'kill-buffer-hook)
4972           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4973      `(lambda (no-highlight)
4974         (let ((mail-parse-charset (or gnus-article-charset
4975                                       ',gnus-newsgroup-charset))
4976               (message-options message-options)
4977               (message-options-set-recipient)
4978               (mail-parse-ignored-charsets
4979                (or gnus-article-ignored-charsets
4980                    ',gnus-newsgroup-ignored-charsets)))
4981           (mml-to-mime)
4982           (mml-destroy-buffers)
4983           (remove-hook 'kill-buffer-hook
4984                        'mml-destroy-buffers t)
4985           (kill-local-variable 'mml-buffer-list))
4986         (gnus-summary-edit-article-done
4987          ,(or (mail-header-references gnus-current-headers) "")
4988          ,(gnus-group-read-only-p)
4989          ,gnus-summary-buffer no-highlight))
4990      t)
4991     ;; Force buttonizing this part.
4992     (let ((gnus-mime-buttonized-part-id current-id))
4993       (gnus-article-edit-done))
4994     (gnus-configure-windows 'article)
4995     (when (and current-id (integerp gnus-auto-select-part))
4996       (gnus-article-jump-to-part
4997        (min (max (+ current-id gnus-auto-select-part) 1)
4998             (with-current-buffer gnus-article-buffer
4999               (length gnus-article-mime-handle-alist)))))))
5000
5001 (defun gnus-mime-replace-part (file)
5002   "Replace MIME part under point with an external body."
5003   ;; Useful if file has already been saved to disk
5004   (interactive
5005    (list
5006     (read-file-name "Replace MIME part with file: "
5007                     (or mm-default-directory default-directory)
5008                     nil nil)))
5009   (gnus-mime-save-part-and-strip file))
5010
5011 (defun gnus-mime-save-part-and-strip (&optional file)
5012   "Save the MIME part under point then replace it with an external body.
5013 If FILE is given, use it for the external part."
5014   (interactive)
5015   (gnus-article-check-buffer)
5016   (when (gnus-group-read-only-p)
5017     (error "The current group does not support deleting of parts"))
5018   (when (mm-complicated-handles gnus-article-mime-handles)
5019     (error "\
5020 The current article has a complicated MIME structure, giving up..."))
5021   (let* ((data (get-text-property (point) 'gnus-data))
5022          (id (get-text-property (point) 'gnus-part))
5023          param
5024          (handles gnus-article-mime-handles))
5025     (unless file
5026       (setq file
5027             (and data (mm-save-part data "Delete MIME part and save to: "))))
5028     (when file
5029       (with-current-buffer (mm-handle-buffer data)
5030         (erase-buffer)
5031         (insert "Content-Type: " (mm-handle-media-type data))
5032         (mml-insert-parameter-string (cdr (mm-handle-type data))
5033                                      '(charset))
5034         ;; Add a filename for the sake of saving the part again.
5035         (mml-insert-parameter
5036          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5037         (insert "\n")
5038         (insert "Content-ID: " (message-make-message-id) "\n")
5039         (insert "Content-Transfer-Encoding: binary\n")
5040         (insert "\n"))
5041       (setcdr data
5042               (cdr (mm-make-handle nil
5043                                    `("message/external-body"
5044                                      (access-type . "LOCAL-FILE")
5045                                      (name . ,file)))))
5046       ;; (set-buffer gnus-summary-buffer)
5047       (gnus-article-edit-part handles id))))
5048
5049 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5050 ;; parts...>') but with stripping would be nice.
5051
5052 (defun gnus-mime-delete-part ()
5053   "Delete the MIME part under point.
5054 Replace it with some information about the removed part."
5055   (interactive)
5056   (gnus-article-check-buffer)
5057   (when (gnus-group-read-only-p)
5058     (error "The current group does not support deleting of parts"))
5059   (when (mm-complicated-handles gnus-article-mime-handles)
5060     (error "\
5061 The current article has a complicated MIME structure, giving up..."))
5062   (when (or gnus-expert-user
5063             (gnus-yes-or-no-p "\
5064 Deleting parts may malfunction or destroy the article; continue? "))
5065     (let* ((data (get-text-property (point) 'gnus-data))
5066            (id (get-text-property (point) 'gnus-part))
5067            (handles gnus-article-mime-handles)
5068            (none "(none)")
5069            (description
5070             (let ((desc (mm-handle-description data)))
5071               (when desc
5072                 (mail-decode-encoded-word-string desc))))
5073            (filename
5074             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5075                 none))
5076            (type (mm-handle-media-type data)))
5077       (unless data
5078         (error "No MIME part under point"))
5079       (with-current-buffer (mm-handle-buffer data)
5080         (let ((bsize (format "%s" (buffer-size))))
5081           (erase-buffer)
5082           (insert
5083            (concat
5084             ",----\n"
5085             "| The following attachment has been deleted:\n"
5086             "|\n"
5087             "| Type:           " type "\n"
5088             "| Filename:       " filename "\n"
5089             "| Size (encoded): " bsize " Byte\n"
5090             (when description
5091               (concat    "| Description:    " description "\n"))
5092             "`----\n"))
5093           (setcdr data
5094                   (cdr (mm-make-handle
5095                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5096                         (list "attachment")
5097                         (format "Deleted attachment (%s bytes)" bsize))))))
5098       ;; (set-buffer gnus-summary-buffer)
5099       (gnus-article-edit-part handles id))))
5100
5101 (defun gnus-mime-save-part ()
5102   "Save the MIME part under point."
5103   (interactive)
5104   (gnus-article-check-buffer)
5105   (let ((data (get-text-property (point) 'gnus-data)))
5106     (when data
5107       (mm-save-part data))))
5108
5109 (defun gnus-mime-pipe-part (&optional cmd)
5110   "Pipe the MIME part under point to a process.
5111 Use CMD as the process."
5112   (interactive)
5113   (gnus-article-check-buffer)
5114   (let ((data (get-text-property (point) 'gnus-data)))
5115     (when data
5116       (mm-pipe-part data cmd))))
5117
5118 (defun gnus-mime-view-part ()
5119   "Interactively choose a viewing method for the MIME part under point."
5120   (interactive)
5121   (gnus-article-check-buffer)
5122   (let ((data (get-text-property (point) 'gnus-data)))
5123     (when data
5124       (setq gnus-article-mime-handles
5125             (mm-merge-handles
5126              gnus-article-mime-handles (setq data (copy-sequence data))))
5127       (mm-interactively-view-part data))))
5128
5129 (defun gnus-mime-view-part-as-type-internal ()
5130   (gnus-article-check-buffer)
5131   (let* ((handle (get-text-property (point) 'gnus-data))
5132          (name (or
5133                 ;; Content-Type: foo/bar; name=...
5134                 (mail-content-type-get (mm-handle-type handle) 'name)
5135                 ;; Content-Disposition: attachment; filename=...
5136                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5137          (def-type (and name (mm-default-file-encoding name))))
5138     (or (and def-type (cons def-type 0))
5139         (and handle
5140              (equal (mm-handle-media-supertype handle) "text")
5141              '("text/plain" . 0))
5142         '("application/octet-stream" . 0))))
5143
5144 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5145   "Choose a MIME media type, and view the part as such.
5146 If non-nil, PRED is a predicate to use during completion to limit the
5147 available media-types."
5148   (interactive)
5149   (unless mime-type
5150     (setq mime-type
5151           (let ((default (gnus-mime-view-part-as-type-internal)))
5152             (completing-read
5153              (format "View as MIME type (default %s): "
5154                      (car default))
5155              (mapcar #'list (mailcap-mime-types))
5156              pred nil nil nil
5157              (car default)))))
5158   (gnus-article-check-buffer)
5159   (let ((handle (get-text-property (point) 'gnus-data)))
5160     (when handle
5161       (when (equal (mm-handle-media-type handle) "message/external-body")
5162         (unless (mm-handle-cache handle)
5163           (mm-extern-cache-contents handle))
5164         (setq handle (mm-handle-cache handle)))
5165       (setq handle
5166             (mm-make-handle (mm-handle-buffer handle)
5167                             (cons mime-type (cdr (mm-handle-type handle)))
5168                             (mm-handle-encoding handle)
5169                             (mm-handle-undisplayer handle)
5170                             (mm-handle-disposition handle)
5171                             (mm-handle-description handle)
5172                             nil
5173                             (mm-handle-id handle)))
5174       (setq gnus-article-mime-handles
5175             (mm-merge-handles gnus-article-mime-handles handle))
5176       (when (mm-handle-displayed-p handle)
5177         (mm-remove-part handle))
5178       (gnus-mm-display-part handle))))
5179
5180 (defun gnus-mime-copy-part (&optional handle arg)
5181   "Put the MIME part under point into a new buffer.
5182 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5183 are decompressed."
5184   (interactive (list nil current-prefix-arg))
5185   (gnus-article-check-buffer)
5186   (unless handle
5187     (setq handle (get-text-property (point) 'gnus-data)))
5188   (when handle
5189     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5190                                                'name)
5191                         (mail-content-type-get (mm-handle-disposition handle)
5192                                                'filename)))
5193           contents dont-decode charset coding-system)
5194       (mm-with-unibyte-buffer
5195         (mm-insert-part handle)
5196         (setq contents (or (condition-case nil
5197                                (mm-decompress-buffer filename nil 'sig)
5198                              (error
5199                               (setq dont-decode t)
5200                               nil))
5201                            (buffer-string))))
5202       (setq filename (cond (filename (file-name-nondirectory filename))
5203                            (dont-decode "*raw data*")
5204                            (t "*decoded*")))
5205       (cond
5206        (dont-decode)
5207        ((not arg)
5208         (unless (setq charset (mail-content-type-get
5209                                (mm-handle-type handle) 'charset))
5210           (unless (setq coding-system (mm-with-unibyte-buffer
5211                                         (insert contents)
5212                                         (mm-find-buffer-file-coding-system)))
5213             (setq charset gnus-newsgroup-charset))))
5214        ((numberp arg)
5215         (setq charset (or (cdr (assq arg
5216                                      gnus-summary-show-article-charset-alist))
5217                           (mm-read-coding-system "Charset: ")))))
5218       (switch-to-buffer (generate-new-buffer filename))
5219       (if (or coding-system
5220               (and charset
5221                    (setq coding-system (mm-charset-to-coding-system charset))
5222                    (not (eq charset 'ascii))))
5223           (progn
5224             (mm-enable-multibyte)
5225             (insert (mm-decode-coding-string contents coding-system))
5226             (setq buffer-file-coding-system
5227                   (if (boundp 'last-coding-system-used)
5228                       (symbol-value 'last-coding-system-used)
5229                     coding-system)))
5230         (mm-disable-multibyte)
5231         (insert contents)
5232         (setq buffer-file-coding-system mm-binary-coding-system))
5233       ;; We do it this way to make `normal-mode' set the appropriate mode.
5234       (unwind-protect
5235           (progn
5236             (setq buffer-file-name (expand-file-name filename))
5237             (normal-mode))
5238         (setq buffer-file-name nil))
5239       (goto-char (point-min)))))
5240
5241 (defun gnus-mime-print-part (&optional handle filename)
5242   "Print the MIME part under point."
5243   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5244   (gnus-article-check-buffer)
5245   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5246          (contents (and handle (mm-get-part handle)))
5247          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5248          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5249     (when contents
5250         (if printer
5251             (unwind-protect
5252                 (progn
5253                   (mm-save-part-to-file handle file)
5254                   (call-process shell-file-name nil
5255                                 (generate-new-buffer " *mm*")
5256                                 nil
5257                                 shell-command-switch
5258                                 (mm-mailcap-command
5259                                  printer file (mm-handle-type handle))))
5260               (delete-file file))
5261           (with-temp-buffer
5262             (insert contents)
5263             (gnus-print-buffer))
5264           (ps-despool filename)))))
5265
5266 (defun gnus-mime-inline-part (&optional handle arg)
5267   "Insert the MIME part under point into the current buffer.
5268 Compressed files like .gz and .bz2 are decompressed."
5269   (interactive (list nil current-prefix-arg))
5270   (gnus-article-check-buffer)
5271   (unless handle
5272     (setq handle (get-text-property (point) 'gnus-data)))
5273   (when handle
5274     (let ((b (point))
5275           (inhibit-read-only t)
5276           contents charset coding-system)
5277       (if (and (not arg) (mm-handle-undisplayer handle))
5278           (mm-remove-part handle)
5279         (mm-with-unibyte-buffer
5280           (mm-insert-part handle)
5281           (setq contents
5282                 (or (mm-decompress-buffer
5283                      (or (mail-content-type-get (mm-handle-type handle)
5284                                                 'name)
5285                          (mail-content-type-get (mm-handle-disposition handle)
5286                                                 'filename))
5287                      nil t)
5288                     (buffer-string))))
5289         (cond
5290          ((not arg)
5291           (unless (setq charset (mail-content-type-get
5292                                  (mm-handle-type handle) 'charset))
5293             (unless (setq coding-system
5294                           (mm-with-unibyte-buffer
5295                             (insert contents)
5296                             (mm-find-buffer-file-coding-system)))
5297               (setq charset gnus-newsgroup-charset))))
5298          ((numberp arg)
5299           (if (mm-handle-undisplayer handle)
5300               (mm-remove-part handle))
5301           (setq charset
5302                 (or (cdr (assq arg
5303                                gnus-summary-show-article-charset-alist))
5304                     (mm-read-coding-system "Charset: "))))
5305          (t
5306           (if (mm-handle-undisplayer handle)
5307               (mm-remove-part handle))))
5308         (forward-line 2)
5309         (mm-insert-inline
5310          handle
5311          (if (or coding-system
5312                  (and charset
5313                       (setq coding-system
5314                             (mm-charset-to-coding-system charset))
5315                       (not (eq coding-system 'ascii))))
5316              (mm-decode-coding-string contents coding-system)
5317            (mm-string-to-multibyte contents)))
5318         (goto-char b)))))
5319
5320 (defun gnus-mime-set-charset-parameters (handle charset)
5321   "Set CHARSET to parameters in HANDLE.
5322 CHARSET may either be a string or a symbol."
5323   (unless (stringp charset)
5324     (setq charset (symbol-name charset)))
5325   (if (stringp (car handle))
5326       (dolist (h (cdr handle))
5327         (gnus-mime-set-charset-parameters h charset))
5328     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5329                                             "message/external-body")
5330                                      (progn
5331                                        (unless (mm-handle-cache handle)
5332                                          (mm-extern-cache-contents handle))
5333                                        (mm-handle-cache handle))
5334                                    handle)))
5335            (param (assq 'charset (cdr type))))
5336       (if param
5337           (setcdr param charset)
5338         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5339
5340 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5341   "Insert the MIME part under point into the current buffer using the
5342 specified charset."
5343   (interactive (list nil current-prefix-arg))
5344   (gnus-article-check-buffer)
5345   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5346         (fun (get-text-property (point) 'gnus-callback))
5347         (gnus-newsgroup-ignored-charsets 'gnus-all)
5348         charset form preferred parts)
5349     (when handle
5350       (when (prog1
5351                 (and fun
5352                      (setq charset
5353                            (or (cdr (assq
5354                                      arg
5355                                      gnus-summary-show-article-charset-alist))
5356                                (mm-read-coding-system "Charset: "))))
5357               (if (mm-handle-undisplayer handle)
5358                   (mm-remove-part handle)))
5359         (gnus-mime-set-charset-parameters handle charset)
5360         (when (and (consp (setq form (cdr-safe fun)))
5361                    (setq form (ignore-errors
5362                                 (assq 'gnus-mime-display-alternative form)))
5363                    (setq preferred (caddr form))
5364                    (progn
5365                      (when (eq (car preferred) 'quote)
5366                        (setq preferred (cadr preferred)))
5367                      (not (equal preferred
5368                                  (get-text-property (point) 'gnus-data))))
5369                    (setq parts (get-text-property (point) 'gnus-part))
5370                    (setq parts (cdr (assq parts
5371                                           gnus-article-mime-handle-alist)))
5372                    (equal (mm-handle-media-type parts) "multipart/alternative")
5373                    (setq parts (reverse (cdr parts))))
5374           (setcar (cddr form)
5375                   (list 'quote (or (cadr (member preferred parts))
5376                                    (car parts)))))
5377         (funcall fun handle)))))
5378
5379 (defun gnus-mime-view-part-externally (&optional handle)
5380   "View the MIME part under point with an external viewer."
5381   (interactive)
5382   (gnus-article-check-buffer)
5383   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5384          (mm-user-display-methods nil)
5385          (mm-inlined-types nil)
5386          (mail-parse-charset gnus-newsgroup-charset)
5387          (mail-parse-ignored-charsets
5388           (with-current-buffer gnus-summary-buffer
5389             gnus-newsgroup-ignored-charsets))
5390          (type (mm-handle-media-type handle))
5391          (method (mailcap-mime-info type))
5392          (mm-enable-external t))
5393     (if (not (stringp method))
5394         (gnus-mime-view-part-as-type
5395          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5396       (when handle
5397         (if (mm-handle-undisplayer handle)
5398             (mm-remove-part handle)
5399           (mm-display-part handle))))))
5400
5401 (defun gnus-mime-view-part-internally (&optional handle)
5402   "View the MIME part under point with an internal viewer.
5403 If no internal viewer is available, use an external viewer."
5404   (interactive)
5405   (gnus-article-check-buffer)
5406   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5407          (mm-inlined-types '(".*"))
5408          (mm-inline-large-images t)
5409          (mail-parse-charset gnus-newsgroup-charset)
5410          (mail-parse-ignored-charsets
5411           (with-current-buffer gnus-summary-buffer
5412             gnus-newsgroup-ignored-charsets))
5413          (inhibit-read-only t))
5414     (if (not (mm-inlinable-p handle))
5415         (gnus-mime-view-part-as-type
5416          nil (lambda (types) (mm-inlinable-p handle (car types))))
5417       (when handle
5418         (if (mm-handle-undisplayer handle)
5419             (mm-remove-part handle)
5420           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5421
5422 (defun gnus-mime-action-on-part (&optional action)
5423   "Do something with the MIME attachment at \(point\)."
5424   (interactive
5425    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5426   (gnus-article-check-buffer)
5427   (let ((action-pair (assoc action gnus-mime-action-alist)))
5428     (if action-pair
5429         (funcall (cdr action-pair)))))
5430
5431 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5432   "Call FUNCTION on MIME part N.
5433 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5434 If INTERACTIVE, call FUNCTION interactivly."
5435   (let (window frame)
5436     ;; Check whether the article is displayed.
5437     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5438                  (setq window (get-buffer-window gnus-article-buffer t))
5439                  (frame-visible-p (setq frame (window-frame window))))
5440       (error "No article is displayed"))
5441     (with-current-buffer gnus-article-buffer
5442       ;; Check whether the article displays the right contents.
5443       (unless (with-current-buffer gnus-summary-buffer
5444                 (eq gnus-current-article (gnus-summary-article-number)))
5445         (error "You should select the right article first"))
5446       (if n
5447           (setq n (prefix-numeric-value n))
5448         (let ((pt (point)))
5449           (setq n (or (get-text-property pt 'gnus-part)
5450                       (and (not (bobp))
5451                            (get-text-property (1- pt) 'gnus-part))
5452                       (get-text-property (prog2
5453                                              (forward-line 1)
5454                                              (point)
5455                                            (goto-char pt))
5456                                          'gnus-part)
5457                       (get-text-property
5458                        (or (and (setq pt (previous-single-property-change
5459                                           pt 'gnus-part))
5460                                 (1- pt))
5461                            (next-single-property-change (point) 'gnus-part)
5462                            (point))
5463                        'gnus-part)
5464                       1))))
5465       ;; Check whether the specified part exists.
5466       (when (> n (length gnus-article-mime-handle-alist))
5467         (error "No such part")))
5468     (unless
5469         (progn
5470           ;; To select the window is needed so that the cursor
5471           ;; might be visible on the MIME button.
5472           (select-window (prog1
5473                              window
5474                            (setq window (selected-window))
5475                            ;; Article may be displayed in the other frame.
5476                            (gnus-select-frame-set-input-focus
5477                             (prog1
5478                                 frame
5479                               (setq frame (selected-frame))))))
5480           (when (gnus-article-goto-part n)
5481             ;; We point the cursor and the arrow at the MIME button
5482             ;; when the `function' prompt the user for something.
5483             (let ((cursor-in-non-selected-windows t)
5484                   (overlay-arrow-string "=>")
5485                   (overlay-arrow-position (point-marker)))
5486               (unwind-protect
5487                   (cond
5488                    ((and no-handle interactive)
5489                     (call-interactively function))
5490                    (no-handle
5491                     (funcall function))
5492                    (interactive
5493                     (call-interactively
5494                      function
5495                      (cdr (assq n gnus-article-mime-handle-alist))))
5496                    (t
5497                     (funcall function
5498                              (cdr (assq n gnus-article-mime-handle-alist)))))
5499                 (set-marker overlay-arrow-position nil)
5500                 (unless gnus-auto-select-part
5501                   (gnus-select-frame-set-input-focus frame)
5502                   (select-window window))))
5503             t))
5504       (if gnus-inhibit-mime-unbuttonizing
5505           ;; This is the default though the program shouldn't reach here.
5506           (error "No such part")
5507         ;; The part which doesn't have the MIME button is selected.
5508         ;; So, we display all the buttons and redo it.
5509         (let ((gnus-inhibit-mime-unbuttonizing t))
5510           (gnus-summary-show-article)
5511           (gnus-article-part-wrapper n function no-handle))))))
5512
5513 (defun gnus-article-pipe-part (n)
5514   "Pipe MIME part N, which is the numerical prefix."
5515   (interactive "P")
5516   (gnus-article-part-wrapper n 'mm-pipe-part))
5517
5518 (defun gnus-article-save-part (n)
5519   "Save MIME part N, which is the numerical prefix."
5520   (interactive "P")
5521   (gnus-article-part-wrapper n 'mm-save-part))
5522
5523 (defun gnus-article-interactively-view-part (n)
5524   "View MIME part N interactively, which is the numerical prefix."
5525   (interactive "P")
5526   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5527
5528 (defun gnus-article-copy-part (n)
5529   "Copy MIME part N, which is the numerical prefix."
5530   (interactive "P")
5531   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5532
5533 (defun gnus-article-view-part-as-charset (n)
5534   "View MIME part N using a specified charset.
5535 N is the numerical prefix."
5536   (interactive "P")
5537   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5538
5539 (defun gnus-article-view-part-externally (n)
5540   "View MIME part N externally, which is the numerical prefix."
5541   (interactive "P")
5542   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5543
5544 (defun gnus-article-inline-part (n)
5545   "Inline MIME part N, which is the numerical prefix."
5546   (interactive "P")
5547   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5548
5549 (defun gnus-article-save-part-and-strip (n)
5550   "Save MIME part N and replace it with an external body.
5551 N is the numerical prefix."
5552   (interactive "P")
5553   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5554
5555 (defun gnus-article-replace-part (n)
5556   "Replace MIME part N with an external body.
5557 N is the numerical prefix."
5558   (interactive "P")
5559   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5560
5561 (defun gnus-article-delete-part (n)
5562   "Delete MIME part N and add some information about the removed part.
5563 N is the numerical prefix."
5564   (interactive "P")
5565   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5566
5567 (defun gnus-article-view-part-as-type (n)
5568   "Choose a MIME media type, and view part N as such.
5569 N is the numerical prefix."
5570   (interactive "P")
5571   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5572
5573 (defun gnus-article-mime-match-handle-first (condition)
5574   (if condition
5575       (let (n)
5576         (dolist (ihandle gnus-article-mime-handle-alist)
5577           (if (and (cond
5578                     ((functionp condition)
5579                      (funcall condition (cdr ihandle)))
5580                     ((eq condition 'undisplayed)
5581                      (not (or (mm-handle-undisplayer (cdr ihandle))
5582                               (equal (mm-handle-media-type (cdr ihandle))
5583                                      "multipart/alternative"))))
5584                     ((eq condition 'undisplayed-alternative)
5585                      (not (mm-handle-undisplayer (cdr ihandle))))
5586                     (t t))
5587                    (gnus-article-goto-part (car ihandle))
5588                    (or (not n) (< (car ihandle) n)))
5589               (setq n (car ihandle))))
5590         (or n 1))
5591     1))
5592
5593 (defun gnus-article-view-part (&optional n)
5594   "View MIME part N, which is the numerical prefix.
5595 If the part is already shown, hide the part.  If N is nil, view
5596 all parts."
5597   (interactive "P")
5598   (with-current-buffer gnus-article-buffer
5599     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5600                              gnus-article-mime-match-handle-function)))
5601     (when (> n (length gnus-article-mime-handle-alist))
5602       (error "No such part"))
5603     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5604       (when (gnus-article-goto-part n)
5605         (if (equal (car handle) "multipart/alternative")
5606             (gnus-article-press-button)
5607           (when (eq (gnus-mm-display-part handle) 'internal)
5608             (gnus-set-window-start)))))))
5609
5610 (defsubst gnus-article-mime-total-parts ()
5611   (if (bufferp (car gnus-article-mime-handles))
5612       1 ;; single part
5613     (1- (length gnus-article-mime-handles))))
5614
5615 (defun gnus-mm-display-part (handle)
5616   "Display HANDLE and fix MIME button."
5617   (let ((id (get-text-property (point) 'gnus-part))
5618         (point (point))
5619         (inhibit-read-only t))
5620     (forward-line 1)
5621     (prog1
5622         (let ((window (selected-window))
5623               (mail-parse-charset gnus-newsgroup-charset)
5624               (mail-parse-ignored-charsets
5625                (if (gnus-buffer-live-p gnus-summary-buffer)
5626                    (with-current-buffer gnus-summary-buffer
5627                      gnus-newsgroup-ignored-charsets)
5628                  nil)))
5629           (save-excursion
5630             (unwind-protect
5631                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5632                       (beg (point)))
5633                   (when win
5634                     (select-window win))
5635                   (goto-char point)
5636                   (forward-line)
5637                   (if (mm-handle-displayed-p handle)
5638                       ;; This will remove the part.
5639                       (mm-display-part handle)
5640                     (save-restriction
5641                       (narrow-to-region (point)
5642                                         (if (eobp) (point) (1+ (point))))
5643                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5644                       ;; We narrow to the part itself and
5645                       ;; then call the treatment functions.
5646                       (goto-char (point-min))
5647                       (forward-line 1)
5648                       (narrow-to-region (point) (point-max))
5649                       (gnus-treat-article
5650                        nil id
5651                        (gnus-article-mime-total-parts)
5652                        (mm-handle-media-type handle)))))
5653               (if (window-live-p window)
5654                   (select-window window)))))
5655       (goto-char point)
5656       (gnus-delete-line)
5657       (gnus-insert-mime-button
5658        handle id (list (mm-handle-displayed-p handle)))
5659       (goto-char point))))
5660
5661 (defun gnus-article-goto-part (n)
5662   "Go to MIME part N."
5663   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5664
5665 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5666   (let ((gnus-tmp-name
5667          (or (mail-content-type-get (mm-handle-type handle) 'name)
5668              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5669              (mail-content-type-get (mm-handle-type handle) 'url)
5670              ""))
5671         (gnus-tmp-type (mm-handle-media-type handle))
5672         (gnus-tmp-description (or (mm-handle-description handle) ""))
5673         (gnus-tmp-dots
5674          (if (if displayed (car displayed)
5675                (mm-handle-displayed-p handle))
5676              "" "..."))
5677         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5678                            (buffer-size)))
5679         gnus-tmp-type-long b e)
5680     (when (string-match ".*/" gnus-tmp-name)
5681       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5682     (setq gnus-tmp-type-long (concat gnus-tmp-type
5683                                      (and (not (equal gnus-tmp-name ""))
5684                                           (concat "; " gnus-tmp-name))))
5685     (unless (equal gnus-tmp-description "")
5686       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5687     (unless (bolp)
5688       (insert "\n"))
5689     (setq b (point))
5690     (gnus-eval-format
5691      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5692      `(keymap ,gnus-mime-button-map
5693               gnus-callback gnus-mm-display-part
5694               gnus-part ,gnus-tmp-id
5695               article-type annotation
5696               gnus-data ,handle))
5697     (setq e (if (bolp)
5698                 ;; Exclude a newline.
5699                 (1- (point))
5700               (point)))
5701     (when gnus-article-button-face
5702       (gnus-overlay-put (gnus-make-overlay b e nil t)
5703                         'face gnus-article-button-face))
5704     (widget-convert-button
5705      'link b e
5706      :mime-handle handle
5707      :action 'gnus-widget-press-button
5708      :button-keymap gnus-mime-button-map
5709      :help-echo
5710      (lambda (widget)
5711        ;; Needed to properly clear the message due to a bug in
5712        ;; wid-edit (XEmacs only).
5713        (if (boundp 'help-echo-owns-message)
5714            (setq help-echo-owns-message t))
5715        (format
5716         "%S: %s the MIME part; %S: more options"
5717         (aref gnus-mouse-2 0)
5718         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5719             "hide" "show")
5720         (aref gnus-down-mouse-3 0))))))
5721
5722 (defun gnus-widget-press-button (elems el)
5723   (goto-char (widget-get elems :from))
5724   (gnus-article-press-button))
5725
5726 (defvar gnus-displaying-mime nil)
5727
5728 (defun gnus-display-mime (&optional ihandles)
5729   "Display the MIME parts."
5730   (save-excursion
5731     (save-selected-window
5732       (let ((window (get-buffer-window gnus-article-buffer))
5733             (point (point)))
5734         (when window
5735           (select-window window)
5736           ;; We have to do this since selecting the window
5737           ;; may change the point.  So we set the window point.
5738           (set-window-point window point)))
5739       (let ((handles ihandles)
5740             (inhibit-read-only t)
5741             handle)
5742         (cond (handles)
5743               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5744                (when gnus-article-emulate-mime
5745                  (mm-uu-dissect-text-parts handles)))
5746               (gnus-article-emulate-mime
5747                (setq handles (mm-uu-dissect))))
5748         (when (and (not ihandles)
5749                    (not gnus-displaying-mime))
5750           ;; Top-level call; we clean up.
5751           (when gnus-article-mime-handles
5752             (mm-destroy-parts gnus-article-mime-handles)
5753             (setq gnus-article-mime-handle-alist nil));; A trick.
5754           (setq gnus-article-mime-handles handles)
5755           ;; We allow users to glean info from the handles.
5756           (when gnus-article-mime-part-function
5757             (gnus-mime-part-function handles)))
5758         (if (and handles
5759                  (or (not (stringp (car handles)))
5760                      (cdr handles)))
5761             (progn
5762               (when (and (not ihandles)
5763                          (not gnus-displaying-mime))
5764                 ;; Clean up for mime parts.
5765                 (article-goto-body)
5766                 (delete-region (point) (point-max)))
5767               (let ((gnus-displaying-mime t))
5768                 (gnus-mime-display-part handles)))
5769           (save-restriction
5770             (article-goto-body)
5771             (narrow-to-region (point) (point-max))
5772             (gnus-treat-article nil 1 1)
5773             (widen)))
5774         (unless ihandles
5775           ;; Highlight the headers.
5776           (save-excursion
5777             (save-restriction
5778               (article-goto-body)
5779               (narrow-to-region (point-min) (point))
5780               (gnus-article-save-original-date
5781                (gnus-treat-article 'head)))))))
5782     ;; Cope with broken MIME messages.
5783     (goto-char (point-max))
5784     (unless (bolp)
5785       (insert "\n"))))
5786
5787 (defcustom gnus-mime-display-multipart-as-mixed nil
5788   "Display \"multipart\" parts as  \"multipart/mixed\".
5789
5790 If t, it overrides nil values of
5791 `gnus-mime-display-multipart-alternative-as-mixed' and
5792 `gnus-mime-display-multipart-related-as-mixed'."
5793   :group 'gnus-article-mime
5794   :type 'boolean)
5795
5796 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5797   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5798   :version "22.1"
5799   :group 'gnus-article-mime
5800   :type 'boolean)
5801
5802 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5803   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5804
5805 If displaying \"text/html\" is discouraged \(see
5806 `mm-discouraged-alternatives'\) images or other material inside a
5807 \"multipart/related\" part might be overlooked when this variable is nil."
5808   :version "22.1"
5809   :group 'gnus-article-mime
5810   :type 'boolean)
5811
5812 (defun gnus-mime-display-part (handle)
5813   (cond
5814    ;; Maybe a broken MIME message.
5815    ((null handle))
5816    ;; Single part.
5817    ((not (stringp (car handle)))
5818     (gnus-mime-display-single handle))
5819    ;; User-defined multipart
5820    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5821     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5822              handle))
5823    ;; multipart/alternative
5824    ((and (equal (car handle) "multipart/alternative")
5825          (not (or gnus-mime-display-multipart-as-mixed
5826                   gnus-mime-display-multipart-alternative-as-mixed)))
5827     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5828       (push (cons id handle) gnus-article-mime-handle-alist)
5829       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5830    ;; multipart/related
5831    ((and (equal (car handle) "multipart/related")
5832          (not (or gnus-mime-display-multipart-as-mixed
5833                   gnus-mime-display-multipart-related-as-mixed)))
5834     ;;;!!!We should find the start part, but we just default
5835     ;;;!!!to the first part.
5836     ;;(gnus-mime-display-part (cadr handle))
5837     ;;;!!! Most multipart/related is an HTML message plus images.
5838     ;;;!!! Unfortunately we are unable to let W3 display those
5839     ;;;!!! included images, so we just display it as a mixed multipart.
5840     ;;(gnus-mime-display-mixed (cdr handle))
5841     ;;;!!! No, w3 can display everything just fine.
5842     (gnus-mime-display-part (cadr handle)))
5843    ((equal (car handle) "multipart/signed")
5844     (gnus-add-wash-type 'signed)
5845     (gnus-mime-display-security handle))
5846    ((equal (car handle) "multipart/encrypted")
5847     (gnus-add-wash-type 'encrypted)
5848     (gnus-mime-display-security handle))
5849    ;; Other multiparts are handled like multipart/mixed.
5850    (t
5851     (gnus-mime-display-mixed (cdr handle)))))
5852
5853 (defun gnus-mime-part-function (handles)
5854   (if (stringp (car handles))
5855       (mapcar 'gnus-mime-part-function (cdr handles))
5856     (funcall gnus-article-mime-part-function handles)))
5857
5858 (defun gnus-mime-display-mixed (handles)
5859   (mapcar 'gnus-mime-display-part handles))
5860
5861 (defun gnus-mime-display-single (handle)
5862   (let ((type (mm-handle-media-type handle))
5863         (ignored gnus-ignored-mime-types)
5864         (not-attachment t)
5865         (move nil)
5866         display text)
5867     (catch 'ignored
5868       (progn
5869         (while ignored
5870           (when (string-match (pop ignored) type)
5871             (throw 'ignored nil)))
5872         (if (and (setq not-attachment
5873                        (and (not (mm-inline-override-p handle))
5874                             (or (not (mm-handle-disposition handle))
5875                                 (equal (car (mm-handle-disposition handle))
5876                                        "inline")
5877                                 (mm-attachment-override-p handle))))
5878                  (mm-automatic-display-p handle)
5879                  (or (and
5880                       (mm-inlinable-p handle)
5881                       (mm-inlined-p handle))
5882                      (mm-automatic-external-display-p type)))
5883             (setq display t)
5884           (when (equal (mm-handle-media-supertype handle) "text")
5885             (setq text t)))
5886         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5887               beg)
5888           (push (cons id handle) gnus-article-mime-handle-alist)
5889           (when (and display
5890                      (equal (mm-handle-media-supertype handle) "message"))
5891             (insert-char
5892              ?\n
5893              (cond ((not (bolp)) 2)
5894                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5895                    (t 1))))
5896           (when (or (not display)
5897                     (not (gnus-unbuttonized-mime-type-p type))
5898                     (eq id gnus-mime-buttonized-part-id))
5899             (gnus-insert-mime-button
5900              handle id (list (or display (and not-attachment text))))
5901             (gnus-article-insert-newline)
5902             ;; Remember modify the number of forward lines.
5903             (setq move t))
5904           (setq beg (point))
5905           (cond
5906            (display
5907             (when move
5908               (forward-line -1)
5909               (setq beg (point)))
5910             (let ((mail-parse-charset gnus-newsgroup-charset)
5911                   (mail-parse-ignored-charsets
5912                    (save-excursion (condition-case ()
5913                                        (set-buffer gnus-summary-buffer)
5914                                      (error))
5915                                    gnus-newsgroup-ignored-charsets)))
5916               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5917             (goto-char (point-max)))
5918            ((and text not-attachment)
5919             (when move
5920               (forward-line -1)
5921               (setq beg (point)))
5922             (gnus-article-insert-newline)
5923             (mm-insert-inline
5924              handle
5925              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5926                                                        'charset)
5927                                 (and (equal type "text/calendar") 'utf-8))))
5928                (cond ((not charset)
5929                       (mm-string-as-multibyte (mm-get-part handle)))
5930                      ((eq charset 'gnus-decoded)
5931                       (with-current-buffer (mm-handle-buffer handle)
5932                         (buffer-string)))
5933                      (t
5934                       (mm-decode-string (mm-get-part handle) charset)))))
5935             (goto-char (point-max))))
5936           ;; Do highlighting.
5937           (save-excursion
5938             (save-restriction
5939               (narrow-to-region beg (point))
5940               (if (eq handle gnus-article-mime-handles)
5941                   ;; The format=flowed case.
5942                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5943                 ;; Don't count signature parts that are never displayed.
5944                 ;; The part number should be re-calculated supposing this
5945                 ;; might be a message/rfc822 part.
5946                 (let (handles)
5947                   (dolist (part gnus-article-mime-handles)
5948                     (unless (or (stringp part)
5949                                 (equal (car (mm-handle-type part))
5950                                        "application/pgp-signature"))
5951                       (push part handles)))
5952                   (gnus-treat-article
5953                    nil (length (memq handle handles)) (length handles)
5954                    (mm-handle-media-type handle)))))))))))
5955
5956 (defun gnus-unbuttonized-mime-type-p (type)
5957   "Say whether TYPE is to be unbuttonized."
5958   (unless gnus-inhibit-mime-unbuttonizing
5959     (when (catch 'found
5960             (let ((types gnus-unbuttonized-mime-types))
5961               (while types
5962                 (when (string-match (pop types) type)
5963                   (throw 'found t)))))
5964       (not (catch 'found
5965              (let ((types gnus-buttonized-mime-types))
5966                (while types
5967                  (when (string-match (pop types) type)
5968                    (throw 'found t)))))))))
5969
5970 (defun gnus-article-insert-newline ()
5971   "Insert a newline, but mark it as undeletable."
5972   (gnus-put-text-property
5973    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5974
5975 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5976   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5977          (ihandles handles)
5978          (point (point))
5979          handle (inhibit-read-only t) from props begend not-pref)
5980     (save-window-excursion
5981       (save-restriction
5982         (when ibegend
5983           (narrow-to-region (car ibegend)
5984                             (or (cdr ibegend)
5985                                 (progn
5986                                   (goto-char (car ibegend))
5987                                   (forward-line 2)
5988                                   (point))))
5989           (delete-region (point-min) (point-max))
5990           (mm-remove-parts handles))
5991         (setq begend (list (point-marker)))
5992         ;; Do the toggle.
5993         (unless (setq not-pref (cadr (member preferred ihandles)))
5994           (setq not-pref (car ihandles)))
5995         (when (or ibegend
5996                   (not preferred)
5997                   (not (gnus-unbuttonized-mime-type-p
5998                         "multipart/alternative")))
5999           (gnus-add-text-properties
6000            (setq from (point))
6001            (progn
6002              (insert (format "%d.  " id))
6003              (point))
6004            `(gnus-callback
6005              (lambda (handles)
6006                (unless ,(not ibegend)
6007                  (setq gnus-article-mime-handle-alist
6008                        ',gnus-article-mime-handle-alist))
6009                (gnus-mime-display-alternative
6010                 ',ihandles ',not-pref ',begend ,id))
6011              keymap ,gnus-mime-button-map
6012              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6013              face ,gnus-article-button-face
6014              gnus-part ,id
6015              article-type multipart))
6016           (widget-convert-button 'link from (point)
6017                                  :action 'gnus-widget-press-button
6018                                  :button-keymap gnus-widget-button-keymap)
6019           ;; Do the handles
6020           (while (setq handle (pop handles))
6021             (gnus-add-text-properties
6022              (setq from (point))
6023              (progn
6024                (insert (format "(%c) %-18s"
6025                                (if (equal handle preferred) ?* ? )
6026                                (mm-handle-media-type handle)))
6027                (point))
6028              `(gnus-callback
6029                (lambda (handles)
6030                  (unless ,(not ibegend)
6031                    (setq gnus-article-mime-handle-alist
6032                          ',gnus-article-mime-handle-alist))
6033                  (gnus-mime-display-alternative
6034                   ',ihandles ',handle ',begend ,id))
6035                keymap ,gnus-mime-button-map
6036                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6037                face ,gnus-article-button-face
6038                gnus-part ,id
6039                gnus-data ,handle))
6040             (widget-convert-button 'link from (point)
6041                                    :action 'gnus-widget-press-button
6042                                    :button-keymap gnus-widget-button-keymap)
6043             (insert "  "))
6044           (insert "\n\n"))
6045         (when preferred
6046           (if (stringp (car preferred))
6047               (gnus-display-mime preferred)
6048             (let ((mail-parse-charset gnus-newsgroup-charset)
6049                   (mail-parse-ignored-charsets
6050                    (with-current-buffer gnus-summary-buffer
6051                      gnus-newsgroup-ignored-charsets)))
6052               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6053               ;; Do highlighting.
6054               (save-excursion
6055                 (save-restriction
6056                   (narrow-to-region (car begend) (point-max))
6057                   (gnus-treat-article
6058                    nil (length gnus-article-mime-handle-alist)
6059                    (gnus-article-mime-total-parts)
6060                    (mm-handle-media-type handle))))))
6061           (goto-char (point-max))
6062           (setcdr begend (point-marker)))))
6063     (when ibegend
6064       (goto-char point))))
6065
6066 (defconst gnus-article-wash-status-strings
6067   (let ((alist '((cite "c" "Possible hidden citation text"
6068                        " " "All citation text visible")
6069                  (headers "h" "Hidden headers"
6070                           " " "All headers visible.")
6071                  (pgp "p" "Encrypted or signed message status hidden"
6072                       " " "No hidden encryption nor digital signature status")
6073                  (signature "s" "Signature has been hidden"
6074                             " " "Signature is visible")
6075                  (overstrike "o" "Overstrike (^H) characters applied"
6076                              " " "No overstrike characters applied")
6077                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6078                            " " "No /*_emphasis_*/ characters applied")))
6079         result)
6080     (dolist (entry alist result)
6081       (let ((key (nth 0 entry))
6082             (on (copy-sequence (nth 1 entry)))
6083             (on-help (nth 2 entry))
6084             (off (copy-sequence (nth 3 entry)))
6085             (off-help (nth 4 entry)))
6086         (put-text-property 0 1 'help-echo on-help on)
6087         (put-text-property 0 1 'help-echo off-help off)
6088         (push (list key on off) result))))
6089   "Alist of strings describing wash status in the mode line.
6090 Each entry has the form (KEY ON OF), where the KEY is a symbol
6091 representing the particular washing function, ON is the string to use
6092 in the article mode line when the washing function is active, and OFF
6093 is the string to use when it is inactive.")
6094
6095 (defun gnus-article-wash-status-entry (key value)
6096   (let ((entry (assoc key gnus-article-wash-status-strings)))
6097     (if value (nth 1 entry) (nth 2 entry))))
6098
6099 (defun gnus-article-wash-status ()
6100   "Return a string which display status of article washing."
6101   (with-current-buffer gnus-article-buffer
6102     (let ((cite (memq 'cite gnus-article-wash-types))
6103           (headers (memq 'headers gnus-article-wash-types))
6104           (boring (memq 'boring-headers gnus-article-wash-types))
6105           (pgp (memq 'pgp gnus-article-wash-types))
6106           (pem (memq 'pem gnus-article-wash-types))
6107           (signed (memq 'signed gnus-article-wash-types))
6108           (encrypted (memq 'encrypted gnus-article-wash-types))
6109           (signature (memq 'signature gnus-article-wash-types))
6110           (overstrike (memq 'overstrike gnus-article-wash-types))
6111           (emphasis (memq 'emphasis gnus-article-wash-types)))
6112       (concat
6113        (gnus-article-wash-status-entry 'cite cite)
6114        (gnus-article-wash-status-entry 'headers (or headers boring))
6115        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6116        (gnus-article-wash-status-entry 'signature signature)
6117        (gnus-article-wash-status-entry 'overstrike overstrike)
6118        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6119
6120 (defun gnus-add-wash-type (type)
6121   "Add a washing of TYPE to the current status."
6122   (add-to-list 'gnus-article-wash-types type))
6123
6124 (defun gnus-delete-wash-type (type)
6125   "Add a washing of TYPE to the current status."
6126   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6127
6128 (defun gnus-add-image (category image)
6129   "Add IMAGE of CATEGORY to the list of displayed images."
6130   (let ((entry (assq category gnus-article-image-alist)))
6131     (unless entry
6132       (setq entry (list category))
6133       (push entry gnus-article-image-alist))
6134     (nconc entry (list image))))
6135
6136 (defun gnus-delete-images (category)
6137   "Delete all images in CATEGORY."
6138   (let ((entry (assq category gnus-article-image-alist)))
6139     (dolist (image (cdr entry))
6140       (gnus-remove-image image category))
6141     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6142     (gnus-delete-wash-type category)))
6143
6144 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6145
6146 (defun gnus-article-maybe-hide-headers ()
6147   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6148 Provided for backwards compatibility."
6149   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6150                  (not (with-current-buffer gnus-summary-buffer
6151                         gnus-have-all-headers)))
6152              (not gnus-inhibit-hiding))
6153     (gnus-article-hide-headers)))
6154
6155 ;;; Article savers.
6156
6157 (defun gnus-output-to-file (file-name)
6158   "Append the current article to a file named FILE-NAME.
6159 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6160 text and used as the value of the coding cookie which is added to the
6161 top of a file.  Otherwise, this function saves a raw article without
6162 the coding cookie."
6163   (let* ((artbuf (current-buffer))
6164          (file-name-coding-system nnmail-pathname-coding-system)
6165          (coding gnus-article-save-coding-system)
6166          (coding-system-for-read (if coding
6167                                      nil ;; Rely on the coding cookie.
6168                                    mm-text-coding-system))
6169          (coding-system-for-write (or coding
6170                                       mm-text-coding-system-for-write
6171                                       mm-text-coding-system))
6172          (exists (file-exists-p file-name)))
6173     (with-temp-buffer
6174       (when exists
6175         (insert-file-contents file-name)
6176         (goto-char (point-min))
6177         ;; Remove the existing coding cookie.
6178         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6179           (delete-region (match-beginning 0) (match-end 0))))
6180       (goto-char (point-max))
6181       (insert-buffer-substring artbuf)
6182       ;; Append newline at end of the buffer as separator, and then
6183       ;; save it to file.
6184       (goto-char (point-max))
6185       (insert "\n")
6186       (when coding
6187         ;; If the coding system is not suitable to encode the text,
6188         ;; ask a user for a proper one.
6189         (when (fboundp 'select-safe-coding-system)
6190           (setq coding (coding-system-base
6191                         (save-window-excursion
6192                           (select-safe-coding-system (point-min) (point-max)
6193                                                      coding))))
6194           (setq coding-system-for-write
6195                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6196                     coding)))
6197         (goto-char (point-min))
6198         ;; Add the coding cookie.
6199         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6200                         coding-system-for-write)))
6201       (if exists
6202           (progn
6203             (write-region (point-min) (point-max) file-name nil 'no-message)
6204             (message "Appended to %s" file-name))
6205         (write-region (point-min) (point-max) file-name))))
6206   t)
6207
6208 (defun gnus-narrow-to-page (&optional arg)
6209   "Narrow the article buffer to a page.
6210 If given a numerical ARG, move forward ARG pages."
6211   (interactive "P")
6212   (setq arg (if arg (prefix-numeric-value arg) 0))
6213   (with-current-buffer gnus-article-buffer
6214     (widen)
6215     ;; Remove any old next/prev buttons.
6216     (when (gnus-visual-p 'page-marker)
6217       (let ((inhibit-read-only t))
6218         (gnus-remove-text-with-property 'gnus-prev)
6219         (gnus-remove-text-with-property 'gnus-next)))
6220     (let (st nd pt)
6221       (when (save-excursion
6222               (cond ((< arg 0)
6223                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6224                          (prog1
6225                              (setq nd (match-beginning 0)
6226                                    pt nd)
6227                            (when (re-search-backward page-delimiter nil t)
6228                              (setq st (match-end 0))))
6229                        (when (re-search-forward page-delimiter nil t)
6230                          (setq nd (match-beginning 0)
6231                                pt (point-min)))))
6232                     ((> arg 0)
6233                      (if (re-search-forward page-delimiter nil 'move arg)
6234                          (prog1
6235                              (setq st (match-end 0)
6236                                    pt st)
6237                            (when (re-search-forward page-delimiter nil t)
6238                              (setq nd (match-beginning 0))))
6239                        (when (re-search-backward page-delimiter nil t)
6240                          (setq st (match-end 0)
6241                                pt (point-max)))))
6242                     (t
6243                      (when (re-search-backward page-delimiter nil t)
6244                        (goto-char (setq st (match-end 0))))
6245                      (when (re-search-forward page-delimiter nil t)
6246                        (setq nd (match-beginning 0)))
6247                      (or st nd))))
6248         (setq gnus-page-broken t)
6249         (when pt (goto-char pt))
6250         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6251         (when (gnus-visual-p 'page-marker)
6252           (save-excursion
6253             (when nd
6254               (goto-char nd)
6255               (gnus-insert-next-page-button))
6256             (when st
6257               (goto-char st)
6258               (gnus-insert-prev-page-button))))))))
6259
6260 ;; Article mode commands
6261
6262 (defun gnus-article-goto-next-page ()
6263   "Show the next page of the article."
6264   (interactive)
6265   (when (gnus-article-next-page)
6266     (goto-char (point-min))
6267     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6268
6269
6270 (defun gnus-article-goto-prev-page ()
6271   "Show the previous page of the article."
6272   (interactive)
6273   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6274       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6275     (gnus-article-prev-page nil)))
6276
6277 ;; This is cleaner but currently breaks `gnus-pick-mode':
6278 ;;
6279 ;; (defun gnus-article-goto-next-page ()
6280 ;;   "Show the next page of the article."
6281 ;;   (interactive)
6282 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6283 ;;     (gnus-summary-next-page)))
6284 ;;
6285 ;; (defun gnus-article-goto-prev-page ()
6286 ;;   "Show the next page of the article."
6287 ;;   (interactive)
6288 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6289 ;;     (gnus-summary-prev-page)))
6290
6291 (defun gnus-article-next-page (&optional lines)
6292   "Show the next page of the current article.
6293 If end of article, return non-nil.  Otherwise return nil.
6294 Argument LINES specifies lines to be scrolled up."
6295   (interactive "p")
6296   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6297   (if (and (not (and gnus-article-over-scroll
6298                      (> (count-lines (window-start) (point-max))
6299                         (if (featurep 'xemacs)
6300                             (or lines (1- (window-height)))
6301                           (+ (or lines (1- (window-height))) scroll-margin)))))
6302            (save-excursion
6303              (end-of-line)
6304              (and (pos-visible-in-window-p)     ;Not continuation line.
6305                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6306       ;; Nothing in this page.
6307       (if (or (not gnus-page-broken)
6308               (save-excursion
6309                 (save-restriction
6310                   (widen)
6311                   (forward-line)
6312                   (eobp)))) ;Real end-of-buffer?
6313           (progn
6314             (when gnus-article-over-scroll
6315               (gnus-article-next-page-1 lines))
6316             t)                  ;Nothing more.
6317         (gnus-narrow-to-page 1)         ;Go to next page.
6318         nil)
6319     ;; More in this page.
6320     (gnus-article-next-page-1 lines)
6321     nil))
6322
6323 (defun gnus-article-beginning-of-window ()
6324   "Move point to the beginning of the window.
6325 In Emacs, the point is placed at the line number which `scroll-margin'
6326 specifies."
6327   (if (featurep 'xemacs)
6328       (move-to-window-line 0)
6329     ;; There is an obscure bug in Emacs that makes it impossible to
6330     ;; scroll past big pictures in the article buffer.  Try to fix
6331     ;; this by adding a sanity check by counting the lines visible.
6332     (when (> (count-lines (window-start) (window-end)) 30)
6333       (move-to-window-line
6334        (min (max 0 scroll-margin)
6335             (max 1 (- (window-height)
6336                       (if mode-line-format 1 0)
6337                       (if header-line-format 1 0)
6338                       2)))))))
6339
6340 (defun gnus-article-next-page-1 (lines)
6341   (condition-case ()
6342       (let ((scroll-in-place nil))
6343         (scroll-up lines))
6344     (end-of-buffer
6345      ;; Long lines may cause an end-of-buffer error.
6346      (goto-char (point-max))))
6347   (gnus-article-beginning-of-window))
6348
6349 (defun gnus-article-prev-page (&optional lines)
6350   "Show previous page of current article.
6351 Argument LINES specifies lines to be scrolled down."
6352   (interactive "p")
6353   (move-to-window-line 0)
6354   (if (and gnus-page-broken
6355            (bobp)
6356            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6357       (progn
6358         (gnus-narrow-to-page -1)        ;Go to previous page.
6359         (goto-char (point-max))
6360         (recenter (if gnus-article-over-scroll
6361                       (if lines
6362                           (max (if (featurep 'xemacs)
6363                                    lines
6364                                  (+ lines scroll-margin))
6365                                3)
6366                         (- (window-height) 2))
6367                     -1)))
6368     (prog1
6369         (condition-case ()
6370             (let ((scroll-in-place nil))
6371               (scroll-down lines))
6372           (beginning-of-buffer
6373            (goto-char (point-min))))
6374       (gnus-article-beginning-of-window))))
6375
6376 (defun gnus-article-only-boring-p ()
6377   "Decide whether there is only boring text remaining in the article.
6378 Something \"interesting\" is a word of at least two letters that does
6379 not have a face in `gnus-article-boring-faces'."
6380   (when (and gnus-article-skip-boring
6381              (boundp 'gnus-article-boring-faces)
6382              (symbol-value 'gnus-article-boring-faces))
6383     (save-excursion
6384       (let ((inhibit-point-motion-hooks t))
6385         (catch 'only-boring
6386           (while (re-search-forward "\\b\\w\\w" nil t)
6387             (forward-char -1)
6388             (when (not (gnus-intersection
6389                         (gnus-faces-at (point))
6390                         (symbol-value 'gnus-article-boring-faces)))
6391               (throw 'only-boring nil)))
6392           (throw 'only-boring t))))))
6393
6394 (defun gnus-article-refer-article ()
6395   "Read article specified by message-id around point."
6396   (interactive)
6397   (save-excursion
6398     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6399     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6400     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6401         (let ((msg-id (concat "<" (match-string 0) ">")))
6402           (set-buffer gnus-summary-buffer)
6403           (gnus-summary-refer-article msg-id))
6404       (error "No references around point"))))
6405
6406 (defun gnus-article-show-summary ()
6407   "Reconfigure windows to show summary buffer."
6408   (interactive)
6409   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6410       (error "There is no summary buffer for this article buffer")
6411     (gnus-article-set-globals)
6412     (gnus-configure-windows 'article)
6413     (gnus-summary-goto-subject gnus-current-article)
6414     (gnus-summary-position-point)))
6415
6416 (defun gnus-article-describe-briefly ()
6417   "Describe article mode commands briefly."
6418   (interactive)
6419   (gnus-message 6 "%s" (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")))
6420
6421 (defun gnus-article-check-buffer ()
6422   "Beep if not in an article buffer."
6423   (unless (equal major-mode 'gnus-article-mode)
6424     (error "Command invoked outside of a Gnus article buffer")))
6425
6426 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6427   "Read a summary buffer key sequence and execute it from the article buffer."
6428   (interactive "P")
6429   (gnus-article-check-buffer)
6430   (let ((nosaves
6431          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6432            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6433            "=" "^" "\M-^" "|"))
6434         (nosave-but-article
6435          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6436            "An" "Ap" [?A (meta return)] [?A delete]))
6437         (nosave-in-article
6438          '("AS" "\C-d"))
6439         (up-to-top
6440          '("n" "Gn" "p" "Gp"))
6441         keys new-sum-point)
6442     (with-current-buffer gnus-article-current-summary
6443       (let (gnus-pick-mode)
6444         (setq unread-command-events (nconc unread-command-events
6445                                            (list (or key last-command-event)))
6446               keys (if (featurep 'xemacs)
6447                        (events-to-keys (read-key-sequence nil t))
6448                      (read-key-sequence nil t)))))
6449
6450     (message "")
6451
6452     (cond
6453      ((eq (aref keys (1- (length keys))) ?\C-h)
6454       (gnus-article-describe-bindings (substring keys 0 -1)))
6455      ((or (member keys nosaves)
6456           (member keys nosave-but-article)
6457           (member keys nosave-in-article))
6458       (let (func)
6459         (save-window-excursion
6460           (pop-to-buffer gnus-article-current-summary)
6461           ;; We disable the pick minor mode commands.
6462           (let (gnus-pick-mode)
6463             (setq func (key-binding keys t))))
6464         (if (or (not func)
6465                 (numberp func))
6466             (ding)
6467           (unless (member keys nosave-in-article)
6468             (set-buffer gnus-article-current-summary))
6469           (call-interactively func)
6470           (setq new-sum-point (point)))
6471         (when (member keys nosave-but-article)
6472           (pop-to-buffer gnus-article-buffer))))
6473      (t
6474       ;; These commands should restore window configuration.
6475       (let ((obuf (current-buffer))
6476             (owin (current-window-configuration))
6477             win func in-buffer selected new-sum-start new-sum-hscroll err)
6478         (cond (not-restore-window
6479                (pop-to-buffer gnus-article-current-summary)
6480                (setq win (selected-window)))
6481               ((setq win (get-buffer-window gnus-article-current-summary))
6482                (select-window win))
6483               (t
6484                (let ((summary-buffer gnus-article-current-summary))
6485                  (gnus-configure-windows 'article)
6486                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6487                    (let ((gnus-buffer-configuration
6488                           '((article ((vertical 1.0
6489                                                 (summary 0.25 point)
6490                                                 (article 1.0)))))))
6491                      (gnus-configure-windows 'article))
6492                    (setq win (get-buffer-window summary-buffer 'visible)))
6493                  (gnus-select-frame-set-input-focus (window-frame win))
6494                  (select-window win))))
6495         (setq in-buffer (current-buffer))
6496         ;; We disable the pick minor mode commands.
6497         (if (and (setq func (let (gnus-pick-mode)
6498                               (key-binding keys t)))
6499                  (functionp func)
6500                  (condition-case code
6501                      (progn
6502                        (call-interactively func)
6503                        t)
6504                    (error
6505                     (setq err code)
6506                     nil)))
6507             (progn
6508               (when (eq win (selected-window))
6509                 (setq new-sum-point (point)
6510                       new-sum-start (window-start win)
6511                       new-sum-hscroll (window-hscroll win)))
6512               (when (or (eq in-buffer (current-buffer))
6513                         (when (eq obuf (current-buffer))
6514                           (set-buffer in-buffer)
6515                           t))
6516                 (setq selected (gnus-summary-select-article))
6517                 (set-buffer obuf)
6518                 (unless not-restore-window
6519                   (set-window-configuration owin))
6520                 (when (and (eq selected 'old)
6521                            new-sum-point)
6522                   (set-window-start (get-buffer-window (current-buffer))
6523                                     1)
6524                   (set-window-point (get-buffer-window (current-buffer))
6525                                     (if (article-goto-body)
6526                                         (1- (point))
6527                                       (point))))
6528                 (when (and (not not-restore-window)
6529                            new-sum-point
6530                            (window-live-p win)
6531                            (with-current-buffer (window-buffer win)
6532                              (eq major-mode 'gnus-summary-mode)))
6533                   (set-window-point win new-sum-point)
6534                   (set-window-start win new-sum-start)
6535                   (set-window-hscroll win new-sum-hscroll))))
6536           (set-window-configuration owin)
6537           (if err
6538               (signal (car err) (cdr err))
6539             (ding))))))))
6540
6541 (defun gnus-article-read-summary-send-keys ()
6542   (interactive)
6543   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6544     (gnus-article-read-summary-keys)))
6545
6546 (defun gnus-article-describe-key (key)
6547   "Display documentation of the function invoked by KEY.
6548 KEY is a string or a vector."
6549   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6550                        (read-key-sequence "Describe key: "))))
6551   (gnus-article-check-buffer)
6552   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6553                                   gnus-article-read-summary-send-keys))
6554       (with-current-buffer gnus-article-current-summary
6555         (setq unread-command-events
6556               (if (featurep 'xemacs)
6557                   (append key nil)
6558                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6559                                         (list 'meta (- x 128))
6560                                       x))
6561                         key)))
6562         (let ((cursor-in-echo-area t)
6563               gnus-pick-mode)
6564           (describe-key (read-key-sequence nil t))))
6565     (describe-key key)))
6566
6567 (defun gnus-article-describe-key-briefly (key &optional insert)
6568   "Display documentation of the function invoked by KEY.
6569 KEY is a string or a vector."
6570   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6571                        (read-key-sequence "Describe key: "))
6572                      current-prefix-arg))
6573   (gnus-article-check-buffer)
6574   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6575                                   gnus-article-read-summary-send-keys))
6576       (with-current-buffer gnus-article-current-summary
6577         (setq unread-command-events
6578               (if (featurep 'xemacs)
6579                   (append key nil)
6580                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6581                                         (list 'meta (- x 128))
6582                                       x))
6583                         key)))
6584         (let ((cursor-in-echo-area t)
6585               gnus-pick-mode)
6586           (describe-key-briefly (read-key-sequence nil t) insert)))
6587     (describe-key-briefly key insert)))
6588
6589 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6590 (defvar gnus-agent-summary-mode)
6591 (defvar gnus-draft-mode)
6592 ;; Calling help-buffer will autoload help-mode.
6593 (defvar help-xref-stack-item)
6594 ;; Emacs 22 doesn't load it in the batch mode.
6595 (eval-when-compile
6596   (autoload 'help-buffer "help-mode"))
6597
6598 (defun gnus-article-describe-bindings (&optional prefix)
6599   "Show a list of all defined keys, and their definitions.
6600 The optional argument PREFIX, if non-nil, should be a key sequence;
6601 then we display only bindings that start with that prefix."
6602   (interactive)
6603   (gnus-article-check-buffer)
6604   (let ((keymap (copy-keymap gnus-article-mode-map))
6605         (map (copy-keymap gnus-article-send-map))
6606         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6607         parent agent draft)
6608     (define-key keymap "S" map)
6609     (define-key map [t] nil)
6610     (with-current-buffer gnus-article-current-summary
6611       (set-keymap-parent
6612        keymap
6613        (if (setq parent (keymap-parent gnus-article-mode-map))
6614            (prog1
6615                (setq parent (copy-keymap parent))
6616              (set-keymap-parent parent (current-local-map)))
6617          (current-local-map)))
6618       (set-keymap-parent map (key-binding "S"))
6619       (let (key def gnus-pick-mode)
6620         (while sumkeys
6621           (setq key (pop sumkeys))
6622           (cond ((and (vectorp key) (= (length key) 1)
6623                       (consp (setq def (aref key 0)))
6624                       (numberp (car def)) (numberp (cdr def)))
6625                  (when (< (max (car def) (cdr def)) 128)
6626                    (setq sumkeys
6627                          (append (mapcar
6628                                   #'vector
6629                                   (nreverse (gnus-uncompress-range def)))
6630                                  sumkeys))))
6631                 ((setq def (key-binding key))
6632                  (unless (eq def 'undefined)
6633                    (define-key keymap key def))))))
6634       (when (boundp 'gnus-agent-summary-mode)
6635         (setq agent gnus-agent-summary-mode))
6636       (when (boundp 'gnus-draft-mode)
6637         (setq draft gnus-draft-mode)))
6638     (with-temp-buffer
6639       (use-local-map keymap)
6640       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6641       (set (make-local-variable 'gnus-draft-mode) draft)
6642       (describe-bindings prefix))
6643     (let ((item `((lambda (prefix)
6644                     (with-current-buffer ,(current-buffer)
6645                       (gnus-article-describe-bindings prefix)))
6646                   ,prefix)))
6647       (with-current-buffer (let (help-xref-following) (help-buffer))
6648         (setq help-xref-stack-item item)))))
6649
6650 (defun gnus-article-reply-with-original (&optional wide)
6651   "Start composing a reply mail to the current message.
6652 The text in the region will be yanked.  If the region isn't active,
6653 the entire article will be yanked."
6654   (interactive)
6655   (let ((article (cdr gnus-article-current))
6656         contents)
6657     (if (not (gnus-region-active-p))
6658         (with-current-buffer gnus-summary-buffer
6659           (gnus-summary-reply (list (list article)) wide))
6660       (setq contents (buffer-substring (point) (mark t)))
6661       ;; Deactivate active regions.
6662       (when (and (boundp 'transient-mark-mode)
6663                  transient-mark-mode)
6664         (setq mark-active nil))
6665       (with-current-buffer gnus-summary-buffer
6666         (gnus-summary-reply
6667          (list (list article contents)) wide)))))
6668
6669 (defun gnus-article-wide-reply-with-original ()
6670   "Start composing a wide reply mail to the current message.
6671 The text in the region will be yanked.  If the region isn't active,
6672 the entire article will be yanked."
6673   (interactive)
6674   (gnus-article-reply-with-original t))
6675
6676 (defun gnus-article-followup-with-original ()
6677   "Compose a followup to the current article.
6678 The text in the region will be yanked.  If the region isn't active,
6679 the entire article will be yanked."
6680   (interactive)
6681   (let ((article (cdr gnus-article-current))
6682         contents)
6683       (if (not (gnus-region-active-p))
6684           (with-current-buffer gnus-summary-buffer
6685             (gnus-summary-followup (list (list article))))
6686         (setq contents (buffer-substring (point) (mark t)))
6687         ;; Deactivate active regions.
6688         (when (and (boundp 'transient-mark-mode)
6689                    transient-mark-mode)
6690           (setq mark-active nil))
6691         (with-current-buffer gnus-summary-buffer
6692           (gnus-summary-followup
6693            (list (list article contents)))))))
6694
6695 (defun gnus-article-hide (&optional arg force)
6696   "Hide all the gruft in the current article.
6697 This means that signatures, cited text and (some) headers will be
6698 hidden.
6699 If given a prefix, show the hidden text instead."
6700   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6701   (gnus-article-hide-headers arg)
6702   (gnus-article-hide-list-identifiers arg)
6703   (gnus-article-hide-citation-maybe arg force)
6704   (gnus-article-hide-signature arg))
6705
6706 (defun gnus-article-maybe-highlight ()
6707   "Do some article highlighting if article highlighting is requested."
6708   (when (gnus-visual-p 'article-highlight 'highlight)
6709     (gnus-article-highlight-some)))
6710
6711 (defun gnus-check-group-server ()
6712   ;; Make sure the connection to the server is alive.
6713   (unless (gnus-server-opened
6714            (gnus-find-method-for-group gnus-newsgroup-name))
6715     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6716     (gnus-request-group gnus-newsgroup-name t)))
6717
6718 (eval-when-compile
6719   (autoload 'nneething-get-file-name "nneething"))
6720
6721 (defun gnus-request-article-this-buffer (article group)
6722   "Get an article and insert it into this buffer."
6723   (let (do-update-line sparse-header)
6724     (prog1
6725         (save-excursion
6726           (erase-buffer)
6727           (gnus-kill-all-overlays)
6728           (setq group (or group gnus-newsgroup-name))
6729
6730           ;; Using `gnus-request-article' directly will insert the article into
6731           ;; `nntp-server-buffer' - so we'll save some time by not having to
6732           ;; copy it from the server buffer into the article buffer.
6733
6734           ;; We only request an article by message-id when we do not have the
6735           ;; headers for it, so we'll have to get those.
6736           (when (stringp article)
6737             (gnus-read-header article))
6738
6739           ;; If the article number is negative, that means that this article
6740           ;; doesn't belong in this newsgroup (possibly), so we find its
6741           ;; message-id and request it by id instead of number.
6742           (when (and (numberp article)
6743                      gnus-summary-buffer
6744                      (get-buffer gnus-summary-buffer)
6745                      (gnus-buffer-exists-p gnus-summary-buffer))
6746             (with-current-buffer gnus-summary-buffer
6747               (let ((header (gnus-summary-article-header article)))
6748                 (when (< article 0)
6749                   (cond
6750                    ((memq article gnus-newsgroup-sparse)
6751                     ;; This is a sparse gap article.
6752                     (setq do-update-line article)
6753                     (setq article (mail-header-id header))
6754                     (setq sparse-header (gnus-read-header article))
6755                     (setq gnus-newsgroup-sparse
6756                           (delq article gnus-newsgroup-sparse)))
6757                    ((vectorp header)
6758                     ;; It's a real article.
6759                     (setq article (mail-header-id header)))
6760                    (t
6761                     ;; It is an extracted pseudo-article.
6762                     (setq article 'pseudo)
6763                     (gnus-request-pseudo-article header))))
6764
6765                 (let ((method (gnus-find-method-for-group
6766                                gnus-newsgroup-name)))
6767                   (when (and (eq (car method) 'nneething)
6768                              (vectorp header))
6769                     (let ((dir (nneething-get-file-name
6770                                 (mail-header-id header))))
6771                       (when (and (stringp dir)
6772                                  (file-directory-p dir))
6773                         (setq article 'nneething)
6774                         (gnus-group-enter-directory dir))))))))
6775
6776           (cond
6777            ;; Refuse to select canceled articles.
6778            ((and (numberp article)
6779                  gnus-summary-buffer
6780                  (get-buffer gnus-summary-buffer)
6781                  (gnus-buffer-exists-p gnus-summary-buffer)
6782                  (eq (cdr (with-current-buffer gnus-summary-buffer
6783                             (assq article gnus-newsgroup-reads)))
6784                      gnus-canceled-mark))
6785             nil)
6786            ;; We first check `gnus-original-article-buffer'.
6787            ((and (get-buffer gnus-original-article-buffer)
6788                  (numberp article)
6789                  (with-current-buffer gnus-original-article-buffer
6790                    (and (equal (car gnus-original-article) group)
6791                         (eq (cdr gnus-original-article) article))))
6792             ;; `insert-buffer-substring' would incorrectly use the
6793             ;; equivalent of string-make-multibyte which amount to decoding
6794             ;; with locale-coding-system, causing failure of
6795             ;; subsequent decoding.
6796             (insert (mm-string-to-multibyte
6797                      (with-current-buffer gnus-original-article-buffer
6798                        (buffer-substring (point-min) (point-max)))))
6799             'article)
6800            ;; Check the backlog.
6801            ((and gnus-keep-backlog
6802                  (gnus-backlog-request-article group article (current-buffer)))
6803             'article)
6804            ;; Check asynchronous pre-fetch.
6805            ((gnus-async-request-fetched-article group article (current-buffer))
6806             (gnus-async-prefetch-next group article gnus-summary-buffer)
6807             (when (and (numberp article) gnus-keep-backlog)
6808               (gnus-backlog-enter-article group article (current-buffer)))
6809             'article)
6810            ;; Check the cache.
6811            ((and gnus-use-cache
6812                  (numberp article)
6813                  (gnus-cache-request-article article group))
6814             'article)
6815            ;; Check the agent cache.
6816            ((gnus-agent-request-article article group)
6817             'article)
6818            ;; Get the article and put into the article buffer.
6819            ((or (stringp article)
6820                 (numberp article))
6821             (let ((gnus-override-method gnus-override-method)
6822                   (methods (and (stringp article)
6823                                 gnus-refer-article-method))
6824                   (backend (car (gnus-find-method-for-group
6825                                  gnus-newsgroup-name)))
6826                   result
6827                   (inhibit-read-only t))
6828               (if (or (not (listp methods))
6829                       (and (symbolp (car methods))
6830                            (assq (car methods) nnoo-definition-alist)))
6831                   (setq methods (list methods)))
6832               (when (and (null gnus-override-method)
6833                          methods)
6834                 (setq gnus-override-method (pop methods)))
6835               (while (not result)
6836                 (when (eq gnus-override-method 'current)
6837                   (setq gnus-override-method
6838                         (with-current-buffer gnus-summary-buffer
6839                           gnus-current-select-method)))
6840                 (erase-buffer)
6841                 (gnus-kill-all-overlays)
6842                 (let ((gnus-newsgroup-name group))
6843                   (gnus-check-group-server))
6844                 (cond
6845                  ((gnus-request-article article group (current-buffer))
6846                   (when (numberp article)
6847                     (gnus-async-prefetch-next group article
6848                                               gnus-summary-buffer)
6849                     (when gnus-keep-backlog
6850                       (gnus-backlog-enter-article
6851                        group article (current-buffer))))
6852                   (setq result 'article))
6853                  (methods
6854                   (setq gnus-override-method (pop methods)))
6855                  ((not (string-match "^400 "
6856                                      (nnheader-get-report backend)))
6857                   ;; If we get 400 server disconnect, reconnect and
6858                   ;; retry; otherwise, assume the article has expired.
6859                   (setq result 'done))))
6860               (and (eq result 'article) 'article)))
6861            ;; It was a pseudo.
6862            (t article)))
6863
6864       ;; Associate this article with the current summary buffer.
6865       (setq gnus-article-current-summary gnus-summary-buffer)
6866
6867       ;; Take the article from the original article buffer
6868       ;; and place it in the buffer it's supposed to be in.
6869       (when (and (get-buffer gnus-article-buffer)
6870                  (equal (buffer-name (current-buffer))
6871                         (buffer-name (get-buffer gnus-article-buffer))))
6872         (save-excursion
6873           (if (get-buffer gnus-original-article-buffer)
6874               (set-buffer gnus-original-article-buffer)
6875             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6876             (buffer-disable-undo)
6877             (setq major-mode 'gnus-original-article-mode)
6878             (setq buffer-read-only t))
6879           (let ((inhibit-read-only t))
6880             (erase-buffer)
6881             (insert-buffer-substring gnus-article-buffer))
6882           (setq gnus-original-article (cons group article)))
6883
6884         ;; Decode charsets.
6885         (run-hooks 'gnus-article-decode-hook)
6886         ;; Mark article as decoded or not.
6887         (setq gnus-article-decoded-p gnus-article-decode-hook))
6888
6889       ;; Update sparse articles.
6890       (when (and do-update-line
6891                  (or (numberp article)
6892                      (stringp article)))
6893         (let ((buf (current-buffer)))
6894           (set-buffer gnus-summary-buffer)
6895           (gnus-summary-update-article do-update-line sparse-header)
6896           (gnus-summary-goto-subject do-update-line nil t)
6897           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6898                             (point))
6899           (set-buffer buf))))))
6900
6901 ;;;
6902 ;;; Article editing
6903 ;;;
6904
6905 (defcustom gnus-article-edit-mode-hook nil
6906   "Hook run in article edit mode buffers."
6907   :group 'gnus-article-various
6908   :type 'hook)
6909
6910 (defvar gnus-article-edit-done-function nil)
6911
6912 (defvar gnus-article-edit-mode-map nil)
6913
6914 ;; Should we be using derived.el for this?
6915 (unless gnus-article-edit-mode-map
6916   (setq gnus-article-edit-mode-map (make-keymap))
6917   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6918
6919   (gnus-define-keys gnus-article-edit-mode-map
6920     "\C-c?"    describe-mode
6921     "\C-c\C-c" gnus-article-edit-done
6922     "\C-c\C-k" gnus-article-edit-exit
6923     "\C-c\C-f\C-t" message-goto-to
6924     "\C-c\C-f\C-o" message-goto-from
6925     "\C-c\C-f\C-b" message-goto-bcc
6926     ;;"\C-c\C-f\C-w" message-goto-fcc
6927     "\C-c\C-f\C-c" message-goto-cc
6928     "\C-c\C-f\C-s" message-goto-subject
6929     "\C-c\C-f\C-r" message-goto-reply-to
6930     "\C-c\C-f\C-n" message-goto-newsgroups
6931     "\C-c\C-f\C-d" message-goto-distribution
6932     "\C-c\C-f\C-f" message-goto-followup-to
6933     "\C-c\C-f\C-m" message-goto-mail-followup-to
6934     "\C-c\C-f\C-k" message-goto-keywords
6935     "\C-c\C-f\C-u" message-goto-summary
6936     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6937     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6938     "\C-c\C-b" message-goto-body
6939     "\C-c\C-i" message-goto-signature
6940
6941     "\C-c\C-t" message-insert-to
6942     "\C-c\C-n" message-insert-newsgroups
6943     "\C-c\C-o" message-sort-headers
6944     "\C-c\C-e" message-elide-region
6945     "\C-c\C-v" message-delete-not-region
6946     "\C-c\C-z" message-kill-to-signature
6947     "\M-\r" message-newline-and-reformat
6948     "\C-c\C-a" mml-attach-file
6949     "\C-a" message-beginning-of-line
6950     "\t" message-tab
6951     "\M-;" comment-region)
6952
6953   (gnus-define-keys (gnus-article-edit-wash-map
6954                      "\C-c\C-w" gnus-article-edit-mode-map)
6955     "f" gnus-article-edit-full-stops))
6956
6957 (easy-menu-define
6958   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6959   '("Field"
6960     ["Fetch To" message-insert-to t]
6961     ["Fetch Newsgroups" message-insert-newsgroups t]
6962     "----"
6963     ["To" message-goto-to t]
6964     ["From" message-goto-from t]
6965     ["Subject" message-goto-subject t]
6966     ["Cc" message-goto-cc t]
6967     ["Reply-To" message-goto-reply-to t]
6968     ["Summary" message-goto-summary t]
6969     ["Keywords" message-goto-keywords t]
6970     ["Newsgroups" message-goto-newsgroups t]
6971     ["Followup-To" message-goto-followup-to t]
6972     ["Mail-Followup-To" message-goto-mail-followup-to t]
6973     ["Distribution" message-goto-distribution t]
6974     ["Body" message-goto-body t]
6975     ["Signature" message-goto-signature t]))
6976
6977 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6978   "Major mode for editing articles.
6979 This is an extended text-mode.
6980
6981 \\{gnus-article-edit-mode-map}"
6982   (make-local-variable 'gnus-article-edit-done-function)
6983   (make-local-variable 'gnus-prev-winconf)
6984   (set (make-local-variable 'font-lock-defaults)
6985        '(message-font-lock-keywords t))
6986   (set (make-local-variable 'mail-header-separator) "")
6987   (set (make-local-variable 'gnus-article-edit-mode) t)
6988   (easy-menu-add message-mode-field-menu message-mode-map)
6989   (mml-mode)
6990   (setq buffer-read-only nil)
6991   (buffer-enable-undo)
6992   (widen))
6993
6994 (defun gnus-article-edit (&optional force)
6995   "Edit the current article.
6996 This will have permanent effect only in mail groups.
6997 If FORCE is non-nil, allow editing of articles even in read-only
6998 groups."
6999   (interactive "P")
7000   (when (and (not force)
7001              (gnus-group-read-only-p))
7002     (error "The current newsgroup does not support article editing"))
7003   (gnus-article-date-original)
7004   (gnus-article-edit-article
7005    'ignore
7006    `(lambda (no-highlight)
7007       'ignore
7008       (gnus-summary-edit-article-done
7009        ,(or (mail-header-references gnus-current-headers) "")
7010        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7011
7012 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7013   "Start editing the contents of the current article buffer."
7014   (let ((winconf (current-window-configuration)))
7015     (set-buffer gnus-article-buffer)
7016     (let ((message-auto-save-directory
7017            ;; Don't associate the article buffer with a draft file.
7018            nil))
7019       (gnus-article-edit-mode))
7020     (funcall start-func)
7021     (set-buffer-modified-p nil)
7022     (gnus-configure-windows 'edit-article)
7023     (setq gnus-article-edit-done-function exit-func)
7024     (setq gnus-prev-winconf winconf)
7025     (unless quiet
7026       (gnus-message 6 "C-c C-c to end edits"))))
7027
7028 (defun gnus-article-edit-done (&optional arg)
7029   "Update the article edits and exit."
7030   (interactive "P")
7031   (let ((func gnus-article-edit-done-function)
7032         (buf (current-buffer))
7033         (start (window-start))
7034         (p (point))
7035         (winconf gnus-prev-winconf))
7036     (widen) ;; Widen it in case that users narrowed the buffer.
7037     (funcall func arg)
7038     (set-buffer buf)
7039     ;; The cache and backlog have to be flushed somewhat.
7040     (when gnus-keep-backlog
7041       (gnus-backlog-remove-article
7042        (car gnus-article-current) (cdr gnus-article-current)))
7043     ;; Flush original article as well.
7044     (when (get-buffer gnus-original-article-buffer)
7045       (with-current-buffer gnus-original-article-buffer
7046         (setq gnus-original-article nil)))
7047     (when gnus-use-cache
7048       (gnus-cache-update-article
7049        (car gnus-article-current) (cdr gnus-article-current)))
7050     ;; We remove all text props from the article buffer.
7051     (kill-all-local-variables)
7052     (set-text-properties (point-min) (point-max) nil)
7053     (gnus-article-mode)
7054     (set-window-configuration winconf)
7055     (set-buffer buf)
7056     (set-window-start (get-buffer-window buf) start)
7057     (set-window-point (get-buffer-window buf) (point)))
7058   (gnus-summary-show-article))
7059
7060 (defun gnus-article-edit-exit ()
7061   "Exit the article editing without updating."
7062   (interactive)
7063   (when (or (not (buffer-modified-p))
7064             (yes-or-no-p "Article modified; kill anyway? "))
7065     (let ((curbuf (current-buffer))
7066           (p (point))
7067           (window-start (window-start)))
7068       (erase-buffer)
7069       (if (gnus-buffer-live-p gnus-original-article-buffer)
7070           (insert-buffer-substring gnus-original-article-buffer))
7071       (let ((winconf gnus-prev-winconf))
7072         (kill-all-local-variables)
7073         (gnus-article-mode)
7074         (set-window-configuration winconf)
7075         ;; Tippy-toe some to make sure that point remains where it was.
7076         (save-current-buffer
7077           (set-buffer curbuf)
7078           (set-window-start (get-buffer-window (current-buffer)) window-start)
7079           (goto-char p))))
7080     (gnus-summary-show-article)))
7081
7082 (defun gnus-article-edit-full-stops ()
7083   "Interactively repair spacing at end of sentences."
7084   (interactive)
7085   (save-excursion
7086     (goto-char (point-min))
7087     (search-forward-regexp "^$" nil t)
7088     (let ((case-fold-search nil))
7089       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7090
7091 ;;;
7092 ;;; Article highlights
7093 ;;;
7094
7095 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7096
7097 ;;; Internal Variables:
7098
7099 (defcustom gnus-button-url-regexp
7100   (concat
7101    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7102    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7103    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7104    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7105        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7106              (punct "!?:;.,"))
7107          (concat
7108           "\\(?:"
7109           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7110           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7111           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7112           "\\|"
7113           "[" chars punct     "]+" "[" chars "]"
7114           "\\)"))
7115      (concat ;; XEmacs 21.4 doesn't support POSIX.
7116       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7117       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7118    "\\)")
7119   "Regular expression that matches URLs."
7120   :group 'gnus-article-buttons
7121   :type 'regexp)
7122
7123 (defcustom gnus-button-valid-fqdn-regexp
7124   message-valid-fqdn-regexp
7125   "Regular expression that matches a valid FQDN."
7126   :version "22.1"
7127   :group 'gnus-article-buttons
7128   :type 'regexp)
7129
7130 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7131 (defcustom gnus-button-valid-localpart-regexp
7132   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7133   "Regular expression that matches a localpart of mail addresses or MIDs."
7134   :version "22.1"
7135   :group 'gnus-article-buttons
7136   :type 'regexp)
7137
7138 (defcustom gnus-button-man-handler 'manual-entry
7139   "Function to use for displaying man pages.
7140 The function must take at least one argument with a string naming the
7141 man page."
7142   :version "22.1"
7143   :type '(choice (function-item :tag "Man" manual-entry)
7144                  (function-item :tag "Woman" woman)
7145                  (function :tag "Other"))
7146   :group 'gnus-article-buttons)
7147
7148 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
7149   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
7150 If the default site is too slow, try to find a CTAN mirror, see
7151 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
7152 the variable `gnus-button-handle-ctan'."
7153   :version "22.1"
7154   :group 'gnus-article-buttons
7155   :link '(custom-manual "(gnus)Group Parameters")
7156   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
7157                  (const "http://tug.ctan.org/tex-archive/")
7158                  (const "http://www.dante.de/CTAN/")
7159                  (string :tag "Other")))
7160
7161 (defcustom gnus-button-ctan-handler 'browse-url
7162   "Function to use for displaying CTAN links.
7163 The function must take one argument, the string naming the URL."
7164   :version "22.1"
7165   :type '(choice (function-item :tag "Browse Url" browse-url)
7166                  (function :tag "Other"))
7167   :group 'gnus-article-buttons)
7168
7169 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
7170   "Bogus strings removed from CTAN URLs."
7171   :version "22.1"
7172   :group 'gnus-article-buttons
7173   :type '(choice (const "^/?tex-archive/\\|/")
7174                  (regexp :tag "Other")))
7175
7176 (defcustom gnus-button-ctan-directory-regexp
7177   (regexp-opt
7178    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
7179          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
7180          "languages" "macros" "nonfree" "obsolete" "support" "systems"
7181          "tds" "tools" "usergrps" "web") t)
7182   "Regular expression for ctan directories.
7183 It should match all directories in the top level of `gnus-ctan-url'."
7184   :version "22.1"
7185   :group 'gnus-article-buttons
7186   :type 'regexp)
7187
7188 (defcustom gnus-button-mid-or-mail-regexp
7189   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7190           gnus-button-valid-fqdn-regexp
7191           ">?\\)\\b")
7192   "Regular expression that matches a message ID or a mail address."
7193   :version "22.1"
7194   :group 'gnus-article-buttons
7195   :type 'regexp)
7196
7197 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7198   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7199 Strings like this can be either a message ID or a mail address.  If it is one
7200 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7201 message ID or a mail address, respectively.  If this variable is set to the
7202 symbol `ask', always query the user what do do.  If it is a function, this
7203 function will be called with the string as its only argument.  The function
7204 must return `mid', `mail', `invalid' or `ask'."
7205   :version "22.1"
7206   :group 'gnus-article-buttons
7207   :type '(choice (function-item :tag "Heuristic function"
7208                                 gnus-button-mid-or-mail-heuristic)
7209                  (const ask)
7210                  (const mid)
7211                  (const mail)))
7212
7213 (defcustom gnus-button-mid-or-mail-heuristic-alist
7214   '((-10.0 . ".+\\$.+@")
7215     (-10.0 . "#")
7216     (-10.0 . "\\*")
7217     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7218     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7219     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7220     (-1.0  . "^[^a-z]+@")
7221     ;;
7222     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7223     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7224     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7225     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7226     ;;
7227     (-2.0  . "^[0-9]")
7228     (-1.0  . "^[0-9][0-9]")
7229     ;;
7230     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7231     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7232     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7233     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7234     ;;
7235     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7236     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7237     ;;       "[0-9]{8,}.*\@"
7238     (-3.0
7239      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7240     ;; "[0-9]{12,}.*\@"
7241     ;; compensation for TDMA dated mail addresses:
7242     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7243     ;;
7244     (-20.0 . "\\.fsf@") ;; Gnus
7245     (-20.0 . "^slrn")
7246     (-20.0 . "^Pine")
7247     (-20.0 . "^alpine\\.")
7248     (-20.0 . "_-_") ;; Subject change in thread
7249     ;;
7250     (-20.0 . "\\.ln@") ;; leafnode
7251     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7252     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7253     ;;
7254     ;; (5.0 . "") ;; $local_part_len <= 7
7255     (10.0  . "^[^0-9]+@")
7256     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7257     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7258     (3.0   . "\@stud")
7259     ;;
7260     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7261     ;;
7262     (0.5   . "^[A-Z][a-z]")
7263     (0.5   . "^[A-Z][a-z][a-z]")
7264     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7265     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7266   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7267
7268 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7269 address.  The REGEXP is processed with `case-fold-search' set to nil."
7270   :version "22.1"
7271   :group 'gnus-article-buttons
7272   :type '(repeat (cons (number :tag "Rate")
7273                        (regexp :tag "Regexp"))))
7274
7275 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7276   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7277 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7278 address, `ask' if unsure and `invalid' if the string is invalid."
7279   (let ((case-fold-search nil)
7280         (list gnus-button-mid-or-mail-heuristic-alist)
7281         (result 0) rate regexp lpartlen elem)
7282     (setq lpartlen
7283           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7284     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7285     ;; Certain special cases...
7286     (when (string-match
7287            (concat
7288             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7289             "^[0-9]+\\.[0-9]+@compuserve\\|"
7290             "@public\\.gmane\\.org")
7291            mid-or-mail)
7292       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7293       (setq result 'mail))
7294     (when (string-match "@.*@\\| " mid-or-mail)
7295       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7296       (setq result 'invalid))
7297     ;; Nothing more to do, if result is not a number here...
7298     (when (numberp result)
7299       (while list
7300         (setq elem (car list)
7301               rate (car elem)
7302               regexp (cdr elem)
7303               list (cdr list))
7304         (when (string-match regexp mid-or-mail)
7305           (setq result (+ result rate))
7306           (gnus-message
7307            9 "`%s' matched `%s', rate `%s', result `%s'."
7308            mid-or-mail regexp rate result)))
7309       (when (<= lpartlen 7)
7310         (setq result (+ result 5.0))
7311         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7312                       mid-or-mail result))
7313       (when (>= lpartlen 12)
7314         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7315         (cond
7316          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7317           ;; Long local part should contain realname if e-mail address,
7318           ;; too many digits: message-id.
7319           ;; $score -= 5.0 + 0.1 * $local_part_len;
7320           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7321           (setq result (+ result rate))
7322           (gnus-message
7323            9 "Many digits in `%s', rate `%s', result `%s'."
7324            mid-or-mail rate result))
7325          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7326                         mid-or-mail)
7327           ;; Too few vowels [^aeiouy]{4,}.*\@
7328           (setq result (+ result -5.0))
7329           (gnus-message
7330            9 "Few vowels in `%s', rate `%s', result `%s'."
7331            mid-or-mail -5.0 result))
7332          (t
7333           (setq result (+ result 5.0))
7334           (gnus-message
7335            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7336     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7337     ;; Maybe we should make this a customizable alist: (condition . 'result)
7338     (cond
7339      ((symbolp result) result)
7340      ;; Now convert number into proper results:
7341      ((< result -10.0) 'mid)
7342      ((> result  10.0) 'mail)
7343      (t 'ask))))
7344
7345 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7346   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7347          (url-mid (concat "news" ":" mid-or-mail))
7348          (url-mailto (concat "mailto" ":" mid-or-mail)))
7349     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7350     (when (fboundp pref)
7351       (setq guessed
7352             ;; get rid of surrounding angles...
7353             (funcall pref
7354                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7355       (if (or (eq 'mid guessed) (eq 'mail guessed))
7356           (setq pref guessed)
7357         (setq pref 'ask)))
7358     (if (eq pref 'ask)
7359         (save-window-excursion
7360           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7361               (setq pref 'mail)
7362             (setq pref 'mid))))
7363     (cond ((eq pref 'mid)
7364            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7365            (gnus-button-handle-news url-mid))
7366           ((eq pref 'mail)
7367            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7368            (gnus-url-mailto url-mailto))
7369           (t (gnus-message 3 "Invalid string.")))))
7370
7371 (defun gnus-button-handle-custom (fun arg)
7372   "Call function FUN on argument ARG.
7373 Both FUN and ARG are supposed to be strings.  ARG will be passed
7374 as a symbol to FUN."
7375   (funcall (intern fun)
7376            (if (string-match "^customize-apropos" fun)
7377                arg
7378              (intern arg))))
7379
7380 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7381
7382 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7383 ;; stuff?
7384
7385 (defun gnus-button-handle-describe-function (url)
7386   "Call `describe-function' when pushing the corresponding URL button."
7387   (describe-function
7388    (intern
7389     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7390
7391 (defun gnus-button-handle-describe-variable (url)
7392   "Call `describe-variable' when pushing the corresponding URL button."
7393   (describe-variable
7394    (intern
7395     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7396
7397 (defun gnus-button-handle-symbol (url)
7398 "Display help on variable or function.
7399 Calls `describe-variable' or `describe-function'."
7400   (let ((sym (intern url)))
7401     (cond
7402      ((fboundp sym) (describe-function sym))
7403      ((boundp sym) (describe-variable sym))
7404      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7405
7406 (defun gnus-button-handle-describe-key (url)
7407   "Call `describe-key' when pushing the corresponding URL button."
7408   (let* ((key-string
7409           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7410          (keys (ignore-errors (eval `(kbd ,key-string)))))
7411     (if keys
7412         (describe-key keys)
7413       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7414
7415 (defun gnus-button-handle-apropos (url)
7416   "Call `apropos' when pushing the corresponding URL button."
7417   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7418
7419 (defun gnus-button-handle-apropos-command (url)
7420   "Call `apropos' when pushing the corresponding URL button."
7421   (apropos-command
7422    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7423
7424 (defun gnus-button-handle-apropos-variable (url)
7425   "Call `apropos' when pushing the corresponding URL button."
7426   (funcall
7427    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7428    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7429
7430 (defun gnus-button-handle-apropos-documentation (url)
7431   "Call `apropos' when pushing the corresponding URL button."
7432   (funcall
7433    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7434    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7435
7436 (defun gnus-button-handle-library (url)
7437   "Call `locate-library' when pushing the corresponding URL button."
7438   (gnus-message 9 "url=`%s'" url)
7439   (let* ((lib (locate-library url))
7440          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7441     (if (not lib)
7442         (gnus-message 1 "Cannot locale library `%s'." url)
7443       (find-file-read-only file))))
7444
7445 (defun gnus-button-handle-ctan (url)
7446   "Call `browse-url' when pushing a CTAN URL button."
7447   (funcall
7448    gnus-button-ctan-handler
7449    (concat
7450     gnus-ctan-url
7451     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7452
7453 (defcustom gnus-button-tex-level 5
7454   "*Integer that says how many TeX-related buttons Gnus will show.
7455 The higher the number, the more buttons will appear and the more false
7456 positives are possible.  Note that you can set this variable local to
7457 specific groups.  Setting it higher in TeX groups is probably a good idea.
7458 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7459 how to set variables in specific groups."
7460   :version "22.1"
7461   :group 'gnus-article-buttons
7462   :link '(custom-manual "(gnus)Group Parameters")
7463   :type 'integer)
7464
7465 (defcustom gnus-button-man-level 5
7466   "*Integer that says how many man-related buttons Gnus will show.
7467 The higher the number, the more buttons will appear and the more false
7468 positives are possible.  Note that you can set this variable local to
7469 specific groups.  Setting it higher in Unix groups is probably a good idea.
7470 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7471 how to set variables in specific groups."
7472   :version "22.1"
7473   :group 'gnus-article-buttons
7474   :link '(custom-manual "(gnus)Group Parameters")
7475   :type 'integer)
7476
7477 (defcustom gnus-button-emacs-level 5
7478   "*Integer that says how many emacs-related buttons Gnus will show.
7479 The higher the number, the more buttons will appear and the more false
7480 positives are possible.  Note that you can set this variable local to
7481 specific groups.  Setting it higher in Emacs or Gnus related groups is
7482 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7483 `gnus-parameters' on how to set variables in specific groups."
7484   :version "22.1"
7485   :group 'gnus-article-buttons
7486   :link '(custom-manual "(gnus)Group Parameters")
7487   :type 'integer)
7488
7489 (defcustom gnus-button-message-level 5
7490   "*Integer that says how many buttons for news or mail messages will appear.
7491 The higher the number, the more buttons will appear and the more false
7492 positives are possible."
7493   ;; mail addresses, MIDs, URLs for news, ...
7494   :version "22.1"
7495   :group 'gnus-article-buttons
7496   :type 'integer)
7497
7498 (defcustom gnus-button-browse-level 5
7499   "*Integer that says how many buttons for browsing will appear.
7500 The higher the number, the more buttons will appear and the more false
7501 positives are possible."
7502   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7503   :version "22.1"
7504   :group 'gnus-article-buttons
7505   :type 'integer)
7506
7507 (defcustom gnus-button-alist
7508   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7509      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7510     ((concat "\\b\\(nntp\\|news\\):\\("
7511              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7512      0 t gnus-button-handle-news 2)
7513     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7514      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7515     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7516      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7517     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7518     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7519      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7520     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7521      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7522     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7523      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7524     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7525      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7526     ;; RFC 2368 (The mailto URL scheme)
7527     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7528      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7529     ("\\bmailto:\\([^ \n\t]+\\)"
7530      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7531     ;; CTAN
7532     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7533              gnus-button-ctan-directory-regexp
7534              "[^][>)!;:,'\n\t ]+\\)")
7535      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7536     ((concat "\\btex-archive/\\("
7537              gnus-button-ctan-directory-regexp
7538              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7539      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7540     ((concat
7541       "\\b\\("
7542       gnus-button-ctan-directory-regexp
7543       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7544      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7545     ;; Info Konqueror style <info:/foo/bar baz>.
7546     ;; Must come before " Gnus home-grown style".
7547     ("\\binfo://?\\([^'\">\n\t]+\\)"
7548      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7549    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7550     ("\\binfo://\\([^'\">\n\t ]+\\)"
7551      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7552     ;; Info GNOME style <info:foo#bar_baz>
7553     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7554      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7555     ;; Info KDE style <info:(foo)bar baz>
7556     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7557      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7558     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7559      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7560     ("\\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,]*\\)\\)?"
7561      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7563     ;; This is custom
7564     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7565      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7566     ;; Emacs help commands
7567     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7568      ;; regexp doesn't match arguments containing ` '.
7569      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7570     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7571      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7572     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7573      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7574     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7575      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7576     ;; The following entries may lead to many false positives so don't enable
7577     ;; them by default (use a high button level).
7578     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7579      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7580      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7581     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7582      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7583     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7584      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7585     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7586      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7587     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7588      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7589     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7590      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7591     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7592      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7593     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7594      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7595     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7596      ;; Unlike the other regexps we really have to require quoting
7597      ;; here to determine where it ends.
7598      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7599     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7600     ("<URL: *\\([^\n<>]*\\)>"
7601      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7602     ;; RFC 2396 (2.4.3., delims) ...
7603     ("\"URL: *\\([^\n\"]*\\)\""
7604      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7605     ;; Raw URLs.
7606     (gnus-button-url-regexp
7607      0 (>= gnus-button-browse-level 0) browse-url 0)
7608     ;; man pages
7609     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7610      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7611      gnus-button-handle-man 1)
7612     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7613     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7614      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7615      gnus-button-handle-man 1)
7616     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7617     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7618     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7619      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7620     ;; Recognizing patches to .el files.  This is somewhat obscure,
7621     ;; but considering the percentage of Gnus users who hack Emacs
7622     ;; Lisp files...
7623     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7624      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7625     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7626      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7627     ;; MID or mail: To avoid too many false positives we don't try to catch
7628     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7629     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7630     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7631     ;; so that non-ambiguous entries (see above) match first.
7632     (gnus-button-mid-or-mail-regexp
7633      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7634   "*Alist of regexps matching buttons in article bodies.
7635
7636 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7637 REGEXP: is the string (case insensitive) matching text around the button (can
7638 also be Lisp expression evaluating to a string),
7639 BUTTON: is the number of the regexp grouping actually matching the button,
7640 FORM: is a Lisp expression which must eval to true for the button to
7641 be added,
7642 CALLBACK: is the function to call when the user push this button, and each
7643 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7644
7645 CALLBACK can also be a variable, in that case the value of that
7646 variable it the real callback function."
7647   :group 'gnus-article-buttons
7648   :type '(repeat (list (choice regexp variable sexp)
7649                        (integer :tag "Button")
7650                        (sexp :tag "Form")
7651                        (function :tag "Callback")
7652                        (repeat :tag "Par"
7653                                :inline t
7654                                (integer :tag "Regexp group")))))
7655 (put 'gnus-button-alist 'risky-local-variable t)
7656
7657 (defcustom gnus-header-button-alist
7658   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7659      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7660     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7661      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7662     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7663      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7664     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7665      0 (>= gnus-button-browse-level 0) browse-url 0)
7666     ("^Subject:" gnus-button-url-regexp
7667      0 (>= gnus-button-browse-level 0) browse-url 0)
7668     ("^[^:]+:" gnus-button-url-regexp
7669      0 (>= gnus-button-browse-level 0) browse-url 0)
7670     ("^OpenPGP:.*url=" gnus-button-url-regexp
7671      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7672     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7673      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7674     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7675      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7676   "*Alist of headers and regexps to match buttons in article heads.
7677
7678 This alist is very similar to `gnus-button-alist', except that each
7679 alist has an additional HEADER element first in each entry:
7680
7681 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7682
7683 HEADER is a regexp to match a header.  For a fuller explanation, see
7684 `gnus-button-alist'."
7685   :group 'gnus-article-buttons
7686   :group 'gnus-article-headers
7687   :type '(repeat (list (regexp :tag "Header")
7688                        (choice regexp variable)
7689                        (integer :tag "Button")
7690                        (sexp :tag "Form")
7691                        (function :tag "Callback")
7692                        (repeat :tag "Par"
7693                                :inline t
7694                                (integer :tag "Regexp group")))))
7695 (put 'gnus-header-button-alist 'risky-local-variable t)
7696
7697 ;;; Commands:
7698
7699 (defun gnus-article-push-button (event)
7700   "Check text under the mouse pointer for a callback function.
7701 If the text under the mouse pointer has a `gnus-callback' property,
7702 call it with the value of the `gnus-data' text property."
7703   (interactive "e")
7704   (set-buffer (window-buffer (posn-window (event-start event))))
7705   (let* ((pos (posn-point (event-start event)))
7706          (data (get-text-property pos 'gnus-data))
7707          (fun (get-text-property pos 'gnus-callback)))
7708     (goto-char pos)
7709     (when fun
7710       (funcall fun data))))
7711
7712 (defun gnus-article-press-button ()
7713   "Check text at point for a callback function.
7714 If the text at point has a `gnus-callback' property,
7715 call it with the value of the `gnus-data' text property."
7716   (interactive)
7717   (let ((data (get-text-property (point) 'gnus-data))
7718         (fun (get-text-property (point) 'gnus-callback)))
7719     (when fun
7720       (funcall fun data))))
7721
7722 (defun gnus-article-highlight (&optional force)
7723   "Highlight current article.
7724 This function calls `gnus-article-highlight-headers',
7725 `gnus-article-highlight-citation',
7726 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7727 do the highlighting.  See the documentation for those functions."
7728   (interactive (list 'force))
7729   (gnus-article-highlight-headers)
7730   (gnus-article-highlight-citation force)
7731   (gnus-article-highlight-signature)
7732   (gnus-article-add-buttons force)
7733   (gnus-article-add-buttons-to-head))
7734
7735 (defun gnus-article-highlight-some (&optional force)
7736   "Highlight current article.
7737 This function calls `gnus-article-highlight-headers',
7738 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7739 do the highlighting.  See the documentation for those functions."
7740   (interactive (list 'force))
7741   (gnus-article-highlight-headers)
7742   (gnus-article-highlight-signature)
7743   (gnus-article-add-buttons))
7744
7745 (defun gnus-article-highlight-headers ()
7746   "Highlight article headers as specified by `gnus-header-face-alist'."
7747   (interactive)
7748   (gnus-with-article-headers
7749     (let (regexp header-face field-face from hpoints fpoints)
7750       (dolist (entry gnus-header-face-alist)
7751         (goto-char (point-min))
7752         (setq regexp (concat "^\\("
7753                              (if (string-equal "" (nth 0 entry))
7754                                  "[^\t ]"
7755                                (nth 0 entry))
7756                              "\\)")
7757               header-face (nth 1 entry)
7758               field-face (nth 2 entry))
7759         (while (and (re-search-forward regexp nil t)
7760                     (not (eobp)))
7761           (beginning-of-line)
7762           (setq from (point))
7763           (unless (search-forward ":" nil t)
7764             (forward-char 1))
7765           (when (and header-face
7766                      (not (memq (point) hpoints)))
7767             (push (point) hpoints)
7768             (gnus-put-text-property from (point) 'face header-face))
7769           (when (and field-face
7770                      (not (memq (setq from (point)) fpoints)))
7771             (push from fpoints)
7772             (if (re-search-forward "^[^ \t]" nil t)
7773                 (forward-char -2)
7774               (goto-char (point-max)))
7775             (gnus-put-text-property from (point) 'face field-face)))))))
7776
7777 (defun gnus-article-highlight-signature ()
7778   "Highlight the signature in an article.
7779 It does this by highlighting everything after
7780 `gnus-signature-separator' using the face `gnus-signature'."
7781   (interactive)
7782   (gnus-with-article-buffer
7783     (let ((inhibit-point-motion-hooks t))
7784       (save-restriction
7785         (when (and gnus-signature-face
7786                    (gnus-article-narrow-to-signature))
7787           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7788                             'face gnus-signature-face)
7789           (widen)
7790           (gnus-article-search-signature)
7791           (let ((start (match-beginning 0))
7792                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7793             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7794                                      end)))))))
7795
7796 (defun gnus-button-in-region-p (b e prop)
7797   "Say whether PROP exists in the region."
7798   (text-property-not-all b e prop nil))
7799
7800 (defun gnus-article-add-buttons (&optional force)
7801   "Find external references in the article and make buttons of them.
7802 \"External references\" are things like Message-IDs and URLs, as
7803 specified by `gnus-button-alist'."
7804   (interactive (list 'force))
7805   (gnus-with-article-buffer
7806     (let ((inhibit-point-motion-hooks t)
7807           (case-fold-search t)
7808           (alist gnus-button-alist)
7809           beg entry regexp)
7810       ;; Remove all old markers.
7811       (let (marker entry new-list)
7812         (while (setq marker (pop gnus-button-marker-list))
7813           (if (or (< marker (point-min)) (>= marker (point-max)))
7814               (push marker new-list)
7815             (goto-char marker)
7816             (when (setq entry (gnus-button-entry))
7817               (put-text-property (match-beginning (nth 1 entry))
7818                                  (match-end (nth 1 entry))
7819                                  'gnus-callback nil))
7820             (set-marker marker nil)))
7821         (setq gnus-button-marker-list new-list))
7822       ;; We skip the headers.
7823       (article-goto-body)
7824       (setq beg (point))
7825       (while (setq entry (pop alist))
7826         (setq regexp (eval (car entry)))
7827         (goto-char beg)
7828         (while (re-search-forward regexp nil t)
7829           (let ((start (match-beginning (nth 1 entry)))
7830                 (end (match-end (nth 1 entry)))
7831                 (from (match-beginning 0)))
7832             (when (and (or (eq t (nth 2 entry))
7833                            (eval (nth 2 entry)))
7834                        (not (gnus-button-in-region-p
7835                              start end 'gnus-callback)))
7836               ;; That optional form returned non-nil, so we add the
7837               ;; button.
7838               (setq from (set-marker (make-marker) from))
7839               (push from gnus-button-marker-list)
7840               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7841                            (gnus-article-extend-url-button from start end))
7842                 (gnus-article-add-button start end
7843                                          'gnus-button-push from)
7844                 (gnus-put-text-property
7845                  start end
7846                  'gnus-string (buffer-substring-no-properties
7847                                start end))))))))))
7848
7849 (defun gnus-article-extend-url-button (beg start end)
7850   "Extend url button if url is folded into two or more lines.
7851 Return non-nil if button is extended.  BEG is a marker that points to
7852 the beginning position of a text containing url.  START and END are
7853 the endpoints of a url button before it is extended.  The concatenated
7854 url is put as the `gnus-button-url' overlay property on the button."
7855   (let ((opoint (point))
7856         (points (list start end))
7857         url delim regexp)
7858     (prog1
7859         (when (and (progn
7860                      (goto-char end)
7861                      (not (looking-at "[\t ]*[\">]")))
7862                    (progn
7863                      (goto-char start)
7864                      (string-match
7865                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7866                       (buffer-substring (point-at-bol) start)))
7867                    (progn
7868                      (setq url (list (buffer-substring start end))
7869                            delim (if (match-beginning 1) ">" "\""))
7870                      (beginning-of-line)
7871                      (setq regexp (concat
7872                                    (when (and (looking-at
7873                                                message-cite-prefix-regexp)
7874                                               (< (match-end 0) start))
7875                                      (regexp-quote (match-string 0)))
7876                                    "\
7877 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7878                                    delim "\\)"))
7879                      (while (progn
7880                               (forward-line 1)
7881                               (and (looking-at regexp)
7882                                    (prog1
7883                                        (match-beginning 1)
7884                                      (push (or (match-string 2)
7885                                                (match-string 1))
7886                                            url)
7887                                      (push (setq end (or (match-end 2)
7888                                                          (match-end 1)))
7889                                            points)
7890                                      (push (or (match-beginning 2)
7891                                                (match-beginning 1))
7892                                            points)))))
7893                      (match-beginning 2)))
7894           (let (gnus-article-mouse-face widget-mouse-face)
7895             (while points
7896               (gnus-article-add-button (pop points) (pop points)
7897                                        'gnus-button-push beg)))
7898           (let ((overlay (gnus-make-overlay start end)))
7899             (gnus-overlay-put overlay 'evaporate t)
7900             (gnus-overlay-put overlay 'gnus-button-url
7901                               (list (mapconcat 'identity (nreverse url) "")))
7902             (when gnus-article-mouse-face
7903               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7904           t)
7905       (goto-char opoint))))
7906
7907 ;; Add buttons to the head of an article.
7908 (defun gnus-article-add-buttons-to-head ()
7909   "Add buttons to the head of the article."
7910   (interactive)
7911   (gnus-with-article-headers
7912     (let (beg end)
7913       (dolist (entry gnus-header-button-alist)
7914         ;; Each alist entry.
7915         (goto-char (point-min))
7916         (while (re-search-forward (car entry) nil t)
7917           ;; Each header matching the entry.
7918           (setq beg (match-beginning 0))
7919           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7920                              (match-beginning 0))
7921                         (point-max)))
7922           (goto-char beg)
7923           (while (re-search-forward (eval (nth 1 entry)) end t)
7924             ;; Each match within a header.
7925             (let* ((entry (cdr entry))
7926                    (start (match-beginning (nth 1 entry)))
7927                    (end (match-end (nth 1 entry)))
7928                    (form (nth 2 entry)))
7929               (goto-char (match-end 0))
7930               (when (eval form)
7931                 (gnus-article-add-button
7932                  start end (nth 3 entry)
7933                  (buffer-substring (match-beginning (nth 4 entry))
7934                                    (match-end (nth 4 entry)))))))
7935           (goto-char end))))))
7936
7937 ;;; External functions:
7938
7939 (defun gnus-article-add-button (from to fun &optional data text)
7940   "Create a button between FROM and TO with callback FUN and data DATA."
7941   (when gnus-article-button-face
7942     (gnus-overlay-put (gnus-make-overlay from to nil t)
7943                       'face gnus-article-button-face))
7944   (gnus-add-text-properties
7945    from to
7946    (nconc (and gnus-article-mouse-face
7947                (list gnus-mouse-face-prop gnus-article-mouse-face))
7948           (list 'gnus-callback fun)
7949           (and data (list 'gnus-data data))))
7950   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7951                          :help-echo (or text "Follow the link")
7952                          :keymap gnus-url-button-map
7953                          :button-keymap gnus-widget-button-keymap))
7954
7955 (defun gnus-article-copy-string ()
7956   "Copy the string in the button to the kill ring."
7957   (interactive)
7958   (gnus-article-check-buffer)
7959   (let ((data (get-text-property (point) 'gnus-string)))
7960     (when data
7961       (with-temp-buffer
7962         (insert data)
7963         (copy-region-as-kill (point-min) (point-max))
7964         (message "Copied %s" data)))))
7965
7966 ;;; Internal functions:
7967
7968 (defun gnus-article-set-globals ()
7969   (with-current-buffer gnus-summary-buffer
7970     (gnus-set-global-variables)))
7971
7972 (defun gnus-signature-toggle (end)
7973   (gnus-with-article-buffer
7974     (let ((inhibit-point-motion-hooks t))
7975       (if (text-property-any end (point-max) 'article-type 'signature)
7976           (progn
7977             (gnus-delete-wash-type 'signature)
7978             (gnus-remove-text-properties-when
7979              'article-type 'signature end (point-max)
7980              (cons 'article-type (cons 'signature
7981                                        gnus-hidden-properties))))
7982         (gnus-add-wash-type 'signature)
7983         (gnus-add-text-properties-when
7984          'article-type nil end (point-max)
7985          (cons 'article-type (cons 'signature
7986                                    gnus-hidden-properties)))))
7987     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7988       (gnus-set-mode-line 'article))))
7989
7990 (defun gnus-button-entry ()
7991   ;; Return the first entry in `gnus-button-alist' matching this place.
7992   (let ((alist gnus-button-alist)
7993         (entry nil))
7994     (while alist
7995       (setq entry (pop alist))
7996       (if (looking-at (eval (car entry)))
7997           (setq alist nil)
7998         (setq entry nil)))
7999     entry))
8000
8001 (defun gnus-button-push (marker)
8002   ;; Push button starting at MARKER.
8003   (save-excursion
8004     (goto-char marker)
8005     (let* ((entry (gnus-button-entry))
8006            (inhibit-point-motion-hooks t)
8007            (fun (nth 3 entry))
8008            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8009                           (get-char-property marker 'gnus-button-url))
8010                      (mapcar (lambda (group)
8011                                (let ((string (match-string group)))
8012                                  (set-text-properties
8013                                   0 (length string) nil string)
8014                                  string))
8015                              (nthcdr 4 entry)))))
8016       (cond
8017        ((fboundp fun)
8018         (apply fun args))
8019        ((and (boundp fun)
8020              (fboundp (symbol-value fun)))
8021         (apply (symbol-value fun) args))
8022        (t
8023         (gnus-message 1 "You must define `%S' to use this button"
8024                       (cons fun args)))))))
8025
8026 (defun gnus-parse-news-url (url)
8027   (let (scheme server port group message-id articles)
8028     (with-temp-buffer
8029       (insert url)
8030       (goto-char (point-min))
8031       (when (looking-at "\\([A-Za-z]+\\):")
8032         (setq scheme (match-string 1))
8033         (goto-char (match-end 0)))
8034       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8035         (setq server (match-string 1))
8036         (setq port (if (stringp (match-string 3))
8037                        (string-to-number (match-string 3))
8038                      (match-string 3)))
8039         (goto-char (match-end 0)))
8040
8041       (cond
8042        ((looking-at "\\(.*@.*\\)")
8043         (setq message-id (match-string 1)))
8044        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8045         (setq group (match-string 1)
8046               articles (split-string (match-string 2) "-")))
8047        ((looking-at "\\([^/]+\\)/?")
8048         (setq group (match-string 1)))
8049        (t
8050         (error "Unknown news URL syntax"))))
8051     (list scheme server port group message-id articles)))
8052
8053 (defun gnus-button-handle-news (url)
8054   "Fetch a news URL."
8055   (destructuring-bind (scheme server port group message-id articles)
8056       (gnus-parse-news-url url)
8057     (cond
8058      (message-id
8059       (with-current-buffer gnus-summary-buffer
8060         (if server
8061             (let ((gnus-refer-article-method
8062                    (nconc (list (list 'nntp server))
8063                           gnus-refer-article-method))
8064                   (nntp-port-number (or port "nntp")))
8065               (gnus-message 7 "Fetching %s with %s"
8066                             message-id gnus-refer-article-method)
8067               (gnus-summary-refer-article message-id))
8068           (gnus-summary-refer-article message-id))))
8069      (group
8070       (gnus-button-fetch-group url)))))
8071
8072 (defun gnus-button-patch (library line)
8073   "Visit an Emacs Lisp library LIBRARY on line LINE."
8074   (interactive)
8075   (let ((file (locate-library (file-name-nondirectory library))))
8076     (unless file
8077       (error "Couldn't find library %s" library))
8078     (find-file file)
8079     (goto-char (point-min))
8080     (forward-line (1- (string-to-number line)))))
8081
8082 (defun gnus-button-handle-man (url)
8083   "Fetch a man page."
8084   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8085   (when (eq gnus-button-man-handler 'woman)
8086     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8087   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8088   (funcall gnus-button-man-handler url))
8089
8090 (defun gnus-button-handle-info-url (url)
8091   "Fetch an info URL."
8092   (setq url (mm-subst-char-in-string ?+ ?\  url))
8093   (cond
8094    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8095     (gnus-info-find-node
8096      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8097                      "Gnus")
8098              ")" (gnus-url-unhex-string (match-string 2 url)))))
8099    ((string-match "([^)\"]+)[^\"]+" url)
8100     (setq url
8101           (gnus-replace-in-string
8102            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8103     (gnus-info-find-node url))
8104    (t (error "Can't parse %s" url))))
8105
8106 (defun gnus-button-handle-info-url-gnome (url)
8107   "Fetch GNOME style info URL."
8108   (setq url (mm-subst-char-in-string ?_ ?\  url))
8109   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8110       (gnus-info-find-node
8111        (concat "("
8112                (gnus-url-unhex-string
8113                  (match-string 1 url))
8114                ")"
8115                (or (gnus-url-unhex-string
8116                     (match-string 2 url))
8117                    "Top")))
8118     (error "Can't parse %s" url)))
8119
8120 (defun gnus-button-handle-info-url-kde (url)
8121   "Fetch KDE style info URL."
8122   (gnus-info-find-node (gnus-url-unhex-string url)))
8123
8124 ;; (info) will autoload info.el
8125 (declare-function Info-menu "info" (menu-item &optional fork))
8126 (declare-function Info-index-next "info" (num))
8127
8128 (defun gnus-button-handle-info-keystrokes (url)
8129   "Call `info' when pushing the corresponding URL button."
8130   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8131   (let (node indx comma)
8132     (if (string-match
8133          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8134                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8135                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8136                  "\\(?:[ \t\n,]*\\)\\)?")
8137          url)
8138         (setq node (match-string 2 url)
8139               indx (match-string 3 url))
8140       (error "Can't parse %s" url))
8141     (info)
8142     (Info-directory)
8143     (Info-menu node)
8144     (when (> (length indx) 0)
8145       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8146                             "\\([ \t\n,]*\\)")
8147                     indx)
8148       (setq comma (match-string 2 indx))
8149       (setq indx  (match-string 1 indx))
8150       (Info-index indx)
8151       (when comma
8152         (dotimes (i (with-temp-buffer
8153                       (insert comma)
8154                       ;; Note: the XEmacs version of `how-many' takes
8155                       ;; no optional argument.
8156                       (goto-char (point-min))
8157                       (how-many ",")))
8158           (Info-index-next 1)))
8159       nil)))
8160
8161 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8162 (declare-function pgg-display-output-buffer "pgg" (start end status))
8163
8164 (defun gnus-button-openpgp (url)
8165   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8166   (with-temp-buffer
8167     (mm-url-insert-file-contents-external url)
8168     (pgg-snarf-keys-region (point-min) (point-max))
8169     (pgg-display-output-buffer nil nil nil)))
8170
8171 (defun gnus-button-message-id (message-id)
8172   "Fetch MESSAGE-ID."
8173   (with-current-buffer gnus-summary-buffer
8174     (gnus-summary-refer-article message-id)))
8175
8176 (defun gnus-button-fetch-group (address &rest ignore)
8177   "Fetch GROUP specified by ADDRESS."
8178   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8179                       address)
8180     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8181     ;; for nntp:// and news://
8182     (setq address (match-string 3 address)))
8183   (if (not (string-match "[:/]" address))
8184       ;; This is just a simple group url.
8185       (gnus-group-read-ephemeral-group address gnus-select-method)
8186     (if (not
8187          (string-match
8188           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8189           address))
8190         (error "Can't parse %s" address)
8191       (gnus-group-read-ephemeral-group
8192        (match-string 4 address)
8193        `(nntp ,(match-string 1 address)
8194               (nntp-address ,(match-string 1 address))
8195               (nntp-port-number ,(if (match-end 3)
8196                                      (match-string 3 address)
8197                                    "nntp")))
8198        nil nil nil
8199        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8200
8201 (defun gnus-url-parse-query-string (query &optional downcase)
8202   (let (retval pairs cur key val)
8203     (setq pairs (split-string query "&"))
8204     (while pairs
8205       (setq cur (car pairs)
8206             pairs (cdr pairs))
8207       (if (not (string-match "=" cur))
8208           nil                           ; Grace
8209         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8210               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8211         (if downcase
8212             (setq key (downcase key)))
8213         (setq cur (assoc key retval))
8214         (if cur
8215             (setcdr cur (cons val (cdr cur)))
8216           (setq retval (cons (list key val) retval)))))
8217     retval))
8218
8219 (defun gnus-url-mailto (url)
8220   ;; Send mail to someone
8221   (when (string-match "mailto:/*\\(.*\\)" url)
8222     (setq url (substring url (match-beginning 1) nil)))
8223   (let (to args subject func)
8224     (setq args (gnus-url-parse-query-string
8225                 (if (string-match "^\\?" url)
8226                     (substring url 1)
8227                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8228                       (concat "to=" (match-string 1 url) "&"
8229                               (match-string 2 url))
8230                     (concat "to=" url)))
8231                 t)
8232           subject (cdr-safe (assoc "subject" args)))
8233     (gnus-msg-mail)
8234     (while args
8235       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8236       (if (fboundp func)
8237           (funcall func)
8238         (message-position-on-field (caar args)))
8239       (insert (gnus-replace-in-string
8240                (mapconcat 'identity (reverse (cdar args)) ", ")
8241                "\r\n" "\n" t))
8242       (setq args (cdr args)))
8243     (if subject
8244         (message-goto-body)
8245       (message-goto-subject))))
8246
8247 (defun gnus-button-embedded-url (address)
8248   "Activate ADDRESS with `browse-url'."
8249   (browse-url (gnus-strip-whitespace address)))
8250
8251 ;;; Next/prev buttons in the article buffer.
8252
8253 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8254 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8255
8256 (defvar gnus-prev-page-map
8257   (let ((map (make-sparse-keymap)))
8258     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8259     (define-key map "\r" 'gnus-button-prev-page)
8260     map))
8261
8262 (defvar gnus-next-page-map
8263   (let ((map (make-sparse-keymap)))
8264     (unless (>= emacs-major-version 21)
8265       ;; XEmacs doesn't care.
8266       (set-keymap-parent map gnus-article-mode-map))
8267     (define-key map gnus-mouse-2 'gnus-button-next-page)
8268     (define-key map "\r" 'gnus-button-next-page)
8269     map))
8270
8271 (defun gnus-insert-prev-page-button ()
8272   (let ((b (point)) e
8273         (inhibit-read-only t))
8274     (gnus-eval-format
8275      gnus-prev-page-line-format nil
8276      `(keymap ,gnus-prev-page-map
8277               gnus-prev t
8278               gnus-callback gnus-article-button-prev-page
8279               article-type annotation))
8280     (setq e (if (bolp)
8281                 ;; Exclude a newline.
8282                 (1- (point))
8283               (point)))
8284     (when gnus-article-button-face
8285       (gnus-overlay-put (gnus-make-overlay b e nil t)
8286                         'face gnus-article-button-face))
8287     (widget-convert-button
8288      'link b e
8289      :action 'gnus-button-prev-page
8290      :button-keymap gnus-prev-page-map)))
8291
8292 (defun gnus-button-next-page (&optional args more-args)
8293   "Go to the next page."
8294   (interactive)
8295   (let ((win (selected-window)))
8296     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8297     (gnus-article-next-page)
8298     (select-window win)))
8299
8300 (defun gnus-button-prev-page (&optional args more-args)
8301   "Go to the prev page."
8302   (interactive)
8303   (let ((win (selected-window)))
8304     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8305     (gnus-article-prev-page)
8306     (select-window win)))
8307
8308 (defun gnus-insert-next-page-button ()
8309   (let ((b (point)) e
8310         (inhibit-read-only t))
8311     (gnus-eval-format gnus-next-page-line-format nil
8312                       `(keymap ,gnus-next-page-map
8313                                gnus-next t
8314                                gnus-callback gnus-article-button-next-page
8315                                article-type annotation))
8316     (setq e (if (bolp)
8317                 ;; Exclude a newline.
8318                 (1- (point))
8319               (point)))
8320     (when gnus-article-button-face
8321       (gnus-overlay-put (gnus-make-overlay b e nil t)
8322                         'face gnus-article-button-face))
8323     (widget-convert-button
8324      'link b e
8325      :action 'gnus-button-next-page
8326      :button-keymap gnus-next-page-map)))
8327
8328 (defun gnus-article-button-next-page (arg)
8329   "Go to the next page."
8330   (interactive "P")
8331   (let ((win (selected-window)))
8332     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8333     (gnus-article-next-page)
8334     (select-window win)))
8335
8336 (defun gnus-article-button-prev-page (arg)
8337   "Go to the prev page."
8338   (interactive "P")
8339   (let ((win (selected-window)))
8340     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8341     (gnus-article-prev-page)
8342     (select-window win)))
8343
8344 (defvar gnus-decode-header-methods
8345   '(mail-decode-encoded-word-region)
8346   "List of methods used to decode headers.
8347
8348 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8349 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8350 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8351 whose names match REGEXP.
8352
8353 For example:
8354 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8355  mail-decode-encoded-word-region
8356  (\"chinese\" . rfc1843-decode-region))
8357 ")
8358
8359 (defvar gnus-decode-header-methods-cache nil)
8360
8361 (defun gnus-multi-decode-header (start end)
8362   "Apply the functions from `gnus-encoded-word-methods' that match."
8363   (unless (and gnus-decode-header-methods-cache
8364                (eq gnus-newsgroup-name
8365                    (car gnus-decode-header-methods-cache)))
8366     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8367     (dolist (x gnus-decode-header-methods)
8368       (if (symbolp x)
8369           (nconc gnus-decode-header-methods-cache (list x))
8370         (if (and gnus-newsgroup-name
8371                  (string-match (car x) gnus-newsgroup-name))
8372             (nconc gnus-decode-header-methods-cache
8373                    (list (cdr x)))))))
8374   (let ((xlist gnus-decode-header-methods-cache))
8375     (pop xlist)
8376     (save-restriction
8377       (narrow-to-region start end)
8378       (while xlist
8379         (funcall (pop xlist) (point-min) (point-max))))))
8380
8381 ;;;
8382 ;;; Treatment top-level handling.
8383 ;;;
8384
8385 (defun gnus-treat-article (condition &optional part-number total-parts type)
8386   (let ((length (- (point-max) (point-min)))
8387         (alist gnus-treatment-function-alist)
8388         (article-goto-body-goes-to-point-min-p t)
8389         (treated-type
8390          (or (not type)
8391              (catch 'found
8392                (let ((list gnus-article-treat-types))
8393                  (while list
8394                    (when (string-match (pop list) type)
8395                      (throw 'found t)))))))
8396         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8397         val elem)
8398     (gnus-run-hooks 'gnus-part-display-hook)
8399     (dolist (elem alist)
8400       (setq val
8401             (save-excursion
8402               (when (gnus-buffer-live-p gnus-summary-buffer)
8403                 (set-buffer gnus-summary-buffer))
8404               (symbol-value (car elem))))
8405       (when (and (or (consp val)
8406                      treated-type)
8407                  (gnus-treat-predicate val)
8408                  (or (not (get (car elem) 'highlight))
8409                      highlightp))
8410         (save-restriction
8411           (funcall (cadr elem)))))))
8412
8413 ;; Dynamic variables.
8414 (defvar part-number)
8415 (defvar total-parts)
8416 (defvar type)
8417 (defvar condition)
8418 (defvar length)
8419
8420 (defun gnus-treat-predicate (val)
8421   (cond
8422    ((null val)
8423     nil)
8424    (condition
8425     (eq condition val))
8426    ((and (listp val)
8427          (stringp (car val)))
8428     (apply 'gnus-or (mapcar `(lambda (s)
8429                                (string-match s ,(or gnus-newsgroup-name "")))
8430                             val)))
8431    ((listp val)
8432     (let ((pred (pop val)))
8433       (cond
8434        ((eq pred 'or)
8435         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8436        ((eq pred 'and)
8437         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8438        ((eq pred 'not)
8439         (not (gnus-treat-predicate (car val))))
8440        ((eq pred 'typep)
8441         (equal (car val) type))
8442        (t
8443         (error "%S is not a valid predicate" pred)))))
8444    ((eq val t)
8445     t)
8446    ((eq val 'head)
8447     nil)
8448    ((eq val 'first)
8449     (eq part-number 1))
8450    ((eq val 'last)
8451     (eq part-number total-parts))
8452    ((numberp val)
8453     (< length val))
8454    (t
8455     (error "%S is not a valid value" val))))
8456
8457 (defun gnus-article-encrypt-body (protocol &optional n)
8458   "Encrypt the article body."
8459   (interactive
8460    (list
8461     (or gnus-article-encrypt-protocol
8462         (completing-read "Encrypt protocol: "
8463                          gnus-article-encrypt-protocol-alist
8464                          nil t))
8465     current-prefix-arg))
8466   ;; User might hit `K E' instead of `K e', so prompt once.
8467   (when (and gnus-article-encrypt-protocol
8468              gnus-novice-user)
8469     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8470       (error "Encrypt aborted")))
8471   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8472     (unless func
8473       (error "Can't find the encrypt protocol %s" protocol))
8474     (if (member gnus-newsgroup-name '("nndraft:delayed"
8475                                       "nndraft:drafts"
8476                                       "nndraft:queue"))
8477         (error "Can't encrypt the article in group %s"
8478                gnus-newsgroup-name))
8479     (gnus-summary-iterate n
8480       (with-current-buffer gnus-summary-buffer
8481         (let ((mail-parse-charset gnus-newsgroup-charset)
8482               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8483               (summary-buffer gnus-summary-buffer)
8484               references point)
8485           (gnus-set-global-variables)
8486           (when (gnus-group-read-only-p)
8487             (error "The current newsgroup does not support article encrypt"))
8488           (gnus-summary-show-article t)
8489           (setq references
8490               (or (mail-header-references gnus-current-headers) ""))
8491           (set-buffer gnus-article-buffer)
8492           (let* ((inhibit-read-only t)
8493                  (headers
8494                   (mapcar (lambda (field)
8495                             (and (save-restriction
8496                                    (message-narrow-to-head)
8497                                    (goto-char (point-min))
8498                                    (search-forward field nil t))
8499                                  (prog2
8500                                      (message-narrow-to-field)
8501                                      (buffer-string)
8502                                    (delete-region (point-min) (point-max))
8503                                    (widen))))
8504                           '("Content-Type:" "Content-Transfer-Encoding:"
8505                             "Content-Disposition:"))))
8506             (message-narrow-to-head)
8507             (message-remove-header "MIME-Version")
8508             (goto-char (point-max))
8509             (setq point (point))
8510             (insert (apply 'concat headers))
8511             (widen)
8512             (narrow-to-region point (point-max))
8513             (let ((message-options message-options))
8514               (message-options-set 'message-sender user-mail-address)
8515               (message-options-set 'message-recipients user-mail-address)
8516               (message-options-set 'message-sign-encrypt 'not)
8517               (funcall func))
8518             (goto-char (point-min))
8519             (insert "MIME-Version: 1.0\n")
8520             (widen)
8521             (gnus-summary-edit-article-done
8522              references nil summary-buffer t))
8523           (when gnus-keep-backlog
8524             (gnus-backlog-remove-article
8525              (car gnus-article-current) (cdr gnus-article-current)))
8526           (when (get-buffer gnus-original-article-buffer)
8527             (with-current-buffer gnus-original-article-buffer
8528               (setq gnus-original-article nil)))
8529           (when gnus-use-cache
8530             (gnus-cache-update-article
8531              (car gnus-article-current) (cdr gnus-article-current))))))))
8532
8533 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8534   "The following specs can be used:
8535 %t  The security MIME type
8536 %i  Additional info
8537 %d  Details
8538 %D  Details if button is pressed")
8539
8540 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8541   "The following specs can be used:
8542 %t  The security MIME type
8543 %i  Additional info
8544 %d  Details
8545 %D  Details if button is pressed")
8546
8547 (defvar gnus-mime-security-button-line-format-alist
8548   '((?t gnus-tmp-type ?s)
8549     (?i gnus-tmp-info ?s)
8550     (?d gnus-tmp-details ?s)
8551     (?D gnus-tmp-pressed-details ?s)))
8552
8553 (defvar gnus-mime-security-button-commands
8554   '((gnus-article-press-button "\r" "Show Detail")
8555     (undefined "v")
8556     (undefined "t")
8557     (undefined "C")
8558     (gnus-mime-security-save-part "o" "Save...")
8559     (undefined "\C-o")
8560     (undefined "r")
8561     (undefined "d")
8562     (undefined "c")
8563     (undefined "i")
8564     (undefined "E")
8565     (undefined "e")
8566     (undefined "p")
8567     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8568     (undefined ".")))
8569
8570 (defvar gnus-mime-security-button-map
8571   (let ((map (make-sparse-keymap)))
8572     (define-key map gnus-mouse-2 'gnus-article-push-button)
8573     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8574     (dolist (c gnus-mime-security-button-commands)
8575       (define-key map (cadr c) (car c)))
8576     map))
8577
8578 (easy-menu-define
8579   gnus-mime-security-button-menu gnus-mime-security-button-map
8580   "Security button menu."
8581   `("Security Part"
8582     ,@(delq nil
8583             (mapcar (lambda (c)
8584                       (unless (eq (car c) 'undefined)
8585                         (vector (caddr c) (car c) :active t)))
8586                     gnus-mime-security-button-commands))))
8587
8588 (defun gnus-mime-security-button-menu (event prefix)
8589   "Construct a context-sensitive menu of security commands."
8590   (interactive "e\nP")
8591   (save-window-excursion
8592     (let ((pos (event-start event)))
8593       (select-window (posn-window pos))
8594       (goto-char (posn-point pos))
8595       (gnus-article-check-buffer)
8596       (popup-menu gnus-mime-security-button-menu nil prefix))))
8597
8598 (defvar gnus-mime-security-details-buffer nil)
8599
8600 (defvar gnus-mime-security-button-pressed nil)
8601
8602 (defvar gnus-mime-security-show-details-inline t
8603   "If non-nil, show details in the article buffer.")
8604
8605 (defun gnus-mime-security-verify-or-decrypt (handle)
8606   (mm-remove-parts (cdr handle))
8607   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8608         point (inhibit-read-only t))
8609     (if region
8610         (goto-char (car region)))
8611     (setq point (point))
8612     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8613       (let* ((mm-verify-option 'known)
8614              (mm-decrypt-option 'known)
8615              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8616         (unless (eq nparts (cdr handle))
8617           (mm-destroy-parts (cdr handle))
8618           (setcdr handle nparts))))
8619     (gnus-mime-display-security handle)
8620     (when region
8621       (delete-region (point) (cdr region))
8622       (set-marker (car region) nil)
8623       (set-marker (cdr region) nil))
8624     (goto-char point)))
8625
8626 (defun gnus-mime-security-show-details (handle)
8627   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8628     (if (not details)
8629         (gnus-message 5 "No details.")
8630       (if gnus-mime-security-show-details-inline
8631           (let ((gnus-mime-security-button-pressed
8632                  (not (get-text-property (point) 'gnus-mime-details)))
8633                 (gnus-mime-security-button-line-format
8634                  (get-text-property (point) 'gnus-line-format))
8635                 (inhibit-read-only t))
8636             (forward-char -1)
8637             (while (eq (get-text-property (point) 'gnus-line-format)
8638                        gnus-mime-security-button-line-format)
8639               (forward-char -1))
8640             (forward-char)
8641             (save-restriction
8642               (narrow-to-region (point) (point))
8643               (gnus-insert-mime-security-button handle))
8644             (delete-region (point)
8645                            (or (text-property-not-all
8646                                 (point) (point-max)
8647                                 'gnus-line-format
8648                                 gnus-mime-security-button-line-format)
8649                                (point-max))))
8650         ;; Not inlined.
8651         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8652             (with-current-buffer gnus-mime-security-details-buffer
8653               (erase-buffer)
8654               t)
8655           (setq gnus-mime-security-details-buffer
8656                 (gnus-get-buffer-create "*MIME Security Details*")))
8657         (with-current-buffer gnus-mime-security-details-buffer
8658           (insert details)
8659           (goto-char (point-min)))
8660         (pop-to-buffer gnus-mime-security-details-buffer)))))
8661
8662 (defun gnus-mime-security-press-button (handle)
8663   (save-excursion
8664     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8665         (gnus-mime-security-show-details handle)
8666       (gnus-mime-security-verify-or-decrypt handle))))
8667
8668 (defun gnus-insert-mime-security-button (handle &optional displayed)
8669   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8670          (gnus-tmp-type
8671           (concat
8672            (or (nth 2 (assoc protocol mm-verify-function-alist))
8673                (nth 2 (assoc protocol mm-decrypt-function-alist))
8674                "Unknown")
8675            (if (equal (car handle) "multipart/signed")
8676                " Signed" " Encrypted")
8677            " Part"))
8678          (gnus-tmp-info
8679           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8680               "Undecided"))
8681          (gnus-tmp-details
8682           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8683          gnus-tmp-pressed-details
8684          b e)
8685     (setq gnus-tmp-details
8686           (if gnus-tmp-details
8687               (concat "\n" gnus-tmp-details)
8688             ""))
8689     (setq gnus-tmp-pressed-details
8690           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8691     (unless (bolp)
8692       (insert "\n"))
8693     (setq b (point))
8694     (gnus-eval-format
8695      gnus-mime-security-button-line-format
8696      gnus-mime-security-button-line-format-alist
8697      `(keymap ,gnus-mime-security-button-map
8698          gnus-callback gnus-mime-security-press-button
8699          gnus-line-format ,gnus-mime-security-button-line-format
8700          gnus-mime-details ,gnus-mime-security-button-pressed
8701          article-type annotation
8702          gnus-data ,handle))
8703     (setq e (if (bolp)
8704                 ;; Exclude a newline.
8705                 (1- (point))
8706               (point)))
8707     (when gnus-article-button-face
8708       (gnus-overlay-put (gnus-make-overlay b e nil t)
8709                         'face gnus-article-button-face))
8710     (widget-convert-button
8711      'link b e
8712      :mime-handle handle
8713      :action 'gnus-widget-press-button
8714      :button-keymap gnus-mime-security-button-map
8715      :help-echo
8716      (lambda (widget)
8717        ;; Needed to properly clear the message due to a bug in
8718        ;; wid-edit (XEmacs only).
8719        (when (boundp 'help-echo-owns-message)
8720          (setq help-echo-owns-message t))
8721        (format
8722         "%S: show detail; %S: more options"
8723         (aref gnus-mouse-2 0)
8724         (aref gnus-down-mouse-3 0))))))
8725
8726 (defun gnus-mime-display-security (handle)
8727   (save-restriction
8728     (narrow-to-region (point) (point))
8729     (unless (gnus-unbuttonized-mime-type-p (car handle))
8730       (gnus-insert-mime-security-button handle))
8731     (gnus-mime-display-part (cadr handle))
8732     (unless (bolp)
8733       (insert "\n"))
8734     (unless (gnus-unbuttonized-mime-type-p (car handle))
8735       (let ((gnus-mime-security-button-line-format
8736              gnus-mime-security-button-end-line-format))
8737         (gnus-insert-mime-security-button handle)))
8738     (mm-set-handle-multipart-parameter
8739      handle 'gnus-region
8740      (cons (set-marker (make-marker) (point-min))
8741            (set-marker (make-marker) (point-max))))
8742     (goto-char (point-max))))
8743
8744 (defun gnus-mime-security-run-function (function)
8745   "Run FUNCTION with the security part under point."
8746   (gnus-article-check-buffer)
8747   (let ((data (get-text-property (point) 'gnus-data))
8748         buffer handle)
8749     (when (and (stringp (car-safe data))
8750                (setq buffer (mm-handle-multipart-original-buffer data))
8751                (setq handle (cadr data)))
8752       (if (bufferp (mm-handle-buffer handle))
8753           (progn
8754             (setq handle (cons buffer (copy-sequence (cdr handle))))
8755             (mm-handle-set-undisplayer handle nil))
8756         (setq handle (mm-make-handle
8757                       buffer
8758                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8759                       nil nil nil nil nil nil)))
8760       (funcall function handle))))
8761
8762 (defun gnus-mime-security-save-part ()
8763   "Save the security part under point."
8764   (interactive)
8765   (gnus-mime-security-run-function 'mm-save-part))
8766
8767 (defun gnus-mime-security-pipe-part ()
8768   "Pipe the security part under point to a process."
8769   (interactive)
8770   (gnus-mime-security-run-function 'mm-pipe-part))
8771
8772 (gnus-ems-redefine)
8773
8774 (provide 'gnus-art)
8775
8776 (run-hooks 'gnus-art-load-hook)
8777
8778 ;;; gnus-art.el ends here