/jEdit/tags/jedit-4-2-pre12/org/gjt/sp/jedit/jEdit.java

# · Java · 2739 lines · 1616 code · 330 blank · 793 comment · 375 complexity · c4436c9e6bdb3dbfd5ae88cc9094fcd7 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * jEdit.java - Main class of the jEdit editor
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 2004 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package org.gjt.sp.jedit;
  22. //{{{ Imports
  23. import bsh.UtilEvalError;
  24. import com.microstar.xml.*;
  25. import javax.swing.plaf.metal.*;
  26. import javax.swing.*;
  27. import java.awt.*;
  28. import java.io.*;
  29. import java.net.*;
  30. import java.text.MessageFormat;
  31. import java.util.*;
  32. import org.gjt.sp.jedit.buffer.BufferIORequest;
  33. import org.gjt.sp.jedit.buffer.KillRing;
  34. import org.gjt.sp.jedit.msg.*;
  35. import org.gjt.sp.jedit.gui.*;
  36. import org.gjt.sp.jedit.help.HelpViewer;
  37. import org.gjt.sp.jedit.io.*;
  38. import org.gjt.sp.jedit.pluginmgr.PluginManager;
  39. import org.gjt.sp.jedit.search.SearchAndReplace;
  40. import org.gjt.sp.jedit.syntax.*;
  41. import org.gjt.sp.jedit.textarea.*;
  42. import org.gjt.sp.util.Log;
  43. //}}}
  44. /**
  45. * The main class of the jEdit text editor.
  46. * @author Slava Pestov
  47. * @version $Id: jEdit.java 5017 2004-04-18 02:02:42Z spestov $
  48. */
  49. public class jEdit
  50. {
  51. //{{{ getVersion() method
  52. /**
  53. * Returns the jEdit version as a human-readable string.
  54. */
  55. public static String getVersion()
  56. {
  57. return MiscUtilities.buildToVersion(getBuild());
  58. } //}}}
  59. //{{{ getBuild() method
  60. /**
  61. * Returns the internal version. MiscUtilities.compareStrings() can be used
  62. * to compare different internal versions.
  63. */
  64. public static String getBuild()
  65. {
  66. // (major).(minor).(<99 = preX, 99 = final).(bug fix)
  67. return "04.02.12.00";
  68. } //}}}
  69. //{{{ main() method
  70. /**
  71. * The main method of the jEdit application.
  72. * This should never be invoked directly.
  73. * @param args The command line arguments
  74. */
  75. public static void main(String[] args)
  76. {
  77. //{{{ Check for Java 1.3 or later
  78. String javaVersion = System.getProperty("java.version");
  79. if(javaVersion.compareTo("1.3") < 0)
  80. {
  81. System.err.println("You are running Java version "
  82. + javaVersion + ".");
  83. System.err.println("jEdit requires Java 1.3 or later.");
  84. System.exit(1);
  85. } //}}}
  86. // later on we need to know if certain code is called from
  87. // the main thread
  88. mainThread = Thread.currentThread();
  89. settingsDirectory = ".jedit";
  90. // MacOS users expect the app to keep running after all windows
  91. // are closed
  92. background = OperatingSystem.isMacOS();
  93. //{{{ Parse command line
  94. boolean endOpts = false;
  95. int level = Log.WARNING;
  96. String portFile = "server";
  97. boolean restore = true;
  98. boolean newView = true;
  99. boolean newPlainView = false;
  100. boolean gui = true; // open initial view?
  101. boolean loadPlugins = true;
  102. boolean runStartupScripts = true;
  103. boolean quit = false;
  104. boolean wait = false;
  105. String userDir = System.getProperty("user.dir");
  106. // script to run
  107. String scriptFile = null;
  108. for(int i = 0; i < args.length; i++)
  109. {
  110. String arg = args[i];
  111. if(arg == null)
  112. continue;
  113. else if(arg.length() == 0)
  114. args[i] = null;
  115. else if(arg.startsWith("-") && !endOpts)
  116. {
  117. if(arg.equals("--"))
  118. endOpts = true;
  119. else if(arg.equals("-usage"))
  120. {
  121. version();
  122. System.err.println();
  123. usage();
  124. System.exit(1);
  125. }
  126. else if(arg.equals("-version"))
  127. {
  128. version();
  129. System.exit(1);
  130. }
  131. else if(arg.startsWith("-log="))
  132. {
  133. try
  134. {
  135. level = Integer.parseInt(arg.substring("-log=".length()));
  136. }
  137. catch(NumberFormatException nf)
  138. {
  139. System.err.println("Malformed option: " + arg);
  140. }
  141. }
  142. else if(arg.equals("-nosettings"))
  143. settingsDirectory = null;
  144. else if(arg.startsWith("-settings="))
  145. settingsDirectory = arg.substring(10);
  146. else if(arg.startsWith("-noserver"))
  147. portFile = null;
  148. else if(arg.equals("-server"))
  149. portFile = "server";
  150. else if(arg.startsWith("-server="))
  151. portFile = arg.substring(8);
  152. else if(arg.startsWith("-background"))
  153. background = true;
  154. else if(arg.startsWith("-nobackground"))
  155. background = false;
  156. else if(arg.equals("-gui"))
  157. gui = true;
  158. else if(arg.equals("-nogui"))
  159. gui = false;
  160. else if(arg.equals("-newview"))
  161. newView = true;
  162. else if(arg.equals("-newplainview"))
  163. newPlainView = true;
  164. else if(arg.equals("-reuseview"))
  165. newPlainView = newView = false;
  166. else if(arg.equals("-restore"))
  167. restore = true;
  168. else if(arg.equals("-norestore"))
  169. restore = false;
  170. else if(arg.equals("-plugins"))
  171. loadPlugins = true;
  172. else if(arg.equals("-noplugins"))
  173. loadPlugins = false;
  174. else if(arg.equals("-startupscripts"))
  175. runStartupScripts = true;
  176. else if(arg.equals("-nostartupscripts"))
  177. runStartupScripts = false;
  178. else if(arg.startsWith("-run="))
  179. scriptFile = arg.substring(5);
  180. else if(arg.equals("-wait"))
  181. wait = true;
  182. else if(arg.equals("-quit"))
  183. quit = true;
  184. else
  185. {
  186. System.err.println("Unknown option: "
  187. + arg);
  188. usage();
  189. System.exit(1);
  190. }
  191. args[i] = null;
  192. }
  193. } //}}}
  194. //{{{ We need these initializations very early on
  195. if(settingsDirectory != null)
  196. {
  197. settingsDirectory = MiscUtilities.constructPath(
  198. System.getProperty("user.home"),
  199. settingsDirectory);
  200. settingsDirectory = MiscUtilities.resolveSymlinks(
  201. settingsDirectory);
  202. }
  203. if(settingsDirectory != null && portFile != null)
  204. portFile = MiscUtilities.constructPath(settingsDirectory,portFile);
  205. else
  206. portFile = null;
  207. Log.init(true,level);
  208. //}}}
  209. //{{{ Try connecting to another running jEdit instance
  210. if(portFile != null && new File(portFile).exists())
  211. {
  212. int port, key;
  213. try
  214. {
  215. BufferedReader in = new BufferedReader(new FileReader(portFile));
  216. String check = in.readLine();
  217. if(!check.equals("b"))
  218. throw new Exception("Wrong port file format");
  219. port = Integer.parseInt(in.readLine());
  220. key = Integer.parseInt(in.readLine());
  221. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),port);
  222. DataOutputStream out = new DataOutputStream(
  223. socket.getOutputStream());
  224. out.writeInt(key);
  225. String script;
  226. if(quit)
  227. {
  228. script = "socket.close();\n"
  229. + "jEdit.exit(null,true);\n";
  230. }
  231. else
  232. {
  233. script = makeServerScript(wait,restore,
  234. newView,newPlainView,args,
  235. scriptFile);
  236. }
  237. out.writeUTF(script);
  238. Log.log(Log.DEBUG,jEdit.class,"Waiting for server");
  239. // block until its closed
  240. try
  241. {
  242. socket.getInputStream().read();
  243. }
  244. catch(Exception e)
  245. {
  246. }
  247. in.close();
  248. out.close();
  249. System.exit(0);
  250. }
  251. catch(Exception e)
  252. {
  253. // ok, this one seems to confuse newbies
  254. // endlessly, so log it as NOTICE, not
  255. // ERROR
  256. Log.log(Log.NOTICE,jEdit.class,"An error occurred"
  257. + " while connecting to the jEdit server instance.");
  258. Log.log(Log.NOTICE,jEdit.class,"This probably means that"
  259. + " jEdit crashed and/or exited abnormally");
  260. Log.log(Log.NOTICE,jEdit.class,"the last time it was run.");
  261. Log.log(Log.NOTICE,jEdit.class,"If you don't"
  262. + " know what this means, don't worry.");
  263. Log.log(Log.NOTICE,jEdit.class,e);
  264. }
  265. }
  266. if(quit)
  267. {
  268. // if no server running and user runs jedit -quit,
  269. // just exit
  270. System.exit(0);
  271. } //}}}
  272. // don't show splash screen if there is a file named
  273. // 'nosplash' in the settings directory
  274. if(!new File(settingsDirectory,"nosplash").exists())
  275. GUIUtilities.showSplashScreen();
  276. //{{{ Initialize settings directory
  277. Writer stream;
  278. if(settingsDirectory != null)
  279. {
  280. File _settingsDirectory = new File(settingsDirectory);
  281. if(!_settingsDirectory.exists())
  282. _settingsDirectory.mkdirs();
  283. File _macrosDirectory = new File(settingsDirectory,"macros");
  284. if(!_macrosDirectory.exists())
  285. _macrosDirectory.mkdir();
  286. String logPath = MiscUtilities.constructPath(
  287. settingsDirectory,"activity.log");
  288. backupSettingsFile(new File(logPath));
  289. try
  290. {
  291. stream = new BufferedWriter(new FileWriter(logPath));
  292. // Write a warning message:
  293. String lineSep = System.getProperty("line.separator");
  294. stream.write("Log file created on " + new Date());
  295. stream.write(lineSep);
  296. stream.write("IMPORTANT:");
  297. stream.write(lineSep);
  298. stream.write("Because updating this file after "
  299. + "every log message would kill");
  300. stream.write(lineSep);
  301. stream.write("performance, it will be *incomplete* "
  302. + "unless you invoke the");
  303. stream.write(lineSep);
  304. stream.write("Utilities->Troubleshooting->Update "
  305. + "Activity Log on Disk command!");
  306. stream.write(lineSep);
  307. }
  308. catch(Exception e)
  309. {
  310. e.printStackTrace();
  311. stream = null;
  312. }
  313. }
  314. else
  315. {
  316. stream = null;
  317. } //}}}
  318. Log.setLogWriter(stream);
  319. Log.log(Log.NOTICE,jEdit.class,"jEdit version " + getVersion());
  320. Log.log(Log.MESSAGE,jEdit.class,"Settings directory is "
  321. + settingsDirectory);
  322. //{{{ Get things rolling
  323. initMisc();
  324. initSystemProperties();
  325. GUIUtilities.advanceSplashProgress();
  326. GUIUtilities.init();
  327. BeanShell.init();
  328. if(jEditHome != null)
  329. initSiteProperties();
  330. initUserProperties();
  331. //}}}
  332. //{{{ Initialize server
  333. if(portFile != null)
  334. {
  335. server = new EditServer(portFile);
  336. if(!server.isOK())
  337. server = null;
  338. }
  339. else
  340. {
  341. if(background)
  342. {
  343. background = false;
  344. Log.log(Log.WARNING,jEdit.class,"You cannot specify both the"
  345. + " -background and -noserver switches");
  346. }
  347. } //}}}
  348. //{{{ Do more stuff
  349. initPLAF();
  350. VFSManager.init();
  351. initResources();
  352. SearchAndReplace.load();
  353. GUIUtilities.advanceSplashProgress();
  354. if(loadPlugins)
  355. initPlugins();
  356. HistoryModel.loadHistory();
  357. BufferHistory.load();
  358. KillRing.load();
  359. propertiesChanged();
  360. GUIUtilities.advanceSplashProgress();
  361. // Buffer sort
  362. sortBuffers = getBooleanProperty("sortBuffers");
  363. sortByName = getBooleanProperty("sortByName");
  364. reloadModes();
  365. GUIUtilities.advanceSplashProgress();
  366. //}}}
  367. //{{{ Initialize Java 1.4-specific code
  368. if(OperatingSystem.hasJava14())
  369. {
  370. try
  371. {
  372. ClassLoader loader = jEdit.class.getClassLoader();
  373. Class clazz;
  374. if(loader != null)
  375. clazz = loader.loadClass("org.gjt.sp.jedit.Java14");
  376. else
  377. clazz = Class.forName("org.gjt.sp.jedit.Java14");
  378. java.lang.reflect.Method meth = clazz
  379. .getMethod("init",new Class[0]);
  380. meth.invoke(null,new Object[0]);
  381. }
  382. catch(Exception e)
  383. {
  384. Log.log(Log.ERROR,jEdit.class,e);
  385. System.exit(1);
  386. }
  387. } //}}}
  388. //{{{ Activate plugins that must be activated at startup
  389. for(int i = 0; i < jars.size(); i++)
  390. {
  391. ((PluginJAR)jars.elementAt(i)).activatePluginIfNecessary();
  392. } //}}}
  393. //{{{ Load macros and run startup scripts, after plugins and settings are loaded
  394. Macros.loadMacros();
  395. Macros.getMacroActionSet().initKeyBindings();
  396. if(runStartupScripts && jEditHome != null)
  397. {
  398. String path = MiscUtilities.constructPath(jEditHome,"startup");
  399. File file = new File(path);
  400. if(file.exists())
  401. runStartupScripts(file);
  402. }
  403. if(runStartupScripts && settingsDirectory != null)
  404. {
  405. String path = MiscUtilities.constructPath(settingsDirectory,"startup");
  406. File file = new File(path);
  407. if(!file.exists())
  408. file.mkdirs();
  409. else
  410. runStartupScripts(file);
  411. } //}}}
  412. //{{{ Run script specified with -run= parameter
  413. if(scriptFile != null)
  414. {
  415. scriptFile = MiscUtilities.constructPath(userDir,scriptFile);
  416. try
  417. {
  418. BeanShell.getNameSpace().setVariable("args",args);
  419. }
  420. catch(UtilEvalError e)
  421. {
  422. Log.log(Log.ERROR,jEdit.class,e);
  423. }
  424. BeanShell.runScript(null,scriptFile,null,false);
  425. } //}}}
  426. GUIUtilities.advanceSplashProgress();
  427. // Open files, create the view and hide the splash screen.
  428. finishStartup(gui,restore,userDir,args);
  429. } //}}}
  430. //{{{ Property methods
  431. //{{{ getProperties() method
  432. /**
  433. * Returns the properties object which contains all known
  434. * jEdit properties. Note that as of jEdit 4.2pre10, this returns a
  435. * new collection, not the existing properties instance.
  436. * @since jEdit 3.1pre4
  437. */
  438. public static final Properties getProperties()
  439. {
  440. return propMgr.getProperties();
  441. } //}}}
  442. //{{{ getProperty() method
  443. /**
  444. * Fetches a property, returning null if it's not defined.
  445. * @param name The property
  446. */
  447. public static final String getProperty(String name)
  448. {
  449. return propMgr.getProperty(name);
  450. } //}}}
  451. //{{{ getProperty() method
  452. /**
  453. * Fetches a property, returning the default value if it's not
  454. * defined.
  455. * @param name The property
  456. * @param def The default value
  457. */
  458. public static final String getProperty(String name, String def)
  459. {
  460. String value = propMgr.getProperty(name);
  461. if(value == null)
  462. return def;
  463. else
  464. return value;
  465. } //}}}
  466. //{{{ getProperty() method
  467. /**
  468. * Returns the property with the specified name.<p>
  469. *
  470. * The elements of the <code>args</code> array are substituted
  471. * into the value of the property in place of strings of the
  472. * form <code>{<i>n</i>}</code>, where <code><i>n</i></code> is an index
  473. * in the array.<p>
  474. *
  475. * You can find out more about this feature by reading the
  476. * documentation for the <code>format</code> method of the
  477. * <code>java.text.MessageFormat</code> class.
  478. *
  479. * @param name The property
  480. * @param args The positional parameters
  481. */
  482. public static final String getProperty(String name, Object[] args)
  483. {
  484. if(name == null)
  485. return null;
  486. if(args == null)
  487. return getProperty(name);
  488. else
  489. {
  490. String value = getProperty(name);
  491. if(value == null)
  492. return null;
  493. else
  494. return MessageFormat.format(value,args);
  495. }
  496. } //}}}
  497. //{{{ getBooleanProperty() method
  498. /**
  499. * Returns the value of a boolean property.
  500. * @param name The property
  501. */
  502. public static final boolean getBooleanProperty(String name)
  503. {
  504. return getBooleanProperty(name,false);
  505. } //}}}
  506. //{{{ getBooleanProperty() method
  507. /**
  508. * Returns the value of a boolean property.
  509. * @param name The property
  510. * @param def The default value
  511. */
  512. public static final boolean getBooleanProperty(String name, boolean def)
  513. {
  514. String value = getProperty(name);
  515. if(value == null)
  516. return def;
  517. else if(value.equals("true") || value.equals("yes")
  518. || value.equals("on"))
  519. return true;
  520. else if(value.equals("false") || value.equals("no")
  521. || value.equals("off"))
  522. return false;
  523. else
  524. return def;
  525. } //}}}
  526. //{{{ getIntegerProperty() method
  527. /**
  528. * Returns the value of an integer property.
  529. * @param name The property
  530. * @param def The default value
  531. * @since jEdit 4.0pre1
  532. */
  533. public static final int getIntegerProperty(String name, int def)
  534. {
  535. String value = getProperty(name);
  536. if(value == null)
  537. return def;
  538. else
  539. {
  540. try
  541. {
  542. return Integer.parseInt(value.trim());
  543. }
  544. catch(NumberFormatException nf)
  545. {
  546. return def;
  547. }
  548. }
  549. } //}}}
  550. //{{{ getDoubleProperty() method
  551. public static double getDoubleProperty(String name, double def)
  552. {
  553. String value = getProperty(name);
  554. if(value == null)
  555. return def;
  556. else
  557. {
  558. try
  559. {
  560. return Double.parseDouble(value.trim());
  561. }
  562. catch(NumberFormatException nf)
  563. {
  564. return def;
  565. }
  566. }
  567. }
  568. //}}}
  569. //{{{ getFontProperty() method
  570. /**
  571. * Returns the value of a font property. The family is stored
  572. * in the <code><i>name</i></code> property, the font size is stored
  573. * in the <code><i>name</i>size</code> property, and the font style is
  574. * stored in <code><i>name</i>style</code>. For example, if
  575. * <code><i>name</i></code> is <code>view.gutter.font</code>, the
  576. * properties will be named <code>view.gutter.font</code>,
  577. * <code>view.gutter.fontsize</code>, and
  578. * <code>view.gutter.fontstyle</code>.
  579. *
  580. * @param name The property
  581. * @since jEdit 4.0pre1
  582. */
  583. public static final Font getFontProperty(String name)
  584. {
  585. return getFontProperty(name,null);
  586. } //}}}
  587. //{{{ getFontProperty() method
  588. /**
  589. * Returns the value of a font property. The family is stored
  590. * in the <code><i>name</i></code> property, the font size is stored
  591. * in the <code><i>name</i>size</code> property, and the font style is
  592. * stored in <code><i>name</i>style</code>. For example, if
  593. * <code><i>name</i></code> is <code>view.gutter.font</code>, the
  594. * properties will be named <code>view.gutter.font</code>,
  595. * <code>view.gutter.fontsize</code>, and
  596. * <code>view.gutter.fontstyle</code>.
  597. *
  598. * @param name The property
  599. * @param def The default value
  600. * @since jEdit 4.0pre1
  601. */
  602. public static final Font getFontProperty(String name, Font def)
  603. {
  604. String family = getProperty(name);
  605. String sizeString = getProperty(name + "size");
  606. String styleString = getProperty(name + "style");
  607. if(family == null || sizeString == null || styleString == null)
  608. return def;
  609. else
  610. {
  611. int size, style;
  612. try
  613. {
  614. size = Integer.parseInt(sizeString);
  615. }
  616. catch(NumberFormatException nf)
  617. {
  618. return def;
  619. }
  620. try
  621. {
  622. style = Integer.parseInt(styleString);
  623. }
  624. catch(NumberFormatException nf)
  625. {
  626. return def;
  627. }
  628. return new Font(family,style,size);
  629. }
  630. } //}}}
  631. //{{{ getColorProperty() method
  632. /**
  633. * Returns the value of a color property.
  634. * @param name The property name
  635. * @since jEdit 4.0pre1
  636. */
  637. public static Color getColorProperty(String name)
  638. {
  639. return getColorProperty(name,Color.black);
  640. } //}}}
  641. //{{{ getColorProperty() method
  642. /**
  643. * Returns the value of a color property.
  644. * @param name The property name
  645. * @param def The default value
  646. * @since jEdit 4.0pre1
  647. */
  648. public static Color getColorProperty(String name, Color def)
  649. {
  650. String value = getProperty(name);
  651. if(value == null)
  652. return def;
  653. else
  654. return GUIUtilities.parseColor(value,def);
  655. } //}}}
  656. //{{{ setColorProperty() method
  657. /**
  658. * Sets the value of a color property.
  659. * @param name The property name
  660. * @param value The value
  661. * @since jEdit 4.0pre1
  662. */
  663. public static void setColorProperty(String name, Color value)
  664. {
  665. setProperty(name,GUIUtilities.getColorHexString(value));
  666. } //}}}
  667. //{{{ setProperty() method
  668. /**
  669. * Sets a property to a new value.
  670. * @param name The property
  671. * @param value The new value
  672. */
  673. public static final void setProperty(String name, String value)
  674. {
  675. propMgr.setProperty(name,value);
  676. } //}}}
  677. //{{{ setTemporaryProperty() method
  678. /**
  679. * Sets a property to a new value. Properties set using this
  680. * method are not saved to the user properties list.
  681. * @param name The property
  682. * @param value The new value
  683. * @since jEdit 2.3final
  684. */
  685. public static final void setTemporaryProperty(String name, String value)
  686. {
  687. propMgr.setTemporaryProperty(name,value);
  688. } //}}}
  689. //{{{ setBooleanProperty() method
  690. /**
  691. * Sets a boolean property.
  692. * @param name The property
  693. * @param value The value
  694. */
  695. public static final void setBooleanProperty(String name, boolean value)
  696. {
  697. setProperty(name,value ? "true" : "false");
  698. } //}}}
  699. //{{{ setIntegerProperty() method
  700. /**
  701. * Sets the value of an integer property.
  702. * @param name The property
  703. * @param value The value
  704. * @since jEdit 4.0pre1
  705. */
  706. public static final void setIntegerProperty(String name, int value)
  707. {
  708. setProperty(name,String.valueOf(value));
  709. } //}}}
  710. //{{{ setDoubleProperty() method
  711. public static final void setDoubleProperty(String name, double value)
  712. {
  713. setProperty(name,String.valueOf(value));
  714. }
  715. //}}}
  716. //{{{ setFontProperty() method
  717. /**
  718. * Sets the value of a font property. The family is stored
  719. * in the <code><i>name</i></code> property, the font size is stored
  720. * in the <code><i>name</i>size</code> property, and the font style is
  721. * stored in <code><i>name</i>style</code>. For example, if
  722. * <code><i>name</i></code> is <code>view.gutter.font</code>, the
  723. * properties will be named <code>view.gutter.font</code>,
  724. * <code>view.gutter.fontsize</code>, and
  725. * <code>view.gutter.fontstyle</code>.
  726. *
  727. * @param name The property
  728. * @param value The value
  729. * @since jEdit 4.0pre1
  730. */
  731. public static final void setFontProperty(String name, Font value)
  732. {
  733. setProperty(name,value.getFamily());
  734. setIntegerProperty(name + "size",value.getSize());
  735. setIntegerProperty(name + "style",value.getStyle());
  736. } //}}}
  737. //{{{ unsetProperty() method
  738. /**
  739. * Unsets (clears) a property.
  740. * @param name The property
  741. */
  742. public static final void unsetProperty(String name)
  743. {
  744. propMgr.unsetProperty(name);
  745. } //}}}
  746. //{{{ resetProperty() method
  747. /**
  748. * Resets a property to its default value.
  749. * @param name The property
  750. *
  751. * @since jEdit 2.5pre3
  752. */
  753. public static final void resetProperty(String name)
  754. {
  755. propMgr.resetProperty(name);
  756. } //}}}
  757. //{{{ propertiesChanged() method
  758. /**
  759. * Reloads various settings from the properties.
  760. */
  761. public static void propertiesChanged()
  762. {
  763. initKeyBindings();
  764. Autosave.setInterval(getIntegerProperty("autosave",30));
  765. saveCaret = getBooleanProperty("saveCaret");
  766. //theme = new JEditMetalTheme();
  767. //theme.propertiesChanged();
  768. //MetalLookAndFeel.setCurrentTheme(theme);
  769. UIDefaults defaults = UIManager.getDefaults();
  770. // give all text areas the same font
  771. Font font = getFontProperty("view.font");
  772. //defaults.put("TextField.font",font);
  773. defaults.put("TextArea.font",font);
  774. defaults.put("TextPane.font",font);
  775. // Enable/Disable tooltips
  776. ToolTipManager.sharedInstance().setEnabled(
  777. jEdit.getBooleanProperty("showTooltips"));
  778. initProxy();
  779. // we do this here instead of adding buffers to the bus.
  780. Buffer buffer = buffersFirst;
  781. while(buffer != null)
  782. {
  783. buffer.resetCachedProperties();
  784. buffer.propertiesChanged();
  785. buffer = buffer.next;
  786. }
  787. HistoryModel.propertiesChanged();
  788. KillRing.propertiesChanged();
  789. EditBus.send(new PropertiesChanged(null));
  790. } //}}}
  791. //}}}
  792. //{{{ Plugin management methods
  793. //{{{ getNotLoadedPluginJARs() method
  794. /**
  795. * Returns a list of plugin JARs that are not currently loaded
  796. * by examining the user and system plugin directories.
  797. * @since jEdit 3.2pre1
  798. */
  799. public static String[] getNotLoadedPluginJARs()
  800. {
  801. Vector returnValue = new Vector();
  802. if(jEditHome != null)
  803. {
  804. String systemPluginDir = MiscUtilities
  805. .constructPath(jEditHome,"jars");
  806. String[] list = new File(systemPluginDir).list();
  807. if(list != null)
  808. getNotLoadedPluginJARs(returnValue,systemPluginDir,list);
  809. }
  810. if(settingsDirectory != null)
  811. {
  812. String userPluginDir = MiscUtilities
  813. .constructPath(settingsDirectory,"jars");
  814. String[] list = new File(userPluginDir).list();
  815. if(list != null)
  816. {
  817. getNotLoadedPluginJARs(returnValue,
  818. userPluginDir,list);
  819. }
  820. }
  821. String[] _returnValue = new String[returnValue.size()];
  822. returnValue.copyInto(_returnValue);
  823. return _returnValue;
  824. } //}}}
  825. //{{{ getPlugin() method
  826. /**
  827. * Returns the plugin with the specified class name.
  828. */
  829. public static EditPlugin getPlugin(String name)
  830. {
  831. return getPlugin(name, false);
  832. } //}}}
  833. //{{{ getPlugin(String, boolean) method
  834. /**
  835. * Returns the plugin with the specified class name. If
  836. * <code>loadIfNecessary</code> is true, the plugin will be activated in
  837. * case it has not yet been started.
  838. * @since jEdit 4.2pre4
  839. */
  840. public static EditPlugin getPlugin(String name, boolean loadIfNecessary)
  841. {
  842. EditPlugin[] plugins = getPlugins();
  843. EditPlugin plugin = null;
  844. for(int i = 0; i < plugins.length; i++)
  845. {
  846. if(plugins[i].getClassName().equals(name))
  847. plugin = plugins[i];
  848. if(loadIfNecessary)
  849. {
  850. if(plugin instanceof EditPlugin.Deferred)
  851. {
  852. plugin.getPluginJAR().activatePlugin();
  853. plugin = plugin.getPluginJAR().getPlugin();
  854. break;
  855. }
  856. }
  857. }
  858. return plugin;
  859. } //}}}
  860. //{{{ getPlugins() method
  861. /**
  862. * Returns an array of installed plugins.
  863. */
  864. public static EditPlugin[] getPlugins()
  865. {
  866. Vector vector = new Vector();
  867. for(int i = 0; i < jars.size(); i++)
  868. {
  869. EditPlugin plugin = ((PluginJAR)jars.elementAt(i))
  870. .getPlugin();
  871. if(plugin != null)
  872. vector.add(plugin);
  873. }
  874. EditPlugin[] array = new EditPlugin[vector.size()];
  875. vector.copyInto(array);
  876. return array;
  877. } //}}}
  878. //{{{ getPluginJARs() method
  879. /**
  880. * Returns an array of installed plugins.
  881. * @since jEdit 4.2pre1
  882. */
  883. public static PluginJAR[] getPluginJARs()
  884. {
  885. PluginJAR[] array = new PluginJAR[jars.size()];
  886. jars.copyInto(array);
  887. return array;
  888. } //}}}
  889. //{{{ getPluginJAR() method
  890. /**
  891. * Returns the JAR with the specified path name.
  892. * @param path The path name
  893. * @since jEdit 4.2pre1
  894. */
  895. public static PluginJAR getPluginJAR(String path)
  896. {
  897. for(int i = 0; i < jars.size(); i++)
  898. {
  899. PluginJAR jar = (PluginJAR)jars.elementAt(i);
  900. if(jar.getPath().equals(path))
  901. return jar;
  902. }
  903. return null;
  904. } //}}}
  905. //{{{ addPluginJAR() method
  906. /**
  907. * Loads the plugin JAR with the specified path. Some notes about this
  908. * method:
  909. *
  910. * <ul>
  911. * <li>Calling this at a time other than jEdit startup can have
  912. * unpredictable results if the plugin has not been updated for the
  913. * jEdit 4.2 plugin API.
  914. * <li>You must make sure yourself the plugin is not already loaded.
  915. * <li>After loading, you just make sure all the plugin's dependencies
  916. * are satisified before activating the plugin, using the
  917. * {@link PluginJAR#checkDependencies()} method.
  918. * </ul>
  919. *
  920. * @param path The JAR file path
  921. * @since jEdit 4.2pre1
  922. */
  923. public static void addPluginJAR(String path)
  924. {
  925. // backwards compatibility...
  926. PluginJAR jar = new EditPlugin.JAR(new File(path));
  927. jars.addElement(jar);
  928. jar.init();
  929. EditBus.send(new PluginUpdate(jar,PluginUpdate.LOADED,false));
  930. if(!isMainThread())
  931. {
  932. EditBus.send(new DynamicMenuChanged("plugins"));
  933. initKeyBindings();
  934. }
  935. } //}}}
  936. //{{{ addPluginJARsFromDirectory() method
  937. /**
  938. * Loads all plugins in a directory.
  939. * @param directory The directory
  940. * @since jEdit 4.2pre1
  941. */
  942. private static void addPluginJARsFromDirectory(String directory)
  943. {
  944. Log.log(Log.NOTICE,jEdit.class,"Loading plugins from "
  945. + directory);
  946. File file = new File(directory);
  947. if(!(file.exists() && file.isDirectory()))
  948. return;
  949. String[] plugins = file.list();
  950. if(plugins == null)
  951. return;
  952. for(int i = 0; i < plugins.length; i++)
  953. {
  954. String plugin = plugins[i];
  955. if(!plugin.toLowerCase().endsWith(".jar"))
  956. continue;
  957. String path = MiscUtilities.constructPath(directory,plugin);
  958. // remove this when 4.1 plugin API is deprecated
  959. if(plugin.equals("EditBuddy.jar")
  960. || plugin.equals("PluginManager.jar")
  961. || plugin.equals("Firewall.jar")
  962. || plugin.equals("Tidy.jar")
  963. || plugin.equals("DragAndDrop.jar"))
  964. {
  965. pluginError(path,"plugin-error.obsolete",null);
  966. continue;
  967. }
  968. addPluginJAR(path);
  969. }
  970. } //}}}
  971. //{{{ removePluginJAR() method
  972. /**
  973. * Unloads the given plugin JAR with the specified path. Note that
  974. * calling this at a time other than jEdit shutdown can have
  975. * unpredictable results if the plugin has not been updated for the
  976. * jEdit 4.2 plugin API.
  977. *
  978. * @param jar The <code>PluginJAR</code> instance
  979. * @param exit Set to true if jEdit is exiting; enables some
  980. * shortcuts so the editor can close faster.
  981. * @since jEdit 4.2pre1
  982. */
  983. public static void removePluginJAR(PluginJAR jar, boolean exit)
  984. {
  985. if(exit)
  986. {
  987. jar.uninit(true);
  988. }
  989. else
  990. {
  991. jar.uninit(false);
  992. jars.removeElement(jar);
  993. initKeyBindings();
  994. }
  995. EditBus.send(new PluginUpdate(jar,PluginUpdate.UNLOADED,exit));
  996. if(!isMainThread() && !exit)
  997. EditBus.send(new DynamicMenuChanged("plugins"));
  998. } //}}}
  999. //}}}
  1000. //{{{ Action methods
  1001. //{{{ getActionContext() method
  1002. /**
  1003. * Returns the action context used to store editor actions.
  1004. * @since jEdit 4.2pre1
  1005. */
  1006. public static ActionContext getActionContext()
  1007. {
  1008. return actionContext;
  1009. } //}}}
  1010. //{{{ addActionSet() method
  1011. /**
  1012. * Adds a new action set to jEdit's list. Plugins probably won't
  1013. * need to call this method.
  1014. * @since jEdit 4.0pre1
  1015. */
  1016. public static void addActionSet(ActionSet actionSet)
  1017. {
  1018. actionContext.addActionSet(actionSet);
  1019. } //}}}
  1020. //{{{ removeActionSet() method
  1021. /**
  1022. * Removes an action set from jEdit's list. Plugins probably won't
  1023. * need to call this method.
  1024. * @since jEdit 4.2pre1
  1025. */
  1026. public static void removeActionSet(ActionSet actionSet)
  1027. {
  1028. actionContext.removeActionSet(actionSet);
  1029. } //}}}
  1030. //{{{ getBuiltInActionSet() method
  1031. /**
  1032. * Returns the set of commands built into jEdit.
  1033. * @since jEdit 4.2pre1
  1034. */
  1035. public static ActionSet getBuiltInActionSet()
  1036. {
  1037. return builtInActionSet;
  1038. } //}}}
  1039. //{{{ getActionSets() method
  1040. /**
  1041. * Returns all registered action sets.
  1042. * @since jEdit 4.0pre1
  1043. */
  1044. public static ActionSet[] getActionSets()
  1045. {
  1046. return actionContext.getActionSets();
  1047. } //}}}
  1048. //{{{ getAction() method
  1049. /**
  1050. * Returns the specified action.
  1051. * @param name The action name
  1052. */
  1053. public static EditAction getAction(String name)
  1054. {
  1055. return actionContext.getAction(name);
  1056. } //}}}
  1057. //{{{ getActionSetForAction() method
  1058. /**
  1059. * Returns the action set that contains the specified action.
  1060. *
  1061. * @param action The action
  1062. * @since jEdit 4.2pre1
  1063. */
  1064. public static ActionSet getActionSetForAction(String action)
  1065. {
  1066. return actionContext.getActionSetForAction(action);
  1067. } //}}}
  1068. //{{{ getActionSetForAction() method
  1069. /**
  1070. * @deprecated Use the form that takes a String instead
  1071. */
  1072. public static ActionSet getActionSetForAction(EditAction action)
  1073. {
  1074. return actionContext.getActionSetForAction(action.getName());
  1075. } //}}}
  1076. //{{{ getActions() method
  1077. /**
  1078. * @deprecated Call getActionNames() instead
  1079. */
  1080. public static EditAction[] getActions()
  1081. {
  1082. String[] names = actionContext.getActionNames();
  1083. EditAction[] actions = new EditAction[names.length];
  1084. for(int i = 0; i < actions.length; i++)
  1085. {
  1086. actions[i] = actionContext.getAction(names[i]);
  1087. if(actions[i] == null)
  1088. Log.log(Log.ERROR,jEdit.class,"wtf: " + names[i]);
  1089. }
  1090. return actions;
  1091. } //}}}
  1092. //{{{ getActionNames() method
  1093. /**
  1094. * Returns all registered action names.
  1095. */
  1096. public static String[] getActionNames()
  1097. {
  1098. return actionContext.getActionNames();
  1099. } //}}}
  1100. //}}}
  1101. //{{{ Edit mode methods
  1102. //{{{ reloadModes() method
  1103. /**
  1104. * Reloads all edit modes.
  1105. * @since jEdit 3.2pre2
  1106. */
  1107. public static void reloadModes()
  1108. {
  1109. /* Try to guess the eventual size to avoid unnecessary
  1110. * copying */
  1111. modes = new Vector(50);
  1112. //{{{ Load the global catalog
  1113. if(jEditHome == null)
  1114. loadModeCatalog("/modes/catalog",true);
  1115. else
  1116. {
  1117. loadModeCatalog(MiscUtilities.constructPath(jEditHome,
  1118. "modes","catalog"),false);
  1119. } //}}}
  1120. //{{{ Load user catalog
  1121. if(settingsDirectory != null)
  1122. {
  1123. File userModeDir = new File(MiscUtilities.constructPath(
  1124. settingsDirectory,"modes"));
  1125. if(!userModeDir.exists())
  1126. userModeDir.mkdirs();
  1127. File userCatalog = new File(MiscUtilities.constructPath(
  1128. settingsDirectory,"modes","catalog"));
  1129. if(!userCatalog.exists())
  1130. {
  1131. // create dummy catalog
  1132. try
  1133. {
  1134. FileWriter out = new FileWriter(userCatalog);
  1135. out.write(jEdit.getProperty("defaultCatalog"));
  1136. out.close();
  1137. }
  1138. catch(IOException io)
  1139. {
  1140. Log.log(Log.ERROR,jEdit.class,io);
  1141. }
  1142. }
  1143. loadModeCatalog(userCatalog.getPath(),false);
  1144. } //}}}
  1145. Buffer buffer = buffersFirst;
  1146. while(buffer != null)
  1147. {
  1148. // This reloads the token marker and sends a message
  1149. // which causes edit panes to repaint their text areas
  1150. buffer.setMode();
  1151. buffer = buffer.next;
  1152. }
  1153. } //}}}
  1154. //{{{ getMode() method
  1155. /**
  1156. * Returns the edit mode with the specified name.
  1157. * @param name The edit mode
  1158. */
  1159. public static Mode getMode(String name)
  1160. {
  1161. for(int i = 0; i < modes.size(); i++)
  1162. {
  1163. Mode mode = (Mode)modes.elementAt(i);
  1164. if(mode.getName().equals(name))
  1165. return mode;
  1166. }
  1167. return null;
  1168. } //}}}
  1169. //{{{ getModes() method
  1170. /**
  1171. * Returns an array of installed edit modes.
  1172. */
  1173. public static Mode[] getModes()
  1174. {
  1175. Mode[] array = new Mode[modes.size()];
  1176. modes.copyInto(array);
  1177. return array;
  1178. } //}}}
  1179. //}}}
  1180. //{{{ Buffer creation methods
  1181. //{{{ openFiles() method
  1182. /**
  1183. * Opens the file names specified in the argument array. This
  1184. * handles +line and +marker arguments just like the command
  1185. * line parser.
  1186. * @param parent The parent directory
  1187. * @param args The file names to open
  1188. * @since jEdit 3.2pre4
  1189. */
  1190. public static Buffer openFiles(View view, String parent, String[] args)
  1191. {
  1192. Buffer retVal = null;
  1193. Buffer lastBuffer = null;
  1194. for(int i = 0; i < args.length; i++)
  1195. {
  1196. String arg = args[i];
  1197. if(arg == null)
  1198. continue;
  1199. else if(arg.startsWith("+line:") || arg.startsWith("+marker:"))
  1200. {
  1201. if(lastBuffer != null)
  1202. gotoMarker(view,lastBuffer,arg);
  1203. continue;
  1204. }
  1205. lastBuffer = openFile(null,parent,arg,false,null);
  1206. if(retVal == null && lastBuffer != null)
  1207. retVal = lastBuffer;
  1208. }
  1209. if(view != null && retVal != null)
  1210. view.setBuffer(retVal);
  1211. return retVal;
  1212. } //}}}
  1213. //{{{ openFile() method
  1214. /**
  1215. * Opens a file. Note that as of jEdit 2.5pre1, this may return
  1216. * null if the buffer could not be opened.
  1217. * @param view The view to open the file in
  1218. * @param path The file path
  1219. *
  1220. * @since jEdit 2.4pre1
  1221. */
  1222. public static Buffer openFile(View view, String path)
  1223. {
  1224. return openFile(view,null,path,false,new Hashtable());
  1225. } //}}}
  1226. //{{{ openFile() method
  1227. /**
  1228. * @deprecated The openFile() forms with the readOnly parameter
  1229. * should not be used. The readOnly prameter is no longer supported.
  1230. */
  1231. public static Buffer openFile(View view, String parent,
  1232. String path, boolean readOnly, boolean newFile)
  1233. {
  1234. return openFile(view,parent,path,newFile,new Hashtable());
  1235. } //}}}
  1236. //{{{ openFile() method
  1237. /**
  1238. * @deprecated The openFile() forms with the readOnly parameter
  1239. * should not be used. The readOnly prameter is no longer supported.
  1240. */
  1241. public static Buffer openFile(View view, String parent,
  1242. String path, boolean readOnly, boolean newFile,
  1243. Hashtable props)
  1244. {
  1245. return openFile(view,parent,path,newFile,props);
  1246. } //}}}
  1247. //{{{ openFile() method
  1248. /**
  1249. * Opens a file. This may return null if the buffer could not be
  1250. * opened for some reason.
  1251. * @param view The view to open the file in
  1252. * @param parent The parent directory of the file
  1253. * @param path The path name of the file
  1254. * @param newFile True if the file should not be loaded from disk
  1255. * be prompted if it should be reloaded
  1256. * @param props Buffer-local properties to set in the buffer
  1257. *
  1258. * @since jEdit 3.2pre10
  1259. */
  1260. public static Buffer openFile(View view, String parent,
  1261. String path, boolean newFile, Hashtable props)
  1262. {
  1263. if(view != null && parent == null)
  1264. parent = view.getBuffer().getDirectory();
  1265. if(MiscUtilities.isURL(path))
  1266. {
  1267. if(MiscUtilities.getProtocolOfURL(path).equals("file"))
  1268. path = path.substring(5);
  1269. }
  1270. path = MiscUtilities.constructPath(parent,path);
  1271. Buffer newBuffer;
  1272. synchronized(bufferListLock)
  1273. {
  1274. Buffer buffer = getBuffer(path);
  1275. if(buffer != null)
  1276. {
  1277. if(view != null)
  1278. view.setBuffer(buffer);
  1279. return buffer;
  1280. }
  1281. if(props == null)
  1282. props = new Hashtable();
  1283. BufferHistory.Entry entry = BufferHistory.getEntry(path);
  1284. if(entry != null && saveCaret && props.get(Buffer.CARET) == null)
  1285. {
  1286. props.put(Buffer.CARET,new Integer(entry.caret));
  1287. /* if(entry.selection != null)
  1288. {
  1289. // getSelection() converts from string to
  1290. // Selection[]
  1291. props.put(Buffer.SELECTION,entry.getSelection());
  1292. } */
  1293. }
  1294. if(entry != null && props.get(Buffer.ENCODING) == null)
  1295. {
  1296. if(entry.encoding != null)
  1297. props.put(Buffer.ENCODING,entry.encoding);
  1298. }
  1299. newBuffer = new Buffer(path,newFile,false,props);
  1300. if(!newBuffer.load(view,false))
  1301. return null;
  1302. addBufferToList(newBuffer);
  1303. }
  1304. EditBus.send(new BufferUpdate(newBuffer,view,BufferUpdate.CREATED));
  1305. if(view != null)
  1306. view.setBuffer(newBuffer);
  1307. return newBuffer;
  1308. } //}}}
  1309. //{{{ openTemporary() method
  1310. /**
  1311. * Opens a temporary buffer. A temporary buffer is like a normal
  1312. * buffer, except that an event is not fired, the the buffer is
  1313. * not added to the buffers list.
  1314. *
  1315. * @param view The view to open the file in
  1316. * @param parent The parent directory of the file
  1317. * @param path The path name of the file
  1318. * @param newFile True if the file should not be loaded from disk
  1319. *
  1320. * @since jEdit 3.2pre10
  1321. */
  1322. public static Buffer openTemporary(View view, String parent,
  1323. String path, boolean newFile)
  1324. {
  1325. if(view != null && parent == null)
  1326. parent = view.getBuffer().getDirectory();
  1327. if(MiscUtilities.isURL(path))
  1328. {
  1329. if(MiscUtilities.getProtocolOfURL(path).equals("file"))
  1330. path = path.substring(5);
  1331. }
  1332. path = MiscUtilities.constructPath(parent,path);
  1333. synchronized(bufferListLock)
  1334. {
  1335. Buffer buffer = getBuffer(path);
  1336. if(buffer != null)
  1337. return buffer;
  1338. buffer = new Buffer(path,newFile,true,new Hashtable());
  1339. if(!buffer.load(view,false))
  1340. return null;
  1341. else
  1342. return buffer;
  1343. }
  1344. } //}}}
  1345. //{{{ commitTemporary() method
  1346. /**
  1347. * Adds a temporary buffer to the buffer list. This must be done
  1348. * before allowing the user to interact with the buffer in any
  1349. * way.
  1350. * @param buffer The buffer
  1351. */
  1352. public static void commitTemporary(Buffer buffer)
  1353. {
  1354. if(!buffer.isTemporary())
  1355. return;
  1356. addBufferToList(buffer);
  1357. buffer.commitTemporary();
  1358. // send full range of events to avoid breaking plugins
  1359. EditBus.send(new BufferUpdate(buffer,null,BufferUpdate.CREATED));
  1360. EditBus.send(new BufferUpdate(buffer,null,BufferUpdate.LOAD_STARTED));
  1361. EditBus.send(new BufferUpdate(buffer,null,BufferUpdate.LOADED));
  1362. } //}}}
  1363. //{{{ newFile() method
  1364. /**
  1365. * Creates a new `untitled' file.
  1366. * @param view The view to create the file in
  1367. */
  1368. public static Buffer newFile(View view)
  1369. {
  1370. String path;
  1371. if(view != null && view.getBuffer() != null)
  1372. {
  1373. path = view.getBuffer().getDirectory();
  1374. VFS vfs = VFSManager.getVFSForPath(path);
  1375. // don't want 'New File' to create a read only buffer
  1376. // if current file is on SQL VFS or something
  1377. if((vfs.getCapabilities() & VFS.WRITE_CAP) == 0)
  1378. path = System.getProperty("user.home");
  1379. }
  1380. else
  1381. path = null;
  1382. return newFile(view,path);
  1383. } //}}}
  1384. //{{{ newFile() method
  1385. /**
  1386. * Creates a new `untitled' file.
  1387. * @param view The view to create the file in
  1388. * @param dir The directory to create the file in
  1389. * @since jEdit 3.1pre2
  1390. */
  1391. public static Buffer newFile(View view, String dir)
  1392. {
  1393. // If only one new file is open which is clean, just close
  1394. // it, which will create an 'Untitled-1'
  1395. if(dir != null
  1396. && buffersFirst != null
  1397. && buffersFirst == buffersLast
  1398. && buffersFirst.isUntitled()
  1399. && !buffersFirst.isDirty())
  1400. {
  1401. closeBuffer(view,buffersFirst);
  1402. // return the newly created 'untitled-1'
  1403. return buffersFirst;
  1404. }
  1405. // Find the highest Untitled-n file
  1406. int untitledCount = 0;
  1407. Buffer buffer = buffersFirst;
  1408. while(buffer != null)
  1409. {
  1410. if(buffer.getName().startsWith("Untitled-"))
  1411. {
  1412. try
  1413. {
  1414. untitledCount = Math.max(untitledCount,
  1415. Integer.parseInt(buffer.getName()
  1416. .substring(9)));
  1417. }
  1418. catch(NumberFormatException nf)
  1419. {
  1420. }
  1421. }
  1422. buffer = buffer.next;
  1423. }
  1424. return openFile(view,dir,"Untitled-" + (untitledCount+1),true,null);
  1425. } //}}}
  1426. //}}}
  1427. //{{{ Buffer management methods
  1428. //{{{ closeBuffer() method
  1429. /**
  1430. * Closes a buffer. If there are unsaved changes, the user is
  1431. * prompted if they should be saved first.
  1432. * @param view The view
  1433. * @param buffer The buffer
  1434. * @return True if the buffer was really closed, false otherwise
  1435. */
  1436. public static boolean closeBuffer(View view, Buffer buffer)
  1437. {
  1438. // Wait for pending I/O requests
  1439. if(buffer.isPerformingIO())
  1440. {
  1441. VFSManager.waitForRequests();
  1442. if(VFSManager.errorOccurred())
  1443. return false;
  1444. }
  1445. if(buffer.isDirty())
  1446. {
  1447. Object[] args = { buffer.getName() };
  1448. int result = GUIUtilities.confirm(view,"notsaved",args,
  1449. JOptionPane.YES_NO_CANCEL_OPTION,
  1450. JOptionPane.WARNING_MESSAGE);
  1451. if(result == JOptionPane.YES_OPTION)
  1452. {
  1453. if(!buffer.save(view,null,true))
  1454. return false;
  1455. VFSManager.waitForRequests();
  1456. if(buffer.getBooleanProperty(BufferIORequest
  1457. .ERROR_OCCURRED))
  1458. {
  1459. return false;
  1460. }
  1461. }
  1462. else if(result != JOptionPane.NO_OPTION)
  1463. return false;
  1464. }
  1465. _closeBuffer(view,buffer);
  1466. return true;
  1467. } //}}}
  1468. //{{{ _closeBuffer() method
  1469. /**
  1470. * Closes the buffer, even if it has unsaved changes.
  1471. * @param view The view
  1472. * @param buffer The buffer
  1473. *
  1474. * @since jEdit 2.2pre1
  1475. */
  1476. public static void _closeBuffer(View view, Buffer buffer)
  1477. {
  1478. if(buffer.isClosed())
  1479. {
  1480. // can happen if the user presses C+w twice real
  1481. // quick and the buffer has unsaved changes
  1482. return;
  1483. }
  1484. if(!buffer.isNewFile())
  1485. {
  1486. view.getEditPane().saveCaretInfo();
  1487. Integer _caret = (Integer)buffer.getProperty(Buffer.CARET);
  1488. int caret = (_caret == null ? 0 : _caret.intValue());
  1489. BufferHistory.setEntry(buffer.getPath(),caret,
  1490. (Selection[])buffer.getProperty(Buffer.SELECTION),
  1491. buffer.getStringProperty(Buffer.ENCODING));
  1492. }
  1493. String path = buffer.getSymlinkPath();
  1494. if((VFSManager.getVFSForPath(path).getCapabilities()
  1495. & VFS.CASE_INSENSITIVE_CAP) != 0)
  1496. {
  1497. path = path.toLowerCase();
  1498. }
  1499. bufferHash.remove(path);
  1500. removeBufferFromList(buffer);
  1501. buffer.close();
  1502. DisplayManager.bufferClosed(buffer);
  1503. EditBus.send(new BufferUpdate(buffer,view,BufferUpdate.CLOSED));
  1504. // Create a new file when the last is closed
  1505. if(buffersFirst == null && buffersLast == null)
  1506. newFile(view);
  1507. } //}}}
  1508. //{{{ closeAllBuffers() method
  1509. /**
  1510. * Closes all open buffers.
  1511. * @param view The view
  1512. */
  1513. public static boolean closeAllBuffers(View view)
  1514. {
  1515. return closeAllBuffers(view,false);
  1516. } //}}}
  1517. //{{{ closeAllBuffers() method
  1518. /**
  1519. * Closes all open buffers.
  1520. * @param view The view
  1521. * @param isExiting This must be false unless this method is
  1522. * being called by the exit() method
  1523. */
  1524. public static boolean closeAllBuffers(View view, boolean isExiting)
  1525. {
  1526. boolean dirty = false;
  1527. Buffer buffer = buffersFirst;
  1528. while(buffer != null)
  1529. {
  1530. if(buffer.isDirty())
  1531. {
  1532. dirty = true;
  1533. break;
  1534. }
  1535. buffer = buffer.next;
  1536. }
  1537. if(dirty)
  1538. {
  1539. boolean ok = new CloseDialog(view).isOK();
  1540. if(!ok)
  1541. return false;
  1542. }
  1543. // Wait for pending I/O requests
  1544. VFSManager.waitForRequests();
  1545. if(VFSManager.errorOccurred())
  1546. return false;
  1547. // close remaining buffers (the close dialog only deals with
  1548. // dirty ones)
  1549. buffer = buffersFirst;
  1550. // zero it here so that BufferTabs doesn't have any problems
  1551. buffersFirst = buffersLast = null;
  1552. bufferHash.clear();
  1553. bufferCount = 0;
  1554. while(buffer != null)
  1555. {
  1556. if(!buffer.isNewFile())
  1557. {
  1558. Integer _caret = (Integer)buffer.getProperty(Buffer.CARET);
  1559. int caret = (_caret == null ? 0 : _caret.intValue());
  1560. BufferHistory.setEntry(buffer.getPath(),caret,
  1561. (Selection[])buffer.getProperty(Buffer.SELECTION),
  1562. buffer.getStringProperty(Buffer.ENCODING));
  1563. }
  1564. buffer.close();
  1565. DisplayManager.bufferClosed(buffer);
  1566. if(!isExiting)
  1567. {
  1568. EditBus.send(new BufferUpdate(buffer,view,
  1569. BufferUpdate.CLOSED));
  1570. }
  1571. buffer = buffer.next;
  1572. }
  1573. if(!isExiting)
  1574. newFile(view);
  1575. return true;
  1576. } //}}}
  1577. //{{{ saveAllBuffers() method
  1578. /**
  1579. * Saves all open buffers.
  1580. * @param view The view
  1581. * @since jEdit 4.2pre1
  1582. */
  1583. public static void saveAllBuffers(View view)
  1584. {
  1585. saveAllBuffers(view,jEdit.getBooleanProperty("confirmSaveAll"));
  1586. } //}}}
  1587. //{{{ saveAllBuffers() method
  1588. /**
  1589. * Saves all open buffers.
  1590. * @param view The view
  1591. * @param confirm If true, a confirmation dialog will be shown first
  1592. * @since jEdit 2.7pre2
  1593. */
  1594. public static void saveAllBuffers(View view, boolean confirm)
  1595. {
  1596. if(confirm)
  1597. {
  1598. int result = GUIUtilities.confirm(view,"saveall",null,
  1599. JOptionPane.YES_NO_OPTION,
  1600. JOptionPane.QUESTION_MESSAGE);
  1601. if(result != JOptionPane.YES_OPTION)
  1602. return;
  1603. }
  1604. Buffer current = view.getBuffer();
  1605. Buffer buffer = buffersFirst;
  1606. while(buffer != null)
  1607. {
  1608. if(buffer.isDirty())
  1609. {
  1610. if(buffer.isNewFile())
  1611. view.setBuffer(buffer);
  1612. buffer.save(view,null,true);
  1613. }
  1614. buffer = buffer.next;
  1615. }
  1616. view.setBuffer(current);
  1617. } //}}}
  1618. //{{{ reloadAllBuffers() method
  1619. /**
  1620. * Reloads all open buffers.
  1621. * @param view The view
  1622. * @param confirm If true, a confirmation dialog will be shown first
  1623. * if any buffers are dirty
  1624. * @since jEdit 2.7pre2
  1625. */
  1626. public static void reloadAllBuffers(final View view, boolean confirm)
  1627. {
  1628. boolean hasDirty = false;
  1629. Buffer[] buffers = jEdit.getBuffers();
  1630. for(int i = 0; i < buffers.length && hasDirty == false; i++)
  1631. hasDirty = buffers[i].isDirty();
  1632. if(confirm && hasDirty)
  1633. {
  1634. int result = GUIUtilities.confirm(view,"reload-all",null,
  1635. JOptionPane.YES_NO_OPTION,
  1636. JOptionPane.QUESTION_MESSAGE);
  1637. if(result != JOptionPane.YES_OPTION)
  1638. return;
  1639. }
  1640. // save caret info. Buffer.load() will load it.
  1641. View _view = viewsFirst;
  1642. while(_view != null)
  1643. {
  1644. EditPane[] panes = _view.getEditPanes();
  1645. for(int i = 0; i < panes.length; i++)
  1646. {
  1647. panes[i].saveCaretInfo();
  1648. }
  1649. _view = _view.next;
  1650. }
  1651. for(int i = 0; i < buffers.length; i++)
  1652. {
  1653. Buffer buffer = buffers[i];
  1654. buffer.load(view,true);
  1655. }
  1656. } //}}}
  1657. //{{{ _getBuffer() method
  1658. /**
  1659. * Returns the buffer with the specified path name. The path name
  1660. * must be an absolute, canonical, path.
  1661. *
  1662. * @param path The path name
  1663. * @see MiscUtilities#constructPath(String,String)
  1664. * @see MiscUtilities#resolveSymlinks(String)
  1665. * @see #getBuffer(String)
  1666. *
  1667. * @since jEdit 4.2pre7
  1668. */
  1669. public static Buffer _getBuffer(String path)
  1670. {
  1671. // paths on case-insensitive filesystems are stored as lower
  1672. // case in the hash.
  1673. if((VFSManager.getVFSForPath(path).getCapabilities()
  1674. & VFS.CASE_INSENSITIVE_CAP) != 0)
  1675. {
  1676. path = path.toLowerCase();
  1677. }
  1678. synchronized(bufferListLock)
  1679. {
  1680. return (Buffer)bufferHash.get(path);
  1681. }
  1682. } //}}}
  1683. //{{{ getBuffer() method
  1684. /**
  1685. * Returns the buffer with the specified path name. The path name
  1686. * must be an absolute path. This method automatically resolves
  1687. * symbolic links. If performance is critical, cache the canonical
  1688. * path and call {@link #_getBuffer(String)} instead.
  1689. *
  1690. * @param path The path name
  1691. * @see MiscUtilities#constructPath(String,String)
  1692. * @see MiscUtilities#resolveSymlinks(String)
  1693. */
  1694. public static Buffer getBuffer(String path)
  1695. {
  1696. if(MiscUtilities.isURL(path))
  1697. return _getBuffer(path);
  1698. else
  1699. return _getBuffer(MiscUtilities.resolveSymlinks(path));
  1700. } //}}}
  1701. //{{{ getBuffers() method
  1702. /**
  1703. * Returns an array of open buffers.
  1704. */
  1705. public static Buffer[] getBuffers()
  1706. {
  1707. synchronized(bufferListLock)
  1708. {
  1709. Buffer[] buffers = new Buffer[bufferCount];
  1710. Buffer buffer = buffersFirst;
  1711. for(int i = 0; i < bufferCount; i++)
  1712. {
  1713. buffers[i] = buffer;
  1714. buffer = buffer.next;
  1715. }
  1716. return buffers;
  1717. }
  1718. } //}}}
  1719. //{{{ getBufferCount() method
  1720. /**
  1721. * Returns the number of open buffers.
  1722. */
  1723. public static int getBufferCount()
  1724. {
  1725. return bufferCount;
  1726. } //}}}
  1727. //{{{ getFirstBuffer() method
  1728. /**
  1729. * Returns the first buffer.
  1730. */
  1731. public static Buffer getFirstBuffer()
  1732. {
  1733. return buffersFirst;
  1734. } //}}}
  1735. //{{{ getLastBuffer() method
  1736. /**
  1737. * Returns the last buffer.
  1738. */
  1739. public static Buffer getLastBuffer()
  1740. {
  1741. return buffersLast;
  1742. } //}}}
  1743. //{{{ checkBufferStatus() method
  1744. /**
  1745. * Checks each buffer's status on disk and shows the dialog box
  1746. * informing the user that buffers changed on disk, if necessary.
  1747. * @param view The view
  1748. * @since jEdit 4.2pre1
  1749. */
  1750. public static void checkBufferStatus(View view)
  1751. {
  1752. // still need to call the status check even if the option is
  1753. // off, so that the write protection is updated if it changes
  1754. // on disk
  1755. boolean showDialogSetting = getBooleanProperty(
  1756. "autoReloadDialog");
  1757. // auto reload changed buffers?
  1758. boolean autoReloadSetting = getBooleanProperty(
  1759. "autoReload");
  1760. // the problem with this is that if we have two edit panes
  1761. // looking at the same buffer and the file is reloaded both
  1762. // will jump to the same location
  1763. View _view = viewsFirst;
  1764. while(_view != null)
  1765. {
  1766. EditPane[] editPanes = _view.getEditPanes();
  1767. for(int i = 0; i < editPanes.length; i++)
  1768. {
  1769. editPanes[i].saveCaretInfo();
  1770. }
  1771. _view = _view.next;
  1772. }
  1773. Buffer buffer = buffersFirst;
  1774. int[] states = new int[bufferCount];
  1775. int i = 0;
  1776. boolean show = false;
  1777. while(buffer != null)
  1778. {
  1779. states[i] = buffer.checkFileStatus(view);
  1780. switch(states[i])
  1781. {
  1782. case Buffer.FILE_CHANGED:
  1783. if(autoReloadSetting
  1784. && showDialogSetting
  1785. && !buffer.isDirty())
  1786. {
  1787. buffer.load(view,true);
  1788. }
  1789. /* fall through */
  1790. case Buffer.FILE_DELETED:
  1791. show = true;
  1792. break;
  1793. }
  1794. buffer = buffer.next;
  1795. i++;
  1796. }
  1797. if(show && showDialogSetting)
  1798. new FilesChangedDialog(view,states,autoReloadSetting);
  1799. } //}}}
  1800. //}}}
  1801. //{{{ View methods
  1802. //{{{ getInputHandler() method
  1803. /**
  1804. * Returns the current input handler (key binding to action mapping)
  1805. * @see org.gjt.sp.jedit.gui.InputHandler
  1806. */
  1807. public static InputHandler getInputHandler()
  1808. {
  1809. return inputHandler;
  1810. } //}}}
  1811. /* public static void newViewTest()
  1812. {
  1813. long time = System.currentTimeMillis();
  1814. for(int i = 0; i < 30; i++)
  1815. {
  1816. Buffer b = newFile(null);
  1817. b.insert(0,"x");
  1818. new View(b…