Skip to content

Commit

Permalink
Fixed UTF encoding when inserting snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ffes committed Jun 21, 2013
1 parent 183832a commit 7681e5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 19 additions & 2 deletions DlgConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ static void OnClose(HWND hWnd)
EndDialog(hWnd, 0);
}

/////////////////////////////////////////////////////////////////////////////
// Convert a wide Unicode string to an UTF8 string

static std::string utf8_encode(const std::wstring &wstr)
{
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}

/////////////////////////////////////////////////////////////////////////////
// Insert the snippet (selected from the context menu item) into the text
// in the Scintilla window. Double clicking get redirected to here as well.
Expand Down Expand Up @@ -547,7 +558,9 @@ static void OnSnippetInsert(HWND hWnd)
}

// Insert the first part of the snippet
SendMsg(SCI_REPLACESEL, 0, (LPARAM) snip.GetBeforeSelection());
std::wstring wstr = snip.WGetBeforeSelection();
std::string strTo = utf8_encode(wstr);
SendMsg(SCI_REPLACESEL, 0, (LPARAM) strTo.c_str());

int beforeSel = (int) SendMsg(SCI_GETCURRENTPOS);

Expand All @@ -566,7 +579,11 @@ static void OnSnippetInsert(HWND hWnd)
{
size_t len = wcslen(snip.WGetAfterSelection());
if (len > 0)
SendMsg(SCI_ADDTEXT, len, (LPARAM) snip.GetAfterSelection());
{
std::wstring wstr = snip.WGetAfterSelection();
std::string strTo = utf8_encode(wstr);
SendMsg(SCI_REPLACESEL, 0, (LPARAM) strTo.c_str());
}
}

// Restore the cursor position and selection
Expand Down
5 changes: 3 additions & 2 deletions Snippets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ void Snippet::SetBeforeSelection(LPCSTR txt)
WSetBeforeSelection(NULL);
return;
}
size_t size = sizeof(WCHAR) * (len + 3);

// Convert from UTF-8 to WCHAR and store
size_t size = MultiByteToWideChar(CP_UTF8, 0, txt, (int) len, NULL, 0);
WCHAR* wBuffer = (WCHAR*) malloc(size);
ZeroMemory(wBuffer, size);
Ansi2Unicode(wBuffer, txt, len);
MultiByteToWideChar(CP_UTF8, 0, txt, (int) len, wBuffer, size);
WSetBeforeSelection(wBuffer);
free(wBuffer);
}
Expand Down

0 comments on commit 7681e5d

Please sign in to comment.