PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/doc/users-guide/bsh-commands.xml

#
XML | 718 lines | 453 code | 119 blank | 146 comment | 0 complexity | b01c35c9a3c1792464aa45a6f7777dcf MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <chapter id="bsh-commands">
  3. <!-- jEdit buffer local properties:
  4. :indentSize=1:noTabs=yes:maxLineLen=72:tabSize=2:
  5. :xml.root=users-guide.xml:
  6. jEdit 3.2 Macro Guide, (C) 2001 John Gellene
  7. Thu Jun 21 23:49:14 EDT 2001 @200 /Internet Time/ -->
  8. <title>BeanShell Commands</title>
  9. <para>BeanShell includes a set of <firstterm>commands</firstterm>;
  10. subroutines that can be called from any script or macro. The following is a
  11. summary of those commands which may be useful within jEdit.</para>
  12. <note>
  13. <para>Java classes in plugins cannot make use of BeanShell commands
  14. directly. However, these commands can be called from BeanShell code that
  15. is part of a plugin, for example the snippets in
  16. <filename>actions.xml</filename>, or any BeanShell scripts shipped with
  17. the plugin and loaded on startup.</para>
  18. </note>
  19. <section id="bsh-commands-output">
  20. <title>Output Commands</title>
  21. <itemizedlist>
  22. <!-- one day jEdit will provide its own error() implementation -->
  23. <!-- <listitem>
  24. <funcsynopsis>
  25. <funcprototype>
  26. <funcdef>void <function>error</function></funcdef>
  27. <paramdef>String <parameter>errorMsg</parameter></paramdef>
  28. </funcprototype>
  29. </funcsynopsis>
  30. <para>
  31. Writes the string represented by <parameter>errorMsg</parameter>
  32. to the error stream of the current process.
  33. </para>
  34. </listitem> -->
  35. <listitem>
  36. <funcsynopsis>
  37. <funcprototype>
  38. <funcdef>void <function>cat</function></funcdef>
  39. <paramdef>String
  40. <parameter>filename</parameter></paramdef>
  41. </funcprototype>
  42. </funcsynopsis>
  43. <para>Writes the contents of <parameter>filename</parameter> to
  44. the activity log.</para>
  45. </listitem>
  46. <listitem>
  47. <funcsynopsis>
  48. <funcprototype>
  49. <funcdef>void <function>javap</function></funcdef>
  50. <paramdef>String | Object | Class
  51. <parameter>target</parameter></paramdef>
  52. </funcprototype>
  53. </funcsynopsis>
  54. <para>Writes the public fields and methods of the specified
  55. class to the output stream of the current process. </para>
  56. </listitem>
  57. <listitem>
  58. <funcsynopsis>
  59. <funcprototype>
  60. <funcdef>void <function>print</function></funcdef>
  61. <paramdef><parameter>arg</parameter></paramdef>
  62. </funcprototype>
  63. </funcsynopsis>
  64. <para>Writes the string value of the argument to the activity
  65. log, or if run from the <application>Console</application>
  66. plugin, to the current output window. If
  67. <parameter>arg</parameter> is an array,
  68. <function>print</function> runs itself recursively on the
  69. array's elements.</para>
  70. </listitem>
  71. </itemizedlist>
  72. </section>
  73. <section id="bsh-commands-file">
  74. <title>File Management Commands</title>
  75. <itemizedlist>
  76. <listitem>
  77. <funcsynopsis>
  78. <funcprototype>
  79. <funcdef>void <function>cd</function></funcdef>
  80. <paramdef>String
  81. <parameter>dirname</parameter></paramdef>
  82. </funcprototype>
  83. </funcsynopsis>
  84. <para>Changes the working directory of the BeanShell interpreter
  85. to <parameter>dirname</parameter>.</para>
  86. </listitem>
  87. <listitem>
  88. <funcsynopsis>
  89. <funcprototype>
  90. <funcdef>void <function>cp</function></funcdef>
  91. <paramdef>String
  92. <parameter>fromFile</parameter></paramdef>
  93. <paramdef>String
  94. <parameter>toFile</parameter></paramdef>
  95. </funcprototype>
  96. </funcsynopsis>
  97. <para>Copy <parameter>fromFile</parameter> to
  98. <parameter>toFile</parameter>.</para>
  99. </listitem>
  100. <listitem>
  101. <funcsynopsis>
  102. <funcprototype>
  103. <funcdef>void <function>dir</function></funcdef>
  104. <paramdef>String
  105. <parameter>dirname</parameter></paramdef>
  106. </funcprototype>
  107. </funcsynopsis>
  108. <para>Displays the contents of directory
  109. <parameter>dirname</parameter>. The format of the display is
  110. similar to the Unix <function>ls -l</function> command.</para>
  111. </listitem>
  112. <listitem>
  113. <funcsynopsis>
  114. <funcprototype>
  115. <funcdef>void <function>mv</function></funcdef>
  116. <paramdef>String
  117. <parameter>fromFile</parameter></paramdef>
  118. <paramdef>String
  119. <parameter>toFile</parameter></paramdef>
  120. </funcprototype>
  121. </funcsynopsis>
  122. <para>Moves the file named by <parameter>fromFile</parameter> to
  123. <parameter>toFile</parameter>.</para>
  124. </listitem>
  125. <listitem>
  126. <funcsynopsis>
  127. <funcprototype>
  128. <funcdef>File <function>pathToFile</function></funcdef>
  129. <paramdef>String
  130. <parameter>filename</parameter></paramdef>
  131. </funcprototype>
  132. </funcsynopsis>
  133. <para>Create a <classname>File</classname> object corresponding
  134. to <function>filename</function>. Relative paths are resolved
  135. with reference to the BeanShell interpreter's working
  136. directory.</para>
  137. </listitem>
  138. <listitem>
  139. <funcsynopsis>
  140. <funcprototype>
  141. <funcdef>void <function>pwd</function></funcdef>
  142. <void />
  143. </funcprototype>
  144. </funcsynopsis>
  145. <para>Writes the current working directory of the BeanShell
  146. interpreter to the output stream of the current process.</para>
  147. </listitem>
  148. <listitem>
  149. <funcsynopsis>
  150. <funcprototype>
  151. <funcdef>void <function>rm</function></funcdef>
  152. <paramdef>String
  153. <parameter>pathname</parameter></paramdef>
  154. </funcprototype>
  155. </funcsynopsis>
  156. <para>Deletes the file name by
  157. <parameter>pathname</parameter>.</para>
  158. </listitem>
  159. </itemizedlist>
  160. </section>
  161. <section id="bsh-commands-serial">
  162. <title>Component Commands</title>
  163. <itemizedlist>
  164. <listitem>
  165. <funcsynopsis>
  166. <funcprototype>
  167. <funcdef>JFrame <function>frame</function></funcdef>
  168. <paramdef>Component
  169. <parameter>frame</parameter></paramdef>
  170. </funcprototype>
  171. </funcsynopsis>
  172. <para>Displays the component in a top-level
  173. <classname>JFrame</classname>, centered and packed. Returns the
  174. <classname>JFrame</classname> object.</para>
  175. </listitem>
  176. <listitem>
  177. <funcsynopsis>
  178. <funcprototype>
  179. <funcdef>Object <function>load</function></funcdef>
  180. <paramdef>String
  181. <parameter>filename</parameter></paramdef>
  182. </funcprototype>
  183. </funcsynopsis>
  184. <para>Loads and returns a serialized Java object from
  185. <parameter>filename</parameter>.</para>
  186. </listitem>
  187. <listitem>
  188. <funcsynopsis>
  189. <funcprototype>
  190. <funcdef>void <function>save</function></funcdef>
  191. <paramdef>Component
  192. <parameter>component</parameter></paramdef>
  193. <paramdef>String
  194. <parameter>filename</parameter></paramdef>
  195. </funcprototype>
  196. </funcsynopsis>
  197. <para>Saves <parameter>component</parameter> in serialized form
  198. to <parameter>filename</parameter>.</para>
  199. </listitem>
  200. <listitem>
  201. <funcsynopsis>
  202. <funcprototype>
  203. <funcdef>Font <function>setFont</function></funcdef>
  204. <paramdef>Component
  205. <parameter>comp</parameter></paramdef>
  206. <paramdef>int <parameter>ptsize</parameter></paramdef>
  207. </funcprototype>
  208. </funcsynopsis>
  209. <para>Set the font size of <parameter>component</parameter> to
  210. <parameter>ptsize</parameter> and returns the new font.</para>
  211. </listitem>
  212. </itemizedlist>
  213. </section>
  214. <section id="bsh-commands-resource">
  215. <title>Resource Management Commands</title>
  216. <itemizedlist>
  217. <listitem>
  218. <funcsynopsis>
  219. <funcprototype>
  220. <funcdef>URL <function>getResource</function></funcdef>
  221. <paramdef>String <parameter>path</parameter></paramdef>
  222. </funcprototype>
  223. </funcsynopsis>
  224. <para>Returns the resource specified by
  225. <parameter>path</parameter>. An absolute path must be used to
  226. return any resource available in the current classpath.</para>
  227. </listitem>
  228. <!-- bsh.classpath not supported in jEdit -->
  229. <!--
  230. <listitem>
  231. <funcsynopsis>
  232. <funcprototype>
  233. <funcdef>void <function>addClassPath</function></funcdef>
  234. <paramdef>String <parameter>path</parameter></paramdef>
  235. </funcprototype>
  236. </funcsynopsis>
  237. </listitem>
  238. <listitem>
  239. <funcsynopsis>
  240. <funcprototype>
  241. <funcdef>void <function>addClassPath</function></funcdef>
  242. <paramdef>URL <parameter>path</parameter></paramdef>
  243. </funcprototype>
  244. </funcsynopsis>
  245. <para>
  246. Adds the directory or jar archive file specified by
  247. <parameter>path</parameter> to the classpath used by the
  248. interpreter.
  249. </para>
  250. </listitem>
  251. <listitem>
  252. <funcsynopsis>
  253. <funcprototype>
  254. <funcdef>void <function>setClassPath</function></funcdef>
  255. <paramdef>URL[] <parameter>paths</parameter></paramdef>
  256. </funcprototype>
  257. </funcsynopsis>
  258. <para>
  259. Changes the classpath for the BeanShell interpreter to the specified
  260. array of directories and/or archives.
  261. </para>
  262. </listitem>
  263. <listitem>
  264. <funcsynopsis>
  265. <funcprototype>
  266. <funcdef>URL[] <function>getClassPath</function></funcdef>
  267. <void/>
  268. </funcprototype>
  269. </funcsynopsis>
  270. <para>
  271. Returns an array containing the current classpath.
  272. </para>
  273. </listitem>
  274. <listitem>
  275. <funcsynopsis>
  276. <funcprototype>
  277. <funcdef>void <function>reloadClasses</function></funcdef>
  278. <paramdef>String <parameter>packagename</parameter></paramdef>
  279. </funcprototype>
  280. </funcsynopsis>
  281. </listitem>
  282. <listitem>
  283. <funcsynopsis>
  284. <funcprototype>
  285. <funcdef>void <function>reloadClasses</function></funcdef>
  286. <void/>
  287. </funcprototype>
  288. </funcsynopsis>
  289. <para>
  290. Reloads the class or package specified by
  291. <parameter>packagename</parameter> in the BeanShell interpreter.
  292. If called without a parameter, reloads all classes
  293. in the interpreter's classpath.
  294. </para>
  295. </listitem>
  296. <listitem>
  297. <funcsynopsis>
  298. <funcprototype>
  299. <funcdef>Class <function>getClass</function></funcdef>
  300. <paramdef>String <parameter>classname</parameter></paramdef>
  301. </funcprototype>
  302. </funcsynopsis>
  303. <para>
  304. This method is similar to the <function>Class.forName()</function>
  305. method of the Java platform, but it uses the classpath of the
  306. current BeanShell interpreter, which can contain additional
  307. classes and packages as well as reloaded classes.
  308. </para>
  309. </listitem> -->
  310. </itemizedlist>
  311. </section>
  312. <section id="bsh-commands-script">
  313. <title>Script Execution Commands</title>
  314. <itemizedlist>
  315. <listitem>
  316. <funcsynopsis>
  317. <funcprototype>
  318. <funcdef>Thread <function>bg</function></funcdef>
  319. <paramdef>String
  320. <parameter>filename</parameter></paramdef>
  321. </funcprototype>
  322. </funcsynopsis>
  323. <para>Run the BeanShell script named by
  324. <parameter>filename</parameter> in a copy of the existing
  325. namespace and in a separate thread. Returns the
  326. <classname>Thread</classname> object so created.</para>
  327. </listitem>
  328. <listitem>
  329. <funcsynopsis>
  330. <funcprototype>
  331. <funcdef>void <function>exec</function></funcdef>
  332. <paramdef>String
  333. <parameter>cmdline</parameter></paramdef>
  334. </funcprototype>
  335. </funcsynopsis>
  336. <para>Start the external process by calling
  337. <function>Runtime.exec()</function> on
  338. <parameter>cmdline</parameter>. Any output is directed to the
  339. output stream of the calling process.</para>
  340. </listitem>
  341. <listitem>
  342. <funcsynopsis>
  343. <funcprototype>
  344. <funcdef>Object <function>eval</function></funcdef>
  345. <paramdef>String
  346. <parameter>expression</parameter></paramdef>
  347. </funcprototype>
  348. </funcsynopsis>
  349. <para>Evaluates the string <parameter>expression</parameter> as
  350. a BeanShell script in the interpreter's current namespace.
  351. Returns the result of the evaluation of
  352. <constant>null</constant>.</para>
  353. </listitem>
  354. <listitem>
  355. <funcsynopsis>
  356. <funcprototype>
  357. <funcdef>org.gjt.sp.jedit.bsh.This
  358. <function>run</function></funcdef>
  359. <paramdef>String
  360. <parameter>filename</parameter></paramdef>
  361. </funcprototype>
  362. </funcsynopsis>
  363. <para>Run the BeanShell script named by
  364. <parameter>filename</parameter> in a copy of the existing
  365. namespace. The return value represent the object context of the
  366. script, allowing you to access its variables and methods.</para>
  367. </listitem>
  368. <listitem>
  369. <funcsynopsis>
  370. <funcprototype>
  371. <funcdef>void
  372. <function>setAccessibility</function></funcdef>
  373. <paramdef>boolean <parameter>flag</parameter></paramdef>
  374. </funcprototype>
  375. </funcsynopsis>
  376. <para>If <parameter>flag</parameter> is <literal>true</literal>,
  377. BeanShell scripts are allowed to change and modify private
  378. variables, and call private methods. The default is
  379. <literal>false</literal>.</para>
  380. </listitem>
  381. <listitem>
  382. <funcsynopsis>
  383. <funcprototype>
  384. <funcdef>void
  385. <function>setStrictJava</function></funcdef>
  386. <paramdef>boolean <parameter>flag</parameter></paramdef>
  387. </funcprototype>
  388. </funcsynopsis>
  389. <para>If <parameter>flag</parameter> is <literal>true</literal>,
  390. BeanShell scripts must follow a much more strict, Java-like
  391. syntax, and are not able to use the convenience features
  392. described in <xref
  393. linkend="macro-tips-BeanShell-convenience" />.</para>
  394. </listitem>
  395. <!-- <listitem>
  396. <funcsynopsis>
  397. <funcprototype>
  398. <funcdef>void <function>server</function></funcdef>
  399. <paramdef>int <parameter>port</parameter></paramdef>
  400. </funcprototype>
  401. </funcsynopsis>
  402. <para>
  403. Creates a <quote>server</quote> version of the BeanShell
  404. interpreter that shares the same namespace as the current
  405. interpreter. The server interpreter listens on the designated
  406. port.
  407. </para>
  408. <para>
  409. This requires the <classname>bsh.util</classname> package, which
  410. is not included with jEdit. It can be found in the stand-alone
  411. BeanShell distribution, available from <ulink
  412. url="http://www.beanshell.org">http://www.beanshell.org</ulink>.
  413. </para>
  414. <caution><para>
  415. Security of this port is not guaranteed. Use this command
  416. with extreme caution.
  417. </para></caution>
  418. </listitem> -->
  419. <listitem>
  420. <funcsynopsis>
  421. <funcprototype>
  422. <funcdef>void <function>source</function></funcdef>
  423. <paramdef>String
  424. <parameter>filename</parameter></paramdef>
  425. </funcprototype>
  426. </funcsynopsis>
  427. <para>Evaluates the contents of <parameter>filename</parameter>
  428. as a BeanShell script in the interpreter's current
  429. namespace.</para>
  430. </listitem>
  431. </itemizedlist>
  432. </section>
  433. <section id="bsh-commands-object">
  434. <title>BeanShell Object Management Commands</title>
  435. <itemizedlist>
  436. <listitem>
  437. <funcsynopsis>
  438. <funcprototype>
  439. <funcdef><function>bind</function></funcdef>
  440. <paramdef>org.gjt.sp.jedit.bsh.This
  441. <parameter>ths</parameter></paramdef>
  442. <paramdef>org.gjt.sp.jedit.bsh.Namespace
  443. <parameter>namespace</parameter></paramdef>
  444. </funcprototype>
  445. </funcsynopsis>
  446. <para>Binds the scripted object <parameter>ths</parameter> to
  447. <parameter>namespace</parameter>.</para>
  448. </listitem>
  449. <listitem>
  450. <funcsynopsis>
  451. <funcprototype>
  452. <funcdef>void <function>clear</function></funcdef>
  453. <void />
  454. </funcprototype>
  455. </funcsynopsis>
  456. <para>Clear all variables, methods, and imports from this
  457. namespace. If this namespace is the root, it will be reset to
  458. the default imports.</para>
  459. </listitem>
  460. <listitem>
  461. <funcsynopsis>
  462. <funcprototype>
  463. <funcdef>org.gjt.sp.jedit.bsh.This
  464. <function>extend</function></funcdef>
  465. <paramdef>org.gjt.sp.jedit.bsh.This
  466. <parameter>object</parameter></paramdef>
  467. </funcprototype>
  468. </funcsynopsis>
  469. <para>Creates a new BeanShell <classname>This</classname>
  470. scripted object that is a child of the parameter
  471. <parameter>object</parameter>.</para>
  472. </listitem>
  473. <listitem>
  474. <funcsynopsis>
  475. <funcprototype>
  476. <funcdef>void
  477. <function>importObject</function></funcdef>
  478. <paramdef>Object
  479. <parameter>object</parameter></paramdef>
  480. </funcprototype>
  481. </funcsynopsis>
  482. <para>Import an object into this namespace. This is somewhat
  483. similar to Java 1.5 static class imports, except you can import
  484. the methods and fields of a Java object instance into a
  485. BeanShell namespace, for example:</para>
  486. <informalexample>
  487. <programlisting>Map map = new HashMap();
  488. importObject( map );
  489. put("foo", "bar");
  490. print( get("foo") ); // "bar"</programlisting>
  491. </informalexample>
  492. </listitem>
  493. <listitem>
  494. <funcsynopsis>
  495. <funcprototype>
  496. <funcdef>org.gjt.sp.jedit.bsh.This
  497. <function>object</function></funcdef>
  498. <void />
  499. </funcprototype>
  500. </funcsynopsis>
  501. <para>Creates a new BeanShell <classname>This</classname>
  502. scripted object which can hold data members. You can use this to
  503. create an object for storing miscellaneous crufties, like
  504. so:</para>
  505. <informalexample>
  506. <programlisting>crufties = object();
  507. crufties.foo = "hello world";
  508. crufties.counter = 5;
  509. ...</programlisting>
  510. </informalexample>
  511. </listitem>
  512. <listitem>
  513. <funcsynopsis>
  514. <funcprototype>
  515. <funcdef><function>setNameSpace</function></funcdef>
  516. <paramdef>org.gjt.sp.jedit.bsh.Namespace
  517. <parameter>namespace</parameter></paramdef>
  518. </funcprototype>
  519. </funcsynopsis>
  520. <para>Set the namespace of the current scope to
  521. <parameter>namespace</parameter>.</para>
  522. </listitem>
  523. <listitem>
  524. <funcsynopsis>
  525. <funcprototype>
  526. <funcdef>org.gjt.sp.jedit.bsh.This
  527. <function>super</function></funcdef>
  528. <paramdef>String
  529. <parameter>scopename</parameter></paramdef>
  530. </funcprototype>
  531. </funcsynopsis>
  532. <para>Returns a reference to the BeanShell
  533. <classname>This</classname> object representing the enclosing
  534. method scope specified by <parameter>scopename</parameter>. This
  535. method work similar to the <function>super</function> keyword
  536. but can refer to enclosing scope at higher levels in a hierarchy
  537. of scopes.</para>
  538. </listitem>
  539. <listitem>
  540. <funcsynopsis>
  541. <funcprototype>
  542. <funcdef>void <function>unset</function></funcdef>
  543. <paramdef>String <parameter>name</parameter></paramdef>
  544. </funcprototype>
  545. </funcsynopsis>
  546. <para>Removes the variable named by <parameter>name</parameter>
  547. from the current interpreter namespace. This has the effect of
  548. <quote>undefining</quote> the variable.</para>
  549. </listitem>
  550. </itemizedlist>
  551. </section>
  552. <section id="bsh-commands-other">
  553. <title>Other Commands</title>
  554. <itemizedlist>
  555. <!-- <listitem>
  556. <funcsynopsis>
  557. <funcprototype>
  558. <funcdef>void <function>exit</function></funcdef>
  559. <void/>
  560. </funcprototype>
  561. </funcsynopsis>
  562. <para>
  563. Calls <function>System.exit(0)</function>.
  564. </para>
  565. <caution><para>
  566. While this command is available, you should always call
  567. <function>jEdit.exit()</function> instead so the application will
  568. shutdown in an orderly fashion.
  569. </para></caution>
  570. </listitem> -->
  571. <listitem>
  572. <funcsynopsis>
  573. <funcprototype>
  574. <funcdef>void <function>debug</function></funcdef>
  575. <void />
  576. </funcprototype>
  577. </funcsynopsis>
  578. <para>Toggles BeanShell's internal debug reporting to the output
  579. stream of the current process.</para>
  580. </listitem>
  581. <listitem>
  582. <funcsynopsis>
  583. <funcprototype>
  584. <funcdef><function>getSourceFileInfo</function></funcdef>
  585. <void />
  586. </funcprototype>
  587. </funcsynopsis>
  588. <para>Returns the name of the file or other source from which
  589. the BeanShell interpreter is reading.</para>
  590. </listitem>
  591. </itemizedlist>
  592. </section>
  593. </chapter>