I can help
When you first start creating a plugin, don't worry about multiple cursors. It will just get your code messy and you'll get confused. The best way to start it looking through the premade plugins in Packages/Default or at someone else's plugin. Also, when working on a plugin, always keep your console open (control + `). "Print" is your friend, especially when learning the difference between a Region, a RegionSet, a String, and a Point. For example, on your second to last line, line() returns a region while insert() is expecting a string. Try printing out a Region to your console, it is essentially a tuple of points. To retrieve the actual text that corresponds to that Region, use self.view.substr().
- If you must deal with multiple cursors, your only way is through looping, yes.
- It's important to know how to convert what you have to what you need. self.view.sel() returns a list of cursor positions. If you look at any of my plugins, I usually start with
sel = self.view.sel()
because more often than not, I'm only using one cursor. However, sel is a Region here, meaning that if you have something selected, it will look like this:
(Start of Selection, End of Selection)
Note: Start of Selection does not mean beginning. If you select text from right to left, the Region will be reversed. So to retrieve a point: you have a few options.
sel.a --> the start of the selection (aka what the user selects first)
sel.b --> the end of the selection (aka where the user stopped the selection)
sel.begin() --> the minimum of a and b (If you need to know the leftmost/topmost point of the selection no matter how the user selects the text)
sel.end() --> the maximum of a and b
Each of these 4 methods returns an int, which, as far as the API is concern, is equivalent to a point.
If there is no text selected, sel.b and sel.end() will return the same thing it's your preference as to which you use to retrieve the current cursor position. So your code would be something like:
sel = self.view.sel()
line = self.view.line(sel.b)
Another great resource: Will Bond wrote an excellent piece on Nettuts+ on how to create a plugin (here: net.tutsplus.com/tutorials/pytho ... -2-plugin/)