/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /*
- * Greedy_Backspace.bsh - If buffer is using soft tabs,
- * this macro will backspace to the previous tab stop,
- * if all characters between the caret and the tab stop
- * are spaces. In all other cases a single character is
- * removed.
- *
- * Copyright (C) 2002-2004 Ollie Rutherfurd <oliver@jedit.org>
- *
- * $Id: Greedy_Backspace.bsh 5032 2004-05-06 19:53:37Z orutherfurd $
- */
- /**
- * @param onlyFullTabs if true, multiple spaces are only
- * removed if they would constitute
- * a 'complete' tab.
- */
- void greedyBackspace(View view, boolean onlyFullTabs)
- {
- JEditTextArea textArea = view.getTextArea();
- Buffer buffer = textArea.getBuffer();
- int caret = textArea.getCaretPosition();
- int caretLine = textArea.getCaretLine();
- int lineStart = textArea.getLineStartOffset(caretLine);
- if(buffer.getBooleanProperty("noTabs") == true)
- {
- // if anything is selected, use standard
- if(textArea.getSelection().length != 0)
- {
- textArea.backspace();
- }
- // if at the start of the line, use standard
- else if(caret == lineStart)
- {
- textArea.backspace();
- }
- else
- {
- int col = caret - lineStart;
- int tabSize = buffer.getIntegerProperty("tabSize",8);
- // unlikely, but just in case
- if(tabSize <= 0)
- {
- buffer.remove(caret-1,1);
- }
- else
- {
- int toTabStop = ((col-1) % tabSize) + 1;
- int count = 1;
- for(int i=1; i < toTabStop; i++)
- {
- // only backspace over spaces
- if(!" ".equals(buffer.getText(caret-(i+1),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,count);
- }
- else{
- buffer.remove(caret-1,1);
- }
- }
- }
- }
- else
- textArea.backspace();
- }
- if(buffer.isReadOnly())
- getToolkit().beep();
- else
- greedyBackspace(view,true);