File: Greedy_Delete.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 (84 lines) | stat: -rw-r--r-- 2,032 bytes parent folder | download | duplicates (4)
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
81
82
83
84
/*
 * Greedy_Delete.bsh - If a buffer is using soft tabs,
 * this macro will delete tabSize number of spaces, if
 * all the characters between the caret and the next tab
 * stop are spaces.  In all other cases a single character
 * is deleted.
 *
 * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
 *
 * $Id: Greedy_Delete.bsh 23971 2015-08-08 19:37:35Z daleanson $
 */

/**
 * @param onlyFullTabs if true, multiple spaces are only
 *                     removed if they would constitute
 *                     a 'complete' tab.
 */
void greedyDelete(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);
	int lineEnd = textArea.getLineEndOffset(caretLine);

	if(buffer.getBooleanProperty("noTabs") == true)
	{
		// if anything is selected, use standard 
		if(textArea.getSelection().length != 0)
		{
			textArea.delete();
		}
		// if at the end of the line, go to the 
		// start of the next line (+1 for \n)
		else if(caret+1 >= lineEnd)
		{
			textArea.delete();
		}
		else
		{
			int col = caret - lineStart;
			int tabSize = buffer.getIntegerProperty("tabSize",8);

			// unlikely, but just in case
			if(tabSize <= 0)
			{
				textArea.delete();
			}
			else
			{
				int toTabStop = (((col+tabSize)-1) % tabSize) + 1;
				// don't wrap to next line
				if(caret+toTabStop > lineEnd){
					textArea.delete();
					return;
				}

				int count = 0;
				for(int i=0; i < toTabStop; i++)
				{
					if(!" ".equals(buffer.getText(caret+i,1)))
						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){
					buffer.remove(caret,count);
				}
				else{
					textArea.delete();
				}
			}
		}
	}
	else
		textArea.delete();
}

	greedyDelete(view,true);