feat: add sections opt for man_pages and fix for macos (#413)
:Telescope man_pages sections=3 will only show man pages from section 3 :Telescope man_pages sections=1,2,3 will show man pages from section 1, 2 and 3
This commit is contained in:
committed by
GitHub
parent
9e76b1613a
commit
c422d86eb9
@@ -429,27 +429,31 @@ internal.help_tags = function(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
internal.man_pages = function(opts)
|
internal.man_pages = function(opts)
|
||||||
local pages = utils.get_os_command_output(opts.man_cmd or { 'apropos', '--sections=1', '' })
|
opts.sections = utils.get_default(opts.sections, {'1'})
|
||||||
|
assert(vim.tbl_islist(opts.sections), 'sections should be a list')
|
||||||
|
opts.man_cmd = utils.get_lazy_default(opts.man_cmd, function()
|
||||||
|
local is_darwin = vim.loop.os_uname().sysname == 'Darwin'
|
||||||
|
return is_darwin and {'apropos', ' '} or {'apropos', ''}
|
||||||
|
end)
|
||||||
|
opts.entry_maker = opts.entry_maker or make_entry.gen_from_apropos(opts)
|
||||||
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = 'Man',
|
prompt_title = 'Man',
|
||||||
finder = finders.new_table {
|
finder = finders.new_oneshot_job(opts.man_cmd, opts),
|
||||||
results = pages,
|
|
||||||
entry_maker = opts.entry_maker or make_entry.gen_from_apropos(opts),
|
|
||||||
},
|
|
||||||
previewer = previewers.man.new(opts),
|
previewer = previewers.man.new(opts),
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
attach_mappings = function(prompt_bufnr)
|
attach_mappings = function(prompt_bufnr)
|
||||||
actions._goto_file_selection:replace(function(_, cmd)
|
actions._goto_file_selection:replace(function(_, cmd)
|
||||||
local selection = actions.get_selected_entry()
|
local selection = actions.get_selected_entry()
|
||||||
|
local args = selection.section .. ' ' .. selection.value
|
||||||
|
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
if cmd == 'edit' or cmd == 'new' then
|
if cmd == 'edit' or cmd == 'new' then
|
||||||
vim.cmd('Man ' .. selection.value)
|
vim.cmd('Man ' .. args)
|
||||||
elseif cmd == 'vnew' then
|
elseif cmd == 'vnew' then
|
||||||
vim.cmd('vert bo Man ' .. selection.value)
|
vim.cmd('vert bo Man ' .. args)
|
||||||
elseif cmd == 'tabedit' then
|
elseif cmd == 'tabedit' then
|
||||||
vim.cmd('tab Man ' .. selection.value)
|
vim.cmd('tab Man ' .. args)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@@ -613,9 +613,14 @@ function make_entry.gen_from_packages(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function make_entry.gen_from_apropos()
|
function make_entry.gen_from_apropos(opts)
|
||||||
|
local sections = {}
|
||||||
|
for _, section in ipairs(opts.sections) do
|
||||||
|
sections[section] = true
|
||||||
|
end
|
||||||
|
|
||||||
local displayer = entry_display.create {
|
local displayer = entry_display.create {
|
||||||
separator = "",
|
separator = ' ',
|
||||||
items = {
|
items = {
|
||||||
{ width = 30 },
|
{ width = 30 },
|
||||||
{ remaining = true },
|
{ remaining = true },
|
||||||
@@ -624,20 +629,21 @@ function make_entry.gen_from_apropos()
|
|||||||
|
|
||||||
local make_display = function(entry)
|
local make_display = function(entry)
|
||||||
return displayer {
|
return displayer {
|
||||||
entry.value,
|
{ entry.keyword, 'TelescopeResultsFunction' },
|
||||||
entry.description
|
entry.description
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
return function(line)
|
return function(line)
|
||||||
local cmd, _, desc = line:match("^(.*)%s+%((.*)%)%s+%-%s(.*)$")
|
local keyword, cmd, section, desc = line:match'^((.-)%s*%(([^)]+)%).-)%s+%-%s+(.*)$'
|
||||||
|
return keyword and sections[section] and {
|
||||||
return {
|
|
||||||
value = cmd,
|
value = cmd,
|
||||||
description = desc,
|
description = desc,
|
||||||
ordinal = cmd,
|
ordinal = cmd,
|
||||||
display = make_display,
|
display = make_display,
|
||||||
}
|
section = section,
|
||||||
|
keyword = keyword,
|
||||||
|
} or nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -372,7 +372,10 @@ previewers.help = defaulter(function(_)
|
|||||||
}
|
}
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
previewers.man = defaulter(function(_)
|
previewers.man = defaulter(function(opts)
|
||||||
|
local pager = utils.get_lazy_default(opts.PAGER, function()
|
||||||
|
return vim.fn.executable('col') == 1 and 'col -bx' or ''
|
||||||
|
end)
|
||||||
return previewers.new_buffer_previewer {
|
return previewers.new_buffer_previewer {
|
||||||
get_buffer_by_name = function(_, entry)
|
get_buffer_by_name = function(_, entry)
|
||||||
return entry.value
|
return entry.value
|
||||||
@@ -380,8 +383,8 @@ previewers.man = defaulter(function(_)
|
|||||||
|
|
||||||
define_preview = function(self, entry, status)
|
define_preview = function(self, entry, status)
|
||||||
local win_width = vim.api.nvim_win_get_width(self.state.winid)
|
local win_width = vim.api.nvim_win_get_width(self.state.winid)
|
||||||
putils.job_maker({'man', '-P', 'cat', entry.value}, self.state.bufnr, {
|
putils.job_maker({'man', entry.section, entry.value}, self.state.bufnr, {
|
||||||
env = { ["MANWIDTH"] = win_width },
|
env = { ["PAGER"] = pager, ["MANWIDTH"] = win_width },
|
||||||
value = entry.value,
|
value = entry.value,
|
||||||
bufname = self.state.bufname
|
bufname = self.state.bufname
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user