/* * Squeeze_Empty_Lines.bsh - a BeanShell macro script for the jEdit text editor * Copyright (C) 2003 Alexey Vdovichenko * tequilacat@mail.ru * http://tequilacat.narod.ru * * 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. * * Notes on use: * This macro is an improvement of standard "Join Lines" function. * When there is no selection in buffer, it joins current line and next NON-empty line, * removing any whitespace content between them. * If some text is selected, all empty lines within selected regions are removed. * Checked for jEdit 4.1 API * * Version 1.01: removed annoying debug message box. */ // void squeezeLine(gnu.regexp.RE re, int from, int to){ if(from <= to) { // Macros.error(view, "Squeeze from "+ from +" to "+ to +""); int nLines = to - from + 1; while(nLines > 0){ // Macros.error(view, "line: '"+ textArea.getLineText(from) +"'"); if(re.isMatch(textArea.getLineText(from))){ textArea.setCaretPosition(textArea.getLineStartOffset(from)); textArea.deleteLine(); }else{ from++; } nLines--; } } } void joinLines(gnu.regexp.RE re){ // last non-whitespace, then look from here to first non-whitespace, and delete between textArea.goToEndOfWhiteSpace(false); textArea.deleteToEndOfLine(); // int fromPos = textArea.getCaretPosition(); int curLine = textArea.getCaretLine()+1; while(curLine < textArea.getLineCount()){ textArea.setCaretPosition(textArea.getLineStartOffset(curLine)); if(re.isMatch(textArea.getLineText(curLine))){ // empty line textArea.deleteLine(); }else{ // remove from 1st line offset to first non-whitespace textArea.goToStartOfWhiteSpace(false); textArea.deleteToStartOfLine(); textArea.backspace(); break; } } } void squeeze(){ selections = textArea.getSelection(); re = new gnu.regexp.RE("^[ \t]*$"); if(selections.length == 0){ joinLines(re); //squeezeLine(re, 0, textArea.getLineCount()-2); }else for( i = 0; i < selections.length; ++i){ selItem = selections[i]; squeezeLine(re, selItem.getStartLine(), selItem.getEndLine()); } textArea.selectNone(); } squeeze();