-package jp.sourceforge.moreemacs.handlers;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import jp.sourceforge.moreemacs.utils.CodePointIterator;\r
-import jp.sourceforge.moreemacs.utils.ColumnUtils;\r
-import jp.sourceforge.moreemacs.utils.DocumentCharSequence;\r
-import jp.sourceforge.moreemacs.utils.DocumentTransaction;\r
-\r
-import org.eclipse.jface.text.BadLocationException;\r
-import org.eclipse.jface.text.IDocument;\r
-import org.eclipse.jface.text.IRegion;\r
-import org.eclipse.jface.text.ITextSelection;\r
-\r
-public final class KillRectangleExecution extends TextEditorExecution {\r
-\r
- @Override\r
- public void execute() throws BadLocationException {\r
-\r
- if(!textEditor.isEditable()) {\r
- return;\r
- }\r
- \r
- ITextSelection selection = getSelection(true);\r
-\r
- int start = selection.getOffset();\r
- int startRow = doc.getLineOfOffset(start);\r
- int startColumn = ColumnUtils.getColumn(doc, start, getTabStop());\r
-\r
- int end = start + selection.getLength();\r
- int endRow = doc.getLineOfOffset(end);\r
- int endColumn = ColumnUtils.getColumn(doc, end, getTabStop());\r
- \r
- if(startColumn > endColumn) {\r
- int work = startColumn;\r
- startColumn = endColumn;\r
- endColumn = work;\r
- }\r
-\r
- int offset = cursor.offset();\r
- DocumentTransaction transaction = new DocumentTransaction(doc); \r
- transaction.begin();\r
- try {\r
- List<String> rectangle = new ArrayList<String>();\r
- offset = killRectangle(doc, startRow, startColumn, endRow, endColumn, rectangle);\r
- RectangleStorage.setRectangle(rectangle);\r
- } finally {\r
- transaction.end();\r
- }\r
- cursor.move(offset);\r
- }\r
- private int killRectangle(IDocument doc,\r
- int startRow, int startColumn,\r
- int endRow, int endColumn, List<String> rectangle)\r
- throws BadLocationException {\r
-\r
-\r
- int result = cursor.offset();\r
- for(int i = startRow; i <= endRow; i++) {\r
- result = killString(doc, i, startColumn, endColumn, rectangle);\r
- }\r
- return result;\r
- }\r
- private int killString(IDocument doc, int row,\r
- int startColumn, int endColumn, List<String> rectangle) throws BadLocationException {\r
- IRegion line = doc.getLineInformation(row);\r
-\r
- StringBuilder builder = new StringBuilder();\r
- int column = 0;\r
- int cutOffset = 0;\r
- int cutLength = 0;\r
- \r
- CharSequence seq = new DocumentCharSequence(doc, line.getOffset(), line.getLength());\r
- \r
- for(CodePointIterator itr = new CodePointIterator(seq); itr.hasNext(); ) {\r
- if(column >= endColumn) {\r
- break;\r
- }\r
- int offset = line.getOffset() + itr.index();\r
- int codePoint = itr.next();\r
-\r
- int nextColumn = ColumnUtils.getNextColumn(column, codePoint, getTabStop());\r
- \r
- if(nextColumn < startColumn+1) {\r
- column = nextColumn;\r
- continue;\r
- }\r
- if(cutLength == 0) { \r
- cutOffset = offset;\r
- }\r
- builder.appendCodePoint(codePoint);\r
- cutLength += Character.charCount(codePoint);\r
- column = nextColumn;\r
- }\r
- \r
- doc.replace(cutOffset, cutLength, "");\r
-\r
- \r
- for(int i = 0; i < endColumn-column; i++) {\r
- builder.append(' ');\r
- }\r
- \r
- rectangle.add(builder.toString());\r
- return cutOffset;\r
- }\r
-\r
-}\r
+package jp.sourceforge.moreemacs.handlers;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import jp.sourceforge.moreemacs.utils.CodePointIterator;
+import jp.sourceforge.moreemacs.utils.ColumnUtils;
+import jp.sourceforge.moreemacs.utils.DocumentCharSequence;
+import jp.sourceforge.moreemacs.utils.DocumentTransaction;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextSelection;
+
+public final class KillRectangleExecution extends TextEditorExecution {
+
+ @Override
+ public void execute() throws BadLocationException {
+
+ if(!textEditor.isEditable()) {
+ return;
+ }
+
+ ITextSelection selection = getSelection(true);
+
+ int start = selection.getOffset();
+ int startRow = doc.getLineOfOffset(start);
+ int startColumn = ColumnUtils.getColumn(doc, start, getTabStop());
+
+ int end = start + selection.getLength();
+ int endRow = doc.getLineOfOffset(end);
+ int endColumn = ColumnUtils.getColumn(doc, end, getTabStop());
+
+ if(startColumn > endColumn) {
+ int work = startColumn;
+ startColumn = endColumn;
+ endColumn = work;
+ }
+
+ int offset = cursor.offset();
+ DocumentTransaction transaction = new DocumentTransaction(doc);
+ transaction.begin();
+ try {
+ List<String> rectangle = new ArrayList<String>();
+ offset = killRectangle(doc, startRow, startColumn, endRow, endColumn, rectangle);
+ RectangleStorage.setRectangle(rectangle);
+ } finally {
+ transaction.end();
+ }
+ cursor.move(offset);
+ }
+ private int killRectangle(IDocument doc,
+ int startRow, int startColumn,
+ int endRow, int endColumn, List<String> rectangle)
+ throws BadLocationException {
+
+
+ int result = cursor.offset();
+ for(int i = startRow; i <= endRow; i++) {
+ result = killString(doc, i, startColumn, endColumn, rectangle);
+ }
+ return result;
+ }
+ private int killString(IDocument doc, int row,
+ int startColumn, int endColumn, List<String> rectangle) throws BadLocationException {
+ IRegion line = doc.getLineInformation(row);
+
+ StringBuilder builder = new StringBuilder();
+ int column = 0;
+ int cutOffset = 0;
+ int cutLength = 0;
+
+ CharSequence seq = new DocumentCharSequence(doc, line.getOffset(), line.getLength());
+
+ for(CodePointIterator itr = new CodePointIterator(seq); itr.hasNext(); ) {
+ if(column >= endColumn) {
+ break;
+ }
+ int offset = line.getOffset() + itr.index();
+ int codePoint = itr.next();
+
+ int nextColumn = ColumnUtils.getNextColumn(column, codePoint, getTabStop());
+
+ if(nextColumn < startColumn+1) {
+ column = nextColumn;
+ continue;
+ }
+ if(cutLength == 0) {
+ cutOffset = offset;
+ }
+ builder.appendCodePoint(codePoint);
+ cutLength += Character.charCount(codePoint);
+ column = nextColumn;
+ }
+
+ doc.replace(cutOffset, cutLength, "");
+
+
+ for(int i = 0; i < endColumn-column; i++) {
+ builder.append(' ');
+ }
+
+ rectangle.add(builder.toString());
+ return cutOffset;
+ }
+
+}