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