Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Open file #32

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions lua/telescope/_extensions/bib_parsers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
local State = {
name = 1,
path = 2,
ext = 3,
done = 4
}

local Commit = {
name = 1,
path = 2,
ext = 3,
}

-- P for parser

P = {}

P.file_list = function(str)
local result = {}
table.insert(result, {})
local start = 1
local index = 1
local state = nil
for c in str:gmatch"." do

if state == nil then
if c == "{" then
state = State.name
goto continue
end
return nil
end

skip = false
commit = nil

if state == State.name then
if c == "}" then
state = State.done
commit = Commit.path
elseif c == ";" then
commit = Commit.path
elseif c == ":" then
state = State.path
commit = Commit.name
else
skip = (c == "\\")
end

elseif state == State.path then
if c == ":" then
state = State.ext
commit = Commit.path
elseif c == "}" or c == ";" then
state = State.err
else
skip = ( c == "\\" )
end

elseif state == State.ext then
if c == ";" then
state = State.name
commit = Commit.ext
elseif c == "}" then
state = State.done
commit = Commit.ext
end

end

if commit == nil then
goto continue
end

term = result[#result]
word = str:sub(start+1, index-1)
start = index

if commit == Commit.name then
term.name = word

elseif commit == Commit.path then
term.path = word

if term.name == nil and state ~= State.done then
table.insert(result, {})
end

elseif commit == Commit.ext then
term.ext = word

if state ~= State.done then
table.insert(result, {})
end
end

if state == State.done then
return result
end

::continue::

index = index + 1
end
end

return P
28 changes: 28 additions & 0 deletions lua/telescope/_extensions/bibtex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ local previewers = require('telescope.previewers')
local conf = require('telescope.config').values
local scan = require('plenary.scandir')
local path = require('plenary.path')
local job = require('plenary.job')
local putils = require('telescope.previewers.utils')
local loop = vim.loop
local parser = require('telescope._extensions.bib_parsers')

local depth = 1
local formats = {}
Expand All @@ -28,6 +30,7 @@ local user_files = {}
local files_initialized = false
local files = {}
local search_keys = { 'author', 'year', 'title' }
local reader = {}

local function table_contains(table, element)
for _, value in pairs(table) do
Expand Down Expand Up @@ -187,6 +190,7 @@ local function bibtex_picker(opts)
attach_mappings = function(_, map)
actions.select_default:replace(key_append(format_string))
map("i", "<c-e>", entry_append)
map("i", "<c-o>", open_file)
return true
end,
}):find()
Expand Down Expand Up @@ -216,8 +220,32 @@ entry_append = function(prompt_bufnr)
end
end

open_file = function(prompt_bufnr)
local entry = action_state.get_selected_entry().id.content
for _, line in pairs(entry) do
local match_base = '%f[%w]file'
local s = line:match(match_base .. '%s*=%s*%b{}') or line:match(match_base .. '%s*=%s*%b""') or line:match(match_base .. '%s*=%s*%d+')
if s ~= nil then
s = s:match('%b{}') or s:match('%b""') or s:match('%d+')
files = parser.file_list(s)
if #files > 1 then
print("TODO deal with multiple entries")
end
file = files[1]
job:new({
command = reader[file.extension] or "xdg-open",
args = { file.path },
detached = true,
}):start()
break
end
end
actions.close(prompt_bufnr)
end

return telescope.register_extension {
setup = function(ext_config)
reader = ext_config.reader or reader
depth = ext_config.depth or depth
local custom_formats = ext_config.custom_formats or {}
for _, format in pairs(custom_formats) do
Expand Down