-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemacs-client.el
More file actions
151 lines (121 loc) · 4.28 KB
/
emacs-client.el
File metadata and controls
151 lines (121 loc) · 4.28 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
;; lco emacs test client
;;
;; Copyright (C) 2010 Lyndon Tremblay <humasect@McHoovy.local>
;; Created: Thu Sep 16 10:32:40 MDT 2010
;;
(global-set-key (kbd "s-.") 'lco)
(require 'json)
;; http://edward.oconnor.cx/2006/03/json.el
(defvar lco-server "localhost")
(defvar lco-port 1979)
(defconst lco-message-buffer-name "*lco-messages*")
(defconst lco-game-buffer-name "*lco-game*")
(defconst lco-splash "VRE client 1.0 ready.\n")
(defun lco-message-buffer ()
(get-buffer-create "*lco-messages*"))
(defun lco-game-buffer ()
(get-buffer-create "*lco-game*"))
(defun lco-log (msg)
(with-current-buffer (lco-message-buffer)
;;(setq buffer-read-only f)
(set-window-point (get-buffer-window (lco-message-buffer)) (point-max))
(goto-char (point-max))
(insert msg)
;;(setq buffer-read-only t)))
))
(defun lco-change-room (room)
(lco-log (format "change room! %s" room))
(with-current-buffer (lco-game-buffer)
(erase-buffer)))
;;--------------------------------------------------------------
;; network
;;--------------------------------------------------------------
(defvar lco-process nil)
(defun lco-send (obj)
(setq str (json-encode obj))
(process-send-string lco-process str))
(defun lco-filter (proc string)
(lco-log string)
(let* ((msg (car (json-read-from-string string)))
(name (car msg))
(args (cdr msg)))
(cond ((eq name 'change_room) (lco-change-room))
(t (lco-log (format "json: %s\n" msg)))
)))
(defun lco-sentinel (proc what)
(lco-log what))
(defun lco-init-net (user pass)
(lco-log "Connecting... ")
(setq lco-process (make-network-process
:name "lco-client"
:type nil
:host lco-server
:service lco-port
:family nil
:buffer (lco-game-buffer)
:coding 'utf-8
:filter 'lco-filter
:sentinel 'lco-sentinel))
(if lco-process
(lco-log "OK.\n")
(lco-log "Error.\n")))
;;--------------------------------------------------------------
;; display / interface
;;--------------------------------------------------------------
(defun lco-init-display ()
(setq f (make-frame
'((title . lco-splash)
(name . "lco-frame")
(width . 80)
(height . 50)
(buffer-list . '((lco-message-buffer) (lco-game-buffer)))
(unsplittable . t)
(menu-bar-lines . nil)
(tool-bar-lines . nil))))
(select-frame f)
(switch-to-buffer (lco-message-buffer))
;;(delete-region (point-min) (point-max))
(setq w2 (split-window (selected-window) 10))
(select-window w2)
(switch-to-buffer (lco-game-buffer))
;;(delete-region (point-min) (point-max))
)
;;(switch-to-buffer (game-buffer)))
(defun lco-move (angle)
;;(interactive "nAngle: ")
(lco-send `(:move ,angle)))
(defun lco-init-keys ()
(set-buffer (lco-game-buffer))
(local-set-key "/" (lambda (cmd)
(interactive "sCommand: ")
(lco-send `(:command ,cmd))))
(local-set-key "t" (lambda (msg)
(interactive "sMessage: ")
(lco-send `(:client (:say ,msg)))))
(local-set-key "q" 'lco-quit)
(local-set-key "4" (lambda () (interactive) (lco-move 270)))
(local-set-key "2" (lambda () (interactive) (lco-move 180)))
(local-set-key "6" (lambda () (interactive) (lco-move 90)))
(local-set-key "8" (lambda () (interactive) (lco-move 0)))
(local-set-key "7" (lambda () (interactive) (lco-move 315)))
(local-set-key "9" (lambda () (interactive) (lco-move 45)))
(local-set-key "3" (lambda () (interactive) (lco-move 135)))
(local-set-key "1" (lambda () (interactive) (lco-move 225))))
;;--------------------------------------------------------------
;; API
;;--------------------------------------------------------------
(defun lco-init ()
(lco-log (format "\n%s" lco-splash))
(lco-init-keys))
(defun lco (user pass)
(interactive "sUsername: \nsPassword: ")
(lco-init-display)
(lco-init)
(lco-init-net user pass)
;(lco-send `(:login [,user ,pass "Test" "japanese"]))
(lco-send `(:client (:login [,user ,pass])))
)
(defun lco-quit ()
(interactive)
(lco-log "Quit.")
(delete-process lco-process))