Last active
December 27, 2015 22:19
-
-
Save keimina/7398672 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import editor | |
import console | |
def main(): | |
text = editor.get_text().decode('utf-8') | |
s,e = editor.get_selection() | |
srow, scol = get_row_col(s,text) | |
erow, ecol = get_row_col(e,text) | |
if scol != ecol: | |
console.alert('Column number mismatch.', 'Both start column number and end column number have to be equal.\n' | |
'StartColNum : %s\nEndColNum : %s'%(scol,ecol)) | |
return | |
insert_str = console.input_alert('Enter insert string') | |
lines = text.splitlines(True) | |
col = scol or ecol | |
newlines = u'' | |
inserted = 0 | |
for line in lines[srow-1:erow]: | |
if col > len(line): | |
newlines += line | |
pass | |
else: | |
newlines += line[:col] + insert_str + line[col:] | |
inserted += 1 | |
srow_chr_index, erow_chr_index = count_chr_to_row(srow, erow, text) | |
editor.replace_text(srow_chr_index, erow_chr_index, newlines) | |
editor.set_selection(s, e + len(insert_str)*inserted) | |
def get_row_col(num,text): | |
lines = text.splitlines(True) | |
sum = 0 | |
row = 0 | |
col = 0 | |
for line in lines: | |
sum += len(line) | |
row += 1 | |
if sum > num: | |
break | |
sum = 0 | |
for line in lines[:row-1]: | |
sum += len(line) | |
col = num - sum | |
return row, col | |
def count_chr_to_row(srow, erow, text): | |
lines = text.splitlines(True) | |
sum_for_srow = 0 | |
sum_for_erow = 0 | |
for line in lines[:srow-1]: | |
sum_for_srow += len(line) | |
for line in lines[:erow]: | |
sum_for_erow += len(line) | |
return sum_for_srow, sum_for_erow | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment