PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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
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
  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. * @param onlyFullTabs if true, multiple spaces are only
  14. * removed if they would constitute
  15. * a 'complete' tab.
  16. */
  17. void greedyDelete(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. int lineEnd = textArea.getLineEndOffset(caretLine);
  25. if(buffer.getBooleanProperty("noTabs") == true)
  26. {
  27. // if anything is selected, use standard
  28. if(textArea.getSelection().length != 0)
  29. {
  30. textArea.delete();
  31. }
  32. // if at the end of the line, go to the
  33. // start of the next line (+1 for \n)
  34. else if(caret+1 >= lineEnd)
  35. {
  36. textArea.delete();
  37. }
  38. else
  39. {
  40. int col = caret - lineStart;
  41. int tabSize = buffer.getIntegerProperty("tabSize",8);
  42. // unlikely, but just in case
  43. if(tabSize <= 0)
  44. {
  45. textArea.delete();
  46. }
  47. else
  48. {
  49. int toTabStop = (((col+tabSize)-1) % tabSize) + 1;
  50. // don't wrap to next line
  51. if(caret+toTabStop > lineEnd)
  52. textArea.delete();
  53. int count = 1;
  54. for(int i=1; i < toTabStop; i++)
  55. {
  56. if(!" ".equals(buffer.getText(caret+i,1)))
  57. break;
  58. count += 1;
  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);
  65. }
  66. else{
  67. textArea.delete();
  68. }
  69. }
  70. }
  71. }
  72. else
  73. textArea.delete();
  74. }
  75. if(buffer.isReadOnly())
  76. getToolkit().beep();
  77. else
  78. greedyDelete(view,true);