From 2598f7b63e147f15f07a2b66d89fd97863196993 Mon Sep 17 00:00:00 2001 From: Stanislav Brabec Date: Mon, 23 Feb 2009 15:14:35 +0000 Subject: mc: Updated to 4.6.2 + openSUSE and Debian UTF-8 patches. --- packages/mc/mc-4.6.2/00-70-utf8-common.patch | 46 + .../mc-4.6.2/00-73-utf8-bottom-buttons-width.patch | 21 + packages/mc/mc-4.6.2/00-75-utf8-cmdline-help.patch | 63 + .../mc/mc-4.6.2/00-76-utf8-hotlist-highlight.patch | 23 + .../00-77-utf8-filename-search-highlight.patch | 17 + packages/mc/mc-4.6.2/mc-utf8-look-and-feel.patch | 206 + packages/mc/mc-4.6.2/mc-utf8-nlink.patch | 11 + packages/mc/mc-4.6.2/mc-utf8.patch | 5357 ++++++++++++++++++++ packages/mc/mc-4.6.2/mhl-stdbool.patch | 16 + packages/mc/mc_4.6.2.bb | 60 + 10 files changed, 5820 insertions(+) create mode 100644 packages/mc/mc-4.6.2/00-70-utf8-common.patch create mode 100644 packages/mc/mc-4.6.2/00-73-utf8-bottom-buttons-width.patch create mode 100644 packages/mc/mc-4.6.2/00-75-utf8-cmdline-help.patch create mode 100644 packages/mc/mc-4.6.2/00-76-utf8-hotlist-highlight.patch create mode 100644 packages/mc/mc-4.6.2/00-77-utf8-filename-search-highlight.patch create mode 100644 packages/mc/mc-4.6.2/mc-utf8-look-and-feel.patch create mode 100644 packages/mc/mc-4.6.2/mc-utf8-nlink.patch create mode 100644 packages/mc/mc-4.6.2/mc-utf8.patch create mode 100644 packages/mc/mc-4.6.2/mhl-stdbool.patch create mode 100644 packages/mc/mc_4.6.2.bb (limited to 'packages/mc') diff --git a/packages/mc/mc-4.6.2/00-70-utf8-common.patch b/packages/mc/mc-4.6.2/00-70-utf8-common.patch new file mode 100644 index 0000000000..244a01580a --- /dev/null +++ b/packages/mc/mc-4.6.2/00-70-utf8-common.patch @@ -0,0 +1,46 @@ +Some common stuff used by other UTF-8 patches. + +================================================================================ +--- mc-4.6.2/src/util.c ++++ mc-4.6.2/src/util.c +@@ -145,6 +145,30 @@ + return strlen (str); + } + ++int ++columns_to_bytes (const char *str, int col) ++{ ++ int bytes = 0; ++ int columns = 0; ++ int i; ++#ifdef UTF8 ++ if (SLsmg_Is_Unicode) { ++ static mbstate_t s; ++ while (columns < col) { ++ memset (&s, 0, sizeof (s)); ++ i = mbrlen (str + bytes, -1, &s); ++ if (i <= 0) { ++ return col + bytes - columns; ++ } ++ bytes += i; ++ columns ++; ++ } ++ return col + bytes - columns; ++ } else ++#endif ++ return col; ++} ++ + #ifdef UTF8 + + void +--- mc-4.6.2/src/util.h ++++ mc-4.6.2/src/util.h +@@ -104,6 +104,7 @@ + + void fix_utf8(char *str); + size_t mbstrlen (const char *); ++int columns_to_bytes (const char *, int); + wchar_t *mbstr_to_wchar (const char *); + char *wchar_to_mbstr (const wchar_t *); + char *utf8_to_local(char *str); diff --git a/packages/mc/mc-4.6.2/00-73-utf8-bottom-buttons-width.patch b/packages/mc/mc-4.6.2/00-73-utf8-bottom-buttons-width.patch new file mode 100644 index 0000000000..a7bc41c59b --- /dev/null +++ b/packages/mc/mc-4.6.2/00-73-utf8-bottom-buttons-width.patch @@ -0,0 +1,21 @@ +Use six character width cyan rectangles for bottom row's buttons. + +================================================================================ +--- mc-4.6.2/src/widget.c ++++ mc-4.6.2/src/widget.c +@@ -2562,11 +2562,14 @@ + attrset (DEFAULT_COLOR); + tty_printf ("%-*s", bb->widget.cols, ""); + for (i = 0; i < COLS / 8 && i < 10; i++) { ++ int j; + widget_move (&bb->widget, 0, i * 8); + attrset (DEFAULT_COLOR); + tty_printf ("%d", i + 1); + attrset (SELECTED_COLOR); +- tty_printf ("%-*s", ((i + 1) * 8 == COLS ? 5 : 6), ++ j = columns_to_bytes(bb->labels [i].text ? bb->labels [i].text : "", ++ ((i + 1) * 8 == COLS ? 5 : 6)); ++ tty_printf ("%-*s", j, + bb->labels[i].text ? bb->labels[i].text : ""); + attrset (DEFAULT_COLOR); + } diff --git a/packages/mc/mc-4.6.2/00-75-utf8-cmdline-help.patch b/packages/mc/mc-4.6.2/00-75-utf8-cmdline-help.patch new file mode 100644 index 0000000000..5f1227d30a --- /dev/null +++ b/packages/mc/mc-4.6.2/00-75-utf8-cmdline-help.patch @@ -0,0 +1,63 @@ +Fix formatting the output of "mc --help". + +================================================================================ +--- mc-4.6.2/src/main.c ++++ mc-4.6.2/src/main.c +@@ -1865,7 +1865,7 @@ + + /* print help for options */ + leftColWidth = poptPrintHelp (ctx, stream, 0); +- fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"), ++ fprintf (stream, " %-*s %s\n", leftColWidth + strlen(_("+number")) - mbstrlen(_("+number")), _("+number"), + _("Set initial line number for the internal editor")); + fputs (_ + ("\n" +--- mc-4.6.2/src/popthelp.c ++++ mc-4.6.2/src/popthelp.c +@@ -101,7 +101,7 @@ + goto out; + } + +- helpLength = strlen(help); ++ helpLength = mbstrlen(help); + while (helpLength > lineLength) { + ch = help + lineLength - 1; + while (ch > help && !isspace((unsigned char) *ch)) ch--; +@@ -112,7 +112,7 @@ + fprintf(f, "%.*s\n%*s", (int) (ch - help), help, indentLength, " "); + help = ch; + while (isspace((unsigned char) *help) && *help) help++; +- helpLength = strlen(help); ++ helpLength = mbstrlen(help); + } + + if (helpLength) fprintf(f, "%s\n", help); +@@ -233,7 +233,7 @@ + if (len == 3) return cursor; + + if (argDescrip) +- len += strlen(argDescrip) + 1; ++ len += mbstrlen(argDescrip) + 1; + + if ((cursor + len) > 79) { + fprintf(f, "\n "); +@@ -304,7 +304,7 @@ + singleTableUsage(f, cursor, con->options, NULL); + + if (con->otherHelp) { +- cursor += strlen(con->otherHelp) + 1; ++ cursor += mbstrlen(con->otherHelp) + 1; + if (cursor > 79) fprintf(f, "\n "); + fprintf(f, " %s", con->otherHelp); + } +--- mc-4.6.2/src/util.c ++++ mc-4.6.2/src/util.c +@@ -152,7 +152,7 @@ + int columns = 0; + int i; + #ifdef UTF8 +- if (SLsmg_Is_Unicode) { ++ if (1) { + static mbstate_t s; + while (columns < col) { + memset (&s, 0, sizeof (s)); diff --git a/packages/mc/mc-4.6.2/00-76-utf8-hotlist-highlight.patch b/packages/mc/mc-4.6.2/00-76-utf8-hotlist-highlight.patch new file mode 100644 index 0000000000..38e0337ad6 --- /dev/null +++ b/packages/mc/mc-4.6.2/00-76-utf8-hotlist-highlight.patch @@ -0,0 +1,23 @@ +Highlight the lines of hotlist in full width. + +================================================================================ +--- mc-4.6.2/src/widget.c ++++ mc-4.6.2/src/widget.c +@@ -1994,6 +1994,7 @@ + { + WLEntry *e; + int i; ++ int j; + int sel_line; + Dlg_head *h = l->widget.parent; + int normalc = DLG_NORMALC (h); +@@ -2024,7 +2025,8 @@ + text = e->text; + e = e->next; + } +- tty_printf (" %-*s ", l->width-2, name_trunc (text, l->width-2)); ++ j = columns_to_bytes (name_trunc (text, l->width-2), l->width-2); ++ tty_printf (" %-*s ", j, name_trunc (text, l->width-2)); + } + l->cursor_y = sel_line; + if (!l->scrollbar) diff --git a/packages/mc/mc-4.6.2/00-77-utf8-filename-search-highlight.patch b/packages/mc/mc-4.6.2/00-77-utf8-filename-search-highlight.patch new file mode 100644 index 0000000000..912d8eb72b --- /dev/null +++ b/packages/mc/mc-4.6.2/00-77-utf8-filename-search-highlight.patch @@ -0,0 +1,17 @@ +Quick filename search (^S) highlights correct width. + +================================================================================ +--- mc-4.6.2/src/screen.c ++++ mc-4.6.2/src/screen.c +@@ -771,8 +771,10 @@ + widget_move (&panel->widget, llines (panel)+3, 1); + + if (panel->searching){ ++ int j; + attrset (INPUT_COLOR); +- tty_printf ("/%-*s", panel->widget.cols-3, panel->search_buffer); ++ j = columns_to_bytes (panel->search_buffer, panel->widget.cols-3); ++ tty_printf ("/%-*s", j, panel->search_buffer); + attrset (NORMAL_COLOR); + return; + } diff --git a/packages/mc/mc-4.6.2/mc-utf8-look-and-feel.patch b/packages/mc/mc-4.6.2/mc-utf8-look-and-feel.patch new file mode 100644 index 0000000000..83ad60135d --- /dev/null +++ b/packages/mc/mc-4.6.2/mc-utf8-look-and-feel.patch @@ -0,0 +1,206 @@ +Index: mc-4.6.2~git20080311/src/main.c +================================================================================ +--- mc-4.6.2/src/main.c ++++ mc-4.6.2/src/main.c +@@ -276,6 +276,9 @@ + /* The user's shell */ + const char *shell = NULL; + ++/* Is the LANG UTF-8 ? */ ++gboolean is_utf8 = FALSE; ++ + /* mc_home: The home of MC */ + char *mc_home = NULL; + +@@ -2126,6 +2129,16 @@ + int + main (int argc, char *argv[]) + { ++ /* Check whether we have UTF-8 locale */ ++ char *lang = getenv("LANG"); ++ size_t len = 0; ++ ++ if ( lang ) ++ len = strlen(lang); ++ ++ if ( len >= 5 && !strcasecmp(&lang[len-5],"UTF-8") ) ++ is_utf8 = TRUE; ++ + /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */ + setlocale (LC_ALL, ""); + bindtextdomain ("mc", LOCALEDIR); +--- mc-4.6.2/src/main.h ++++ mc-4.6.2/src/main.h +@@ -69,6 +69,7 @@ + extern int only_leading_plus_minus; + extern int output_starts_shell; + extern int midnight_shutdown; ++extern gboolean is_utf8; + extern char cmd_buf [512]; + extern const char *shell; + +--- mc-4.6.2/src/screen.c ++++ mc-4.6.2/src/screen.c +@@ -892,6 +892,9 @@ + } + #endif /* HAVE_SLANG */ + ++ vscrollbar (panel->widget, panel->widget.lines, panel->widget.cols-1, 2, 2, ++ panel->selected, panel->count, TRUE); ++ + if (panel->active) + attrset (REVERSE_COLOR); + +@@ -1493,7 +1496,7 @@ + panel->dirty = 1; + + /* Status needn't to be split */ +- usable_columns = ((panel->widget.cols-2)/((isstatus) ++ usable_columns = ((panel->widget.cols-3)/((isstatus) + ? 1 + : (panel->split+1))) - (!isstatus && panel->split); + +--- mc-4.6.2/src/widget.c ++++ mc-4.6.2/src/widget.c +@@ -1944,52 +1944,86 @@ + return in; + } + +- +-/* Listbox widget */ ++/* Vertical scrollbar widget */ + +-/* Should draw the scrollbar, but currently draws only +- * indications that there is more information +- */ +-static int listbox_cdiff (WLEntry *s, WLEntry *e); +- +-static void +-listbox_drawscroll (WListbox *l) ++void ++vscrollbar (Widget widget, int height, int width, int tpad, int bpad, ++ int selected, int count, gboolean color) + { + int line; +- int i, top; +- int max_line = l->height-1; +- ++ int i; ++ + /* Are we at the top? */ +- widget_move (&l->widget, 0, l->width); +- if (l->list == l->top) +- one_vline (); ++ widget_move (&widget, tpad, width); ++#ifndef UTF8 ++ if (!selected) ++ one_vline (); + else +- addch ('^'); ++ addch ('^'); ++#else ++ if (color) attrset (MARKED_COLOR); ++ if (is_utf8) ++ SLsmg_write_string("▴"); ++ else ++ addch ('^'); ++ if (color) attrset (NORMAL_COLOR); ++#endif + + /* Are we at the bottom? */ +- widget_move (&l->widget, max_line, l->width); +- top = listbox_cdiff (l->list, l->top); +- if ((top + l->height == l->count) || l->height >= l->count) +- one_vline (); ++ widget_move (&widget, height-1-bpad, width); ++#ifndef UTF8 ++ if (selected == count-1) ++ one_vline (); ++ else ++ addch ('v'); ++#else ++ if (color) attrset (MARKED_COLOR); ++ if (is_utf8) ++ SLsmg_write_string("▾"); + else +- addch ('v'); ++ addch('v'); ++ if (color) attrset (NORMAL_COLOR); ++#endif + + /* Now draw the nice relative pointer */ +- if (l->count) +- line = 1+ ((l->pos * (l->height-2)) / l->count); ++ if (count > 1) ++ line = tpad + 1 + ((selected * (height-3-tpad-bpad)) / (count-1)); + else +- line = 0; +- +- for (i = 1; i < max_line; i++){ +- widget_move (&l->widget, i, l->width); +- if (i != line) +- one_vline (); +- else +- addch ('*'); ++ line = 0; ++ ++ for (i = tpad + 1; i < height-1-bpad; i++){ ++ widget_move (&widget, i, width); ++ if (i != line) ++#ifndef UTF8 ++ one_vline (); ++ else ++ addch ('*'); ++#else ++ if (is_utf8) ++ SLsmg_write_string("▒"); ++ else ++ one_vline(); ++ else { ++ if (color) attrset (MARKED_COLOR); ++ if (is_utf8) ++ SLsmg_write_string("◈"); ++ else ++ addch('*'); ++ if (color) attrset (NORMAL_COLOR); ++ } ++#endif + } + } +- +-static void ++ ++ ++/* Listbox widget */ ++ ++/* Should draw the scrollbar, but currently draws only ++ * indications that there is more information ++ */ ++static int listbox_cdiff (WLEntry *s, WLEntry *e); ++ ++void + listbox_draw (WListbox *l, int focused) + { + WLEntry *e; +@@ -2032,7 +2066,7 @@ + if (!l->scrollbar) + return; + attrset (normalc); +- listbox_drawscroll (l); ++ vscrollbar (l->widget, l->height, l->width, 0, 0, l->pos, l->count, FALSE); + } + + /* Returns the number of items between s and e, +--- mc-4.6.2/src/widget.h ++++ mc-4.6.2/src/widget.h +@@ -187,6 +187,10 @@ + /* Listbox manager */ + WLEntry *listbox_get_data (WListbox *l, int pos); + ++/* Vertical scrollbar */ ++void vscrollbar (Widget widget, int height, int width, int tpad, int bpad, ++ int selected, int count, gboolean color); ++ + /* search text int listbox entries */ + WLEntry *listbox_search_text (WListbox *l, const char *text); + void listbox_select_entry (WListbox *l, WLEntry *dest); diff --git a/packages/mc/mc-4.6.2/mc-utf8-nlink.patch b/packages/mc/mc-4.6.2/mc-utf8-nlink.patch new file mode 100644 index 0000000000..9e361630c4 --- /dev/null +++ b/packages/mc/mc-4.6.2/mc-utf8-nlink.patch @@ -0,0 +1,11 @@ +--- mc-4.6.2/src/screen.c ++++ mc-4.6.2/src/screen.c +@@ -671,7 +671,7 @@ + SLsmg_write_nwchars (((wchar_t *) buffer) + + txtlen - n2, n2); + } else +- SLsmg_write_nwchars ((wchar_t *) buffer, len); ++ SLsmg_write_nwchars ((wchar_t *) buffer + still, len); + } else { + printw ("%*s", still, ""); + SLsmg_write_nwchars ((wchar_t *) buffer, txtlen); diff --git a/packages/mc/mc-4.6.2/mc-utf8.patch b/packages/mc/mc-4.6.2/mc-utf8.patch new file mode 100644 index 0000000000..5778604350 --- /dev/null +++ b/packages/mc/mc-4.6.2/mc-utf8.patch @@ -0,0 +1,5357 @@ +--- mc-4.6.2/acinclude.m4 ++++ mc-4.6.2/acinclude.m4 +@@ -399,14 +399,14 @@ + fi + + dnl Unless external S-Lang was requested, reject S-Lang with UTF-8 hacks +- if test x$with_screen = xslang; then +- : +- m4_if([$1], strict, , +- [AC_CHECK_LIB([slang], [SLsmg_write_nwchars], +- [AC_MSG_WARN([Rejecting S-Lang with UTF-8 support, \ +-it's not fully supported yet]) +- with_screen=mcslang])]) +- fi ++dnl if test x$with_screen = xslang; then ++dnl : ++dnl m4_if([$1], strict, , ++dnl [AC_CHECK_LIB([slang], [SLsmg_write_nwchars], ++dnl [AC_MSG_WARN([Rejecting S-Lang with UTF-8 support, \ ++dnl it's not fully supported yet]) ++dnl with_screen=mcslang])]) ++dnl fi + + if test x$with_screen = xslang; then + AC_DEFINE(HAVE_SYSTEM_SLANG, 1, +--- mc-4.6.2/edit/edit-widget.h ++++ mc-4.6.2/edit/edit-widget.h +@@ -30,6 +30,11 @@ + long command; + } edit_key_map_type; + ++struct action { ++ mc_wchar_t ch; ++ long flags; ++}; ++ + struct WEdit { + Widget widget; + +@@ -42,8 +47,17 @@ + /* dynamic buffers and cursor position for editor: */ + long curs1; /* position of the cursor from the beginning of the file. */ + long curs2; /* position from the end of the file */ ++#ifndef UTF8 + unsigned char *buffers1[MAXBUFF + 1]; /* all data up to curs1 */ + unsigned char *buffers2[MAXBUFF + 1]; /* all data from end of file down to curs2 */ ++#else /* UTF8 */ ++ mc_wchar_t *buffers1[MAXBUFF + 1]; /* all data up to curs1 */ ++ mc_wchar_t *buffers2[MAXBUFF + 1]; /* all data from end of file down to curs2 */ ++ ++ unsigned char charbuf[MB_LEN_MAX]; ++ int charpoint; ++#endif /* UTF8 */ ++ + + /* search variables */ + long search_start; /* First character to start searching from */ +@@ -87,7 +101,7 @@ + + /* undo stack and pointers */ + unsigned long stack_pointer; +- long *undo_stack; ++ struct action *undo_stack; + unsigned long stack_size; + unsigned long stack_size_mask; + unsigned long stack_bottom; +--- mc-4.6.2/edit/edit.c ++++ mc-4.6.2/edit/edit.c +@@ -105,7 +105,11 @@ + + static void user_menu (WEdit *edit); + ++#ifndef UTF8 + int edit_get_byte (WEdit * edit, long byte_index) ++#else ++mc_wchar_t edit_get_byte (WEdit * edit, long byte_index) ++#endif + { + unsigned long p; + if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) +@@ -134,7 +138,7 @@ + + edit->curs1 = 0; + edit->curs2 = 0; +- edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE); ++ edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + } + + /* +@@ -159,7 +163,7 @@ + } + + if (!edit->buffers2[buf2]) +- edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE); ++ edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + + mc_read (file, + (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE - +@@ -169,7 +173,7 @@ + for (buf = buf2 - 1; buf >= 0; buf--) { + /* edit->buffers2[0] is already allocated */ + if (!edit->buffers2[buf]) +- edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE); ++ edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE); + } + +@@ -242,9 +246,44 @@ + { + int c; + long i = 0; +- while ((c = fgetc (f)) >= 0) { ++#ifndef UTF8 ++ while ((c = fgetc (f)) != EOF) { + edit_insert (edit, c); + i++; ++#else /* UTF8 */ ++ unsigned char buf[MB_LEN_MAX]; ++ int charpos = 0; ++ mbstate_t mbs; ++ ++ while ((c = fgetc (f)) != EOF) { ++ mc_wchar_t wc; ++ int size; ++ int j; ++ ++ buf[charpos++] = c; ++ ++ memset (&mbs, 0, sizeof (mbs)); ++ size = mbrtowc(&wc, (char *)buf, charpos, &mbs); ++ ++ if (size == -2) ++ continue; /* incomplete */ ++ ++ else if (size >= 0) { ++ edit_insert (edit, wc); ++ i++; ++ charpos = 0; ++ continue; ++ } ++ else { ++ ++ /* invalid */ ++#ifdef __STDC_ISO_10646__ ++ for (j=0; jlast_byte; i++) + if (fputc (edit_get_byte (edit, i), f) < 0) + break; ++#else /* UTF8 */ ++ for (i = 0; i < edit->last_byte; i++) { ++ mc_wchar_t wc = edit_get_byte (edit, i); ++ int res; ++ char tmpbuf[MB_LEN_MAX]; ++ mbstate_t mbs; ++ ++ memset (&mbs, 0, sizeof (mbs)); ++ ++#ifdef __STDC_ISO_10646__ ++ if (wc >= BINARY_CHAR_OFFSET && wc < (BINARY_CHAR_OFFSET + 256)) { ++ res = 1; ++ tmpbuf[0] = (char) (wc - BINARY_CHAR_OFFSET); ++ } else ++#endif ++ res = wcrtomb(tmpbuf, wc, &mbs); ++ if (res > 0) { ++ if (fwrite(tmpbuf, res, 1, f) != 1) ++ break; ++ } ++ } ++#endif /* UTF8 */ + return i; + } + +@@ -293,12 +355,46 @@ + int i, file, blocklen; + long current = edit->curs1; + unsigned char *buf; ++#ifdef UTF8 ++ mbstate_t mbs; ++ int bufstart = 0; ++ ++ memset (&mbs, 0, sizeof (mbs)); ++#endif /* UTF8 */ + if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) + return 0; + buf = g_malloc (TEMP_BUF_LEN); ++#ifndef UTF8 + while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) { + for (i = 0; i < blocklen; i++) + edit_insert (edit, buf[i]); ++#else /* UTF8 */ ++ while ((blocklen = mc_read (file, (char *) buf + bufstart, TEMP_BUF_LEN - bufstart)) > 0) { ++ blocklen += bufstart; ++ bufstart = 0; ++ for (i = 0; i < blocklen; ) { ++ mc_wchar_t wc; ++ int j; ++ int size = mbrtowc(&wc, (char *)buf + i, blocklen - i, &mbs); ++ if (size == -2) { /*incomplete char*/ ++ bufstart = blocklen - i; ++ memcpy(buf, buf+i, bufstart); ++ i = blocklen; ++ memset (&mbs, 0, sizeof (mbs)); ++ } ++ else if (size <= 0) { ++#ifdef __STDC_ISO_10646__ ++ edit_insert (edit, BINARY_CHAR_OFFSET + (mc_wchar_t)buf[i]); ++#endif ++ memset (&mbs, 0, sizeof (mbs)); ++ i++; /* skip broken char */ ++ } ++ else { ++ edit_insert (edit, wc); ++ i+=size; ++ } ++ } ++#endif /* UTF8 */ + } + edit_cursor_move (edit, current - edit->curs1); + g_free (buf); +@@ -388,7 +484,11 @@ + static int + edit_load_file (WEdit *edit) + { ++#ifndef UTF8 + int fast_load = 1; ++#else /* UTF8 */ ++ int fast_load = 0; /* can't be used with multibyte characters */ ++#endif /* UTF8 */ + + /* Cannot do fast load if a filter is used */ + if (edit_find_filter (edit->filename) >= 0) +@@ -454,6 +554,7 @@ + edit->prev_col = column; + edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1)); + edit_move_display (edit, line - (edit->num_widget_lines / 2)); ++ edit->charpoint = 0; + } + + /* Save cursor position in the file */ +@@ -537,7 +638,7 @@ + edit_set_filename (edit, filename); + edit->stack_size = START_STACK_SIZE; + edit->stack_size_mask = START_STACK_SIZE - 1; +- edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long)); ++ edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (struct action)); + if (edit_load_file (edit)) { + /* edit_load_file already gives an error message */ + if (to_free) +@@ -692,14 +793,23 @@ + { + unsigned long sp = edit->stack_pointer; + unsigned long spm1; +- long *t; ++ ++ struct action *t; ++ mc_wchar_t ch = 0; ++ ++ if (c == CHAR_INSERT || c == CHAR_INSERT_AHEAD) { ++ va_list ap; ++ va_start (ap, c); ++ ch = va_arg (ap, mc_wint_t); ++ va_end (ap); ++ } + + /* first enlarge the stack if necessary */ + if (sp > edit->stack_size - 10) { /* say */ + if (option_max_undo < 256) + option_max_undo = 256; + if (edit->stack_size < (unsigned long) option_max_undo) { +- t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long)); ++ t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (struct action)); + if (t) { + edit->undo_stack = t; + edit->stack_size <<= 1; +@@ -714,7 +824,7 @@ + #ifdef FAST_MOVE_CURSOR + if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) { + va_list ap; +- edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT; ++ edit->undo_stack[sp].flags = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT; + edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask; + va_start (ap, c); + c = -(va_arg (ap, int)); +@@ -725,12 +835,14 @@ + && spm1 != edit->stack_bottom + && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) { + int d; +- if (edit->undo_stack[spm1] < 0) { +- d = edit->undo_stack[(sp - 2) & edit->stack_size_mask]; +- if (d == c) { +- if (edit->undo_stack[spm1] > -1000000000) { ++ mc_wchar_t d_ch; ++ if (edit->undo_stack[spm1].flags < 0) { ++ d = edit->undo_stack[(sp - 2) & edit->stack_size_mask].flags; ++ d_ch = edit->undo_stack[(sp - 2) & edit->stack_size_mask].ch; ++ if (d == c && d_ch == ch) { ++ if (edit->undo_stack[spm1].flags > -1000000000) { + if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */ +- edit->undo_stack[spm1]--; ++ edit->undo_stack[spm1].flags--; + return; + } + } +@@ -738,19 +850,20 @@ + #ifndef NO_STACK_CURSMOVE_ANIHILATION + else if ((c == CURS_LEFT && d == CURS_RIGHT) + || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */ +- if (edit->undo_stack[spm1] == -2) ++ if (edit->undo_stack[spm1].flags == -2) + edit->stack_pointer = spm1; + else +- edit->undo_stack[spm1]++; ++ edit->undo_stack[spm1].flags++; + return; + } + #endif + } else { +- d = edit->undo_stack[spm1]; +- if (d == c) { ++ d = edit->undo_stack[spm1].flags; ++ d_ch = edit->undo_stack[spm1].ch; ++ if (d == c && d_ch == ch) { + if (c >= KEY_PRESS) + return; /* --> no need to push multiple do-nothings */ +- edit->undo_stack[sp] = -2; ++ edit->undo_stack[sp].flags = -2; + goto check_bottom; + } + #ifndef NO_STACK_CURSMOVE_ANIHILATION +@@ -762,7 +875,9 @@ + #endif + } + } +- edit->undo_stack[sp] = c; ++ edit->undo_stack[sp].flags = c; ++ edit->undo_stack[sp].ch = ch; ++ + check_bottom: + + edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask; +@@ -775,10 +890,10 @@ + (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom) + do { + edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask; +- } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer); ++ } while (edit->undo_stack[edit->stack_bottom].flags < KEY_PRESS && edit->stack_bottom != edit->stack_pointer); + + /*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */ +- if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS) ++ if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom].flags < KEY_PRESS) + edit->stack_bottom = edit->stack_pointer = 0; + } + +@@ -787,30 +902,30 @@ + then the file should be as it was when he loaded up. Then set edit->modified to 0. + */ + static long +-pop_action (WEdit * edit) ++pop_action (WEdit * edit, struct action *c) + { +- long c; + unsigned long sp = edit->stack_pointer; + if (sp == edit->stack_bottom) { +- return STACK_BOTTOM; ++ c->flags = STACK_BOTTOM; ++ return c->flags; + } + sp = (sp - 1) & edit->stack_size_mask; +- if ((c = edit->undo_stack[sp]) >= 0) { +-/* edit->undo_stack[sp] = '@'; */ ++ *c = edit->undo_stack[sp]; ++ if (edit->undo_stack[sp].flags >= 0) { + edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask; +- return c; ++ return c->flags; + } + if (sp == edit->stack_bottom) { + return STACK_BOTTOM; + } +- c = edit->undo_stack[(sp - 1) & edit->stack_size_mask]; +- if (edit->undo_stack[sp] == -2) { +-/* edit->undo_stack[sp] = '@'; */ ++ *c = edit->undo_stack[(sp - 1) & edit->stack_size_mask]; ++ ++ if (edit->undo_stack[sp].flags == -2) { + edit->stack_pointer = sp; + } else +- edit->undo_stack[sp]++; ++ edit->undo_stack[sp].flags++; + +- return c; ++ return c->flags; + } + + /* is called whenever a modification is made by one of the four routines below */ +@@ -831,7 +946,7 @@ + */ + + void +-edit_insert (WEdit *edit, int c) ++edit_insert (WEdit *edit, mc_wchar_t c) + { + /* check if file has grown to large */ + if (edit->last_byte >= SIZE_LIMIT) +@@ -869,12 +984,11 @@ + /* add a new buffer if we've reached the end of the last one */ + if (!(edit->curs1 & M_EDIT_BUF_SIZE)) + edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = +- g_malloc (EDIT_BUF_SIZE); ++ g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + + /* perform the insertion */ +- edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit-> +- curs1 & M_EDIT_BUF_SIZE] +- = (unsigned char) c; ++ edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] ++ [edit->curs1 & M_EDIT_BUF_SIZE] = c; + + /* update file length */ + edit->last_byte++; +@@ -885,7 +999,7 @@ + + + /* same as edit_insert and move left */ +-void edit_insert_ahead (WEdit * edit, int c) ++void edit_insert_ahead (WEdit * edit, mc_wchar_t c) + { + if (edit->last_byte >= SIZE_LIMIT) + return; +@@ -908,7 +1022,7 @@ + edit->last_get_rule += (edit->last_get_rule >= edit->curs1); + + if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE)) +- edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE); ++ edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c; + + edit->last_byte++; +@@ -918,7 +1032,7 @@ + + int edit_delete (WEdit * edit) + { +- int p; ++ mc_wint_t p; + if (!edit->curs2) + return 0; + +@@ -942,7 +1056,7 @@ + edit->total_lines--; + edit->force |= REDRAW_AFTER_CURSOR; + } +- edit_push_action (edit, p + 256); ++ edit_push_action (edit, CHAR_INSERT_AHEAD, p); + if (edit->curs1 < edit->start_display) { + edit->start_display--; + if (p == '\n') +@@ -956,7 +1070,7 @@ + static int + edit_backspace (WEdit * edit) + { +- int p; ++ mc_wint_t p; + if (!edit->curs1) + return 0; + +@@ -980,7 +1094,7 @@ + edit->total_lines--; + edit->force |= REDRAW_AFTER_CURSOR; + } +- edit_push_action (edit, p); ++ edit_push_action (edit, CHAR_INSERT, p); + + if (edit->curs1 < edit->start_display) { + edit->start_display--; +@@ -993,10 +1107,18 @@ + + #ifdef FAST_MOVE_CURSOR + +-static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n) ++static void memqcpy (WEdit * edit, mc_wchar_t *dest, mc_wchar_t *src, int n) + { + unsigned long next; ++#ifndef UTF8 + while ((next = (unsigned long) memccpy (dest, src, '\n', n))) { ++#else /* UTF8 */ ++ while (n) { ++ next = 0; ++ while (next < n && src[next]!='\n') next++; ++ if (next < n) next++; ++ wmemcpy (dest, src, next) ++#endif /* UTF8 */ + edit->curs_line--; + next -= (unsigned long) dest; + n -= next; +@@ -1009,7 +1131,7 @@ + edit_move_backward_lots (WEdit *edit, long increment) + { + int r, s, t; +- unsigned char *p; ++ mc_wchar_t *p; + + if (increment > edit->curs1) + increment = edit->curs1; +@@ -1049,7 +1171,7 @@ + edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p; + else + edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = +- g_malloc (EDIT_BUF_SIZE); ++ g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + } else { + g_free (p); + } +@@ -1087,7 +1209,7 @@ + edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p; + else + edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = +- g_malloc (EDIT_BUF_SIZE); ++ g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + } else { + g_free (p); + } +@@ -1120,7 +1242,7 @@ + + c = edit_get_byte (edit, edit->curs1 - 1); + if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE)) +- edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE); ++ edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c; + edit->curs2++; + c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE]; +@@ -1144,7 +1266,7 @@ + + c = edit_get_byte (edit, edit->curs1); + if (!(edit->curs1 & M_EDIT_BUF_SIZE)) +- edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE); ++ edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE * sizeof(mc_wchar_t)); + edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c; + edit->curs1++; + c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1]; +@@ -1249,7 +1371,7 @@ + q = edit->last_byte + 2; + + for (col = 0, p = current; p < q; p++) { +- int c; ++ mc_wchar_t c; + if (cols != -10) { + if (col == cols) + return p; +@@ -1267,7 +1389,7 @@ + } else if (c < 32 || c == 127) + col += 2; /* Caret notation for control characters */ + else +- col++; ++ col += wcwidth(c); + } + return col; + } +@@ -1400,12 +1522,16 @@ + is_blank (WEdit *edit, long offset) + { + long s, f; +- int c; ++ mc_wchar_t c; + s = edit_bol (edit, offset); + f = edit_eol (edit, offset) - 1; + while (s <= f) { + c = edit_get_byte (edit, s++); ++#ifndef UTF8 + if (!isspace (c)) ++#else ++ if (!iswspace (c)) ++#endif /* UTF8 */ + return 0; + } + return 1; +@@ -1660,6 +1786,7 @@ + return 2; + return 0x80000000UL; + } ++#ifndef UTF8 + if (isupper (c)) + c = 'A'; + else if (islower (c)) +@@ -1670,6 +1797,18 @@ + c = '0'; + else if (isspace (c)) + c = ' '; ++#else ++ if (iswupper (c)) ++ c = 'A'; ++ else if (iswlower (c)) ++ c = 'a'; ++ else if (iswalpha (c)) ++ c = 'a'; ++ else if (iswdigit (c)) ++ c = '0'; ++ else if (iswspace (c)) ++ c = ' '; ++#endif /* UTF8 */ + q = strchr (option_chars_move_whole_word, c); + if (!q) + return 0xFFFFFFFFUL; +@@ -1694,10 +1833,18 @@ + c2 = edit_get_byte (edit, edit->curs1); + if (!(my_type_of (c1) & my_type_of (c2))) + break; ++#ifndef UTF8 + if (isspace (c1) && !isspace (c2)) ++#else ++ if (iswspace (c1) && !iswspace (c2)) ++#endif /* UTF8 */ + break; + if (s) ++#ifndef UTF8 + if (!isspace (c1) && isspace (c2)) ++#else ++ if (!iswspace (c1) && iswspace (c2)) ++#endif /* UTF8 */ + break; + } + } +@@ -1720,10 +1867,18 @@ + c2 = edit_get_byte (edit, edit->curs1); + if (!(my_type_of (c1) & my_type_of (c2))) + break; ++#ifndef UTF8 + if (isspace (c1) && !isspace (c2)) ++#else ++ if (iswspace (c1) && !iswspace (c2)) ++#endif /* UTF8 */ + break; + if (s) ++#ifndef UTF8 + if (!isspace (c1) && isspace (c2)) ++#else ++ if (!iswspace (c1) && iswspace (c2)) ++#endif /* UTF8 */ + break; + } + } +@@ -1743,7 +1898,11 @@ + break; + c1 = edit_delete (edit); + c2 = edit_get_byte (edit, edit->curs1); ++#ifndef UTF8 + if ((isspace (c1) == 0) != (isspace (c2) == 0)) ++#else ++ if ((iswspace (c1) == 0) != (iswspace (c2) == 0)) ++#endif /* UTF8 */ + break; + if (!(my_type_of (c1) & my_type_of (c2))) + break; +@@ -1758,7 +1917,11 @@ + break; + c1 = edit_backspace (edit); + c2 = edit_get_byte (edit, edit->curs1 - 1); ++#ifndef UTF8 + if ((isspace (c1) == 0) != (isspace (c2) == 0)) ++#else ++ if ((iswspace (c1) == 0) != (iswspace (c2) == 0)) ++#endif /* UTF8 */ + break; + if (!(my_type_of (c1) & my_type_of (c2))) + break; +@@ -1772,13 +1935,13 @@ + static void + edit_do_undo (WEdit * edit) + { +- long ac; ++ struct action ac; + long count = 0; + + edit->stack_disable = 1; /* don't record undo's onto undo stack! */ + +- while ((ac = pop_action (edit)) < KEY_PRESS) { +- switch ((int) ac) { ++ while (pop_action (edit, &ac) < KEY_PRESS) { ++ switch ((int) ac.flags) { + case STACK_BOTTOM: + goto done_undo; + case CURS_RIGHT: +@@ -1799,31 +1962,33 @@ + case COLUMN_OFF: + column_highlighting = 0; + break; ++ case CHAR_INSERT: ++ edit_insert (edit, ac.ch); ++ break; ++ case CHAR_INSERT_AHEAD: ++ edit_insert_ahead (edit, ac.ch); ++ break; + } +- if (ac >= 256 && ac < 512) +- edit_insert_ahead (edit, ac - 256); +- if (ac >= 0 && ac < 256) +- edit_insert (edit, ac); + +- if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) { +- edit->mark1 = ac - MARK_1; ++ if (ac.flags >= MARK_1 - 2 && ac.flags < MARK_2 - 2) { ++ edit->mark1 = ac.flags - MARK_1; + edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1); +- } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) { +- edit->mark2 = ac - MARK_2; ++ } else if (ac.flags >= MARK_2 - 2 && ac.flags < KEY_PRESS) { ++ edit->mark2 = ac.flags - MARK_2; + edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2); + } + if (count++) + edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */ + } + +- if (edit->start_display > ac - KEY_PRESS) { +- edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display); ++ if (edit->start_display > ac.flags - KEY_PRESS) { ++ edit->start_line -= edit_count_lines (edit, ac.flags - KEY_PRESS, edit->start_display); + edit->force |= REDRAW_PAGE; +- } else if (edit->start_display < ac - KEY_PRESS) { +- edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS); ++ } else if (edit->start_display < ac.flags - KEY_PRESS) { ++ edit->start_line += edit_count_lines (edit, edit->start_display, ac.flags - KEY_PRESS); + edit->force |= REDRAW_PAGE; + } +- edit->start_display = ac - KEY_PRESS; /* see push and pop above */ ++ edit->start_display = ac.flags - KEY_PRESS; /* see push and pop above */ + edit_update_curs_row (edit); + + done_undo:; +@@ -2103,7 +2268,7 @@ + * passed as -1. Commands are executed, and char_for_insertion is + * inserted at the cursor. + */ +-void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion) ++void edit_execute_key_command (WEdit *edit, int command, mc_wint_t char_for_insertion) + { + if (command == CK_Begin_Record_Macro) { + edit->macro_i = 0; +@@ -2138,7 +2303,7 @@ + all of them. It also does not check for the Undo command. + */ + void +-edit_execute_cmd (WEdit *edit, int command, int char_for_insertion) ++edit_execute_cmd (WEdit *edit, int command, mc_wint_t char_for_insertion) + { + edit->force |= REDRAW_LINE; + +@@ -2171,7 +2336,7 @@ + } + + /* An ordinary key press */ +- if (char_for_insertion >= 0) { ++ if (char_for_insertion != (mc_wint_t) -1) { + if (edit->overwrite) { + if (edit_get_byte (edit, edit->curs1) != '\n') + edit_delete (edit); +--- mc-4.6.2/edit/edit.h ++++ mc-4.6.2/edit/edit.h +@@ -25,6 +25,27 @@ + + #include + ++#include "src/tty.h" ++ ++#ifdef UTF8 ++#include ++#include ++ ++#define mc_wchar_t wchar_t ++#define mc_wint_t wint_t ++ ++#else ++ ++#define mc_wchar_t unsigned char ++#define mc_wint_t int ++ ++#endif ++ ++ ++/* unicode private use area */ ++#define BINARY_CHAR_OFFSET 0xFFE00 ++ ++ + #define N_menus 5 + + #define SEARCH_DIALOG_OPTION_NO_SCANF (1 << 0) +@@ -86,6 +107,8 @@ + #define START_STACK_SIZE 32 + + /* Some codes that may be pushed onto or returned from the undo stack */ ++#define CHAR_INSERT 65 ++#define CHAR_INSERT_AHEAD 66 + #define CURS_LEFT 601 + #define CURS_RIGHT 602 + #define DELCHAR 603 +@@ -105,7 +128,7 @@ + + struct macro { + short command; +- short ch; ++ mc_wchar_t ch; + }; + + struct WEdit; +@@ -120,8 +143,12 @@ + void menu_save_mode_cmd (void); + int edit_raw_key_query (const char *heading, const char *query, int cancel); + int edit_file (const char *_file, int line); +-int edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch); ++int edit_translate_key (WEdit *edit, long x_key, int *cmd, mc_wint_t *ch); ++#ifndef UTF8 + int edit_get_byte (WEdit * edit, long byte_index); ++#else /* UTF8 */ ++mc_wchar_t edit_get_byte (WEdit * edit, long byte_index); ++#endif /* UTF8 */ + int edit_count_lines (WEdit * edit, long current, int upto); + long edit_move_forward (WEdit * edit, long current, int lines, long upto); + long edit_move_forward3 (WEdit * edit, long current, int cols, long upto); +@@ -148,11 +175,11 @@ + void edit_delete_line (WEdit * edit); + + int edit_delete (WEdit * edit); +-void edit_insert (WEdit * edit, int c); ++void edit_insert (WEdit * edit, mc_wchar_t c); + void edit_cursor_move (WEdit * edit, long increment); + void edit_push_action (WEdit * edit, long c, ...); + void edit_push_key_press (WEdit * edit); +-void edit_insert_ahead (WEdit * edit, int c); ++void edit_insert_ahead (WEdit * edit, mc_wchar_t c); + long edit_write_stream (WEdit * edit, FILE * f); + char *edit_get_write_filter (const char *writename, const char *filename); + int edit_save_confirm_cmd (WEdit * edit); +@@ -183,7 +210,7 @@ + int eval_marks (WEdit * edit, long *start_mark, long *end_mark); + void edit_status (WEdit * edit); + void edit_execute_key_command (WEdit *edit, int command, +- int char_for_insertion); ++ mc_wint_t char_for_insertion); + void edit_update_screen (WEdit * edit); + int edit_print_string (WEdit * e, const char *s); + void edit_move_to_line (WEdit * e, long line); +@@ -233,7 +260,7 @@ + void format_paragraph (WEdit *edit, int force); + + /* either command or char_for_insertion must be passed as -1 */ +-void edit_execute_cmd (WEdit *edit, int command, int char_for_insertion); ++void edit_execute_cmd (WEdit *edit, int command, mc_wint_t char_for_insertion); + + #define get_sys_error(s) (s) + +--- mc-4.6.2/edit/editcmd.c ++++ mc-4.6.2/edit/editcmd.c +@@ -60,7 +60,7 @@ + #include "../src/selcodepage.h" + + struct selection { +- unsigned char * text; ++ mc_wchar_t *text; + int len; + }; + +@@ -83,12 +83,16 @@ + #define MAX_REPL_LEN 1024 + + static int edit_save_cmd (WEdit *edit); +-static unsigned char *edit_get_block (WEdit *edit, long start, ++static mc_wchar_t *edit_get_block (WEdit *edit, long start, + long finish, int *l); + +-static inline int my_lower_case (int c) ++static inline mc_wchar_t my_lower_case (mc_wchar_t c) + { ++#ifndef UTF8 + return tolower(c & 0xFF); ++#else ++ return towlower(c); ++#endif + } + + static const char * +@@ -123,11 +127,11 @@ + #endif /* !HAVE_MEMMOVE */ + + /* #define itoa MY_itoa <---- this line is now in edit.h */ +-static char * ++static mc_wchar_t * + MY_itoa (int i) + { +- static char t[14]; +- char *s = t + 13; ++ static mc_wchar_t t[14]; ++ mc_wchar_t *s = t + 13; + int j = i; + *s-- = 0; + do { +@@ -212,6 +216,48 @@ + doupdate(); + } + ++#ifdef UTF8 ++ ++static size_t ++wchar_write(int fd, mc_wchar_t *buf, size_t len) ++{ ++ char *tmpbuf = g_malloc(len + MB_LEN_MAX); ++ mbstate_t mbs; ++ size_t i; ++ size_t outlen = 0; ++ size_t res; ++ ++ for (i = 0; i < len; i++) { ++ if (outlen >= len) { ++ if ((res = mc_write(fd, tmpbuf, outlen)) != outlen) { ++ g_free(tmpbuf); ++ return -1; ++ } ++ outlen = 0; ++ } ++ memset (&mbs, 0, sizeof (mbs)); ++#ifdef __STDC_ISO_10646__ ++ if (buf[i] >= BINARY_CHAR_OFFSET && buf[i] < (BINARY_CHAR_OFFSET + 256)) { ++ res = 1; ++ tmpbuf[outlen] = (char) (buf[i] - BINARY_CHAR_OFFSET); ++ ++ } else ++#endif ++ res = wcrtomb(tmpbuf + outlen, buf[i], &mbs); ++ if (res > 0) { ++ outlen += res; ++ } ++ } ++ if ((res = mc_write(fd, tmpbuf, outlen)) != outlen) { ++ g_free(tmpbuf); ++ return -1; ++ } ++ g_free(tmpbuf); ++ return len; ++} ++ ++#endif /* UTF8 */ ++ + /* If 0 (quick save) then a) create/truncate file, + b) save to ; + if 1 (safe save) then a) save to , +@@ -359,32 +405,48 @@ + buf = 0; + filelen = edit->last_byte; + while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1) { ++#ifndef UTF8 + if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) ++#else /* UTF8 */ ++ if (wchar_write (fd, edit->buffers1[buf], EDIT_BUF_SIZE) ++#endif /* UTF8 */ + != EDIT_BUF_SIZE) { + mc_close (fd); + goto error_save; + } + buf++; + } ++#ifndef UTF8 + if (mc_write + (fd, (char *) edit->buffers1[buf], ++#else /* UTF8 */ ++ if (wchar_write ++ (fd, edit->buffers1[buf], ++#endif /* UTF8 */ + edit->curs1 & M_EDIT_BUF_SIZE) != + (edit->curs1 & M_EDIT_BUF_SIZE)) { + filelen = -1; + } else if (edit->curs2) { + edit->curs2--; + buf = (edit->curs2 >> S_EDIT_BUF_SIZE); +- if (mc_write +- (fd, +- (char *) edit->buffers2[buf] + EDIT_BUF_SIZE - ++#ifndef UTF8 ++ if (mc_write(fd, (char *) edit->buffers2[buf] + EDIT_BUF_SIZE - ++#else /* UTF8 */ ++ if (wchar_write(fd, edit->buffers2[buf] + EDIT_BUF_SIZE - ++#endif /* UTF8 */ + (edit->curs2 & M_EDIT_BUF_SIZE) - 1, + 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != + 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) { + filelen = -1; + } else { + while (--buf >= 0) { ++#ifndef UTF8 + if (mc_write + (fd, (char *) edit->buffers2[buf], ++#else /* UTF8 */ ++ if (wchar_write ++ (fd, edit->buffers2[buf], ++#endif /* UTF8 */ + EDIT_BUF_SIZE) != EDIT_BUF_SIZE) { + filelen = -1; + break; +@@ -705,13 +767,21 @@ + if (!n || n == EOF) + break; + n = 0; ++#ifndef UTF8 + while (fscanf (f, "%hd %hd, ", ¯o[n].command, ¯o[n].ch)) ++#else /* UTF8 */ ++ while (fscanf (f, "%hd %lu, ", ¯o[n].command, ¯o[n].ch)) ++#endif /* UTF8 */ + n++; + fscanf (f, ";\n"); + if (s != k) { + fprintf (g, ("key '%d 0': "), s); + for (i = 0; i < n; i++) ++#ifndef UTF8 + fprintf (g, "%hd %hd, ", macro[i].command, macro[i].ch); ++#else /* UTF8 */ ++ fprintf (g, "%hd %lu, ", macro[i].command, macro[i].ch); ++#endif /* UTF8 */ + fprintf (g, ";\n"); + } + } +@@ -744,7 +814,11 @@ + if (f) { + fprintf (f, ("key '%d 0': "), s); + for (i = 0; i < n; i++) ++#ifndef UTF8 + fprintf (f, "%hd %hd, ", macro[i].command, macro[i].ch); ++#else /* UTF8 */ ++ fprintf (f, "%hd %lu, ", macro[i].command, macro[i].ch); ++#endif /* UTF8 */ + fprintf (f, ";\n"); + fclose (f); + if (saved_macros_loaded) { +@@ -794,10 +868,18 @@ + saved_macro[i++] = s; + if (!found) { + *n = 0; ++#ifndef UTF8 + while (*n < MAX_MACRO_LENGTH && 2 == fscanf (f, "%hd %hd, ", ¯o[*n].command, ¯o[*n].ch)) ++#else /* UTF8 */ ++ while (*n < MAX_MACRO_LENGTH && 2 == fscanf (f, "%hd %lu, ", ¯o[*n].command, ¯o[*n].ch)) ++#endif /* UTF8 */ + (*n)++; + } else { ++#ifndef UTF8 + while (2 == fscanf (f, "%hd %hd, ", &dummy.command, &dummy.ch)); ++#else /* UTF8 */ ++ while (2 == fscanf (f, "%hd %lu, ", &dummy.command, &dummy.ch)); ++#endif /* UTF8 */ + } + fscanf (f, ";\n"); + if (s == k) +@@ -945,7 +1027,7 @@ + #define space_width 1 + + static void +-edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width) ++edit_insert_column_of_text (WEdit * edit, mc_wchar_t *data, int size, int width) + { + long cursor; + int i, col; +@@ -993,7 +1075,7 @@ + { + long start_mark, end_mark, current = edit->curs1; + int size; +- unsigned char *copy_buf; ++ mc_wchar_t *copy_buf; + + edit_update_curs_col (edit); + if (eval_marks (edit, &start_mark, &end_mark)) +@@ -1033,7 +1115,7 @@ + { + long count; + long current; +- unsigned char *copy_buf; ++ mc_wchar_t *copy_buf; + long start_mark, end_mark; + int deleted = 0; + int x = 0; +@@ -1094,7 +1176,7 @@ + edit_push_action (edit, COLUMN_ON); + column_highlighting = 0; + } else { +- copy_buf = g_malloc (end_mark - start_mark); ++ copy_buf = g_malloc ((end_mark - start_mark) * sizeof(mc_wchar_t)); + edit_cursor_move (edit, start_mark - edit->curs1); + edit_scroll_screen_over_cursor (edit); + count = start_mark; +@@ -1433,7 +1515,11 @@ + /* This function is a modification of mc-3.2.10/src/view.c:regexp_view_search() */ + /* returns -3 on error in pattern, -1 on not found, found_len = 0 if either */ + static int ++#ifndef UTF8 + string_regexp_search (char *pattern, char *string, int match_type, ++#else /* UTF8 */ ++string_regexp_search (char *pattern, mc_wchar_t *wstring, int match_type, ++#endif /* UTF8 */ + int match_bol, int icase, int *found_len, void *d) + { + static regex_t r; +@@ -1442,6 +1528,11 @@ + regmatch_t *pmatch; + static regmatch_t s[1]; + ++#ifdef UTF8 ++ char *string; ++ int i; ++#endif /* UTF8 */ ++ + pmatch = (regmatch_t *) d; + if (!pmatch) + pmatch = s; +@@ -1462,13 +1553,51 @@ + old_type = match_type; + old_icase = icase; + } ++ ++#ifdef UTF8 ++ string = wchar_to_mbstr(wstring); ++ if (string == NULL) ++ return -1; ++#endif /* UTF8 */ ++ + if (regexec + (&r, string, d ? NUM_REPL_ARGS : 1, pmatch, + ((match_bol + || match_type != match_normal) ? 0 : REG_NOTBOL)) != 0) { + *found_len = 0; ++ ++#ifdef UTF8 ++ g_free(string); ++#endif /* UTF8 */ ++ + return -1; + } ++ ++#ifdef UTF8 ++ for (i = 0; i < (d ? NUM_REPL_ARGS : 1); i++) { ++ char tmp; ++ int new_o; ++ ++ if (pmatch[i].rm_so < 0) ++ continue; ++ tmp = string[pmatch[i].rm_so]; ++ string[pmatch[i].rm_so] = 0; ++ new_o = mbstrlen(string); ++ string[pmatch[i].rm_so] = tmp; ++ pmatch[i].rm_so = new_o; ++ ++ if (pmatch[i].rm_eo < 0) ++ continue; ++ tmp = string[pmatch[i].rm_eo]; ++ string[pmatch[i].rm_eo] = 0; ++ new_o = mbstrlen(string); ++ string[pmatch[i].rm_eo] = tmp; ++ pmatch[i].rm_eo = new_o; ++ } ++ ++ g_free(string); ++#endif /* UTF8 */ ++ + *found_len = pmatch[0].rm_eo - pmatch[0].rm_so; + return (pmatch[0].rm_so); + } +@@ -1476,13 +1605,29 @@ + /* thanks to Liviu Daia for getting this + (and the above) routines to work properly - paul */ + ++#ifndef UTF8 + typedef int (*edit_getbyte_fn) (WEdit *, long); ++#else /* UTF8 */ ++typedef mc_wchar_t (*edit_getbyte_fn) (WEdit *, long); ++#endif /* UTF8 */ + + static long ++#ifndef UTF8 + edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit_getbyte_fn get_byte, void *data, int once_only, void *d) ++#else /* UTF8 */ ++edit_find_string (long start, unsigned char *exp_mb, int *len, long last_byte, edit_getbyte_fn get_byte, void *data, int once_only, void *d) ++#endif /* UTF8 */ + { + long p, q = 0; +- long l = strlen ((char *) exp), f = 0; ++ long f = 0; ++ ++#ifndef UTF8 ++ long l = strlen ((char *) exp); ++#else /* UTF8 */ ++ mc_wchar_t *exp = mbstr_to_wchar((char *)exp_mb); ++ mc_wchar_t *exp_backup = exp; ++ long l = wcslen(exp); ++#endif /* UTF8 */ + int n = 0; + + for (p = 0; p < l; p++) /* count conversions... */ +@@ -1491,19 +1636,22 @@ + n++; + + if (replace_scanf || replace_regexp) { +- int c; +- unsigned char *buf; +- unsigned char mbuf[MAX_REPL_LEN * 2 + 3]; ++ mc_wint_t c; ++ mc_wchar_t *buf; ++ mc_wchar_t mbuf[MAX_REPL_LEN * 2 + 3]; + + replace_scanf = (!replace_regexp); /* can't have both */ + + buf = mbuf; + + if (replace_scanf) { +- unsigned char e[MAX_REPL_LEN]; +- if (n >= NUM_REPL_ARGS) +- return -3; +- ++ mc_wchar_t e[MAX_REPL_LEN]; ++ if (n >= NUM_REPL_ARGS) { ++#ifdef UTF8 ++ g_free(exp_backup); ++#endif /* UTF8 */ ++ return -3; ++ } + if (replace_case) { + for (p = start; p < last_byte && p < start + MAX_REPL_LEN; p++) + buf[p - start] = (*get_byte) (data, p); +@@ -1517,20 +1665,36 @@ + } + + buf[(q = p - start)] = 0; ++#ifndef UTF8 + strcpy ((char *) e, (char *) exp); + strcat ((char *) e, "%n"); ++#else /* UTF8 */ ++ wcscpy (e, exp); ++ wcscat (e, L"%n"); ++#endif /* UTF8 */ + exp = e; + + while (q) { + *((int *) sargs[n]) = 0; /* --> here was the problem - now fixed: good */ ++#ifndef UTF8 + if (n == sscanf ((char *) buf, (char *) exp, SCANF_ARGS)) { ++#else /* UTF8 */ ++ if (n == swscanf (buf, exp, SCANF_ARGS)) { ++#endif /* UTF8 */ + if (*((int *) sargs[n])) { + *len = *((int *) sargs[n]); ++#ifdef UTF8 ++ g_free(exp_backup); ++#endif /* UTF8 */ + return start; + } + } +- if (once_only) ++ if (once_only) { ++#ifdef UTF8 ++ g_free(exp_backup); ++#endif /* UTF8 */ + return -2; ++ } + if (q + start < last_byte) { + if (replace_case) { + buf[q] = (*get_byte) (data, q + start); +@@ -1544,7 +1708,11 @@ + start++; + buf++; /* move the window along */ + if (buf == mbuf + MAX_REPL_LEN) { /* the window is about to go past the end of array, so... */ ++#ifndef UTF8 + memmove (mbuf, buf, strlen ((char *) buf) + 1); /* reset it */ ++#else /* UTF8 */ ++ wmemmove (mbuf, buf, (wcslen (buf) + 1)); /* reset it */ ++#endif /* UTF8 */ + buf = mbuf; + } + q--; +@@ -1571,10 +1739,16 @@ + + buf = mbuf; + while (q) { ++#ifndef UTF8 + found_start = string_regexp_search ((char *) exp, (char *) buf, match_normal, match_bol, !replace_case, len, d); +- ++#else /* UTF8 */ ++ found_start = string_regexp_search ((char *) exp_mb, buf, match_normal, match_bol, !replace_case, len, d); ++#endif /* UTF8 */ + if (found_start <= -2) { /* regcomp/regexec error */ + *len = 0; ++#ifdef UTF8 ++ g_free (exp_backup); ++#endif /* UTF8 */ + return -3; + } + else if (found_start == -1) /* not found: try next line */ +@@ -1585,15 +1759,27 @@ + match_bol = 0; + continue; + } +- else /* found */ ++ else { /* found */ ++#ifdef UTF8 ++ g_free(exp_backup); ++#endif /* UTF8 */ + return (start + offset - q + found_start); ++ } + } +- if (once_only) ++ if (once_only) { ++#ifdef UTF8 ++ g_free(exp_backup); ++#endif /* UTF8 */ + return -2; ++ } + + if (buf[q - 1] != '\n') { /* incomplete line: try to recover */ + buf = mbuf + MAX_REPL_LEN / 2; ++#ifndef UTF8 + q = strlen ((const char *) buf); ++#else /* UTF8 */ ++ q = wcslen (buf); ++#endif /* UTF8 */ + memmove (mbuf, buf, q); + p = start + q; + move_win = 1; +@@ -1603,36 +1789,59 @@ + } + } + } else { ++#ifndef UTF8 + *len = strlen ((const char *) exp); ++#else /* UTF8 */ ++ *len = wcslen (exp); ++#endif /* UTF8 */ + if (replace_case) { + for (p = start; p <= last_byte - l; p++) { +- if ((*get_byte) (data, p) == (unsigned char)exp[0]) { /* check if first char matches */ ++ if ((*get_byte) (data, p) == exp[0]) { /* check if first char matches */ + for (f = 0, q = 0; q < l && f < 1; q++) +- if ((*get_byte) (data, q + p) != (unsigned char)exp[q]) ++ if ((*get_byte) (data, q + p) != exp[q]) + f = 1; +- if (f == 0) ++ if (f == 0) { ++#ifdef UTF8 ++ g_free (exp_backup); ++#endif /* UTF8 */ + return p; ++ } + } +- if (once_only) ++ if (once_only) { ++#ifdef UTF8 ++ g_free(exp_backup); ++#endif /* UTF8 */ + return -2; ++ } + } + } else { + for (p = 0; exp[p] != 0; p++) + exp[p] = my_lower_case (exp[p]); + + for (p = start; p <= last_byte - l; p++) { +- if (my_lower_case ((*get_byte) (data, p)) == (unsigned char)exp[0]) { ++ if (my_lower_case ((*get_byte) (data, p)) == exp[0]) { + for (f = 0, q = 0; q < l && f < 1; q++) +- if (my_lower_case ((*get_byte) (data, q + p)) != (unsigned char)exp[q]) ++ if (my_lower_case ((*get_byte) (data, q + p)) != exp[q]) + f = 1; +- if (f == 0) ++ if (f == 0) { ++#ifdef UTF8 ++ g_free (exp_backup); ++#endif /* UTF8 */ + return p; ++ } + } +- if (once_only) ++ if (once_only) { ++#ifdef UTF8 ++ g_free (exp_backup); ++#endif /* UTF8 */ + return -2; ++ } + } + } + } ++#ifdef UTF8 ++ g_free (exp_backup); ++#endif /* UTF8 */ + return -2; + } + +@@ -1646,9 +1855,14 @@ + + while ((p = edit_find_string (p, exp, len, last_byte, get_byte, data, once_only, d)) >= 0) { + if (replace_whole) { ++#ifndef UTF8 + /*If the bordering chars are not in option_whole_chars_search then word is whole */ + if (!strcasechr (option_whole_chars_search, (*get_byte) (data, p - 1)) + && !strcasechr (option_whole_chars_search, (*get_byte) (data, p + *len))) ++#else /* UTF8 */ ++ if (!iswalnum((*get_byte) (data, p - 1)) ++ && !iswalnum((*get_byte) (data, p + *len))) ++#endif /* UTF8 */ + return p; + if (once_only) + return -2; +@@ -1680,6 +1894,7 @@ + + #define is_digit(x) ((x) >= '0' && (x) <= '9') + ++#ifndef UTF8 + #define snprint(v) { \ + *p1++ = *p++; \ + *p1 = '\0'; \ +@@ -1687,33 +1902,48 @@ + if (n >= (size_t) (e - s)) goto nospc; \ + s += n; \ + } ++#else /* UTF8 */ ++#define snprint(v) { \ ++ *p1++ = *p++; \ ++ *p1 = '\0'; \ ++ n = swprintf(s, e-s, q1,v); \ ++ if (n >= (size_t) (e - s)) goto nospc; \ ++ s += n; \ ++ } ++#endif /* UTF8 */ + + /* this function uses the sprintf command to do a vprintf */ + /* it takes pointers to arguments instead of the arguments themselves */ + /* The return value is the number of bytes written excluding '\0' + if successfull, -1 if the resulting string would be too long and + -2 if the format string is errorneous. */ +-static int snprintf_p (char *str, size_t size, const char *fmt,...) +- __attribute__ ((format (printf, 3, 4))); +- +-static int snprintf_p (char *str, size_t size, const char *fmt,...) ++static int snprintf_p (mc_wchar_t *str, size_t size, const mc_wchar_t *fmt,...) + { + va_list ap; + size_t n; +- const char *q, *p; +- char *s = str, *e = str + size; +- char q1[40]; +- char *p1; ++ const mc_wchar_t *q, *p; ++ mc_wchar_t *s = str, *e = str + size; ++ mc_wchar_t q1[40]; ++ ++ mc_wchar_t *p1; + int nargs = 0; + + va_start (ap, fmt); + p = q = fmt; + ++#ifndef UTF8 + while ((p = strchr (p, '%'))) { ++#else /* UTF8 */ ++ while ((p = wcschr (p, L'%'))) { ++#endif /* UTF8 */ + n = p - q; + if (n >= (size_t) (e - s)) + goto nospc; ++#ifndef UTF8 + memcpy (s, q, n); /* copy stuff between format specifiers */ ++#else /* UTF8 */ ++ wmemcpy (s, q, n); /* copy stuff between format specifiers */ ++#endif /* UTF8 */ + s += n; + q = p; + p1 = q1; +@@ -1741,45 +1971,78 @@ + *p1++ = *p++; + if (*p == '*') { + p++; ++#ifndef UTF8 + strcpy (p1, MY_itoa (*va_arg (ap, int *))); /* replace field width with a number */ + p1 += strlen (p1); ++#else /* UTF8 */ ++ wcscpy (p1, MY_itoa (*va_arg (ap, int *))); /* replace field width with a number */ ++ p1 += wcslen (p1); ++#endif /* UTF8 */ + } else { +- while (is_digit (*p) && p1 < q1 + 20) ++#ifndef UTF8 ++ while (is_digit (*p) ++#else /* UTF8 */ ++ while (iswdigit (*p) ++#endif /* UTF8 */ ++ && p1 < q1 + 20) + *p1++ = *p++; +- if (is_digit (*p)) ++#ifndef UTF8 ++ if (is_digit (*p)) ++#else /* UTF8 */ ++ if (iswdigit (*p)) ++#endif /* UTF8 */ + goto err; + } + if (*p == '.') + *p1++ = *p++; + if (*p == '*') { + p++; ++#ifndef UTF8 + strcpy (p1, MY_itoa (*va_arg (ap, int *))); /* replace precision with a number */ + p1 += strlen (p1); ++#else /* UTF8 */ ++ wcscpy (p1, MY_itoa (*va_arg (ap, int *))); /* replace precision with a number */ ++ p1 += wcslen (p1); ++#endif /* UTF8 */ + } else { +- while (is_digit (*p) && p1 < q1 + 32) ++#ifndef UTF8 ++ while (is_digit (*p) ++#else /* UTF8 */ ++ while (iswdigit (*p) ++#endif /* UTF8 */ ++ && p1 < q1 + 32) + *p1++ = *p++; +- if (is_digit (*p)) ++#ifndef UTF8 ++ if (is_digit (*p)) ++#else /* UTF8 */ ++ if (iswdigit (*p)) ++#endif /* UTF8 */ + goto err; + } + /* flags done, now get argument */ + if (*p == 's') { ++#ifndef UTF8 + snprint (va_arg (ap, char *)); ++#else /* UTF8 */ ++ *p1++ = 'l'; ++ snprint (va_arg (ap, mc_wchar_t *)); ++#endif /* UTF8 */ + } else if (*p == 'h') { +- if (strchr ("diouxX", *p)) ++ if (*p < 128 && strchr ("diouxX", *p)) + snprint (*va_arg (ap, short *)); + } else if (*p == 'l') { + *p1++ = *p++; +- if (strchr ("diouxX", *p)) ++ if (*p < 128 && strchr ("diouxX", *p)) + snprint (*va_arg (ap, long *)); +- } else if (strchr ("cdiouxX", *p)) { ++ } else if (*p < 128 && strchr ("cdiouxX", *p)) { + snprint (*va_arg (ap, int *)); + } else if (*p == 'L') { + *p1++ = *p++; +- if (strchr ("EefgG", *p)) ++ if (*p < 128 && strchr ("EefgG", *p)) + snprint (*va_arg (ap, double *)); /* should be long double */ +- } else if (strchr ("EefgG", *p)) { ++ } else if (*p < 128 && strchr ("EefgG", *p)) { + snprint (*va_arg (ap, double *)); +- } else if (strchr ("DOU", *p)) { ++ } else if (*p < 128 && strchr ("DOU", *p)) { + snprint (*va_arg (ap, long *)); + } else if (*p == 'p') { + snprint (*va_arg (ap, void **)); +@@ -1788,10 +2051,17 @@ + q = p; + } + va_end (ap); ++#ifndef UTF8 + n = strlen (q); + if (n >= (size_t) (e - s)) + return -1; + memcpy (s, q, n + 1); ++#else /* UTF8 */ ++ n = wcslen (q); ++ if (n >= (size_t) (e - s)) ++ return -1; ++ wmemcpy (s, q, n + 1); ++#endif /* UTF8 */ + return s + n - str; + nospc: + va_end (ap); +@@ -1970,8 +2240,11 @@ + } + } + if (replace_yes) { /* delete then insert new */ ++#ifdef UTF8 ++ mc_wchar_t *winput2 = mbstr_to_wchar(input2); ++#endif /* UTF8 */ + if (replace_scanf) { +- char repl_str[MAX_REPL_LEN + 2]; ++ mc_wchar_t repl_str[MAX_REPL_LEN + 2]; + int ret = 0; + + /* we need to fill in sargs just like with scanf */ +@@ -1980,17 +2253,25 @@ + for (k = 1; + k < NUM_REPL_ARGS && pmatch[k].rm_eo >= 0; + k++) { ++#ifndef UTF8 + unsigned char *t; ++#else /* UTF8 */ ++ mc_wchar_t *t; ++#endif + + if (pmatch[k].rm_eo - pmatch[k].rm_so > 255) { + ret = -1; + break; + } ++#ifndef UTF8 + t = (unsigned char *) &sargs[k - 1][0]; ++#else /* UTF8 */ ++ t = (mc_wchar_t *) &sargs[k - 1][0]; ++#endif /* UTF8 */ + for (j = 0; + j < pmatch[k].rm_eo - pmatch[k].rm_so + && j < 255; j++, t++) +- *t = (unsigned char) edit_get_byte (edit, ++ *t = edit_get_byte (edit, + edit-> + search_start + - +@@ -2008,14 +2289,23 @@ + } + if (!ret) + ret = ++#ifndef UTF8 + snprintf_p (repl_str, MAX_REPL_LEN + 2, input2, ++#else /* UTF8 */ ++ snprintf_p (repl_str, MAX_REPL_LEN + 2, winput2, ++#endif /* UTF8 */ + PRINTF_ARGS); + if (ret >= 0) { + times_replaced++; + while (i--) + edit_delete (edit); ++#ifndef UTF8 + while (repl_str[++i]) + edit_insert (edit, repl_str[i]); ++#else /* UTF8 */ ++ while (winput2[++i]) ++ edit_insert (edit, winput2[i]); ++#endif /* UTF8 */ + } else { + edit_error_dialog (_(" Replace "), + ret == +@@ -2029,10 +2319,18 @@ + times_replaced++; + while (i--) + edit_delete (edit); ++#ifndef UTF8 + while (input2[++i]) + edit_insert (edit, input2[i]); ++#else /* UTF8 */ ++ while (winput2[++i]) ++ edit_insert (edit, winput2[i]); ++#endif /* UTF8 */ + } + edit->found_len = i; ++#ifdef UTF8 ++ g_free (winput2); ++#endif /* UTF8 */ + } + /* so that we don't find the same string again */ + if (replace_backwards) { +@@ -2205,16 +2503,17 @@ + #define TEMP_BUF_LEN 1024 + + /* Return a null terminated length of text. Result must be g_free'd */ +-static unsigned char * ++static mc_wchar_t * + edit_get_block (WEdit *edit, long start, long finish, int *l) + { +- unsigned char *s, *r; +- r = s = g_malloc (finish - start + 1); ++ mc_wchar_t *s, *r; ++ r = s = g_malloc ((finish - start + 1) * sizeof(mc_wchar_t)); + if (column_highlighting) { + *l = 0; + /* copy from buffer, excluding chars that are out of the column 'margins' */ + while (start < finish) { +- int c, x; ++ mc_wchar_t c; ++ int x; + x = edit_move_forward3 (edit, edit_bol (edit, start), 0, + start); + c = edit_get_byte (edit, start); +@@ -2247,11 +2546,15 @@ + return 0; + + if (column_highlighting) { +- unsigned char *block, *p; ++ mc_wchar_t *block, *p; + int r; + p = block = edit_get_block (edit, start, finish, &len); + while (len) { ++#ifndef UTF8 + r = mc_write (file, p, len); ++#else /* UTF8 */ ++ r = wchar_write (file, p, len); ++#endif /* UTF8 */ + if (r < 0) + break; + p += r; +@@ -2259,15 +2562,19 @@ + } + g_free (block); + } else { +- unsigned char *buf; ++ mc_wchar_t *buf; + int i = start, end; + len = finish - start; +- buf = g_malloc (TEMP_BUF_LEN); ++ buf = g_malloc (TEMP_BUF_LEN * sizeof(mc_wchar_t)); + while (start != finish) { + end = min (finish, start + TEMP_BUF_LEN); + for (; i < end; i++) + buf[i - start] = edit_get_byte (edit, i); ++#ifndef UTF8 + len -= mc_write (file, (char *) buf, end - start); ++#else /* UTF8 */ ++ len -= wchar_write (file, buf, end - start); ++#endif /* UTF8 */ + start = end; + } + g_free (buf); +@@ -2609,17 +2916,20 @@ + + /* prints at the cursor */ + /* returns the number of chars printed */ ++#ifndef UTF8 + int edit_print_string (WEdit * e, const char *s) ++#else /* UTF8 */ ++int edit_print_wstring (WEdit * e, mc_wchar_t *s) ++#endif /* UTF8 */ + { + int i = 0; + while (s[i]) +- edit_execute_cmd (e, -1, (unsigned char) s[i++]); ++ edit_execute_cmd (e, -1, s[i++]); + e->force |= REDRAW_COMPLETELY; + edit_update_screen (e); + return i; + } + +- + static void pipe_mail (WEdit *edit, char *to, char *subject, char *cc) + { + FILE *p = 0; +@@ -2713,15 +3023,20 @@ + /* find first character of current word */ + static int edit_find_word_start (WEdit *edit, long *word_start, int *word_len) + { +- int i, c, last; ++ int i; ++ mc_wint_t c, last; + + /* return if at begin of file */ + if (edit->curs1 <= 0) + return 0; + +- c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1); ++ c = edit_get_byte (edit, edit->curs1 - 1); + /* return if not at end or in word */ ++#ifndef UTF8 + if (isspace (c) || !(isalnum (c) || c == '_')) ++#else /* UTF8 */ ++ if (iswspace (c) || !(iswalnum (c) || c == '_')) ++#endif /* UTF8 */ + return 0; + + /* search start of word to be completed */ +@@ -2731,11 +3046,19 @@ + return 0; + + last = c; +- c = (unsigned char) edit_get_byte (edit,