blob: 24338ca8ae5a1be154639487d596fa47b5c01b27 [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 Moolenaard592deb2022-06-17 15:42:40 +01003" Last Change: 2022 Jun 17
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
21 " Is this useful? Should allow for using K on word with a colon.
22 setlocal iskeyword+=:
23
24 " Ensure text width matches window width
25 setlocal foldcolumn& nofoldenable nonumber norelativenumber
26
27 " In case Vim was invoked with -M
28 setlocal modifiable
Bram Moolenaarb20545f2016-04-30 14:15:54 +020029
Bram Moolenaar72540672018-02-09 22:00:53 +010030 " Emulate 'col -b'
Bram Moolenaar6aa57292021-08-14 21:25:52 +020031 silent! keepj keepp %s/\v(.)\b\ze\1?//ge
32
33 " Remove ansi sequences
34 silent! keepj keepp %s/\v\e\[%(%(\d;)?\d{1,2})?[mK]//ge
Bram Moolenaarb20545f2016-04-30 14:15:54 +020035
Bram Moolenaar72540672018-02-09 22:00:53 +010036 " Remove empty lines above the header
37 call cursor(1, 1)
38 let n = search(".*(.*)", "c")
39 if n > 1
40 exe "1," . n-1 . "d"
41 endif
Bram Moolenaar72540672018-02-09 22:00:53 +010042
Bram Moolenaard592deb2022-06-17 15:42:40 +010043 " Finished preprocessing the buffer, prevent any further modifications
44 setlocal nomodified nomodifiable
45
46 " Set filetype to man even if ftplugin is disabled
47 setlocal iskeyword+=: filetype=man
48 runtime ftplugin/man.vim
Bram Moolenaarb20545f2016-04-30 14:15:54 +020049endfunction