Re: [gedit-list] Getting the actual word or selection

Hi Eckhard.

On Thu, 2008-06-26 at 15:06 +0200, Eckhard M. Jäger wrote:
> i developed several plugins for gEdit and i like to update now my
> Language Reference plugin 
> to support new features of Evince:
> For that I'd like to get the actual selected words or the word where
> the cursor is located. 
> I didn't know how to get it. Please, can someone tell me how?

Here is a rough doctest that illustrates how to work with the cursor,
and how to get the selected text (which is just two lines of code)

To work with the text of a Gedit.Document, you need to use the API
of the underlying object, the GtkTextBuffer. Assuming the user
selects [The text I selected] from this paragraph.

The plugin can get the document from the window or view:

    >>> document = window.get_active_document()
    >>> document
    <Document object at ... (GtkSourceBuffer at ...)>

The cursor at the insert 'mark' in the GtkTextBuffer.

    >>> cursor_iter = document.get_iter_at_mark(document.get_insert())
    >>> cursor_iter.get_offset()

The cursor_iter can be moved forwards an backwards. True or False
is returned upon success.

    >>> cursor_iter.forward_cursor_position()
    >>> cursor_iter.get_offset()
    >>> cursor_iter.forward_cursor_position(3)
    >>> cursor_iter.get_offset()
    >>> cursor_iter.backward_cursor_position()
    >>> cursor_iter.get_offset()
    >>> cursor_iter.backward_cursor_positions(3)
    >>> cursor_iter.get_offset()

The current selection can be retrieved using get_selection_bounds().

    >>> (start_iter, end_iter) = document.get_selection_bounds()
    >>> selected_text = document.get_text(start_iter, end_iter)
    >>> selected_text
    'The text I selected'


__C U R T I S  C.  H O V E Y_______
Guilty of stealing everything I am.

Attachment: signature.asc
Description: This is a digitally signed message part

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]