PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Unknown | 83 lines | 76 code | 7 blank | 0 comment | 0 complexity | 990b66808a5026e546c67203405df96b 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_Left.bsh - If a buffer is using soft tabs,
  3. * this macro will move the caret tabSize spaces to the left,
  4. * if all the characters between the caret and the previous
  5. * tab stop are all spaces. In all other cases, the caret
  6. * is moved a single character to the left.
  7. *
  8. * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
  9. *
  10. * $Id: Greedy_Left.bsh 5032 2004-05-06 19:53:37Z orutherfurd $
  11. */
  12. /**
  13. * @param onlyFullTabs if true, the caret will only be moved
  14. * multiple spaces it would constitute
  15. * a 'complete' tab.
  16. */
  17. void greedyLeft(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(textArea.getCaretPosition() == 0)
  25. return;
  26. if(buffer.getBooleanProperty("noTabs") == true)
  27. {
  28. // if anything is selected, use standard
  29. if(textArea.getSelection().length != 0)
  30. {
  31. textArea.setCaretPosition(caret-1);
  32. }
  33. // if at the start of the line, use standard
  34. else if(caret == lineStart)
  35. {
  36. textArea.setCaretPosition(caret-1);
  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.setCaretPosition(caret-1);
  46. }
  47. else
  48. {
  49. int toTabStop = ((col-1) % tabSize) + 1;
  50. int count = 1;
  51. for(int i=1; i < toTabStop; i++)
  52. {
  53. // only backspace over spaces
  54. if(!" ".equals(buffer.getText(caret-(i+1),1)))
  55. break;
  56. count += 1;
  57. }
  58. // if onlyFullTabs must be only spaces to
  59. // the tabStop and must have tabSize number
  60. // of spaces to remove them all.
  61. if(onlyFullTabs == false || count == tabSize){
  62. textArea.setCaretPosition(caret-count);
  63. }
  64. else{
  65. textArea.setCaretPosition(caret-1);
  66. }
  67. }
  68. }
  69. }
  70. else
  71. textArea.setCaretPosition(caret-1);
  72. }
  73. if(buffer.isReadOnly())
  74. getToolkit().beep();
  75. else
  76. greedyLeft(view,true);