如何在 Gedit 插件中获取当前行的长度(或行尾的偏移量)
在用 Python 编写的 Gedit 插件中,我可以获取当前行开头的偏移量,
document = window.get_active_document()
offset = document.get_iter_at_mark(document.get_insert())
如何获取同一行末尾的偏移量?现在我正在使用一种解决方法:获取下一行的偏移量并从中减去所需行的偏移量,然后减去 1(对最后一行进行特殊情况处理)。有更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道有点晚了,但迟到总比不到好。我正在运行 gedit 3.2.3,我不知道这些东西从一个版本到另一个版本有多少变化,但这对我有用:
编辑 1:没有考虑到文件没有被换行符终止的情况
编辑2:考虑行尾的不同定义
PS:无论这个解决方案还是您当前的解决方案是“更干净”还是“更好”,我不知道,我想这是主观的。
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:
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.