blob: 43f343a6bec3b89e997cdd77735644da7a4c361e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: man
Bram Moolenaar5c736222010-01-06 20:54:52 +01003" Maintainer: SungHyun Nam <goweol@gmail.com>
Bram Moolenaarcbebd482016-02-07 23:02:56 +01004" Last Change: 2016 Feb 04
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
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
Bram Moolenaar81af9252010-12-10 20:35:50 +010018 " Ensure Vim is not recursively invoked (man-db does this)
19 " when doing ctrl-[ on a man page reference.
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020020 if exists("$MANPAGER")
21 let $MANPAGER = ""
22 endif
Bram Moolenaar81af9252010-12-10 20:35:50 +010023
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 " allow dot and dash in manual page name.
25 setlocal iskeyword+=\.,-
26
27 " Add mappings, unless the user didn't want this.
28 if !exists("no_plugin_maps") && !exists("no_man_maps")
29 if !hasmapto('<Plug>ManBS')
30 nmap <buffer> <LocalLeader>h <Plug>ManBS
31 endif
Bram Moolenaard2cec5b2006-03-28 21:08:56 +000032 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
34 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
35 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
Bram Moolenaard042dc82015-11-24 19:18:36 +010036 nnoremap <buffer> <silent> q :q<CR>
37 endif
38
39 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1)
40 setlocal foldmethod=indent foldnestmax=1 foldenable
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 endif
42
Bram Moolenaar84f72352012-03-11 15:57:40 +010043 let b:undo_ftplugin = "setlocal iskeyword<"
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045endif
46
47if exists(":Man") != 2
48 com -nargs=+ Man call s:GetPage(<f-args>)
49 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
50endif
51
52" Define functions only once.
53if !exists("s:man_tag_depth")
54
55let s:man_tag_depth = 0
56
Bram Moolenaar864207d2008-06-24 22:14:38 +000057let s:man_sect_arg = ""
58let s:man_find_arg = "-w"
59try
60 if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5"
61 let s:man_sect_arg = "-s"
62 let s:man_find_arg = "-l"
63 endif
64catch /E145:/
65 " Ignore the error in restricted mode
66endtry
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68func <SID>PreGetPage(cnt)
69 if a:cnt == 0
70 let old_isk = &iskeyword
Bram Moolenaarc2299672014-11-13 14:25:38 +010071 if &ft == 'man'
72 setl iskeyword+=(,)
73 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 let str = expand("<cword>")
75 let &l:iskeyword = old_isk
76 let page = substitute(str, '(*\(\k\+\).*', '\1', '')
77 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
78 if match(sect, '^[0-9 ]\+$') == -1
79 let sect = ""
80 endif
81 if sect == page
82 let sect = ""
83 endif
84 else
85 let sect = a:cnt
86 let page = expand("<cword>")
87 endif
88 call s:GetPage(sect, page)
89endfunc
90
91func <SID>GetCmdArg(sect, page)
92 if a:sect == ''
93 return a:page
94 endif
95 return s:man_sect_arg.' '.a:sect.' '.a:page
96endfunc
97
98func <SID>FindPage(sect, page)
99 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
100 if where !~ "^/"
101 if matchstr(where, " [^ ]*$") !~ "^ /"
102 return 0
103 endif
104 endif
105 return 1
106endfunc
107
108func <SID>GetPage(...)
109 if a:0 >= 2
110 let sect = a:1
111 let page = a:2
112 elseif a:0 >= 1
113 let sect = ""
114 let page = a:1
115 else
116 return
117 endif
118
119 " To support: nmap K :Man <cword>
120 if page == '<cword>'
121 let page = expand('<cword>')
122 endif
123
124 if sect != "" && s:FindPage(sect, page) == 0
125 let sect = ""
126 endif
127 if s:FindPage(sect, page) == 0
128 echo "\nCannot find a '".page."'."
129 return
130 endif
131 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
132 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
133 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
134 let s:man_tag_depth = s:man_tag_depth + 1
135
136 " Use an existing "man" window if it exists, otherwise open a new one.
137 if &filetype != "man"
138 let thiswin = winnr()
139 exe "norm! \<C-W>b"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000140 if winnr() > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 exe "norm! " . thiswin . "\<C-W>w"
142 while 1
143 if &filetype == "man"
144 break
145 endif
146 exe "norm! \<C-W>w"
147 if thiswin == winnr()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 break
149 endif
150 endwhile
151 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000152 if &filetype != "man"
153 new
154 setl nonu fdc=0
155 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 endif
157 silent exec "edit $HOME/".page.".".sect."~"
158 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000159 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200161 setl ma nonu nornu nofen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 silent exec "norm 1GdG"
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100163 if empty($MANWIDTH)
164 let $MANWIDTH = winwidth(0)
165 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b"
167 " Remove blank lines from top and bottom.
168 while getline(1) =~ '^\s*$'
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100169 silent keepj norm ggdd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 endwhile
171 while getline('$') =~ '^\s*$'
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100172 silent keepj norm Gdd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 endwhile
174 1
175 setl ft=man nomod
176 setl bufhidden=hide
177 setl nobuflisted
178endfunc
179
180func <SID>PopPage()
181 if s:man_tag_depth > 0
182 let s:man_tag_depth = s:man_tag_depth - 1
183 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
184 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
185 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
186 exec s:man_tag_buf."b"
187 exec s:man_tag_lin
188 exec "norm ".s:man_tag_col."|"
189 exec "unlet s:man_tag_buf_".s:man_tag_depth
190 exec "unlet s:man_tag_lin_".s:man_tag_depth
191 exec "unlet s:man_tag_col_".s:man_tag_depth
192 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
193 endif
194endfunc
195
196endif
197
198" vim: set sw=2: