PageRenderTime 106ms CodeModel.GetById 103ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

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