From 9501ee299eb6317f8d86ae22b5d7e32bdbe43ea0 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Sat, 5 Feb 2022 17:26:54 +0800 Subject: [PATCH] refactor(manpage): move `SPC h m` to fuzzy finder layer --- autoload/SpaceVim/layers/core.vim | 2 - autoload/SpaceVim/layers/leaderf.vim | 31 +++++++ autoload/SpaceVim/layers/unite.vim | 2 + bundle/vim-van/.gitignore | 2 + bundle/vim-van/LICENSE | 21 +++++ bundle/vim-van/README.md | 87 +++++++++++++++++++ .../autoload/unite/sources/manpage.vim | 50 +++++++++++ bundle/vim-van/plugin/superman.vim | 42 +++++++++ 8 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 bundle/vim-van/.gitignore create mode 100644 bundle/vim-van/LICENSE create mode 100644 bundle/vim-van/README.md create mode 100644 bundle/vim-van/autoload/unite/sources/manpage.vim create mode 100644 bundle/vim-van/plugin/superman.vim diff --git a/autoload/SpaceVim/layers/core.vim b/autoload/SpaceVim/layers/core.vim index 5cfc51d04..b5710ed9d 100644 --- a/autoload/SpaceVim/layers/core.vim +++ b/autoload/SpaceVim/layers/core.vim @@ -161,8 +161,6 @@ function! SpaceVim#layers#core#config() abort call SpaceVim#mapping#space#def('nnoremap', ['h', 'I'], 'call SpaceVim#issue#report()', 'report-issue-or-bug', 1) call SpaceVim#mapping#space#def('nnoremap', ['h', 'l'], 'SPLayer -l', 'list-all-layers', 1) call SpaceVim#mapping#space#def('nnoremap', ['h', 'L'], 'SPRuntimeLog', 'view-runtime-log', 1) - " @todo move this key binding to fuzzy layer - call SpaceVim#mapping#space#def('nnoremap', ['h', 'm'], 'Unite manpage', 'search available man pages', 1) call SpaceVim#mapping#space#def('nnoremap', ['h', 'k'], 'LeaderGuide "[KEYs]"', 'show-top-level-bindings', 1) call SpaceVim#mapping#space#def('nnoremap', ['j', '0'], 'm`^', 'jump-to-beginning-of-line', 0) call SpaceVim#mapping#space#def('nnoremap', ['j', '$'], 'm`g_', 'jump-to-end-of-line', 0) diff --git a/autoload/SpaceVim/layers/leaderf.vim b/autoload/SpaceVim/layers/leaderf.vim index 24899faac..f0a579415 100644 --- a/autoload/SpaceVim/layers/leaderf.vim +++ b/autoload/SpaceVim/layers/leaderf.vim @@ -150,6 +150,21 @@ function! SpaceVim#layers#leaderf#config() abort \ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3] \ } + let g:Lf_Extensions.manpage = + \ { + \ 'source': string(s:_function('s:manpage', 1))[10:-3], + \ 'accept': string(s:_function('s:manpage_acp', 1))[10:-3], + \ 'highlights_def': { + \ 'Lf_register_name': '^".', + \ 'Lf_register_content': '\s\+.*', + \ }, + \ 'highlights_cmd': [ + \ 'hi def link Lf_register_name ModeMsg', + \ 'hi def link Lf_register_content Normal', + \ ], + \ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3] + \ } + let g:Lf_Extensions.neoyank = \ { \ 'source': string(s:_function('s:neoyank', 1))[10:-3], @@ -246,6 +261,8 @@ function! SpaceVim#layers#leaderf#config() abort " without this key binding, SPC h SPC always open key binding guide. nmap h [SPC]h[SPC] + call SpaceVim#mapping#space#def('nnoremap', ['h', 'm'], 'Leaderf manpage', 'search-available-man-pages', 1) + let lnum = expand('') + s:lnum - 1 call SpaceVim#mapping#space#def('nnoremap', ['b', 'b'], 'Leaderf buffer', \ ['buffer-list', @@ -431,6 +448,20 @@ function! s:message_acp(line, args) abort echohl None endfunction +function! s:manpage(...) abort + if executable('man') && exists(':Man') ==# 2 + return getcompletion(':Man ', 'cmdline') + else + return [] + endif +endfunction + +function! s:manpage_acp(line, args) abort + if !empty(a:line) && exists(':Man') ==# 2 + exe printf('Man %s', a:line) + endif +endfunction + func! s:neoyank(...) abort let yank = [] for text in neoyank#_get_yank_histories()['"'] diff --git a/autoload/SpaceVim/layers/unite.vim b/autoload/SpaceVim/layers/unite.vim index 1ef87429d..a4d0cf21a 100644 --- a/autoload/SpaceVim/layers/unite.vim +++ b/autoload/SpaceVim/layers/unite.vim @@ -61,6 +61,7 @@ function! SpaceVim#layers#unite#plugins() abort " outline source fo call add(plugins, ['Shougo/unite-outline', {'merged' : 0}]) call add(plugins, ['Shougo/neomru.vim', {'merged' : 0}]) + call add(plugins, [g:_spacevim_root_dir . 'bundle/vim-van', {'merged' : 0}]) if g:spacevim_enable_googlesuggest call add(plugins, ['mopp/googlesuggest-source.vim']) @@ -75,6 +76,7 @@ let s:lnum = expand('') + 2 function! SpaceVim#layers#unite#config() abort call SpaceVim#mapping#space#def('nnoremap', ['?'], 'Unite menu:CustomKeyMaps -input=[SPC]', 'show mappings', 1) call SpaceVim#mapping#space#def('nnoremap', ['h', '[SPC]'], 'Unite help -input=SpaceVim', 'unite-SpaceVim-help', 1) + call SpaceVim#mapping#space#def('nnoremap', ['h', 'm'], 'Unite manpage', 'search available man pages', 1) call SpaceVim#mapping#space#def('nnoremap', ['b', 'b'], 'Unite buffer', 'buffer list', 1) call SpaceVim#mapping#space#def('nnoremap', ['h', 'i'], 'UniteWithCursorWord help', 'get help with the symbol at point', 1) call SpaceVim#mapping#space#def('nnoremap', ['i', 'u'], 'Unite unicode', 'search-and-insert-unicode', 1) diff --git a/bundle/vim-van/.gitignore b/bundle/vim-van/.gitignore new file mode 100644 index 000000000..c3d761053 --- /dev/null +++ b/bundle/vim-van/.gitignore @@ -0,0 +1,2 @@ +.*.swp +tags diff --git a/bundle/vim-van/LICENSE b/bundle/vim-van/LICENSE new file mode 100644 index 000000000..aa34a60f4 --- /dev/null +++ b/bundle/vim-van/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Jacob Zimmerman + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/bundle/vim-van/README.md b/bundle/vim-van/README.md new file mode 100644 index 000000000..6cd81c005 --- /dev/null +++ b/bundle/vim-van/README.md @@ -0,0 +1,87 @@ +# Vim SuperMan + +Read Unix `man` pages faster than a speeding bullet! + +Unix man pages by default open with the `less` pager. Getting them to open with +Vim can be a little bit of a pain, but in recent versions of Vim there's a +plugin (`$VIMRUNTIME/ftplugin/man.vim`) that makes this easy. + +This is a simple Vim plugin and sh function that makes replacing `man` from the +command line a cinch. + +## Installation + +Use your favorite plugin manager. If you don't have one, I'd recommend Vundle, +though you should probably also take a look at Pathogen, as it's more common. + +```bash +# if your ~/.vim folder isn't under source control: +git clone https://github.com/Z1MM32M4N/vim-superman ~/.vim/bundle/ + +# if your ~/.vim folder is under source control: +git submodule add https://github.com/Z1MM32M4N/vim-superman ~/.vim/bundle/ +``` + +Then, add the following to your `.bashrc`, `.bash_profile`, `.zshrc`, or +whatever file you use to configure your shell: + +```bash +vman() { + vim -c "SuperMan $*" + + if [ "$?" != "0" ]; then + echo "No manual entry for $*" + fi +} +``` + +Close and reopen your terminal and you're set! + +## Usage + +This predominantly a command line tool. To open the man page for `vim`: + +```bash +$ vman vim +``` + +![vman vim](http://blog.zimmerman.io/images/vim.1.png) + +It's that simple. The underlying `:Man` command supports specifying a specific +section, so you could also do something like + +```bash +$ vman 3 printf +``` + +To see the man page for the C `printf()` library call. + +![vman 3 printf](http://blog.zimmerman.io/images/printf.3.png) + +## FAQ + +For more information, see the [associated blog post][blog]. + +### Jake, why not just call the bash function `man`? + +The actual `man` command supports many more features than the Vim plugin does +(for a complete list, see `man(1)`). If you shadow the real `man` command, +things start to break, for example `apropos`, which uses `man` under the hood. + +### When I install `vim-superman` it looks nothing like this! + +There are a couple other plugins of mine featured prominently here, including +[Solarized Dark][sdark] for the color scheme and [Vim Airline][vairline] for the +statusbar. If you're curious about my whole setup, be sure to check out my +[dotfiles repository][dotfiles]. + +## License + +MIT License. See LICENSE. + + +[blog]: http://blog.zimmerman.io/2014/12/20/vim-as-a-man-page-viewer/ +[sdark]: https://github.com/altercation/vim-colors-solarized +[vairline]: https://github.com/bling/vim-airline +[dotfiles]: https://github.com/Z1MM32M4N/dotfiles + diff --git a/bundle/vim-van/autoload/unite/sources/manpage.vim b/bundle/vim-van/autoload/unite/sources/manpage.vim new file mode 100644 index 000000000..4e32cc2bb --- /dev/null +++ b/bundle/vim-van/autoload/unite/sources/manpage.vim @@ -0,0 +1,50 @@ +let s:save_cpo = &cpo +set cpo&vim + +let s:unite_source = { + \ 'name': 'manpage', + \ 'hooks': {}, + \ 'action_table': {'*': {}}, + \ } + +let s:unite_source.action_table['*'].preview = { + \ 'description' : 'open this manpage', + \ 'is_quit' : 0, + \ } + +function! s:unite_source.action_table['*'].preview.func(candidate) + execute a:candidate.action__command +endfunction + +function! s:manpage(x) + return printf("%s %s", "SuperMan", a:x) +endfunction + +function! s:unite_source.gather_candidates(args, context) + + let l:manpages = system("apropos . | awk \'{print $1}\'") + + let manpageslist = unite#util#sort_by(unite#util#uniq_by( + \ map(split(l:manpages, '\n'), + \'[fnamemodify(v:val, ":t:r"), fnamemodify(v:val, ":p")]'), 'v:val[0]'), + \'v:val[0]') + + return map(manpageslist, '{ + \ "word": v:val[0], + \ "source": "manpage", + \ "kind": ["file", "command"], + \ "action__command": s:manpage(v:val[0]), + \ "action__type": ": ", + \ "action__path": v:val[1], + \ "action__directory": fnamemodify(v:val[1], ":h"), + \ }') +endfunction + +function! unite#sources#manpage#define() + return s:unite_source +endfunction + +"unlet s:unite_source + +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/bundle/vim-van/plugin/superman.vim b/bundle/vim-van/plugin/superman.vim new file mode 100644 index 000000000..ba8ca5429 --- /dev/null +++ b/bundle/vim-van/plugin/superman.vim @@ -0,0 +1,42 @@ +" Name: SuperMan man pages +" Author: Jacob Zimmerman +" License: MIT License +" +" URL: +" +" Created: Dec 20 2014 +" Modified: Dec 20 2014 + +" Wrapper around man.vim's Man command +function! superman#SuperMan(...) + " Needed to get access to Man + source $VIMRUNTIME/ftplugin/man.vim + + " Build and pass off arguments to Man command + execute 'Man' join(a:000, ' ') + + " Quit with error code if there is only one line in the buffer + " (i.e., manpage not found) + if line('$') == 1 | cquit | endif + + " Why :Man opens up in a split I shall never know + only + + " Set options appropriate for viewing manpages + setlocal readonly + setlocal nomodifiable + + setlocal number + + setlocal noexpandtab + setlocal tabstop=8 + setlocal softtabstop=8 + setlocal shiftwidth=8 + + " To make us behave more like less + noremap q :q +endfunction + +" Command alias for our function +command! -nargs=+ SuperMan call superman#SuperMan() +