Skip to content

Instantly share code, notes, and snippets.

@keimina
Created November 13, 2013 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keimina/7449677 to your computer and use it in GitHub Desktop.
Save keimina/7449677 to your computer and use it in GitHub Desktop.
Delete_rect
# -*- 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 not equal.\n'
'StartColNum : %s\nEndColNum : %s'%(scol,ecol))
return
lines = text.splitlines(True)
newlines = u''
deleted = 0
if scol > ecol:
scol, ecol = ecol, scol
s = s - (ecol - scol)
e = e + (ecol - scol)
for line in lines[srow-1:erow]:
if scol >= len(line):
newlines += line
pass
else:
newlines += line[:scol] + line[ecol:-1] + '\n'
deleted += len(line[:-1][scol:ecol])
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 - deleted)
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