]> Sergey Matveev's repositories - nnn.git/commitdiff
Fix cursor position issue with astral symbols
authorArun Prakash Jana <engineerarun@gmail.com>
Sat, 26 Aug 2017 08:04:43 +0000 (13:34 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 26 Aug 2017 09:16:49 +0000 (14:46 +0530)
In case of astral symbols like Devanagari matras multiple wide-char codepoints
occupy a single position/column. Positioning the cursor wrt. the actual number
of wide-characters in a wide-char string gets "visually incorrect".  The trick
is to calculate the correct number of columns needed to represent a fixed-size
wide-character string.

Relevant man pages:

1. wcswidth(3)
2. wctomb(3)
3. mblen(3)

Interesting links:

1. https://www.gnu.org/software/libc/manual/html_node/Non_002dreentrant-Character-Conversion.html
2. https://www.gnu.org/software/libc/manual/html_node/Shift-State.html
3. https://10hash.com/c-programming/uchar/
4. https://mathiasbynens.be/notes/javascript-unicode#accounting-for-astral-symbols

Example file names for test:

1. Malgudi Days - मालगुडी डेज - E05. Swami and Friends - स्वामी और उसके दोस्त (Part 5)
2. Eso eso aamar ghare eso ♫ এসো এসো আমার ঘরে এসো ♫ Swagatalakshmi Dasgupta
3. Führer

nnn.c

diff --git a/nnn.c b/nnn.c
index b554f2bfe584a3c7931bdb4a0cdfa1fa8a18875c..5d71b2151dd0ae2c07f45727da7520d828f3e806 100644 (file)
--- a/nnn.c
+++ b/nnn.c
@@ -44,6 +44,9 @@
 #define _XOPEN_SOURCE_EXTENDED
 #endif
 #endif
+#ifndef __USE_XOPEN /* Fix failure due to wcswidth(), ncursesw/curses.h includes whcar.h on Ubuntu 14.04 */
+#define __USE_XOPEN
+#endif
 #include <curses.h>
 #include <dirent.h>
 #include <errno.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
-#include <wchar.h>
 #include <readline/history.h>
 #include <readline/readline.h>
 #ifndef __USE_XOPEN_EXTENDED
 #define __USE_XOPEN_EXTENDED 1
 #endif
 #include <ftw.h>
+#include <wchar.h>
 
 #include "config.h"
 
@@ -1026,7 +1029,7 @@ xreadline(char *fname)
        while (1) {
                buf[len] = ' ';
                mvaddnwstr(y, x, buf, len + 1);
-               move(y, x + pos);
+               move(y, x + wcswidth(buf, pos));
 
                if ((r = get_wch(ch)) != ERR) {
                        if (r == OK) {