/* * Open_Selection_with_Extension.bsh - a BeanShell macro script for the * jEdit text editor - opens file which base name is the selected text and with * the same extension - creates the file if it does not exist * Copyright (C) 2005-2009 Gael Ecorchard * * 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. * * inspired from Open_Selection.bsh from Slava Pestov * * v 1.0 */ void openSelection() { Selection[] selection = textArea.getSelection(); if(selection == null) view.getToolkit().beep(); else { Buffer buffer = textArea.getBuffer(); String path = buffer.getPath(); String extension = getExtension(path); for(i = 0; i < selection.length; i++) { if(extension.equals("")) { jEdit.openFile(view,textArea.getSelectedText(selection[i])); } else { jEdit.openFile(view,textArea.getSelectedText(selection[i])+ "." +extension); } } } } String getExtension(String path) { // could be simplified with the use of path.lastIndexOf(".") // and extention.reverse() int len = path.length(); int i=(len-1); StringBuffer extension = new StringBuffer(len); StringBuffer tmp = new StringBuffer(len); do { tmp.append(path.charAt(i).toString()); i--; } while(path.charAt(i)!='.' && i>=0 && path.charAt(i)!='\\' && path.charAt(i)!='/'); if (path.charAt(i)=='.') { len = tmp.length(); for (int i = (len - 1); i >= 0; i--) extension.append(tmp.charAt(i).toString()); } if(extension==null) return(""); else return(extension.toString()); } openSelection(); /* Macro index data (in DocBook format) Open_Selection_with_Extension.bsh Opens the file which base name is the selected text and which extension is identical to the one of the file where the text is selected - creates the file if it does not exist. */ // end Open_Selection_with_Extension.bsh