Reputation: 27050
In a Gedit plugin written in Python, I can get the offset of the beginning of the current line with
document = window.get_active_document()
offset = document.get_iter_at_mark(document.get_insert())
How could I get the offset of the end of this same line? For now I am using a workaround: I get the offset of the next line and subtract the offset of the desired line from it, and subtract 1 (with an special case treated for the last line). Is there a better way of doing it?
Upvotes: 3
Views: 283
Reputation: 3144
A bit late, I know, but better late than never. I am running gedit 3.2.3 and I don't know how much these things change from one version to another, but this works for me:
line_types = {"cr-lf": '\r\n',
"lf": '\n',
"cr": '\r'}
document = window.get_active_document()
newline = line_types[document.get_newline_type().value_nick]
insert_mark = document.get_insert()
offset = document.get_iter_at_mark(insert_mark)
line = offset.get_line()
# we subtract 1 because get_chars_in_line() counts the newline character
# there is a special case with the last line if it doesn't have a newline at the end
# we deal with that later
line_length = offset.get_chars_in_line() - len(newline)
end_of_line = document.get_iter_at_line_offset(line, line_length)
if end_of_line.get_char() != newline[0]:
end_of_line = document.get_iter_at_offset(end_of_line.get_offset()+len(newline))
# if the above code is correct this should select from the current insert point
# to the end of line
document.move_mark(insert_mark, end_of_line)
Edit 1: Was not accounting for the case where the file wasn't terminated by a newline character
Edit 2: Account for different definitions of the end of line
PS: Whether this or your currently solution is "cleaner" or "better", I don't know, I guess that's subjective.
Upvotes: 1