PageRenderTime 86ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/macros/Text/Vertical_Paste.bsh

#
Unknown | 285 lines | 236 code | 49 blank | 0 comment | 0 complexity | 0af0412917b530ff102080278fa1c6f6 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. * Vertical_Paste.bsh - a BeanShell macro script for the
  3. * jEdit text editor - Pastes the content of the clipboard
  4. * vertically and fills empty areas if necessary.
  5. * Copyright (c) 2001 Andre Kaplan, portions by Slava Pestov
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. * Checked for jEeit 4.0 API
  22. *
  23. */
  24. import java.util.StringTokenizer;
  25. import javax.swing.text.BadLocationException;
  26. import javax.swing.text.Segment;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.jedit.textarea.*;
  29. import org.gjt.sp.util.Log;
  30. // Fill empty areas to the left.
  31. boolean leftFill = true;
  32. // Fill empty areas to the right.
  33. boolean rightFill = true;
  34. /**
  35. * Adapted from org.gjt.sp.jedit.gui.StatusBar.VICaretStatus.getVirtualPosition
  36. */
  37. int physicalToVirtualLineOffset(int line, int physicalLineOffset) {
  38. Segment seg = new Segment();
  39. textArea.getLineText(line, seg);
  40. int physicalPosition = 0;
  41. int virtualPosition = 0;
  42. int tabSize = buffer.getTabSize();
  43. for (; (physicalPosition < seg.count) && (physicalPosition < physicalLineOffset); ++physicalPosition) {
  44. char ch = seg.array[seg.offset + physicalPosition];
  45. if (ch == '\t') {
  46. virtualPosition += tabSize
  47. - (virtualPosition % tabSize);
  48. } else {
  49. ++virtualPosition;
  50. }
  51. }
  52. return virtualPosition;
  53. }
  54. ConversionIndicator() {
  55. type = 0;
  56. return this;
  57. }
  58. /**
  59. * Returns the virtual to physical line offset conversion.
  60. * The conversion indicator is set to:
  61. * <ul>
  62. * <li>0 if the virtual line offset could be converted to a physical
  63. * offset</li>
  64. * <li>-1 if the virtual line offset is beyond the line end offset</li>
  65. * <li>1 if the virtual could not be exactly converted to a physical
  66. * offset due to the expansion of a tab</li>
  67. * </ul>
  68. */
  69. int virtualToPhysicalLineOffset(int line, int virtualLineOffset, Object indicator) {
  70. Segment seg = new Segment();
  71. textArea.getLineText(line, seg);
  72. int physicalPosition = 0;
  73. int virtualPosition = 0;
  74. int tabSize = buffer.getTabSize();
  75. for (; (physicalPosition < seg.count) && (virtualPosition < virtualLineOffset); ++physicalPosition) {
  76. char ch = seg.array[seg.offset + physicalPosition];
  77. if (ch == '\t') {
  78. virtualPosition += tabSize
  79. - (virtualPosition % tabSize);
  80. } else {
  81. ++virtualPosition;
  82. }
  83. }
  84. if (virtualPosition < virtualLineOffset) {
  85. indicator.type = -1;
  86. } else if (virtualPosition == virtualLineOffset) {
  87. indicator.type = 0;
  88. } else {
  89. indicator.type = 1;
  90. }
  91. return physicalPosition;
  92. }
  93. int getExpandedLength(String text, int virtualLineOffset) {
  94. int len = text.length();
  95. int virtualPosition = virtualLineOffset;
  96. int tabSize = buffer.getTabSize();
  97. for (int i = 0; i < len; i++) {
  98. char ch = text.charAt(i);
  99. if (ch == '\t') {
  100. virtualPosition += tabSize
  101. - (virtualPosition % tabSize);
  102. } else {
  103. ++virtualPosition;
  104. }
  105. }
  106. return (virtualPosition - virtualLineOffset);
  107. }
  108. void verticalPaste() {
  109. /*
  110. * Guard for readonly files becuase Buffer.insert()
  111. * ignores the flag
  112. *
  113. */
  114. if(buffer.isReadOnly())
  115. {
  116. Macros.error(view, "This file is read only.");
  117. return;
  118. }
  119. String verticalText = Registers.getRegister('$').toString();
  120. final int firstLine = textArea.getCaretLine();
  121. final int firstPhysicalLineOffset = (
  122. textArea.getCaretPosition()
  123. - textArea.getLineStartOffset(firstLine)
  124. );
  125. final int firstVirtualLineOffset = physicalToVirtualLineOffset(
  126. firstLine, firstPhysicalLineOffset
  127. );
  128. int minVirtualLineOffset = firstVirtualLineOffset;
  129. int maxVirtualLineOffset = firstVirtualLineOffset;
  130. StringTokenizer st = new StringTokenizer(verticalText, "\n");
  131. int height = st.countTokens();
  132. for (int line = firstLine; st.hasMoreTokens(); line++) {
  133. String s = st.nextToken();
  134. int virtualLineEndOffset = (
  135. (line >= textArea.getLineCount())
  136. ? 0
  137. : physicalToVirtualLineOffset(line, textArea.getLineLength(line))
  138. );
  139. int virtualLineOffset = (
  140. (leftFill)
  141. ? firstVirtualLineOffset
  142. : Math.min(firstVirtualLineOffset, virtualLineEndOffset)
  143. );
  144. int expandedLen = getExpandedLength(s, virtualLineOffset);
  145. if (virtualLineOffset < minVirtualLineOffset) {
  146. minVirtualLineOffset = virtualLineOffset;
  147. }
  148. if ((virtualLineOffset + expandedLen) > maxVirtualLineOffset) {
  149. maxVirtualLineOffset = (virtualLineOffset + expandedLen);
  150. }
  151. }
  152. try {
  153. buffer.beginCompoundEdit();
  154. st = new StringTokenizer(verticalText, "\n");
  155. for (int line = firstLine; st.hasMoreTokens(); line++) {
  156. String s = st.nextToken();
  157. int virtualLineEndOffset = (
  158. (line >= textArea.getLineCount())
  159. ? 0
  160. : physicalToVirtualLineOffset(line, textArea.getLineLength(line))
  161. );
  162. int virtualLineOffset = (
  163. (leftFill)
  164. ? firstVirtualLineOffset
  165. : Math.min(firstVirtualLineOffset, virtualLineEndOffset)
  166. );
  167. int expandedLen = getExpandedLength(s, virtualLineOffset);
  168. String leftSpacer = "";
  169. String rightSpacer = "";
  170. int lineStartOffset;
  171. int physicalLineOffset;
  172. if (line >= textArea.getLineCount()) {
  173. buffer.insert(
  174. textArea.getLineEndOffset(textArea.getLineCount() - 1) - 1
  175. , "\n"
  176. );
  177. lineStartOffset = textArea.getLineStartOffset(
  178. textArea.getLineCount() - 1
  179. );
  180. physicalLineOffset = 0;
  181. } else {
  182. lineStartOffset = textArea.getLineStartOffset(line);
  183. Object indicator = ConversionIndicator();
  184. physicalLineOffset = virtualToPhysicalLineOffset(
  185. line, virtualLineOffset, indicator
  186. );
  187. if (indicator.type == 1) {
  188. // A tab is in the way: prepend spaces
  189. int spacesToPrepend =
  190. virtualLineOffset
  191. - physicalToVirtualLineOffset(
  192. line, physicalLineOffset - 1
  193. );
  194. buffer.insert(
  195. lineStartOffset + physicalLineOffset - 1
  196. , MiscUtilities.createWhiteSpace(spacesToPrepend, 0)
  197. );
  198. physicalLineOffset--;
  199. physicalLineOffset += spacesToPrepend;
  200. }
  201. }
  202. if (leftFill) {
  203. if (virtualLineOffset > virtualLineEndOffset) {
  204. leftSpacer = MiscUtilities.createWhiteSpace(
  205. virtualLineOffset - virtualLineEndOffset, 0
  206. );
  207. }
  208. }
  209. if (rightFill) {
  210. if (maxVirtualLineOffset > (virtualLineOffset + expandedLen)) {
  211. rightSpacer = MiscUtilities.createWhiteSpace(
  212. maxVirtualLineOffset - (virtualLineOffset + expandedLen), 0
  213. );
  214. }
  215. }
  216. buffer.insert(
  217. lineStartOffset + physicalLineOffset
  218. , leftSpacer + s + rightSpacer
  219. );
  220. }
  221. } catch (BadLocationException ble) {
  222. } finally {
  223. buffer.endCompoundEdit();
  224. }
  225. }
  226. verticalPaste();
  227. /*
  228. Macro index data (in DocBook format)
  229. <listitem>
  230. <para><filename>Vertical_Paste.bsh</filename></para>
  231. <abstract><para>
  232. Pastes the content of the clipboard vertically and fills empty
  233. areas if necessary.
  234. </para></abstract>
  235. </listitem>
  236. */