Making Vala Easier in Vim

Chat about just about anything else

Making Vala Easier in Vim

Postby Corvalis on Sun Jan 19, 2014 12:54 am

I'm trying to teach myself Vala and GTK so I made this plugin to help make working in Vim easier
and also because the commands to the terminal take so long to type :)

Vim has a folder located in the Home folder. It starts with a dot "."
because it is hidden.

If you have Vim installed and this folder isn't there you'll want to create ".vim"
The same goes for these non-hidden folders that go inside it:
1) ftplugin
2) ftdetect
3) syntax

The naming convention of Vim files is "Language.vim"

As an example, Vim looks for Python.vim in the ftplugin folder for Python plugins and syntax folder for Syntax files.
If Vim didn't recognize Python files by default, we would also have a Python.vim file in the ftdetect folder.

Because Vim doesn't recgonize Vala we must fix this by putting a Vala.vim in the ftdetect folder!

Vala.vim for ftdetect folder.

Code: Select all
"Detects Vala file type.
"Necessary for everything to work properly.

au BufNewFile,BufRead *.vala   set filetype=vala


Now that Vim can recognize the Vala filetype it will look for any plugins it might have upon startup.

Vala.vim for ftplugin folder.


Code: Select all
" Plugin for working on Vala files.

" NOTES:
" See :help usr_40.2 in Vim for info on User Defined commands.
" User defined commands/functions must start with an Uppercase letter
" and are case sensitive when typed.
"
" These commands can be auto-completed by hitting the tab key.
" Typing gtk + <Tab> will autofill GtkValac.
"
" % represents the current filename. %< removes the file extension.
" The exclamation mark (!) lets us issue a command to BASH.
" If you have a file open in Vim called Test.vala
" the command :!echo % will print the filename to the terminal.
"
" The combination of exec() and shellescape() lets us use Vim
" variables in the command we issue to the terminal.
"
" Cobbled together by Corvalis
 

" Create Vala library from the current file.
" Creates a C, Header, and VAPI file.
command CreateVlib :call CreateVlib()
function CreateVlib()
        !valac %<.vala -C -H %<.h --library %< --basedir ./
endfunction

" Compile Vala library from current file.
" Creates a SO file.
command CompileVlib :call CompileVlib()
function CompileVlib()
   !gcc %<.c --shared -fPIC -o lib%<.so $(pkg-config --cflags --libs gobject-2.0)
endfunction

" Remove C, Header, and VAPI files used to compile library.
command SweepVlib :call SweepVlib()
function SweepVlib()
   !rm %<.c %<.h %<.vapi
endfunction

" Link Vala file to a Vlib. Takes 1 argument after command is entered.
" For instance, :LinkVlib theLibrary
command -nargs=1 LinkVlib call LinkVlib(<f-args>)
function LinkVlib(Library)
   exec("!valac -X -I. -X -L. -X -l" . shellescape(a:Library) . " %<.vala -o %< " . shellescape(a:Library) . ".vapi --basedir ./")
endfunction

" Compile Vala file with the Gtk+-3 package.
command GtkValac call GtkValac()
function GtkValac()
   !valac --pkg gtk+-3.0 %
endfunction

" Run the current executable Vala file, y'know, if it exists.
" NOTE: This assumes that the executable doesn't have a file extension
"    but you can tweak it however you like.
command RunVala call RunVala()
function RunVala()
   !LD_LIBRARY_PATH=$PWD ./%<
endfunction


Syntax Support

It took some searching, but I found that the Gnome wiki has a page where you can download a Vala syntax file, which naturally goes in your syntax folder ^_^

https://wiki.gnome.org/Projects/Vala/Vim
Horace: Sedit qui timuit ne non succederet - He who feared he would not succeed sat still. (For fear of failure, he did nothing.)
Corvalis
Level 1
Level 1
 
Posts: 16
Joined: Wed Dec 19, 2012 7:30 pm

Linux Mint is funded by ads and donations.
 

Return to Open chat

Who is online

Users browsing this forum: 2010ad, crazyjust and 9 guests