/* * ChangeDirectory.bsh - a BeanShell macro script for the * jEdit text editor - Changes working directory of console's plugin * system shell and/or beanshell to current buffer's directory. * * Copyright (C) 2006-2013 Robert Schwenn * * There must be set at least one of two boolean properties (manually) to control the macro: * - macro.changeDirectory.ChangeBeanshell * - macro.changeDirectory.ChangeConsole * * * 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. * * $Id$ */ /* Returns the current directory of the current System shell of the current Console. Originally by Alan Ezust */ getCurrentSystemDir() { // To access non-public members setAccessibility(true); // Obtain the console instance consoleDockable = view.getDockableWindowManager().getDockable("console"); if (consoleDockable == null) { // Add dockable because it wasn't found => The dockable will popup. //Macros.message(view, "Add dockable because it wasn't found"); view.getDockableWindowManager().addDockableWindow("console"); consoleDockable = view.getDockableWindowManager().getDockable("console"); // Close Console's dockable, that should have been opened. jEdit.getAction("console-toggle").invoke(view); } sysShell = console.Shell.getShell("System"); if (sysShell.getConsoleState(consoleDockable) == null) { currentDirectory = java.lang.System.getProperty("user.dir"); } else { currentDirectory = sysShell.getConsoleState(consoleDockable).currentDirectory; } return currentDirectory + File.separator; } /* Sets the current directory of the current System shell of the current Console. */ setCurrentSystemDir(String targetDir) { // To access non-public members setAccessibility(true); // Obtain the console instance consoleDockable = wm.getDockable("console"); if (consoleDockable == null) { // Add dockable because it wasn't found => The dockable will popup. view.getDockableWindowManager().addDockableWindow("console"); consoleDockable = view.getDockableWindowManager().getDockable("console"); // Close Console's dockable, that should have been opened. jEdit.getAction("console-toggle").invoke(view); } // Obtain the system shell and it's state sysShell = console.Shell.getShell("System"); sysShellState = consoleDockable.getShellState(sysShell); // Execute the cd command cmd = "cd \"" + targetDir + "\""; sysShell.execute(consoleDockable, null, sysShellState, null, cmd); // Acknowledge the new working dir at the promt sysShellState.print(consoleDockable.getPlainColor(), ""); sysShell.printPrompt(consoleDockable, sysShellState); } /* Manage the Working Directory */ void changeDirectory() { // Implement the runnable interface. void run() { try { // New buffers don't have a directory if (! buffer.isNewFile()) { // Get settings. ChangeBeanshell = Boolean.parseBoolean(jEdit.getProperty("macro.changeDirectory.ChangeBeanshell")); ChangeConsole = Boolean.parseBoolean(jEdit.getProperty("macro.changeDirectory.ChangeConsole")); // Get info. targetDir = buffer.getDirectory(); //path = buffer.getPath(); //os = System.getProperty("os.name"); if (ChangeBeanshell) { // Working directory of Beanshell (not sure if working well). cd(targetDir); } if (ChangeConsole) { // Working directory of console's system shell. // Macros.message(view, "targetDir=" + targetDir + "; CurrentSystemDir=" + getCurrentSystemDir()); if (! targetDir.equals(getCurrentSystemDir())) { setCurrentSystemDir(targetDir); //view.getStatus().setMessageAndClear("Changed Console's System Directory to: " + targetDir); } } } } catch (exception) {} } // Manage startup/nonstartup script if (jEdit.getLastView() == null) { VFSManager.runInAWTThread(this); } else { run(); } } changeDirectory(); /* ChangeDirectory.bsh Changes working directory of console's plugin system shell and/or beanshell to current buffer's directory. */ // :folding=indent::collapseFolds=1: