From a4caa2c53a7ad5a58d0ebc37498490f669b4ebc5 Mon Sep 17 00:00:00 2001 From: James Eapen Date: Tue, 12 Mar 2024 16:20:00 -0400 Subject: [PATCH] re-introduce field picker to insert bibtext field values --- lua/telescope-bibtex/actions.lua | 51 ++++++++++++++++++++++++++++ lua/telescope-bibtex/utils.lua | 9 +++++ lua/telescope/_extensions/bibtex.lua | 1 + 3 files changed, 61 insertions(+) diff --git a/lua/telescope-bibtex/actions.lua b/lua/telescope-bibtex/actions.lua index 1326c46..76fcef2 100644 --- a/lua/telescope-bibtex/actions.lua +++ b/lua/telescope-bibtex/actions.lua @@ -1,6 +1,10 @@ local actions = require('telescope.actions') local utils = require('telescope-bibtex.utils') local action_state = require('telescope.actions.state') +local pickers = require("telescope.pickers") +local finders = require('telescope.finders') +local previewers = require('telescope.previewers') +local conf = require('telescope.config').values return { key_append = function(format_string) @@ -43,5 +47,52 @@ return { vim.api.nvim_paste(citation, true, -1) end end + end, + + field_append = function() + return function(prompt_bufnr) + local entry = action_state.get_selected_entry().id.content + actions.close(prompt_bufnr) + local parsed = utils.parse_entry(entry) + pickers.new(opts, { + prompt_title = "Bibtex fields", + sorter = conf.generic_sorter(opts), + finder = finders.new_table{ + results = utils.get_bibkeys(parsed), + }, + previewer = previewers.new_buffer_previewer({ + define_preview = function(self, bib_entry, status) + vim.api.nvim_buf_set_lines( + self.state.bufnr, + 0, + -1, + true, + {parsed[bib_entry[1]]} + ) + vim.api.nvim_win_set_option( + status.preview_win, + 'wrap', + true + ) + end, + }), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace( + function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + local mode = vim.api.nvim_get_mode().mode + if mode == 'i' then + vim.api.nvim_put({parsed[selection[1]]}, '', false, true) + vim.api.nvim_feedkeys('a', 'n', true) + else + vim.api.nvim_put({parsed[selection[1]]}, '', true, true) + end + end + ) + return true + end + }):find() + end end } diff --git a/lua/telescope-bibtex/utils.lua b/lua/telescope-bibtex/utils.lua index 90495d7..8f7898a 100644 --- a/lua/telescope-bibtex/utils.lua +++ b/lua/telescope-bibtex/utils.lua @@ -719,4 +719,13 @@ M.format_citation = function(entry, template, opts) return M.format_template(parsed, template) end +-- Make a dict of parsed bibtex fields +M.get_bibkeys = function(parsed_entry) + local bibkeys={} + for key,_ in pairs(parsed_entry) do + table.insert(bibkeys, key) + end + return bibkeys +end + return M diff --git a/lua/telescope/_extensions/bibtex.lua b/lua/telescope/_extensions/bibtex.lua index 7600e85..ed6f22a 100644 --- a/lua/telescope/_extensions/bibtex.lua +++ b/lua/telescope/_extensions/bibtex.lua @@ -47,6 +47,7 @@ local keymaps = { i = { [""] = bibtex_actions.entry_append, [""] = bibtex_actions.citation_append(citation_format), + [""] = bibtex_actions.field_append(citation_format), } }