PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
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 5230 2005-07-20 13:31:08Z 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. JEditBuffer 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. return;
  54. }
  55. int count = 0;
  56. for(int i=0; i < toTabStop; i++)
  57. {
  58. if(!" ".equals(buffer.getText(caret+i,1)))
  59. break;
  60. count += 1;
  61. }
  62. // if onlyFullTabs must be only spaces to
  63. // the tabStop and must have tabSize number
  64. // of spaces to remove them all.
  65. if(onlyFullTabs == false || count == tabSize){
  66. buffer.remove(caret,count);
  67. }
  68. else{
  69. textArea.delete();
  70. }
  71. }
  72. }
  73. }
  74. else
  75. textArea.delete();
  76. }
  77. if(buffer.isReadOnly())
  78. Toolkit.getDefaultToolkit().beep();
  79. else
  80. greedyDelete(view,true);