blob: 6ed6030254da92c3159a47388e06a92cb4bc7634 [file] [log] [blame]
Bram Moolenaarb20545f2016-04-30 14:15:54 +02001" Vim plugin for using Vim as manpager.
2" Maintainer: Enno Nagel <ennonagel+vim@gmail.com>
Enno9d85d4d2024-07-04 13:37:10 +02003" Last Change: 2024 Jul 03
Bram Moolenaar6ebe4f92022-10-28 20:47:54 +01004
5if exists('g:loaded_manpager_plugin')
6 finish
7endif
8let g:loaded_manpager_plugin = 1
Bram Moolenaarb20545f2016-04-30 14:15:54 +02009
Bram Moolenaard592deb2022-06-17 15:42:40 +010010" Set up the current buffer (likely read from stdin) as a manpage
11command MANPAGER call s:ManPager()
Bram Moolenaarb20545f2016-04-30 14:15:54 +020012
Bram Moolenaar016188f2022-06-06 20:52:59 +010013function s:ManPager()
14 " global options, keep these to a minimum to avoid side effects
15 if &compatible
16 set nocompatible
17 endif
Bram Moolenaar72540672018-02-09 22:00:53 +010018 if exists('+viminfofile')
19 set viminfofile=NONE
Bram Moolenaarb20545f2016-04-30 14:15:54 +020020 endif
Bram Moolenaard592deb2022-06-17 15:42:40 +010021 syntax on
Bram Moolenaarb20545f2016-04-30 14:15:54 +020022
Bram Moolenaard592deb2022-06-17 15:42:40 +010023 " Ensure text width matches window width
24 setlocal foldcolumn& nofoldenable nonumber norelativenumber
25
26 " In case Vim was invoked with -M
27 setlocal modifiable
Bram Moolenaarb20545f2016-04-30 14:15:54 +020028
Bram Moolenaar72540672018-02-09 22:00:53 +010029 " Emulate 'col -b'
Filip Gospodinov64dea842023-08-09 18:00:36 +020030 exe 'silent! keepj keepp %s/\v(.)\b\ze\1?//e' .. (&gdefault ? '' : 'g')
Bram Moolenaar6aa57292021-08-14 21:25:52 +020031
32 " Remove ansi sequences
Filip Gospodinov64dea842023-08-09 18:00:36 +020033 exe 'silent! keepj keepp %s/\v\e\[%(%(\d;)?\d{1,2})?[mK]//e' .. (&gdefault ? '' : 'g')
Bram Moolenaarb20545f2016-04-30 14:15:54 +020034
Bram Moolenaar72540672018-02-09 22:00:53 +010035 " Remove empty lines above the header
36 call cursor(1, 1)
37 let n = search(".*(.*)", "c")
38 if n > 1
39 exe "1," . n-1 . "d"
40 endif
Bram Moolenaar72540672018-02-09 22:00:53 +010041
Bram Moolenaard592deb2022-06-17 15:42:40 +010042 " Finished preprocessing the buffer, prevent any further modifications
43 setlocal nomodified nomodifiable
44
Enno9d85d4d2024-07-04 13:37:10 +020045 " Make this an unlisted, readonly scratch buffer
46 setlocal buftype=nofile noswapfile bufhidden=hide nobuflisted readonly
47
Bram Moolenaard592deb2022-06-17 15:42:40 +010048 " Set filetype to man even if ftplugin is disabled
Bram Moolenaarf269eab2022-10-03 18:04:35 +010049 setlocal filetype=man
Bram Moolenaard592deb2022-06-17 15:42:40 +010050 runtime ftplugin/man.vim
Bram Moolenaarb20545f2016-04-30 14:15:54 +020051endfunction