Page MenuHome

Text Editor (python): Add Text.selection_set()
AbandonedPublic

Authored by Kai Jægersen (kaio) on Tue, Sep 24, 11:09 AM.

Details

Summary

Adds function Text.selection_set() so it's possible to set text selection through python api.

Function takes four arguments: start line, start character, end line, end character

e.g:

import bpy

text = bpy.context.space_data.text

endl = 2
endc = 10

text.selection_set(0, 0, endl, endc)

selects all text from the beginning of line 1 to the 10th character at line 3.

Reverse selection also possible.

Added checks to ensure given range is valid. Line lengths are measured using BLI_strlen_utf8.

When a char index passed is lower than 0, the rna function automatically clamps at 0.
When a char index passed is higher than the length of a line, clamp at line length.

Diff Detail

Event Timeline

so now it would be possible to get back selection to what it was before the last undo using this

This patch has the issue that indices can be between unicode characters.

See comments for D1613: Add set selection function to text api

Campbell Barton (campbellbarton) requested changes to this revision.Mon, Sep 30, 7:04 PM
This revision now requires changes to proceed.Mon, Sep 30, 7:04 PM
Kai Jægersen (kaio) added a comment.EditedMon, Sep 30, 8:41 PM

This patch has the issue that indices can be between unicode characters.
See comments for D1613: Add set selection function to text api

Thanks. If I'm not misunderstanding that's not an issue to be solved here. See https://developer.blender.org/T65843.

I could measure and account for the unicode size, but given that those who would use low-level selection take their character indices from
current_character and select_end_character, which already report wrong values, the selection would never match up.

For this to be a viable solution the said properties would also require to be correct.

Edit:
I just re-read the comments on the link you posted.

Looks like I'll be making another patch first :P

Right, all properties should use the character index not the byte offset.