jEdit Community - Resources for users of the jEdit Text Editor
search and replace macro...
Submitted by Molamini on Thursday, 13 November, 2008 - 17:41
Hello, I have a search and replace macro problem. while searching, if the next text (e.g. WEB PAGES) is not in the display, a runtime exception occurs when searching for the next word. An example would be the text WEB PAGES being more than 50 lines down (I'm assuming 50 lines down is below the display of the screen) from the WEB PAGES text found. giving the following macro filename: test.bsh -------------------------- // This is a recorded macro. First, check over the // commands to make sure this is what you intended. Then, // save this buffer, and the macro should appear in the // Macros menu. SearchAndReplace.setSearchString("WEB PAGE"); SearchAndReplace.setAutoWrapAround(false); SearchAndReplace.setReverseSearch(false); SearchAndReplace.setIgnoreCase(false); SearchAndReplace.setRegexp(false); SearchAndReplace.setSearchFileSet(new CurrentBufferSet()); SearchAndReplace.find(view); SearchAndReplace.setSearchString("WEB PAGE"); SearchAndReplace.setReplaceString("<<>>"); SearchAndReplace.setBeanShellReplace(false); SearchAndReplace.setIgnoreCase(false); SearchAndReplace.setRegexp(false); SearchAndReplace.replace(view); SearchAndReplace.setSearchString("WEB PAGE"); SearchAndReplace.setAutoWrapAround(false); SearchAndReplace.setReverseSearch(false); SearchAndReplace.setIgnoreCase(false); SearchAndReplace.setRegexp(false); SearchAndReplace.setSearchFileSet(new CurrentBufferSet()); SearchAndReplace.find(view); SearchAndReplace.setSearchString("WEB PAGE"); SearchAndReplace.setReplaceString("<<>>"); SearchAndReplace.setBeanShellReplace(false); SearchAndReplace.setIgnoreCase(false); SearchAndReplace.setRegexp(false); SearchAndReplace.replace(view); SearchAndReplace.setSearchString("WEB PAGE"); SearchAndReplace.setAutoWrapAround(false); SearchAndReplace.setReverseSearch(false); SearchAndReplace.setIgnoreCase(false); SearchAndReplace.setRegexp(false); SearchAndReplace.setSearchFileSet(new CurrentBufferSet()); SearchAndReplace.find(view); ------------------------------------------- and given the file to search and replace filename: test.txt ------------------------------------------ WEB PAGE ...(about 60 lines down)... WEB PAGE ------------------------------------------------ I get the following runtime error... 9:29:38 AM [error] SearchAndReplace: java.lang.RuntimeException: Invalid screen line count: 10 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.textarea.DisplayManager.getScreenLineCount(DisplayManager.java:192) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.textarea.FirstLine.physDown(FirstLine.java:174) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.textarea.DisplayManager.setFirstPhysicalLine(DisplayManager.java:632) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.textarea.TextArea.setFirstPhysicalLine(TextArea.java:754) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.textarea.TextArea.scrollTo(TextArea.java:981) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.textarea.TextArea.scrollTo(TextArea.java:887) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.search.SearchAndReplace.find(SearchAndReplace.java:661) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.search.SearchAndReplace.find(SearchAndReplace.java:519) 9:29:38 AM [error] SearchAndReplace: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 9:29:38 AM [error] SearchAndReplace: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 9:29:38 AM [error] SearchAndReplace: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 9:29:38 AM [error] SearchAndReplace: at java.lang.reflect.Method.invoke(Method.java:597) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.Reflect.invokeMethod(Reflect.java:134) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.Reflect.invokeStaticMethod(Reflect.java:98) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.Name.invokeMethod(Name.java:796) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.bsh.Interpreter.eval(Interpreter.java:644) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.BeanShell._runScript(BeanShell.java:336) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.BeanShell._runScript(BeanShell.java:281) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.BeanShell.runScript(BeanShell.java:207) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.Macros$BeanShellHandler.runMacro(Macros.java:1020) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.Macros$Macro.invoke(Macros.java:441) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.gui.InputHandler.invokeAction(InputHandler.java:383) 9:29:38 AM [error] SearchAndReplace: at org.gjt.sp.jedit.jEdit$3.invokeAction(jEdit.java:3000) -----------------------------------------------------
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
Mmk. That looks like a rather
by elberry on Fri, 14/11/2008 - 07:06
Mmk. That looks like a rather overly complicated macro. Smiling

What exactly are you trying to do, we might be able to help you come up with something similar and less error prone.

Learn from the past. Live in the present. Plan for the future.
11101000
Blog
 
It's not that complicated at all ... Just a search and replace..
by Molamini on Fri, 14/11/2008 - 18:22
Here I'm going to repost the macro and hopefully will render correctly.
This bug really should not exist. In a nutshell, Macros can't search and replace if the next found text is outside the view area.


---------------------------
// This is a recorded macro. First, check over the
// commands to make sure this is what you intended. Then,
// save this buffer, and the macro should appear in the
// Macros menu.
SearchAndReplace.setSearchString("WEB PAGE");
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.find(view);
SearchAndReplace.setSearchString("WEB PAGE");
SearchAndReplace.setReplaceString("<<>>");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.replace(view);
SearchAndReplace.setSearchString("WEB PAGE");
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.find(view);
SearchAndReplace.setSearchString("WEB PAGE");
SearchAndReplace.setReplaceString("<<>>");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.replace(view);
SearchAndReplace.setSearchString("WEB PAGE");
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.find(view);
----------------------------------------


Here is the sample to search and replace.
--------------------------






WEB PAGE

































































WEB PAGE













































WEB PAGE
---------------------------------

Here is the error
---------------------------------
java.lang.RuntimeException: Invalid screen line count: 73
at org.gjt.sp.jedit.textarea.DisplayManager.getScreenLineCount(DisplayManager.java:192)
at org.gjt.sp.jedit.textarea.FirstLine.physDown(FirstLine.java:174)
at org.gjt.sp.jedit.textarea.DisplayManager.setFirstPhysicalLine(DisplayManager.java:632)
at org.gjt.sp.jedit.textarea.TextArea.setFirstPhysicalLine(TextArea.java:754)
at org.gjt.sp.jedit.textarea.TextArea.scrollTo(TextArea.java:981)
at org.gjt.sp.jedit.textarea.TextArea.scrollTo(TextArea.java:887)
at org.gjt.sp.jedit.search.SearchAndReplace.find(SearchAndReplace.java:661)
at org.gjt.sp.jedit.search.SearchAndReplace.find(SearchAndReplace.java:519)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
---------------------------------

 
Again, what exactly are you t
by elberry on Sat, 15/11/2008 - 04:50
Again, what exactly are you trying to do? It looks like you're trying to do a search (5 times) for "WEB PAGE" and you want to do a replace after the 4th one?

It seems to me, you may have some sort of concurrency issue. Perhaps the next search after the replacement is failing because it's searching before the replacement is finished. Or perhaps your replacement is removing all the "WEB PAGE" texts, and now the search can't find any more?

Also, I ran your macro against your test file. I can't reproduce the error.

Again, what exactly are you trying to accomplish with this macro?

Learn from the past. Live in the present. Plan for the future.
11101000
Blog
 
which version of Jedit and JRE....
by Molamini on Thu, 20/11/2008 - 15:26
Which version of Jedit and JRE are you using?


Thanks!

Molamini
 
Re: Again, what exactly are you t
by Molamini on Sat, 15/11/2008 - 05:55
I'm creating a search and replace macro. Unfortunately, the macro fails with a similar error to what I've written in the previous message. I tried to create a super simple example of Jedit failing. I got a failure using the latest JEdit on Windows and Ubuntu Linux with JRE 1.6.0_10 or and JRE 1.6.0_06.

I'm a-bit amazed that you can't reproduce the error. Let me know if you still can't reproduce the error -- I'll create a more sophisticated macro.

Molamini

Molamini
 
Hmm. Still can't recreate it.
by elberry on Sat, 15/11/2008 - 08:53
Hmm. Still can't recreate it. In fact when I try to run the macro it doesn't look like anything is happening at all. Perhaps I don't have the right set up within jEdit.

As for jEdit not working, I haven't heard of any issues with the Windows one, but for the Ubuntu issue, just make sure you've got the Sun Java installed, not the GNU one as this Java doesn't work right.

What's the purpose of your search and replace macro? Just to search out and replace some special text in the buffer that is currently open?

Learn from the past. Live in the present. Plan for the future.
11101000
Blog
 
Ah.. K, still can't reproduce
by elberry on Sat, 15/11/2008 - 08:56
Ah.. K, still can't reproduce your error, but I got your macro to work. I had to select the text you'd provided. Just having the buffer open didn't do anything.


Learn from the past. Live in the present. Plan for the future.
11101000
Blog
 
try this macro...
by Molamini on Sat, 15/11/2008 - 16:02
// This macro will iterate over a document finding 'WEB PAGE'
// Once found, it will ask you if you'd like to replace
// the text with a primary tag or alternative tag
// (alternative is yes)
//
// This is a recorded macro. First, check over the
// commands to make sure this is what you intended. Then,
// save this buffer, and the macro should appear in the
// Macros menu.


String value = "WEB PAGE";
String key = value.replace(" ", "_");

String link = ""+value+"";
String linkp = ""+value+"";
String linkd = ""+value+"";

textArea.setCaretPosition(1,true);
textArea.setSelectedText(linkd);
textArea.insertEnterAndIndent();
textArea.setCaretPosition( textArea.getCaretPosition()+1 );


SearchAndReplace.setSearchString(value);
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);

//
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
Log.log(Log.DEBUG, BeanShell.class, "Just before find");
while (SearchAndReplace.find(view))
{
Log.log(Log.DEBUG, BeanShell.class, "Just after find...Now question");

int response = JOptionPane.showConfirmDialog(null, "use 'alternative' string: "+value);
if (response == JOptionPane.YES_OPTION)
{
SearchAndReplace.setSearchString(value);
SearchAndReplace.setReplaceString(link);
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);

Log.log(Log.DEBUG, BeanShell.class, "about to replace text:"+value+", with link: "+link);
SearchAndReplace.replace(view);
}
else if (response == JOptionPane.NO_OPTION)
{
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setReplaceString(linkp);
SearchAndReplace.replace(view);
}
else
{


}
textArea.setCaretPosition( textArea.getCaretPosition()+1 );
SearchAndReplace.setSearchString(value);
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
}

 
I get an error now, though it
by elberry on Thu, 20/11/2008 - 21:44
I get an error now, though it's not the same one you get.

[error]
java.lang.IllegalArgumentException: caret out of bounds: 152
at org.gjt.sp.jedit.textarea.TextArea.moveCaretPosition(TextArea.java:2225)
at org.gjt.sp.jedit.textarea.TextArea.moveCaretPosition(TextArea.java:2206)
at org.gjt.sp.jedit.textarea.TextArea.setCaretPosition(TextArea.java:2173)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.gjt.sp.jedit.bsh.Reflect.invokeMethod(Reflect.java:134)
at org.gjt.sp.jedit.bsh.Reflect.invokeObjectMethod(Reflect.java:80)
at org.gjt.sp.jedit.bsh.Name.invokeMethod(Name.java:855)
at org.gjt.sp.jedit.bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
at org.gjt.sp.jedit.bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at org.gjt.sp.jedit.bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at org.gjt.sp.jedit.bsh.BSHBlock.eval(BSHBlock.java:80)
at org.gjt.sp.jedit.bsh.BSHBlock.eval(BSHBlock.java:46)
at org.gjt.sp.jedit.bsh.BSHWhileStatement.eval(BSHWhileStatement.java:72)
at org.gjt.sp.jedit.bsh.Interpreter.eval(Interpreter.java:644)
at org.gjt.sp.jedit.BeanShell._runScript(BeanShell.java:334)
at org.gjt.sp.jedit.BeanShell._runScript(BeanShell.java:279)
at org.gjt.sp.jedit.BeanShell.runScript(BeanShell.java:205)
at org.gjt.sp.jedit.Macros$BeanShellHandler.runMacro(Macros.java:1023)
at org.gjt.sp.jedit.Macros$Macro.invoke(Macros.java:442)
at org.gjt.sp.jedit.gui.InputHandler.invokeAction(InputHandler.java:352)
at org.gjt.sp.jedit.jEdit$4.invokeAction(jEdit.java:3083)
at org.gjt.sp.jedit.jEdit$4.invokeAction(jEdit.java:3067)
at org.gjt.sp.jedit.EditAction$Wrapper.actionPerformed(EditAction.java:220)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:334)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1051)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1092)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5602)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5367)
at java.awt.Container.processEvent(Container.java:2010)
at java.awt.Component.dispatchEventImpl(Component.java:4068)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Component.dispatchEvent(Component.java:3903)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
at java.awt.Container.dispatchEventImpl(Container.java:2054)
at java.awt.Window.dispatchEventImpl(Window.java:1801)
at java.awt.Component.dispatchEvent(Component.java:3903)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
[/error]

When I execute your macro I get "WEB PAGE" inserted on the first line. Is this the desired effect?

Also, it looks like you're using textArea.setCaretPosition. It would make sense that you're going to get an error because eventually you're going to tell it to set the caret position to a position that doesn't exist. In fact, if I'm reading your macro correctly, you're updating the caret position twice?

As for my platform, I'm on a mac running Java5.

Learn from the past. Live in the present. Plan for the future.
11101000
Blog
 
fixed...
by Molamini on Mon, 24/11/2008 - 17:08
elberry,
My computer update system asked me to update JEdit. After updating the program to JEdit 4.2 final, my problem went away.

Thanks for discussing this issue with me.

Molamini
 
Wow. Cool. :) What version
by elberry on Wed, 26/11/2008 - 01:14
Wow. Cool. Smiling

What version were you running before?

If you are able, I would suggest trying out the 4.3pre16 release. It's really quite stable.


Learn from the past. Live in the present. Plan for the future.
11101000
Blog
User login
Browse archives
« April 2024  
MoTuWeThFrSaSu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82348
Context Free Art (*.cfdg)   0.31   46055
JBuilder scheme   .001   18495
BBEdit scheme   1.0   18116
ColdFusion scheme   1.0   18024
R Edit Mode - extensive version   0.1   17473
Advanced HTML edit mode   1.0   16206
Matlab Edit Mode   1.0   16068
jEdit XP icons   1.0   15229
XP icons for jEdit   1.1   14293