DMelt:General/7 Working with text

From HandWiki
Revision as of 11:06, 14 February 2021 by imported>Jworkorg
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Working with text in IDE

You can use DataMelt IDE as a usual text editor, jeHEP. The editor is a highly configurable text editor built to enable efficient text editing. You can do copy, paste, replace and find using the tool bar menu, as for any other editor. The editor also has syntax highlighting almost for any programming language.

But you can do something more: The main idea of DataMelt is that you can use Jython scripts to access any Java library. This also means that you can access the TextArea of the scripts itself and all internal variables of the IDE at runtime.

[[#ref_{{{1}}}|^]] In fact, a script can modify itself at runtime, or even design itself while executing itself. This offers a tremendous power!

Let us consider how to access internal variable of the DataMelt IDE. Load this script into the IDE and execute it:

from jehep.ui import SetEnv
from jehep.ui import Editor
print Editor.DocName() # name of this script
print Editor.DocDir()  # directory of this script
print SetEnv.ProjDir   # project directory
print SetEnv.ClassPath # Java classpath

Now let us make a Jython script which, after being loaded to DataMelt IDE, prints itself:

print view.getText()                 # access the TextArea of this script and prints itself
cc=view.getCaretPosition()           # get the current caret position
view.insertString("inserted text at position 4", 4) # inset some text at position 4

In fact, this script simply destroys itself since it inserts some line in itself! Note "view" in this case is an instance if the TextArea which is used to keep the file. This class is very comprehensive, it has about 100 methods for text manipulation.

Now, let us do something useful and constrictive. What we want to do is to generate an HTML page with LaTeX equation. As we know from above, we can use double dollar character to enclose LateX equation and generate an HTML file with equation image using "CodeView" ([View]-[CodeView] menu).

But we do not need to use GUI for this, in DataMelt one can use just a script which will do everything you need.

<H2>This is an equation </H2>
Our equation is 
# 1 means to display HTML file

Look at view.generateHtml(1) method. The script executes itself, generates an HTML file from the multiline comment and opens a HTML browser inside DataMelt. Your HTML file with the equation is ready! The file is located in the directory "cachedir" together with the image of this equation.

Text processing using Python macros

In this section we will discuss how to write simple Python macros to manipulate text loaded to the DataMelt IDE. While executing these macros, you will see how the text changes while Jython performs various operations. We will write this macros in Python, but will call active Java objects used to keep the text in DataMelt IDE edit window. In this Python macro, we:

 - Load a text file into DataMelt IDE text area
 - Remove some part of the text
 - Determine the current caret position
 - Inset a text at some specific caret position

from jehep.ui import SetEnv
from jehep.ui import Editor 
from javax.swing import JButton, JDialog, JLabel, JPanel, JScrollPane, JTextArea
from java.awt import BorderLayout
from java.awt import Color
from java.awt.event import ActionListener, KeyEvent, KeyListener, MouseListener
from java.lang import Thread

help_file=SystemDir+fSep+"Docs"+fSep+"license.txt";  # load text into the edit area
print("Open  a test  file  and wait for 3 sec"+help_file); help_file, 0  );
Thread.sleep(3000);        # wait for 3000 ms
print("Select the text and wait for 3 sec");

print("Remove the text and wait for 3 sec");

print("Type some text and wait for 3 sec");
view.setText("This is some sample text");

print("Insert some text and wait for 3 sec");
view.insertString("Some inserted text at position 4", 4);
view.insertString("Again some text is  inserted at position 10", 10);

print("Put the removed text back, go to line 4");

print("Insert some text at line 20");
view.insertString("This text was inserted", cc);
view.insertString("", cc); 

print("set caret and select, then  wait for 3 sec");

print("replace \"t\" by \"m\", then  wait for 3 sec"); 

print("Above is the list of jeHEP imported classes");

print("--This macro is located here:"+SystemDir+

print("--The user can put any macro in:"+SystemDir+