|
1 | | -local pickers, telescope_actions, conf |
2 | | -if pcall(require, "telescope") then |
3 | | - pickers = require("telescope.pickers") |
4 | | - telescope_actions = require("telescope.actions") |
5 | | - conf = require("telescope.config").values |
6 | | -else |
7 | | - error("Cannot find telescope!") |
8 | | -end |
9 | | -local status_ok, _ = pcall(require, "toggleterm") |
10 | | -if not status_ok then |
11 | | - error("Cannot find toggleterm!") |
12 | | -end |
| 1 | +local pickers = require("telescope.pickers") |
| 2 | +local telescope_actions = require("telescope.actions") |
| 3 | +local conf = require("telescope.config").values |
13 | 4 | local util = require("util") |
14 | 5 |
|
| 6 | +--- Create autocommand to enter insert mode when the cursor leaves the telescope buffer. |
| 7 | +--- Useful for actions that are called with exit_on_action set to false b/c it allows the user |
| 8 | +--- to manually exit telescope but still automatically enter insert mode in the terminal buffer |
| 9 | +--- @param picker table The telescope picker object. |
| 10 | +local function telescope_leave_autocmd(picker) |
| 11 | + vim.api.nvim_create_augroup("InsertOnPickerLeave", {}) |
| 12 | + vim.api.nvim_create_autocmd("BufLeave", { |
| 13 | + buffer = picker.prompt_bufnr, |
| 14 | + group = "InsertOnPickerLeave", |
| 15 | + nested = true, |
| 16 | + once = true, |
| 17 | + callback = function() |
| 18 | + local desktopPath = os.getenv("HOME") .. "/Desktop/new.txt" |
| 19 | + local file, err = io.open(desktopPath, "a") |
| 20 | + if not file then |
| 21 | + print("Error opening file:", err) |
| 22 | + return |
| 23 | + end |
| 24 | + file:write("leave" .. "\n") |
| 25 | + |
| 26 | + local win_is_valid = vim.api.nvim_win_is_valid(picker.original_win_id) |
| 27 | + file:write("picker.original_win_id: " .. picker.original_win_id .. "\n") |
| 28 | + if win_is_valid then |
| 29 | + file:write("win_is_valid" .. "\n") |
| 30 | + local picker_orig_win_bufnr = vim.fn.winbufnr(picker.original_win_id) |
| 31 | + local buftype = vim.api.nvim_buf_get_option(picker_orig_win_bufnr, "filetype") |
| 32 | + file:write("buftype: " .. buftype .. "\n") |
| 33 | + if buftype == "toggleterm" then |
| 34 | + util.start_insert_mode() |
| 35 | + end |
| 36 | + end |
| 37 | + file:write("\n") |
| 38 | + file:close() |
| 39 | + end, |
| 40 | + }) |
| 41 | +end |
| 42 | + |
15 | 43 | local M = {} |
| 44 | + |
| 45 | +--- Entry point. Opens a telescope picker. |
| 46 | +--- @param opts table The options for the picker. |
16 | 47 | M.open = function(opts) |
17 | 48 | local config = require("config").options |
18 | 49 | -- set origin window, which will need to be retrieved in some actions (actions/init.lua) |
@@ -43,39 +74,6 @@ M.open = function(opts) |
43 | 74 | end, |
44 | 75 | }) |
45 | 76 | picker:find() |
46 | | - |
47 | | - -- create autocommand to enter insert mode when the cursor leaves the telescope buffer |
48 | | - -- useful for actions that are called with exit_on_action set to false b/c it allows the user |
49 | | - -- to manually exit telescope but still automatically enter insert mode in the terminal buffer |
50 | | - vim.api.nvim_create_augroup("InsertOnPickerLeave", {}) |
51 | | - vim.api.nvim_create_autocmd("BufLeave", { |
52 | | - buffer = picker.prompt_bufnr, |
53 | | - group = "InsertOnPickerLeave", |
54 | | - nested = true, |
55 | | - once = true, |
56 | | - callback = function() |
57 | | - local desktopPath = os.getenv("HOME") .. "/Desktop/new.txt" |
58 | | - local file, err = io.open(desktopPath, "a") |
59 | | - if not file then |
60 | | - print("Error opening file:", err) |
61 | | - return |
62 | | - end |
63 | | - file:write("leave" .. "\n") |
64 | | - |
65 | | - local win_is_valid = vim.api.nvim_win_is_valid(picker.original_win_id) |
66 | | - file:write("picker.original_win_id: " .. picker.original_win_id .. "\n") |
67 | | - if win_is_valid then |
68 | | - file:write("win_is_valid" .. "\n") |
69 | | - local picker_orig_win_bufnr = vim.fn.winbufnr(picker.original_win_id) |
70 | | - local buftype = vim.api.nvim_buf_get_option(picker_orig_win_bufnr, "filetype") |
71 | | - file:write("buftype: " .. buftype .. "\n") |
72 | | - if buftype == "toggleterm" then |
73 | | - util.start_insert_mode() |
74 | | - end |
75 | | - end |
76 | | - file:write("\n") |
77 | | - file:close() |
78 | | - end, |
79 | | - }) |
| 77 | + telescope_leave_autocmd(picker) |
80 | 78 | end |
81 | 79 | return M |
0 commit comments