xref: /vim-8.2.3635/runtime/ftplugin/man.vim (revision 17fb0e89)
1" Vim filetype plugin file
2" Language:	man
3" Maintainer:	SungHyun Nam <[email protected]>
4" Last Change:	2010 Nov 29
5
6" To make the ":Man" command available before editing a manual page, source
7" this script from your startup vimrc file.
8
9" If 'filetype' isn't "man", we must have been called to only define ":Man".
10if &filetype == "man"
11
12  " Only do this when not done yet for this buffer
13  if exists("b:did_ftplugin")
14    finish
15  endif
16  let b:did_ftplugin = 1
17
18  " Ensure Vim is not recursively invoked (man-db does this)
19  " when doing ctrl-[ on a man page reference.
20  let $MANPAGER = ""
21
22  " allow dot and dash in manual page name.
23  setlocal iskeyword+=\.,-
24
25  " Add mappings, unless the user didn't want this.
26  if !exists("no_plugin_maps") && !exists("no_man_maps")
27    if !hasmapto('<Plug>ManBS')
28      nmap <buffer> <LocalLeader>h <Plug>ManBS
29    endif
30    nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
31
32    nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
33    nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
34  endif
35
36endif
37
38if exists(":Man") != 2
39  com -nargs=+ Man call s:GetPage(<f-args>)
40  nmap <Leader>K :call <SID>PreGetPage(0)<CR>
41endif
42
43" Define functions only once.
44if !exists("s:man_tag_depth")
45
46let s:man_tag_depth = 0
47
48let s:man_sect_arg = ""
49let s:man_find_arg = "-w"
50try
51  if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5"
52    let s:man_sect_arg = "-s"
53    let s:man_find_arg = "-l"
54  endif
55catch /E145:/
56  " Ignore the error in restricted mode
57endtry
58
59func <SID>PreGetPage(cnt)
60  if a:cnt == 0
61    let old_isk = &iskeyword
62    setl iskeyword+=(,)
63    let str = expand("<cword>")
64    let &l:iskeyword = old_isk
65    let page = substitute(str, '(*\(\k\+\).*', '\1', '')
66    let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
67    if match(sect, '^[0-9 ]\+$') == -1
68      let sect = ""
69    endif
70    if sect == page
71      let sect = ""
72    endif
73  else
74    let sect = a:cnt
75    let page = expand("<cword>")
76  endif
77  call s:GetPage(sect, page)
78endfunc
79
80func <SID>GetCmdArg(sect, page)
81  if a:sect == ''
82    return a:page
83  endif
84  return s:man_sect_arg.' '.a:sect.' '.a:page
85endfunc
86
87func <SID>FindPage(sect, page)
88  let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
89  if where !~ "^/"
90    if matchstr(where, " [^ ]*$") !~ "^ /"
91      return 0
92    endif
93  endif
94  return 1
95endfunc
96
97func <SID>GetPage(...)
98  if a:0 >= 2
99    let sect = a:1
100    let page = a:2
101  elseif a:0 >= 1
102    let sect = ""
103    let page = a:1
104  else
105    return
106  endif
107
108  " To support:	    nmap K :Man <cword>
109  if page == '<cword>'
110    let page = expand('<cword>')
111  endif
112
113  if sect != "" && s:FindPage(sect, page) == 0
114    let sect = ""
115  endif
116  if s:FindPage(sect, page) == 0
117    echo "\nCannot find a '".page."'."
118    return
119  endif
120  exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
121  exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
122  exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
123  let s:man_tag_depth = s:man_tag_depth + 1
124
125  " Use an existing "man" window if it exists, otherwise open a new one.
126  if &filetype != "man"
127    let thiswin = winnr()
128    exe "norm! \<C-W>b"
129    if winnr() > 1
130      exe "norm! " . thiswin . "\<C-W>w"
131      while 1
132	if &filetype == "man"
133	  break
134	endif
135	exe "norm! \<C-W>w"
136	if thiswin == winnr()
137	  break
138	endif
139      endwhile
140    endif
141    if &filetype != "man"
142      new
143      setl nonu fdc=0
144    endif
145  endif
146  silent exec "edit $HOME/".page.".".sect."~"
147  " Avoid warning for editing the dummy file twice
148  setl buftype=nofile noswapfile
149
150  setl ma
151  silent exec "norm 1GdG"
152  let $MANWIDTH = winwidth(0)
153  silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b"
154  " Remove blank lines from top and bottom.
155  while getline(1) =~ '^\s*$'
156    silent norm ggdd
157  endwhile
158  while getline('$') =~ '^\s*$'
159    silent norm Gdd
160  endwhile
161  1
162  setl ft=man nomod
163  setl bufhidden=hide
164  setl nobuflisted
165endfunc
166
167func <SID>PopPage()
168  if s:man_tag_depth > 0
169    let s:man_tag_depth = s:man_tag_depth - 1
170    exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
171    exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
172    exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
173    exec s:man_tag_buf."b"
174    exec s:man_tag_lin
175    exec "norm ".s:man_tag_col."|"
176    exec "unlet s:man_tag_buf_".s:man_tag_depth
177    exec "unlet s:man_tag_lin_".s:man_tag_depth
178    exec "unlet s:man_tag_col_".s:man_tag_depth
179    unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
180  endif
181endfunc
182
183endif
184
185" vim: set sw=2:
186