/* * IntelliWrap.bsh - Shoban Jayaraj * Version 0.3 * * Intelligent wrap - attempts to wrap selected text intelligently. If there are * any prefix characters before any comments and sentences, this macro wraps the * text by appending the prefix for all lines. Useful if writing multiline * comments. Also takes max line lenght into account. * * There are two modes of operation. When mode is set to '0', the algorithm * identifies the prefix of the first line of text and uses it for all the rest * of the lines. * * When mode is set to 1, the algorithm looks at all the lines and tries to * identify the minimal prefix string it can use. * * If it encounters the same prefix at the beginning of any of the * other lines, it alligns the prefix to match the first line * * Licencse * -------- * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place - Suite 330, Boston, MA 02111-1307, USA. * * Changes * ------- * 0.1 (30/Mar/2004) - Initial release * 0.2 (31/Mar/2004) - Added proper comment headers * 0.3 (05/Apr/2004) - Fixed problem of macro working in terms on lines * instead of selection * Added a more efficient prefix detection method. */ import java.util.*; int mode = 0; // Mode = 1: Look at all lines and detrermine the prefix char. // Mode = 0: Look at the first line and determines the prefix and // uses it on other lines arr = textArea.getSelectedLines(); if(arr == null) { Macro.error(view, "No text selected"); return; } // check to see if the user's chosen consicutive lines int start = arr[0]; Selection[] selection = textArea.getSelection(); if(selection == null || selection.length > 1) { Macro.message(view, "Cannot wrap non-continuous selections"); return; } // get prefix char String nonPrefix = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; int prefix_len = 10000; String prefix = null; if(mode == 1) { for(i=0; i c) { prefix_len = c; prefix = line.substring(0, c); } } } } } else if(mode == 0) { String line = null; i=0; Selection[] sel = textArea.getSelection(); line = textArea.getLineText(arr[i]).substring(sel[0].getStart(buffer, arr[i])-textArea.getLineStartOffset(arr[i]), sel[0].getEnd(buffer, arr[i])-textArea.getLineStartOffset(arr[i])); if(line != null && line.length() != 0) { int c = 0; int len = line.length(); while(c < len) { if(nonPrefix.indexOf(line.charAt(c)) == -1) c++; else break; } if(c != len) { prefix_len = c; prefix = line.substring(0, c); } } } if(prefix_len == 10000) { // cant find prefix return; } /* String firstLine = textArea.getLineText(arr[0]); String prefix = firstLine.substring(0, prefix_len); */ // Now form the wrapped text int line_len = 0; int limit = buffer.getMode().getProperty("maxLineLen"); StringBuffer buff = new StringBuffer(prefix); line_len += prefix_len; for(i=0; i 0) { t = line.substring(c); } } StringTokenizer st = new StringTokenizer(t, " \t"); while(st.hasMoreTokens()) { token = st.nextToken(); if(line_len != prefix_len && line_len + token.length() > limit) { buff.append("\n"); buff.append(prefix); line_len = prefix_len; } buff.append(token); buff.append(" "); line_len += token.length() + 1; } } textArea.setSelectedText(buff.toString());