aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkristoffer.lunden <kristoffer.lunden@dd23f896-6e2b-0410-bc3e-d184d5eb269a>2010-10-04 16:39:04 (GMT)
committerkristoffer.lunden <kristoffer.lunden@dd23f896-6e2b-0410-bc3e-d184d5eb269a>2010-10-04 16:39:04 (GMT)
commit0da28ea5679f653ec278418eac87dbc5234397e7 (patch)
tree3719e9ba2a36b5d4f0a2cce294eca27953b77cc8
parent4db806f75c0fb5e19d389465962b2ebcf96546fc (diff)
downloadgedit-autotab-0da28ea5679f653ec278418eac87dbc5234397e7.zip
gedit-autotab-0da28ea5679f653ec278418eac87dbc5234397e7.tar.gz
gedit-autotab-0da28ea5679f653ec278418eac87dbc5234397e7.tar.bz2
formatted paste, not perfected
git-svn-id: http://gedit-autotab.googlecode.com/svn/trunk@31 dd23f896-6e2b-0410-bc3e-d184d5eb269a
-rw-r--r--autotab.py73
1 files changed, 67 insertions, 6 deletions
diff --git a/autotab.py b/autotab.py
index 52a771c..7daddfe 100644
--- a/autotab.py
+++ b/autotab.py
@@ -134,17 +134,78 @@ class AutoTab(gedit.Plugin):
if text is None:
# nothing on clipboard
return
+
+ # start and end of selection, or the same if no selection
+ start_iter = doc.get_iter_at_mark(doc.get_insert())
+ end_iter = doc.get_iter_at_mark(doc.get_selection_bound())
+
+ # the line above and below selection/cursor position
+ start_line = start_iter.get_line()
+ end_line = end_iter.get_line()
+ if start_line > 0:
+ start_line -= 1
+ if end_line < doc.get_line_count() - 1:
+ end_line += 1
+
+ line_iter = doc.get_iter_at_line(start_iter.get_line())
- iter = doc.get_iter_at_mark(doc.get_insert())
+ before_iter = doc.get_iter_at_line(start_line)
+ after_iter = doc.get_iter_at_line(end_line)
+
+ space = view.get_insert_spaces_instead_of_tabs()
+ size = view.get_tab_width()
+ if space:
+ tab = " "
+ else:
+ tab = "\t"
+
+ while line_iter.get_char() == tab:
+ line_iter.forward_char()
+ while before_iter.get_char() == tab:
+ before_iter.forward_char()
+
+ # pick the line, before or after with the most indent:
+ #indent = max(line_iter.get_line_offset(), before_iter.get_line_offset(), after_iter.get_line_offset())
+ indent = max(line_iter.get_line_offset(), before_iter.get_line_offset())
+
+ # check the position we are pasting on, to see if we are inside non-whitespace
+ # if so, assume position is already correct and do not paste
+ text_before_paste = doc.get_text(line_iter, start_iter)
+ inside_line = len(text_before_paste.translate(None, " \t")) > 0
+ if not inside_line:
+ doc.delete(line_iter, start_iter)
+ doc.delete_selection(False, True)
+
+ # this rounds to multiple of the tab settings, if needed
+ # ie if indent is 7 and the tab size is 2, it will go to 6 instead
+ if(space):
+ indent /= size
- # just testing that we are here
- # text = '*****' + text
+ lines = text.splitlines(True)
doc.begin_user_action()
- doc.delete_selection(False, True)
- doc.insert_at_cursor(text)
- doc.end_user_action()
+
+ last_line_indent = -1
+ for line in lines:
+ for line_indent in range(0, len(line)):
+ if line[line_indent] != tab:
+ break
+
+ if last_line_indent != -1: # not first line
+ if line_indent > last_line_indent:
+ indent += 1
+ elif line_indent < last_line_indent:
+ indent -= 1
+
+ prefix = tab * indent * size
+
+ if inside_line and last_line_indent == -1: # first line
+ doc.insert_at_cursor(line)
+ else:
+ doc.insert_at_cursor(prefix + line.lstrip())
+ last_line_indent = line_indent
+ doc.end_user_action()
view.scroll_mark_onscreen(doc.get_insert())
# If default tab size changes