jEdit Community - Resources for users of the jEdit Text Editor
Copy or Cut Lines Containing
Submitted by tflight on Sunday, 1 August, 2004 - 17:52
Hello, I'm new to jEdit coming from BBEdit. BBEdit had functions called "Copy Lines Containing..." and "Cut Lines Containing...". I can't find that jEdit has a similar function, does it exist?

For those that are not familiar with those functions here is an example. Take the following text file:

bananas, apples, oranges, grapes
apples, bananas, grapes
oranges, grapes
bananas, oranges, apples, grapes, kiwi

If you choose the "Copy Lines Containing..." function and specify 'bananas' as your query your clipboard will be set with the following

bananas, apples, oranges, grapes
apples, bananas, grapes
bananas, oranges, apples, grapes, kiwi
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
try these macros: Copy
by Anonymous on Sun, 01/08/2004 - 20:48
try these macros:
Copy_Lines_Containing.bsh:
copyLinesContaining(){
	String text = Macros.input(view,"Copy lines containing:");
	if(text == null || "".equals(text))
		return;
	int count = 0;
	StringBuffer buff = new StringBuffer();
	for(int i = 0; i < buffer.getLineCount(); i++){
		String line = buffer.getLineText(i);
		if(line.indexOf(text) > -1){
			buff.append(line).append('\n');
			count++;
		}
	}
	Registers.setRegister('$',buff.toString());
	HistoryModel.getModel("clipboard").addItem(buff.toString());
	view.getStatus().setMessageAndClear("" + count + " lines copied");
}

copyLinesContaining();
Cut_Lines_Containing.bsh:
cutLinesContaining(){
	String text = Macros.input(view,"Cut lines containing:");
	if(text == null || "".equals(text))
		return;
	int count = 0;
	int start = 0;
	int end = 0;
	StringBuffer buff = new StringBuffer();
	try{
		buffer.beginCompoundEdit();
		for(int i = buffer.getLineCount() - 1 ; i >= 0; i--){
			String line = buffer.getLineText(i);
			if(line.indexOf(text) > -1){
				buff.append(line).append('\n');
				int start = buffer.getLineStartOffset(i);
				int end = buffer.getLineEndOffset(i);
				buffer.remove(start,end-start);
				count++;
			}
		}
	}
	finally{
		if(count > 0)
			buffer.endCompoundEdit();
	}
	Registers.setRegister('$',buff.toString());
	HistoryModel.getModel("clipboard").addItem(buff.toString());
	view.getStatus().setMessageAndClear("" + count + " lines cut");
}

if(buffer.isReadOnly())
	Toolkit.getDefaultToolkit().beep();
else
	cutLinesContaining();
You can also use the TextFilter plugin to write scripts to manipulate buffers -- I believe the plugin is based on a BBEdit feature.
 
Thanks!
by tflight on Sun, 01/08/2004 - 21:51
Thanks a bunch. That is GREAT!!!!! The copy lines containing macro works spectacular. The cut lines containing macro resulted in the following error:

java.lang.ArrayIndexOutOfBoundsException: 76:40
at org.gjt.sp.jedit.Buffer.remove(Buffer.java:1218)
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:324)
at bsh.Reflect.invokeOnMethod(Reflect.java:149)
at bsh.Reflect.invokeObjectMethod(Reflect.java:81)
at bsh.Name.invokeMethod(Name.java:856)
at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:72)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHForStatement.eval(BSHForStatement.java:111)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHTryStatement.eval(BSHTryStatement.java:86)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BshMethod.invokeImpl(BshMethod.java:349)
at bsh.BshMethod.invoke(BshMethod.java:246)
at bsh.BshMethod.invoke(BshMethod.java:179)
at bsh.Name.invokeLocalMethod(Name.java:915)
at bsh.Name.invokeMethod(Name.java:805)
at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:72)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:51)
at bsh.Interpreter.eval(Interpreter.java:641)
at org.gjt.sp.jedit.BeanShell._runScript(BeanShell.java:355)
at org.gjt.sp.jedit.BeanShell._runScript(BeanShell.java:284)
at org.gjt.sp.jedit.BeanShell.runScript(BeanShell.java:210)
at org.gjt.sp.jedit.Macros$BeanShellHandler.runMacro(Macros.java:993)
at org.gjt.sp.jedit.Macros$Macro.invoke(Macros.java:417)
at org.gjt.sp.jedit.gui.InputHandler.invokeAction(InputHandler.java:229)
at org.gjt.sp.jedit.jEdit$3.invokeAction(jEdit.java:2912)
at org.gjt.sp.jedit.EditAction$Wrapper.actionPerformed(EditAction.java:216)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1819)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1872)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:321)
at apple.laf.ScreenMenuItem.actionPerformed(ScreenMenuItem.java:83)
at java.awt.MenuItem.processActionEvent(MenuItem.java:589)
at java.awt.MenuItem.processEvent(MenuItem.java:548)
at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:285)
at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:273)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:170)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
 
Oops, that and reversing the
by Anonymous on Mon, 02/08/2004 - 13:21
Oops, that and reversing the cut lines should be fixed:
cutLinesContaining(){
	String text = Macros.input(view,"Cut lines containing:");
	if(text == null || "".equals(text))
		return;
	int count = 0;
	int start = 0;
	int end = 0;
	StringBuffer buff = new StringBuffer();
	try{
		buffer.beginCompoundEdit();
		for(int i = buffer.getLineCount() - 1 ; i >= 0; i--){
			String line = buffer.getLineText(i);
			if(line.indexOf(text) > -1){
				buff.insert(0,'\n').insert(0,line);
				int start = buffer.getLineStartOffset(i);
				int end = buffer.getLineEndOffset(i);
				buffer.remove(start,Math.min(end,buffer.getLength())-start);
				count++;
			}
		}
	}
	finally{
		buffer.endCompoundEdit();
	}
	Registers.setRegister('$',buff.toString());
	HistoryModel.getModel("clipboard").addItem(buff.toString());
	view.getStatus().setMessageAndClear("" + count + " lines cut");
}

if(buffer.isReadOnly())
	Toolkit.getDefaultToolkit().beep();
else
	cutLinesContaining();
 
Now that works
by tflight on Tue, 03/08/2004 - 03:23
Thanks a bunch!! Yippe!!!!! You have no idea how much easier that makes life! YEA!!!!!

-Tim
User login
Browse archives
« November 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   101634
Context Free Art (*.cfdg)   0.31   46062
BBEdit scheme   1.0   18601
JBuilder scheme   .001   18502
ColdFusion scheme   1.0   18031
R Edit Mode - extensive version   0.1   17481
Advanced HTML edit mode   1.0   16213
Matlab Edit Mode   1.0   16075
jEdit XP icons   1.0   15236
XP icons for jEdit   1.1   14300