|
1 | 1 | local M = {} |
2 | 2 |
|
3 | | -function M.get_selection() |
4 | | - local start_line, start_col = unpack(vim.fn.getpos("'<")[2]) |
5 | | - local end_line, end_col = unpack(vim.fn.getpos("'>")[2]) |
6 | | - |
7 | | - -- selection is empty |
8 | | - if start_line == end_line and start_col == end_col then |
9 | | - return nil |
| 3 | +local function is_visual() |
| 4 | + if vim.fn.mode() ~= "v" and vim.fn.mode() ~= "V" and vim.fn.mode() ~= "<C-V>" then |
| 5 | + return false |
10 | 6 | end |
11 | 7 |
|
12 | | - -- selection is not empty |
| 8 | + return true |
| 9 | +end |
| 10 | + |
| 11 | +function M.get_current_selection(start_line, end_line) |
13 | 12 | local bufnr = vim.api.nvim_get_current_buf() |
14 | 13 |
|
15 | | - start_line = start_line - 1 -- convert to 0-based line number |
16 | | - end_line = end_line - 1 -- convert to 0-based line number |
| 14 | + start_line = start_line - 1 -- Convert to 0-based line number |
| 15 | + end_line = end_line - 1 -- Convert to 0-based line number |
17 | 16 |
|
18 | | - local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line, false) |
| 17 | + local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line + 1, false) |
19 | 18 |
|
20 | 19 | return table.concat(lines, "\n") |
21 | 20 | end |
22 | 21 |
|
| 22 | +function M.get_last_selection() |
| 23 | + local bufnr = vim.api.nvim_get_current_buf() |
| 24 | + |
| 25 | + -- Save the current cursor position |
| 26 | + local saved_cursor = vim.api.nvim_win_get_cursor(0) |
| 27 | + |
| 28 | + -- Get the start and end positions of the visual selection |
| 29 | + vim.cmd("normal! gv") |
| 30 | + local start_pos = vim.fn.getpos("'<") |
| 31 | + local end_pos = vim.fn.getpos("'>") |
| 32 | + |
| 33 | + -- Restore the cursor position |
| 34 | + vim.api.nvim_win_set_cursor(0, saved_cursor) |
| 35 | + |
| 36 | + local start_line = start_pos[2] - 1 |
| 37 | + local end_line = end_pos[2] |
| 38 | + local content_lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line, false) |
| 39 | + local content = table.concat(content_lines, "\n") |
| 40 | + |
| 41 | + return content |
| 42 | +end |
| 43 | + |
| 44 | +local function read_file(path) |
| 45 | + local file = io.open(path, "rb") -- r read mode and b binary mode |
| 46 | + if not file then |
| 47 | + return nil |
| 48 | + end |
| 49 | + local content = file:read("*a") -- *a or *all reads the whole file |
| 50 | + file:close() |
| 51 | + return content |
| 52 | +end |
| 53 | + |
| 54 | +function M.exec(cmd, stdin) |
| 55 | + print(string.format("Executing: %s", cmd)) |
| 56 | + local tmp = os.tmpname() |
| 57 | + |
| 58 | + local pipe = io.popen(cmd .. "> " .. tmp, "w") |
| 59 | + |
| 60 | + if not pipe then |
| 61 | + return nil |
| 62 | + end |
| 63 | + |
| 64 | + if stdin then |
| 65 | + pipe:write(stdin) |
| 66 | + end |
| 67 | + |
| 68 | + pipe:close() |
| 69 | + |
| 70 | + local output = read_file(tmp) |
| 71 | + os.remove(tmp) |
| 72 | + |
| 73 | + return output |
| 74 | +end |
| 75 | + |
| 76 | +function M.extract_gist_url(output) |
| 77 | + local pattern = "https://gist.github.com/%S+" |
| 78 | + |
| 79 | + return output:match(pattern) |
| 80 | +end |
| 81 | + |
23 | 82 | return M |
0 commit comments