Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Add multibyte support, using builtin emscripten UTF8 functions. #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/bin/bash
set -e
[ -z $EM_DIR] && EM_DIR=~/src/emscripten
[ -z "$EM_DIR" ] && EM_DIR=~/src/emscripten

OPTZ="-Oz"
# for gcc, which doesn't support -z
OPTS="-Os"

do_config() {
echo config
# something wrong with emcc + cproto, use gcc as CPP instead
CPPFLAGS="-Os -DFEAT_GUI_WEB" \
CPPFLAGS="$OPTS -DFEAT_GUI_WEB" \
CFLAGS="$OPTS" \
CPP="gcc -E" \
$EM_DIR/emconfigure ./configure \
--enable-gui=web \
Expand Down Expand Up @@ -46,7 +51,7 @@ $EM_DIR/emconfigure ./configure \
}

do_make() {
$EM_DIR/emmake make -j8
$EM_DIR/emmake make CFLAGS="$OPTZ" -j8
}

do_link() {
Expand All @@ -59,13 +64,14 @@ OPT_ASYNCIFY="-s ASYNCIFY=1 \
-s EXPORTED_FUNCTIONS=\"['_main', '_input_available', '_gui_web_handle_key']\" \
-s ASYNCIFY_FUNCTIONS=\"['emscripten_sleep', 'vimjs_flash', 'vimjs_browse']\" "

OPT_EMTERPRETER="-s EMTERPRETIFY=1 -s EMTERPRETIFY_ASYNC=1"

OPT_EMTERPRETER="-s EMTERPRETIFY=1 -s EMTERPRETIFY_ASYNC=1 \
-s EXPORTED_FUNCTIONS=['_main','_input_available','_gui_web_handle_key']"

# Use vim.js as filename to generate vim.js.mem
$EM_DIR/emcc vim.bc \
$EM_DIR/emcc \
vim.bc \
-o vim.js \
-Oz \
$OPTZ \
$OPT_EMTERPRETER \
--memory-init-file 1 \
--js-library vim_lib.js \
Expand Down
2 changes: 2 additions & 0 deletions web/usr/local/share/vim/vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 36 additions & 11 deletions web/vim_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,27 @@ var LibraryVIM = {
if(e.altKey) modifiers |= 0x08;
if(e.metaKey) modifiers |= 0x10;

var handled = false;
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));
handled = true;
}
vimjs.gui_web_handle_key(keyCode, modifiers, special.charCodeAt(0), special.charCodeAt(1));
} else {
vimjs.gui_web_handle_key(keyCode, modifiers, 0, 0);
}
} else {
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], 0, 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);
}
}
}

if(!handled)
vimjs.gui_web_handle_key(charCode || keyCode, modifiers, 0, 0);

},//VIMJS_FOLD_END

get_color_string: function(color) {//VIMJS_FOLD_START
Expand Down Expand Up @@ -695,14 +704,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;
});
var ctrl_keys_to_intercept_upon_keydown = {};
[
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);
}
Expand Down Expand Up @@ -778,7 +797,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)) {
Expand All @@ -788,8 +815,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;
Expand Down