import gdb.printing

class ArrayIterator:
    def __init__ (self, data, count):
        self.data = data
        self.count = count
        self.index = 0

    def __iter__ (self):
        return self

    def next (self):
        if self.index == self.count:
            raise StopIteration

        index = self.index
        self.index = self.index + 1
        return ('[%d]' % index, (self.data + index).dereference())

class MemoryView:
    """Print a memoryview"""

    def __init__(self, val):
        self.val = val

    def display_hint(self):
        return 'array'

    def children(self):
        return ArrayIterator(self.val['m_pointer'], self.val['m_size'])

    def to_string(self):
        value_type = self.val.type.template_argument(0).unqualified().strip_typedefs()
        return "memoryview<%s>" % (value_type)

class LineAndColumn:
    """Print a LineAndColumn"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        value_type = self.val.type.unqualified()
        return "%s(%s, %s)" % (value_type, self.val['line'], self.val['column'])

class BufferIterator:
    """ Print a BufferIterator"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        if self.val['m_buffer']['m_ptr'] != 0:
            return "buffer<%s>@(%s, %s)" % (self.val['m_buffer']['m_ptr'].dereference()['m_name'], self.val['m_coord']['line'], self.val['m_coord']['column'])
        else:
            return "buffer<none>@(%s, %s)" % (self.val['m_coord']['line'], self.val['m_coord']['column'])

std_str = gdb.lookup_type("std::string")
class String:
    """ Print a String"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val.cast(std_str)

class Option:
    """ Print a Option"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val["m_value"]

class CharCount:
    """Print a CharCount"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val["m_value"]

class ByteCount:
    """Print a ByteCount"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val["m_value"]

class LineCount:
    """Print a LineCount"""

    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val["m_value"]

def build_pretty_printer():
    pp = gdb.printing.RegexpCollectionPrettyPrinter("kakoune")
    pp.add_printer('memoryview',     '^Kakoune::memoryview<.*>$',    MemoryView)
    pp.add_printer('LineAndColumn',  '^Kakoune::LineAndColumn<.*>$', LineAndColumn)
    pp.add_printer('ByteCoord',      '^Kakoune::ByteCoord$',         LineAndColumn)
    pp.add_printer('CharCoord',      '^Kakoune::CharCoord$',         LineAndColumn)
    pp.add_printer('BufferIterator', '^Kakoune::BufferIterator$',    BufferIterator)
    pp.add_printer('String',         '^Kakoune::String$',            String)
    pp.add_printer('Option',         '^Kakoune::Option$',            Option)
    pp.add_printer('LineCount',      '^Kakoune::LineCount$',         LineCount)
    pp.add_printer('CharCount',      '^Kakoune::CharCount$',         CharCount)
    pp.add_printer('ByteCount',      '^Kakoune::ByteCount$',         ByteCount)
    return pp


@@ -423,21 +423,20 @@
 %0A%0Aclass 
-Memor
+Arra
 yView:%0A 
@@ -449,23 +449,22 @@
 Print a 
-memoryv
+ArrayV
 iew%22%22%22%0A%0A
@@ -793,23 +793,22 @@
 return %22
-memoryv
+ArrayV
 iew%3C%25s%3E%22
@@ -1553,49 +1553,8 @@
 %5D)%0A%0A
-std_str = gdb.lookup_type(%22std::string%22)%0A
 clas
@@ -1680,36 +1680,319 @@
     
-return self.val.cast(std_str
+std_str = gdb.lookup_type(%22std::string%22)%0A        return self.val.cast(std_str)%0A%0Aclass StringView:%0A    %22%22%22 Print a StringView%22%22%22%0A%0A    def __init__(self, val):%0A        self.val = val%0A%0A    def to_string(self):%0A        return %22%5C%22%25s%5C%22%22 %25 (self.val%5B'm_data'%5D.string(%22utf-8%22, %22ignore%22, self.val%5B'm_length'%5D%5B'm_value'%5D)
 )%0A%0Ac
@@ -2740,20 +2740,20 @@
 er('
-memoryv
+ArrayV
 iew',
+ 
     
@@ -2768,15 +2768,14 @@
 ne::
-memoryv
+ArrayV
 iew%3C
@@ -2788,13 +2788,13 @@
     
-Memor
+ Arra
 yVie
@@ -3210,16 +3210,178 @@
 String)%0A
+    pp.add_printer('StringView',     '%5EKakoune::StringView$',        StringView)%0A    pp.add_printer('SharedString',   '%5EKakoune::SharedString$',      StringView)%0A
     pp.a

Update gdb pretty printer