aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-08-04 00:39:33 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-08-04 00:39:33 +1200
commit6607ee111795f2922ae5e1f2b78f8f6c137c0400 (patch)
treea01da45a858236a800f2326984e2bea93cbd697e
parentCorrect a comment (diff)
downloaddotfiles-6607ee111795f2922ae5e1f2b78f8f6c137c0400.tar.gz
dotfiles-6607ee111795f2922ae5e1f2b78f8f6c137c0400.zip
Add Perl boilerplate generation mapping
-rw-r--r--vim/after/ftplugin/perl.vim64
1 files changed, 64 insertions, 0 deletions
diff --git a/vim/after/ftplugin/perl.vim b/vim/after/ftplugin/perl.vim
index 2a350805..a85685e3 100644
--- a/vim/after/ftplugin/perl.vim
+++ b/vim/after/ftplugin/perl.vim
@@ -13,11 +13,75 @@ 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
endif
+" Add boilerplate intelligently
+nnoremap <buffer> <LocalLeader>b
+ \ :<C-U>call <SID>Boilerplate()<CR>
+let b:undo_ftplugin .= '|nunmap <buffer> <LocalLeader>b'
+
" Mappings to choose compiler
nnoremap <buffer> <LocalLeader>c
\ :<C-U>compiler perl<CR>