From 259161077e666a77c887bb5beaeeb1c2af6198b0 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 15 Aug 2018 09:19:57 +1200 Subject: Move Perl boilerplate generation to autoload --- vim/after/ftplugin/perl.vim | 61 +-------------------------------------------- vim/autoload/perl.vim | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 60 deletions(-) create mode 100644 vim/autoload/perl.vim (limited to 'vim') diff --git a/vim/after/ftplugin/perl.vim b/vim/after/ftplugin/perl.vim index 9c3ce6f6..ee8da2a7 100644 --- a/vim/after/ftplugin/perl.vim +++ b/vim/after/ftplugin/perl.vim @@ -13,65 +13,6 @@ let b:undo_ftplugin .= '|unlet b:current_compiler' setlocal matchpairs+=<:> let b:undo_ftplugin .= '|setlocal matchpairs<' -" Function to add boilerplate intelligently -function! s:Boilerplate() abort - - " Flag whether the buffer started blank - let l:blank = line2byte(line('$') + 1) <= 2 - - " This is a .pm file, guess its package name from path - if expand('%:e') ==# 'pm' - - let l:match = matchlist(expand('%:p'), '.*/lib/\(.\+\).pm$') - if len(l:match) - let l:package = substitute(l:match[1], '/', '::', 'g') - else - let l:package = expand('%:t:r') - endif - - " Otherwise, just use 'main' - else - let l:package = 'main' - endif - - " Lines always to add - let l:lines = [ - \ 'package '.l:package.';', - \ '', - \ 'use strict;', - \ 'use warnings;', - \ 'use utf8;', - \ '', - \ 'use 5.006;', - \ '', - \ 'our $VERSION = ''0.01'';', - \ '' - \ ] - - " Conditional lines depending on package - if l:package ==# 'main' - let l:lines = ['#!perl'] + l:lines - else - let l:lines = l:lines + ['', '1;'] - endif - - " Add all the lines in the array - for l:line in l:lines - call append(line('.') - 1, l:line) - endfor - - " If we started in a completely empty buffer, delete the current blank line - if l:blank - delete - endif - - " If we added a trailing '1' for a package, move the cursor up two lines - if l:package !=# 'main' - -2 - endif - -endfunction - " Stop here if the user doesn't want ftplugin mappings if exists('g:no_plugin_maps') || exists('g:no_perl_maps') finish @@ -79,7 +20,7 @@ endif " Add boilerplate intelligently nnoremap b - \ :call Boilerplate() + \ :call perl#Boilerplate() let b:undo_ftplugin .= '|nunmap b' " Mappings to choose compiler diff --git a/vim/autoload/perl.vim b/vim/autoload/perl.vim new file mode 100644 index 00000000..3b87bb36 --- /dev/null +++ b/vim/autoload/perl.vim @@ -0,0 +1,58 @@ +" Function to add boilerplate intelligently +function! perl#Boilerplate() abort + + " Flag whether the buffer started blank + let l:blank = line2byte(line('$') + 1) <= 2 + + " This is a .pm file, guess its package name from path + if expand('%:e') ==# 'pm' + + let l:match = matchlist(expand('%:p'), '.*/lib/\(.\+\).pm$') + if len(l:match) + let l:package = substitute(l:match[1], '/', '::', 'g') + else + let l:package = expand('%:t:r') + endif + + " Otherwise, just use 'main' + else + let l:package = 'main' + endif + + " Lines always to add + let l:lines = [ + \ 'package '.l:package.';', + \ '', + \ 'use strict;', + \ 'use warnings;', + \ 'use utf8;', + \ '', + \ 'use 5.006;', + \ '', + \ 'our $VERSION = ''0.01'';', + \ '' + \ ] + + " Conditional lines depending on package + if l:package ==# 'main' + let l:lines = ['#!perl'] + l:lines + else + let l:lines = l:lines + ['', '1;'] + endif + + " Add all the lines in the array + for l:line in l:lines + call append(line('.') - 1, l:line) + endfor + + " If we started in a completely empty buffer, delete the current blank line + if l:blank + delete + endif + + " If we added a trailing '1' for a package, move the cursor up two lines + if l:package !=# 'main' + -2 + endif + +endfunction -- cgit v1.2.3