📄 jedit.java
字号:
/* * jEdit.java - Main class of the jEdit editor * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1998, 2004 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.UtilEvalError;import com.microstar.xml.*;import javax.swing.plaf.metal.*;import javax.swing.*;import java.awt.*;import java.io.*;import java.net.*;import java.text.MessageFormat;import java.util.*;import org.gjt.sp.jedit.buffer.BufferIORequest;import org.gjt.sp.jedit.buffer.KillRing;import org.gjt.sp.jedit.msg.*;import org.gjt.sp.jedit.gui.*;import org.gjt.sp.jedit.help.HelpViewer;import org.gjt.sp.jedit.io.*;import org.gjt.sp.jedit.pluginmgr.PluginManager;import org.gjt.sp.jedit.search.SearchAndReplace;import org.gjt.sp.jedit.syntax.*;import org.gjt.sp.jedit.textarea.*;import org.gjt.sp.util.Log;//}}}/** * The main class of the jEdit text editor. * @author Slava Pestov * @version $Id: jEdit.java,v 1.238 2004/08/29 02:58:43 spestov Exp $ */public class jEdit{ //{{{ getVersion() method /** * Returns the jEdit version as a human-readable string. */ public static String getVersion() { return MiscUtilities.buildToVersion(getBuild()); } //}}} //{{{ getBuild() method /** * Returns the internal version. MiscUtilities.compareStrings() can be used * to compare different internal versions. */ public static String getBuild() { // (major).(minor).(<99 = preX, 99 = final).(bug fix) return "04.02.99.00"; } //}}} //{{{ main() method /** * The main method of the jEdit application. * This should never be invoked directly. * @param args The command line arguments */ public static void main(String[] args) { //{{{ Check for Java 1.3 or later String javaVersion = System.getProperty("java.version"); if(javaVersion.compareTo("1.3") < 0) { System.err.println("You are running Java version " + javaVersion + "."); System.err.println("jEdit requires Java 1.3 or later."); System.exit(1); } //}}} // later on we need to know if certain code is called from // the main thread mainThread = Thread.currentThread(); settingsDirectory = ".jedit"; // MacOS users expect the app to keep running after all windows // are closed background = OperatingSystem.isMacOS(); //{{{ Parse command line boolean endOpts = false; int level = Log.WARNING; String portFile = "server"; boolean restore = true; boolean newView = true; boolean newPlainView = false; boolean gui = true; // open initial view? boolean loadPlugins = true; boolean runStartupScripts = true; boolean quit = false; boolean wait = false; String userDir = System.getProperty("user.dir"); // script to run String scriptFile = null; for(int i = 0; i < args.length; i++) { String arg = args[i]; if(arg == null) continue; else if(arg.length() == 0) args[i] = null; else if(arg.startsWith("-") && !endOpts) { if(arg.equals("--")) endOpts = true; else if(arg.equals("-usage")) { version(); System.err.println(); usage(); System.exit(1); } else if(arg.equals("-version")) { version(); System.exit(1); } else if(arg.startsWith("-log=")) { try { level = Integer.parseInt(arg.substring("-log=".length())); } catch(NumberFormatException nf) { System.err.println("Malformed option: " + arg); } } else if(arg.equals("-nosettings")) settingsDirectory = null; else if(arg.startsWith("-settings=")) settingsDirectory = arg.substring(10); else if(arg.startsWith("-noserver")) portFile = null; else if(arg.equals("-server")) portFile = "server"; else if(arg.startsWith("-server=")) portFile = arg.substring(8); else if(arg.startsWith("-background")) background = true; else if(arg.startsWith("-nobackground")) background = false; else if(arg.equals("-gui")) gui = true; else if(arg.equals("-nogui")) gui = false; else if(arg.equals("-newview")) newView = true; else if(arg.equals("-newplainview")) newPlainView = true; else if(arg.equals("-reuseview")) newPlainView = newView = false; else if(arg.equals("-restore")) restore = true; else if(arg.equals("-norestore")) restore = false; else if(arg.equals("-plugins")) loadPlugins = true; else if(arg.equals("-noplugins")) loadPlugins = false; else if(arg.equals("-startupscripts")) runStartupScripts = true; else if(arg.equals("-nostartupscripts")) runStartupScripts = false; else if(arg.startsWith("-run=")) scriptFile = arg.substring(5); else if(arg.equals("-wait")) wait = true; else if(arg.equals("-quit")) quit = true; else { System.err.println("Unknown option: " + arg); usage(); System.exit(1); } args[i] = null; } } //}}} //{{{ We need these initializations very early on if(settingsDirectory != null) { settingsDirectory = MiscUtilities.constructPath( System.getProperty("user.home"), settingsDirectory); settingsDirectory = MiscUtilities.resolveSymlinks( settingsDirectory); } if(settingsDirectory != null && portFile != null) portFile = MiscUtilities.constructPath(settingsDirectory,portFile); else portFile = null; Log.init(true,level); //}}} //{{{ Try connecting to another running jEdit instance if(portFile != null && new File(portFile).exists()) { int port, key; try { BufferedReader in = new BufferedReader(new FileReader(portFile)); String check = in.readLine(); if(!check.equals("b")) throw new Exception("Wrong port file format"); port = Integer.parseInt(in.readLine()); key = Integer.parseInt(in.readLine()); Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),port); DataOutputStream out = new DataOutputStream( socket.getOutputStream()); out.writeInt(key); String script; if(quit) { script = "socket.close();\n" + "jEdit.exit(null,true);\n"; } else { script = makeServerScript(wait,restore, newView,newPlainView,args, scriptFile); } out.writeUTF(script); Log.log(Log.DEBUG,jEdit.class,"Waiting for server"); // block until its closed try { socket.getInputStream().read(); } catch(Exception e) { } in.close(); out.close(); System.exit(0); } catch(Exception e) { // ok, this one seems to confuse newbies // endlessly, so log it as NOTICE, not // ERROR Log.log(Log.NOTICE,jEdit.class,"An error occurred" + " while connecting to the jEdit server instance."); Log.log(Log.NOTICE,jEdit.class,"This probably means that" + " jEdit crashed and/or exited abnormally"); Log.log(Log.NOTICE,jEdit.class,"the last time it was run."); Log.log(Log.NOTICE,jEdit.class,"If you don't" + " know what this means, don't worry."); Log.log(Log.NOTICE,jEdit.class,e); } } if(quit) { // if no server running and user runs jedit -quit, // just exit System.exit(0); } //}}} // don't show splash screen if there is a file named // 'nosplash' in the settings directory if(!new File(settingsDirectory,"nosplash").exists()) GUIUtilities.showSplashScreen(); //{{{ Initialize settings directory Writer stream; if(settingsDirectory != null) { File _settingsDirectory = new File(settingsDirectory); if(!_settingsDirectory.exists()) _settingsDirectory.mkdirs(); File _macrosDirectory = new File(settingsDirectory,"macros"); if(!_macrosDirectory.exists()) _macrosDirectory.mkdir(); String logPath = MiscUtilities.constructPath( settingsDirectory,"activity.log"); backupSettingsFile(new File(logPath)); try { stream = new BufferedWriter(new FileWriter(logPath)); // Write a warning message: String lineSep = System.getProperty("line.separator"); stream.write("Log file created on " + new Date()); stream.write(lineSep); stream.write("IMPORTANT:"); stream.write(lineSep); stream.write("Because updating this file after " + "every log message would kill"); stream.write(lineSep); stream.write("performance, it will be *incomplete* " + "unless you invoke the"); stream.write(lineSep); stream.write("Utilities->Troubleshooting->Update " + "Activity Log on Disk command!"); stream.write(lineSep); } catch(Exception e) { e.printStackTrace(); stream = null; } } else { stream = null; } //}}} Log.setLogWriter(stream); Log.log(Log.NOTICE,jEdit.class,"jEdit version " + getVersion()); Log.log(Log.MESSAGE,jEdit.class,"Settings directory is " + settingsDirectory); //{{{ Get things rolling initMisc(); initSystemProperties(); GUIUtilities.advanceSplashProgress(); GUIUtilities.init(); BeanShell.init(); if(jEditHome != null) initSiteProperties(); initUserProperties(); //}}} //{{{ Initialize server if(portFile != null) { server = new EditServer(portFile); if(!server.isOK()) server = null; } else { if(background) { background = false; Log.log(Log.WARNING,jEdit.class,"You cannot specify both the" + " -background and -noserver switches"); } } //}}} //{{{ Do more stuff initPLAF(); VFSManager.init(); initResources(); SearchAndReplace.load(); GUIUtilities.advanceSplashProgress(); if(loadPlugins) initPlugins(); HistoryModel.loadHistory(); BufferHistory.load(); KillRing.load(); propertiesChanged(); GUIUtilities.advanceSplashProgress(); // Buffer sort sortBuffers = getBooleanProperty("sortBuffers"); sortByName = getBooleanProperty("sortByName"); reloadModes(); GUIUtilities.advanceSplashProgress(); //}}} //{{{ Initialize Java 1.4-specific code if(OperatingSystem.hasJava14()) { try { ClassLoader loader = jEdit.class.getClassLoader(); Class clazz; if(loader != null) clazz = loader.loadClass("org.gjt.sp.jedit.Java14"); else clazz = Class.forName("org.gjt.sp.jedit.Java14"); java.lang.reflect.Method meth = clazz .getMethod("init",new Class[0]); meth.invoke(null,new Object[0]); } catch(Exception e) { Log.log(Log.ERROR,jEdit.class,e); System.exit(1); } } //}}} //{{{ Activate plugins that must be activated at startup for(int i = 0; i < jars.size(); i++) { ((PluginJAR)jars.elementAt(i)).activatePluginIfNecessary(); } //}}} //{{{ Load macros and run startup scripts, after plugins and settings are loaded Macros.loadMacros(); Macros.getMacroActionSet().initKeyBindings(); if(runStartupScripts && jEditHome != null) { String path = MiscUtilities.constructPath(jEditHome,"startup"); File file = new File(path); if(file.exists()) runStartupScripts(file); } if(runStartupScripts && settingsDirectory != null) { String path = MiscUtilities.constructPath(settingsDirectory,"startup"); File file = new File(path); if(!file.exists()) file.mkdirs(); else runStartupScripts(file); } //}}} //{{{ Run script specified with -run= parameter if(scriptFile != null) { scriptFile = MiscUtilities.constructPath(userDir,scriptFile); try { BeanShell.getNameSpace().setVariable("args",args); } catch(UtilEvalError e) { Log.log(Log.ERROR,jEdit.class,e); } BeanShell.runScript(null,scriptFile,null,false); } //}}} GUIUtilities.advanceSplashProgress(); // Open files, create the view and hide the splash screen. finishStartup(gui,restore,userDir,args); } //}}} //{{{ Property methods //{{{ getProperties() method /** * Returns the properties object which contains all known * jEdit properties. Note that as of jEdit 4.2pre10, this returns a * new collection, not the existing properties instance. * @since jEdit 3.1pre4 */ public static final Properties getProperties() { return propMgr.getProperties(); } //}}} //{{{ getProperty() method /** * Fetches a property, returning null if it's not defined. * @param name The property */ public static final String getProperty(String name) { return propMgr.getProperty(name); } //}}} //{{{ getProperty() method /** * Fetches a property, returning the default value if it's not * defined. * @param name The property * @param def The default value */ public static final String getProperty(String name, String def) { String value = propMgr.getProperty(name); if(value == null) return def; else return value; } //}}} //{{{ getProperty() method /** * Returns the property with the specified name.<p> * * The elements of the <code>args</code> array are substituted * into the value of the property in place of strings of the * form <code>{<i>n</i>}</code>, where <code><i>n</i></code> is an index * in the array.<p> * * You can find out more about this feature by reading the * documentation for the <code>format</code> method of the * <code>java.text.MessageFormat</code> class. * * @param name The property * @param args The positional parameters */ public static final String getProperty(String name, Object[] args) { if(name == null) return null; if(args == null) return getProperty(name); else { String value = getProperty(name); if(value == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -