PageRenderTime 65ms CodeModel.GetById 61ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

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