PageRenderTime 29ms CodeModel.GetById 26ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Editing/Greedy_Delete.bsh

#
Unknown | 87 lines | 80 code | 7 blank | 0 comment | 0 complexity | 2e610839ade5e7d62feac1cf3d9cd855 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 5230 2005-07-20 13:31:08Z 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	JEditBuffer 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					return;
57				}
58
59				int count = 0;
60				for(int i=0; i < toTabStop; i++)
61				{
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					buffer.remove(caret,count);
72				}
73				else{
74					textArea.delete();
75				}
76			}
77		}
78	}
79	else
80		textArea.delete();
81}
82
83if(buffer.isReadOnly())
84	Toolkit.getDefaultToolkit().beep();
85else
86	greedyDelete(view,true);
87