changeset 19:03814a8d0b2a

*** empty log message ***
author kono
date Mon, 08 Sep 2008 19:58:08 +0900
parents 70868348fbd8
children 5896e8fc8c7f
files src/memline.c src/reditor.c src/reditor.h
diffstat 3 files changed, 113 insertions(+), 331 deletions(-) [+]
line wrap: on
line diff
--- a/src/memline.c	Sun Sep 07 22:55:48 2008 +0900
+++ b/src/memline.c	Mon Sep 08 19:58:08 2008 +0900
@@ -47,6 +47,10 @@
 #endif
 
 #include "vim.h"
+/*
+   Remote Editor
+ */
+#include "reditor.h"
 
 #ifndef UNIX		/* it's in os_unix.h for Unix */
 # include <time.h>
@@ -2173,6 +2177,13 @@
 
     if (curbuf->b_ml.ml_line_lnum != 0)
 	ml_flush_line(curbuf);
+    /*
+     * Remote Editor
+     */
+    if (rep_permit()) {
+        rep_prevline_flush(lnum,1);
+    }
+
     return ml_append_int(curbuf, lnum, line, len, newfile, FALSE);
 }
 
@@ -2199,7 +2210,7 @@
      * Remote Editor
      */
     if (rep_permit()) {
-        rep_prevline_flush();
+        rep_prevline_flush(lnum,1);
     }
 
     return ml_append_int(buf, lnum, line, len, newfile, FALSE);
@@ -2724,6 +2735,12 @@
     if (line == NULL)		/* just checking... */
 	return FAIL;
 
+    /*
+     * Remote Editor
+     */
+    if (rep_permit()) {
+        rep_prevline_flush(lnum,0);
+    }
     /* When starting up, we might still need to create the memfile */
     if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL) == FAIL)
 	return FAIL;
@@ -2767,7 +2784,7 @@
      * Remote Editor
      */
     if (rep_permit()) {
-        rep_prevline_flush();
+        rep_prevline_flush(lnum, -1);
     }
 
     return ml_delete_int(curbuf, lnum, message);
--- a/src/reditor.c	Sun Sep 07 22:55:48 2008 +0900
+++ b/src/reditor.c	Mon Sep 08 19:58:08 2008 +0900
@@ -26,15 +26,12 @@
 static int append_memline_wrp(long lnum, char *text);
 static int delete_memline_wrp(long lnum);
 static void update_screen_now_wrp();
-static long get_bufmaxline_wrp(BUFTYPE* buf);
-static char* getstr_input_wrp(char *msg);
 static char *get_fullpath_wrp(BUFTYPE *buf);
 /* Wrapper END */
 
 
 static rep_T* get_rep();
 
-static int del_ignored_cmd(rep_cmdlist *cmdlist);
 static int rep_connect(char *host);
 
 static void* rep_alloc(int size);
@@ -45,13 +42,11 @@
 static Session* make_session(char *name, BUFTYPE *buf);
 static void free_session(Session *sn);
 static void free_session_list(Session *head);
-static void register_session(Session *sn);
 static Session* set_cursession(Session *sn);
 static Session* find_session_by_buf(BUFTYPE *buf);
 static Session* find_session_by_name(char *name);
 static char* get_fullpath(Session *session);
 
-static char* get_sname_by_snum(int snum);
 static int writen(int fd, char *textbuf, unsigned int len);
 
 static char* make_packet(unsigned int cmd,
@@ -70,8 +65,6 @@
 static int free_cmdlist(rep_cmdlist *cmdlist);
 static void add_cmd_to_list(rep_cmdlist *cmdlist, rep_cmd *cmd);
 
-static void check_line_change(Session *sn, unsigned int lnum);
-
 static unsigned int get_header(char *buf,int offset);
 static int set_header(unsigned int num, char *pkt, int offset);
 
@@ -87,11 +80,10 @@
 
 static int rep_recv_cmds(int fd, rep_cmdlist *smcmdlist,rep_cmdlist *txtcmdlist);
 static int rep_send_cmds(int fd, rep_cmdlist *cmdlist);
+static int rep_send_cmd( int fd, rep_cmd *cur);
 
 static int append_newline_sep_text(Session *sn, char *text);
 
-static char_u *copy_and_get_memline(BUFTYPE *buf, long lnum);
-
 /* g_rep has an all information of Remote Editor */
 rep_T g_rep;
 
@@ -110,7 +102,7 @@
 }
 
 /* 通常のメッセージを出力 */
-static void
+void
 puts_msg_wrp(msg)
     char * msg;
 {
@@ -256,27 +248,13 @@
 }
 
 /* 編集中のバッファの行数を返す */
-static long
+long
 get_bufmaxline_wrp(buf)
     BUFTYPE *buf;
 {
     return buf->b_ml.ml_line_count;
 }
 
-/* XXX もう使わないので消す予定 */
-/* ユーザに文字列を入力させる */
-static char *
-getstr_input_wrp(msg)
-    char *msg; // 入力時のメッセージ
-{
-    char *cmdline;
-
-    /* 受け取る文字列は使用後 vim_free() する */
-    // cmdline = (char*)getcmdline_prompt('@', (char_u*)msg, 0);
-    cmdline = NULL;
-
-    return cmdline;
-}
 
 /* get full path of buffer */
 static char *
@@ -370,37 +348,6 @@
 }
 
 
-/*
- * cmdlistを辿り、statメンバが REP_IGNORE であるREPコマンドを削除する。
- */
-static int
-del_ignored_cmd(cmdlist)
-    rep_cmdlist *cmdlist;
-{
-    rep_cmd *curcmd;
-    rep_cmd *next;
-
-    for (;(cmdlist->head) && (cmdlist->head->stat == REP_IGNORE); cmdlist->head=next) {
-        next = cmdlist->head->next;
-        free_cmd(cmdlist->head);
-    }
-    
-    if (cmdlist->head == NULL) return(TRUE);
-    
-    for (curcmd=cmdlist->head; curcmd->next;) {
-        next = curcmd->next->next;
-        if (curcmd->next->stat == REP_IGNORE) {
-            free_cmd(curcmd->next);
-            curcmd->next = next;
-            cmdlist->num--;
-        } else {
-            curcmd = curcmd->next;
-        }
-    }
-    return(TRUE);
-}
-
-
 static char default_host[] = "localhost";
 
 static int
@@ -550,20 +497,6 @@
     return;
 }
 
-static void
-register_session(sn)
-    Session *sn;
-{
-    rep_T *rep;
-
-    rep = get_rep();
-
-    sn->next = rep->shead;
-    rep->shead = sn;
-
-    return;
-}
-
 static Session*
 set_cursession(sn)
     Session *sn;
@@ -625,28 +558,6 @@
     return find_session_by_buf(buf);
 }
 
-int
-rep_input_reg_session() // input own session(s)
-{
-    char *sname;
-    
-    while (1) {
-        // retrun value (sname) is allocated.
-        sname = getstr_input_wrp("Session to offer = ");
-        if (sname == NULL) return FALSE;
-        if (*sname=='\0') {  // input pert is finished
-            free_wrp(sname);
-            break;
-        }
-        register_session(make_session(sname, NULL));
-        
-        free_wrp(sname);
-    }
-
-    update_screen_now_wrp(); /* ウィンドウを再描画 */
-    return TRUE;
-}
-
 
 static char*
 get_fullpath(session)
@@ -664,31 +575,6 @@
 
 /* End Session */
 
-
-
-
-char *
-rep_input_param(msg, err_msg)
-    char *msg;
-    char *err_msg;
-{
-    char *input_string;
-
-    if ((input_string = getstr_input_wrp(msg)) == NULL) {
-        return NULL;
-    }
-
-    if (*input_string == '\0') {
-        if (err_msg) {
-            puts_msg_wrp(err_msg);
-        }
-        free_wrp(input_string);
-        return NULL;
-    }
-
-    return input_string;
-}
-
 int
 rep_join()
 {
@@ -698,11 +584,6 @@
     rep_cmdlist cmdlist = {NULL,0};
 
     rep = get_rep();
-/*    
-    if (server == NULL) {
-        return FALSE;
-    }
-*/
     if ((sock = rep_connect(NULL)) < 0) {
         return FALSE;
     }
@@ -722,44 +603,14 @@
 
 }
 
-int 
-rep_select_command(session_no)
-    char *session_no;
-{
-    rep_T *rep = get_rep();
-    rep_cmdlist cmdlist = {NULL, 0};
-    int sid;
-
-
-    if (rep->smfd < 0 || session_no == NULL) {
-        return FALSE;
-    }
-
-    if (rep->waiting_session_name) {
-        rep_free(rep->waiting_session_name);
-    }
-   sid = atol(session_no); 
-    if ((rep->waiting_session_name = get_sname_by_snum(sid)) == NULL) {
-        e_msg_wrp("Session Selection is false.");
-        return FALSE;
-    }
-    
-    add_cmd_to_list(&cmdlist, make_cmd(REP_SELECT_CMD, sid, rep->eid, rep->seqno++, 0, session_no));
-    rep_send_cmds(rep->smfd, &cmdlist);
-
-    free_cmdlist(&cmdlist);
-
-    return TRUE;
-}
-
 int
-rep_put(sname)
-    char *sname;
+rep_put()
 {
     int sock;
     rep_T *rep = get_rep();
     rep_cmdlist cmdlist = {NULL, 0};
     int len;
+    char *sname;
 
     if ((sock = rep_connect(NULL)) < 0) {
         return FALSE;
@@ -771,11 +622,9 @@
     rep->smfd = sock;
     rep->permit = TRUE;
    
-    if (sname == NULL) {
-        /* get current buffer name */
-        if ((sname = get_fullpath(rep->cursession)) == NULL) {
-            sname = NO_NAME;  /* the buffer has not name */
-        }
+    /* get current buffer name */
+    if ((sname = get_fullpath(rep->cursession)) == NULL) {
+	sname = NO_NAME;  /* the buffer has not name */
     }
 
     if (rep->waiting_session_name) {
@@ -894,7 +743,7 @@
     char *packet;
     unsigned int len = 0;
 
-    if (text) len += strlen(text) + 1;// + 1; /* for include '\0' */
+    if (text) len = strlen(text);// + 1; /* for include '\0' */
 
     if ((packet = (char *)rep_alloc(REP_HEADER_SIZE+len)) == NULL) {
         return(NULL);
@@ -1012,18 +861,26 @@
 }
 
 
+/*
+    Before any line changes, keep the lines as DELETE_LINE_CMD.
+
+    xtr==0    ml_replace
+    xtr==1    ml_append
+    xtr==-1   ml_delete
+ */
 void
-rep_prevline_flush()
+rep_prevline_flush(int lnum, int xtr)
 {
     BUFTYPE *cbuf;
     Session *cursn;
     rep_cmd *cmd;
     rep_T *rep = get_rep();
-    char *text;
 
     cursn = rep->cursession;
 
-    if ((cursn == NULL) || (cursn->prevline == -1)) return;
+    // 既にコピーしていたら、何もしない
+    if ((cursn == NULL) || (cursn->prevline == -1)) 
+	return;
 
     // バッファが変更された場合には rep->cursession も合わす
     if ((cbuf = get_curbuf_wrp()) != cursn->buf) {
@@ -1033,72 +890,32 @@
         rep->cursession = cursn;
     }
 
-    //text = get_memline_wrp(cursn->buf, cursn->prevline);
-    text = copy_and_get_memline(cursn->buf, cursn->prevline);
-
-    cmd = make_cmd(REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, cursn->prev_text);
-    cmd->next = make_cmd(REP_INSERT_CMD, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, text);
-
-    // 現在のバッファを取っておく
-    cursn->prev_text = text;
-
-    add_cmd_to_list(&(cursn->new_cmdlist), cmd);
-
-    if (cursn->sent_cmdlist.num == 0) { // 自トークンを送信してない場合
-        rep_send_cur_cmdlist();
+    if(cursn->prevline!=lnum&&xtr!=0) {
+	if (cursn->del_cmd) {
+	    // Cancel delete cmd 
+	    rep_free(cursn->del_cmd);
+	    cursn->del_cmd = 0;
+        }
     }
-    
-    cursn->prevline = -1;
+    cursn->prevline=lnum;
+    if (xtr<0) {
+	// ml_delete case
+	cmd = make_cmd(REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum));
+	rep_send_cmd(rep->smfd,cmd);
+	rep_free(cmd);
+    } else if (xtr==0) {
+	// ml_replace case
+	if (cursn->del_cmd) 
+	    return;       // already line saved do nothing
+	cursn->del_cmd = make_cmd(REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum));
+	
+    } else {
+	// ml_append case
+	// do nothing
+    }
 }
 
-static char_u *
-copy_and_get_memline(buf, lnum)
-    BUFTYPE *buf;
-    linenr_T    lnum;
-{
-    char_u      *src;
-    char_u      *dst;
-    unsigned    len;
-
-    src = get_memline_wrp(buf,lnum);
-    len = (unsigned)STRLEN(src);
-    if ((dst = lalloc((long_u)((len) + 1), FALSE)) != NULL)
-        mch_memmove(dst, src, (size_t)(len + 1));
-    return (dst);
-}
-
-static void
-check_line_change(sn, lnum)
-    Session *sn;
-    unsigned int lnum;
-{
-    rep_cmd *cmd;
-    rep_T *rep = get_rep();
-
-    
-    if (sn->prevline == lnum) {
-        return;
-    }
-
-    if (sn->prevline == -1) {
-        sn->prevline = lnum;
-        return;
-    }
-    //cmd = make_cmd(REP_REPLACE_CMD, sn->sid, rep->eid, rep->seqno++, sn->prevline, get_memline_wrp(sn->buf, sn->prevline));
-
-    // DELETE
-    cmd = make_cmd(REP_DELETE_LINE_CMD, sn->sid, rep->eid, rep->seqno++, sn->prevline, sn->prev_text);
-
-    // INSERT
-    cmd->next = make_cmd(REP_INSERT_CMD, sn->sid, rep->eid, rep->seqno++, sn->prevline, get_memline_wrp(sn->buf, sn->prevline));
-
-    add_cmd_to_list(&(sn->new_cmdlist), cmd);
-
-    sn->prevline = lnum;
-    return;
-}
-
-int
+void
 rep_register(lnum, lnume, xtra)
     unsigned int lnum;
     unsigned int lnume;
@@ -1110,51 +927,47 @@
     rep_cmd *cmd;
     rep_T *rep = get_rep();
     
-    if ((cursn = rep->cursession) == NULL) return FALSE;
+    if ((cursn = rep->cursession) == NULL) return ;
 
     // バッファが変更された場合には rep->cursession も合わす
     if ((cbuf = get_curbuf_wrp()) != cursn->buf) {
         cursn = find_session_by_buf(cbuf);
         if (cursn == NULL)
-            return FALSE;
+            return ;
         rep->cursession = cursn;
     }
-        
-    if (xtra == 0) {
-        for (i = 0; lnume-lnum > i; i++) {
-            check_line_change(cursn,lnum+i);
-        }
-    } else if (xtra > 0) {
-        if (lnum != lnume) { /* 行の途中から改行 */
-            // cmd = make_cmd( REP_REPLACE_CMD, cursn->sid, rep->eid, rep->seqno++, lnum, get_memline_wrp(cursn->buf, lnum));
-            // DELETE
-            cmd = make_cmd( REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, lnum, cursn->prev_text);
-
-            // INSERT
-            cmd->next = make_cmd( REP_INSERT_CMD, cursn->sid, rep->eid, rep->seqno++, lnum, get_memline_wrp(cursn->buf, lnum));
-            add_cmd_to_list(&(cursn->new_cmdlist), cmd);
+    if (xtra<0) {
+	// delete case, the command was sent, do nothing
+	assert(cursn->del_cmd==0);
+    } else if (xtra>0) {
+	// append case
+    
+	for(i=lnum;i<lnume+xtra;i++) {
+	    // make INSERT_CMD for insert or changed lines if any
+	    cmd = make_cmd(REP_INSERT_CMD, cursn->sid, rep->eid, rep->seqno++, i, get_memline_wrp(cursn->buf, i));
+	    add_cmd_to_list(&(cursn->new_cmdlist), cmd);
+	}
+	rep_send_cur_cmdlist();
+    } else if (xtra==0) {
+	// replace case
 
-            lnum++;
-        }
-        for (i = 0; xtra > 0; i++, xtra--) {
-            cmd = make_cmd( REP_INSERT_CMD, cursn->sid, rep->eid, rep->seqno++, lnum+i,
-                            get_memline_wrp(cursn->buf, lnum+i));
-            add_cmd_to_list(&(cursn->new_cmdlist), cmd);
-        } 
-    } else { /* xtra < 0 */
-        xtra = -xtra;
-        for (; xtra > 0; xtra--) {
-            //cmd = make_cmd(REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, lnum, NULL);
-            cmd = make_cmd(REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, lnum, cursn->prev_text);
-            add_cmd_to_list(&(cursn->new_cmdlist), cmd);
-        }
+
+	// send saved DELETE command
+	assert(cursn->del_cmd!=0);
+	cmd = cursn->del_cmd;
+	rep_send_cmd(rep->smfd,cmd);
+	rep_free(cmd);
+	cursn->del_cmd = 0;
+
+	// send saved new line as INSERT command
+	cmd = make_cmd(REP_INSERT_CMD, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum));
+	rep_send_cmd(rep->smfd,cmd);
+	rep_free(cmd);
+
+	// save current line for next replace
+	cursn->del_cmd = make_cmd(REP_DELETE_LINE_CMD, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum));
     }
 
-    if (cursn->sent_cmdlist.num == 0) {
-        rep_send_cur_cmdlist();
-    }
-    
-    return(lnume - lnum);
 }
 
 
@@ -1391,6 +1204,20 @@
 }
 
 static int
+rep_send_cmd(fd,cur)
+    int fd;
+    rep_cmd *cur;
+{
+    int len = cur->len+REP_HEADER_SIZE ;
+    while (len>0) {
+	int i =  writen(fd, cur->pkt, len);
+	if (i<0) return FALSE;
+	i -= len;
+    }
+    return TRUE;
+}
+
+static int
 rep_send_cmds(fd,cmdlist)
     int fd;
     rep_cmdlist *cmdlist;
@@ -1406,7 +1233,7 @@
     }
     
     for (cur = cmdlist->head; cur; cur = cur->next) {
-        if (writen(fd, cur->pkt, cur->len+REP_HEADER_SIZE) < 0) {
+        if (rep_send_cmd(fd,cur)==FALSE) {
             return(FALSE);
         }    
     }
@@ -1435,21 +1262,12 @@
     rep_p = get_rep();
 
     if ((rep_p->smfd > 0) && (FD_ISSET(rep_p->smfd, rfds_p))) {
-		/* we don't need this?
-                // 受け取ったトークンとユーザからのREPコマンドを比較・マージする
-                // 既に送信した REPコマンド列 と比較
-                translate(&(sn->sent_cmdlist), &(cmdlist));
-                del_ignored_cmd(&(cmdlist));
-                set_header_to_pkt(&(cmdlist));
-		*/
-
         if (rep_recv_cmds(rep_p->smfd, &(smcmdlist), &(txtcmdlist)) == FALSE) {
             close(rep_p->smfd);
             rep_p->smfd = -1;
             return FALSE;
         }
         /* Session ごとに行う↓*/
-        del_ignored_cmd(&txtcmdlist);
         rep_exe_pktlist(&txtcmdlist);
 
 	if (rep_send_cmds(rep_p->smfd, &(txtcmdlist)) == FALSE) {
@@ -1583,43 +1401,3 @@
     return TRUE;
 }
 
-/* return value (file name) is allocated */
-static char *
-get_sname_by_snum(snum)
-    int snum;
-{
-    char *tmp, *text, *sname;
-    int i, len;
-    int maxlnum;
-    rep_T *rep = get_rep();
-    
-    maxlnum = get_bufmaxline_wrp(rep->slineup->buf);
-    
-    for (i = 1; i <= maxlnum; i++) {
-        // text is "filename:hostname:sessionnumber"
-        text = get_memline_wrp(rep->slineup->buf, i);
-        
-        
-        // get ':' separated last parameter (session number)
-        if ((tmp = strrchr(text, ':')) == NULL) {
-            return NULL;
-        }
-        tmp++;
-
-        if (snum == atol(tmp)) {
-            // get ':' separated first parameter (filename)
-            tmp = strchr(text, ':');
-            len = tmp - text;
-            if ((sname = (char *)rep_alloc(len+1)) == NULL){
-                e_msg_wrp("no memory!");
-                return NULL;
-            }
-            memcpy(sname, text, len);
-            sname[len] = '\0';
-            return sname;
-        }
-    }
-
-    return NULL;
-    
-}
--- a/src/reditor.h	Sun Sep 07 22:55:48 2008 +0900
+++ b/src/reditor.h	Mon Sep 08 19:58:08 2008 +0900
@@ -1,5 +1,4 @@
 #define REP_PORT 8766
-//#define OTHER_REP_PORT 8765
 #define BUFFSIZE   200
 #define SOCK_MAX   5
 #define MAX_FDS    10
@@ -114,18 +113,9 @@
     rep_cmdlist fromsmcmlist; /* from sm */
 
     int permit; /* TRUE or FALSE */
-//    rep_cmdlist others;  /* others REP command list*/
-    
-//    int sbuf_changed;
 
-//    unsigned short uid;       /* user id */
-//    int usercount; /* joined user number */
-
-//    int sfd;    /* send fd       */
-//    int rfd;    /* receive fd    */
-
-    int prevline;    /* Previous Edited line */
-    char_u *prev_text; /* Previous Edited line text */
+    int prevline;        /* Previous Edited line */
+    rep_cmd *del_cmd;    /* saved line for replace is kept here */
 } Session;
 
 typedef struct rep {
@@ -156,23 +146,20 @@
 extern void rep_start_create_cmds();
 extern void rep_stop_create_cmds();
 
-extern char* rep_input_param(char*, char*);
-
 //extern int rep_connect(char*);
 //extern int rep_add_cmd(rep_cmdlist*,unsigned short,unsigned short,unsigned int,char*);
 //extern void rep_check_line_change(Session*,unsigned int);
-extern int rep_register(unsigned int, unsigned int, int);
+extern void rep_register(unsigned int, unsigned int, int);
 //extern int rep_clear_cmdlist(rep_cmdlist*);
 //extern int rep_recv_cmds(int fd,rep_cmdlist*);
 //extern int rep_send_cmds(int fd,rep_cmdlist*);
 extern int rep_fd_check(int, fd_set*, fd_set*);
 extern int rep_fd_set(fd_set*, fd_set*, int);
 extern int rep_select(int);
-extern void rep_prevline_flush();
+extern void rep_prevline_flush(int,int);
 extern void rep_end();
 
 extern int rep_join();
-extern int rep_select_command(char *);
-extern int rep_put(char *);
+extern int rep_put();
 
 extern void rep_send_cur_cmdlist();