Convert to LF during copy paste

This commit is contained in:
Krzosa Karol
2024-08-09 07:42:31 +02:00
parent a7dc343fd6
commit 93530bd089
3 changed files with 46 additions and 38 deletions

View File

@@ -42,8 +42,12 @@ void Command_Paste(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
const char *text = SDL_GetClipboardText();
defer { SDL_free((void *)text); };
String string_ = text;
String16 string = ToString16(scratch, string_);
String string_ = text;
Int cap = string_.len * 3;
wchar_t *string16_buffer = AllocArray(scratch, wchar_t, cap);
Int len = ConvertUTF8ToUTF16UnixLine(string_, string16_buffer, cap);
String16 string = {string16_buffer, len};
// Regular paste
if (string != SavedClipboardString || SavedClipboardCursors.len != view->carets.len) {

View File

@@ -196,6 +196,43 @@ String GetCurrentBufferDir() {
return name;
}
Int ConvertUTF8ToUTF16UnixLine(String string, wchar_t *buffer, Int buffer_cap) {
Int buffer_len = 0;
Assert(buffer_cap > string.len * 2);
for (Int i = 0; i < string.len;) {
if (string.data[i] == '\r') {
i += 1;
continue;
}
if (string.data[i] == '\t') {
// @WARNING: DONT INCREASE THE SIZE CARELESSLY, WE NEED TO ADJUST BUFFER SIZE
for (Int i = 0; i < 4; i += 1) buffer[buffer_len++] = L' ';
i += 1;
continue;
}
uint32_t u32 = '?';
UTF32Result decode = UTF8ToUTF32(string.data + i, (int64_t)(string.len - i));
if (!decode.error) {
i += decode.advance;
u32 = decode.out_str;
} else {
i += 1;
}
UTF16Result encode = UTF32ToUTF16(u32);
if (!encode.error) {
for (int64_t encode_i = 0; encode_i < encode.len; encode_i += 1) {
buffer[buffer_len++] = encode.out_str[encode_i];
Assert(buffer_len < buffer_cap);
}
} else {
buffer[buffer_len++] = L'?';
}
}
return buffer_len;
}
// This function as name suggests tries to open a buffer,
// there is no name resolution here, path should already be resolved etc.
//
@@ -231,38 +268,7 @@ Buffer *BufferOpenFile(String path) {
path = Intern(&GlobalInternTable, path);
String string = ReadFile(scratch, path);
buffer = CreateBuffer(sys_allocator, path, string.len * 4);
for (Int i = 0; i < string.len;) {
if (string.data[i] == '\r') {
i += 1;
continue;
}
if (string.data[i] == '\t') {
// @WARNING: DONT INCREASE THE SIZE CARELESSLY, WE NEED TO ADJUST BUFFER SIZE
for (Int i = 0; i < 4; i += 1) buffer->data[buffer->len++] = L' ';
i += 1;
continue;
}
uint32_t u32 = '?';
UTF32Result decode = UTF8ToUTF32(string.data + i, (int64_t)(string.len - i));
if (!decode.error) {
i += decode.advance;
u32 = decode.out_str;
} else {
i += 1;
}
UTF16Result encode = UTF32ToUTF16(u32);
if (!encode.error) {
for (int64_t encode_i = 0; encode_i < encode.len; encode_i += 1) {
buffer->data[buffer->len++] = encode.out_str[encode_i];
Assert(buffer->len < buffer->cap);
}
} else {
buffer->data[buffer->len++] = L'?';
}
}
buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap);
UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len});
}
return buffer;

View File

@@ -1,18 +1,16 @@
- Exec word should try to match lua function calls (first match word, then expand selection right)
- Remove pointers and use ViewIDs (enable array debug while doing this)
- try using git grep for search for now, combine with fuzzy search buffer
- need to rewrite the path matching in lua
- I think clipboard adds \r\n, let's execute the conversion on that
- prevent lua from infinite looping
- Append to console and change console directory
- hotkey to select window title
- the 'invisible when inactive' flag does it apply to title bar windows??
- win32: change all stack buffers to arena
- search as a command to execute which is going to be in the title bar
- search backwards
- braces enclose should be performed even if we put the mouse out of bounds!
- select until
- some split selection commands
- assign commands or lua functions to F1-F12 keys
- word complete
- Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?)