# HG changeset patch # User gongo # Date 1219997195 -32400 # Node ID 312ea950243c9430b4cbc16bfb30572c1a87fc69 # Parent 0f7d2aab0e048ef6c630762301387e8aac7f660e *** empty log message *** diff -r 0f7d2aab0e04 -r 312ea950243c ChangeLog --- a/ChangeLog Thu Aug 28 19:44:23 2008 +0900 +++ b/ChangeLog Fri Aug 29 17:06:35 2008 +0900 @@ -1,10 +1,22 @@ +2008-08-29 Wataru MIYAGUNI + + * redit-client-sm.el: delete + redit-client-session-id + redit-client-editor-id + + を、大域変数から削除しました。 + SessionInfo で管理してて、必要ある毎に + redit-sinfo-get-* とかしてて sinfo 取得 -> sid eid をゲット + とかしてるので。 + まあもしかしたらまた使う事になるかもしれないが、今は要らない。 + 2008-08-28 Wataru MIYAGUNI * tag v20080828-2 現在使ってない関数を抹殺したver いずれ必要になるとは思うが、今は見づらくなるので消去。 - 後ほど必要になったらしたの tag で取ってこればおk。 + 後ほど必要になったら、下の tag で取ってこればおk。 * tag v20080828-1 現在使ってない関数をコメントで残しているver diff -r 0f7d2aab0e04 -r 312ea950243c redit-client-sm.el --- a/redit-client-sm.el Thu Aug 28 19:44:23 2008 +0900 +++ b/redit-client-sm.el Fri Aug 29 17:06:35 2008 +0900 @@ -34,16 +34,6 @@ (defvar redit-client-buffer-number nil "Remote-edit client's buffer-number.") -;; obsolete -;; SessionInfo で管理する -;;(defvar redit-client-editor-id "0" -;;"Remote-edit client's editor-id.") - -;; obsolete -;; SessionInfo で管理する -;;(defvar redit-client-session-id 0 -;;"Remote-edit client's session-id.") - ;; editor local sequence number (defvar redit-client-seqno 0 "Remote-edit client's editor-id.") @@ -76,6 +66,10 @@ (defconst redit-string-coding 'utf-8 "Default coding that REP use") +;; host +(defconst redit-process-hostname "localhost" + "Default hostname that REP use") + ;; port (defconst redit-process-port 8766 "Default port that REP use") @@ -196,8 +190,9 @@ ;; prevline: この Session に対応するバッファで最後に編集された行 ;; send_cmdlist: rep_command list from user to SessionManager ;; recv_cmdlist: rep_command list from SessionManager to user -(defstruct SessionInfo (sid nil) (eid nil) (prevline 1) - (send_cmdlist nil) (recv_cmdlist nil)) +(defstruct SessionInfo "a" (sid nil) (eid nil) (prevline 1) + (send_cmdlist nil) (recv_cmdlist nil) +) ;; create and initialize (defun redit-create-session-info (sid eid) @@ -308,7 +303,7 @@ (delete-process redit-client-process) (error nil)))) (setq redit-client-process (open-network-stream - "redit-client" nil "localhost" + "redit-client" nil redit-process-hostname redit-process-port)) ;; called when the process changes state @@ -327,10 +322,6 @@ ;; 今は nil で (set-process-query-on-exit-flag redit-client-process nil) - ;; not necessary since Emacs 21.1 - ;; (make-local-hook 'before-change-functions) - ;; (make-local-hook 'after-change-functions) - (if (string= "*scratch*" bufname) (progn ;; JOIN if buffer-name is "*scratch*" @@ -364,44 +355,83 @@ ;; _text が指定されている場合、バッファのlinenumの文字列ではなく ;; _text で指定された文字列を送信する (defun redit-client-insert-line (linenum isnewline &optional _text) + "Send \"redit-insert-command\" to SessionManager. + +If current buffer is not connected with SessionManager, +nothing is done (see `redit-client-exec-join', `redit-client-exec-put'). + +* REP command header + +cmd : 6 (redit-insert-command). +sid : Session ID corresponding to current buffer. +eid : Editor ID corresponding to current buffer. + (sid & eid are acquired from hash table `SessionInfo'.) +seq : Sequence Number. it can be acquired it from function `gen-seqno'. +lno : line number, LINENUM, that insert text. +siz : byte size of insert text. + +* REP command text + +String of the LINENUM line of current buffer. +If the optional argument _TEXT is non-nil, text is _TEXT. +IF ISNEWLINE is t, line feed code is added behind text. +" (if redit-client-process (save-excursion - (let (sinfo packet text text-size) - (if (null _text) - (let (beginp endp) - (setq beginp - (progn (goto-line linenum) (beginning-of-line) (point))) - (setq endp - (progn (goto-line linenum) (end-of-line) (point))) - (setq text - (concat (encode-coding-string - (concat (buffer-substring beginp endp) - (if (eq isnewline t) "\n")) - redit-string-coding) - ))) - (setq text _text)) - (setq text-size (string-bytes text)) + (let (sinfo) (setq sinfo (redit-sinfo-get-buf2sinfo (buffer-name))) (if sinfo - (let ((sid (SessionInfo-sid sinfo)) - (eid (SessionInfo-eid sinfo)) (packet)) - - (setq packet (redit-make-packet - redit-insert-command sid eid - (gen-seqno) linenum text-size text)) + (let (text text-size) + (if (null _text) + (let (beginp endp) + (goto-line linenum) + (setq beginp (progn (beginning-of-line) (point))) + (setq endp (progn (end-of-line) (point))) + (setq text + (concat (encode-coding-string + (concat (buffer-substring beginp endp) + (if (eq isnewline t) "\n")) + redit-string-coding) + ))) + (setq text _text)) + (setq text-size (string-bytes text)) + (let ((sid (SessionInfo-sid sinfo)) + (eid (SessionInfo-eid sinfo)) (packet)) + + (setq packet (redit-make-packet + redit-insert-command sid eid + (gen-seqno) linenum text-size text)) - (process-send-string redit-client-process packet) + (process-send-string redit-client-process packet) - (redit-debug-message - (format "insert-command: %d %d %d %d %d %d %s" - redit-insert-command sid eid (gen-seqno) - linenum text-size text)) - )) - )) + (redit-debug-message + (format "insert-command: %d %d %d %d %d %d %s" + redit-insert-command sid eid (gen-seqno) + linenum text-size text)) + )) + ))) (error "redit-client is not running."))) ;; linenum で指定した行の削除命令を redit-client-process に送信する (defun redit-client-delete-line (linenum) + "Send \"redit-delete-line-command\" to SessionManager. + +If current buffer is not connected with SessionManager, +nothing is done (see `redit-client-exec-join', `redit-client-exec-put'). + +* REP command header + +cmd : 9 (redit-delete-line-command). +sid : Session ID corresponding to current buffer. +eid : Editor ID corresponding to current buffer. + (sid & eid are acquired from hash table `SessionInfo'.) +seq : Sequence Number. it can be acquired it from function `gen-seqno'. +lno : Delete line number, LINENUM. +siz : 0 + +* REP command text + +No text." (if redit-client-process (save-excursion (let (sinfo) @@ -428,9 +458,7 @@ ;; proc == redit-client-process ;; string == redit-client-process からの output (defun redit-client-process-filter (proc string) - ;; バッファローカルなフックリスト before-change-functions から - ;; redit-client-before-change-function を取り除く. after も同じ - ;; proc からの出力に hook しないように + "" (remove-hook 'before-change-functions 'redit-client-before-change-function) (remove-hook 'after-change-functions @@ -462,28 +490,7 @@ (setq txt (redit-get-text-from-pkt string)) ;; command がどの命令かを判断し、対応した処理をする。case みたい - (cond - ;; FIXME - ;;((if (= command redit-open-ack) - ;;(if (/= redit-client-editor-id eid) - ;;(redit-client-exec-open-ack string)))) - - ;;((if (= cmd redit-read-ack) - ;;(if (= redit-client-editor-id (redit-get-editor-id-from-pkt string)) - ;;(redit-client-exec-read-ack string)))) - - ;;((if (= command redit-insert-ack) - ;;(if (= redit-client-editor-id (redit-get-editor-id-from-pkt string)) - ;;(redit-client-exec-insert-ack string)))) - - ;;((if (= command redit-delete-line-ack) - ;;(if (= redit-client-editor-id (redit-get-editor-id-from-pkt string)) - ;;(redit-client-exec-delete-line-ack string)))) - - ;;((if (= command redit-close-ack) - ;;(if (= redit-client-editor-id (redit-get-editor-id-from-pkt string)) - ;;(redit-client-exec-close-ack string)))) - + (cond ((if (= cmd redit-insert-command) (redit-client-exec-insert-line sid lno txt))) @@ -515,33 +522,44 @@ 'redit-client-after-change-function t) ) -;; currentp の位置の行数を返す (defun real-count-lines (currentp) (+ (count-lines (point-min) currentp) (if (= (save-excursion (goto-char currentp) (current-column)) 0) 1 0))) -;; before-change-functions に hook される -;; バッファが変更される直前に呼ばれる。 -;; begin と end には変更前の変更部分の始まりと終わりの point が入る (defun redit-client-before-change-function (begin end) + "This function was added at the hook `before-change-functions' list. +It call before each text change of all buffer. +Two arguments are passed to each function: the positions of +the beginning BEGIN and end END of the range of old text to be changed. + +In this function, the line of beginning, end and max line +of old buffer are only secured. +" (setq redit-client-before-begin-line (real-count-lines begin)) (setq redit-client-before-end-line (real-count-lines end)) (setq redit-client-previous-max-line (real-count-lines (point-max))) ) -;; after-change-functions に hook される -;; バッファが変更された直後に呼ばれる -;; begin と end には変更後の変更部分の始まりと終わりの point が入る (defun redit-client-after-change-function (begin end length) + "This function was added at the hook `after-change-functions' list. +It call after each text change of all buffer. +Three arguments are passed to each function: the positions of +the beginning BEGIN and end END of the range of changed text, +and the length LENGTH in bytes of the pre-change text replaced by that range. + +If current buffer (edited buffer) is not connected with SessionManager, +nothing is done (see `redit-client-exec-join', `redit-client-exec-put'). + +The edited lines is checked, and insert/delete commands are issued. +The change by the same line is disregarded. +" (let (sinfo) (setq sinfo (redit-sinfo-get-buf2sinfo (buffer-name))) - ;; sinfo が nil であれば、編集されたバッファは - ;; REP と無関係のバッファってことで、以降無視。 (if sinfo (let ((editl) (cur-max-line) (max-line-diff) - (prev-edit-line) (flg nil)) + (prev-edit-line) (lf-flg nil)) (setq redit-client-after-begin-line (real-count-lines begin)) (setq redit-client-after-end-line (real-count-lines end)) @@ -583,12 +601,12 @@ (setq max-line-diff (- cur-max-line redit-client-previous-max-line)) (if (> max-line-diff 0) - (setq flg t)) ;; "\n" の付加 + (setq lf-flg t)) ;; ;; insert (setq editl redit-client-before-begin-line) (while (< editl redit-client-after-end-line) - (redit-client-insert-line editl flg) + (redit-client-insert-line editl lf-flg) (setq editl (1+ editl)) ) (redit-client-insert-line redit-client-after-end-line nil) @@ -608,10 +626,26 @@ (setf (SessionInfo-prevline sinfo) redit-client-after-end-line) )))) -;; 引き数で与えられた string (line_num + text_data) から -;; 指定された行を削除し、そこに text_data を挿入する -(defun redit-client-exec-insert-line (sid editlineno text) - (let ((curlineno) (bufname) (sinfo)) +(defun redit-client-exec-insert-line (sid linenum text) + "Call when received \"redit-insert-command\" from SessionManager. +see. `redit-client-process-filter'. + +TEXT is inserted in line LINENUM of buffer corresponding to SID. + +For example, now buffer + +1: hoge +2: void +3: elisp + +argument LINENUM = 2, TEXT = \"redit-client\" + +result + +1: hoge +2: redit-client +3: elisp" + (let (curlineno bufname sinfo) ;; SessionID から、このテキストを挿入するべき ;; バッファ名を取得する (setq bufname (redit-sinfo-get-sid2buf sid)) @@ -619,50 +653,70 @@ (save-excursion (set-buffer bufname) - ;; 指定行番号へ移動 - ;; diff には、editlineno と 実際に移動した行番号の差が入る - ;; バッファの末尾の行が指定した行に足りない場合に diff > 0 になる - (goto-line editlineno) ;; 指定行番号へ移動 - (setq curlineno (real-count-lines (point))) ;; 現在の行番号 - (if (> editlineno curlineno) - ;; buffer の 最後の行番号が、editlineno に足りない場合、 + (goto-line linenum) + (setq curlineno (real-count-lines (point))) + (if (> linenum curlineno) + ;; buffer の 最後の行番号が、linenum に足りない場合、 ;; その行数だけ改行し、その行へ移動する。 ;; newline なので、下のようにテキストを削除する必要は無い (progn (end-of-line) - (newline (- editlineno curlineno)) - (goto-line editlineno)) + (newline (- linenum curlineno)) + (goto-line linenum)) ;; 行頭から末尾までのテキストを削除 (delete-region (progn (beginning-of-line) (point)) (progn (end-of-line) (point))) ) - ;; 新しい行を挿入 - (insert text)))) + (insert text) +))) + +(defun redit-client-exec-delete-line (sid linenum) + "Call when received \"redit-delete-line-command\" from SessionManager. +see. `redit-client-process-filter'. + +Line LINENUM of buffer corresponding to SID is deleted. + +For example, now buffer -;; 引き数 string (line_num + text_data) で指定された行を削除する -(defun redit-client-exec-delete-line (sid linenum) +1: hoge +2: void +3: elisp + +argument LINENUM = 2 + +result + +1: hoge +2: elisp +" (let ((diff) (bufname (redit-sinfo-get-sid2buf sid))) (save-excursion (set-buffer bufname) (setq diff (goto-line linenum)) (if (= diff 0) (progn - ;; 行頭から末尾までのテキストを削除 (delete-region (progn (beginning-of-line) (point)) (progn (end-of-line) (point))) ;; 指定された行自体を削除 - ;; 指定された行番号は別の行の番号を表すことになる (if (= (point) (point-max)) (delete-backward-char 1) (delete-char 1))) ) ))) -;; 引き数で与えられた string から Editor IDを取得する (defun redit-client-exec-join (filename sid eid) - (let ((bufname) (sinfo)) - ;; SessionInfo の生成 + "Call when received \"redit-join-ack-command\" from SessionManager. +see. `redit-client-process-filter'. + +SID is id of session which client participation. +EID is id of client in session. +FILENAME file name used by session. + +Buffer that name is the same FILENAME is generated, and move to it. +`SessionInfo' is created by using SID and EID. +" + (let (bufname sinfo) (setq sinfo (redit-create-session-info sid eid)) ;; SessionManager から受け取った、このセッションのファイル名を @@ -701,14 +755,6 @@ (redit-debug-message (format "put-ack Session [%d] %s" sid bufname)) (redit-debug-message (format "Your editor id = %d" eid)) )) - -;; SessionIDを名前とした、新しいバッファを開く -;; FIXME -;; SessionIDではなく、Session Nameが望ましい? -;; バッファを開いた後、Sessionで開いてるファイルの中身を表示するべき -;;(defun redit-client-exec-select () -;; (switch-to-buffer (get-buffer-create -;; (number-to-string redit-client-session-id)))) ;; プロセスの状態を見て、対応したメッセージを表示 (defun redit-client-sentinel (proc msg)