blob: 9ee593725d209e2e1b79aa40fbcb1f11ae22b7c9 [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>
Bram Moolenaarf269eab2022-10-03 18:04:35 +01003" Last Change: 2022 Sep 30
Bram Moolenaarb20545f2016-04-30 14:15:54 +02004
Bram Moolenaard592deb2022-06-17 15:42:40 +01005" Set up the current buffer (likely read from stdin) as a manpage
6command MANPAGER call s:ManPager()
Bram Moolenaarb20545f2016-04-30 14:15:54 +02007
Bram Moolenaar016188f2022-06-06 20:52:59 +01008function s:ManPager()
9 " global options, keep these to a minimum to avoid side effects
10 if &compatible
11 set nocompatible
12 endif
Bram Moolenaar72540672018-02-09 22:00:53 +010013 if exists('+viminfofile')
14 set viminfofile=NONE
Bram Moolenaarb20545f2016-04-30 14:15:54 +020015 endif
Bram Moolenaard592deb2022-06-17 15:42:40 +010016 syntax on
Bram Moolenaarb20545f2016-04-30 14:15:54 +020017
Bram Moolenaard592deb2022-06-17 15:42:40 +010018 " Make this an unlisted, readonly scratch buffer
19 setlocal buftype=nofile noswapfile bufhidden=hide nobuflisted readonly
20
Bram Moolenaard592deb2022-06-17 15:42:40 +010021 " Ensure text width matches window width
22 setlocal foldcolumn& nofoldenable nonumber norelativenumber
23
24 " In case Vim was invoked with -M
25 setlocal modifiable
Bram Moolenaarb20545f2016-04-30 14:15:54 +020026
Bram Moolenaar72540672018-02-09 22:00:53 +010027 " Emulate 'col -b'
Bram Moolenaar6aa57292021-08-14 21:25:52 +020028 silent! keepj keepp %s/\v(.)\b\ze\1?//ge
29
30 " Remove ansi sequences
31 silent! keepj keepp %s/\v\e\[%(%(\d;)?\d{1,2})?[mK]//ge
Bram Moolenaarb20545f2016-04-30 14:15:54 +020032
Bram Moolenaar72540672018-02-09 22:00:53 +010033 " Remove empty lines above the header
34 call cursor(1, 1)
35 let n = search(".*(.*)", "c")
36 if n > 1
37 exe "1," . n-1 . "d"
38 endif
Bram Moolenaar72540672018-02-09 22:00:53 +010039
Bram Moolenaard592deb2022-06-17 15:42:40 +010040 " Finished preprocessing the buffer, prevent any further modifications
41 setlocal nomodified nomodifiable
42
43 " Set filetype to man even if ftplugin is disabled
Bram Moolenaarf269eab2022-10-03 18:04:35 +010044 setlocal filetype=man
Bram Moolenaard592deb2022-06-17 15:42:40 +010045 runtime ftplugin/man.vim
Bram Moolenaarb20545f2016-04-30 14:15:54 +020046endfunction