/* * Shuffle_Lines.bsh - a BeanShell macro script for shuffling the order * of lines in a buffer. If there are selections, the lines in the selections * will be shuffled, otherwise the whole buffer will be shuffled. * * Copyright (C) 2005 DreamTangerine, dreamtangerine@hotmail.com * * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false: * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1: * * $Id: Suffle_Lines.bsh,v 1.0 2005/10/06 15:58:00 dremtangerine Exp $ */ void suffleLines(buffer, startLine, endLine) { int startOffset = buffer.getLineStartOffset(startLine); int endOffset = buffer.getLineEndOffset(endLine); int nLines = endLine - startLine + 1 ; String[] lines = new String[nLines] ; for (int i = 0 ; i < nLines ; ++i) lines[i] = buffer.getLineText(startLine + i) ; Collections.shuffle(Arrays.asList(lines)) ; buffer.remove(startOffset, endOffset - startOffset - 1) ; for(int i = 0 ; i < nLines - 1; ++i) { buffer.insert(startOffset, lines[i] + "\n") ; startLine++ ; startOffset = buffer.getLineStartOffset(startLine) ; } buffer.insert(startOffset, lines[nLines - 1]); } void suffleLines(textArea) { Buffer buffer = textArea.getBuffer(); Selection[] selections = textArea.getSelection(); // doesn't work with rectangular selections, check for them up-front for(int i = 0; i < selections.length; i++) { if(selections[i] instanceof Selection.Rect) { Macros.error(view, "Sorry, this macro doesn't work with Rectangular Selections."); return; } } buffer.beginCompoundEdit(); // do whole buffer if no selections if(selections.length == 0) { suffleLines(buffer, 0, buffer.getLineCount() - 1) ; } // shuffle all lines that are selected *NOT* just selected portions of lines else { for(int i = 0; i < selections.length; i++) { int startLine = selections[i].getStartLine(); int endLine = selections[i].getEndLine(); suffleLines(buffer, startLine, endLine) ; } } buffer.endCompoundEdit(); } if(buffer.isReadOnly()) Macros.error(view, "Buffer is read-only."); else suffleLines(textArea); /* Macro index entry (in DocBook XML) Shuffle_Lines.bsh Shuffle the selected lines or the entire buffer if no lines are selected. Does not support Rectangular Selections. */