PageRenderTime 251ms CodeModel.GetById 248ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Text/Reverse_Lines.bsh

#
Unknown | 97 lines | 86 code | 11 blank | 0 comment | 0 complexity | d4c424bc80eaa63e8785182013980822 MD5 | raw file
 1/*
 2 * Reverse_Lines.bsh - a BeanShell macro script for reversing the order
 3 * of lines in a buffer.  If there are selections, the lines in the selections
 4 * will be reversed, otherwise the whole buffer will be reversed.
 5 * NOTE: Lines are not sorted or reverse sorted, simply reversed.
 6 *
 7 * Copyright (C) 2002-2004 Ollie Rutherfurd, oliver@rutherfurd.net
 8 *
 9 * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false:
10 * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
11 *
12 * $Id: Reverse_Lines.bsh 5230 2005-07-20 13:31:08Z orutherfurd $
13 */
14
15void reverseLines(textArea)
16{
17	JEditBuffer buffer = textArea.getBuffer();
18	Selection[] selections = textArea.getSelection();
19
20	// doesn't work with rectangular selections, check for them up-front
21	for(int i = 0; i < selections.length; i++)
22	{
23		if(selections[i] instanceof Selection.Rect)
24		{
25			Macros.error(view, "Sorry, this macro doesn't work with Rectangular Selections.");
26			return;
27		}
28	}
29
30	buffer.beginCompoundEdit();
31
32	// do whole buffer if no selections
33	if(selections.length == 0)
34	{
35		StringBuffer sb = new StringBuffer(buffer.getLength());
36		for(int i = buffer.getLineCount() - 1; i >= 0; i--)
37		{
38			String line = buffer.getLineText(i);
39			sb.append(line);
40			if(i > 0)	// don't append a newline for the last line
41				sb.append('\n');
42		}
43		buffer.remove(0, buffer.getLength());
44		buffer.insert(0, sb.toString());
45	}
46	// reverse all lines that are selected *NOT* just selected portions of lines
47	else
48	{
49		for(int i = 0; i < selections.length; i++){
50
51			int startLine = selections[i].getStartLine();
52			int endLine = selections[i].getEndLine();
53			int startOffset = buffer.getLineStartOffset(startLine);
54
55			if(startLine < endLine)
56			{
57				StringBuffer sb = new StringBuffer();
58				for(int i = endLine; i > startLine; i--)
59				{
60					String line = buffer.getLineText(i);
61					sb.append(line).append('\n');
62				}
63
64				// appending first line here, so a check
65				// can be made to see whether to append
66				// a newline (don't if the original line
67				// was the last line of the buffer)
68				sb.append(buffer.getLineText(startLine));
69				if(endLine < buffer.getLineCount() - 1)
70					sb.append('\n');
71
72				String reversed = sb.toString();
73				buffer.remove(startOffset, reversed.length());
74				buffer.insert(startOffset, reversed);
75			}
76		}
77	}
78	buffer.endCompoundEdit();
79}
80
81if(buffer.isReadOnly())
82	Macros.error(view, "Buffer is read-only.");
83else
84	reverseLines(textArea);
85
86/*
87	Macro index entry (in DocBook XML)
88
89<listitem>
90	<para><filename>Reverse_Lines.bsh</filename></para>
91	<abstract><para>
92		Reverses the selected lines or the entire buffer if no
93		lines are selected.  Does not support Rectangular
94		Selections.
95	</para></abstract>
96</listitem>
97*/