/* * Select_Word_or_Enclosed.bsh * a BeanShell macro for jEdit * by Tomek Pêszor * tomek at taat dot pl * * version 1.0 * * This macro is ment for fast selecting text with one keyboard shortcut. * It selects word when no selection is set. * When selection is set (second run), it looks for pairs of characters like parenthesis ()[]{}<> or quotations "" '' or line separator and selects text beetween. * Just assign shortcut to this macro (I use CTRL+ALT+UP ARROW) and you are able to select word no matter when caret is on the begginig, inside or at the end of word. * Then hit this keyboard shortcut again to select text between characters described above. * Hit once more to extend selection to select enclosing characters too, and once more to expand selection to next pairs. * This macro useful for selecting words, syntax keywords, strings, function values, HTML attribute values and many more. * See it in action on this screencast: http://youtube.com/watch?v=TxCuLjFrFxs * * 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 the jEdit program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ /** * function in_array * checks if string is in array * returns -1 if not, array key if any */ int in_array(String[] arr, String str) { for (int i=0;i"}; String[] closings = {"\n", "\"", "'", "%", ")", "]", "}", ">", "<"}; String[] notwords = { ",", ".", "?", "!", "'", "\"", "{", "}", "[", "]", "(", ")", " ", "\n", ":" , ";", "$", "%", "<", ">", "=", "\\", "/" }; fc=fo=-1; cp=textArea.getCaretPosition(); if (cp==buffer.getLength() || cp==0) return; if (textArea.getSelectionCount()<1) { // if // version one, keep moving back until skip all non-words // begin version one // better, but hangs, so far /* if ((in_array(notwords,textArea.getText(cp-1,1))>=0 && in_array(notwords,textArea.getText(cp ,1))>=0) return; // if previous character is opening if (in_array(openings,textArea.getText(cp-1,1))>=0) { do { textArea.goToNextCharacter(false); } while (in_array(openings,textArea.getText(cp,1))>=0); } else { do { // exclude single characters, which are not words textArea.goToPrevCharacter(false); } while (in_array(notwords, textArea.getText(cp,1))>=0); }*/ // end version one // version two - move back to skip only current non-word // begin version two if (in_array(notwords, textArea.getText(cp,1))>=0) textArea.goToPrevCharacter(false); // end version two // if nothing selected - select current word textArea.selectWord(); } else { // if selection found // check opening and closing characters sel=textArea.getSelection(0); selStart=sel.getStart(); selEnd=sel.getEnd(); if (selStart<=0) return; if (selEnd>=buffer.getLength()) return; chStart=textArea.getText(selStart-1,1); chEnd=textArea.getText(selEnd,1); fo=in_array(openings, chStart); fc=in_array(closings, chEnd); // if selected inside - select enclosing too if (fc>-1 && fo>-1) { textArea.select(selStart-1, selEnd+1); } else { // if not selected inside i=-1; do { i++; chStart=textArea.getText(selStart-1-i,1); chEnd=textArea.getText(selEnd+i,1); fo=in_array(openings,chStart); fc=in_array(closings,chEnd); } while (fo<0 && fc<0); if (fo>-1) { // if found opening start=selStart-i; do { if (selEnd+i==buffer.getLength()) break; /*if (selEnd+i==buffer.getLength()-1) return;*/ chEnd=textArea.getText(selEnd+i,1); i++; } while (!chEnd.equals(closings[fo])); textArea.select(start, selEnd+i-1); } else if (fc>-1) { // if found closing end=selEnd+i; do { chStart=textArea.getText(selStart-i-1, 1); i++; } while (!chStart.equals(openings[fc])); textArea.select(selStart-i+1, end); } } } /* Macro index data (in DocBook format) Select_Word_or_Enclosed.bsh This macro is ment for fast selecting text with one keyboard shortcut. It selects word when no selection is set. When selection is set (second run), it looks outside selection for pairs of characters like parenthesis ()[]{}<> or quotations "" '' or line separator and selects text beetween. Just assign shortcut to this macro (I use CTRL+ALT+UP ARROW) and you are able to select word no matter when caret is on the begginig, inside or at the end of word. Then hit this keyboard shortcut again to select text between characters described above. Hit once more to extend selection to select enclosing characters too, and once more to expand selection to next pairs. This macro useful for selecting words, syntax keywords, strings, function values, HTML attribute values and many more. See it in action on this screencast: http://youtube.com/watch?v=TxCuLjFrFxs */