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

# · Unknown · 87 lines · 80 code · 7 blank · 0 comment · 0 complexity · 028a2d3aaef7374a7de2d1526ef51003 MD5 · raw file

  1. /*
  2. * Greedy_Right.bsh - If a buffer is using soft tabs,
  3. * this macro will move the caret tabSize spaces to the right,
  4. * if all the characters between the caret and the next
  5. * tab stop are all spaces. In all other cases, the caret
  6. * is moved a single character to the right.
  7. *
  8. * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
  9. *
  10. * $Id: Greedy_Right.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 greedyRight(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(textArea.getCaretPosition() == buffer.getLength())
  26. return;
  27. if(buffer.getBooleanProperty("noTabs") == true)
  28. {
  29. // if anything is selected, use standard
  30. if(textArea.getSelection().length != 0)
  31. {
  32. textArea.setCaretPosition(caret+1);
  33. }
  34. // if at the end of the line, go to the
  35. // start of the next line (+1 for \n)
  36. else if(caret+1 >= lineEnd)
  37. {
  38. textArea.setCaretPosition(caret+1);
  39. }
  40. else
  41. {
  42. int col = caret - lineStart;
  43. int tabSize = buffer.getIntegerProperty("tabSize",8);
  44. // unlikely, but just in case
  45. if(tabSize <= 0)
  46. {
  47. textArea.setCaretPosition(caret+11);
  48. }
  49. else
  50. {
  51. int toTabStop = (((col+tabSize)-1) % tabSize) + 1;
  52. int count = 1;
  53. for(int i=1; i < toTabStop; i++)
  54. {
  55. int pos = caret+i+1;
  56. if(pos > buffer.getLength())
  57. break;
  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. textArea.setCaretPosition(caret+count);
  67. }
  68. else{
  69. textArea.setCaretPosition(caret+1);
  70. }
  71. }
  72. }
  73. }
  74. else
  75. textArea.setCaretPosition(caret+1);
  76. }
  77. if(buffer.isReadOnly())
  78. getToolkit().beep();
  79. else
  80. greedyRight(view,true);