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

/plugins/XInsert/tags/release-2_5/src/InsertTextCommand.java

#
Java | 152 lines | 103 code | 12 blank | 37 comment | 23 complexity | f042ee8e468bb32372417544f7cbbeb7 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. * InsertTextCommand.java
  3. * Copyright (C) 2001 Dominic Stolerman
  4. * dstolerman@jedit.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /**
  21. *@author Dominic Stolerman
  22. */
  23. import org.gjt.sp.jedit.*;
  24. import org.gjt.sp.jedit.textarea.JEditTextArea;
  25. import org.gjt.sp.util.Log;
  26. public class InsertTextCommand extends java.lang.Object implements Command {
  27. public static void insertText(String text, ScriptContext context) {
  28. InsertTextCommand cmd = new InsertTextCommand(text);
  29. cmd.run(context);
  30. }
  31. /* constructor */
  32. public InsertTextCommand(String text) {
  33. this.text = text;
  34. // this.src = MiscUtilities.escapesToChars(text);
  35. this.src = text;
  36. this.result = new StringBuffer(src.length());
  37. this.i = 0;
  38. }
  39. public void run(ScriptContext context) {
  40. XTreeNode node = context.getNode();
  41. View view = context.getView();
  42. JEditTextArea textarea = view.getTextArea();
  43. Buffer buffer = view.getBuffer();
  44. int tabSize = buffer.getTabSize();
  45. char c;
  46. boolean braced;
  47. int caretpos = -1;
  48. for (i = 0; i < src.length(); i++) {
  49. c = src.charAt(i);
  50. switch(c) {
  51. case '|':
  52. // double '|'
  53. if(nextCharIs('|')) {
  54. i++;
  55. result.append(c);
  56. }
  57. // insert selection
  58. else {
  59. result.append(context.getSelection());
  60. caretpos = result.length();
  61. }
  62. // pos = j;
  63. break;
  64. case '\\':
  65. // new line
  66. if(nextCharIs('n')) {
  67. i++;
  68. result.append('\n');
  69. }
  70. // tab
  71. else if(nextCharIs('t')) {
  72. i++;
  73. if(jEdit.getBooleanProperty("buffer.noTabs", false))
  74. for(int k = 0; k < tabSize; k++)
  75. result.append(" ");
  76. else
  77. result.append("\t");
  78. }
  79. // escaped dollar sign
  80. else if(nextCharIs('$')) {
  81. i++;
  82. result.append('$');
  83. }
  84. // escaped pipe
  85. else if(nextCharIs('|')) {
  86. i++;
  87. result.append('|');
  88. }
  89. // escaped backslash
  90. else if(nextCharIs('\\')) {
  91. i++;
  92. result.append(c);
  93. }
  94. else
  95. result.append(c);
  96. break;
  97. // insert variable
  98. case '$':
  99. braced = nextCharIs('{');
  100. // insertText ${varname}
  101. if(braced) i++;
  102. i++;
  103. int temp = i;
  104. while(i < src.length() && Character.isLetterOrDigit(src.charAt(i)))
  105. i++;
  106. // Log.log(Log.DEBUG, this, "$ = " + src.substring(temp, i));
  107. String val = XScripter.getSubstituteFor(view, src.substring(temp, i), node);
  108. if(val == null)
  109. result.append(src.substring(temp, i));
  110. else
  111. result.append(val);
  112. if(!braced) i--;
  113. break;
  114. default:
  115. result.append(c);
  116. break;
  117. }
  118. }
  119. if(jEdit.getBooleanProperty("xtree.carriage", false))
  120. result.append('\n');
  121. int caret = textarea.getCaretPosition();
  122. int len = result.length();
  123. if(len > 0) {
  124. textarea.setSelectedText(result.toString());
  125. // set caret to end of insert or last "|" position
  126. textarea.setCaretPosition(caretpos > -1
  127. ? caret + caretpos
  128. : caret + len
  129. );
  130. }
  131. }
  132. // enhances source readability
  133. private boolean nextCharIs(char ch) {
  134. return (i < src.length() - 1) && (src.charAt(i + 1) == ch);
  135. }
  136. private int i;
  137. private String text;
  138. private String src;
  139. private StringBuffer result;
  140. }