-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(nvim) Define utility function
switch_to_hangeul
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
local M = {} | ||
|
||
local function generate_command(line, language) | ||
local inko_option = '' | ||
|
||
if language == 'korean' then | ||
inko_option = 'en2ko' | ||
end | ||
|
||
local sanitized_line = '' | ||
local last_pos = #line | ||
if line:sub(last_pos, last_pos) == "\n" then | ||
sanitized_line = line:sub(1, last_pos - 1) | ||
else | ||
sanitized_line = line | ||
end | ||
|
||
local command = "python -c \"" | ||
command = command .. "from inko import Inko;" | ||
command = command .. "converter = Inko();" | ||
command = command .. "print(converter." .. inko_option .. "('" .. sanitized_line .. "'), end='')" | ||
command = command .. "\"" | ||
|
||
return command | ||
end | ||
|
||
function M.switch_to_hangeul() | ||
local buffer = vim.api.nvim_get_current_buf() | ||
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) | ||
|
||
local start_line = vim.fn.line("'<") | ||
local end_line = vim.fn.line("'>") | ||
|
||
for line = start_line, end_line do | ||
local command = generate_command(lines[line], "korean") | ||
local handle = io.popen(command) | ||
local result = handle:read("a*") | ||
handle:close() | ||
if #lines[line] >= 1 then | ||
vim.api.nvim_buf_set_text(0, line - 1, 0, line - 1, #lines[line], {result}) | ||
end | ||
end | ||
end | ||
|
||
return M |