PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/doc/users-guide/simple-macros.xml

#
XML | 719 lines | 614 code | 88 blank | 17 comment | 0 complexity | ef4ebe3dd45fc483380b4cf8bfde79c0 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. <!-- jEdit 4.0 Macro Guide, (C) 2001, 2002 John Gellene -->
  2. <!-- jEdit buffer-local properties: -->
  3. <!-- :indentSize=1:noTabs=yes:maxLineLen=72:tabSize=2: -->
  4. <!-- Tue Jun 19 18:36:20 EDT 2001 @983 /Internet Time/ -->
  5. <!-- This file covers the chapter "A few simple macros", -->
  6. <!-- which introduces BeanShell through a "Hello world" script. -->
  7. <!-- It provides a little background on Java syntax and -->
  8. <!-- class structure. A few simple methods and macros are also -->
  9. <!-- discussed, as well as one-time macro execution features -->
  10. <chapter id="simple-macros"><title>A Few Simple Macros</title>
  11. <sect1 id="first-example" ><title>The Mandatory First Example</title>
  12. <informalexample><!-- <title>A first one-line macro</title> -->
  13. <programlisting>Macros.message(view, "Hello world!");</programlisting>
  14. </informalexample>
  15. <para>
  16. Running this one line script causes jEdit to display a message
  17. box (more precisely, a <classname>JOptionPane</classname> object) with
  18. the traditional beginner's message and an <guilabel>OK</guilabel> button.
  19. Let's see what is happening here.
  20. </para>
  21. <para>
  22. This statement calls a static method (or function) named
  23. <function>message</function> in jEdit's <classname>Macros</classname>
  24. class. If you don't know anything about classes or static methods or
  25. Java (or C++, which employs the same concept), you will need to gain
  26. some understanding of a few terms. Obviously this is not the place for
  27. academic precision, but if you are entirely new to object-oriented
  28. programming, here are a few skeleton ideas to help you with BeanShell.
  29. </para>
  30. <itemizedlist>
  31. <listitem>
  32. <para>
  33. An <glossterm>object</glossterm> is a collection of data that can be
  34. initialized, accessed and manipulated in certain defined ways.
  35. </para>
  36. </listitem>
  37. <listitem>
  38. <para>
  39. A <glossterm>class</glossterm> is a specification of what data an object
  40. contains and what methods can be used to work with the data. A Java
  41. application consists of one or more classes (in the case of jEdit ,over
  42. 500 classes) written by the programmer that defines the application's
  43. behavior. A BeanShell macro uses these classes, along with built-in
  44. classes that are supplied with the Java platform, to define its own
  45. behavior.
  46. </para>
  47. </listitem>
  48. <listitem>
  49. <para>
  50. A <glossterm>subclass</glossterm> (or child class) is a class which
  51. uses (or <quote>inherits</quote>) the data and methods of its parent
  52. class along with additions or modifications that alter the subclass's
  53. behavior. Classes are typically organized in hierarchies of parent
  54. and child classes to organize program code, to define common
  55. behavior in shared parent class code, and to specify the types of
  56. similar behavior that child classes will perform in their own specific ways.
  57. </para>
  58. </listitem>
  59. <listitem>
  60. <para>
  61. A <glossterm>method</glossterm> (or function) is a procedure that works
  62. with data in a particular object, other data (including other objects)
  63. supplied as <glossterm>parameters</glossterm>, or both. Methods
  64. typically are applied to a particular object which is an
  65. <glossterm>instance</glossterm> of the class to which the method
  66. belongs.
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. A <glossterm>static method</glossterm> differs from other methods
  72. in that it does not deal with the data in a particular object but is
  73. included within a class for the sake of convenience.
  74. </para>
  75. </listitem>
  76. </itemizedlist>
  77. <para>
  78. Java has a rich set of classes defined as part of the Java platform.
  79. Like all Java applications, jEdit is organized as a set of classes that
  80. are themselves derived from the Java platform's classes. We will refer
  81. to <firstterm>Java classes</firstterm> and <firstterm>jEdit
  82. classes</firstterm> to make this distinction. Some of jEdit's classes
  83. (such as those dealing with regular expressions and XML) are derived
  84. from or make use of classes in other open-source Java packages. Except
  85. for BeanShell itself, we won't be discussing them in this guide.
  86. </para>
  87. <para>
  88. In our one line script, the static method
  89. <function>Macros.message()</function> has two parameters because that is
  90. the way the method is defined in the <classname>Macros</classname>
  91. class. You must specify both parameters when you call the function. The
  92. first parameter, <parameter>view</parameter>, is a a variable naming a
  93. <classname>View</classname> object - an instance of jEdit's
  94. <classname>View</classname> class. A <classname>View</classname>
  95. represents a <quote>parent</quote> or top-level frame window that
  96. contains the various visible components of the program, including the
  97. text area, menu bar, toolbar, and any docked windows. It is a
  98. subclass of Java's <classname>JFrame</classname> class.
  99. With jEdit, you can create and display multiple views simultaneously.
  100. The variable <varname>view</varname> is predefined for purposes of
  101. BeanShell scripts as the current, active <classname>View</classname> object.
  102. This is in fact the variable you want to specify as the first parameter.
  103. Normally you would not want to associate a message box with anything
  104. other than the current <classname>View</classname>.
  105. </para>
  106. <para>
  107. The second parameter, which appears to be quoted text, is a
  108. <glossterm>string literal</glossterm> - a sequence of characters of
  109. fixed length and content. Behind the scenes, BeanShell and Java take
  110. this string literal and use it to create a <classname>String</classname>
  111. object. Normally, if you want to create an object in Java or BeanShell,
  112. you must construct the object using the <function>new</function> keyword
  113. and a <firstterm>constructor</firstterm> method that is part of the
  114. object's class. We'll show an example of that later. However, both Java
  115. and BeanShell let you use a string literal anytime a method's parameter
  116. calls for a <classname>String</classname>.
  117. </para>
  118. <para>
  119. If you are a Java programmer, you might wonder about a few things
  120. missing from this one line program. There is no class definition, for
  121. example. You can think of a BeanShell script as an implicit definition
  122. of a <function>main()</function> method in an anonymous class. That is
  123. in fact how BeanShell is implemented; the class is derived from
  124. a BeanShell class called <classname>XThis</classname>. If you
  125. don't find that helpful, just think of a script as one or more blocks of
  126. procedural statements conforming to Java syntax rules. You will also get
  127. along fine (for the most part) with C or C++ syntax if you leave out
  128. anything to do with pointers or memory management - Java and BeanShell
  129. do not have pointers and deal with memory management automatically.
  130. </para>
  131. <para>
  132. Another missing item from a Java perspective is a
  133. <function>package</function> statement. In Java, such a statement is
  134. used to bundle together a number of files so that their classes become
  135. visible to one another. Packages are not part of BeanShell,
  136. and you don't need to know anything about them to write
  137. BeanShell macros.
  138. </para>
  139. <para>
  140. Finally, there are no <function>import</function> statements in this
  141. script. In Java, an <function>import</function> statement makes public
  142. classes from other packages visible within the file in which the
  143. statement occurs without having to specify a fully
  144. qualified class name. Without an import statement or a fully qualified
  145. name, Java cannot identify most classes using a single name as an identifier.
  146. </para>
  147. <para>
  148. jEdit automatically imports a number of commonly-used packages into the
  149. namespace of every BeanShell script. Because of this, the script output
  150. of a recorded macro does not contain <function>import</function>
  151. statements. For the same reason, most BeanShell scripts you write will
  152. not require <function>import</function> statements.
  153. </para>
  154. <para>
  155. Java requires <literal>import</literal> statement at the beginning of a
  156. source file. BeanShell allows you to place <literal>import</literal>
  157. statements anywhere in a script, including inside a block of
  158. statements. The <literal>import</literal> statement will cover all names
  159. used following the statement in the enclosing block.
  160. </para>
  161. <para>
  162. If you try to use a class that is not imported without its
  163. fully-qualified name, the BeanShell interpreter will complain with an
  164. error message relating to the offending line of code.
  165. </para>
  166. <sidebar>
  167. <para>
  168. Here is the full list of packages automatically imported by jEdit:
  169. </para>
  170. <programlisting>java.awt
  171. java.awt.event
  172. java.net
  173. java.util
  174. java.io
  175. java.lang
  176. javax.swing
  177. javax.swing.event
  178. org.gjt.sp.jedit
  179. org.gjt.sp.jedit.browser
  180. org.gjt.sp.jedit.buffer
  181. org.gjt.sp.jedit.gui
  182. org.gjt.sp.jedit.io
  183. org.gjt.sp.jedit.msg
  184. org.gjt.sp.jedit.options
  185. org.gjt.sp.jedit.pluginmgr
  186. org.gjt.sp.jedit.search
  187. org.gjt.sp.jedit.syntax
  188. org.gjt.sp.jedit.textarea
  189. org.gjt.sp.util</programlisting>
  190. </sidebar>
  191. </sect1>
  192. <sect1 id="helpful-methods"><title>Helpful Methods in the
  193. Macros Class</title>
  194. <para>
  195. Including <function>message()</function>, there are five static methods
  196. in the <classname>Macros</classname> class that allow you to converse
  197. easily with your macros. They all encapsulate calls to methods of the
  198. Java platform's <classname>JOptionPane</classname> class.
  199. </para>
  200. <itemizedlist>
  201. <listitem>
  202. <funcsynopsis>
  203. <funcprototype>
  204. <funcdef>public static void <function>message</function></funcdef>
  205. <paramdef>Component <parameter>comp</parameter></paramdef>
  206. <paramdef>String <parameter>message</parameter></paramdef>
  207. </funcprototype>
  208. </funcsynopsis>
  209. </listitem>
  210. <listitem>
  211. <funcsynopsis>
  212. <funcprototype>
  213. <funcdef>public static void <function>error</function></funcdef>
  214. <paramdef>Component <parameter>comp</parameter></paramdef>
  215. <paramdef>String <parameter>message</parameter></paramdef>
  216. </funcprototype>
  217. </funcsynopsis>
  218. </listitem>
  219. <listitem>
  220. <funcsynopsis>
  221. <funcprototype>
  222. <funcdef>public static String <function>input</function></funcdef>
  223. <paramdef>Component <parameter>comp</parameter></paramdef>
  224. <paramdef>String <parameter>prompt</parameter></paramdef>
  225. </funcprototype>
  226. </funcsynopsis>
  227. </listitem>
  228. <listitem>
  229. <funcsynopsis>
  230. <funcprototype>
  231. <funcdef>public static String <function>input</function></funcdef>
  232. <paramdef>Component <parameter>comp</parameter></paramdef>
  233. <paramdef>String <parameter>prompt</parameter></paramdef>
  234. <paramdef>String <parameter>defaultValue</parameter></paramdef>
  235. </funcprototype>
  236. </funcsynopsis>
  237. </listitem>
  238. <listitem>
  239. <funcsynopsis>
  240. <funcprototype>
  241. <funcdef>public static int <function>confirm</function></funcdef>
  242. <paramdef>Component <parameter>comp</parameter></paramdef>
  243. <paramdef>String <parameter>prompt</parameter></paramdef>
  244. <paramdef>int <parameter>buttons</parameter></paramdef>
  245. </funcprototype>
  246. </funcsynopsis>
  247. </listitem>
  248. </itemizedlist>
  249. <para>
  250. The format of these four <glossterm>declarations</glossterm> provides a
  251. concise reference to the way in which the methods may be used. The
  252. keyword <function>public</function> means that the method can be used
  253. outside the <classname>Macros</classname> class. The alternatives are
  254. <function>private</function> and <function>protected</function>. For
  255. purposes of BeanShell, you just have to know that BeanShell can only use
  256. public methods of other Java classes. The keyword
  257. <function>static</function> we have already discussed. It means that the
  258. method does not operate on a particular object. You call a static
  259. function using the name of the class (like
  260. <classname>Macros</classname>) rather than the name of a particular
  261. object (like <varname>view</varname>). The third word is the type of the
  262. value returned by the method. The keyword <function>void</function> is
  263. Java's way of saying the the method does not have a return value.
  264. </para>
  265. <para>
  266. The <function>error()</function> method works just like
  267. <function>message()</function> but displays an error icon in the message
  268. box. The <function>input()</function> method furnishes a text field for
  269. input, an <guilabel>OK</guilabel> button and a
  270. <guilabel>Cancel</guilabel> button. If <guilabel>Cancel</guilabel> is pressed,
  271. the method returns <constant>null</constant>. If <guilabel>OK</guilabel>
  272. is pressed, a <classname>String</classname> containing the contents of
  273. the text field is returned. Note that there are two forms of the
  274. <function>input()</function> method; the first form with two parameters
  275. displays an empty input field, the other forms lets you specify an initial,
  276. default input value.
  277. </para>
  278. <para>
  279. For those without Java experience, it is important to know that
  280. <constant>null</constant> is <emphasis>not</emphasis> the same as an
  281. empty, <quote>zero-length</quote> <classname>String</classname>. It is
  282. Java's way of saying that there is no object associated with this
  283. variable. Whenever you seek to use a return value from
  284. <function>input()</function> in your macro, you should test it to see if
  285. it is <constant>null</constant>. In most cases, you will
  286. want to exit gracefully from the script with a
  287. <function>return</function> statement, because the presence of a null
  288. value for an input variable usually means that the user intended to
  289. cancel macro execution. BeanShell will complain if you call any
  290. methods on a <constant>null</constant> object.
  291. </para>
  292. <para>
  293. The <function>confirm()</function> method in the
  294. <classname>Macros</classname> class is a little more complex. The
  295. <varname>buttons</varname> parameter has an <classname>int</classname>
  296. type, and the usual way to supply a value is to use one of
  297. the predefined values taken from Java's
  298. <classname>JOptionPane</classname> class. You can choose among
  299. <constant>JOptionPane.YES_NO_OPTION</constant>,
  300. <constant>JOptionPane.YES_NO_CANCEL_OPTION</constant>, or
  301. <constant>JOptionPane.OK_CANCEL_OPTION</constant>. The return
  302. value of the method is also an <classname>int</classname>, and should be tested
  303. against the value of other predefined constants:
  304. <constant>JOptionPane.YES_OPTION</constant>,
  305. <constant>JOptionPane.NO_OPTION</constant>,
  306. <constant>JOptionPane.OK_OPTION</constant> or
  307. <constant>JOptionPane.CANCEL_OPTION</constant>.
  308. </para>
  309. <para>
  310. We've looked at using <function>Macros.message()</function>. To
  311. use the other methods, you would write something like the
  312. following:
  313. </para>
  314. <informalexample><!-- <title>Using <function>Macros.error()</function> and
  315. <function>Macros.input()</function></title> -->
  316. <programlisting>Macros.error(view, "Goodbye, cruel world!");
  317. String result = Macros.input(view, "Type something here.");
  318. String result = Macros.input(view, "When were you born?",
  319. "I don't remember, I was very young at the time");
  320. int result = Macros.confirm("Do you really want to learn"
  321. + " about BeanShell?",JOptionPane.YES_NO_OPTION);
  322. </programlisting>
  323. </informalexample>
  324. <para>
  325. In the last three examples, placing the word <classname>String</classname>
  326. or <classname>int</classname>
  327. before the variable name <varname>result</varname> tells BeanShell that
  328. the variable refers to an integer or a <classname>String</classname> object, even
  329. before a particular value is assigned to the variable.
  330. In BeanShell, this <glossterm>declaration</glossterm> of the
  331. <glossterm>type</glossterm> of <varname>result</varname> is not
  332. necessary; BeanShell can figure it out when the macro runs. This can be
  333. helpful if you are not comfortable with specifying types and classes;
  334. just use your variables and let BeanShell worry about it.
  335. </para>
  336. <para>
  337. Without an explicit <glossterm>type declaration</glossterm> like
  338. <classname>String</classname> <varname>result</varname>, BeanShell
  339. variables can change their type at runtime depending on the object or
  340. data assigned to it. This dynamic typing allows you to write code like
  341. this (if you really wanted to):
  342. </para>
  343. <informalexample><!-- <title>Dynamic typing of variables</title> -->
  344. <programlisting>// note: no type declaration
  345. result = Macros.input(view, <quote>Type something here.</quote>);
  346. // this is our predefined, current View
  347. result = view;
  348. // this is an <quote>int</quote> (for integer);
  349. // in Java and BeanShell, int is one of a small number
  350. // of <quote>primitive</quote> data types which are not classes
  351. result = 14;</programlisting>
  352. </informalexample>
  353. <para>
  354. However, if you first declared <varname>result</varname> to be type
  355. <classname>String</classname> and and then tried these reassignments,
  356. BeanShell would complain. While avoiding explicit type declaration makes
  357. writing macro code simpler, using them can act as a check to make sure you are
  358. not using the wrong variable type of object at a later point in your
  359. script. It also makes it easier (if you are so inclined) to take a
  360. BeanShell <quote>prototype</quote> and incorporate it in a Java program.
  361. </para>
  362. <para>
  363. One last thing before we bury our first macro. The double slashes in the
  364. examples just above signify that everything following them on that line
  365. should be ignored by BeanShell as a comment. As in Java and C/C++, you
  366. can also embed comments in your BeanShell code by setting them off with
  367. pairs of <userinput>/* */</userinput>, as in the following example:
  368. </para>
  369. <informalexample>
  370. <programlisting>/* This is a long comment that covers several lines
  371. and will be totally ignored by BeanShell regardless of how
  372. many lines it covers */</programlisting>
  373. </informalexample>
  374. </sect1>
  375. <sect1 id="something-useful"><title>Now For Something Useful</title>
  376. <para>
  377. Here is a macro that inserts the path of the current buffer in
  378. the text:
  379. </para>
  380. <informalexample>
  381. <!-- <title>Insert buffer path in text</title> -->
  382. <programlisting>String newText = buffer.getPath();
  383. textArea.setSelectedText(newText);</programlisting>
  384. </informalexample>
  385. <para>
  386. Two of the new names we see here, <varname>buffer</varname> and
  387. <varname>textArea</varname>, are predefined variables like
  388. <varname>view</varname>. The variable <varname>buffer</varname>
  389. represents a jEdit <varname>Buffer</varname> object, and
  390. <varname>textArea</varname> represents a
  391. <classname>JEditTextArea</classname> object.
  392. </para>
  393. <itemizedlist>
  394. <listitem>
  395. <para>
  396. A <classname>Buffer</classname> object represents the contents of an open text
  397. file. The variable <varname>buffer</varname> is predefined as the
  398. current, visible buffer being edited.
  399. </para>
  400. </listitem>
  401. <listitem>
  402. <para>
  403. A <classname>JEditTextArea</classname> is the visible component that
  404. displays the file being edited. It is derived from the
  405. <classname>JComponent</classname> class. The variable
  406. <varname>textArea</varname> represents the current
  407. <classname>JEditTextArea</classname> object, which in turn displays
  408. the current buffer.
  409. </para>
  410. </listitem>
  411. </itemizedlist>
  412. <para>
  413. Unlike in our first macro example, here we are calling class methods on
  414. particular objects. First, we call <function>getPath()</function> on the
  415. current <classname>Buffer</classname> object to get the full path of the
  416. text file currently being edited. Next, we call
  417. <function>setSelectedText()</function> on the current text display
  418. component, specifying the text to be inserted as a parameter.
  419. </para>
  420. <para>
  421. In precise terms, the <function>setSelectedText()</function> method
  422. substitutes the contents of the <classname>String</classname>
  423. parameter for a range of selected text that includes the current caret
  424. position. If no text is selected at the caret position, the effect
  425. of this operation is simply to insert the new text at that position.
  426. </para>
  427. <para>
  428. Here's a few alternatives to the full file path that you could
  429. use to insert various useful things:
  430. </para>
  431. <informalexample><!-- <title>Items to use with
  432. <function>setSelectedText()</function></title> -->
  433. <programlisting>// the file name (without full path)
  434. String newText = buffer.getName();
  435. // today's date
  436. import java.text.DateFormat;
  437. String newText = DateFormat.getDateInstance()
  438. .format(new Date());
  439. // a line count for the current buffer
  440. String newText = "This file contains "
  441. + textArea.getLineCount() + " lines.";</programlisting>
  442. </informalexample>
  443. <para>
  444. Here are brief comments on each:
  445. </para>
  446. <itemizedlist>
  447. <listitem>
  448. <para>
  449. In the first, the call to <function>getName()</function> invokes
  450. another method of the <classname>Buffer</classname> class.
  451. </para>
  452. </listitem>
  453. <listitem>
  454. <para>
  455. The syntax of the second example chains the results of
  456. several methods. You could write it this way:
  457. </para>
  458. <programlisting>import java.text.DateFormat;
  459. Date d = new Date();
  460. DateFormat df = DateFormat.getDateInstance();
  461. String result = df.format(d);
  462. </programlisting>
  463. <para>
  464. Taking the pieces in order:
  465. </para>
  466. <itemizedlist>
  467. <listitem>
  468. <para>
  469. A Java <classname>Date</classname> object is created using the
  470. <function>new</function> keyword. The empty parenthesis after
  471. <classname>Date</classname> signify a call on the <glossterm>
  472. constructor method</glossterm> of <classname>Date</classname> having no
  473. parameters; here, a <classname>Date</classname> is created representing
  474. the current date and time.
  475. </para>
  476. </listitem>
  477. <listitem>
  478. <para>
  479. <function>DateFormat.getDateInstance()</function> is a static method
  480. that creates and returns a <classname>DateFormat</classname> object. As
  481. the name implies, <classname>DateFormat</classname> is a Java class
  482. that takes <classname>Date</classname> objects and produces readable
  483. text. The method <function>getDateInstance()</function> returns a
  484. <classname>DateFormat</classname> object that parses and formats dates.
  485. It will use the default <glossterm>locale</glossterm> or text format
  486. specified in the user's Java installation.
  487. </para>
  488. </listitem>
  489. <listitem>
  490. <para>
  491. Finally, <classname>DateFormat.format()</classname> is called on the
  492. new <classname>DateFormat</classname> object using the
  493. <classname>Date</classname> object as a parameter. The result is a
  494. <classname>String</classname> containing the date in the default
  495. locale.
  496. </para>
  497. </listitem>
  498. <listitem>
  499. <para>
  500. Note that the <classname>Date</classname> class is contained in
  501. the <literal>java.util</literal> package, so an explicit import
  502. statement is not required. However, <classname>DateFormat</classname>
  503. is part of the <literal>java.text</literal> package, which is
  504. not automatically imported, so an explicit
  505. <function>import</function> statement must be used.
  506. </para>
  507. </listitem>
  508. </itemizedlist>
  509. </listitem>
  510. <listitem>
  511. <para>
  512. The third example shows three items of note:
  513. <itemizedlist>
  514. <listitem>
  515. <para>
  516. <function>getLineCount()</function> is a method in jEdit's
  517. <classname>JEditTextArea</classname> class. It returns an
  518. <classname>int</classname> representing the number of lines in the
  519. current text buffer. We call it on <varname>textArea</varname>, the
  520. pre-defined, current <classname>JEditTextArea</classname> object.
  521. </para>
  522. </listitem>
  523. <listitem>
  524. <para>
  525. The use of the <function>+</function> operator (which can be chained,
  526. as here) appends objects and string
  527. literals to return a single, concatenated <classname>String</classname>.
  528. </para>
  529. </listitem>
  530. </itemizedlist>
  531. </para>
  532. </listitem>
  533. </itemizedlist>
  534. <sidebar><title>The other pre-defined variable</title>
  535. <para>
  536. In addition to <varname>view</varname>, <varname>buffer</varname> and
  537. <varname>textArea</varname>, there is one more pre-defined variable
  538. available for use in macros -- <varname>editPane</varname>. That
  539. variable is set to the current <classname>EditPane</classname> instance.
  540. An <classname>EditPane</classname> object contains a text area and
  541. buffer switcher. A view can be split to display multiple
  542. buffers, each in its own edit pane. Among other things, the
  543. <classname>EditPane</classname> class contains methods for selecting
  544. the buffer to edit.
  545. </para>
  546. <para>
  547. Most of the time your macros will manipulate the <varname>buffer</varname>
  548. or the <varname>textArea</varname>. Sometimes you will need to use
  549. <varname>view</varname> as a parameter in a method call. You will probably
  550. only need to use <varname>editPane</varname> if your macros work with
  551. split views.
  552. </para>
  553. </sidebar>
  554. </sect1>
  555. <!-- moved from introductory chapter in 3.2 macro guide -->
  556. <sect1 id="single-macros"><title>Single Execution Macros</title>
  557. <para>
  558. At this point, we have covered some basic techniques for writing
  559. one-line or other short macros. As noted earlier, you can save a BeanShell
  560. script of any length as a text file with the <filename>.bsh</filename>
  561. extension. There are three other ways jEdit lets you use BeanShell quickly,
  562. without saving a script to storage, on a <quote>one time only</quote>
  563. basis. You will find them in the <guimenu>Utilities</guimenu> menu.
  564. </para>
  565. <para>
  566. <guimenu>Utilities</guimenu>&gt;<guisubmenu>BeanShell</guisubmenu>&gt;<guimenuitem>Evaluate BeanShell
  567. Expression</guimenuitem> displays a text input dialog
  568. that asks you to type a single line of BeanShell commands. You can type
  569. more than one BeanShell statement so long as each of them ends with a
  570. semicolon. If BeanShell successfully interprets your input, a message
  571. box will appear with the return value of the last statement. You can do
  572. the same thing using the BeanShell interpreter provided with the
  573. <application>Console</application> plugin; the return value will appear
  574. in the output window.
  575. </para>
  576. <para>
  577. <guimenu>Utilities</guimenu>&gt;<guisubmenu>BeanShell</guisubmenu>&gt;<guimenuitem>Evaluate For Selected
  578. Lines</guimenuitem> displays a text input dialog that asks you to
  579. type a single line of BeanShell commands. The commands are evaluated
  580. for each line of the selection. In addition to the standard set of
  581. variables, this command defines the following:
  582. </para>
  583. <itemizedlist>
  584. <listitem><para><varname>line</varname> - the line number, from the
  585. start of the buffer. The first line is numbered 0.
  586. </para></listitem>
  587. <listitem><para><varname>index</varname> - the line number, from the
  588. start of the selection. The first line is numbered 0.
  589. </para></listitem>
  590. <listitem><para><varname>text</varname> - the text of the line.
  591. </para></listitem>
  592. </itemizedlist>
  593. <informalexample>
  594. <para>
  595. Try typing an expression like <userinput>(line + 1) + ": " + text</userinput>
  596. in the <guimenuitem>Evaluate For Selected Lines</guimenuitem> dialog
  597. box. This will add a line number to each selected line beginning with
  598. the number <userinput>1</userinput>.
  599. </para>
  600. </informalexample>
  601. <para>
  602. The BeanShell expression you enter will be evaluated and substituted in place of
  603. the entire text of a selected line. If you want to leave the line's current
  604. text as an element of the modified line, you must include the defined variable
  605. <userinput>text</userinput> as part of the BeanShell expression that you enter.
  606. </para>
  607. <para>
  608. <guimenu>Utilities</guimenu>&gt;<guisubmenu>BeanShell</guisubmenu>&gt;<guimenuitem>Evaluate Selection</guimenuitem>
  609. evaluates the selected text as a BeanShell script and
  610. replaces it with the return value of the statement.
  611. </para>
  612. <para>
  613. Using <guimenuitem>Evaluate Selection</guimenuitem> is an
  614. easy way to do arithmetic calculations inline while editing. BeanShell
  615. uses numbers and arithmetic operations in an ordinary, intuitive way.
  616. </para>
  617. <informalexample>
  618. <para>
  619. Try typing an expression like <userinput>(3745*856)+74</userinput>
  620. in the buffer, select it, and choose
  621. <guimenu>Utilities</guimenu>&gt;<guisubmenu>BeanShell</guisubmenu>&gt;<guimenuitem>Evaluate
  622. Selection</guimenuitem>. The selected text will be replaced by the
  623. answer, <userinput>3205794</userinput>.
  624. <!-- Irrelevant? -->
  625. <!-- Since this is a text file
  626. and not a spreadsheet, the original values that BeanShell evaluated are
  627. not retained or saved as part of the buffer's contents. -->
  628. </para>
  629. </informalexample>
  630. </sect1>
  631. </chapter>