Skip to content

Commit

Permalink
Merge pull request #128 from bprb/geometry
Browse files Browse the repository at this point in the history
Fix: --geometry should take &guifont into account
  • Loading branch information
RMichelsen authored Sep 27, 2024
2 parents cfcea51 + f0da7e2 commit 3e04fc8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 99 deletions.
43 changes: 18 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
struct Context {
bool start_maximized;
bool start_fullscreen;
bool disable_fullscreen;
int64_t start_rows;
int64_t start_cols;
bool disable_fullscreen;
HWND hwnd;
Nvim *nvim;
Renderer *renderer;
Expand Down Expand Up @@ -51,23 +53,21 @@ void ProcessMPackMessage(Context *context, mpack_tree_t *tree) {
case MPackMessageType::Response: {
assert(result.response.msg_id <= context->nvim->next_msg_id);
switch (context->nvim->msg_id_to_method[result.response.msg_id]) {
case NvimRequest::nvim_eval: {
case NvimRequest::nvim_get_option_value: {
Vec<char> guifont_buffer;
NvimParseConfig(context->nvim, result.params, &guifont_buffer);
NvimParseOptionValueStr(context->nvim, result.params, &guifont_buffer);
if (!guifont_buffer.empty()) {
bool updated = RendererUpdateGuiFont(context->renderer, guifont_buffer.data(), strlen(guifont_buffer.data()));
if(!updated) {
guifont_buffer[guifont_buffer.size() - 1] = '"';
guifont_buffer.push_back('\0');
const char *error = "echom \"Unknown font: ";
char *command = static_cast<char *>(malloc(strlen(error) + guifont_buffer.size()));
memcpy(command, error, strlen(error));
memcpy(command + strlen(error), guifont_buffer.data(), guifont_buffer.size());
NvimSendCommand(context->nvim, command);
free(command);
RendererUpdateGuiFont(context->renderer, guifont_buffer.data(), strlen(guifont_buffer.data()));

if (context->start_rows != 0 && context->start_cols != 0) {
// after user config is read, process --geometry resize for the current font.
// if user config also sets lines or columns, --geometry takes precedence.
PixelSize start_size = RendererGridToPixelSize(context->renderer, context->start_rows, context->start_cols);
SetWindowPos(context->hwnd, HWND_TOP, 0, 0,
start_size.width, start_size.height, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
} break;
} break;
case NvimRequest::vim_get_api_info:
case NvimRequest::nvim_input:
case NvimRequest::nvim_input_mouse:
Expand All @@ -85,7 +85,7 @@ void ProcessMPackMessage(Context *context, mpack_tree_t *tree) {
// nvim has read user init file, we can now request info if we want
// like additional startup settings or something else
NvimSendResponse(context->nvim, result.request.msg_id);
NvimQueryConfig(context->nvim);
NvimGetOptionValue(context->nvim, "guifont");
}
} break;
}
Expand Down Expand Up @@ -511,6 +511,8 @@ int WINAPI wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, _
Context context {
.start_maximized = start_maximized,
.start_fullscreen = start_fullscreen,
.start_rows = start_rows,
.start_cols = start_cols,
.disable_fullscreen = disable_fullscreen,
.nvim = &nvim,
.renderer = &renderer,
Expand Down Expand Up @@ -552,19 +554,10 @@ int WINAPI wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, _
// Forceably update the window to prevent any frames where the window is blank. Windows API docs
// specify that SetWindowPos should be called with these arguments after SetWindowLong is called.
UINT window_flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED;
int window_w = 0, window_h = 0;

if (start_rows != 0 && start_cols != 0) {
PixelSize start_size = RendererGridToPixelSize(context.renderer,
start_rows, start_cols);
window_w = start_size.width;
window_h = start_size.height;
window_flags = window_flags & ~SWP_NOSIZE;
}
if (start_pos_x != CW_USEDEFAULT || start_pos_y != CW_USEDEFAULT) {
window_flags = window_flags & ~SWP_NOMOVE;
}
SetWindowPos(hwnd, HWND_TOP, start_pos_x, start_pos_y, window_w, window_h, window_flags);
SetWindowPos(hwnd, HWND_TOP, start_pos_x, start_pos_y, 0, 0, window_flags);

if (start_fullscreen) {
ToggleFullscreen(context.hwnd, &context);
Expand Down
82 changes: 14 additions & 68 deletions src/nvim/nvim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,83 +526,29 @@ bool NvimProcessKeyDown(Nvim *nvim, int virtual_key) {
return true;
}

void NvimQueryConfig(Nvim *nvim) {
void NvimGetOptionValue(Nvim *nvim, const char *option) {
char data[MAX_MPACK_OUTBOUND_MESSAGE_SIZE];
mpack_writer_t writer;
mpack_writer_init(&writer, data, MAX_MPACK_OUTBOUND_MESSAGE_SIZE);
MPackStartRequest(RegisterRequest(nvim, nvim_eval), NVIM_REQUEST_NAMES[nvim_eval], &writer);
mpack_start_array(&writer, 1);
mpack_write_cstr(&writer, "stdpath('config')");
MPackStartRequest(RegisterRequest(nvim, nvim_get_option_value), NVIM_REQUEST_NAMES[nvim_get_option_value], &writer);
mpack_start_array(&writer, 2);
mpack_write_cstr(&writer, option);
mpack_start_map(&writer, 0);
mpack_finish_map(&writer);
mpack_finish_array(&writer);
size_t size = MPackFinishMessage(&writer);
size_t size = MPackFinishMessage(&writer);
MPackSendData(nvim->stdin_write, data, size);
}

void NvimParseConfig(Nvim *nvim, mpack_node_t config_node, Vec<char> *guifont_out) {
void NvimParseOptionValueStr(Nvim *nvim, mpack_node_t value_node, Vec<char> *value_out) {
char path[MAX_PATH];
const char *config_path = mpack_node_str(config_node);
size_t config_path_strlen = mpack_node_strlen(config_node);
strncpy_s(path, MAX_PATH, config_path, config_path_strlen);
strcat_s(path, MAX_PATH - config_path_strlen - 1, "\\init.vim");

HANDLE config_file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (config_file == INVALID_HANDLE_VALUE) {
return;
const char *value_path = mpack_node_str(value_node);
size_t value_path_strlen = mpack_node_strlen(value_node);
if (value_path && value_path_strlen)
{
value_out->resize(value_path_strlen);
memcpy(value_out->data(), value_path, value_path_strlen);
}

char *buffer;
LARGE_INTEGER file_size;
if (!GetFileSizeEx(config_file, &file_size)) {
CloseHandle(config_file);
return;
}
buffer = static_cast<char *>(malloc(file_size.QuadPart));

DWORD bytes_read;
if (!ReadFile(config_file, buffer, file_size.QuadPart, &bytes_read, NULL)) {
CloseHandle(config_file);
free(buffer);
return;
}
CloseHandle(config_file);

char *strtok_context;
char *line = strtok_s(buffer, "\r\n", &strtok_context);
while (line) {
char *guifont = strstr(line, "set guifont=");
if (guifont) {
// Check if we're inside a comment
int leading_count = guifont - line;
bool inside_comment = false;
for (int i = 0; i < leading_count; ++i) {
if (line[i] == '"') {
inside_comment = !inside_comment;
}
}
if (!inside_comment) {
guifont_out->clear();

int line_offset = (guifont - line + strlen("set guifont="));
int guifont_strlen = strlen(line) - line_offset;
int escapes = 0;
for (int i = 0; i < guifont_strlen; ++i) {
if (line[line_offset + i] == '\\' && i < (guifont_strlen - 1) && line[line_offset + i + 1] == ' ') {
guifont_out->push_back(' ');
++i;
continue;
}
guifont_out->push_back(line[i + line_offset]);

}
guifont_out->push_back('\0');
}
}
line = strtok_s(NULL, "\r\n", &strtok_context);
}

free(buffer);
}

void NvimSendCommand(Nvim *nvim, const char *command) {
Expand Down
12 changes: 6 additions & 6 deletions src/nvim/nvim.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ enum NvimRequest : uint8_t {
vim_get_api_info = 0,
nvim_input = 1,
nvim_input_mouse = 2,
nvim_eval = 3,
nvim_command = 4
nvim_command = 3,
nvim_get_option_value = 4
};
constexpr const char *NVIM_REQUEST_NAMES[] {
"nvim_get_api_info",
"nvim_input",
"nvim_input_mouse",
"nvim_eval",
"nvim_command"
"nvim_command",
"nvim_get_option_value"
};
enum NvimOutboundNotification : uint8_t {
nvim_ui_attach = 0,
Expand Down Expand Up @@ -56,8 +56,8 @@ struct Nvim {
void NvimInitialize(Nvim *nvim, wchar_t *command_line, HWND hwnd);
void NvimShutdown(Nvim *nvim);

void NvimQueryConfig(Nvim *nvim);
void NvimParseConfig(Nvim *nvim, mpack_node_t config_node, Vec<char> *guifont_out);
void NvimGetOptionValue(Nvim *nvim, const char *option);
void NvimParseOptionValueStr(Nvim *nvim, mpack_node_t value_node, Vec<char> *value_out);

void NvimSendCommand(Nvim *nvim, const char *command);
void NvimSendUIAttach(Nvim *nvim, int grid_rows, int grid_cols);
Expand Down

0 comments on commit 3e04fc8

Please sign in to comment.