#!/usr/bin/python

import pyatspi

def listener(e):
    try:
        text = e.source.queryText()
    except NotImplementedError:
        allText = 'None'
        currentChar = 'None'
        lineAtOffset = 'None'
        charAtOffset = 'None'
    else:
        allText = text.getText(0, -1)
        currentChar = text.getText(e.detail1, e.detail1 + 1)
        lineAtOffset = text.getTextAtOffset(pyatspi.TEXT_BOUNDARY_LINE_START, e.detail1)
        charAtOffset = text.getTextAtOffset(pyatspi.TEXT_BOUNDARY_CHAR, e.detail1)

    print '%s(%s, %s, %s)\n  all text: %s\n  current char: %s\n' \
          '  line at offset: %s\n  char at offset: %s' \
          % (e.type, e.detail1, e.detail2, e.any_data, allText,
             currentChar, lineAtOffset, charAtOffset)

pyatspi.Registry.registerEventListener(listener, 'object:text-caret-moved')
pyatspi.Registry.start()

