94 lines
2.4 KiB
Lua
94 lines
2.4 KiB
Lua
local entry_display = require('telescope.pickers.entry_display')
|
|
|
|
local orgmode = require('orgmode.api')
|
|
|
|
local utils = {}
|
|
|
|
utils.get_entries = function(opts)
|
|
local file_results = vim.tbl_map(function(file)
|
|
return { file = file, filename = file.filename }
|
|
end, orgmode.load())
|
|
|
|
if not opts.archived then
|
|
file_results = vim.tbl_filter(function(entry)
|
|
return not entry.file.is_archive_file
|
|
end, file_results)
|
|
end
|
|
|
|
if opts.max_depth == 0 then
|
|
return file_results
|
|
end
|
|
|
|
local results = {}
|
|
for _, file_entry in ipairs(file_results) do
|
|
for _, headline in ipairs(file_entry.file.headlines) do
|
|
local allowed_depth = opts.max_depth == nil or headline.level <= opts.max_depth
|
|
local allowed_archive = opts.archived or not headline.is_archived
|
|
if allowed_depth and allowed_archive then
|
|
local entry = {
|
|
file = file_entry.file,
|
|
filename = file_entry.filename,
|
|
headline = headline,
|
|
}
|
|
table.insert(results, entry)
|
|
end
|
|
end
|
|
end
|
|
|
|
return results
|
|
end
|
|
|
|
utils.make_entry = function(opts)
|
|
local function make_display(entry)
|
|
local displayer = entry_display.create({
|
|
separator = ' ',
|
|
items = {
|
|
{ width = 0.2 },
|
|
{ width = (#entry.tags > 0 and 0.5 or 0.8) },
|
|
{ remaining = true },
|
|
},
|
|
})
|
|
|
|
local location = entry.location .. ':' .. entry.lnum
|
|
local headline = entry.headline
|
|
local headline_level = (headline.level % 8 == 0 and 8 or headline.level)
|
|
local headline_hl = '@org.headline.level' .. headline_level
|
|
return displayer({
|
|
location,
|
|
{ entry.line, headline_hl },
|
|
{ ' ' .. entry.tags, '@org.tag' },
|
|
})
|
|
end
|
|
|
|
return function(entry)
|
|
local headline = entry.headline
|
|
local lnum = nil
|
|
local location = vim.fn.fnamemodify(entry.filename, ':t')
|
|
local line = ''
|
|
local tags = ''
|
|
|
|
if headline then
|
|
lnum = headline.position.start_line
|
|
line = headline.title
|
|
line = string.format('%s %s', string.rep('*', headline.level), headline.title)
|
|
if #headline.all_tags > 0 then
|
|
tags = ':' .. table.concat(headline.all_tags, ':') .. ':'
|
|
end
|
|
end
|
|
|
|
return {
|
|
value = entry,
|
|
ordinal = location .. ' ' .. headline.title .. ' ' .. tags,
|
|
filename = entry.filename,
|
|
lnum = lnum,
|
|
tags = tags,
|
|
headline = headline,
|
|
display = make_display,
|
|
location = location,
|
|
line = line,
|
|
}
|
|
end
|
|
end
|
|
|
|
return utils
|