"" ---------------------------------------------------------------------- "" Description: Execute a file with a default action "" Author: FINNPERL "" Version: 0.2 "" ReleaseDate: 2017-31-07 "" License: MIT License (see below) "" "" Copyright (C) 2017 FINNPERL under the MIT License "" "" Permission is hereby granted, free of charge, to any person obtaining a "" copy of this software and associated documentation files (the "Software"), "" to deal in the Software without restriction, including without limitation "" the rights to use, copy, modify, merge, publish, distribute, sublicense, "" and/or sell copies of the Software, and to permit persons to whom the "" Software is furnished to do so, subject to the following conditions: "" "" The above copyright notice and this permission notice shall be included in "" all copies or substantial portions of the Software. "" "" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS "" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, "" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL "" THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "" OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "" ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR "" OTHER DEALINGS IN THE SOFTWARE. "" ---------------------------------------------------------------------- "" "" Installation: Copy easy-execute.vim to your plugin directory. "" "" Overview: "" "" Often, there is a single action that is performed often for a given filetype. "" For example, for source code file unit tests are performed if present. "" "" Usage: "" "" Execute the file related to the current buffer: "" call g:Exec_file() "" "" An example of .vimrc: "" nnore :w:call Exec_file() "" "" g:easy_execution_pattern associates the file path to pattern handlers (funcref). "" See the examples in this file. Notice that a handler should return 1 if "" the file was executed and 0 otherwise (0 means the filetype was not recognized). "" This allows setting handlers for uncertain cases such as shell scripts (which may "" or may not have .sh in the end). The handler takes the path of the file and the shell "" escaped name as a parameter. "" "" As the working directory is also used to determine the file type (e.g. for "" Perl modules, we check if Makefile.PL is found). Consequently, changing the "" working directory using e.g. vim-rooter prior to calling g:Exec_file is "" advisable (by adding commands to the key map). "" "" The utility function g:Easy_system may be used for forking processes. "" Usually the handlers should return 0 in the case of failed execution as "" the filetype was recognized. g:Easy_system does the error handling by "" checking the error code and showing the process output to the user in a "" buffer associated with a temporary file. "" function s:Handle_shell(full_name, escaped_name) if getline(1) !~ '\v^#!/bin/[A-Za-z]*sh' return 0 endif exec "!chmod u+rx ".a:escaped_name return g:Easy_system(a:escaped_name) endfunction function s:Handle_gnuplot(full_name, escaped_name) return g:Easy_system('gnuplot '.a:escaped_name) endfunction function s:Handle_latex(full_name, escaped_name) exe "cd" fnameescape(expand('%:p:h')) let pdf = substitute(a:escaped_name, "\\Ctex'$", "pdf'", 'g') let maincmd = 'pdflatex '.a:escaped_name.';' return g:Easy_system(maincmd.maincmd.'fbpdf2 '.pdf) endfunction function s:Handle_make(full_name, escaped_name) if filereadable('./Makefile') "Execute the default target and tests if found. This is a guess that "works in many cases. call g:Easy_system('make') return g:Easy_system('if grep -E "^test\s*:" ./Makefile 2>/dev/null; then; make test;fi') end endfunction function s:Handle_perl(full_name, escaped_name) let c = "" if file_readable('./Makefile.PL') return g:Easy_system('perl Makefile.PL;make;make test') elseif isdirectory('./t') return g:Easy_system('prove -l t') elseif file_readable('./Makefile') return s:Handle_make(a:full_name, a:escaped_name) else return g:Easy_system('perl '.a:escaped_name) endif endfunction function s:Handle_perlxs(full_name, escaped_name) let c = "" if filereadable('./Makefile.PL') return g:Easy_system('perl Makefile.PL;make;make test') end return 0 endfunction function s:Handle_ant(full_name, escaped_name) let c = "" if filereadable('./build.xml') return g:Easy_system('ant;if ant -p |grep junit; then;ant junit;fi') end return 0 endfunction function s:Handle_maven(full_name, escaped_name) let c = "" if filereadable('./pom.xml') return g:Easy_system('mvn clean;mvn test') end return 0 endfunction function s:Handle_java(full_name, escaped_name) let c = "" if filereadable('./build.xml') return s:Handle_ant(a:full_name, a:escaped_name) elseif filereadable('./pom.xml') return s:Handle_maven(a:full_name, a:escaped_name) end return 0 endfunction function Exec_file() let f = expand('%:p') let fl = getline(1) let fq = shellescape(f) for key in keys(g:easy_exec_patterns) if f =~ '\v\C'.key let r = g:easy_exec_patterns[key](f, fq) if r != 0 return r endif endif endfor echo "Could not determine how to execute the file." return 0 endfunction function g:Easy_system(c) let r = systemlist(a:c) if v:shell_error != 0 echo "ERROR: Subprocess returned non-zero exitcode" endif if join(r,"") =~ "[0-9A-Za-z]" execute("sp") let tf=tempname() execute("edit ".tf) call setline(1, r) execute("w") set bufhidden=delete endif return 1 endfunction let g:easy_exec_patterns = {} let g:easy_exec_patterns['gnuplot$'] = function('s:Handle_gnuplot') let g:easy_exec_patterns['\.(pm|pl|t)$'] = function('s:Handle_perl') let g:easy_exec_patterns['\.(xs|PL)$'] = function('s:Handle_perlxs') "handles Makefile.PL as well let g:easy_exec_patterns['\.tex$'] = function('s:Handle_latex') let g:easy_exec_patterns['(^|/)Makefile$'] = function('s:Handle_make') let g:easy_exec_patterns['\.(c|h)$'] = function('s:Handle_make') "attempt to handle c files with make let g:easy_exec_patterns['build.xml$'] = function('s:Handle_ant') let g:easy_exec_patterns['pom.xml$'] = function('s:Handle_maven') let g:easy_exec_patterns['\.java$'] = function('s:Handle_java') let g:easy_exec_patterns['.*'] = function('s:Handle_shell')