fromgtkimportTextBufferclassBuffer(object):def__init__(self,engine):self.engine=engineself.text_buffer=TextBuffer()self.bufnr=engine.add(self)@propertydeftext(self):returnself.text_buffer.get_text(self.text_buffer.get_start_iter(),self.text_buffer.get_end_iter())@text.setterdeftext(self,text):self.text_buffer.set_text(text)defappend(self,text):self.text_buffer.insert(self.text_buffer.get_end_iter(),text)def__len__(self):returnself.text_buffer.get_line_count()def__getitem__(self,item):ifisinstance(item,int):start=self.text_buffer.get_iter_at_line(item)# the last line doesnt end with \nifitem+1<len(self):end=self.text_buffer.get_iter_at_line(item+1)else:end=self.text_buffer.get_end_iter()returnself.text_buffer.get_slice(start,end)else:ifitem.startisNone:start=self.text_buffer.get_start_iter()else:start=self.text_buffer.get_iter_at_line(item.start)ifitem.stopisNoneoritem.stop>=len(self):end=self.text_buffer.get_end_iter()else:end=self.text_buffer.get_iter_at_line(item.stop)slice=self.text_buffer.get_slice(start,end)returnslice.splitlines(True)def__setitem__(self,item,value):ifisinstance(value,list):#XXX: smarter?value=''.join(value)ifisinstance(item,int):start=self.text_buffer.get_iter_at_line(item)end=self.text_buffer.get_iter_at_line(item+1)else:ifitem.startisNone:start=self.text_buffer.get_start_iter()end=self.text_buffer.get_iter_at_line(item.stop)self.text_buffer.select_range(start,end)self.text_buffer.delete_selection(False,False)ifisinstance(item,int):start=self.text_buffer.get_iter_at_line(item)elifitem.startisNone:start=self.text_buffer.get_start_iter()else:start=self.text_buffer.get_iter_at_line(item.start)self.text_buffer.insert(start,value)