comparison redit-client-sm.el @ 40:1978dc75b15c

*** empty log message ***
author gongo
date Tue, 19 Aug 2008 16:36:10 +0900
parents dd22e85d6eab
children 991526bc3f16
comparison
equal deleted inserted replaced
39:ca6df6937dd1 40:1978dc75b15c
1 ;; $Id$ 1 ;; $Id$
2 2
3 (eval-when-compile (require 'cl)) 3 ;; cl-macs でマクロとして定義されているため
4 ;; cl.el をインクルードしておく
5 (require 'cl)
4 6
5 ;; 7 ;;
6 ;; REP Command 8 ;; REP Command
7 ;; 9 ;;
8 ;; 32 bits 32 32 32 32 32 textsiz 10 ;; 32 bits 32 32 32 32 32 textsiz
55 "Behind cursor line of remote-edit client.") 57 "Behind cursor line of remote-edit client.")
56 58
57 (defvar redit-max-string-length 1004 59 (defvar redit-max-string-length 1004
58 "Read-Write max length of string.") 60 "Read-Write max length of string.")
59 61
60 ;; REP $B$,;HMQ$9$kJ8;z%3!<%I(B 62 ;; REP が使用する文字コード
61 (defvar rep-string-encoding 'utf-8) 63 (defconst rep-string-encoding 'utf-8)
62 64
63 (defvar redit-open-command 1) 65 ;; port
64 (defvar redit-open-ack 2) 66 (defconst redit-process-port 8766)
65 (defvar redit-read-command 3) 67
66 (defvar redit-read-ack 4) 68 (defconst redit-open-command 1)
67 (defvar redit-read-finish-ack 5) ;; obsolete 69 (defconst redit-open-ack 2)
68 (defvar redit-insert-command 6) 70 (defconst redit-read-command 3)
69 (defvar redit-insert-ack 7) 71 (defconst redit-read-ack 4)
70 ;;(defvar redit-write-still-ack "7") 72 (defconst redit-read-finish-ack 5) ;; obsolete
71 ;;(defvar redit-write-finish-ack "8") 73 (defconst redit-insert-command 6)
72 (defvar redit-delete-line-command 9) 74 (defconst redit-insert-ack 7)
73 (defvar redit-delete-line-ack 10) 75 ;;(defconst redit-write-still-ack "7")
74 (defvar redit-close-command 11) 76 ;;(defconst redit-write-finish-ack "8")
75 (defvar redit-close-ack 12) 77 (defconst redit-delete-line-command 9)
76 (defvar redit-replace-command 13) 78 (defconst redit-delete-line-ack 10)
77 (defvar redit-replace-ack 14) 79 (defconst redit-close-command 11)
78 (defvar redit-nop-command 15) 80 (defconst redit-close-ack 12)
81 (defconst redit-replace-command 13)
82 (defconst redit-replace-ack 14)
83 (defconst redit-nop-command 15)
79 84
80 ;; REP Session 85 ;; REP Session
81 (defvar redit-join-command 41) 86 (defconst redit-join-command 41)
82 (defvar redit-join-ack-command 42) 87 (defconst redit-join-ack-command 42)
83 (defvar redit-get-command 43) ;; obsolete 88 (defconst redit-get-command 43) ;; obsolete
84 (defvar redit-get-ack-command 44) ;; obsolete 89 (defconst redit-get-ack-command 44) ;; obsolete
85 (defvar redit-put-command 45) 90 (defconst redit-put-command 45)
86 (defvar redit-put-ack-command 46) 91 (defconst redit-put-ack-command 46)
87 (defvar redit-select-command 47) ;; obsolete 92 (defconst redit-select-command 47) ;; obsolete
88 (defvar redit-select-ack-command 48) 93 (defconst redit-select-ack-command 48)
89 (defvar redit-register-command 49) ;; obsolete 94 (defconst redit-register-command 49) ;; obsolete
90 (defvar redit-register-ack-command 50) ;; obsolete 95 (defconst redit-register-ack-command 50) ;; obsolete
91 (defvar redit-deregister-command 51) ;; obsolete 96 (defconst redit-deregister-command 51) ;; obsolete
92 (defvar redit-deregister-ack-command 52) ;; obsolete 97 (defconst redit-deregister-ack-command 52) ;; obsolete
93 (defvar redit-quit-command 53) 98 (defconst redit-quit-command 53)
94 (defvar redit-quit-ack-command 54) 99 (defconst redit-quit-ack-command 54)
95 100
96 ;;;;;;;;;;;;;;;;;; 101 ;;;;;;;;;;;;;;;;;;
97 ;; Queue struct ;; 102 ;; Queue struct ;;
98 ;;;;;;;;;;;;;;;;;; 103 ;;;;;;;;;;;;;;;;;;
99 (defstruct Queue (front nil) (rear nil)) 104 (defstruct Queue (front nil) (rear nil))
242 (set-process-sentinel redit-client-process nil) 247 (set-process-sentinel redit-client-process nil)
243 (condition-case () ;; 248 (condition-case () ;;
244 (delete-process redit-client-process) (error nil)))) 249 (delete-process redit-client-process) (error nil))))
245 250
246 (setq redit-client-process (open-network-stream 251 (setq redit-client-process (open-network-stream
247 "redit-client" nil "localhost" 8766)) 252 "redit-client" nil "localhost"
253 redit-process-port))
248 254
249 ;; called when the process changes state 255 ;; called when the process changes state
250 (set-process-sentinel redit-client-process 'redit-client-sentinel) 256 (set-process-sentinel redit-client-process 'redit-client-sentinel)
251 257
252 ;; called when the process receive packet 258 ;; called when the process receive packet
253 (set-process-filter redit-client-process 'redit-client-process-filter) 259 (set-process-filter redit-client-process 'redit-client-process-filter)
254 260
255 ;; //Code of the process input/output buffer is rep-string-encoding 261 ;; //Code of the process input/output buffer is rep-string-encoding
256 ;; //(set-process-coding-system redit-client-process rep-string-encoding rep-string-encoding) 262 ;; //(set-process-coding-system redit-client-process rep-string-encoding rep-string-encoding)
257 ;; $BAw<u?.$9$k%G!<%?$O%P%$%J%j$GLc$o$J$$$H$$$1$J$$!#(B 263 ;; 送受信するデータはバイナリで貰わないといけない。
258 ;; utf-8 $B$8$c$J$/$F$:$l$k2DG=@-$,$"$k$N$G(B 264 ;; utf-8 じゃなくてずれる可能性があるので
259 (set-process-coding-system redit-client-process 'binary 'binary) 265 (set-process-coding-system redit-client-process 'binary 'binary)
260 266
261 ;; $B%W%m%;%9$,@8$-$F$k$H$-!"(B emacs $B$r=*N;$7$h$&$H$9$k$H(B 267 ;; プロセスが生きてるとき、 emacs を終了しようとすると
262 ;; $B!V(Bprocess$B$,$&$s$?$i$G!"K\Ev$K@Z$C$F$b$$$$$N(B y/n$B!W(B 268 ;; 「processがうんたらで、本当に切ってもいいの y/n」
263 ;; $B$C$FJ9$+$l$k!#(Bnil $B$F$d$C$F$*$/$HJ9$+$l$J$$!#(B 269 ;; って聞かれる。nil てやっておくと聞かれない。
264 ;; quit $B$rF~$l$k$s$J$i(B t $B$K$7$FM-8z$K$9$Y$-$J$s$@$m$&$+!#(B 270 ;; quit を入れるんなら t にして有効にすべきなんだろうか。
265 ;; $B:#$O(B nil $B$G(B 271 ;; 今は nil で
266 (set-process-query-on-exit-flag redit-client-process nil) 272 (set-process-query-on-exit-flag redit-client-process nil)
267 273
268 ;; not necessary since Emacs 21.1 274 ;; not necessary since Emacs 21.1
269 ;; (make-local-hook 'before-change-functions) 275 ;; (make-local-hook 'before-change-functions)
270 ;; (make-local-hook 'after-change-functions) 276 ;; (make-local-hook 'after-change-functions)
277 (concat 283 (concat
278 (redit-pack 284 (redit-pack
279 redit-join-command 285 redit-join-command
280 0 0 0 0 (string-bytes bufname)) 286 0 0 0 0 (string-bytes bufname))
281 (buffer-name))) 287 (buffer-name)))
282 (message "join-command")) 288 (message (concat
289 "join-command: "
290 (number-to-string redit-join-command) " "
291 (number-to-string 0) " "
292 (number-to-string 0) " "
293 (number-to-string 0) " "
294 (number-to-string 0) " "
295 (number-to-string (string-bytes bufname)) " "
296 "\"" bufname "\""
297 )))
283 ;; PUT if buffer-name is not "*scratch*" 298 ;; PUT if buffer-name is not "*scratch*"
284 (progn 299 (progn
285 (process-send-string 300 (process-send-string
286 redit-client-process 301 redit-client-process
287 (concat 302 (concat
293 ))) 308 )))
294 309
295 (defun redit-quit-command () 310 (defun redit-quit-command ()
296 "quit" 311 "quit"
297 (interactive) 312 (interactive)
313 ;; process kill とかしよう
298 ) 314 )
299 315
316
317 ;; obsolete
300 (defun redit-client-open (file) 318 (defun redit-client-open (file)
301 (interactive "P") 319 (interactive "P")
302 (if redit-client-process 320 (if redit-client-process
303 (if (setq file (read-string "Filename: ")) 321 (if (setq file (read-string "Filename: "))
304 (progn 322 (progn
305 ;; redit-client-process $B$N(B input $B$KJ8;zNs$rAw?.$9$k(B 323 ;; redit-client-process の input に文字列を送信する
306 (process-send-string 324 (process-send-string
307 redit-client-process 325 redit-client-process
308 ;; redit-open-command (01) $B$H(B file $B$rO"7k$5$;$k(B 326 ;; redit-open-command (01) と file を連結させる
309 (concat redit-open-command file)) 327 (concat redit-open-command file))
310 ;; redit-client-process $B$+$i=PNO$rF@$k$^$G%k!<%W(B 328 ;; redit-client-process から出力を得るまでループ
311 (while (eq nil (accept-process-output redit-client-process))))) 329 (while (eq nil (accept-process-output redit-client-process)))))
312 (error "redit-client is not running."))) 330 (error "redit-client is not running.")))
313 331
314 ;; redit-client-process $B$K(Bread$B%3%^%s%I$H%P%C%U%!HV9f!"9THV9f$rAw$j!"(B 332 ;; redit-client-process にreadコマンドとバッファ番号、行番号を送り、
315 ;; $B%5!<%P$+$i$N=PNO$rF@$k(B 333 ;; サーバからの出力を得る
316 (defun redit-client-read-line (linenum) 334 (defun redit-client-read-line (linenum)
317 (if redit-client-process 335 (if redit-client-process
318 (save-excursion 336 (save-excursion
319 (progn 337 (progn
320 (process-send-string 338 (process-send-string
323 (while (eq nil 341 (while (eq nil
324 (accept-process-output redit-client-process))))) 342 (accept-process-output redit-client-process)))))
325 (error "redit-client is not running."))) 343 (error "redit-client is not running.")))
326 344
327 345
328 ;; linenum$B$G;XDj$5$l$?9T$NJT=8$r%5!<%P$XAw$k(B 346 ;; linenumで指定された行の編集をサーバへ送る
329 ;; redit-client-process $B$K(B insert $B%3%^%s%I$H(B 347 ;; redit-client-process に insert コマンドと
330 ;;$B%P%C%U%!HV9f!"9THV9f!"9T$NFbMF$rAw$j!"(B Ack $B$r<u$1<h$k(B 348 ;; バッファ番号、行番号、行の内容を送り、 Ack を受け取る
331 (defun redit-client-insert-line (linenum isnewline) 349 (defun redit-client-insert-line (linenum isnewline)
332 (if redit-client-process 350 (if redit-client-process
333 (save-excursion 351 (save-excursion
334 (let ((beginp) (endp) (text) (length) (packet) (sinfo)) 352 (let ((beginp) (endp) (text) (length) (packet) (sinfo))
335 (setq beginp 353 (setq beginp
371 text 389 text
372 (if (eq isnewline t) "\n"))) 390 (if (eq isnewline t) "\n")))
373 (process-send-string redit-client-process packet))) 391 (process-send-string redit-client-process packet)))
374 (error "redit-client is not running."))) 392 (error "redit-client is not running.")))
375 393
376 ;; linenum $B$G;XDj$7$?9T$N:o=|L?Na$r(B redit-client-process $B$KAw?.$9$k(B 394 ;; linenum で指定した行の削除命令を redit-client-process に送信する
377 (defun redit-client-delete-line (linenum) 395 (defun redit-client-delete-line (linenum)
378 (if redit-client-process 396 (if redit-client-process
379 (save-restriction 397 (save-restriction
380 398
381 ;; $B%H!<%/%s$,2s$C$F$/$k$N$rBT$D>l9g$O!"(B 399 ;; トークンが回ってくるのを待つ場合は、
382 ;; $B$9$0$K(B process-send-string $B$;$:$K(B 400 ;; すぐに process-send-string せずに
383 ;; $BJT=8>pJs$rJ]B8$9$k$@$1$K$H$I$a$k(B 401 ;; 編集情報を保存するだけにとどめる
384 ;;(enqueue redit-client-command-queue 402 ;;(enqueue redit-client-command-queue
385 ;;(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")) 403 ;;(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"))
386 404
387 ;; insert lines on server buffer 405 ;; insert lines on server buffer
388 (process-send-string 406 (process-send-string
392 redit-client-session-id 410 redit-client-session-id
393 redit-client-editor-id 411 redit-client-editor-id
394 (gen-seqno) linenum 0)))) 412 (gen-seqno) linenum 0))))
395 (error "redit-client is not running."))) 413 (error "redit-client is not running.")))
396 414
397 ;; redit-client-process $B$X(Bclose$B%3%^%s%I$rAw$k(B 415 ;; redit-client-process へcloseコマンドを送る
398 (defun redit-client-close () 416 (defun redit-client-close ()
399 "Save File and exit Remote-edit client." 417 "Save File and exit Remote-edit client."
400 (interactive) 418 (interactive)
401 (let ((line redit-client-previous-line)) 419 (let ((line redit-client-previous-line))
402 (redit-client-insert-line line nil) 420 (redit-client-insert-line line nil)
409 redit-close-command 427 redit-close-command
410 redit-client-session-id 428 redit-client-session-id
411 redit-client-editor-id 429 redit-client-editor-id
412 (gen-seqno) 0 0) "\n")))) 430 (gen-seqno) 0 0) "\n"))))
413 431
414 ;; redit-client-process $B$r=*N;$5$;$k(B 432 ;; redit-client-process を終了させる
415 (defun redit-client-kill () 433 (defun redit-client-kill ()
416 "Stop Remote-Edit client process." 434 "Stop Remote-Edit client process."
417 (interactive) 435 (interactive)
418 (if redit-client-process 436 (if redit-client-process
419 (kill-process redit-client-process)) 437 (kill-process redit-client-process))
420 (if redit-client-buffer 438 (if redit-client-buffer
421 (kill-buffer redit-client-buffer)) 439 (kill-buffer redit-client-buffer))
422 (kill-all-local-variables)) 440 (kill-all-local-variables))
423 441
424 ;; proc == redit-client-process 442 ;; proc == redit-client-process
425 ;; string == redit-client-process $B$+$i$N(B output 443 ;; string == redit-client-process からの output
426 (defun redit-client-process-filter (proc string) 444 (defun redit-client-process-filter (proc string)
427 ;; $B%P%C%U%!%m!<%+%k$J%U%C%/%j%9%H(B before-change-functions $B$+$i(B 445 ;; バッファローカルなフックリスト before-change-functions から
428 ;; redit-client-before-change-function $B$r<h$j=|$/(B. after $B$bF1$8(B 446 ;; redit-client-before-change-function を取り除く. after も同じ
429 ;; proc $B$+$i$N=PNO$K(B hook $B$7$J$$$h$&$K(B 447 ;; proc からの出力に hook しないように
430 (remove-hook 'before-change-functions 448 (remove-hook 'before-change-functions
431 'redit-client-before-change-function t) 449 'redit-client-before-change-function t)
432 (remove-hook 'after-change-functions 450 (remove-hook 'after-change-functions
433 'redit-client-after-change-function t) 451 'redit-client-after-change-function t)
434 452
435 (message (concat "redit-client-process-filter: " string)) 453 (message (concat "redit-client-process-filter: " string))
436 (let ((command (rep-get-command-from-pkt string))) 454 (let ((command (rep-get-command-from-pkt string)))
437 ;; command $B$,$I$NL?Na$+$rH=CG$7!"BP1~$7$?=hM}$r$9$k!#(Bcase $B$_$?$$(B 455 ;; command がどの命令かを判断し、対応した処理をする。case みたい
438 (cond 456 (cond
439 ;; FIXME 457 ;; FIXME
440 ;;((if (= command redit-open-ack) 458 ;;((if (= command redit-open-ack)
441 ;;(if (/= redit-client-editor-id (rep-get-editor-id-from-pkt string)) 459 ;;(if (/= redit-client-editor-id (rep-get-editor-id-from-pkt string))
442 ;;(redit-client-exec-open-ack string)))) 460 ;;(redit-client-exec-open-ack string))))
480 'redit-client-before-change-function t t) 498 'redit-client-before-change-function t t)
481 (add-hook 'after-change-functions 499 (add-hook 'after-change-functions
482 'redit-client-after-change-function t t) 500 'redit-client-after-change-function t t)
483 ) 501 )
484 502
485 ;; window-scroll-functions $B$K(B hook $B$5$l$k!#(B 503 ;; window-scroll-functions に hook される。
486 ;; window $B$,%9%/%m!<%k$9$kEY$K8F$P$l$k(B 504 ;; window がスクロールする度に呼ばれる
487 (defun redit-client-update-function (window window-start) 505 (defun redit-client-update-function (window window-start)
488 (if (equal (window-buffer) redit-client-buffer) 506 (if (equal (window-buffer) redit-client-buffer)
489 (save-excursion 507 (save-excursion
490 (remove-hook 'before-change-functions 508 (remove-hook 'before-change-functions
491 'redit-client-before-change-function t) 509 'redit-client-before-change-function t)
515 (add-hook 'before-change-functions 533 (add-hook 'before-change-functions
516 'redit-client-before-change-function t t) 534 'redit-client-before-change-function t t)
517 (add-hook 'after-change-functions 535 (add-hook 'after-change-functions
518 'redit-client-after-change-function t t)))) 536 'redit-client-after-change-function t t))))
519 537
520 ;; currentp $B$N0LCV$N9T?t$rJV$9(B 538 ;; currentp の位置の行数を返す
521 (defun real-count-lines (currentp) 539 (defun real-count-lines (currentp)
522 (+ (count-lines (point-min) currentp) 540 (+ (count-lines (point-min) currentp)
523 (if (= (save-excursion (goto-char currentp) (current-column)) 541 (if (= (save-excursion (goto-char currentp) (current-column))
524 0) 542 0)
525 1 0))) 543 1 0)))
529 (while exprs 547 (while exprs
530 (eval (car exprs)) 548 (eval (car exprs))
531 (setq exprs (cdr exprs))) 549 (setq exprs (cdr exprs)))
532 (goto-char saved-point))) 550 (goto-char saved-point)))
533 551
534 ;; before-change-functions $B$K(B hook $B$5$l$k(B 552 ;; before-change-functions に hook される
535 ;; $B%P%C%U%!$KJQ99$,$"$k$?$S$K8F$P$l$k!#%P%C%U%!$,JQ99$5$l$kA0$K8F$P$l$k!#(B 553 ;; バッファに変更があるたびに呼ばれる。バッファが変更される前に呼ばれる。
536 ;; begin $B$H(B end $B$K$OJQ99A0$NJQ99ItJ,$N;O$^$j$H=*$o$j$N(B point $B$,F~$k(B 554 ;; begin と end には変更前の変更部分の始まりと終わりの point が入る
537 (defun redit-client-before-change-function (begin end) 555 (defun redit-client-before-change-function (begin end)
538 (let ((beginl (real-count-lines begin)) ;; begin $B$N9THV9f(B 556 (let ((beginl (real-count-lines begin)) ;; begin の行番号
539 (endl (real-count-lines end)) ;; end $B$N9THV9f(B 557 (endl (real-count-lines end)) ;; end の行番号
540 (currline)) ;; currline = nil 558 (currline)) ;; currline = nil
541 559
542 ;; (message "before-change-function") 560 ;; (message "before-change-function")
543 561
544 ;; $BA02s$NJT=8$7$?9T$H:#2s$NJT=8$7$?9T$,(B 562 ;; 前回の編集した行と今回の編集した行が
545 ;; $B0c$C$F$$$?$iA02s$NJT=8$r%5!<%P$XAw$k(B 563 ;; 違っていたら前回の編集をサーバへ送る
546 (if (not (= beginl redit-client-previous-edited-line)) 564 (if (not (= beginl redit-client-previous-edited-line))
547 (redit-client-insert-line redit-client-previous-edited-line nil)) 565 (redit-client-insert-line redit-client-previous-edited-line nil))
548 566
549 ;; $B:o=|=hM}$G5/$3$k!#(B 567 ;; 削除処理で起こる。
550 ;; (ex. BackSpace$B$K$h$k9T$=$N$b$N$N:o=|(B, C-Space C-w$B$J$I$N%+%C%H(B, ...) 568 ;; (ex. BackSpaceによる行そのものの削除, C-Space C-wなどのカット, ...)
551 (if (< beginl endl) 569 (if (< beginl endl)
552 (progn 570 (progn
553 (setq currline endl) 571 (setq currline endl)
554 ;; endl$B$+$i(Bbeginl$B$^$G9T$N:o=|L?Na$r=P$9(B 572 ;; endlからbeginlまで行の削除命令を出す
555 (while (> currline beginl) 573 (while (> currline beginl)
556 (redit-client-delete-line endl) 574 (redit-client-delete-line endl)
557 (setq currline (- currline 1))))))) 575 (setq currline (- currline 1)))))))
558 576
559 ;; after-change-functions $B$K(B hook $B$5$l$k(B 577 ;; after-change-functions に hook される
560 ;; $B%P%C%U%!$KJQ99$,$"$k$?$S$K8F$P$l$k!#%P%C%U%!$,JQ99$5$l$?8e$K8F$P$l$k(B 578 ;; バッファに変更があるたびに呼ばれる。バッファが変更された後に呼ばれる
561 ;; begin $B$H(B end $B$K$OJQ998e$NJQ99ItJ,$N;O$^$j$H=*$o$j$N(B point $B$,F~$k(B 579 ;; begin と end には変更後の変更部分の始まりと終わりの point が入る
562 (defun redit-client-after-change-function (begin end length) 580 (defun redit-client-after-change-function (begin end length)
563 (let ((beginl (real-count-lines begin)) 581 (let ((beginl (real-count-lines begin))
564 (endl (real-count-lines end)) 582 (endl (real-count-lines end))
565 (currline)) 583 (currline))
566 584
567 ;; $BA^F~=hM}$G5/$3$k!#(B 585 ;; 挿入処理で起こる。
568 ;; (ex. $B2~9T(B, C-y $B$J$I$N%Z!<%9%H(B, ...) 586 ;; (ex. 改行, C-y などのペースト, ...)
569 (if (< beginl endl) 587 (if (< beginl endl)
570 (progn 588 (progn
571 (setq currline beginl) 589 (setq currline beginl)
572 ;; beginl$B$+$i(Bendl$B$^$GA^F~L?Na$r=P$9(B 590 ;; beginlからendlまで挿入命令を出す
573 (while (<= currline endl) 591 (while (<= currline endl)
574 (progn (redit-client-insert-line currline nil) 592 (progn (redit-client-insert-line currline nil)
575 (setq currline (+ currline 1)))))) 593 (setq currline (+ currline 1))))))
576 (setq redit-client-previous-edited-line endl))) 594 (setq redit-client-previous-edited-line endl)))
577 595
578 ;; $B0z$-?t$GM?$($i$l$?(B string (line_num + text_data) $B$+$i(B 596 ;; 引き数で与えられた string (line_num + text_data) から
579 ;; $B;XDj$5$l$?9T$r:o=|$7!"$=$3$K(B text_data $B$rA^F~$9$k(B 597 ;; 指定された行を削除し、そこに text_data を挿入する
580 (defun redit-client-exec-insert-line (string) 598 (defun redit-client-exec-insert-line (string)
581 (let ((linenum (rep-get-line-number-from-pkt string)) ;; $B9THV9f(B 599 (let ((linenum (rep-get-line-number-from-pkt string)) ;; 行番号
582 (text (rep-get-text-from-pkt string))) ;; $B%F%-%9%H%G!<%?(B 600 (text (rep-get-text-from-pkt string))) ;; テキストデータ
583 (if (< (real-count-lines (point-max)) linenum) 601 (if (< (real-count-lines (point-max)) linenum)
584 (progn 602 (progn
585 (call-interactively 'end-of-buffer) (newline))) 603 (call-interactively 'end-of-buffer) (newline)))
586 (goto-line linenum) 604 (goto-line linenum)
587 (delete-region (progn (beginning-of-line) (point)) ;; $B9T$NF,$+$i(B 605 (delete-region (progn (beginning-of-line) (point)) ;; 行の頭から
588 (progn (end-of-line) (point))) ;; $B9T$NKvHx$^$G:o=|(B 606 (progn (end-of-line) (point))) ;; 行の末尾まで削除
589 (insert text) ;; $B%F%-%9%H%G!<%?$rA^F~(B 607 (insert text) ;; テキストデータを挿入
590 (message (concat "recv = " text ", line = " (number-to-string linenum)))
591 )) 608 ))
592 609
593 ;; $B0z$-?t(B string (line_num + text_data) $B$G;XDj$5$l$?9T$r:o=|$9$k(B 610 ;; 引き数 string (line_num + text_data) で指定された行を削除する
594 (defun redit-client-exec-delete-line (string) 611 (defun redit-client-exec-delete-line (string)
595 (let ((linenum (rep-get-line-number-from-pkt string))) 612 (let ((linenum (rep-get-line-number-from-pkt string)))
596 (goto-line linenum) 613 (goto-line linenum)
597 ;; $B9TF,$+$iKvHx$^$G$N%F%-%9%H$r:o=|(B 614 ;; 行頭から末尾までのテキストを削除
598 (delete-region (progn (beginning-of-line) (point)) 615 (delete-region (progn (beginning-of-line) (point))
599 (progn (end-of-line) (point))) 616 (progn (end-of-line) (point)))
600 ;; $B;XDj$5$l$?9T<+BN$r:o=|(B 617 ;; 指定された行自体を削除
601 ;; $B;XDj$5$l$?9THV9f$OJL$N9T$NHV9f$rI=$9$3$H$K$J$k(B 618 ;; 指定された行番号は別の行の番号を表すことになる
602 (if (= (point) (point-max)) 619 (if (= (point) (point-max))
603 (delete-backward-char 1) 620 (delete-backward-char 1)
604 (delete-char 1))) 621 (delete-char 1)))
605 ) 622 )
606 623
607 ;; $B2?MQ!)Aj<j$,3+$$$F$k%U%!%$%k$r<hF@$9$k$N$+$J!)(B 624 ;; 何用?相手が開いてるファイルを取得するのかな?
608 ;; read $B$_$?$$$J$b$s!)(B 625 ;; read みたいなもん?
609 ;; $B0z$-?t(B string (buf_num + line_num + text_data) 626 ;; 引き数 string (buf_num + line_num + text_data)
610 ;;(defun redit-client-exec-open-ack (string) 627 ;;(defun redit-client-exec-open-ack (string)
611 ;; (save-excursion 628 ;; (save-excursion
612 ;; (let ((bufnum (rep-get-editor-id-from-pkt string)) 629 ;; (let ((bufnum (rep-get-editor-id-from-pkt string))
613 ;; (linenum (rep-get-line-number-from-pkt string))) 630 ;; (linenum (rep-get-line-number-from-pkt string)))
614 ;; (make-variable-buffer-local 'redit-client-buffer-name) 631 ;; (make-variable-buffer-local 'redit-client-buffer-name)
615 ;; (make-variable-buffer-local 'redit-client-editor-id) 632 ;; (make-variable-buffer-local 'redit-client-editor-id)
616 ;; (setq redit-client-buffer-name 633 ;; (setq redit-client-buffer-name
617 ;; (rep-get-text-from-pkt string)) 634 ;; (rep-get-text-from-pkt string))
618 ;; (setq redit-client-buffer (get-buffer-create redit-client-buffer-name)) 635 ;; (setq redit-client-buffer (get-buffer-create redit-client-buffer-name))
619 ;; ;; $B%W%m%;%9$H%P%C%U%!$r4XO"$E$1$k(B 636 ;; ;; プロセスとバッファを関連づける
620 ;; (set-process-buffer redit-client-process redit-client-buffer) 637 ;; (set-process-buffer redit-client-process redit-client-buffer)
621 ;; (switch-to-buffer redit-client-buffer) ;; window$B$N%P%C%U%!$rJQ99$9$k(B 638 ;; (switch-to-buffer redit-client-buffer) ;; windowのバッファを変更する
622 ;; (make-variable-buffer-local 'redit-client-previous-edited-line) 639 ;; (make-variable-buffer-local 'redit-client-previous-edited-line)
623 ;; (make-variable-buffer-local 'redit-client-after-edited-line) 640 ;; (make-variable-buffer-local 'redit-client-after-edited-line)
624 ;; (make-variable-buffer-local 'redit-client-line-max-in-server) 641 ;; (make-variable-buffer-local 'redit-client-line-max-in-server)
625 ;; (make-variable-buffer-local 'redit-client-previous-line) 642 ;; (make-variable-buffer-local 'redit-client-previous-line)
626 ;; (setq redit-client-line-max-in-server linenum) 643 ;; (setq redit-client-line-max-in-server linenum)
635 ;; ;;'redit-client-after-change-function t t) 652 ;; ;;'redit-client-after-change-function t t)
636 ;; 653 ;;
637 ;; ;; not necessary since Emacs 21.1 654 ;; ;; not necessary since Emacs 21.1
638 ;; ;; (make-local-hook 'window-scroll-functions) 655 ;; ;; (make-local-hook 'window-scroll-functions)
639 ;; 656 ;;
640 ;; ;; window $B$,%9%/%m!<%k$9$kEY$K8F$P$l$k4X?t72(B 657 ;; ;; window がスクロールする度に呼ばれる関数群
641 ;; (add-hook 'window-scroll-functions 658 ;; (add-hook 'window-scroll-functions
642 ;; 'redit-client-update-function t t)))) 659 ;; 'redit-client-update-function t t))))
643 660
644 661
645 ;; string (line_num + text_data) $B$G;XDj$5$l$?9T$K(B 662 ;; string (line_num + text_data) で指定された行に
646 ;; text_data $B$rA^F~$9$k(B 663 ;; text_data を挿入する
647 (defun redit-client-exec-read-ack (string) 664 (defun redit-client-exec-read-ack (string)
648 (save-excursion 665 (save-excursion
649 (let ((lines (rep-get-line-number-from-pkt string))) 666 (let ((lines (rep-get-line-number-from-pkt string)))
650 ;; redit-client-buffer $B$r%+%l%s%H%P%C%U%!$K$9$k(B 667 ;; redit-client-buffer をカレントバッファにする
651 (set-buffer redit-client-buffer) 668 (set-buffer redit-client-buffer)
652 (goto-line lines) 669 (goto-line lines)
653 (beginning-of-line) ;; $B9T$N;O$a$X0\F0(B 670 (beginning-of-line) ;; 行の始めへ移動
654 ;; (delete-region (progn (beginning-of-line) (point)) 671 ;; (delete-region (progn (beginning-of-line) (point))
655 ;; (progn (end-of-line) (point))) 672 ;; (progn (end-of-line) (point)))
656 (insert (rep-get-text-from-pkt string))))) ;; $B%F%-%9%H%G!<%?$rA^F~(B 673 (insert (rep-get-text-from-pkt string))))) ;; テキストデータを挿入
657 674
658 ;; redit-client-line-max-in-server $B$K9THV9f$rF~$l$F$k(B($B$@$1(B) 675 ;; redit-client-line-max-in-server に行番号を入れてる(だけ)
659 (defun redit-client-exec-insert-ack (string) 676 (defun redit-client-exec-insert-ack (string)
660 (save-excursion 677 (save-excursion
661 (set-buffer redit-client-buffer) 678 (set-buffer redit-client-buffer)
662 (let ((linenum (rep-get-line-number-from-pkt string ))) 679 (let ((linenum (rep-get-line-number-from-pkt string )))
663 (setq redit-client-line-max-in-server linenum)))) 680 (setq redit-client-line-max-in-server linenum))))
666 (save-excursion 683 (save-excursion
667 (set-buffer redit-client-buffer) 684 (set-buffer redit-client-buffer)
668 (let ((linenum (rep-get-line-number-from-pkt string))) 685 (let ((linenum (rep-get-line-number-from-pkt string)))
669 (setq redit-client-line-max-in-server linenum)))) 686 (setq redit-client-line-max-in-server linenum))))
670 687
671 ;; $B%W%m%;%9$H%P%C%U%!!"%m!<%+%kJQ?t$r:o=|$9$k(B 688 ;; プロセスとバッファ、ローカル変数を削除する
672 (defun redit-client-exec-close-ack (string) 689 (defun redit-client-exec-close-ack (string)
673 (if redit-client-process 690 (if redit-client-process
674 (kill-process redit-client-process)) 691 (kill-process redit-client-process))
675 (if redit-client-buffer 692 (if redit-client-buffer
676 (kill-buffer redit-client-buffer)) 693 (kill-buffer redit-client-buffer))
677 (kill-all-local-variables)) 694 (kill-all-local-variables))
678 695
679 ;; $B0z$-?t$GM?$($i$l$?(B string $B$+$i(B Editor ID$B$r<hF@$9$k(B 696 ;; 引き数で与えられた string から Editor IDを取得する
680 (defun redit-client-exec-join (string) 697 (defun redit-client-exec-join (string)
681 (let (sinfo) 698 (let (sinfo)
682 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string)) 699 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string))
683 (setq redit-client-session-id (rep-get-session-id-from-pkt string)) 700 (setq redit-client-session-id (rep-get-session-id-from-pkt string))
684 701
691 "] " 708 "] "
692 "test.c")) 709 "test.c"))
693 (message (concat "Your editor id = " (number-to-string redit-client-editor-id))) 710 (message (concat "Your editor id = " (number-to-string redit-client-editor-id)))
694 )) 711 ))
695 712
696 ;; //$B0z$-?t$GM?$($i$l$?(B string $B$+$i(B Session ID$B$r<hF@$9$k(B 713 ;; //引き数で与えられた string から Session IDを取得する
697 ;; $B?7;EMM$G!"(B(put = $B%U%!%$%kM-$j(Bjoin) $B$C$F$3$H$K$J$C$F(B 714 ;; 新仕様で、(put = ファイル有りjoin) ってことになって
698 ;; editor id $B$rJV$9$3$H$K$J$C$?$N$G!"(Bsid $B$H6&$K(B eid $B$b%;!<%V$9$k(B 715 ;; editor id を返すことになったので、sid と共に eid もセーブする
699 (defun redit-client-exec-put (string) 716 (defun redit-client-exec-put (string)
700 (let (sinfo) 717 (let (sinfo)
701 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string)) 718 (setq redit-client-editor-id (rep-get-editor-id-from-pkt string))
702 (setq redit-client-session-id (rep-get-session-id-from-pkt string)) 719 (setq redit-client-session-id (rep-get-session-id-from-pkt string))
703 720
709 (number-to-string redit-client-session-id) 726 (number-to-string redit-client-session-id)
710 "] " 727 "] "
711 "test.c" "\n")) 728 "test.c" "\n"))
712 (message (concat "Your editor id = " (number-to-string redit-client-editor-id) "\n")))) 729 (message (concat "Your editor id = " (number-to-string redit-client-editor-id) "\n"))))
713 730
714 ;; SessionID$B$rL>A0$H$7$?!"?7$7$$%P%C%U%!$r3+$/(B 731 ;; SessionIDを名前とした、新しいバッファを開く
715 ;; FIXME 732 ;; FIXME
716 ;; SessionID$B$G$O$J$/!"(BSession Name$B$,K>$^$7$$(B? 733 ;; SessionIDではなく、Session Nameが望ましい?
717 ;; $B%P%C%U%!$r3+$$$?8e!"(BSession$B$G3+$$$F$k%U%!%$%k$NCf?H$rI=<($9$k$Y$-(B 734 ;; バッファを開いた後、Sessionで開いてるファイルの中身を表示するべき
718 (defun redit-client-exec-select () 735 (defun redit-client-exec-select ()
719 (switch-to-buffer (get-buffer-create 736 (switch-to-buffer (get-buffer-create
720 (number-to-string redit-client-session-id)))) 737 (number-to-string redit-client-session-id))))
721 738
722 ;; $B%W%m%;%9$N>uBV$r8+$F!"BP1~$7$?%a%C%;!<%8$rI=<((B 739 ;; プロセスの状態を見て、対応したメッセージを表示
723 (defun redit-client-sentinel (proc msg) 740 (defun redit-client-sentinel (proc msg)
724 (cond ((eq (process-status proc) 'exit) 741 (cond ((eq (process-status proc) 'exit)
725 (message "Client subprocess exited")) 742 (message "Client subprocess exited"))
726 ((eq (process-status proc) 'signal) 743 ((eq (process-status proc) 'signal)
727 (message "Client subprocess killed")))) 744 (message "Client subprocess killed"))))
744 (defun rep-get-line-number-from-pkt (pkt) 761 (defun rep-get-line-number-from-pkt (pkt)
745 (redit-unpack-int pkt 5)) 762 (redit-unpack-int pkt 5))
746 (defun rep-get-text-size-from-pkt (pkt) 763 (defun rep-get-text-size-from-pkt (pkt)
747 (redit-unpack-int pkt 6)) 764 (redit-unpack-int pkt 6))
748 765
749 ;; $B%3%^%s%I8e$m$K$"$k%F%-%9%H$O(B 766 ;; コマンド後ろにあるテキストは
750 ;; (substring packet 24) 767 ;; (substring packet 24)
751 ;; $B$G$H$l$k$N$G!"(Btext-size $B$OMW$i$J$$!"$H$&$l$7$$!#(B 768 ;; でとれるので、text-size は要らない、とうれしい。
752 ;; $B$,!"(B($BDL?.%F%9%H$^$@$J$N$G(B)$BNc$($PO"B3$7$F%Q%1%C%H$,Aw$i$l$FMh$?$H$-(B 769 ;; が、(通信テストまだなので)例えば連続してパケットが送られて来たとき
753 ;; $B$=$N%Q%1%C%H$,7R$,$C$F$7$^$&$HBLL\!#(B 770 ;; そのパケットが繋がってしまうと駄目。
754 ;; $B$b$7!"(Bprocess-filter $B$,!"0l$D$N%Q%1%C%H$N=hM}$,=*$o$k$^$G(B 771 ;; もし、process-filter が、一つのパケットの処理が終わるまで
755 ;; $B8eB3$N%Q%1%C%H$r;_$a$F$F$/$l$k$J$i$3$l$G$$$1$k$,!#(B 772 ;; 後続のパケットを止めててくれるならこれでいけるが。
756 ;; $BG0$N$?$a!"<u$1<h$C$?(B text-size $B$HHf3S$7$F(B 773 ;; 念のため、受け取った text-size と比較して
757 ;; $BF1$8$J$i$*(Bk$B!"$@$a$J$i$=$N%5%$%:J,$@$1<h$k!"$C$F=j$+!#(B 774 ;; 同じならおk、だめならそのサイズ分だけ取る、って所か。
758 ;; $B:G=i$+$i%5%$%:J,$@$1$G$$$$$s$8$c$M!)$H;W$C$?$j$b$9$k$,!#(B 775 ;; 最初からサイズ分だけでいいんじゃね?と思ったりもするが。
759 ;; $B$"$H!"8=;~E@$G$O%5%$%:$,0c$&(B($B<:GT(B)$B;~$N=hM}$$$l$F$J$$!#(B 776 ;; あと、現時点ではサイズが違う(失敗)時の処理いれてない。
760 (defun rep-get-text-from-pkt (pkt) 777 (defun rep-get-text-from-pkt (pkt)
761 (let ((size) (text)) 778 (let ((size) (text))
762 (setq size (rep-get-text-size-from-pkt pkt)) 779 (setq size (rep-get-text-size-from-pkt pkt))
763 (setq text (substring pkt 24)) 780 (setq text (substring pkt 24))
764 (decode-coding-string text rep-string-encoding))) 781 (decode-coding-string text rep-string-encoding)))