1 package jp.sourceforge.moreemacs.handlers;
5 import jp.sourceforge.moreemacs.utils.CodePointIterator;
6 import jp.sourceforge.moreemacs.utils.ColumnUtils;
7 import jp.sourceforge.moreemacs.utils.DocumentCharSequence;
8 import jp.sourceforge.moreemacs.utils.DocumentTransaction;
10 import org.eclipse.jface.text.BadLocationException;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.jface.text.IRegion;
13 import org.eclipse.jface.text.TextUtilities;
15 public final class YankRectangleExecution extends TextEditorExecution {
17 public void execute() throws BadLocationException {
18 if(!textEditor.isEditable()) {
22 List<String> rectangle = RectangleStorage.getRectangle();
23 if(rectangle == null) {
27 int current = cursor.offset();
28 int row = doc.getLineOfOffset(current);
29 int column = ColumnUtils.getColumn(doc, current, getTabStop());
32 int offset = cursor.offset();
33 DocumentTransaction transaction = new DocumentTransaction(doc);
36 ensureLines(doc, row + rectangle.size());
37 offset = yankRectangle(doc, row, column, rectangle);
44 private void ensureLines(IDocument doc, int lines) throws BadLocationException {
45 int n = lines - doc.getNumberOfLines();
50 StringBuilder builder = new StringBuilder();
51 String delim = TextUtilities.getDefaultLineDelimiter(doc);
52 for(int i = 0; i < n; i++) {
53 builder.append(delim);
55 doc.replace(doc.getLength(), 0, builder.toString());
59 private int yankRectangle(IDocument doc,
60 int row, int column, List<String> rectangle)
61 throws BadLocationException {
62 int offset = cursor.offset();
63 for(int i = 0; i < rectangle.size(); i++) {
64 offset = yankString(doc, row+i, column, rectangle.get(i));
69 private int yankString(IDocument doc, int row, int column, String str)
70 throws BadLocationException
72 IRegion line = doc.getLineInformation(row);
75 CharSequence seq = new DocumentCharSequence(doc, line.getOffset(), line.getLength());
77 for(CodePointIterator itr = new CodePointIterator(seq); itr.hasNext(); ) {
78 int offset = line.getOffset() + itr.index();
79 int codePoint = itr.next();
81 doc.replace(offset, 0, str);
82 return offset+str.length();
84 col = ColumnUtils.getNextColumn(col, codePoint, getTabStop());
88 StringBuilder builder = new StringBuilder();
89 for(int i = 0; i < column-col; i++) {
93 doc.replace(line.getOffset()+line.getLength(), 0, builder.toString());
94 return line.getOffset()+line.getLength()+builder.length();