PageRenderTime 266ms CodeModel.GetById 262ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Unknown | 83 lines | 76 code | 7 blank | 0 comment | 0 complexity | 990b66808a5026e546c67203405df96b MD5 | raw file
 1/*
 2 * Greedy_Left.bsh - If a buffer is using soft tabs,
 3 * this macro will move the caret tabSize spaces to the left,
 4 * if all the characters between the caret and the previous
 5 * tab stop are all spaces.  In all other cases, the caret
 6 * is moved a single character to the left.
 7 *
 8 * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
 9 *
10 * $Id: Greedy_Left.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 greedyLeft(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
26	if(textArea.getCaretPosition() == 0)
27		return;
28
29	if(buffer.getBooleanProperty("noTabs") == true)
30	{
31		// if anything is selected, use standard 
32		if(textArea.getSelection().length != 0)
33		{
34			textArea.setCaretPosition(caret-1);
35		}
36		// if at the start of the line, use standard
37		else if(caret == lineStart)
38		{
39			textArea.setCaretPosition(caret-1);
40		}
41		else
42		{
43			int col = caret - lineStart;
44			int tabSize = buffer.getIntegerProperty("tabSize",8);
45
46			// unlikely, but just in case
47			if(tabSize <= 0)
48			{
49				textArea.setCaretPosition(caret-1);
50			}
51			else
52			{
53				int toTabStop = ((col-1) % tabSize) + 1;
54				int count = 1;
55				for(int i=1; i < toTabStop; i++)
56				{
57					// only backspace over spaces
58					if(!" ".equals(buffer.getText(caret-(i+1),1)))
59						break;
60					count += 1;
61				}
62
63				// if onlyFullTabs must be only spaces to
64				// the tabStop and must have tabSize number 
65				// of spaces to remove them all.
66				if(onlyFullTabs == false || count == tabSize){
67					textArea.setCaretPosition(caret-count);
68				}
69				else{
70					textArea.setCaretPosition(caret-1);
71				}
72			}
73		}
74	}
75	else
76		textArea.setCaretPosition(caret-1);
77}
78
79if(buffer.isReadOnly())
80	getToolkit().beep();
81else
82	greedyLeft(view,true);
83