Skip to main content

TextCursor

Struct TextCursor 

Source
pub struct TextCursor { /* private fields */ }
Expand description

A cursor into a TextDocument.

Multiple cursors can coexist on the same document (like Qt’s QTextCursor). When any cursor edits text, all other cursors’ positions are automatically adjusted by the document.

Cloning a cursor creates an independent cursor at the same position.

Implementations§

Source§

impl TextCursor

Source

pub fn position(&self) -> usize

Current cursor position (between characters).

Source

pub fn anchor(&self) -> usize

Anchor position. Equal to position() when no selection.

Source

pub fn has_selection(&self) -> bool

Returns true if there is a selection.

Source

pub fn selection_start(&self) -> usize

Start of the selection (min of position and anchor).

Source

pub fn selection_end(&self) -> usize

End of the selection (max of position and anchor).

Source

pub fn selected_text(&self) -> Result<String>

Get the selected text. Returns empty string if no selection.

Source

pub fn clear_selection(&self)

Collapse the selection by moving anchor to position.

Source

pub fn at_block_start(&self) -> bool

True if the cursor is at the start of a block.

Source

pub fn at_block_end(&self) -> bool

True if the cursor is at the end of a block.

Source

pub fn at_start(&self) -> bool

True if the cursor is at position 0.

Source

pub fn at_end(&self) -> bool

True if the cursor is at the very end of the document.

Source

pub fn block_number(&self) -> usize

The block number (0-indexed) containing the cursor.

Source

pub fn position_in_block(&self) -> usize

The cursor’s column within the current block (0-indexed).

Source

pub fn set_position(&self, position: usize, mode: MoveMode)

Set the cursor to an absolute position.

When extending a selection (KeepAnchor) across a table boundary, the position is snapped to the adjacent block outside the table so the entire table is “trapped” inside the selection range. This mirrors LibreOffice’s behaviour: partial table selections from outside are not allowed; the table is always fully enclosed.

The snap is skipped when:

  • mode is MoveAnchor (plain click / move without selection)
  • No adjacent block exists (table is first or last in the document)
Source

pub fn move_position( &self, operation: MoveOperation, mode: MoveMode, n: usize, ) -> bool

Move the cursor by a semantic operation.

n is used as a repeat count for character-level movements (NextCharacter, PreviousCharacter, Left, Right). For all other operations it is ignored. Returns true if the cursor moved.

Source

pub fn select(&self, selection: SelectionType)

Select a region relative to the cursor position.

Source

pub fn insert_text(&self, text: &str) -> Result<()>

Insert plain text at the cursor. Replaces selection if any.

Source

pub fn insert_formatted_text( &self, text: &str, format: &TextFormat, ) -> Result<()>

Insert text with a specific character format. Replaces selection if any.

Source

pub fn insert_block(&self) -> Result<()>

Insert a block break (new paragraph). Replaces selection if any.

Source

pub fn insert_html(&self, html: &str) -> Result<()>

Insert an HTML fragment at the cursor position. Replaces selection if any.

Source

pub fn insert_markdown(&self, markdown: &str) -> Result<()>

Insert a Markdown fragment at the cursor position. Replaces selection if any.

Source

pub fn insert_fragment(&self, fragment: &DocumentFragment) -> Result<()>

Insert a document fragment at the cursor. Replaces selection if any.

Source

pub fn selection(&self) -> DocumentFragment

Extract the current selection as a DocumentFragment.

Source

pub fn insert_image(&self, name: &str, width: u32, height: u32) -> Result<()>

Insert an image at the cursor. Replaces selection if any.

Source

pub fn insert_frame(&self) -> Result<()>

Insert a new frame at the cursor.

Source

pub fn insert_table(&self, rows: usize, columns: usize) -> Result<TextTable>

Insert a table at the cursor position.

Creates a rows × columns table with empty cells. The cursor moves into the first cell of the table. Returns a handle to the created table.

Source

pub fn current_table(&self) -> Option<TextTable>

Returns the table the cursor is currently inside, if any.

Returns None if the cursor is in the main document flow (not inside a table cell).

Source

pub fn current_table_cell(&self) -> Option<TableCellRef>

Returns the table cell the cursor is currently inside, if any.

Returns None if the cursor is not inside a table cell. When Some, provides the table, row, and column.

Source

pub fn current_frame(&self) -> Option<FrameRef>

The innermost frame enclosing the cursor’s current block, or None if the cursor sits directly in the root frame (no enclosing sub-frame). The returned depth is the nesting level from the root (1 for a direct child of root, 2 for a grandchild, etc.).

Source

pub fn is_in_blockquote(&self) -> bool

True if the cursor’s block lives inside any blockquote frame (at any nesting level).

Source

pub fn current_blockquote_frame_id(&self) -> Option<usize>

Id of the innermost blockquote frame enclosing the cursor’s block, or None if not in a blockquote.

Source

pub fn blockquote_depth_at_cursor(&self) -> usize

Nesting depth of the cursor inside blockquote frames: 0 = not in any quote, 1 = top-level quote, 2 = quote inside a quote, …

Source

pub fn is_first_block_in_current_frame(&self) -> bool

True iff the cursor’s block is the first positive entry in its owning frame’s child_order. Used by the keyboard handler to decide whether Backspace should unwrap the enclosing frame. A single-block frame returns true for both is_first_* and is_last_*.

Source

pub fn is_last_block_in_current_frame(&self) -> bool

True iff the cursor’s block is the last positive entry in its owning frame’s child_order. Used by the keyboard handler to decide whether forward Delete should unwrap the enclosing frame.

Source

pub fn current_block_is_empty(&self) -> bool

True iff the block at the cursor has no characters of content. Used by the Enter handler to decide whether to exit a blockquote.

Source

pub fn selection_spans_multiple_frames(&self) -> bool

True iff the cursor’s anchor and head sit in different frames. Used by the toolbar to disable the “toggle blockquote” button on selections that cross frame boundaries.

Source

pub fn wrap_selection_in_blockquote(&self) -> Result<()>

Wrap the current block (or the blocks in the current selection) in a new blockquote frame nested inside the cursor’s current parent frame. Returns an error if the selection spans multiple frames.

Source

pub fn insert_blockquote(&self) -> Result<()>

Wrap the current block in a new blockquote frame at the current nesting level. Equivalent to wrap_selection_in_blockquote() when there is no selection.

Source

pub fn toggle_blockquote(&self) -> Result<()>

If the cursor is inside any blockquote, unwrap the innermost one (lift its blocks into the parent frame and delete the frame). Otherwise, wrap the current block / selection in a new blockquote. Mirrors the toggle behaviour of a toolbar button.

Source

pub fn unwrap_current_frame(&self) -> Result<()>

Unwrap the innermost frame enclosing the cursor (any frame, not just blockquotes). Errors if the cursor’s block sits in the root frame.

Source

pub fn unwrap_current_block_from_blockquote(&self) -> Result<()>

Extract the cursor’s current block from its innermost enclosing blockquote frame, lifting it one nesting level. If the cursor is not in a blockquote, errors.

Source

pub fn increase_blockquote_depth(&self) -> Result<()>

Wrap the current block in a new blockquote frame. If the cursor is already inside a blockquote, this creates a deeper nested quote (depth + 1). If outside, this creates a top-level quote.

Source

pub fn decrease_blockquote_depth(&self) -> Result<()>

Pop the cursor out of one nesting level of blockquotes. If the cursor is in a depth-N quote with multiple blocks, the current block is extracted (splitting the quote if needed). If the current block is the only one in the quote, the whole quote is unwrapped.

Source

pub fn remove_table(&self, table_id: usize) -> Result<()>

Remove a table from the document by its ID.

Source

pub fn insert_table_row(&self, table_id: usize, row_index: usize) -> Result<()>

Insert a row into a table at the given index.

Source

pub fn insert_table_column( &self, table_id: usize, column_index: usize, ) -> Result<()>

Insert a column into a table at the given index.

Source

pub fn remove_table_row(&self, table_id: usize, row_index: usize) -> Result<()>

Remove a row from a table. Fails if only one row remains.

Source

pub fn remove_table_column( &self, table_id: usize, column_index: usize, ) -> Result<()>

Remove a column from a table. Fails if only one column remains.

Source

pub fn merge_table_cells( &self, table_id: usize, start_row: usize, start_column: usize, end_row: usize, end_column: usize, ) -> Result<()>

Merge a rectangular range of cells within a table.

Source

pub fn split_table_cell( &self, cell_id: usize, split_rows: usize, split_columns: usize, ) -> Result<()>

Split a previously merged cell.

Source

pub fn set_table_format( &self, table_id: usize, format: &TableFormat, ) -> Result<()>

Set formatting on a table.

Source

pub fn set_table_cell_format( &self, cell_id: usize, format: &CellFormat, ) -> Result<()>

Set formatting on a table cell.

Source

pub fn remove_current_table(&self) -> Result<()>

Remove the table the cursor is currently inside. Returns an error if the cursor is not inside a table.

Source

pub fn insert_row_above(&self) -> Result<()>

Insert a row above the cursor’s current row. Returns an error if the cursor is not inside a table.

Source

pub fn insert_row_below(&self) -> Result<()>

Insert a row below the cursor’s current row. Returns an error if the cursor is not inside a table.

Source

pub fn insert_column_before(&self) -> Result<()>

Insert a column before the cursor’s current column. Returns an error if the cursor is not inside a table.

Source

pub fn insert_column_after(&self) -> Result<()>

Insert a column after the cursor’s current column. Returns an error if the cursor is not inside a table.

Source

pub fn remove_current_row(&self) -> Result<()>

Remove the row at the cursor’s current position. Returns an error if the cursor is not inside a table.

Source

pub fn remove_current_column(&self) -> Result<()>

Remove the column at the cursor’s current position. Returns an error if the cursor is not inside a table.

Source

pub fn merge_selected_cells(&self) -> Result<()>

Merge cells spanned by the current selection.

Both cursor position and anchor must be inside the same table. The cell range is derived from the cells at position and anchor. Returns an error if the cursor is not inside a table or position and anchor are in different tables.

Source

pub fn split_current_cell( &self, split_rows: usize, split_columns: usize, ) -> Result<()>

Split the cell at the cursor’s current position. Returns an error if the cursor is not inside a table.

Source

pub fn set_current_table_format(&self, format: &TableFormat) -> Result<()>

Set formatting on the table the cursor is currently inside. Returns an error if the cursor is not inside a table.

Source

pub fn set_current_cell_format(&self, format: &CellFormat) -> Result<()>

Set formatting on the cell the cursor is currently inside. Returns an error if the cursor is not inside a table.

Source

pub fn selection_kind(&self) -> SelectionKind

Determine the kind of selection the cursor currently has.

Returns Cells when position and anchor are in different cells of the same table (rectangular cell selection), or when an explicit cell-selection override is active.

Source

pub fn is_cell_selection(&self) -> bool

Returns true when the current selection involves whole-cell selection.

Source

pub fn selected_cell_range(&self) -> Option<CellRange>

Returns the rectangular cell range if the cursor has a cell selection.

Source

pub fn selected_cells(&self) -> Vec<TableCellRef>

Returns all cells in the selected rectangular range.

Source

pub fn select_table_cell(&self, table_id: usize, row: usize, col: usize)

Set an explicit single-cell selection override.

Source

pub fn select_cell_range( &self, table_id: usize, start_row: usize, start_col: usize, end_row: usize, end_col: usize, )

Set an explicit rectangular cell-range selection override.

Source

pub fn clear_cell_selection(&self)

Clear any cell-selection override without changing position/anchor.

Source

pub fn delete_char(&self) -> Result<()>

Delete the character after the cursor (Delete key).

Source

pub fn delete_previous_char(&self) -> Result<()>

Delete the character before the cursor (Backspace key).

Source

pub fn remove_selected_text(&self) -> Result<String>

Delete the selected text. Returns the deleted text. No-op if no selection.

Source

pub fn current_list(&self) -> Option<TextList>

Returns the list that the block at the cursor position belongs to, or None if the current block is not a list item.

Source

pub fn create_list(&self, style: ListStyle) -> Result<()>

Turn the block(s) in the selection into a list.

Source

pub fn insert_list(&self, style: ListStyle) -> Result<()>

Insert a new list item at the cursor position.

Source

pub fn set_list_format(&self, list_id: usize, format: &ListFormat) -> Result<()>

Set formatting on a list by its ID.

Source

pub fn set_current_list_format(&self, format: &ListFormat) -> Result<()>

Set formatting on the list that the current block belongs to. Returns an error if the cursor is not inside a list item.

Source

pub fn add_block_to_list(&self, block_id: usize, list_id: usize) -> Result<()>

Add a block to a list by their IDs.

Source

pub fn add_current_block_to_list(&self, list_id: usize) -> Result<()>

Add the block at the cursor position to a list.

Source

pub fn remove_block_from_list(&self, block_id: usize) -> Result<()>

Remove a block from its list by block ID.

Source

pub fn remove_current_block_from_list(&self) -> Result<()>

Remove the block at the cursor position from its list. Returns an error if the current block is not a list item.

Source

pub fn remove_list_item(&self, list_id: usize, index: usize) -> Result<()>

Remove a list item by index within the list. Resolves the index to a block, then removes it from the list.

Source

pub fn char_format(&self) -> Result<TextFormat>

Get the character format at the cursor position. Reads the covering FormatRun (or image anchor) directly from the store.

Source

pub fn block_format(&self) -> Result<BlockFormat>

Get the block format of the block containing the cursor.

Source

pub fn set_char_format(&self, format: &TextFormat) -> Result<()>

Set the character format for the selection.

Source

pub fn merge_char_format(&self, format: &TextFormat) -> Result<()>

Merge a character format into the selection.

Source

pub fn set_block_format(&self, format: &BlockFormat) -> Result<()>

Set the block format for the current block (or all blocks in selection).

Source

pub fn set_frame_format( &self, frame_id: usize, format: &FrameFormat, ) -> Result<()>

Set the frame format.

Source

pub fn begin_edit_block(&self)

Begin a group of operations that will be undone as a single unit.

Source

pub fn end_edit_block(&self)

End the current edit block.

Source

pub fn join_previous_edit_block(&self)

Alias for begin_edit_block.

Semantically indicates that the new composite should be merged with the previous one (e.g., consecutive keystrokes grouped into a single undo unit). The current backend treats this identically to begin_edit_block; future versions may implement automatic merging.

Trait Implementations§

Source§

impl Clone for TextCursor

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.