File: Greedy_Left.bsh

package info (click to toggle)
jedit 5.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,252 kB
  • ctags: 11,190
  • sloc: java: 98,480; xml: 94,070; makefile: 52; sh: 42; cpp: 6; python: 6
file content (80 lines) | stat: -rw-r--r-- 2,053 bytes parent folder | download | duplicates (6)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Greedy_Left.bsh - If a buffer is using soft tabs,
 * this macro will move the caret tabSize spaces to the left,
 * if all the characters between the caret and the previous
 * tab stop are all spaces.  In all other cases, the caret
 * is moved a single character to the left.
 *
 * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
 *
 * $Id: Greedy_Left.bsh 5230 2005-07-20 13:31:08Z orutherfurd $
 */

/**
 * @param onlyFullTabs if true, the caret will only be moved
 *                     multiple spaces it would constitute
 *                     a 'complete' tab.
 */
void greedyLeft(View view, boolean onlyFullTabs)
{
	JEditTextArea textArea = view.getTextArea();
	JEditBuffer buffer = textArea.getBuffer();
	int caret = textArea.getCaretPosition();
	int caretLine = textArea.getCaretLine();
	int lineStart = textArea.getLineStartOffset(caretLine);

	if(textArea.getCaretPosition() == 0)
		return;

	if(buffer.getBooleanProperty("noTabs") == true)
	{
		// if anything is selected, use standard 
		if(textArea.getSelection().length != 0)
		{
			textArea.setCaretPosition(caret-1);
		}
		// if at the start of the line, use standard
		else if(caret == lineStart)
		{
			textArea.setCaretPosition(caret-1);
		}
		else
		{
			int col = caret - lineStart;
			int tabSize = buffer.getIntegerProperty("tabSize",8);

			// unlikely, but just in case
			if(tabSize <= 0)
			{
				textArea.setCaretPosition(caret-1);
			}
			else
			{
				int toTabStop = ((col-1) % tabSize) + 1;
				int count = 0;
				String chunk = buffer.getText(caret-toTabStop,toTabStop);
				for(int i=0; i < toTabStop; i++)
				{
					if(' ' != chunk.charAt(i))
						break;
					count += 1;
				}

				// if onlyFullTabs must be only spaces to
				// the tabStop and must have tabSize number 
				// of spaces to remove them all.
				if(onlyFullTabs == false || count == tabSize){
					textArea.setCaretPosition(caret-count);
				}
				else{
					textArea.setCaretPosition(caret-1);
				}
			}
		}
	}
	else
		textArea.setCaretPosition(caret-1);
}

greedyLeft(view,true);