Mercurial > hg > RemoteEditor > emacs
annotate redit-client-sm.el @ 31:ccd1ad69a852 rep_camp_20071111
*** empty log message ***
author | gongo |
---|---|
date | Fri, 21 Dec 2007 13:30:50 +0900 |
parents | 9e43cec05dc5 |
children | c8bac1eb3c9b |
rev | line source |
---|---|
6 | 1 ;; $Id$ |
29 | 2 |
3 ;; not use | |
4 ;;(defcustom redit-client-program "/Users/gongo/src/cr/REP_project/emacs/redit_client.pl" | |
5 ;; "The program to use as the remote-edit client." | |
6 ;; :group 'redit | |
7 ;; :type 'string) | |
0 | 8 |
6 | 9 ;; 送受信されるコマンドは |
10 ;; 2 + 2 + 9 + data | |
11 ;; command_number + buffer_number + line_number + data | |
0 | 12 |
6 | 13 (eval-when-compile (require 'cl)) |
0 | 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 | |
6 | 33 ;; editor local sequence number |
0 | 34 (defvar redit-client-seqno 0 |
7 | 35 "Remote-edit client's editor-id.") |
0 | 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 | |
17 | 52 ;; REP が使用する文字コード |
15 | 53 (defvar rep-string-encoding 'utf-8) |
54 | |
0 | 55 (defvar redit-open-command "1") |
29 | 56 (defvar redit-open-ack 2) |
57 (defvar redit-read-command 3) | |
58 (defvar redit-read-ack 4) | |
59 (defvar redit-read-finish-ack 5) ;; obsolete | |
60 (defvar redit-insert-command 6) | |
61 (defvar redit-insert-ack 7) | |
62 ;;(defvar redit-write-still-ack "7") | |
63 ;;(defvar redit-write-finish-ack "8") | |
64 (defvar redit-delete-line-command 9) | |
65 (defvar redit-delete-line-ack 10) | |
66 (defvar redit-close-command 11) | |
67 (defvar redit-close-ack 12) | |
68 (defvar redit-replace-command 13) | |
69 (defvar redit-replace-ack 14) | |
70 (defvar redit-nop-command 15) | |
0 | 71 |
72 ;; REP Session | |
29 | 73 (defvar redit-join-command 41) |
74 (defvar redit-join-ack-command 42) | |
75 (defvar redit-get-command 43) ;; obsolete | |
76 (defvar redit-get-ack-command 44) ;; obsolete | |
77 (defvar redit-put-command 45) | |
78 (defvar redit-put-ack-command 46) | |
79 (defvar redit-select-command 47) ;; obsolete | |
80 (defvar redit-select-ack-command 48) ;; obsolete | |
81 (defvar redit-register-command 49) ;; obsolete | |
82 (defvar redit-register-ack-command 50) ;; obsolete | |
83 (defvar redit-deregister-command 51) ;; obsolete | |
84 (defvar redit-deregister-ack-command 52) ;; obsolete | |
85 (defvar redit-quit-command 53) | |
86 (defvar redit-quit-ack-command 54) | |
0 | 87 |
6 | 88 ;; Queue struct |
26 | 89 ;; (defstruct Queue (front nil) (rear nil)) |
90 ;; (setq redit-client-command-queue (make-Queue)) | |
91 ;; (setq redit-client-receive-queue (make-Queue)) | |
92 ;; (setq redit-client-send-queue (make-Queue)) | |
5 | 93 |
6 | 94 ;; line translated queue |
13 | 95 ;; (setq after-CQ (make-Queue)) |
96 ;; (setq after-RQ (make-Queue)) | |
97 | |
98 | |
99 ;; Fix Me | |
100 ;; 適当に作ってしまったので。 | |
101 ;; 一応コマンドは認識してます。 | |
16 | 102 (defun redit-pack-int-loop (num count) |
13 | 103 (if (> count 0) |
104 (concat | |
16 | 105 (redit-pack-int-loop (/ num 256) (- count 1)) |
13 | 106 (char-to-string (% num 256))))) |
107 | |
16 | 108 (defun redit-pack-int (num) |
109 (redit-pack-int-loop num 4)) | |
110 | |
111 (defun redit-pack (cmd sid eid seq lno siz) | |
112 (concat | |
113 (redit-pack-int cmd) (redit-pack-int sid) (redit-pack-int eid) | |
114 (redit-pack-int seq) (redit-pack-int lno) (redit-pack-int siz))) | |
115 | |
30 | 116 (defun redit-unpack-int-loop (pkt pos count) |
117 (if (> count 1) | |
118 (+ | |
119 (* (redit-unpack-int-loop pkt (- pos 1) (- count 1)) 256) | |
120 (string-to-char (substring pkt (- pos 1) pos))) | |
121 0 | |
122 )) | |
123 | |
124 (defun redit-unpack-int (pkt pos) | |
125 (redit-unpack-int-loop pkt (* pos 4) 4)) | |
13 | 126 |
127 | |
16 | 128 ;; これ使ってる? |
129 ;; redit-join-command の仕事と被ってるっぽいのでコメント | |
130 ;; (defun redit-client-start (host) | |
131 ;; "Allow this Emacs process to be a remote-edit client for client processes." | |
132 ;; (interactive "P") | |
133 ;; (if redit-client-process | |
134 ;; (progn | |
135 ;; (set-process-sentinel redit-client-process nil) | |
136 ;; (condition-case () ;; エラーが起こるとプロセスを消す | |
137 ;; (delete-process redit-client-process) (error nil)))) | |
138 ;; (if (setq host (read-string "Hostname: ")) ;; minibufferでホスト名を入力させる | |
139 ;; (let ((process-connection-type nil)) | |
140 ;; ;; クライアントプロセス(redit-client)内のプログラムをスタートさせる。 | |
141 ;; ;; 引き数として host が与えられる | |
142 ;; (setq redit-client-process | |
143 ;; (start-process "reditclient" nil redit-client-program host)) | |
144 ;; | |
145 ;; ;; redit-client-process の状態が変化すると redit-client-sentinel を呼ぶ | |
146 ;; (set-process-sentinel redit-client-process 'redit-client-sentinel) | |
147 ;; ;; redit-client-process からのデータ入力は redit-client-process-filter | |
148 ;; ;; を介する | |
149 ;; (set-process-filter redit-client-process 'redit-client-process-filter) | |
150 ;; ;; redit-client-process からの入出力を 8-bit codes で取扱う。 | |
151 ;; ;;(set-process-coding-system redit-client-process | |
152 ;; ;;'raw-text 'raw-text) | |
153 ;; (set-process-coding-system redit-client-process | |
21
7ba615ef1593
change utf-8 string to rep-string-encoding variable.
shinobu
parents:
19
diff
changeset
|
154 ;; rep-string-encoding rep-string-encoding) |
16 | 155 ;; ;; emacs が終了すると redit-client-process も終了する |
156 ;; (set-process-query-on-exit-flag redit-client-process nil) | |
157 ;; | |
158 ;; ;; フック変数 before-change-functions, after-change-functions | |
159 ;; ;; をバッファローカルにする | |
160 ;; ;; not necessary since Emacs 21.1 | |
161 ;; ;; (make-local-hook 'before-change-functions) | |
162 ;; ;; (make-local-hook 'after-change-functions) | |
163 ;; | |
164 ;; (message "Remote edit client started.")) (error "redit-client connection refused."))) | |
165 ;; | |
166 ;; (redit-join-command ()) | |
13 | 167 |
168 | |
16 | 169 (defun redit-join-command () |
0 | 170 "Allow this Emacs process to be a remote-edit session manager for client processes." |
16 | 171 (interactive) |
31 | 172 (let (bufname) |
173 (setq bufname (encode-coding-string | |
174 (buffer-name) | |
175 rep-string-encoding)) | |
176 (if redit-client-process | |
177 (progn | |
178 (set-process-sentinel redit-client-process nil) | |
179 (condition-case () ;; | |
180 (delete-process redit-client-process) (error nil)))) | |
181 | |
182 (setq redit-client-process (open-network-stream | |
183 "redit-client" nil "localhost" 8766)) | |
7 | 184 |
31 | 185 ;; called when the process changes state |
186 (set-process-sentinel redit-client-process 'redit-client-sentinel) | |
187 | |
188 ;; called when the process receive packet | |
189 (set-process-filter redit-client-process 'redit-client-process-filter) | |
16 | 190 |
31 | 191 ;; //Code of the process input/output buffer is rep-string-encoding |
192 ;; //(set-process-coding-system redit-client-process rep-string-encoding rep-string-encoding) | |
193 ;; 送受信するデータはバイナリで貰わないといけない。 | |
194 ;; utf-8 じゃなくてずれる可能性があるので | |
195 (set-process-coding-system redit-client-process 'binary 'binary) | |
196 | |
197 ;; プロセスが生きてるとき、 emacs を終了しようとすると | |
198 ;; 「processがうんたらで、本当に切ってもいいの y/n」 | |
199 ;; って聞かれる。nil てやっておくと聞かれない。 | |
200 ;; quit を入れるんなら t にして有効にすべきなんだろうか。 | |
201 ;; 今は nil で | |
202 (set-process-query-on-exit-flag redit-client-process nil) | |
16 | 203 |
31 | 204 ;; not necessary since Emacs 21.1 |
205 ;; (make-local-hook 'before-change-functions) | |
206 ;; (make-local-hook 'after-change-functions) | |
207 | |
208 (if (string= "*scratch*" bufname) | |
209 ;; JOIN if buffer-name is "*scratch*" | |
210 (process-send-string | |
211 redit-client-process | |
212 (concat | |
213 (redit-pack | |
214 redit-join-command | |
215 0 0 0 0 0))) | |
216 ;; PUT if buffer-name is not "*scratch*" | |
217 (process-send-string | |
218 redit-client-process | |
219 (concat | |
220 (redit-pack | |
29 | 221 redit-put-command |
31 | 222 0 0 0 0 (string-bytes bufname)) |
223 (buffer-name))) | |
224 ))) | |
225 | |
16 | 226 ;; 'get' is obsolete command |
227 ;; (defun redit-get-command () | |
228 ;; "get. alias redit-join-command and redit-select-command." | |
229 ;; (interactive) | |
230 ;; (redit-join-command) | |
231 ;; (redit-select-command ())) | |
0 | 232 |
6 | 233 ;; select : select session |
234 ;; ユーザの入力を受け取って、selectコマンドを構成し、通信プロセスに渡す | |
235 ;; (redit-select-command () | |
31 | 236 (defun redit-select-command (session-name) |
237 "select session" | |
238 (interactive "P") | |
239 (if (setq session-name (read-string "session name:")) | |
240 (progn | |
241 (setq redit-client-session-id (string-to-number session-name)) | |
242 (process-send-string | |
243 redit-client-process | |
244 (redit-pack | |
245 redit-select-command redit-client-session-id redit-client-editor-id | |
246 (gen-seqno) 0 0)) | |
247 | |
248 ;;(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")) | |
249 ) | |
250 (error "invalid session name.") | |
251 ) | |
252 ) | |
253 | |
0 | 254 |
27 | 255 ;; put-command は、join-command 内で |
256 ;; ファイルを開いてない *scratch* の時に呼び出されるので | |
257 ;; ここで定義する必要は無い | |
258 ;;(defun redit-put-command (session-name) | |
259 ;; "put session" | |
260 ;; (interactive "P") | |
261 ;; (if (setq session-name (read-string "put session name:")) | |
262 ;; (process-send-string | |
263 ;; redit-client-process | |
264 ;; (concat | |
265 ;; (redit-pack | |
266 ;; (string-to-number redit-put-command) 0 0 0 0 | |
267 ;; (string-bytes (encode-coding-string session-name rep-string-encoding))) | |
268 ;; session-name | |
269 ;; ) | |
270 ;; ) | |
271 ;; (error "invalid session name.") | |
272 ;; )) | |
0 | 273 |
16 | 274 ;; 'register' is obsolete command |
275 ;; (defun redit-register-command (session-name) | |
276 ;; "register" | |
277 ;; (interactive "P") | |
278 ;; (if (setq session-name (read-string "session name:")) | |
279 ;; (process-send-string | |
280 ;; redit-client-process | |
281 ;; (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")) | |
282 ;; (error "invalid session name.") | |
283 ;; ) | |
284 ;; ) | |
285 ;; | |
0 | 286 |
16 | 287 ;; 'deregister' is obsolete command |
288 ;;(defun redit-deregister-command () | |
289 ;; "deregister" | |
290 ;; (interactive) | |
291 ;; ) | |
0 | 292 |
6 | 293 (defun redit-quit-command () |
0 | 294 "quit" |
295 (interactive) | |
296 ) | |
297 | |
298 (defun redit-client-open (file) | |
299 (interactive "P") | |
300 (if redit-client-process | |
301 (if (setq file (read-string "Filename: ")) | |
302 (progn | |
6 | 303 ;; redit-client-process の input に文字列を送信する |
0 | 304 (process-send-string |
305 redit-client-process | |
6 | 306 ;; redit-open-command (01) と file を連結させる |
0 | 307 (concat redit-open-command file)) |
6 | 308 ;; redit-client-process から出力を得るまでループ |
0 | 309 (while (eq nil (accept-process-output redit-client-process))))) |
310 (error "redit-client is not running."))) | |
311 | |
6 | 312 ;; redit-client-process にreadコマンドとバッファ番号、行番号を送り、 |
313 ;; サーバからの出力を得る | |
0 | 314 (defun redit-client-read-line (linenum) |
315 (if redit-client-process | |
316 (save-excursion | |
317 (progn | |
318 (process-send-string | |
319 redit-client-process | |
29 | 320 (format "%10d%10d%10d%10d%10d%10d\n" redit-read-command redit-client-session-id redit-client-editor-id (gen-seqno) linenum 0)) |
0 | 321 (while (eq nil |
322 (accept-process-output redit-client-process))))) | |
323 (error "redit-client is not running."))) | |
324 | |
6 | 325 ;; linenumで指定された行の編集をサーバへ送る |
29 | 326 ;; redit-client-process に insert コマンドと |
327 ;;バッファ番号、行番号、行の内容を送り、 Ack を受け取る | |
328 (defun redit-client-insert-line (linenum isnewline) | |
0 | 329 (if redit-client-process |
330 (save-excursion | |
6 | 331 (let ((beginp) (endp) (length)) |
29 | 332 ;; insert lines on server buffer |
6 | 333 (setq beginp |
10 | 334 (progn (goto-line linenum) (beginning-of-line) (point))) |
6 | 335 (setq endp |
336 (progn (goto-line linenum) (end-of-line) (point))) | |
17 | 337 ;; (setq length (- endp beginp)) |
24 | 338 ;; rep-string-encoding に変換してから長さを求める |
15 | 339 (setq length (string-bytes (encode-coding-string (buffer-substring beginp endp) rep-string-encoding))) |
24 | 340 |
6 | 341 ;; トークンが回ってくるのを待つ場合は、 |
342 ;; すぐに process-send-string せずに | |
343 ;; 編集情報を保存するだけにとどめる | |
13 | 344 ;;(enqueue |
345 ;;redit-client-command-queue | |
346 ;;(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) | |
347 ;;(buffer-substring beginp endp) | |
348 ;;(if (eq isnewline t) "\n") | |
349 ;;"\n")) | |
10 | 350 |
6 | 351 ;; redit-client-process へ |
352 ;; command_num + buffer_num + line_num + string | |
353 ;; の文字列を送る | |
24 | 354 |
7 | 355 (process-send-string |
6 | 356 redit-client-process |
24 | 357 (concat |
27 | 358 |
359 ;; obsolete send string | |
24 | 360 ;;(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) |
27 | 361 |
24 | 362 (redit-pack |
29 | 363 redit-insert-command |
24 | 364 redit-client-session-id |
365 redit-client-editor-id | |
366 (gen-seqno) linenum length) | |
367 | |
368 ;; (buffer-substring beginp endp) | |
369 ;; rep-string-encoding に変換 | |
370 (encode-coding-string (buffer-substring beginp endp) rep-string-encoding) | |
371 (if (eq isnewline t) "\n") | |
372 "\n")) | |
11 | 373 (sleep-for 0.3) |
6 | 374 )) |
0 | 375 (error "redit-client is not running."))) |
376 | |
6 | 377 ;; linenum で指定した行の削除命令を redit-client-process に送信する |
0 | 378 (defun redit-client-delete-line (linenum) |
379 (if redit-client-process | |
380 (save-restriction | |
5 | 381 |
6 | 382 ;; トークンが回ってくるのを待つ場合は、 |
383 ;; すぐに process-send-string せずに | |
384 ;; 編集情報を保存するだけにとどめる | |
13 | 385 ;;(enqueue redit-client-command-queue |
386 ;;(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")) | |
387 | |
29 | 388 ;; insert lines on server buffer |
0 | 389 (process-send-string |
390 redit-client-process | |
27 | 391 |
392 (concat | |
393 ;; obsolete send string | |
394 ;; (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")) | |
395 | |
396 (redit-pack | |
29 | 397 redit-delete-line-command |
27 | 398 redit-client-session-id |
399 redit-client-editor-id | |
400 (gen-seqno) linenum 0))) | |
401 (sleep-for 0.3) | |
6 | 402 ) |
0 | 403 (error "redit-client is not running."))) |
404 | |
6 | 405 ;; redit-client-process へcloseコマンドを送る |
0 | 406 (defun redit-client-close () |
407 "Save File and exit Remote-edit client." | |
408 (interactive) | |
409 (let ((line redit-client-previous-line)) | |
29 | 410 (redit-client-insert-line line nil) |
0 | 411 (process-send-string |
412 redit-client-process | |
27 | 413 (concat |
414 ;; obsolete send string | |
415 ;; (format "%10d%10d%10d%10d%10d%10d" (string-to-number redit-close-command) redit-client-session-id redit-client-editor-id (gen-seqno) 0 0) | |
416 (redit-pack | |
29 | 417 redit-close-command |
27 | 418 redit-client-session-id |
419 redit-client-editor-id | |
420 (gen-seqno) 0 0) "\n")))) | |
0 | 421 |
6 | 422 ;; redit-client-process を終了させる |
0 | 423 (defun redit-client-kill () |
424 "Stop Remote-Edit client process." | |
425 (interactive) | |
426 (if redit-client-process | |
427 (kill-process redit-client-process)) | |
428 (if redit-client-buffer | |
429 (kill-buffer redit-client-buffer)) | |
430 (kill-all-local-variables)) | |
431 | |
6 | 432 ;; proc == redit-client-process |
433 ;; string == redit-client-process からの output | |
0 | 434 (defun redit-client-process-filter (proc string) |
6 | 435 ;; バッファローカルなフックリスト before-change-functions から |
436 ;; redit-client-before-change-function を取り除く. after も同じ | |
7 | 437 ;; proc からの出力に hook しないように |
0 | 438 (remove-hook 'before-change-functions |
439 'redit-client-before-change-function t) | |
440 (remove-hook 'after-change-functions | |
441 'redit-client-after-change-function t) | |
16 | 442 |
2 | 443 (let ((command (rep-get-command-from-pkt string))) |
6 | 444 ;; command がどの命令かを判断し、対応した処理をする。case みたい |
0 | 445 (cond |
6 | 446 ;; FIXME |
29 | 447 ((if (= command redit-open-ack) |
7 | 448 (if (/= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
6 | 449 (redit-client-exec-open-ack string)))) |
0 | 450 |
29 | 451 ((if (= command redit-read-ack) |
6 | 452 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
453 (redit-client-exec-read-ack string)))) | |
0 | 454 |
29 | 455 ((if (= command redit-insert-ack) |
6 | 456 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
29 | 457 (redit-client-exec-insert-ack string)))) |
0 | 458 |
29 | 459 ((if (= command redit-delete-line-ack) |
6 | 460 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
461 (redit-client-exec-delete-line-ack string)))) | |
0 | 462 |
29 | 463 ((if (= command redit-close-ack) |
6 | 464 (if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
465 (redit-client-exec-close-ack string)))) | |
0 | 466 |
29 | 467 ((if (= command redit-insert-command) |
6 | 468 (if (/= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
29 | 469 (redit-client-exec-insert-line string)))) |
0 | 470 |
6 | 471 ;; from Session Manager |
472 ;; join ack (editor id) | |
29 | 473 ((if (= command redit-join-ack-command) |
16 | 474 ;;(message "redit-client-exec-join") |
0 | 475 (redit-client-exec-join string))) |
2 | 476 |
6 | 477 ;; put ack (editor id) |
29 | 478 ((if (= command redit-put-ack-command) |
0 | 479 (redit-client-exec-put string))) |
2 | 480 |
6 | 481 ;; select ack |
482 ;; TODO : start editing | |
27 | 483 ;; obsolete |
29 | 484 ;;((if (= command redit-select-ack-command) |
27 | 485 ;;(redit-client-exec-select))) |
7 | 486 |
6 | 487 ;; get |
29 | 488 ;;((if (= command redit-get-command) |
16 | 489 ;;(if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
490 ;;(redit-client-exec-get string)))) | |
491 | |
6 | 492 ;; register |
29 | 493 ;;((if (= command redit-register-command) |
16 | 494 ;;(if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
495 ;;(redit-client-exec-register string)))) | |
496 | |
6 | 497 ;; deregister |
29 | 498 ;;((if (= command redit-deregister-command) |
16 | 499 ;;(if (= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
500 ;;(redit-client-exec-deregister string)))) | |
0 | 501 |
6 | 502 ;; delete line |
29 | 503 ((if (= command redit-delete-line-command) |
6 | 504 (if (/= redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
505 (redit-client-exec-delete-line string)))) | |
7 | 506 )) |
0 | 507 |
7 | 508 (add-hook 'before-change-functions |
509 'redit-client-before-change-function t t) | |
510 (add-hook 'after-change-functions | |
511 'redit-client-after-change-function t t) | |
6 | 512 ) |
7 | 513 |
6 | 514 ;; window-scroll-functions に hook される。 |
515 ;; window がスクロールする度に呼ばれる | |
0 | 516 (defun redit-client-update-function (window window-start) |
517 (if (equal (window-buffer) redit-client-buffer) | |
518 (save-excursion | |
519 (remove-hook 'before-change-functions | |
520 'redit-client-before-change-function t) | |
521 (remove-hook 'after-change-functions | |
522 'redit-client-after-change-function t) | |
6 | 523 ;; read part. |
0 | 524 (while (and (= (point-max) |
525 (window-end nil t)) | |
526 (> redit-client-line-max-in-server | |
527 (real-count-lines (window-end nil t)))) | |
6 | 528 ;; (count-lines (window-end nil t)))) |
0 | 529 (redit-client-read-line (real-count-lines (window-end nil t)))) |
530 | |
531 (if (not (= (count-lines (point-min) (window-end nil t)) | |
532 (real-count-lines (window-end nil t)))) | |
533 (redit-client-read-line | |
534 (progn (goto-line redit-client-line-max-in-server) | |
535 (real-count-lines (point))))) | |
536 | |
537 (if (< redit-client-line-max-in-server | |
538 (real-count-lines (window-end nil t))) | |
539 (progn (goto-line (window-end nil t)) | |
540 (delete-region | |
541 (progn (beginning-of-line) (backward-char) (point)) | |
542 (progn (forward-char) (end-of-line) (point))))) | |
543 | |
544 (add-hook 'before-change-functions | |
545 'redit-client-before-change-function t t) | |
546 (add-hook 'after-change-functions | |
547 'redit-client-after-change-function t t)))) | |
548 | |
6 | 549 ;; currentp の位置の行数を返す |
0 | 550 (defun real-count-lines (currentp) |
551 (+ (count-lines (point-min) currentp) | |
552 (if (= (save-excursion (goto-char currentp) (current-column)) | |
553 0) | |
554 1 0))) | |
555 | |
556 (defun limited-save-excursion (&rest exprs) | |
557 (let ((saved-point (point))) | |
558 (while exprs | |
559 (eval (car exprs)) | |
560 (setq exprs (cdr exprs))) | |
561 (goto-char saved-point))) | |
562 | |
6 | 563 ;; before-change-functions に hook される |
564 ;; バッファに変更があるたびに呼ばれる。バッファが変更される前に呼ばれる。 | |
565 ;; begin と end には変更前の変更部分の始まりと終わりの point が入る | |
0 | 566 (defun redit-client-before-change-function (begin end) |
6 | 567 (let ((beginl (real-count-lines begin)) ;; begin の行番号 |
568 (endl (real-count-lines end)) ;; end の行番号 | |
569 (currline)) ;; currline = nil | |
10 | 570 |
31 | 571 ;; (message "before-change-function") |
10 | 572 |
573 ;; 前回の編集した行と今回の編集した行が | |
574 ;; 違っていたら前回の編集をサーバへ送る | |
0 | 575 (if (not (= beginl redit-client-previous-edited-line)) |
29 | 576 (redit-client-insert-line redit-client-previous-edited-line nil)) |
10 | 577 |
578 ;; 削除処理で起こる。 | |
579 ;; (ex. BackSpaceによる行そのものの削除, C-Space C-wなどのカット, ...) | |
0 | 580 (if (< beginl endl) |
581 (progn | |
582 (setq currline endl) | |
6 | 583 ;; endlからbeginlまで行の削除命令を出す |
0 | 584 (while (> currline beginl) |
585 (redit-client-delete-line endl) | |
586 (setq currline (- currline 1))))))) | |
10 | 587 |
6 | 588 ;; after-change-functions に hook される |
589 ;; バッファに変更があるたびに呼ばれる。バッファが変更された後に呼ばれる | |
590 ;; begin と end には変更後の変更部分の始まりと終わりの point が入る | |
0 | 591 (defun redit-client-after-change-function (begin end length) |
592 (let ((beginl (real-count-lines begin)) | |
593 (endl (real-count-lines end)) | |
594 (currline)) | |
10 | 595 |
31 | 596 ;; (message "after-change-function") |
10 | 597 |
598 ;; 挿入処理で起こる。 | |
599 ;; (ex. 改行, C-y などのペースト, ...) | |
0 | 600 (if (< beginl endl) |
601 (progn | |
602 (setq currline beginl) | |
6 | 603 ;; beginlからendlまで挿入命令を出す |
604 (while (<= currline endl) | |
29 | 605 (progn (redit-client-insert-line currline nil) |
0 | 606 (setq currline (+ currline 1)))))) |
607 (setq redit-client-previous-edited-line endl))) | |
7 | 608 |
6 | 609 ;; 引き数で与えられた string (line_num + text_data) から |
610 ;; 指定された行を削除し、そこに text_data を挿入する | |
29 | 611 (defun redit-client-exec-insert-line (string) |
7 | 612 (let ((linenum (rep-get-line-number-from-pkt string)) ;; 行番号 |
613 (text (rep-get-text-from-pkt string))) ;; テキストデータ | |
614 (if (< (real-count-lines (point-max)) linenum) | |
615 (progn | |
8 | 616 (call-interactively 'end-of-buffer) (newline))) |
7 | 617 (goto-line linenum) |
618 (delete-region (progn (beginning-of-line) (point)) ;; 行の頭から | |
619 (progn (end-of-line) (point))) ;; 行の末尾まで削除 | |
620 (insert text) ;; テキストデータを挿入 | |
10 | 621 (message (concat "recv = " text ", line = " (number-to-string linenum))) |
7 | 622 )) |
0 | 623 |
6 | 624 ;; 引き数 string (line_num + text_data) で指定された行を削除する |
0 | 625 (defun redit-client-exec-delete-line (string) |
7 | 626 (let ((linenum (rep-get-line-number-from-pkt string))) |
627 (goto-line linenum) | |
628 ;; 行頭から末尾までのテキストを削除 | |
629 (delete-region (progn (beginning-of-line) (point)) | |
630 (progn (end-of-line) (point))) | |
631 ;; 指定された行自体を削除 | |
632 ;; 指定された行番号は別の行の番号を表すことになる | |
633 (if (= (point) (point-max)) | |
634 (delete-backward-char 1) | |
635 (delete-char 1))) | |
636 ) | |
0 | 637 |
6 | 638 ;; 引き数 string (buf_num + line_num + text_data) |
0 | 639 (defun redit-client-exec-open-ack (string) |
7 | 640 (save-excursion |
641 (let ((bufnum (rep-get-editor-id-from-pkt string)) | |
642 (linenum (rep-get-line-number-from-pkt string))) | |
643 (make-variable-buffer-local 'redit-client-buffer-name) | |
644 (make-variable-buffer-local 'redit-client-editor-id) | |
645 (setq redit-client-buffer-name | |
8 | 646 (rep-get-text-from-pkt string)) |
7 | 647 (setq redit-client-buffer (get-buffer-create redit-client-buffer-name)) |
648 ;; プロセスとバッファを関連づける | |
649 (set-process-buffer redit-client-process redit-client-buffer) | |
650 (switch-to-buffer redit-client-buffer) ;; windowのバッファを変更する | |
651 (make-variable-buffer-local 'redit-client-previous-edited-line) | |
652 (make-variable-buffer-local 'redit-client-after-edited-line) | |
653 (make-variable-buffer-local 'redit-client-line-max-in-server) | |
654 (make-variable-buffer-local 'redit-client-previous-line) | |
655 (setq redit-client-line-max-in-server linenum) | |
656 (setq redit-client-editor-id bufnum) | |
657 (setq redit-client-previous-edited-line 1) | |
658 (setq redit-client-after-edited-line 1) | |
659 (setq redit-client-previous-line 1) | |
23 | 660 |
7 | 661 ;;(add-hook 'before-change-functions |
662 ;;'redit-client-before-change-function t t) | |
663 ;;(add-hook 'after-change-functions | |
664 ;;'redit-client-after-change-function t t) | |
0 | 665 |
7 | 666 ;; not necessary since Emacs 21.1 |
667 ;; (make-local-hook 'window-scroll-functions) | |
6 | 668 |
7 | 669 ;; window がスクロールする度に呼ばれる関数群 |
670 (add-hook 'window-scroll-functions | |
671 'redit-client-update-function t t)))) | |
6 | 672 |
673 ;; string (line_num + text_data) で指定された行に | |
674 ;; text_data を挿入する | |
0 | 675 (defun redit-client-exec-read-ack (string) |
7 | 676 (save-excursion |
677 (let ((lines (rep-get-line-number-from-pkt string))) | |
678 ;; redit-client-buffer をカレントバッファにする | |
679 (set-buffer redit-client-buffer) | |
680 (goto-line lines) | |
681 (beginning-of-line) ;; 行の始めへ移動 | |
682 ;; (delete-region (progn (beginning-of-line) (point)) | |
683 ;; (progn (end-of-line) (point))) | |
684 (insert (rep-get-text-from-pkt string))))) ;; テキストデータを挿入 | |
0 | 685 |
6 | 686 ;; redit-client-line-max-in-server に行番号を入れてる(だけ) |
29 | 687 (defun redit-client-exec-insert-ack (string) |
7 | 688 (save-excursion |
689 (set-buffer redit-client-buffer) | |
690 (let ((linenum (rep-get-line-number-from-pkt string ))) | |
691 (setq redit-client-line-max-in-server linenum)))) | |
0 | 692 |
693 (defun redit-client-exec-delete-line-ack (string) | |
7 | 694 (save-excursion |
695 (set-buffer redit-client-buffer) | |
696 (let ((linenum (rep-get-line-number-from-pkt string))) | |
697 (setq redit-client-line-max-in-server linenum)))) | |
0 | 698 |
6 | 699 ;; プロセスとバッファ、ローカル変数を削除する |
0 | 700 (defun redit-client-exec-close-ack (string) |
7 | 701 (if redit-client-process |
702 (kill-process redit-client-process)) | |
703 (if redit-client-buffer | |
704 (kill-buffer redit-client-buffer)) | |
705 (kill-all-local-variables)) | |
6 | 706 |
707 ;; 引き数で与えられた string から Editor IDを取得する | |
708 (defun redit-client-exec-join (string) | |
7 | 709 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
27 | 710 (message (concat "your editor id = " (number-to-string redit-client-editor-id) "\n"))) |
0 | 711 |
27 | 712 ;; //引き数で与えられた string から Session IDを取得する |
713 ;; 新仕様で、(put = ファイル有りjoin) ってことになって | |
714 ;; editor id を返すことになったので、sid と共に eid もセーブする | |
2 | 715 (defun redit-client-exec-put (string) |
27 | 716 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string)) |
7 | 717 (setq redit-client-session-id (rep-get-session-id-from-pkt string)) |
27 | 718 (message (concat "Your Editor ID = " (number-to-string redit-client-editor-id) "\n")) |
719 (message (concat "Session ID = " (number-to-string redit-client-session-id) "\n"))) | |
2 | 720 |
6 | 721 ;; SessionIDを名前とした、新しいバッファを開く |
722 ;; FIXME | |
723 ;; SessionIDではなく、Session Nameが望ましい? | |
724 ;; バッファを開いた後、Sessionで開いてるファイルの中身を表示するべき | |
725 (defun redit-client-exec-select () | |
7 | 726 (switch-to-buffer (get-buffer-create |
727 (number-to-string redit-client-session-id)))) | |
6 | 728 |
729 ;; プロセスの状態を見て、対応したメッセージを表示 | |
0 | 730 (defun redit-client-sentinel (proc msg) |
7 | 731 (cond ((eq (process-status proc) 'exit) |
732 (message "Client subprocess exited")) | |
733 ((eq (process-status proc) 'signal) | |
734 (message "Client subprocess killed")))) | |
0 | 735 |
736 (defun gen-seqno () | |
7 | 737 "generate editor local sequence number." |
738 (setq redit-client-seqno (+ redit-client-seqno 1))) | |
0 | 739 |
740 (defun rep-get-command-from-pkt (pkt) | |
26 | 741 (redit-unpack-int pkt 1)) |
2 | 742 (defun rep-get-session-id-from-pkt (pkt) |
26 | 743 (redit-unpack-int pkt 2)) |
2 | 744 (defun rep-get-editor-id-from-pkt (pkt) |
26 | 745 (redit-unpack-int pkt 3)) |
0 | 746 (defun rep-get-sequence-id-from-pkt (pkt) |
26 | 747 (redit-unpack-int pkt 4)) |
0 | 748 (defun rep-get-line-number-from-pkt (pkt) |
26 | 749 (redit-unpack-int pkt 5)) |
0 | 750 (defun rep-get-text-size-from-pkt (pkt) |
26 | 751 (redit-unpack-int pkt 6)) |
0 | 752 (defun rep-get-text-from-pkt (pkt) |
6 | 753 (let ((size (rep-get-text-size-from-pkt pkt))) |
13 | 754 (substring (decode-coding-string pkt 'euc-jp) 27 (+ 27 size)))) |
6 | 755 |
756 ;; enqueue item in queue | |
13 | 757 ;; (defun enqueue (queue item) |
758 ;; (let ((new-cell (list item))) | |
759 ;; (if (Queue-front queue) | |
760 ;; ;; 最終セルを書き換える | |
761 ;; (setf (cdr (Queue-rear queue)) new-cell) | |
762 ;; ;; キューは空の状態 | |
763 ;; (setf (Queue-front queue) new-cell)) | |
764 ;; (setf (Queue-rear queue) new-cell))) | |
5 | 765 |
6 | 766 ;; deque last queue |
13 | 767 ;; (defun dequeue (queue) |
768 ;; (if (Queue-front queue) | |
769 ;; (prog1 | |
770 ;; (pop (Queue-front queue)) | |
771 ;; (unless (Queue-front queue) | |
772 ;; ;; キューは空になった | |
773 ;; (setf (Queue-rear queue) nil))))) | |
5 | 774 |
6 | 775 ;; |
776 ;; USER | |
777 ;; | i | r | d | |
778 ;; ---|-------------------- | |
779 ;; T i | 0\+1 | 0\+1 | 0\+1 | |
780 ;; O ---|-------------------- | |
781 ;; K r | +1\0 | 0\X | i\X | |
782 ;; E ---|-------------------- | |
783 ;; N d | +1\0 | X\i | X\X | |
784 ;; | |
5 | 785 (defun dequeue-all (queue) |
786 "clean queue" | |
787 (while (Queue-front queue) | |
788 (dequeue queue))) | |
789 | |
790 (defun redit-line-translate-out (cque rque) | |
791 "redit line translate for output" | |
792 (let ((cc) (rc) (xcc) (xrc) (ccc) (crc) (cignore 0) (rignore 0)) | |
6 | 793 (setq xcc 0) |
794 (setq xrc 0) | |
795 (setq cignore 0) | |
796 (setq rignore 0) | |
797 (while cque | |
798 (setq cc (car cque)) | |
799 (while rque | |
800 (setq rc (car rque)) | |
801 ;; -------- translation ------------ | |
5 | 802 |
8 | 803 (if (< (+ (redit-get-line-from-queue cc) xcc) (+ (redit-get-line-from-queue rc) xrc)) |
29 | 804 (if (= (redit-get-command-from-queue cc) redit-insert-command) (setq xrc (- xrc 1))) |
805 (if (= (redit-get-command-from-queue cc) redit-delete-line-command) (setq xrc (- xrc 1)))) | |
5 | 806 |
8 | 807 (if (> (+ (redit-get-line-from-queue cc) xcc) (+ (redit-get-line-from-queue rc) xrc)) |
29 | 808 (if (= (redit-get-command-from-queue rc) redit-insert-command) (setq xcc (+ xcc 1))) |
809 (if (= (redit-get-command-from-queue rc) redit-delete-line-command) (setq xcc (- xcc 1)))) | |
5 | 810 |
8 | 811 (if (= (+ (redit-get-line-from-queue cc) xcc) (+ (redit-get-line-from-queue rc) xrc)) |
29 | 812 (if (= (redit-get-command-from-queue rc) redit-insert-command) (setq xcc (+ xcc 1))) |
813 (if (= (redit-get-command-from-queue rc) redit-replace-command) | |
814 (if (= (redit-get-command-from-queue cc) redit-insert-command) (setq xrc (+ xrc 1))) | |
815 (if (= (redit-get-command-from-queue cc) redit-replace-command) (setq cignore 1)) | |
816 (if (= (redit-get-command-from-queue cc) redit-delete-line-command) (setq crc redit-insert-command) (setq cignore 1)))) | |
5 | 817 |
29 | 818 (if (= (redit-get-command-from-queue rc) redit-delete-line-command) |
819 (if (= (redit-get-command-from-queue cc) redit-insert-command) (setq xrc (+ (redit-get-line-from-queue rc) 1))) | |
820 (if (= (redit-get-command-from-queue cc) redit-replace-command) (setq cignore 1)) | |
821 (if (= (redit-get-command-from-queue cc) redit-delete-line-command) (setq cignore 1) (setq rignore 1))) | |
5 | 822 |
6 | 823 ;; -------- translation ------------ |
5 | 824 |
6 | 825 (setq rque (cdr rque))) ;; while rque |
5 | 826 |
6 | 827 ;; ignore |
828 (if (= cignore 1) | |
829 ;; xxx | |
830 (setq cignore 0)) | |
831 (if (= rignore 1) | |
832 ;; xxx | |
833 (setq cignore 0)) | |
5 | 834 |
6 | 835 ;; -------- add after que ------------ |
8 | 836 (enqueue after-CQ (concat (format "%2d%2d%9d" (redit-get-command-from-queue cc) (redit-get-uid-from-queue cc) (+ (redit-get-line-from-queue cc) xcc)) (redit-get-text-from-queue cc))) |
6 | 837 ;; -------- add after que ------------ |
5 | 838 |
6 | 839 (setq cque (cdr cque))) ;; while cque |
840 ) ;; let | |
841 ) ;; defun | |
5 | 842 |
843 (defun redit-line-translate-in (cque rque) | |
844 "redit line translate for input" | |
845 (let ((cc) (rc) (xcc) (xrc) (ccc) (crc) (cignore 0) (rignore 0)) | |
6 | 846 (setq xcc 0) |
847 (setq xrc 0) | |
5 | 848 (setq cignore 0) |
849 (setq rignore 0) | |
6 | 850 (while rque |
851 (setq rc (car rque)) | |
852 (setq cignore 0) | |
853 (setq rignore 0) | |
854 (while cque | |
855 (setq cc (car cque)) | |
856 ;; -------- translation ------------ | |
5 | 857 |
6 | 858 (if (and (= cignore 1) (= rignore 1)) |
8 | 859 (if (< (+ (redit-get-line-from-queue rc) xrc) (+ (redit-get-line-from-queue cc) xcc)) |
29 | 860 (if (= (redit-get-command-from-queue rc) redit-insert-command) (setq xcc (+ xcc 1))) |
861 (if (= (redit-get-command-from-queue rc) redit-delete-line-command) (setq xcc (- xcc 1)))) | |
5 | 862 |
8 | 863 (if (> (+ (redit-get-line-from-queue rc) xrc) (+ (redit-get-line-from-queue cc) xcc)) |
29 | 864 (if (= (redit-get-command-from-queue cc) redit-insert-command) (setq xrc (- xrc 1))) |
865 (if (= (redit-get-command-from-queue cc) redit-delete-line-command) (setq xrc (+ xrc 1)))) | |
5 | 866 |
8 | 867 (if (= (+ (redit-get-line-from-queue rc) xrc) (+ (redit-get-line-from-queue cc) xcc)) |
29 | 868 (if (= (redit-get-command-from-queue cc) redit-insert-command) (setq xcc (+ xcc 1))) |
869 (if (= (redit-get-command-from-queue cc) redit-replace-command) | |
870 (if (= (redit-get-command-from-queue rc) redit-insert-command) (setq xcc (+ xcc 1))) | |
871 (if (= (redit-get-command-from-queue rc) redit-replace-command) (setq cignore 1)) | |
872 (if (= (redit-get-command-from-queue rc) redit-delete-line-command) (setq crc redit-insert-command) (setq cignore 1)))) | |
873 (if (= (redit-get-command-from-queue cc) redit-delete-line-command) | |
874 (if (= (redit-get-command-from-queue rc) redit-insert-command) (setq xcc (+ (redit-get-line-from-queue cc) 1))) | |
875 (if (= (redit-get-command-from-queue rc) redit-replace-command) (setq crc redit-insert-command) (setq cignore 1)) | |
876 (if (= (redit-get-command-from-queue rc) redit-delete-line-command) (setq cignore 1) (setq rignore 1)))) | |
5 | 877 |
6 | 878 ;; -------- translation ------------ |
5 | 879 |
6 | 880 (setq cque (cdr cque))) ;; while rque |
5 | 881 |
6 | 882 ;; ignore |
883 (if (= cignore 1) | |
884 ;; xxx | |
885 (setq cignore 0)) | |
886 (if (= rignore 1) | |
887 ;; xxx | |
888 (setq rignore 0)) | |
5 | 889 |
6 | 890 ;; -------- add after que ------------ |
8 | 891 (enqueue after-RQ (concat (format "%2d%2d%9d" (redit-get-command-from-queue rc) (redit-get-uid-from-queue rc) (+ (redit-get-line-from-queue rc) xrc)) (redit-get-text-from-queue rc))) |
6 | 892 ;; -------- add after que ------------ |
5 | 893 |
6 | 894 (setq rque (cdr rque))) ;; while cque |
895 ) ;; let | |
896 ) ;; defun | |
5 | 897 |
6 | 898 ;; for debug |
5 | 899 (defun redit-client-print-command-queue () |
900 "Print command queue." | |
901 (interactive) | |
7 | 902 ;;(message redit-client-command-queue)) |
903 (let ((queue redit-client-command-queue)) | |
904 (while (Queue-front queue) | |
905 (message (Queue-front queue)) | |
906 (setq queue (cdr (Queue-rear queue))) | |
907 ) | |
908 )) | |
5 | 909 |
910 (defun redit-client-print-recive-queue () | |
911 "Print command queue." | |
912 (interactive) | |
913 (print redit-client-receive-queue)) | |
914 | |
915 (defun redit-client-print-send-queue () | |
916 "Print command queue." | |
917 (interactive) | |
8 | 918 (print redit-client-send-queue)) |
919 | |
920 (defun redit-get-command-from-queue (queue) | |
921 (nth 0 (Queue-front queue))) | |
922 (defun redit-get-line-from-queue (queue) | |
923 (nth 1 (Queue-front queue))) | |
924 (defun redit-get-uid-from-queue (queue) | |
925 (nth 2 (Queue-front queue))) | |
926 (defun redit-get-text-from-queue (queue) | |
15 | 927 (nth 3 (Queue-front queue))) |