Sunday, January 23, 2011

Native vim for Android with mbyte and color

[Edit]
I've uploaded the compiled binaries for this version of vim, which can be found at http://cid-0e29e4b9a313ea5b.office.live.com/self.aspx/Releases/Vim%5E_Android%5E_With%5E_Multibyte.zip.Please copy the files to /data/vim (or any directory you can access that's not on the sdcard), set executable by chmod 7777 ./vim, export PATH, then you're good to go. The xxd file is also an executable that enables binary editing, may come handy if you're editing any binary files on android (discouraged).

I came across credentialitiy's 'Native Vim for Android' the other day, and it immediately solved my problem of wanting to program on Android with a powerful enough editor - Text Edit simply won't do. Here I'd like to first express my gratitude to credentiality for his excellent job on compiling Vim for Android in the first place.

But then nearly immediately, I found several problems: there's no color, either the environment color set using ":color somecolorname" command or syntax-highlighting; then, the jog-ball doesn't work.
If you're wondering if it's the problem of the terminal emulator, be assured that it isn't, as the 'ls' command that comes with busybox does show coloring on my terminal, while a version of bash compiled for Android accepts job-ball directional commands perfectly happily. And that troubled me for some while, till some extensive searches led me to a thread on LinuxQuestions.Org: http://www.linuxquestions.org/questions/aix-43/no-colors-in-vim-on-command-line-442695/. It turned out to be the setting of the 'TERM' environment variable and some initializations required by Vim to display colors in the terminal.
As it happened, it isn't the problem of just Android, it happened on other platforms too. So I added the lines of code to my vimrc, then it just worked!
Later on, I discovered that it is actually documented in vim. Just type ':help syntax' and scroll to the bottom of that help page.

Then problems arose about the mbyte support of that vim. I was quite astonished that it did have the 'encoding' setting, or the 'fileencoding' setting. Apparently, mbyte support wasn't compiled in. So I set out to compile one with that turned on.

It did take me some time to compile my first vim for android, and there was a mistake that bothered me for nearly an hour. Follow credentiality's instruction at 'Native Vim for Android'  to get your first vim for android compiled. Note that when configuring ncurses for compilation, there's a mistake in the instructions. Change:

CC=agcc ./configure --host=arm-eabi --without-cxx-binding


into:

CC=agcc.pl ./configure --host=arm-eabi --without-cxx-binding

 

After compiling vim, I found the appropriate configuration option in src/configure.in
Reconfigure with the following command:

vim_cv_memcpy_handles_overlap=no \

vim_cv_bcopy_handles_overlap=no \

vim_cv_memmove_handles_overlap=no \

vim_cv_stat_ignores_slash=no \

vim_cv_getcwd_broken=no \

vim_cv_tty_group=world \

vim_cv_terminfo=yes \

LDFLAGS="-L/path/to/ncurses-5.7/lib" \

CFLAGS="-I/path/to/ncurses-5.7/lib" \

vim_cv_toupper_broken=no \

CC=agcc.pl \

./configure --host=arm-eabi --with-tlib=ncurses --enable-multibyte


then patch the header file as instructed in credentiality's blog, and patch the following file:
src/mbyte.c:
Add:

#ifndef __amigaos4__

# define __amigaos4__

#endif




below other #include declarations.

As I figured out, that definition tricks vim into believing that there's no mblen() on the platform, which is true, as multibyte support is always faulty in NDK.

Then you can try it for yourself, it should work with Unicode encodings, I've tested UTF-8, UTF-16LE, but ANSI encodings like cp936 are still not supported. Good enough for me, at least I can now edit Chinese text documents with vim.

Photo (Still haven't figured out how to use the screenshot program):
Note that the photo has been post processed to remove lens distortion.

4 comments:

  1. Thanks for the bug report (and these excellent instructions)! I fixed the bug in my ncurses post.

    ReplyDelete
  2. how did you compile vim for android?
    can you share your vim binary to me?
    is this static binary?

    ReplyDelete
  3. Yes, this is a static binary, it works fine on Android 2.1. I've uploaded it, check the top of post.

    ReplyDelete
  4. I have been using your binary and was very happy with it until Android 5.0 came. It would be incredibly helpful if you could post a binary that would work on Android 5.

    ReplyDelete