jEdit Community - Resources for users of the jEdit Text Editor
How do you manipulate the Selected Text?
Submitted by LDiracDelta on Friday, 15 September, 2006 - 16:17
I want to write a macro that grabs out the selected text, manipulates it ( using something other than a search and replace) and then re-inserts the text back into the buffer. What are the magic classes and things I need to do to make this happen?
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
RE: How do you manipulate the Selected Text?
by LDiracDelta on Fri, 15/09/2006 - 21:19
You can decipher this from http://community.jedit.org/?q=node/view/2781 or from the code:

void columnize_selection()
{
	int margin = 2;
	String delimiter = "\\s+";
	Buffer buffer = textArea.getBuffer();
	Selection[] selections = textArea.getSelection();
	
	// doesn't work with rectangular selections and multi-selections, check for them up-front
	if (selections.length > 1)
	{
		Macros.error(view, "Sorry, this macro doesn't work with more than one Selection. ");
		return;
	}
	else if (selections.length == 1 && selections[0] instanceof Selection.Rect)
	{
		Macros.error(view, "Sorry, this macro doesn't work with Rectangular Selections.");
		return; 
	}
	
	// get the selection. 
	Selection selection = selections.length > 0 ? selections[0] : null;
	// a map for sorting
	//Map map = new TreeMap(); 
	// a map to store the duplicate rows. 
	//Map dupMap = new HashMap(); 
	
	int startLine = 0; 
	int endLine = 0; 
	if (selections.length == 0)
	{
		startLine = 0; 
		endLine = buffer.getLineCount() - 1; 
	}
	else 
	{
		startLine = selection.getStartLine(); 
		endLine = selection.getEndLine(); 
	}
	
	ArrayList longest_in_column = new ArrayList(); 
	
	// Find the longest line
	for (int i = startLine; i <= endLine; i++) 
	{
		String line = buffer.getLineText(i);
		String [] split = line.split(delimiter);
		for (int j = 0 ; j < split.length; j++){
			if (j >= longest_in_column.size())
				longest_in_column.add(new Integer(0));
			longest_in_column.set(j, new Integer( 
				java.lang.Math.max( 
					((Integer)longest_in_column.get(j)).intValue(),
					split[j].length()
					)));
		}
	}
	
	StringBuffer sb = new StringBuffer();
	
	// Format the text
	for (int i = startLine; i <= endLine; i++){
		String line = buffer.getLineText(i);
		String [] split = line.split("\\s+");
		for (int j = 0 ; j < split.length; j++){
			sb.append(split[j]);
			if ( j != split.length - 1){
				for ( int k = 0; 
					k < ((Integer) longest_in_column.get(j)).intValue() + margin - ((String)split[j]).length();
					k++)
					sb.append(' ');
			}
		}
		if ( i != endLine)
			sb.append("\n");
	}
	
	buffer.beginCompoundEdit();
	
	int startOffset = buffer.getLineStartOffset(startLine);
	int endOffset = buffer.getLineEndOffset(endLine); 
	
	buffer.remove(startOffset, endOffset - startOffset - 1);
	buffer.insert(startOffset, sb.toString());
	
	buffer.endCompoundEdit();
}

columnize_selection();
User login
Browse archives
« March 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
31
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   82339
Context Free Art (*.cfdg)   0.31   46046
JBuilder scheme   .001   18487
BBEdit scheme   1.0   18108
ColdFusion scheme   1.0   18016
R Edit Mode - extensive version   0.1   17465
Advanced HTML edit mode   1.0   16198
Matlab Edit Mode   1.0   16060
jEdit XP icons   1.0   15221
XP icons for jEdit   1.1   14285