Repository navigation
Emoji picker for mini.pick #2577
Replies: 2 comments 3 replies
|
Thanks for sharing! Didn't closely read the "load-write-read-cache" part (it does look there are more safeguards than necessary, though), but here is my main thought about the 'mini.pick' part. Executing a picker will return the picked item (or
Hope this makes sense. |
|
I was entertaining an idea of adding an
To prevent my previous efforts from being completely wasted, I simplified the 'mini.extra' picker into a more general script that uses Emoji `vim.ui.select()` picker-- Picker ---------------------------------------------------------------------
-- Emoji picker that downloads emoji data on the first run after confirmation.
-- Uses `vim.ui.select` to work with any picker. Most current pickers work only
-- in Normal mode. Built-in and 'mini.pick' can be used in Insert mode.
-- Requires Neovim>=0.12 for `vim.net.request`.
local source_url = 'https://www.unicode.org/Public/emoji/latest/emoji-test.txt'
local store_path = vim.fn.stdpath('data') .. '/mini-extra-emoji.json'
local emoji_items
local emoji_download = function()
local msg = 'There is no local data at ' .. store_path .. '\nDownload emoji data from this URL?\n' .. source_url
if vim.fn.confirm(msg, '&Yes\n&No', 1, 'Question') ~= 1 then return end
vim.notify('Downloading emoji data. Wait until finished and retry.', vim.log.levels.INFO)
local on_response = vim.schedule_wrap(function(err, res)
if err then return vim.notify('Error downloading emoji data: ' .. tostring(err), vim.log.levels.ERROR) end
local parsed = {}
local pattern = ';%s+fully%-qualified%s+#%s+(%S+)%s+E[0-9.]+%s+(.-)\n'
res.body:gsub(pattern, function(em, name) table.insert(parsed, { em, name }) end)
vim.fn.mkdir(vim.fn.fnamemodify(store_path, ':h'), 'p')
vim.fn.writefile(vim.json.encode(parsed, { indent = ' ' }), store_path)
vim.notify('Downloaded emoji data at ' .. store_path, vim.log.levels.INFO)
end)
vim.net.request(source_url, {}, on_response)
end
_G.pick_emoji = function()
-- Get items: download on first run ever, read from file on first session run
if vim.uv.fs_stat(store_path) == nil then return emoji_download() end
if emoji_items == nil then
local emoji_json = vim.json.decode(vim.fn.readblob(store_path))
emoji_items = vim.tbl_map(function(em) return { emoji = em[1], name = em[2] } end, emoji_json)
end
-- Start picker
local format_item = function(item) return string.format('%s │ %s', item.emoji, item.name) end
local on_choice = function(item, _)
if item == nil then return end
local cur = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_buf_set_text(0, cur[1] - 1, cur[2], cur[1] - 1, cur[2], { item.emoji })
local key = vim.fn.mode() == 'i' and '<Right>' or '<Left>'
vim.fn.feedkeys(vim.keycode(key), 'n')
end
vim.ui.select(emoji_items, { prompt = 'Emoji', format_item = format_item }, on_choice)
end
vim.keymap.set('n', '<Leader>fe', '<Cmd>lua pick_emoji()<CR>', { desc = 'Pick and insert emoji' })
vim.keymap.set('i', '<C-.>', '<Cmd>lua pick_emoji()<CR>', { desc = 'Pick and insert emoji' }) |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I could not find a simple implementation of an emoji picker for mini.nvim so I made one. I'm not sure it's optimal but it works well enough :) The emoji source and caching are the same as for Snacks.nvim.
It works in insert and normal mode. The source is available in my config or below.
Here are example mappings for it:
Click for the full module code
All reactions