⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanshell.java

📁 用java 编写的源码开放的文本编辑器。有很多有用的特性
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * BeanShell.java - BeanShell scripting support * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 2000, 2001, 2002 Slava Pestov * * 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. */package org.gjt.sp.jedit;//{{{ Importsimport bsh.*;import java.lang.reflect.InvocationTargetException;import java.io.*;import org.gjt.sp.jedit.io.*;import org.gjt.sp.jedit.gui.BeanShellErrorDialog;import org.gjt.sp.jedit.textarea.*;import org.gjt.sp.util.Log;//}}}/** * BeanShell is jEdit's extension language.<p> * * When run from jEdit, BeanShell code has access to the following predefined * variables: * * <ul> * <li><code>view</code> - the currently active {@link View}.</li> * <li><code>editPane</code> - the currently active {@link EditPane}.</li> * <li><code>textArea</code> - the edit pane's {@link JEditTextArea}.</li> * <li><code>buffer</code> - the edit pane's {@link Buffer}.</li> * <li><code>scriptPath</code> - the path name of the currently executing * BeanShell script.</li> * </ul> * * @author Slava Pestov * @version $Id: BeanShell.java,v 1.31 2003/02/15 22:17:55 spestov Exp $ */public class BeanShell{	//{{{ evalSelection() method	/**	 * Evaluates the text selected in the specified text area.	 * @since jEdit 2.7pre2	 */	public static void evalSelection(View view, JEditTextArea textArea)	{		String command = textArea.getSelectedText();		if(command == null)		{			view.getToolkit().beep();			return;		}		Object returnValue = eval(view,global,command);		if(returnValue != null)			textArea.setSelectedText(returnValue.toString());	} //}}}	//{{{ showEvaluateDialog() method	/**	 * Prompts for a BeanShell expression to evaluate.	 * @since jEdit 2.7pre2	 */	public static void showEvaluateDialog(View view)	{		String command = GUIUtilities.input(view,"beanshell-eval-input",null);		if(command != null)		{			if(!command.endsWith(";"))				command = command + ";";			int repeat = view.getInputHandler().getRepeatCount();			if(view.getMacroRecorder() != null)			{				view.getMacroRecorder().record(repeat,command);			}			Object returnValue = null;			try			{				for(int i = 0; i < repeat; i++)				{					returnValue = _eval(view,global,command);				}			}			catch(Throwable e)			{				Log.log(Log.ERROR,BeanShell.class,e);				handleException(view,null,e);			}			if(returnValue != null)			{				String[] args = { returnValue.toString() };				GUIUtilities.message(view,"beanshell-eval",args);			}		}	} //}}}	//{{{ showEvaluateLinesDialog() method	/**	 * Evaluates the specified script for each selected line.	 * @since jEdit 4.0pre1	 */	public static void showEvaluateLinesDialog(View view)	{		String command = GUIUtilities.input(view,"beanshell-eval-line",null);		JEditTextArea textArea = view.getTextArea();		Buffer buffer = view.getBuffer();		if(command == null || command.length() == 0)			return;		Selection[] selection = textArea.getSelection();		if(selection.length == 0)		{			view.getToolkit().beep();			return;		}		if(!command.endsWith(";"))			command = command + ";";		if(view.getMacroRecorder() != null)			view.getMacroRecorder().record(1,command);		try		{			buffer.beginCompoundEdit();			for(int i = 0; i < selection.length; i++)			{				Selection s = selection[i];				for(int j = s.getStartLine(); j <= s.getEndLine(); j++)				{					// if selection ends on the start of a					// line, don't filter that line					if(s.getEnd() == textArea.getLineStartOffset(j))						break;					global.setVariable("line",new Integer(j));					global.setVariable("index",new Integer(						j - s.getStartLine()));					int start = s.getStart(buffer,j);					int end = s.getEnd(buffer,j);					String text = buffer.getText(start,						end - start);					global.setVariable("text",text);					Object returnValue = _eval(view,global,command);					if(returnValue != null)					{						buffer.remove(start,end - start);						buffer.insert(start,							returnValue.toString());					}				}			}		}		catch(Throwable e)		{			Log.log(Log.ERROR,BeanShell.class,e);			handleException(view,null,e);		}		finally		{			buffer.endCompoundEdit();		}		textArea.selectNone();	} //}}}	//{{{ runScript() method	/**	 * Runs a BeanShell script. Errors are shown in a dialog box.<p>	 *	 * If the <code>in</code> parameter is non-null, the script is	 * read from that stream; otherwise it is read from the file identified	 * by <code>path</code>.<p>	 *	 * The <code>scriptPath</code> BeanShell variable is set to the path	 * name of the script.	 *	 * @param view The view. Within the script, references to	 * <code>buffer</code>, <code>textArea</code> and <code>editPane</code>	 * are determined with reference to this parameter.	 * @param path The script file's VFS path.	 * @param in The reader to read the script from, or <code>null</code>.	 * @param ownNamespace If set to <code>false</code>, methods and	 * variables defined in the script will be available to all future	 * uses of BeanShell; if set to <code>true</code>, they will be lost as	 * soon as the script finishes executing. jEdit uses a value of	 * <code>false</code> when running startup scripts, and a value of	 * <code>true</code> when running all other macros.	 *	 * @since jEdit 4.0pre7	 */	public static void runScript(View view, String path, Reader in,		boolean ownNamespace)	{		try		{			_runScript(view,path,in,ownNamespace);		}		catch(Throwable e)		{			Log.log(Log.ERROR,BeanShell.class,e);			handleException(view,path,e);		}	} //}}}	//{{{ _runScript() method	/**	 * Runs a BeanShell script. Errors are passed to the caller.<p>	 *	 * If the <code>in</code> parameter is non-null, the script is	 * read from that stream; otherwise it is read from the file identified	 * by <code>path</code>.<p>	 *	 * The <code>scriptPath</code> BeanShell variable is set to the path	 * name of the script.	 *	 * @param view The view. Within the script, references to	 * <code>buffer</code>, <code>textArea</code> and <code>editPane</code>	 * are determined with reference to this parameter.	 * @param path The script file's VFS path.	 * @param in The reader to read the script from, or <code>null</code>.	 * @param ownNamespace If set to <code>false</code>, methods and	 * variables defined in the script will be available to all future	 * uses of BeanShell; if set to <code>true</code>, they will be lost as	 * soon as the script finishes executing. jEdit uses a value of	 * <code>false</code> when running startup scripts, and a value of	 * <code>true</code> when running all other macros.	 * @exception Exception instances are thrown when various BeanShell errors	 * occur	 * @since jEdit 4.0pre7	 */	public static void _runScript(View view, String path, Reader in,		boolean ownNamespace) throws Exception	{		Log.log(Log.MESSAGE,BeanShell.class,"Running script " + path);		NameSpace namespace;		if(ownNamespace)			namespace = new NameSpace(global,"script namespace");		else			namespace = global;		Interpreter interp = createInterpreter(namespace);		VFS vfs = null;		Object session = null;		try		{			if(in == null)			{				Buffer buffer = jEdit.getBuffer(path);				vfs = VFSManager.getVFSForPath(path);				session = vfs.createVFSSession(path,view);				if(session == null)				{					// user cancelled???					return;				}				if(buffer != null)				{					if(!buffer.isLoaded())						VFSManager.waitForRequests();					in = new StringReader(buffer.getText(0,						buffer.getLength()));				}				else				{					in = new BufferedReader(new InputStreamReader(						vfs._createInputStream(session,						path,false,view)));				}			}			if(view != null)			{				interp.set("view",view);				EditPane editPane = view.getEditPane();				interp.set("editPane",editPane);				interp.set("buffer",editPane.getBuffer());				interp.set("textArea",editPane.getTextArea());			}			interp.set("scriptPath",path);			running = true;			interp.eval(in,namespace,path);		}		catch(Exception e)		{			unwrapException(e);		}		finally		{			running = false;			if(session != null)			{				try				{					vfs._endVFSSession(session,view);				}				catch(IOException io)				{					Log.log(Log.ERROR,BeanShell.class,io);					GUIUtilities.error(view,"read-error",						new String[] { path, io.toString() });				}			}			try			{				// no need to do this for macros!				if(!ownNamespace)				{					if(view != null)					{						interp.unset("view");						interp.unset("editPane");						interp.unset("buffer");						interp.unset("textArea");					}					interp.unset("scriptPath");				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -