summaryrefslogtreecommitdiff
path: root/vim/bundle/slimv/swank-clojure/swank/core/connection.clj
blob: 1b78bc61f91cebad7bf60b0e880a4b3a31f56081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns swank.core.connection
  (:use (swank util)
        (swank.util sys)
        (swank.core protocol))
  (:import (java.net ServerSocket Socket InetAddress)
           (java.io InputStreamReader OutputStreamWriter)))

(def #^{:dynamic true} *current-connection*)
(def default-encoding "iso-8859-1")

(defmacro with-connection [conn & body]
  `(binding [*current-connection* ~conn] ~@body))

(def encoding-map
     {"latin-1" "iso-8859-1"
      "latin-1-unix" "iso-8859-1"
      "iso-latin-1-unix" "iso-8859-1"
      "iso-8859-1" "iso-8859-1"
      "iso-8859-1-unix" "iso-8859-1"

      "utf-8" "utf-8"
      "utf-8-unix" "utf-8"

      "euc-jp" "euc-jp"
      "euc-jp-unix" "euc-jp"

      "us-ascii" "us-ascii" 
      "us-ascii-unix" "us-ascii"})

(defn make-connection ;; rename to make-swank-connection
  "Given a `socket', creates a swank connection. Accepts an optional
   argument `encoding' to define the encoding of the connection. If
   encoding is nil, then the default encoding will be used.

   See also: `default-encoding', `start-server-socket!'"
  ([#^Socket socket] (make-connection socket default-encoding))
  ([#^Socket socket encoding]
     (let [#^String
           encoding (or (encoding-map encoding encoding) default-encoding)]
       {:socket socket
        :reader (InputStreamReader. (.getInputStream socket) encoding)
        :writer (OutputStreamWriter. (.getOutputStream socket) encoding)
        :writer-redir (ref nil)
        
        :indent-cache (ref {})
        :indent-cache-pkg (ref nil)
        
        :control-thread (ref nil)
        :read-thread (ref nil)
        :repl-thread (ref nil)})))

(defn read-from-connection
  "Reads a single message from a swank-connection.

   See also: `write-to-connection', `read-swank-message',
     `make-swank-connection'"
  ([] (read-from-connection *current-connection*))
  ([conn]
     (read-swank-message (conn :reader))))

(defn write-to-connection
  "Writes a single message to a swank-connection.

  See also: `read-from-connection', `write-swank-message',
    `make-swank-connection'"
  ([msg] (write-to-connection *current-connection* msg))
  ([conn msg]
     (write-swank-message (conn :writer) msg)))