blob: 1738dc9439bce477f0f8bec1c8a358fdcc82df80 [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 Moolenaar6ebe4f92022-10-28 20:47:54 +01003" Last Change: 2022 Oct 17
4
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 " Make this an unlisted, readonly scratch buffer
24 setlocal buftype=nofile noswapfile bufhidden=hide nobuflisted readonly
25
Bram Moolenaard592deb2022-06-17 15:42:40 +010026 " Ensure text width matches window width
27 setlocal foldcolumn& nofoldenable nonumber norelativenumber
28
29 " In case Vim was invoked with -M
30 setlocal modifiable
Bram Moolenaarb20545f2016-04-30 14:15:54 +020031
Bram Moolenaar72540672018-02-09 22:00:53 +010032 " Emulate 'col -b'
Bram Moolenaar6aa57292021-08-14 21:25:52 +020033 silent! keepj keepp %s/\v(.)\b\ze\1?//ge
34
35 " Remove ansi sequences
36 silent! keepj keepp %s/\v\e\[%(%(\d;)?\d{1,2})?[mK]//ge
Bram Moolenaarb20545f2016-04-30 14:15:54 +020037
Bram Moolenaar72540672018-02-09 22:00:53 +010038 " Remove empty lines above the header
39 call cursor(1, 1)
40 let n = search(".*(.*)", "c")
41 if n > 1
42 exe "1," . n-1 . "d"
43 endif
Bram Moolenaar72540672018-02-09 22:00:53 +010044
Bram Moolenaard592deb2022-06-17 15:42:40 +010045 " Finished preprocessing the buffer, prevent any further modifications
46 setlocal nomodified nomodifiable
47
48 " 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