summaryrefslogtreecommitdiff
path: root/lua/telescope/mappings.lua
blob: 38c664a0b8a7e5aab3686789cd29457032207b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
-- TODO: Customize keymap
local a = vim.api

local actions = require('telescope.actions')
local state = require('telescope.state')

local mappings = {}
local keymap = {}

local keymap_store = {}

--[[
mappings.apply_keymap(42, {
  normal = {
    ["<leader>x"] = "just do this string",

    ["<CR>"] = function(picker, prompt_bufnr)
      actions.close_prompt()

      local filename = ...
      vim.cmd(string.format(":e %s", filename))
    end,
  },
  insert = {
  }
})
--]]
mappings.apply_keymap = function(prompt_bufnr, buffer_keymap)

  vim.cmd(string.format(
    [[autocmd BufDelete %s :lua require('telescope.mappings').clear(%s)]],
    prompt_bufnr,
    prompt_bufnr
  ))
end

mappings.clear = function(prompt_bufnr)
  keymap_store[prompt_bufnr] = nil
end

mappings.set_keymap = function(prompt_bufnr, results_bufnr)
  local function default_mapper(mode, map_key, table_key)
    a.nvim_buf_set_keymap(
      prompt_bufnr,
      mode,
      map_key,
      string.format(
        [[<C-O>:lua __TelescopeMapping(%s, %s, '%s')<CR>]],
        prompt_bufnr,
        results_bufnr,
        table_key
        ),
      {
        silent = true,
      }
    )
  end

  default_mapper('i', '<c-n>', 'next_selection')
  default_mapper('i', '<c-p>', 'previous_selection')
  default_mapper('i', '<CR>', 'enter')

  default_mapper('n', '<esc>', 'escape')
end


function __TelescopeMapping(prompt_bufnr, results_bufnr, characters)
  if keymap[characters] then
    keymap[characters](prompt_bufnr, results_bufnr)
  end
end

-- TODO: Refactor this to use shared code.
-- TODO: Move from top to bottom, etc.
-- TODO: It seems like doing this brings us back to the beginning of the prompt, which is not great.
keymap["next_selection"] = function(prompt_bufnr, _)
  actions.shift_current_selection(prompt_bufnr, 1)
end

keymap["previous_selection"] = function(prompt_bufnr, _)
  actions.shift_current_selection(prompt_bufnr, -1)
end

keymap["enter"] = function(prompt_bufnr, results_bufnr)
  local picker = actions.get_current_picker(prompt_bufnr)
  local entry = actions.get_selected_entry(prompt_bufnr)

  if not entry then
    print("[telescope] Nothing currently selected")
    return
  else
    local value = entry.value
    if not value then
      print("Could not do anything with blank line...")
      return
    end

    -- TODO: This is not great.
    if type(value) == "table" then
      value = entry.display
    end

    local sections = vim.split(value, ":")

    local filename = sections[1]
    local row = tonumber(sections[2])
    local col = tonumber(sections[3])

    vim.cmd(string.format([[bwipeout! %s]], prompt_bufnr))

    a.nvim_set_current_win(picker.original_win_id or 0)
    vim.cmd(string.format(":e %s", filename))

    local bufnr = vim.api.nvim_get_current_buf()
    a.nvim_buf_set_option(bufnr, 'buflisted', true)
    if row and col then
      a.nvim_win_set_cursor(0, {row, col})
    end

    vim.cmd [[stopinsert]]
  end
end

keymap["escape"] = function(prompt_bufnr)
  vim.cmd(string.format([[bwipeout! %s]], prompt_bufnr))
end

return mappings