PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Text/Reverse_Lines.bsh

#
Unknown | 97 lines | 86 code | 11 blank | 0 comment | 0 complexity | d4c424bc80eaa63e8785182013980822 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. * Reverse_Lines.bsh - a BeanShell macro script for reversing the order
  3. * of lines in a buffer. If there are selections, the lines in the selections
  4. * will be reversed, otherwise the whole buffer will be reversed.
  5. * NOTE: Lines are not sorted or reverse sorted, simply reversed.
  6. *
  7. * Copyright (C) 2002-2004 Ollie Rutherfurd, oliver@rutherfurd.net
  8. *
  9. * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false:
  10. * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  11. *
  12. * $Id: Reverse_Lines.bsh 5230 2005-07-20 13:31:08Z orutherfurd $
  13. */
  14. void reverseLines(textArea)
  15. {
  16. JEditBuffer buffer = textArea.getBuffer();
  17. Selection[] selections = textArea.getSelection();
  18. // doesn't work with rectangular selections, check for them up-front
  19. for(int i = 0; i < selections.length; i++)
  20. {
  21. if(selections[i] instanceof Selection.Rect)
  22. {
  23. Macros.error(view, "Sorry, this macro doesn't work with Rectangular Selections.");
  24. return;
  25. }
  26. }
  27. buffer.beginCompoundEdit();
  28. // do whole buffer if no selections
  29. if(selections.length == 0)
  30. {
  31. StringBuffer sb = new StringBuffer(buffer.getLength());
  32. for(int i = buffer.getLineCount() - 1; i >= 0; i--)
  33. {
  34. String line = buffer.getLineText(i);
  35. sb.append(line);
  36. if(i > 0) // don't append a newline for the last line
  37. sb.append('\n');
  38. }
  39. buffer.remove(0, buffer.getLength());
  40. buffer.insert(0, sb.toString());
  41. }
  42. // reverse all lines that are selected *NOT* just selected portions of lines
  43. else
  44. {
  45. for(int i = 0; i < selections.length; i++){
  46. int startLine = selections[i].getStartLine();
  47. int endLine = selections[i].getEndLine();
  48. int startOffset = buffer.getLineStartOffset(startLine);
  49. if(startLine < endLine)
  50. {
  51. StringBuffer sb = new StringBuffer();
  52. for(int i = endLine; i > startLine; i--)
  53. {
  54. String line = buffer.getLineText(i);
  55. sb.append(line).append('\n');
  56. }
  57. // appending first line here, so a check
  58. // can be made to see whether to append
  59. // a newline (don't if the original line
  60. // was the last line of the buffer)
  61. sb.append(buffer.getLineText(startLine));
  62. if(endLine < buffer.getLineCount() - 1)
  63. sb.append('\n');
  64. String reversed = sb.toString();
  65. buffer.remove(startOffset, reversed.length());
  66. buffer.insert(startOffset, reversed);
  67. }
  68. }
  69. }
  70. buffer.endCompoundEdit();
  71. }
  72. if(buffer.isReadOnly())
  73. Macros.error(view, "Buffer is read-only.");
  74. else
  75. reverseLines(textArea);
  76. /*
  77. Macro index entry (in DocBook XML)
  78. <listitem>
  79. <para><filename>Reverse_Lines.bsh</filename></para>
  80. <abstract><para>
  81. Reverses the selected lines or the entire buffer if no
  82. lines are selected. Does not support Rectangular
  83. Selections.
  84. </para></abstract>
  85. </listitem>
  86. */