Saturday, September 6, 2008

Vim pr0n: Creating named marks

A few days ago in #vim, someone asked if it is possible to create "named marks" with vim, i.e. marking a location with a name instead of a letter or number. The answer is no, but it was an interesting idea, so I turned down my pornography and wrote a script to do it.

When writing the code, I used prototype based OO — mainly to provide a more complex example for my previous raving.

Marks in vim


In vim you can mark a location in a file so you can quickly jump back there. A mark consists of a position (line/column) and an identifier (a single letter). Every buffer has its own set of lowercase marks (letters a–z) that exist only as long as the buffer exists. There is also a set of global marks using capital letters. These marks are persistent and there is only one set.

More info can be found at :help mark-motions.

Enter "named marks"


In the IRC chat, the guy wanted the same functionality as the uppercase marks, but with more descriptive names. Which is fair enough IMO since "A" doesn't really tell you anything about the position it jumps to.

So named marks are global, persistent marks with descriptive names.

The code


OMG check this out:

  1 "start the named mark prototype
  2 let s:NamedMark = {}
  3
  4 "the file the marks are stored in
  5 let s:NamedMark.Filename = expand('~/.namedMarks')
  6
  7 "constructor
  8 function! s:NamedMark.New(name, column, line, path)
  9   if a:name =~ ' '
 10     throw "IllegalNamedmarkNameError illegal name:" . a:name
 11   endif
 12
 13   let newNamedMark = copy(self)
 14   let newNamedMark.name = a:name
 15   let newNamedMark.column = a:column
 16   let newNamedMark.line = a:line
 17   let newNamedMark.path = a:path
 18   return newNamedMark
 19 endfunction
 20
 21 "lazy load and cache all named marks
 22 function! s:NamedMark.All()
 23   if !exists("s:NamedMark.AllMarks")
 24     let s:NamedMark.AllMarks = s:NamedMark.Read()
 25   endif
 26   return s:NamedMark.AllMarks
 27 endfunction
 28
 29 "create and add a new mark to the list
 30 function! s:NamedMark.Add(name, column, line, path)
 31
 32   try
 33     "if the mark already exists, just update it
 34     let mark = s:NamedMark.FindFor(a:name)
 35     let mark.column = a:column
 36     let mark.line = a:line
 37     let mark.path = a:path
 38
 39   catch /NamedMarkNotFoundError/
 40     let newMark = s:NamedMark.New(a:name, a:column, a:line, a:path)
 41     call add(s:NamedMark.All(), newMark)
 42
 43   finally
 44     call s:NamedMark.Write()
 45   endtry
 46 endfunction
 47
 48 "find the mark with the given name
 49 function! s:NamedMark.FindFor(name)
 50   for i in s:NamedMark.All()
 51     if i.name == a:name
 52       return i
 53     endif
 54   endfor
 55   throw "NamedMarkNotFoundError no mark found for name: \"".a:name.'"'
 56 endfunction
 57
 58 "get a list of all mark names
 59 function! s:NamedMark.Names()
 60   let names = []
 61   for i in s:NamedMark.All()
 62     call add(names, i.name)
 63   endfor
 64   return names
 65 endfunction
 66
 67 "delete this mark
 68 function! s:NamedMark.delete()
 69   call remove(s:NamedMark.All(), index(s:NamedMark.All(), self))
 70   call s:NamedMark.Write()
 71 endfunction
 72
 73 "go to this mark
 74 function! s:NamedMark.recall()
 75   exec "edit " . self.path
 76   call cursor(self.line, self.column)
 77 endfunction
 78
 79 "read the marks from the filesystem and return the list
 80 function! s:NamedMark.Read()
 81   let marks = []
 82   if filereadable(s:NamedMark.Filename)
 83     let lines = readfile(s:NamedMark.Filename)
 84     for i in lines
 85       let name   = substitute(i, '^\(.\{-}\) \d\{-} \d\{-} .*$', '\1', '')
 86       let column = substitute(i, '^.\{-} \(\d\{-}\) \d\{-} .*$', '\1', '')
 87       let line   = substitute(i, '^.\{-} \d\{-} \(\d\{-}\) .*$', '\1', '')
 88       let path   = substitute(i, '^.\{-} \d\{-} \d\{-} \(.*\)$', '\1', '')
 89
 90       let namedMark = s:NamedMark.New(name, column, line, path)
 91       call add(marks, namedMark)
 92     endfor
 93   endif
 94   return marks
 95 endfunction
 96
 97 "write all named marks to the filesystem
 98 function! s:NamedMark.Write()
 99   let lines = []
100   for i in s:NamedMark.All()
101     call add(lines, i.name .' '. i.column .' '. i.line .' '. i.path)
102   endfor
103   call writefile(lines, s:NamedMark.Filename)
104 endfunction
105
106 "NM command, adds a new named mark
107 command! -nargs=1
108   \ NM call s:NamedMark.Add('<args>', col("."), line("."), expand("%:p"))
109
110 "RM command, recalls a named mark
111 command! -nargs=1 -complete=customlist,s:CompleteNamedMarks
112   \ RM call s:NamedMark.FindFor('<args>').recall()
113
114 "DeleteNamedMark command
115 command! -nargs=1 -complete=customlist,s:CompleteNamedMarks
116   \ DeleteNamedMark call s:NamedMark.FindFor('<args>').delete()
117
118 "used by the above commands for cmd line completion
119 function! s:CompleteNamedMarks(A,L,P)
120   return filter(s:NamedMark.Names(), 'v:val =~ "^' . a:A . '"')
121 endfunction


lol, so... wtf does that do?


From a users perspective, if you were to shove this code in your vimrc, or in a plugin, it would provide three commands:

  • NM: create a new named mark, e.g. :NM main-function would create the main-function mark and bind it to the current file and cursor position. If that mark already existed, it would be overwritten

  • RM: recall a previous mark, e.g. :RM main-function would jump the cursor back to the same file and position where main-function was created

  • DeleteNamedMark: delete a named mark, e.g. :DeleteNamedMark main-function would remove main-function from the mark list.


From a programming perspective, the code defines one prototype object (lines 1–104), the three commands (lines 106–116), and a completion function for two of the commands (lines 118–121).

If I had to jack off to one part of this code, it would be the prototype object. It contains:

  • Seven class methods and two class variables which are used to maintain the list of named marks, including reading/writing to the filesystem.

  • Two instance methods for deleting and recalling marks.

  • Four instance variables specifying the position and name of the mark.


If I was actually going to turn this into a plugin, I would want to flesh it out a bit and add, for example, better error handling and a command similar to the existing :marks command to list named marks.

Wednesday, September 3, 2008

Vim pr0n: Implementing prototype based objects

When vim 7 came along with support for lists (arrays) and dictionaries (hashes), nerds were delirious with joy. On the day it was released, geeks everywhere ripped off their pants and cartwheeled up and down their office aisles, their junk flapping freely in the air con. It was a great day to be alive.

It was also a great day to be blind.

One of the cool things about having dictionaries is that we can now implement prototype based OO. I've been playing around with this in vim script for a while now. If you are interested in doing any sort of OO programming with vim script then read this raving.

"Prototype Based OO" wtf yo?!


There are two types of object oriented programming: class based OO and prototype based OO.

In class based OO (e.g. java, c++, ruby etc) you write the blueprints for your objects then use those blueprints to create working object instances.

In prototype based OO (e.g. javascript, lua), you create a fully functional working object (i.e. a "prototype") and then clone that object. So the prototype object effectively serves as the class, while the clones of that prototype serve as the instances.

Getting started — methods, properties and constructors


In vim script the prototype object is defined as a dictionary, where the dictionary keys map to values (properties) and function references (methods).

Check this example out:


 1 "start the prototype
 2 let AK47 = {}
 3
 4 "the constructor
 5 function! AK47.New(ammo)
 6     let newAK47 = copy(self)
 7     let newAK47.ammo = a:ammo
 8     return newAK47
 9 endfunction
10
11 "an instance method
12 function! AK47.fire()
13     if self.ammo > 0
14         echo "BANG!"
15         let self.ammo -= 1
16     else
17         echo "click"
18     endif
19 endfunction
20
21 "at runtime we can do this:
22 let a = AK47.New(2)
23 echo a.ammo   " => 2
24 call a.fire() " => BANG!
25 call a.fire() " => BANG!
26 call a.fire() " => click


In this example, our prototype starts as an empty dictionary.

The first thing we add to the prototype is a constructor. There are a few ways you could do this, but I like to do it with a method called New(). It clones the prototype object (i.e. self), assigns the ammo instance property and returns the new object.

Next we add an instance method called fire() which "shoots" a bullet if there's any ammo left.

Notice that self is a reference to the current object. It is mandatory to use this reference when accessing members of the current object. In the above example, if self.ammo > 0 succeeds, whereas if ammo > 0 would result in an Undefined variable error.

Private methods and properties


There is no way to make methods or properties private. However, if you make up some conventions for yourself, then you can make the intent of your code clear (even though vim wont actually enforce it). I have the convention that any method or property starting with an underscore should be treated as private.


So, for example, I could make the ammo instance property "private" like this:


 1 "start the prototype
 2 let AK47 = {}
 3
 4 "the constructor
 5 function! AK47.New(ammo)
 6     let newAK47 = copy(self)
 7     let newAK47._ammo = a:ammo
 8     return newAK47
 9 endfunction
10
11 "an instance method
12 function! AK47.fire()
13     if self._ammo > 0
14         echo "BANG!"
15         let self._ammo -= 1
16     else
17         echo "click"
18     endif
19 endfunction


Class methods and class properties


Officially in vim script, there are no such things as class methods or class variables, but you can still implement them.

If a method doesn't access any instance variables or instance methods then, practically speaking, it's a class method. The New() methods above are examples.

If a variable is defined and accessed on the prototype object, then its a class variable.

I have some conventions I've been using:

  • I like to start all class members with a capital letter and all instance members with a lower case letter.

  • If I'm calling a class method from inside an instance method then I like to use the prototype name as the target object, i.e. TheClass.TheClassMethod() rather than self.TheClassMethod(). Similarly for class variables.


Inheritance


Prototyping with Vim script is an example of "Pure prototyping", or "Concatenative prototyping". Each object stands alone and has no links to its parent prototype. If you want to create a subclass, you must clone the parent prototype then add the new features to the clone. You can see why it's called called "Concatenative", since all the prototypes are joined together as you go down the inheritance tree.

This is in contrast to, say, javascript where, instead of cloning the parent, every object has a magic prototype property which points to the parent. The interpreter then uses this link for method dispatching, i.e. it searches back up the inheritance tree for the method definition.

Anyway, let's look at an example:


 1 "clone the AK47 prototype
 2 let AK47GL = copy(AK47)
 3
 4 "override the old constructor
 5 function! AK47GL.New(ammo, grenades)
 6     let newAK47GL = copy(self)
 7     let newAK47GL.ammo = a:ammo
 8     let newAK47GL.grenades = a:grenades
 9     return newAK47GL
10 endfunction
11
12 "define a new instance method
13 function! AK47GL.fireGL()
14     if self.grenades > 0
15         echo "OMG BOOOOOM!"
16         let self.grenades -= 1
17     else
18         echo "click"
19     endif
20 endfunction
21
22 "at runtime we can do this:
23 let a = AK47GL.New(2,1)
24 echo a.ammo     " => 2
25 echo a.grenades " => 1
26 call a.fire()   " => BANG!
27 call a.fire()   " => BANG!
28 call a.fire()   " => click
29 call a.fireGL() " => OMG BOOOOOM!
30 call a.fireGL() " => click


Here we define a new type of AK47 called AK47GL (an AK with an under-slung grenade launcher).

First we clone the AK47 prototype.

Then we replace the New method with one that accepts a grenade ammo counter. This is how we override methods. If we were really hardcore, we could rename the old New() method to something like _AK47_New() so that it will still be available to us, but I haven't bothered here.

Lastly, we define a new method called fireGL().

One thing to note about inheritance is that the subclass must be defined after the superclass in the code. Otherwise the initial copy() will fail.

Final ranting


We've seen that, using dictionaries we can create prototype objects with methods and properties. We can implement class methods and class variables. We can't implement private or protected members, but we can at least indicate our intentions with naming conventions. We've also seen how to implement inheritance.

I realise that there's a lot of stuff that I've left out, and if you need to know something I haven't covered here then the best source of information is other prototyping languages. Take a look at how people do it with javascript (the prototype library could be useful) or lua. Also, there's a whole list of prototype based OO languages here that you can steal ideas from.