PageRenderTime 81ms CodeModel.GetById 78ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Editing/Greedy_Right.bsh

#
Unknown | 87 lines | 80 code | 7 blank | 0 comment | 0 complexity | 028a2d3aaef7374a7de2d1526ef51003 MD5 | raw file
 1/*
 2 * Greedy_Right.bsh - If a buffer is using soft tabs,
 3 * this macro will move the caret tabSize spaces to the right,
 4 * if all the characters between the caret and the next
 5 * tab stop are all spaces.  In all other cases, the caret
 6 * is moved a single character to the right.
 7 *
 8 * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
 9 *
10 * $Id: Greedy_Right.bsh 5032 2004-05-06 19:53:37Z orutherfurd $
11 */
12
13/**
14 * @param onlyFullTabs if true, the caret will only be moved
15 *                     multiple spaces it would constitute
16 *                     a 'complete' tab.
17 */
18void greedyRight(View view, boolean onlyFullTabs)
19{
20	JEditTextArea textArea = view.getTextArea();
21	Buffer buffer = textArea.getBuffer();
22	int caret = textArea.getCaretPosition();
23	int caretLine = textArea.getCaretLine();
24	int lineStart = textArea.getLineStartOffset(caretLine);
25	int lineEnd = textArea.getLineEndOffset(caretLine);
26
27	if(textArea.getCaretPosition() == buffer.getLength())
28		return;
29
30	if(buffer.getBooleanProperty("noTabs") == true)
31	{
32		// if anything is selected, use standard 
33		if(textArea.getSelection().length != 0)
34		{
35			textArea.setCaretPosition(caret+1);
36		}
37		// if at the end of the line, go to the 
38		// start of the next line (+1 for \n)
39		else if(caret+1 >= lineEnd)
40		{
41			textArea.setCaretPosition(caret+1);
42		}
43		else
44		{
45			int col = caret - lineStart;
46			int tabSize = buffer.getIntegerProperty("tabSize",8);
47
48			// unlikely, but just in case
49			if(tabSize <= 0)
50			{
51				textArea.setCaretPosition(caret+11);
52			}
53			else
54			{
55				int toTabStop = (((col+tabSize)-1) % tabSize) + 1;
56				int count = 1;
57				for(int i=1; i < toTabStop; i++)
58				{
59					int pos = caret+i+1;
60					if(pos > buffer.getLength())
61						break;
62					if(!" ".equals(buffer.getText(caret+i,1)))
63						break;
64					count += 1;
65				}
66
67				// if onlyFullTabs must be only spaces to
68				// the tabStop and must have tabSize number 
69				// of spaces to remove them all.
70				if(onlyFullTabs == false || count == tabSize){
71					textArea.setCaretPosition(caret+count);
72				}
73				else{
74					textArea.setCaretPosition(caret+1);
75				}
76			}
77		}
78	}
79	else
80		textArea.setCaretPosition(caret+1);
81}
82
83if(buffer.isReadOnly())
84	getToolkit().beep();
85else
86	greedyRight(view,true);
87