0
|
1 ; $Id$
|
|
2 ; (defcustom redit-client-program "/usr/home/masasi/work/redit/reditclient"
|
|
3 ;(defcustom redit-client-program "/usr/home/shinobu/redit/demo/reditclient"
|
2
|
4 (defcustom redit-client-program "/Users/dot/work/REP_project/emacs/redit_client.pl"
|
0
|
5 "The program to use as the remote-edit client."
|
|
6 :group 'redit
|
|
7 :type 'string)
|
|
8
|
|
9 ; 送受信されるコマンドは
|
|
10 ; 2 + 2 + 9 + data
|
|
11 ; command_number + buffer_number + line_number + data
|
|
12
|
|
13
|
|
14 (defvar redit-client-process nil
|
|
15 "Remote-edit client process.")
|
|
16
|
|
17 (defvar redit-client-buffer nil
|
|
18 "Remote-edit client's buffer.")
|
|
19
|
|
20 (defvar redit-client-buffer-name nil
|
|
21 "Remote-edit client's buffer-name.")
|
|
22
|
|
23 (defvar redit-client-buffer-number nil
|
|
24 "Remote-edit client's buffer-number.")
|
|
25
|
|
26 (defvar redit-client-editor-id "0"
|
|
27 "Remote-edit client's editor-id.")
|
|
28
|
2
|
29 (defvar redit-client-session-id 0
|
|
30 "Remote-edit client's session-id.")
|
|
31
|
0
|
32 ; editor local sequence number
|
|
33 (defvar redit-client-seqno 0
|
|
34 "Remote-edit client's editor-id.")
|
|
35
|
|
36 (defvar redit-client-line-max-in-server nil
|
|
37 "Value of point-max in server's buffer.")
|
|
38
|
|
39 (defvar redit-client-previous-edited-line 1
|
|
40 "Current cursor line of remote-edit client.")
|
|
41
|
|
42 (defvar redit-client-after-edited-line 1
|
|
43 "Current cursor line of remote-edit client.")
|
|
44
|
|
45 (defvar redit-client-previous-line 1
|
|
46 "Behind cursor line of remote-edit client.")
|
|
47
|
|
48 (defvar redit-max-string-length 1004
|
|
49 "Read-Write max length of string.")
|
|
50
|
|
51 (defvar redit-open-command "1")
|
|
52 (defvar redit-open-ack "2")
|
|
53 (defvar redit-read-command "3")
|
|
54 (defvar redit-read-ack "4")
|
|
55 (defvar redit-read-finish-ack "5")
|
|
56 (defvar redit-write-command "6") ; insert
|
|
57 (defvar redit-write-still-ack "7")
|
|
58 (defvar redit-write-finish-ack "8")
|
|
59 (defvar redit-delete-line-command "9")
|
|
60 (defvar redit-delete-line-ack "10")
|
|
61 (defvar redit-close-command "11")
|
|
62 (defvar redit-close-ack "12")
|
|
63 (defvar redit-replace-command "13")
|
|
64
|
|
65 ;; REP Session
|
2
|
66 (defvar redit-join-command "41")
|
|
67 (defvar redit-join-ack-command "42")
|
|
68 (defvar redit-get-command "43")
|
|
69 (defvar redit-get-ack-command "44")
|
|
70 (defvar redit-put-command "45")
|
|
71 (defvar redit-put-ack-command "46")
|
|
72 (defvar redit-select-command "47")
|
|
73 (defvar redit-select-ack-command "48")
|
|
74 (defvar redit-register-command "49")
|
|
75 (defvar redit-register-ack-command "50")
|
|
76 (defvar redit-deregister-command "51")
|
|
77 (defvar redit-deregister-ack-command "52")
|
|
78 (defvar redit-quit-command "53")
|
|
79 (defvar redit-quit-ack-command "54")
|
0
|
80
|
|
81 (defun redit-client-start (host)
|
|
82 "Allow this Emacs process to be a remote-edit client for client processes."
|
|
83 (interactive "P")
|
|
84 (if redit-client-process
|
|
85 (progn
|
|
86 (set-process-sentinel redit-client-process nil)
|
|
87 (condition-case () ; エラーが起こるとプロセスを消す
|
|
88 (delete-process redit-client-process) (error nil))))
|
|
89 (if (setq host (read-string "Hostname: ")); minibufferでホスト名を入力させる
|
|
90 (let ((process-connection-type nil))
|
|
91 ; クライアントプロセス(redit-client)内のプログラムをスタートさせる。
|
|
92 ; 引き数として host が与えられる
|
|
93 (setq redit-client-process
|
|
94 (start-process "reditclient" nil redit-client-program host))
|
|
95 ; redit-client-process の状態が変化すると redit-client-sentinel を呼ぶ
|
|
96 (set-process-sentinel redit-client-process 'redit-client-sentinel)
|
|
97 ; redit-client-process からのデータ入力は redit-client-process-filter
|
|
98 ; を介する
|
|
99 (set-process-filter redit-client-process 'redit-client-process-filter)
|
|
100 ; redit-client-process からの入出力を 8-bit codes で取扱う。
|
|
101 (set-process-coding-system redit-client-process
|
|
102 'raw-text 'raw-text)
|
|
103 ; emacs が終了すると redit-client-process も終了する
|
|
104 (process-kill-without-query redit-client-process)
|
|
105 ; フック変数 before-change-functions, after-change-functions
|
|
106 ;をバッファローカルにする
|
|
107 (make-local-hook 'before-change-functions)
|
|
108 (make-local-hook 'after-change-functions)
|
|
109
|
|
110 (message "Remote edit client started.")) (error "redit-client connection refused.")))
|
|
111
|
|
112 ; (redit-join-command ())
|
|
113 (defun redit-join-command (host)
|
|
114 "Allow this Emacs process to be a remote-edit session manager for client processes."
|
|
115 (interactive "P")
|
|
116 (if redit-client-process
|
|
117 (progn
|
|
118 (set-process-sentinel redit-client-process nil)
|
|
119 (condition-case () ;
|
|
120 (delete-process redit-client-process) (error nil))))
|
|
121 (if (setq host (read-string "Hostname: "))
|
|
122 (let ((process-connection-type nil))
|
|
123 (setq redit-client-process
|
|
124 (start-process "reditclient" nil redit-client-program host))
|
|
125 (set-process-sentinel redit-client-process 'redit-client-sentinel)
|
|
126 (set-process-filter redit-client-process 'redit-client-process-filter)
|
|
127 (set-process-coding-system redit-client-process
|
|
128 'raw-text 'raw-text)
|
|
129 (process-kill-without-query redit-client-process)
|
2
|
130 ; (make-local-hook 'before-change-functions)
|
|
131 ; (make-local-hook 'after-change-functions)
|
0
|
132 ; FIXME
|
|
133 ; concat my hostname this command?
|
|
134 (process-send-string
|
|
135 redit-client-process
|
2
|
136 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-join-command) 0 0 (gen-seqno) 0 0) "\n")))
|
0
|
137 (error "redit-client connection refused.")))
|
|
138
|
|
139 ; (redit-get-command)
|
|
140 (defun redit-get-command ()
|
|
141 "get. alias redit-join-command and redit-select-command."
|
|
142 (interactive)
|
|
143 (redit-join-command ())
|
|
144 (redit-select-command ()))
|
|
145
|
|
146 ; select : select session
|
|
147 ; ユーザの入力を受け取って、selectコマンドを構成し、通信プロセスに渡す
|
|
148 ; (redit-select-command ()
|
|
149 (defun redit-select-command (session-name)
|
|
150 "select session"
|
|
151 (interactive "P")
|
|
152 (if (setq session-name (read-string "session name:"))
|
2
|
153 (progn
|
|
154 (setq redit-client-session-id (string-to-number session-name))
|
|
155 (process-send-string
|
|
156 redit-client-process
|
|
157 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-select-command) redit-client-session-id redit-client-editor-id (gen-seqno) 0 0) session-name "\n"))
|
|
158 )
|
0
|
159 (error "invalid session name.")
|
|
160 )
|
|
161 )
|
|
162
|
2
|
163 ;(defun redit-put-command ()
|
|
164 ; "put"
|
|
165 ; (interactive)
|
|
166 ; )
|
|
167 (defun redit-put-command (session-name)
|
|
168 "put session"
|
|
169 (interactive "P")
|
|
170 (if (setq session-name (read-string "put session name:"))
|
|
171 (process-send-string
|
|
172 redit-client-process
|
|
173 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-put-command) redit-client-session-id redit-client-editor-id (gen-seqno) 0 0) session-name "\n"))
|
|
174 (error "invalid session name.")
|
|
175 )
|
0
|
176 )
|
|
177
|
|
178 (defun redit-register-command (session-name)
|
|
179 "register"
|
|
180 (interactive "P")
|
|
181 (if (setq session-name (read-string "session name:"))
|
|
182 (process-send-string
|
|
183 redit-client-process
|
2
|
184 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-register-command) redit-client-session-id redit-client-editor-id (gen-seqno) 0 0) session-name "\n"))
|
0
|
185 (error "invalid session name.")
|
|
186 )
|
|
187 )
|
|
188
|
|
189 (defun redit-deregister-command ()
|
|
190 "deregister"
|
|
191 (interactive)
|
|
192 )
|
|
193
|
|
194 (defun redit-quit-command ()
|
|
195 "quit"
|
|
196 (interactive)
|
|
197 )
|
|
198
|
|
199 (defun redit-client-open (file)
|
|
200 (interactive "P")
|
|
201 (if redit-client-process
|
|
202 (if (setq file (read-string "Filename: "))
|
|
203 (progn
|
|
204 ; redit-client-process の input に文字列を送信する
|
|
205 (process-send-string
|
|
206 redit-client-process
|
|
207 ; redit-open-command (01) と file を連結させる
|
|
208 (concat redit-open-command file))
|
|
209 ; redit-client-process から出力を得るまでループ
|
|
210 (while (eq nil (accept-process-output redit-client-process)))))
|
|
211 (error "redit-client is not running.")))
|
|
212
|
|
213 ; redit-client-process にreadコマンドとバッファ番号、行番号を送り、
|
|
214 ; サーバからの出力を得る
|
|
215 (defun redit-client-read-line (linenum)
|
|
216 (if redit-client-process
|
|
217 (save-excursion
|
|
218 (progn
|
|
219 (process-send-string
|
|
220 redit-client-process
|
2
|
221 (format "%10d%10d%10d%10d%10d%10d\n" (string-to-number redit-read-command) redit-client-session-id redit-client-editor-id (gen-seqno) linenum 0))
|
0
|
222 (while (eq nil
|
|
223 (accept-process-output redit-client-process)))))
|
|
224 (error "redit-client is not running.")))
|
|
225
|
|
226 ; linenumで指定された行の編集をサーバへ送る
|
|
227 ; redit-client-process にwriteコマンドとバッファ番号、行番号、行の内容を送り、
|
|
228 ; Ack を受け取る
|
|
229 (defun redit-client-write-line (linenum isnewline)
|
|
230 (if redit-client-process
|
|
231 (save-excursion
|
|
232 (let ((beginp) (endp) (length))
|
|
233 ; write lines on server buffer
|
|
234 (setq beginp
|
|
235 (progn (goto-line linenum) (point)))
|
|
236 (setq endp
|
|
237 (progn (goto-line linenum) (end-of-line) (point)))
|
|
238 (setq length (- endp beginp))
|
|
239 ; redit-client-process へ command_num + buffer_num + line_num + string
|
|
240 ;の文字列を送る
|
|
241 (process-send-string
|
|
242 redit-client-process
|
2
|
243 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-write-command) redit-client-session-id redit-client-editor-id (gen-seqno) linenum length)
|
0
|
244 (buffer-substring beginp endp)
|
|
245 (if (eq isnewline t)
|
2
|
246 "\n") "\n"))
|
|
247 ;(while (eq nil
|
|
248 ; (accept-process-output redit-client-process 1)))))
|
|
249 ))
|
0
|
250 (error "redit-client is not running.")))
|
|
251
|
|
252 ; linenum で指定した行の削除命令を redit-client-process に送信する
|
|
253 (defun redit-client-delete-line (linenum)
|
|
254 (if redit-client-process
|
|
255 (save-restriction
|
|
256 ; write lines on server buffer
|
|
257 (process-send-string
|
|
258 redit-client-process
|
2
|
259 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-delete-line-command) redit-client-session-id redit-client-editor-id (gen-seqno) linenum 0) "\n"))
|
|
260 ;(while (eq nil
|
|
261 ; (accept-process-output redit-client-process 1))))
|
|
262 )
|
0
|
263 (error "redit-client is not running.")))
|
|
264
|
|
265 ; redit-client-process へcloseコマンドを送る
|
|
266 (defun redit-client-close ()
|
|
267 "Save File and exit Remote-edit client."
|
|
268 (interactive)
|
|
269 (let ((line redit-client-previous-line))
|
|
270 (redit-client-write-line line nil)
|
|
271 (process-send-string
|
|
272 redit-client-process
|
2
|
273 (concat (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-close-command) redit-client-session-id redit-client-editor-id (gen-seqno) linenum 0) "\n"))))
|
0
|
274
|
|
275 ; redit-client-process を終了させる
|
|
276 (defun redit-client-kill ()
|
|
277 "Stop Remote-Edit client process."
|
|
278 (interactive)
|
|
279 (if redit-client-process
|
|
280 (kill-process redit-client-process))
|
|
281 (if redit-client-buffer
|
|
282 (kill-buffer redit-client-buffer))
|
|
283 (kill-all-local-variables))
|
|
284
|
|
285 ; proc == redit-client-process
|
|
286 ; string == redit-client-process からの output
|
|
287 (defun redit-client-process-filter (proc string)
|
|
288 ;バッファローカルなフックリスト before-change-functions から
|
|
289 ;redit-client-before-change-function を取り除く. after も同じ
|
|
290 (remove-hook 'before-change-functions
|
|
291 'redit-client-before-change-function t)
|
|
292 (remove-hook 'after-change-functions
|
|
293 'redit-client-after-change-function t)
|
2
|
294 ; (message string) ;; add
|
|
295 (let ((command (rep-get-command-from-pkt string)))
|
0
|
296 ;command がどの命令かを判断し、対応した処理をする。case みたい
|
|
297 (cond
|
|
298 ; FIXME
|
2
|
299 ((if (= command (string-to-number redit-open-ack))
|
|
300 (if (/= redit-client-editor-id)
|
|
301 (redit-client-exec-open-ack string))))
|
0
|
302
|
2
|
303 ((if (= command (string-to-number redit-read-ack))
|
|
304 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
305 (redit-client-exec-read-ack string))))
|
0
|
306
|
2
|
307 ((if (= command (string-to-number redit-write-still-ack))
|
|
308 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
309 (redit-client-exec-write-still-ack string))))
|
0
|
310
|
2
|
311 ((if (= command (string-to-number redit-delete-line-ack))
|
|
312 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
313 (redit-client-exec-delete-line-ack string))))
|
0
|
314
|
2
|
315 ((if (= command (string-to-number redit-close-ack))
|
|
316 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
317 (redit-client-exec-close-ack string))))
|
0
|
318
|
2
|
319 ((if (= command (string-to-number redit-write-command))
|
|
320 (if (/= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
321 (redit-client-exec-write-line string))))
|
0
|
322
|
|
323 ; from Session Manager
|
2
|
324
|
|
325 ; join ack (editor id)
|
|
326 ; editor id を保存する
|
|
327 ((if (= command (string-to-number redit-join-ack-command))
|
0
|
328 (redit-client-exec-join string)))
|
2
|
329
|
|
330 ; put ack (editor id)
|
|
331 ; session id を保存する
|
|
332 ((if (= command (string-to-number redit-put-ack-command))
|
0
|
333 (redit-client-exec-put string)))
|
2
|
334
|
|
335 ; select ack
|
|
336 ; TODO : start editing
|
|
337 ((if (= command (string-to-number redit-select-ack-command))
|
|
338 (progn
|
|
339 (add-hook 'before-change-functions
|
|
340 'redit-client-before-change-function t t)
|
|
341 (add-hook 'after-change-functions
|
|
342 'redit-client-after-change-function t t))))
|
|
343
|
|
344 ; get
|
|
345 ((if (= command (string-to-number redit-get-command))
|
|
346 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
347 (redit-client-exec-get string))))
|
0
|
348 ; register
|
2
|
349 ((if (= command (string-to-number redit-register-command))
|
|
350 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
351 (redit-client-exec-register string))))
|
0
|
352 ; deregister
|
2
|
353 ((if (= command (string-to-number redit-deregister-command))
|
|
354 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
355 (redit-client-exec-deregister string))))
|
0
|
356
|
|
357 ; delete line
|
2
|
358 ((if (= command (string-to-number redit-delete-line-command))
|
|
359 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
360 (redit-server-exec-delete-line string))))
|
0
|
361 ((string-equal string ""))))
|
2
|
362 ;(add-hook 'before-change-functions
|
|
363 ; 'redit-client-before-change-function t t)
|
|
364 ;(add-hook 'after-change-functions
|
|
365 ; 'redit-client-after-change-function t t)
|
|
366 )
|
0
|
367
|
|
368 ; window-scroll-functions に hook される。
|
|
369 ; window がスクロールする度に呼ばれる
|
|
370 (defun redit-client-update-function (window window-start)
|
|
371 (if (equal (window-buffer) redit-client-buffer)
|
|
372 (save-excursion
|
|
373 (remove-hook 'before-change-functions
|
|
374 'redit-client-before-change-function t)
|
|
375 (remove-hook 'after-change-functions
|
|
376 'redit-client-after-change-function t)
|
|
377 ; read part.
|
|
378 (while (and (= (point-max)
|
|
379 (window-end nil t))
|
|
380 (> redit-client-line-max-in-server
|
|
381 (real-count-lines (window-end nil t))))
|
|
382 ; (count-lines (window-end nil t))))
|
|
383 (redit-client-read-line (real-count-lines (window-end nil t))))
|
|
384
|
|
385 (if (not (= (count-lines (point-min) (window-end nil t))
|
|
386 (real-count-lines (window-end nil t))))
|
|
387 (redit-client-read-line
|
|
388 (progn (goto-line redit-client-line-max-in-server)
|
|
389 (real-count-lines (point)))))
|
|
390
|
|
391 (if (< redit-client-line-max-in-server
|
|
392 (real-count-lines (window-end nil t)))
|
|
393 (progn (goto-line (window-end nil t))
|
|
394 (delete-region
|
|
395 (progn (beginning-of-line) (backward-char) (point))
|
|
396 (progn (forward-char) (end-of-line) (point)))))
|
|
397
|
|
398 (add-hook 'before-change-functions
|
|
399 'redit-client-before-change-function t t)
|
|
400 (add-hook 'after-change-functions
|
|
401 'redit-client-after-change-function t t))))
|
|
402
|
|
403 ; currentp の位置の行数を返す
|
|
404 (defun real-count-lines (currentp)
|
|
405 (+ (count-lines (point-min) currentp)
|
|
406 (if (= (save-excursion (goto-char currentp) (current-column))
|
|
407 0)
|
|
408 1 0)))
|
|
409
|
|
410 (defun limited-save-excursion (&rest exprs)
|
|
411 (let ((saved-point (point)))
|
|
412 (while exprs
|
|
413 (eval (car exprs))
|
|
414 (setq exprs (cdr exprs)))
|
|
415 (goto-char saved-point)))
|
|
416
|
|
417 ; before-change-functions に hook される
|
|
418 ; バッファに変更があるたびに呼ばれる。バッファが変更される前に呼ばれる。
|
|
419 ; begin と end には変更前の変更部分の始まりと終わりの point が入る
|
|
420 (defun redit-client-before-change-function (begin end)
|
|
421 ; check delete
|
2
|
422 ; (message "call redit-client-before-change-function")
|
0
|
423 (let ((beginl (real-count-lines begin)) ; begin の行番号
|
|
424 (endl (real-count-lines end)) ; end の行番号
|
|
425 (currline)) ; currline = nil
|
|
426 ;前回の編集した行と今回の編集した行が違っていたら前回の編集をサーバへ送る
|
|
427 (if (not (= beginl redit-client-previous-edited-line))
|
|
428 (redit-client-write-line redit-client-previous-edited-line nil))
|
|
429 (if (< beginl endl)
|
|
430 (progn
|
|
431 (setq currline endl)
|
|
432 ; endlからbeginlまで行の削除命令を出す
|
|
433 (while (> currline beginl)
|
|
434 (redit-client-delete-line endl)
|
|
435 (setq currline (- currline 1)))))))
|
|
436
|
|
437 ; after-change-functions に hook される
|
|
438 ; バッファに変更があるたびに呼ばれる。バッファが変更された後に呼ばれる
|
|
439 ; begin と end には変更後の変更部分の始まりと終わりの point が入る
|
|
440 (defun redit-client-after-change-function (begin end length)
|
|
441 ; check insert
|
2
|
442 ; (message "call redit-client-after-change-function")
|
0
|
443 (let ((beginl (real-count-lines begin))
|
|
444 (endl (real-count-lines end))
|
|
445 (currline))
|
|
446 (if (< beginl endl)
|
|
447 (progn
|
|
448 (setq currline beginl)
|
|
449 ; beginlからendlまで挿入命令を出す
|
|
450 (while (< currline endl)
|
|
451 (progn (redit-client-write-line beginl nil);t)
|
|
452 (setq currline (+ currline 1))))))
|
|
453 (setq redit-client-previous-edited-line endl)))
|
|
454
|
|
455 ; 引き数で与えられた string (line_num + text_data) から
|
|
456 ; 指定された行を削除し、そこに text_data を挿入する
|
|
457 (defun redit-client-exec-write-line (string)
|
2
|
458 (let ((linenum (rep-get-line-number-from-pkt string)) ; 行番号
|
0
|
459 (text (rep-get-text-from-pkt string))) ; テキストデータ
|
|
460 (if (< (real-count-lines (point-max)) linenum)
|
|
461 (progn
|
|
462 (end-of-buffer)
|
|
463 (newline)))
|
|
464 (goto-line linenum)
|
|
465 (delete-region (progn (beginning-of-line) (point)); 行の頭から
|
|
466 (progn (end-of-line) (point))) ; 行の末尾まで削除
|
|
467 (insert text))) ; テキストデータを挿入
|
|
468
|
|
469 ; 引き数 string (line_num + text_data) で指定された行を削除する
|
|
470 (defun redit-client-exec-delete-line (string)
|
2
|
471 (let ((linenum (rep-get-line-number-from-pkt string)))
|
0
|
472 (goto-line linenum)
|
|
473 ; 行頭から末尾までのテキストを削除
|
|
474 (delete-region (progn (beginning-of-line) (point))
|
|
475 (progn (end-of-line) (point)))
|
|
476 ; 指定された行自体を削除
|
|
477 ; 指定された行番号は別の行の番号を表すことになる
|
|
478 (if (= (point) (point-max))
|
|
479 (delete-backward-char 1)
|
|
480 (delete-char 1))))
|
|
481
|
|
482 ; 引き数 string (buf_num + line_num + text_data)
|
|
483 (defun redit-client-exec-open-ack (string)
|
|
484 (save-excursion
|
2
|
485 (let ((bufnum (rep-get-editor-id-from-pkt string))
|
|
486 (linenum (rep-get-line-number-from-pkt string)))
|
0
|
487 (make-variable-buffer-local 'redit-client-buffer-name)
|
|
488 (make-variable-buffer-local 'redit-client-editor-id)
|
|
489 (setq redit-client-buffer-name
|
|
490 (rep-get-text-from-pk string))
|
|
491 (setq redit-client-buffer (get-buffer-create redit-client-buffer-name))
|
|
492 ; プロセスとバッファを関連づける
|
|
493 (set-process-buffer redit-client-process redit-client-buffer)
|
|
494 (switch-to-buffer redit-client-buffer) ; windowのバッファを変更する
|
|
495 (make-variable-buffer-local 'redit-client-previous-edited-line)
|
|
496 (make-variable-buffer-local 'redit-client-after-edited-line)
|
|
497 (make-variable-buffer-local 'redit-client-line-max-in-server)
|
|
498 (make-variable-buffer-local 'redit-client-previous-line)
|
|
499 (setq redit-client-line-max-in-server linenum)
|
|
500 (setq redit-client-editor-id bufnum)
|
|
501 (setq redit-client-previous-edited-line 1)
|
|
502 (setq redit-client-after-edited-line 1)
|
|
503 (setq redit-client-previous-line 1)
|
|
504 (add-hook 'before-change-functions
|
|
505 'redit-client-before-change-function t t)
|
|
506 (add-hook 'after-change-functions
|
|
507 'redit-client-after-change-function t t)
|
|
508 (make-local-hook 'window-scroll-functions)
|
|
509 ; window がスクロールする度に呼ばれる関数群
|
|
510 (add-hook 'window-scroll-functions
|
|
511 'redit-client-update-function t t))))
|
|
512
|
|
513 ; string (line_num + text_data) で指定された行に
|
|
514 ; text_data を挿入する
|
|
515 (defun redit-client-exec-read-ack (string)
|
|
516 (save-excursion
|
2
|
517 (let ((lines (rep-get-line-number-from-pkt string)))
|
0
|
518 ; redit-client-buffer をカレントバッファにする
|
|
519 (set-buffer redit-client-buffer)
|
|
520 (goto-line lines)
|
|
521 (beginning-of-line) ; 行の始めへ移動
|
|
522 ; (delete-region (progn (beginning-of-line) (point))
|
|
523 ; (progn (end-of-line) (point)))
|
|
524 (insert (rep-get-text-from-pkt string))))) ; テキストデータを挿入
|
|
525
|
|
526 ; redit-client-line-max-in-server に行番号を入れてる(だけ)
|
|
527 (defun redit-client-exec-write-still-ack (string)
|
|
528 (save-excursion
|
|
529 (set-buffer redit-client-buffer)
|
2
|
530 (let ((linenum (rep-get-line-number-from-pkt string )))
|
0
|
531 (setq redit-client-line-max-in-server linenum))))
|
|
532
|
|
533 (defun redit-client-exec-delete-line-ack (string)
|
|
534 (save-excursion
|
|
535 (set-buffer redit-client-buffer)
|
2
|
536 (let ((linenum (rep-get-line-number-from-pkt string)))
|
0
|
537 (setq redit-client-line-max-in-server linenum))))
|
|
538
|
|
539 ; プロセスとバッファ、ローカル変数を削除する
|
|
540 (defun redit-client-exec-close-ack (string)
|
|
541 (if redit-client-process
|
|
542 (kill-process redit-client-process))
|
|
543 (if redit-client-buffer
|
|
544 (kill-buffer redit-client-buffer))
|
|
545 (kill-all-local-variables))
|
|
546
|
2
|
547 ; 引き数で与えられた string から Editor IDを取得する
|
0
|
548 (defun redit-client-exec-join (string)
|
|
549 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string))
|
|
550 (insert (rep-get-text-from-pkt string)))
|
|
551
|
2
|
552 ; 引き数で与えられた string から Session IDを取得する
|
|
553 (defun redit-client-exec-put (string)
|
|
554 (setq redit-client-session-id (rep-get-session-id-from-pkt string)))
|
|
555
|
0
|
556 ; プロセスの状態を見て、対応したメッセージを表示
|
|
557 (defun redit-client-sentinel (proc msg)
|
|
558 (cond ((eq (process-status proc) 'exit)
|
|
559 (message "Client subprocess exited"))
|
|
560 ((eq (process-status proc) 'signal)
|
|
561 (message "Client subprocess killed"))))
|
|
562
|
|
563 (defun gen-seqno ()
|
|
564 "generate editor local sequence number."
|
|
565 (setq redit-client-seqno (+ redit-client-seqno 1)))
|
|
566
|
|
567 (defun rep-get-command-from-pkt (pkt)
|
|
568 (string-to-number (substring pkt 0 10)))
|
2
|
569 (defun rep-get-session-id-from-pkt (pkt)
|
0
|
570 (string-to-number (substring pkt 10 20)))
|
2
|
571 (defun rep-get-editor-id-from-pkt (pkt)
|
|
572 (string-to-number (substring pkt 20 30)))
|
0
|
573 (defun rep-get-sequence-id-from-pkt (pkt)
|
2
|
574 (string-to-number (substring pkt 30 40)))
|
0
|
575 (defun rep-get-line-number-from-pkt (pkt)
|
2
|
576 (string-to-number (substring pkt 40 50)))
|
0
|
577 (defun rep-get-text-size-from-pkt (pkt)
|
2
|
578 (string-to-number (substring pkt 50 60)))
|
0
|
579 (defun rep-get-text-from-pkt (pkt)
|
2
|
580 (substring pkt 60))
|