DMelt:Text/1 Working with text in IDE

From HandWiki
Revision as of 19:21, 20 August 2017 by imported>Jworkorg
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Member

Working with text using 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 
$$
y=frac{sin(x)}{2*sqrt(x)}
$$
"""
# 1 means to display HTML file
view.generateHtml(1)

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)
view.open( help_file, 0  )
Thread.sleep(3000)        # wait for 3000 ms
  
print("Select the text and wait for 3 sec")
view.selectAll()
Thread.sleep(3000) 

print("Remove the text and wait for 3 sec")
mydoc=view.getText()
view.setText("")
Thread.sleep(3000) 

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

print("Insert some text and wait for 3 sec")
cc=view.getCaretPosition()
view.insertString("Some inserted text at position 4", 4)
view.insertString("Again some text is  inserted at position 10", 10)
Thread.sleep(3000) 

print("Put the removed text back, go to line 4")
view.setText(mydoc)
view.goToLine(4)
cc=view.getCaretPosition()
Thread.sleep(3000)

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

print("set caret and select, then  wait for 3 sec")
view.setCaretPos(50)
view.select(0,20)
Thread.sleep(3000)
print("replace \"t\" by \"m\", then  wait for 3 sec") 
ss=view.getText()
ss=ss.replace("t","m")
view.setText(ss)
Thread.sleep(3000)
dir()
print("")
print("Above is the list of jeHEP imported classes")
print("--This macro is located here:"+SystemDir+
            fSep+"macros"+fSep+"system"+fSep+"test.py")

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


Using Python

Let us remind that you can use Python commands to work with text, since DMelt is designed to support the Python language from the ground. Here is the example to show a few simple manipulation with the text:

s = "mary had a little lamb"
print s[0]          # index is zero-based
print s[11:18]      # 'slice' a subsequence
print s[:4]         # empty slice-begin assumes zero
print s[5:-5]       # can use "from end" index with negatives
print s[:5]+s[5:]   # slice-begin & slice-end are complimentary