diff --git a/clite/libs/libcurses.js b/clite/libs/libcurses.js index 8ebb231..2f24b38 100644 --- a/clite/libs/libcurses.js +++ b/clite/libs/libcurses.js @@ -106,6 +106,16 @@ return Object.create({ clite.io.vfprintf(io.pid,io.stdout,fmt,args); }, + addstr:function(str) { + clite.io.write(io.pid,io.stdout,str); + }, + + addch:function(ch,opts) { + if (!this.isinit) + return; + this.addstr(ch); + }, + getch:function(cb) { if (!this.isinit) return false; diff --git a/clite/vi.js b/clite/vi.js index c9cbc5a..43f25fb 100644 --- a/clite/vi.js +++ b/clite/vi.js @@ -6,11 +6,17 @@ clite.commands.load('vi',function(args,env,io) { var curses = io.include('curses'); // definitely not dark magic var term = io.include('term'); + const VIMODE_VIEW = 0; + const VIMODE_CMD = 1; + const VIMODE_EDIT = 2; + var cmd_buff = ''; var file = null; - var mode = 0; // 0 view mode, 1 command mode, 2 edit mode + var mode = VIMODE_VIEW; // 0 view mode, 1 command mode, 2 edit mode var cursor_line = 0; var cursor_col = 0; + var cursor_line_real = 0; + var cursor_col_real = 0; var changed = false; var num = false; var message = ''; @@ -20,10 +26,6 @@ clite.commands.load('vi',function(args,env,io) { var topLine = 0; var bottomLine = 0; - const VIMODE_VIEW = 0; - const VIMODE_CMD = 1; - const VIMODE_EDIT = 2; - function switchMode(m) { if (m == mode) return; @@ -68,7 +70,7 @@ Options: var i = lines.length-1; for (; i>-1; i--) { - r -= lines[i][1]; + r -= lines[i].rows; if (r<0) i++; if (r <= 0) @@ -78,6 +80,60 @@ Options: bottomLine = i; } + function drawCursorLine(line) { + var cl = 0; + var s = -1; + var disp = ''; + var rows = 0; + for (var i=0; i cols) { + var b = ''; + var e = ''; + if (s > -1) { + b = disp.substring(0,s); + e = l.disp.substring(s); + }else{ + b = disp.substring(0,disp.length-1); + e = disp.substring(disp.length-1); + } + disp = b+'\n'+e; + cl = e.length; + rows++; + s = -1; + } + switch (line[i]) { + case '\t': + { + if (cl > (cols-4)) { + disp += '\n'; + cl = 0; + rows++; + s = -1; + break; + } + s = disp.length; + var c = 4-(s%4); + for (var j=0; j lines.length) line = lines.length; @@ -88,20 +144,28 @@ Options: if (end > lines.length) end = lines.length; curses.clear(); - var cursor_screen = [0,cursor_col+1]; + var cursor_line_screen = 0; var k = 0; if (num) { cursor_screen[1] += 4; for (var i=start; i cols) { + var b = ''; + var e = ''; + if (s > -1) { + b = l.disp.substring(0,s); + e = l.disp.substring(s); + }else{ + b = l.disp.substring(0,l.disp.length-1); + e = l.disp.substring(l.disp.length-1); + } + l.disp = b+'\n'+e; + cl = e.length; + l.rows++; + s = -1; + } + switch (line[i]) { + case '\t': + { + if (cl > (cols-4)) { + l.disp += '\n'; + cl = 0; + l.rows++; + s = -1; + break; + } + s = l.disp.length; + var c = 4-(s%4); + for (var j=0; j cols) { - var count = Math.ceil(line.length/cols); - lines.push([line,count]); - return; - } - lines.push([line,1]); + lines.push(prepLine(line)); }); if (lines.length < 1) - lines.push(['',0]); + lines.push(prepLine('')); - cursor_line = lines.length-1; + cursor_line = 0; + topLine = 0; calculateBottomLine(); - topLine = bottomLine; showAt(topLine); } @@ -158,7 +270,7 @@ Options: return; var data = ''; lines.forEach(function(l) { - data += l[0]+'\n'; + data += l.real+'\n'; }); stdio.write(fd,data); stdio.close(fd); @@ -205,6 +317,8 @@ Options: } } + switchMode(VIMODE_VIEW); + return true; } @@ -232,20 +346,18 @@ Options: showAt(topLine); } function cursorRt() { - if (cursor_col < lines[cursor_line][0].length) + if (cursor_col < lines[cursor_line].real.length) cursor_col++; showAt(topLine); } function setLine(str) { - lines[cursor_line][0] = str; - var count = Math.ceil(lines[cursor_line][0].length/cols); - lines[cursor_line][1] = count; + lines[cursor_line] = prepLine(str); calculateBottomLine(); changed = true; } function insertLine(str) { - var l = [str,str.length]; + var l = prepLine(str); lines.splice(cursor_line+1,0,l); cursor_col = 0; cursorDn(); @@ -325,7 +437,7 @@ Options: break; case curses.key.KEY_BACKSPACE: if (cursor_col > 0) { - var l = lines[cursor_line][0]; + var l = lines[cursor_line].real; var b = l.substring(0,cursor_col-1); var e = l.substring(cursor_col); setLine(b+e); @@ -334,7 +446,7 @@ Options: } break; case curses.key.KEY_ENTER: - var l = lines[cursor_line][0]; + var l = lines[cursor_line].real; var b = l.substring(0,cursor_col); var e = l.substring(cursor_col); setLine(b); @@ -343,17 +455,17 @@ Options: showAt(topLine); break; case curses.key.KEY_DL: - if (cursor_col < lines[cursor_line][1]) { - var l = lines[cursor_line][0]; + if (cursor_col < lines[cursor_line].real.length) { + var l = lines[cursor_line].real; var b = l.substring(0,cursor_col); var e = l.substring(cursor_col+1); - setLine(b+key+e); + setLine(b+e); showAt(topLine); } break; default: if (typeof key === 'string' && key.length == 1) { - var l = lines[cursor_line][0]; + var l = lines[cursor_line].real; var b = l.substring(0,cursor_col); var e = l.substring(cursor_col); setLine(b+key+e);