Browse code

Add manpager workarounds

Patches the built-in Vim runtime files `plugin/manpager.vim` and
`ftplugin/man.vim` to work around tree issues:

1. `nocompatible` is set unconditionally, overriding options in the
vimrc. Fix it to only set it if necessary.
2. `col` fails with `col: write error` when Vim is invoked through
`man` with `$MANPAGER` (but not when starting Vim normally and
running `:Man`). The reason is unknown. Piping through `cat` fixes
the issue.
3. `man` likes to issue a lot of formatting warnings when output is
not a terminal (or when `MAN_KEEP_STDERR` is set) and the default
`shellredir` captures this. The fix is to temporarily set
`shellredir` to ignore `stderr`.

The patch:

for file in plugin/manpager.vim ftplugin/man.vim
do
diff -u $VIMRUNTIME/$file .vim/after/$file
done

--- $VIMRUNTIME/plugin/manpager.vim
+++ .vim/after/plugin/manpager.vim
@@ -5,7 +5,9 @@
command! -nargs=0 MANPAGER call s:ManPager() | delcommand MANPAGER

function! s:ManPager()
- set nocompatible
+ if &compatible
+ set nocompatible
+ endif
if exists('+viminfofile')
set viminfofile=NONE
endif

--- $VIMRUNTIME/ftplugin/man.vim
+++ .vim/after/ftplugin/man.vim
@@ -209,8 +209,11 @@
endif
let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat'
let env_cmd .= ' GROFF_NO_SGR=1'
- let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b'
+ let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b | cat'
+ let old_srr = &shellredir
+ let &shellredir = '>'
silent exec "r !" . man_cmd
+ let &shellredir = old_srr

if unsetwidth
let $MANWIDTH = ''

Robert Cranston authored on 20/06/2022 02:58:07
Showing 2 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,32 @@
1
+" Vim plugin for using Vim as manpager.
2
+" Maintainer: Enno Nagel <ennonagel+vim@gmail.com>
3
+" Last Change: 2018 Feb 04
4
+
5
+command! -nargs=0 MANPAGER call s:ManPager() | delcommand MANPAGER
6
+
7
+function! s:ManPager()
8
+  if &compatible
9
+    set nocompatible
10
+  endif
11
+  if exists('+viminfofile')
12
+    set viminfofile=NONE
13
+  endif
14
+  set noswapfile 
15
+
16
+  setlocal ft=man
17
+  runtime ftplugin/man.vim
18
+  setlocal buftype=nofile bufhidden=hide iskeyword+=: modifiable
19
+
20
+  " Emulate 'col -b'
21
+  silent keepj keepp %s/\v(.)\b\ze\1?//ge
22
+
23
+  " Remove empty lines above the header
24
+  call cursor(1, 1)
25
+  let n = search(".*(.*)", "c")
26
+  if n > 1
27
+    exe "1," . n-1 . "d"
28
+  endif
29
+  setlocal nomodified readonly
30
+
31
+  syntax on
32
+endfunction
0 33
new file mode 100644
... ...
@@ -0,0 +1,256 @@
1
+" Vim filetype plugin file
2
+" Language:	man
3
+" Maintainer:	Jason Franklin <vim@justemail.net>
4
+" Maintainer:	SungHyun Nam <goweol@gmail.com>
5
+" Last Change: 	2020 Oct 09
6
+
7
+" To make the ":Man" command available before editing a manual page, source
8
+" this script from your startup vimrc file.
9
+
10
+" If 'filetype' isn't "man", we must have been called to only define ":Man".
11
+if &filetype == "man"
12
+
13
+  " Only do this when not done yet for this buffer
14
+  if exists("b:did_ftplugin")
15
+    finish
16
+  endif
17
+  let b:did_ftplugin = 1
18
+endif
19
+
20
+let s:cpo_save = &cpo
21
+set cpo-=C
22
+
23
+if &filetype == "man"
24
+  " allow dot and dash in manual page name.
25
+  setlocal iskeyword+=\.,-
26
+  let b:undo_ftplugin = "setlocal iskeyword<"
27
+
28
+  " Add mappings, unless the user didn't want this.
29
+  if !exists("no_plugin_maps") && !exists("no_man_maps")
30
+    if !hasmapto('<Plug>ManBS')
31
+      nmap <buffer> <LocalLeader>h <Plug>ManBS
32
+      let b:undo_ftplugin = b:undo_ftplugin
33
+	    \ . '|silent! nunmap <buffer> <LocalLeader>h'
34
+    endif
35
+    nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
36
+
37
+    nnoremap <buffer> <silent> <c-]> :call <SID>PreGetPage(v:count)<CR>
38
+    nnoremap <buffer> <silent> <c-t> :call <SID>PopPage()<CR>
39
+    nnoremap <buffer> <silent> q :q<CR>
40
+
41
+    " Add undo commands for the maps
42
+    let b:undo_ftplugin = b:undo_ftplugin
43
+	  \ . '|silent! nunmap <buffer> <Plug>ManBS'
44
+	  \ . '|silent! nunmap <buffer> <c-]>'
45
+	  \ . '|silent! nunmap <buffer> <c-t>'
46
+	  \ . '|silent! nunmap <buffer> q'
47
+  endif
48
+
49
+  if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1)
50
+    setlocal foldmethod=indent foldnestmax=1 foldenable
51
+    let b:undo_ftplugin = b:undo_ftplugin
52
+	  \ . '|silent! setl fdm< fdn< fen<'
53
+  endif
54
+
55
+endif
56
+
57
+if exists(":Man") != 2
58
+  com -nargs=+ -complete=shellcmd Man call s:GetPage(<q-mods>, <f-args>)
59
+  nmap <Leader>K :call <SID>PreGetPage(0)<CR>
60
+  nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR>
61
+endif
62
+
63
+" Define functions only once.
64
+if !exists("s:man_tag_depth")
65
+
66
+let s:man_tag_depth = 0
67
+
68
+let s:man_sect_arg = ""
69
+let s:man_find_arg = "-w"
70
+try
71
+  if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5"
72
+    let s:man_sect_arg = "-s"
73
+    let s:man_find_arg = "-l"
74
+  endif
75
+catch /E145:/
76
+  " Ignore the error in restricted mode
77
+endtry
78
+
79
+func s:PreGetPage(cnt)
80
+  if a:cnt == 0
81
+    let old_isk = &iskeyword
82
+    if &ft == 'man'
83
+      setl iskeyword+=(,)
84
+    endif
85
+    let str = expand("<cword>")
86
+    let &l:iskeyword = old_isk
87
+    let page = substitute(str, '(*\(\k\+\).*', '\1', '')
88
+    let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
89
+    if match(sect, '^[0-9 ]\+$') == -1
90
+      let sect = ""
91
+    endif
92
+    if sect == page
93
+      let sect = ""
94
+    endif
95
+  else
96
+    let sect = a:cnt
97
+    let page = expand("<cword>")
98
+  endif
99
+  call s:GetPage('', sect, page)
100
+endfunc
101
+
102
+func s:GetCmdArg(sect, page)
103
+
104
+  if empty(a:sect)
105
+    return shellescape(a:page)
106
+  endif
107
+
108
+  return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page)
109
+endfunc
110
+
111
+func s:FindPage(sect, page)
112
+  let l:cmd = printf('man %s %s', s:man_find_arg, s:GetCmdArg(a:sect, a:page))
113
+  call system(l:cmd)
114
+
115
+  if v:shell_error
116
+    return 0
117
+  endif
118
+
119
+  return 1
120
+endfunc
121
+
122
+func s:GetPage(cmdmods, ...)
123
+  if a:0 >= 2
124
+    let sect = a:1
125
+    let page = a:2
126
+  elseif a:0 >= 1
127
+    let sect = ""
128
+    let page = a:1
129
+  else
130
+    return
131
+  endif
132
+
133
+  " To support:	    nmap K :Man <cword>
134
+  if page == '<cword>'
135
+    let page = expand('<cword>')
136
+  endif
137
+
138
+  if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0)
139
+    if sect != "" && s:FindPage(sect, page) == 0
140
+      let sect = ""
141
+    endif
142
+  endif
143
+  if s:FindPage(sect, page) == 0
144
+    let msg = 'man.vim: no manual entry for "' . page . '"'
145
+    if !empty(sect)
146
+      let msg .= ' in section ' . sect
147
+    endif
148
+    echomsg msg
149
+    return
150
+  endif
151
+  exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
152
+  exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
153
+  exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
154
+  let s:man_tag_depth = s:man_tag_depth + 1
155
+
156
+  let open_cmd = 'edit'
157
+
158
+  " Use an existing "man" window if it exists, otherwise open a new one.
159
+  if &filetype != "man"
160
+    let thiswin = winnr()
161
+    exe "norm! \<C-W>b"
162
+    if winnr() > 1
163
+      exe "norm! " . thiswin . "\<C-W>w"
164
+      while 1
165
+	if &filetype == "man"
166
+	  break
167
+	endif
168
+	exe "norm! \<C-W>w"
169
+	if thiswin == winnr()
170
+	  break
171
+	endif
172
+      endwhile
173
+    endif
174
+    if &filetype != "man"
175
+      if exists("g:ft_man_open_mode")
176
+        if g:ft_man_open_mode == 'vert'
177
+	  let open_cmd = 'vsplit'
178
+        elseif g:ft_man_open_mode == 'tab'
179
+	  let open_cmd = 'tabedit'
180
+        else
181
+	  let open_cmd = 'split'
182
+        endif
183
+      else
184
+	let open_cmd = a:cmdmods . ' split'
185
+      endif
186
+    endif
187
+  endif
188
+
189
+  silent execute open_cmd . " $HOME/" . page . '.' . sect . '~'
190
+
191
+  " Avoid warning for editing the dummy file twice
192
+  setl buftype=nofile noswapfile
193
+
194
+  setl fdc=0 ma nofen nonu nornu
195
+  %delete _
196
+  let unsetwidth = 0
197
+  if empty($MANWIDTH)
198
+    let $MANWIDTH = winwidth(0)
199
+    let unsetwidth = 1
200
+  endif
201
+
202
+  " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[
203
+  " on a man page reference by unsetting MANPAGER.
204
+  " Some versions of env(1) do not support the '-u' option, and in such case
205
+  " we set MANPAGER=cat.
206
+  if !exists('s:env_has_u')
207
+    call system('env -u x true')
208
+    let s:env_has_u = (v:shell_error == 0)
209
+  endif
210
+  let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat'
211
+  let env_cmd .= ' GROFF_NO_SGR=1'
212
+  let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b | cat'
213
+  let old_srr = &shellredir
214
+  let &shellredir = '>'
215
+  silent exec "r !" . man_cmd
216
+  let &shellredir = old_srr
217
+
218
+  if unsetwidth
219
+    let $MANWIDTH = ''
220
+  endif
221
+  " Remove blank lines from top and bottom.
222
+  while line('$') > 1 && getline(1) =~ '^\s*$'
223
+    1delete _
224
+  endwhile
225
+  while line('$') > 1 && getline('$') =~ '^\s*$'
226
+    $delete _
227
+  endwhile
228
+  1
229
+  setl ft=man nomod
230
+  setl bufhidden=hide
231
+  setl nobuflisted
232
+  setl noma
233
+endfunc
234
+
235
+func s:PopPage()
236
+  if s:man_tag_depth > 0
237
+    let s:man_tag_depth = s:man_tag_depth - 1
238
+    exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
239
+    exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
240
+    exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
241
+    exec s:man_tag_buf."b"
242
+    exec s:man_tag_lin
243
+    exec "norm! ".s:man_tag_col."|"
244
+    exec "unlet s:man_tag_buf_".s:man_tag_depth
245
+    exec "unlet s:man_tag_lin_".s:man_tag_depth
246
+    exec "unlet s:man_tag_col_".s:man_tag_depth
247
+    unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
248
+  endif
249
+endfunc
250
+
251
+endif
252
+
253
+let &cpo = s:cpo_save
254
+unlet s:cpo_save
255
+
256
+" vim: set sw=2 ts=8 noet: