IDA Python, get selected text

I want to get the selected text from a given widget. I know about the example dump_selection.py but there are at least 2 major bugs in that code and I have tried to fix it but reading that code is insane. How can there not be a simple ida_kernwin.get_selected_text()?

So the bugs:

  1. First is that the dump_selection do not work if you select text on 1 line only (That one I think I got fixed)
  2. The second bug is way worse… If you select text on 1 line that spans over some special characters (not sure which ones but usually selecting a functions name and arguments do the trick) the dump_selection.py returns an empty string.

Anyone got a fully functional select_text() function that I can use?

Hi,

Could you prepare a small repro where the sample code is not working? And what was your fix for point 1?

Sure, I have made some small video clips showing the problems also.
Clip 1: 5.74 MB file on MEGA
Clip 2: 6.28 MB file on MEGA
zip file with I64 + input files: 6.06 MB file on MEGA

EDIT: My fix (which I don’t really call a fix since it only fixes the problem with text on 1 line) is that I check if the end and start are on the same line and if they are, then I use ida_kernwin.get_custom_viewer_curline() instead

Code here:

Thanks, we reproduced the problem!

1 Like