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:
JINNOUCHI Yasushi
2021-02-02 21:23:37 +09:00
committed by GitHub
parent 9e76b1613a
commit c422d86eb9
3 changed files with 31 additions and 18 deletions

View File

@@ -429,27 +429,31 @@ internal.help_tags = function(opts)
end
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, {
prompt_title = 'Man',
finder = finders.new_table {
results = pages,
entry_maker = opts.entry_maker or make_entry.gen_from_apropos(opts),
},
finder = finders.new_oneshot_job(opts.man_cmd, opts),
previewer = previewers.man.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions._goto_file_selection:replace(function(_, cmd)
local selection = actions.get_selected_entry()
local args = selection.section .. ' ' .. selection.value
actions.close(prompt_bufnr)
if cmd == 'edit' or cmd == 'new' then
vim.cmd('Man ' .. selection.value)
vim.cmd('Man ' .. args)
elseif cmd == 'vnew' then
vim.cmd('vert bo Man ' .. selection.value)
vim.cmd('vert bo Man ' .. args)
elseif cmd == 'tabedit' then
vim.cmd('tab Man ' .. selection.value)
vim.cmd('tab Man ' .. args)
end
end)

View File

@@ -613,9 +613,14 @@ function make_entry.gen_from_packages(opts)
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 {
separator = "",
separator = ' ',
items = {
{ width = 30 },
{ remaining = true },
@@ -624,20 +629,21 @@ function make_entry.gen_from_apropos()
local make_display = function(entry)
return displayer {
entry.value,
{ entry.keyword, 'TelescopeResultsFunction' },
entry.description
}
end
return function(line)
local cmd, _, desc = line:match("^(.*)%s+%((.*)%)%s+%-%s(.*)$")
return {
local keyword, cmd, section, desc = line:match'^((.-)%s*%(([^)]+)%).-)%s+%-%s+(.*)$'
return keyword and sections[section] and {
value = cmd,
description = desc,
ordinal = cmd,
display = make_display,
}
section = section,
keyword = keyword,
} or nil
end
end

View File

@@ -372,7 +372,10 @@ previewers.help = defaulter(function(_)
}
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 {
get_buffer_by_name = function(_, entry)
return entry.value
@@ -380,8 +383,8 @@ previewers.man = defaulter(function(_)
define_preview = function(self, entry, status)
local win_width = vim.api.nvim_win_get_width(self.state.winid)
putils.job_maker({'man', '-P', 'cat', entry.value}, self.state.bufnr, {
env = { ["MANWIDTH"] = win_width },
putils.job_maker({'man', entry.section, entry.value}, self.state.bufnr, {
env = { ["PAGER"] = pager, ["MANWIDTH"] = win_width },
value = entry.value,
bufname = self.state.bufname
})