Author: bdonlan
Date: 2005-01-26 18:05:00 -0500 (Wed, 26 Jan 2005)
New Revision: 616

Added:
   trunk/dev-tools/vim-template/
   trunk/dev-tools/vim-template/sample-plugins/
   trunk/dev-tools/vim-template/sample-plugins/c_header_template.vim
   trunk/dev-tools/vim-template/sample-plugins/perl_mod_template.vim
   trunk/dev-tools/vim-template/sample-templates/
   trunk/dev-tools/vim-template/sample-templates/haver-template.pm
   trunk/dev-tools/vim-template/sample-templates/template.h
   trunk/dev-tools/vim-template/sample-templates/template.pm
   trunk/dev-tools/vim-template/vim-template.vim
Modified:
   /
Log:
 [EMAIL PROTECTED]:  bdonlan | 2005-01-26 18:04:12 -0500
 Add a more general version of vim-automod



Property changes on: 
___________________________________________________________________
Name: svk:merge
   - 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local:16462
   + 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local:16483

Added: trunk/dev-tools/vim-template/sample-plugins/c_header_template.vim
===================================================================
--- trunk/dev-tools/vim-template/sample-plugins/c_header_template.vim   
2005-01-26 20:56:33 UTC (rev 615)
+++ trunk/dev-tools/vim-template/sample-plugins/c_header_template.vim   
2005-01-26 23:05:00 UTC (rev 616)
@@ -0,0 +1,22 @@
+" vim: set ft=vim ts=8 sts=4 sw=4:
+let b:TemplateSubstituteHook="CHeaderTemplateSubstitute"
+
+if exists('did_c_autoheader')
+       finish
+endif
+
+let did_c_autoheader=1
+
+if !exists("CHeaderTemplateSubstitute")
+       function CHeaderTemplateSubstitute(path)
+               " Kill all but the header name
+               let file = a:path
+               let define = substitute(file, '^[^/]\+/', '', 'g')
+               let define = toupper(define)
+               let define = substitute(define, '\.', '_', 'g')
+
+               " Replace <<DEFINE>> with the guessed preprocessor define name.
+               silent %substitute/<<DEFINE>>/\=define/ge
+       endfunction
+endif
+

Added: trunk/dev-tools/vim-template/sample-plugins/perl_mod_template.vim
===================================================================
--- trunk/dev-tools/vim-template/sample-plugins/perl_mod_template.vim   
2005-01-26 20:56:33 UTC (rev 615)
+++ trunk/dev-tools/vim-template/sample-plugins/perl_mod_template.vim   
2005-01-26 23:05:00 UTC (rev 616)
@@ -0,0 +1,68 @@
+" vim: set ft=vim ts=8 sts=4 sw=4:
+"
+let b:TemplateSubstituteHook="PerlAutomodTemplateSubstitute"
+
+if exists('did_perl_automod')
+    finish
+endif
+
+let did_perl_automod=1
+
+if !exists("PerlAutomodTemplateSubstitute")
+       function PerlAutomodTemplateSubstitute(path)
+               let package = s:PathToPerlPackage(a:path)
+
+               silent %substitute/<<PACKAGE>>/\=package/ge
+       endfunction
+endif
+
+" Arguments: path -- the relative to . or absolute path to a perl module.
+" Returns: a perl package name. 
+" Example: PathToPerlPackage( "lib/foo/bar/baz.pm" ) == "foo::bar::baz"
+" Description:
+"   This function uses a simple heuristic to turn a
+"   path name into a perl module package name.
+"   The heuristic is to take the full path of the file,
+"   search for the part after /lib/, change slashes to ::'s,
+"   and remove the .pm
+" Notes:
+"   This is a very simple heuristic, and could probably
+"   be improved, but it works for me.
+" Author:
+"   Dylan Hardison <[EMAIL PROTECTED]>
+
+function s:PathToPerlPackage(path)
+    " Is the file absolute or relative?
+    if match(a:path, '^/') == -1
+        " It is relative.
+        let path = getcwd() . '/' . a:path
+    else
+        let path = a:path
+    endif
+
+    " Skip the first char, "/"
+    let path = strpart(path, 1)
+    
+    " Find the first occurance of "/lib/" in the path
+    let pos = match(path, "/lib/")
+    
+    " If "/lib/" did not occure in the path
+    if pos == -1
+        let path = substitute(path, '^[^A-Z]\+/', '', 'g')
+    else
+        " Oh, it did. Start eating 
+        while pos > -1
+            " skip past the rest of the path + 5 chars
+            " to skip the /lib/ part.
+            let path = strpart(path, pos + 5)
+            let pos  = match(path, "/lib/")
+        endwhile
+    endif
+
+    " Change slashes to ::'s
+    let package = substitute(path, '/', '::', 'g')
+    " Remove the .pm extension.
+    let package = substitute(package, '\.pm$', '', '')
+
+    return package
+endfunction

Added: trunk/dev-tools/vim-template/sample-templates/haver-template.pm
===================================================================
--- trunk/dev-tools/vim-template/sample-templates/haver-template.pm     
2005-01-26 20:56:33 UTC (rev 615)
+++ trunk/dev-tools/vim-template/sample-templates/haver-template.pm     
2005-01-26 23:05:00 UTC (rev 616)
@@ -0,0 +1,72 @@
+# vim: set ts=4 sw=4 expandtab si ai sta tw=100:
+# This module is copyrighted, see end of file for details.
+package <<PACKAGE>>;
+use strict;
+use warnings;
+
+our $VERSION = 0.01;
+use base 'Haver::Base'; # Highly recommended for haver stuff.
+
+sub initialize {
+    my ($me) = @_;
+
+    # <<CURSOR>>
+}
+
+# use this instead of overriding DESTROY, if you extend Haver::Base.
+# sub finalize {
+#     my ($me) = @_;
+#     
+# }
+
+
+
+1;
+__END__
+
+=head1 NAME
+
+<<PACKAGE>> - description
+
+=head1 SYNOPSIS
+
+  use <<PACKAGE>>
+  # Small code example.
+
+=head1 DESCRIPTION
+
+FIXME
+
+=head1 METHODS
+
+<<PACKAGE>> implements the following methods:
+
+=head1 BUGS
+
+Describe how to report bugs, and list any known bugs.
+
+=head1 AUTHOR
+
+<<AUTHOR>>, E<lt><<EMAIL>>E<gt>
+
+=head1 SEE ALSO
+
+L<perl(1)>, and any other related modules / manpages / websites.
+
+=head1 COPYRIGHT and LICENSE
+
+Copyright (C) <<YEAR>> by <<AUTHOR>>. All Rights Reserved.
+
+This module is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This module is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this module; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Added: trunk/dev-tools/vim-template/sample-templates/template.h
===================================================================
--- trunk/dev-tools/vim-template/sample-templates/template.h    2005-01-26 
20:56:33 UTC (rev 615)
+++ trunk/dev-tools/vim-template/sample-templates/template.h    2005-01-26 
23:05:00 UTC (rev 616)
@@ -0,0 +1,6 @@
+#ifndef <<DEFINE>>
+#define <<DEFINE>> 1
+
+<<CURSOR>>
+
+#endif


Property changes on: trunk/dev-tools/vim-template/sample-templates/template.h
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/dev-tools/vim-template/sample-templates/template.pm
===================================================================
--- trunk/dev-tools/vim-template/sample-templates/template.pm   2005-01-26 
20:56:33 UTC (rev 615)
+++ trunk/dev-tools/vim-template/sample-templates/template.pm   2005-01-26 
23:05:00 UTC (rev 616)
@@ -0,0 +1,59 @@
+# vim: set ts=4 sw=4 expandtab si ai sta tw=100:
+# This module is copyrighted, see end of file for details.
+package <<PACKAGE>>;
+use strict;
+use warnings;
+
+our $VERSION = 0.01;
+
+<<CURSOR>>
+
+1;
+__END__
+
+=head1 NAME
+
+<<PACKAGE>> - description
+
+=head1 SYNOPSIS
+
+  use <<PACKAGE>>
+  # Small code example.
+
+=head1 DESCRIPTION
+
+FIXME
+
+=head1 METHODS
+
+<<PACKAGE>> implements the following methods:
+
+=head1 BUGS
+
+Describe how to report bugs, and list any known bugs.
+
+=head1 AUTHOR
+
+<<AUTHOR>>, E<lt><<EMAIL>>E<gt>
+
+=head1 SEE ALSO
+
+L<perl(1)>, and any other related modules / manpages / websites.
+
+=head1 COPYRIGHT and LICENSE
+
+Copyright (C) <<YEAR>> by <<AUTHOR>>. All Rights Reserved.
+
+This module is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This module is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this module; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


Property changes on: trunk/dev-tools/vim-template/sample-templates/template.pm
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

Added: trunk/dev-tools/vim-template/vim-template.vim
===================================================================
--- trunk/dev-tools/vim-template/vim-template.vim       2005-01-26 20:56:33 UTC 
(rev 615)
+++ trunk/dev-tools/vim-template/vim-template.vim       2005-01-26 23:05:00 UTC 
(rev 616)
@@ -0,0 +1,96 @@
+" vim: set ft=vim ts=8 sts=4 sw=4:
+"
+" vim-template.vim
+"
+" Maintainer:   Bryan Donlan <[EMAIL PROTECTED]>
+" Last Change:  24-Jan-2005
+" Description:  This is a script which allows templates for various file
+"                 extensions to be defined.
+"
+" To install: 
+"   * Copy to $VIMRUNTIME/plugins
+"   * Copy some templates to $VIMRUNTIME/templates
+"   * Add the following to your .vimrc:
+"     runtime plugins/vim-template.vim
+"   * In your ~/.vimrc, use the following autocmd for each template you wish
+"    to define :
+"     
+"     autocmd BufNewFile *.(ext) nested call TemplateNewFile("template_name")
+"     
+"   * Define b:TemplateSubstitute to perform filetype substitutions
+"   * In your ~/.bashrc (or equivelent):
+"     export REALNAME="My Real Name"
+"     export EMAIL="[EMAIL PROTECTED]"
+"     
+"     <<AUTHOR>> in the template file will be replaced with $REALNAME,
+"     and <<EMAIL>> with $EMAIL.
+" 
+" Based on the vim-automod script by Dylan Hardison <[EMAIL PROTECTED]>
+
+if exists('did_template')
+    finish
+endif
+
+let did_template=1
+
+" Arguments: template - the template file (under $VIMRUNTIME/templates) to use
+" Returns: nothing
+" Example: TemplateNewFile("footemplate.pm")
+" Description:
+"    This function loads the specified template into a new buffer and performs
+"    a number of substitutions on it. If b:TemplateSubstituteHook is defined,
+"    the function named there will be called with the filename as an argument.
+
+function TemplateNewFile(template)
+    let fullpath = s:ResolveFilename("templates/" . a:template)
+    if fullpath == ""
+        return
+    endif
+
+    execute "0read" fullpath
+    
+    if exists("$REALNAME")
+        silent %substitute/<<AUTHOR>>/\=$REALNAME/ge
+    endif
+
+    if exists("$EMAIL")
+        silent %substitute/<<EMAIL>>/\=$EMAIL/ge
+    endif
+
+    let year=strftime("%Y")
+    silent %substitute/<<YEAR>>/\=year/ge
+
+    if exists("b:TemplateSubstituteHook")
+        execute "call" b:TemplateSubstituteHook "(@%)"
+    endif
+    
+    silent %substitute/<<CURSOR>>//ge
+
+endfunction
+
+" Arguments: file - Filename to search for
+" Returns: Full path to filename under $VIMRUNTIME, or "" if not found
+" Example: s:ResolveFilename("foo") => "/home/user/.vim/foo"
+" Description:
+"    Searches for the given filename under $VIMRUNTIME
+function s:ResolveFilename(file)
+    let path = &runtimepath
+    while match(path, "/") != -1
+        if match(path, ",") != -1
+            let thispath = substitute(path, ",.*", "", "")
+            let path = substitute(path, "^[^,]*,", "", "")
+        else
+            let thispath = path
+            let path = ""
+        endif
+        
+        let fullpath = thispath . "/" . a:file
+        let fullpath = substitute(fullpath, "^ *", "", "")
+        let fullpath = substitute(fullpath, " *$", "", "")
+        if filereadable(fullpath)
+            return fullpath
+        endif
+    endwhile
+    return ""
+endfunction
+


Reply via email to