/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. * @param onlyFullTabs if true, multiple spaces are only
  14. * removed if they would constitute
  15. * a 'complete' tab.
  16. */
  17. void greedyBackspace(View view, boolean onlyFullTabs)
  18. {
  19. JEditTextArea textArea = view.getTextArea();
  20. Buffer buffer = textArea.getBuffer();
  21. int caret = textArea.getCaretPosition();
  22. int caretLine = textArea.getCaretLine();
  23. int lineStart = textArea.getLineStartOffset(caretLine);
  24. if(buffer.getBooleanProperty("noTabs") == true)
  25. {
  26. // if anything is selected, use standard
  27. if(textArea.getSelection().length != 0)
  28. {
  29. textArea.backspace();
  30. }
  31. // if at the start of the line, use standard
  32. else if(caret == lineStart)
  33. {
  34. textArea.backspace();
  35. }
  36. else
  37. {
  38. int col = caret - lineStart;
  39. int tabSize = buffer.getIntegerProperty("tabSize",8);
  40. // unlikely, but just in case
  41. if(tabSize <= 0)
  42. {
  43. buffer.remove(caret-1,1);
  44. }
  45. else
  46. {
  47. int toTabStop = ((col-1) % tabSize) + 1;
  48. int count = 1;
  49. for(int i=1; i < toTabStop; i++)
  50. {
  51. // only backspace over spaces
  52. if(!" ".equals(buffer.getText(caret-(i+1),1)))
  53. break;
  54. count += 1;
  55. }
  56. // if onlyFullTabs must be only spaces to
  57. // the tabStop and must have tabSize number
  58. // of spaces to remove them all.
  59. if(onlyFullTabs == false || count == tabSize){
  60. buffer.remove(caret-count,count);
  61. }
  62. else{
  63. buffer.remove(caret-1,1);
  64. }
  65. }
  66. }
  67. }
  68. else
  69. textArea.backspace();
  70. }
  71. if(buffer.isReadOnly())
  72. getToolkit().beep();
  73. else
  74. greedyBackspace(view,true);