Proof 09-12.
author |
Steve Losh <steve@stevelosh.com> |
date |
Wed, 03 Apr 2013 21:16:19 -0400 |
parents |
405fe87b3cf8 |
children |
(none) |
Detecting Filetypes
===================
Let's create a Potion file we can use as a sample as we're working on our
plugin. Create a `factorial.pn` file somewhere and put the following Potion
code inside it:
:::text
factorial = (n):
total = 1
n to 1 (i):
total *= i.
total.
10 times (i):
i string print
'! is: ' print
factorial (i) string print
"\n" print.
This code creates a simple factorial function and calls it ten times, printing
the results each time. Go ahead and run it with `potion factorial.pn`. The
output should look like this:
:::text
0! is: 0
1! is: 1
2! is: 2
3! is: 6
4! is: 24
5! is: 120
6! is: 720
7! is: 5040
8! is: 40320
9! is: 362880
If you don't get this output, or you get an error, stop and figure out what's
gone wrong. The code should work exactly as-is.
Take some time to understand how the code works. Refer to the Potion docs
liberally. It's not critical to understanding Vimscript but it will make you
a better programmer.
Detecting Potion Files
----------------------
Open `factorial.pn` in Vim and run the following command:
:::vim
:set filetype?
Vim will display `filetype=` because it doesn't know what a `.pn` file is yet.
Let's fix that.
Create `ftdetect/potion.vim` in your plugin's repo. Put the following lines
into it:
:::vim
au BufNewFile,BufRead *.pn set filetype=potion
This creates a single autocommand: a command to set the filetype of `.pn` files
to `potion`. Pretty straightforward.
Notice that we *didn't* use an autocommand group like we usually would. Vim
automatically wraps the contents of `ftdetect/*.vim` files in autocommand groups
for you, so you don't need to worry about it.
Close the `factorial.pn` file and reopen it. Now run the previous command
again:
:::vim
:set filetype?
This time Vim displays `filetype=potion`. When Vim started up it loaded the
autocommand group inside `~/.vim/bundle/potion/ftdetect/potion.vim`, and when it
opened `factorial.pn` the autocommand fired, setting the `filetype` to `potion`.
Now that we've taught Vim to recognize Potion files we can move on to actually
creating some useful behavior in our plugin.
Exercises
---------
Read `:help ft`. Don't worry if you don't understand everything there.
Read `:help setfiletype`.
Modify the Potion plugin's `ftdetect/potion.vim` script to use `setfiletype`
instead of `set filetype`.