/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
- /*
- * Vertical_Paste.bsh - a BeanShell macro script for the
- * jEdit text editor - Pastes the content of the clipboard
- * vertically and fills empty areas if necessary.
- * Copyright (c) 2001 Andre Kaplan, portions by Slava Pestov
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * Checked for jEeit 4.0 API
- *
- */
- import java.util.StringTokenizer;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Segment;
- import org.gjt.sp.jedit.*;
- import org.gjt.sp.jedit.textarea.*;
- import org.gjt.sp.util.Log;
- // Fill empty areas to the left.
- boolean leftFill = true;
- // Fill empty areas to the right.
- boolean rightFill = true;
- /**
- * Adapted from org.gjt.sp.jedit.gui.StatusBar.VICaretStatus.getVirtualPosition
- */
- int physicalToVirtualLineOffset(int line, int physicalLineOffset) {
- Segment seg = new Segment();
- textArea.getLineText(line, seg);
- int physicalPosition = 0;
- int virtualPosition = 0;
- int tabSize = buffer.getTabSize();
- for (; (physicalPosition < seg.count) && (physicalPosition < physicalLineOffset); ++physicalPosition) {
- char ch = seg.array[seg.offset + physicalPosition];
- if (ch == '\t') {
- virtualPosition += tabSize
- - (virtualPosition % tabSize);
- } else {
- ++virtualPosition;
- }
- }
- return virtualPosition;
- }
- ConversionIndicator() {
- type = 0;
- return this;
- }
- /**
- * Returns the virtual to physical line offset conversion.
- * The conversion indicator is set to:
- * <ul>
- * <li>0 if the virtual line offset could be converted to a physical
- * offset</li>
- * <li>-1 if the virtual line offset is beyond the line end offset</li>
- * <li>1 if the virtual could not be exactly converted to a physical
- * offset due to the expansion of a tab</li>
- * </ul>
- */
- int virtualToPhysicalLineOffset(int line, int virtualLineOffset, Object indicator) {
- Segment seg = new Segment();
- textArea.getLineText(line, seg);
- int physicalPosition = 0;
- int virtualPosition = 0;
- int tabSize = buffer.getTabSize();
- for (; (physicalPosition < seg.count) && (virtualPosition < virtualLineOffset); ++physicalPosition) {
- char ch = seg.array[seg.offset + physicalPosition];
- if (ch == '\t') {
- virtualPosition += tabSize
- - (virtualPosition % tabSize);
- } else {
- ++virtualPosition;
- }
- }
- if (virtualPosition < virtualLineOffset) {
- indicator.type = -1;
- } else if (virtualPosition == virtualLineOffset) {
- indicator.type = 0;
- } else {
- indicator.type = 1;
- }
- return physicalPosition;
- }
- int getExpandedLength(String text, int virtualLineOffset) {
- int len = text.length();
- int virtualPosition = virtualLineOffset;
- int tabSize = buffer.getTabSize();
- for (int i = 0; i < len; i++) {
- char ch = text.charAt(i);
- if (ch == '\t') {
- virtualPosition += tabSize
- - (virtualPosition % tabSize);
- } else {
- ++virtualPosition;
- }
- }
- return (virtualPosition - virtualLineOffset);
- }
- void verticalPaste() {
- /*
- * Guard for readonly files becuase Buffer.insert()
- * ignores the flag
- *
- */
- if(buffer.isReadOnly())
- {
- Macros.error(view, "This file is read only.");
- return;
- }
- String verticalText = Registers.getRegister('$').toString();
- final int firstLine = textArea.getCaretLine();
- final int firstPhysicalLineOffset = (
- textArea.getCaretPosition()
- - textArea.getLineStartOffset(firstLine)
- );
- final int firstVirtualLineOffset = physicalToVirtualLineOffset(
- firstLine, firstPhysicalLineOffset
- );
- int minVirtualLineOffset = firstVirtualLineOffset;
- int maxVirtualLineOffset = firstVirtualLineOffset;
- StringTokenizer st = new StringTokenizer(verticalText, "\n");
- int height = st.countTokens();
- for (int line = firstLine; st.hasMoreTokens(); line++) {
- String s = st.nextToken();
- int virtualLineEndOffset = (
- (line >= textArea.getLineCount())
- ? 0
- : physicalToVirtualLineOffset(line, textArea.getLineLength(line))
- );
- int virtualLineOffset = (
- (leftFill)
- ? firstVirtualLineOffset
- : Math.min(firstVirtualLineOffset, virtualLineEndOffset)
- );
- int expandedLen = getExpandedLength(s, virtualLineOffset);
- if (virtualLineOffset < minVirtualLineOffset) {
- minVirtualLineOffset = virtualLineOffset;
- }
- if ((virtualLineOffset + expandedLen) > maxVirtualLineOffset) {
- maxVirtualLineOffset = (virtualLineOffset + expandedLen);
- }
- }
- try {
- buffer.beginCompoundEdit();
- st = new StringTokenizer(verticalText, "\n");
- for (int line = firstLine; st.hasMoreTokens(); line++) {
- String s = st.nextToken();
- int virtualLineEndOffset = (
- (line >= textArea.getLineCount())
- ? 0
- : physicalToVirtualLineOffset(line, textArea.getLineLength(line))
- );
- int virtualLineOffset = (
- (leftFill)
- ? firstVirtualLineOffset
- : Math.min(firstVirtualLineOffset, virtualLineEndOffset)
- );
- int expandedLen = getExpandedLength(s, virtualLineOffset);
- String leftSpacer = "";
- String rightSpacer = "";
- int lineStartOffset;
- int physicalLineOffset;
- if (line >= textArea.getLineCount()) {
- buffer.insert(
- textArea.getLineEndOffset(textArea.getLineCount() - 1) - 1
- , "\n"
- );
- lineStartOffset = textArea.getLineStartOffset(
- textArea.getLineCount() - 1
- );
- physicalLineOffset = 0;
- } else {
- lineStartOffset = textArea.getLineStartOffset(line);
- Object indicator = ConversionIndicator();
- physicalLineOffset = virtualToPhysicalLineOffset(
- line, virtualLineOffset, indicator
- );
- if (indicator.type == 1) {
- // A tab is in the way: prepend spaces
- int spacesToPrepend =
- virtualLineOffset
- - physicalToVirtualLineOffset(
- line, physicalLineOffset - 1
- );
- buffer.insert(
- lineStartOffset + physicalLineOffset - 1
- , MiscUtilities.createWhiteSpace(spacesToPrepend, 0)
- );
- physicalLineOffset--;
- physicalLineOffset += spacesToPrepend;
- }
- }
- if (leftFill) {
- if (virtualLineOffset > virtualLineEndOffset) {
- leftSpacer = MiscUtilities.createWhiteSpace(
- virtualLineOffset - virtualLineEndOffset, 0
- );
- }
- }
- if (rightFill) {
- if (maxVirtualLineOffset > (virtualLineOffset + expandedLen)) {
- rightSpacer = MiscUtilities.createWhiteSpace(
- maxVirtualLineOffset - (virtualLineOffset + expandedLen), 0
- );
- }
- }
- buffer.insert(
- lineStartOffset + physicalLineOffset
- , leftSpacer + s + rightSpacer
- );
- }
- } catch (BadLocationException ble) {
- } finally {
- buffer.endCompoundEdit();
- }
- }
- verticalPaste();
- /*
- Macro index data (in DocBook format)
- <listitem>
- <para><filename>Vertical_Paste.bsh</filename></para>
- <abstract><para>
- Pastes the content of the clipboard vertically and fills empty
- areas if necessary.
- </para></abstract>
- </listitem>
- */