From 8840a844f178d16ff451726c7622ebf3d5110e71 Mon Sep 17 00:00:00 2001 From: Eivind Magnus Hvidevold Date: Fri, 30 Jan 2015 18:42:34 +0100 Subject: [PATCH 1/3] Add multibyte support. Increased vim.js compiled size from 25MB to 44MB. It is disabled by default. Uses builtin emscripten UTF8 functions. --- build.sh | 2 +- web/usr/local/share/vim/vimrc | 2 ++ web/vim_lib.js | 27 ++++++++++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/build.sh b/build.sh index d16a029..3227ead 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ #!/bin/bash set -e -[ -z $EM_DIR] && EM_DIR=~/src/emscripten +[ -z "$EM_DIR" ] && EM_DIR=~/src/emscripten do_config() { echo config diff --git a/web/usr/local/share/vim/vimrc b/web/usr/local/share/vim/vimrc index 39cf7e4..b99c257 100644 --- a/web/usr/local/share/vim/vimrc +++ b/web/usr/local/share/vim/vimrc @@ -55,6 +55,8 @@ set wildmenu set tabstop=4 set expandtab set nocompatible +set encoding=utf8 +set termencoding=utf8 set fileencodings=utf8 set backspace=indent,eol,start set wildmode=longest,list,full diff --git a/web/vim_lib.js b/web/vim_lib.js index 0792b28..9dafd79 100644 --- a/web/vim_lib.js +++ b/web/vim_lib.js @@ -91,8 +91,19 @@ var LibraryVIM = { } } - if(!handled) - vimjs.gui_web_handle_key(charCode || keyCode, modifiers, 0, 0); + if(!handled) { + var MAX_UTF8_BYTES = 6; + var chars = new Uint8Array(MAX_UTF8_BYTES + 1); // null-terminated + var charLen = stringToUTF8Array(String.fromCharCode(charCode), chars, 0, MAX_UTF8_BYTES); + if (charLen == 1) { + vimjs.gui_web_handle_key(chars[0], modifiers, 0, 0); + } else { + // no modifers for UTF-8, should be handled in chars already + for (var i = 0; i < charLen; i++) { + vimjs.gui_web_handle_key(chars[i], 0, 0, 0); + } + } + } },//VIMJS_FOLD_END @@ -778,7 +789,15 @@ var LibraryVIM = { }, vimjs_draw_string__deps: ['vimjs_clear_block'], - vimjs_draw_string: function(row, col, s, len, flags) { + vimjs_draw_string: function(row, col, s_ptr, len, flags) { + var byteArray = []; + for (var i = 0; i < len; i++) { + c = getValue(s_ptr + i, 'i8', true); + byteArray.push(c); + } + byteArray.push(0); + var s = UTF8ArrayToString(byteArray, 0); + len = s.length; // TODO: use macros for flag constants if(!(flags & 0x01)) { @@ -788,8 +807,6 @@ var LibraryVIM = { var font = vimjs.font; if(flags & 0x02) font = 'bold ' + font; - s = Pointer_stringify(s, len); - var ctx = vimjs.canvas_ctx; ctx.font = font; From edffc1294c230c9b47e0e8b0f57c236308db4d6e Mon Sep 17 00:00:00 2001 From: Eivind Magnus Hvidevold Date: Fri, 30 Jan 2015 22:48:24 +0100 Subject: [PATCH 2/3] Fix $ to end of line, HOME, END, CTRL+F (forward) and CTRL+B (backward) on Chrome. Also tested on Firefox and IE. --- web/vim_lib.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/web/vim_lib.js b/web/vim_lib.js index 9dafd79..ec9877c 100644 --- a/web/vim_lib.js +++ b/web/vim_lib.js @@ -86,9 +86,12 @@ var LibraryVIM = { if(charCode == 0) { var special = vimjs.special_keys[keyCode]; if(special !== undefined) { - vimjs.gui_web_handle_key(charCode || keyCode, modifiers, special.charCodeAt(0), special.charCodeAt(1)); + vimjs.gui_web_handle_key(keyCode, modifiers, special.charCodeAt(0), special.charCodeAt(1)); handled = true; - } + } else { + vimjs.gui_web_handle_key(keyCode, modifiers, 0, 0); + handled = true; + } } if(!handled) { @@ -96,7 +99,7 @@ var LibraryVIM = { var chars = new Uint8Array(MAX_UTF8_BYTES + 1); // null-terminated var charLen = stringToUTF8Array(String.fromCharCode(charCode), chars, 0, MAX_UTF8_BYTES); if (charLen == 1) { - vimjs.gui_web_handle_key(chars[0], modifiers, 0, 0); + vimjs.gui_web_handle_key(chars[0], 0, 0, 0); } else { // no modifers for UTF-8, should be handled in chars already for (var i = 0; i < charLen; i++) { @@ -696,6 +699,7 @@ var LibraryVIM = { * C means "needed for Chrome" */ var keys_to_intercept_upon_keydown = {}; + var ctrl_keys_to_intercept_upon_keydown = {}; [ KeyEvent.DOM_VK_ESCAPE, // CF KeyEvent.DOM_VK_TAB, // C KeyEvent.DOM_VK_BACK_SPACE, // C @@ -706,14 +710,24 @@ var LibraryVIM = { KeyEvent.DOM_VK_DELETE, // C KeyEvent.DOM_VK_PAGE_UP, // C KeyEvent.DOM_VK_PAGE_DOWN, // C + KeyEvent.DOM_VK_HOME, // C + KeyEvent.DOM_VK_END, // C ].forEach(function(k) { keys_to_intercept_upon_keydown[k] = 1; }); + [ + KeyEvent.DOM_VK_F, + KeyEvent.DOM_VK_B, + ].forEach(function(k) { + ctrl_keys_to_intercept_upon_keydown[k] = 1; + }); + /* capture some special keys that won't trigger 'keypress' */ document.addEventListener('keydown', function(e) { if (ignoreKeys()) return true; - if(e.keyCode in keys_to_intercept_upon_keydown) { + if((e.keyCode in keys_to_intercept_upon_keydown) || + (e.ctrlKey && (e.keyCode in ctrl_keys_to_intercept_upon_keydown))) { e.preventDefault(); vimjs.handle_key(0, e.keyCode, e); } From f7800f40ff850e01a65f922ef05725bef27117b4 Mon Sep 17 00:00:00 2001 From: Eivind Magnus Hvidevold Date: Sat, 31 Jan 2015 00:47:43 +0100 Subject: [PATCH 3/3] Consistently use -Oz -O3 optimization across all stages of compilation: configure, make and link. This reduces vim.js from 44MB to 37MB (with multibyte). --- build.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/build.sh b/build.sh index 3227ead..2bc251a 100755 --- a/build.sh +++ b/build.sh @@ -2,11 +2,14 @@ set -e [ -z "$EM_DIR" ] && EM_DIR=~/src/emscripten +OPTZ="-Oz -O3" + do_config() { echo config # something wrong with emcc + cproto, use gcc as CPP instead -CPPFLAGS="-Os -DFEAT_GUI_WEB" \ -CPP="gcc -E" \ +CPPFLAGS="$OPTZ -DFEAT_GUI_WEB" \ +CFLAGS="$OPTZ" \ +CPP="emcc -E" \ $EM_DIR/emconfigure ./configure \ --enable-gui=web \ --with-features=small \ @@ -46,7 +49,7 @@ $EM_DIR/emconfigure ./configure \ } do_make() { -$EM_DIR/emmake make -j8 +$EM_DIR/emmake make CFLAGS="$OPTZ" -j8 } do_link() { @@ -58,7 +61,7 @@ cat vim_lib.js | sed -e "1 s/\(foldmethod\|foldmarker\)[^ ]\+//g" > usr/local/sh # Use vim.js as filename to generate vim.js.mem $EM_DIR/emcc vim.bc \ -o vim.js \ - -Oz \ + $OPTZ \ --memory-init-file 1 \ --js-library vim_lib.js \ -s ASYNCIFY=1 \