feat(neovim): Add a working telescope extension for notmuch

This commit is contained in:
Tom Hubrecht 2024-01-28 15:40:07 +01:00
parent 9947f546ba
commit 008ae4d5a5
3 changed files with 113 additions and 0 deletions

32
README.md Normal file
View file

@ -0,0 +1,32 @@
# Telescope Notmuch Complete
This will extract email addresses from `notmuch` and present them using [Telescope](https://github.com/nvim-telescope/telescope.nvim).
This plugin is inspired by https://codeberg.org/JoshuaCrewe/telescope-notmuch.nvim, but removes the dependency on jq, and sorts the addresses by their apparition count.
## Requirements
- [Telescope](https://github.com/nvim-telescope/telescope.nvim)
## Installation
Using lazy.nvim:
```lua
{
"https://git.hubrecht.ovh/hubrecht/telescope-notmuch.nvim",
config = function()
require("telescope").load_extension("notmuch")
end,
ft = { "mail" },
keys = { { "<c-x>", "<cmd>Telescope notmuch theme=cursor<CR>", mode = "n" } },
}
```
## Notmuch
Under the hood it runs the following command :
```
notmuch address --output count "*"
```

76
lua/notmuch/init.lua Normal file
View file

@ -0,0 +1,76 @@
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local sorters = require("telescope.sorters")
local utils = require("telescope.utils")
local actions = require("telescope.actions")
local actions_set = require("telescope.actions.set")
local action_state = require("telescope.actions.state")
local addresses = nil
local function get_addresses()
-- Cache the results
if addresses == nil then
addresses = {}
local file = assert(io.popen("notmuch address --output=count '*'"))
local output = file:read("*a")
file:close()
for line in string.gmatch(output, "([^'\n']+)") do
local score_str, email = line:match("(%d+)\t(.+)")
local score = tonumber(score_str)
if score ~= nil then
table.insert(addresses, { score = score, email = email })
end
end
table.sort(addresses, function(a, b)
return a.score > b.score
end)
end
return addresses
end
local function gen_from_notmuch(_)
return function(entry)
local email = entry.email
return {
value = entry,
ordinal = email,
display = email,
}
end
end
local notmuch = function(opts)
opts = opts or {}
opts.entry_maker = utils.get_lazy_default(opts.entry_maker, gen_from_notmuch, opts)
pickers
.new(opts, {
prompt_title = "Contact",
finder = finders.new_table({
results = get_addresses(),
entry_maker = opts.entry_maker,
}),
sorter = sorters.fuzzy_with_index_bias(opts),
attach_mappings = function(prompt_bufnr)
actions_set.select:replace(function()
local entry = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.api.nvim_put({ entry.value.email .. ", " }, "c", true, true)
end)
return true
end,
})
:find()
end
return notmuch

View file

@ -0,0 +1,5 @@
return require("telescope").register_extension({
exports = {
notmuch = require("notmuch"),
},
})