/** Replace one occurrence of the search string with the replacement string. */
  private static int replaceOne(
      View view,
      JEditBuffer buffer,
      SearchMatcher.Match occur,
      int offset,
      CharSequence found,
      boolean smartCaseReplace)
      throws Exception {
    String subst = replaceOne(view, buffer, occur, found);
    if (smartCaseReplace && ignoreCase) {
      int strCase = TextUtilities.getStringCase(found);
      if (strCase == TextUtilities.LOWER_CASE) subst = subst.toLowerCase();
      else if (strCase == TextUtilities.UPPER_CASE) subst = subst.toUpperCase();
      else if (strCase == TextUtilities.TITLE_CASE) subst = TextUtilities.toTitleCase(subst);
    }

    if (subst != null) {
      int start = offset + occur.start;
      int end = offset + occur.end;

      if (end - start > 0) buffer.remove(start, end - start);
      buffer.insert(start, subst);
      return subst.length();
    } else return -1;
  } // }}}
  public void apply(
      JEditBuffer buffer,
      int thisLineIndex,
      int prevLineIndex,
      int prevPrevLineIndex,
      List<IndentAction> indentActions) {
    int index;
    if (aligned) index = thisLineIndex;
    else index = prevLineIndex;

    if (index == -1) return;

    String line = buffer.getLineText(index);

    int offset = line.lastIndexOf(closeBracket);
    if (offset == -1) return;

    int closeCount = getBrackets(line).closeCount;
    if (closeCount != 0) {
      IndentAction.AlignBracket alignBracket = new IndentAction.AlignBracket(buffer, index, offset);

      String openLine = alignBracket.getOpenBracketLine();
      int column = alignBracket.getOpenBracketColumn();
      if (openLine != null) {
        String leadingBrackets = openLine.substring(0, column);
        alignBracket.setExtraIndent(getBrackets(leadingBrackets).openCount);
      }

      indentActions.add(alignBracket);
    }
  }
Beispiel #3
0
  // {{{ userInput() method
  protected void userInput(char ch) {
    lastActionCount = 0;

    JEditTextArea textArea = view.getTextArea();

    /* Buffer buffer = view.getBuffer();
    if(!buffer.insideCompoundEdit())
    	buffer.beginCompoundEdit(); */

    if (repeatCount == 1) textArea.userInput(ch);
    else {
      // stop people doing dumb stuff like C+ENTER 100 C+n
      if (repeatCount > REPEAT_COUNT_THRESHOLD) {
        Object[] pp = {String.valueOf(ch), repeatCount};

        if (GUIUtilities.confirm(
                view,
                "large-repeat-count.user-input",
                pp,
                JOptionPane.WARNING_MESSAGE,
                JOptionPane.YES_NO_OPTION)
            != JOptionPane.YES_OPTION) {
          repeatCount = 1;
          view.getStatus().setMessage(null);
          return;
        }
      }

      JEditBuffer buffer = view.getBuffer();
      try {
        if (repeatCount != 1) buffer.beginCompoundEdit();
        for (int i = 0; i < repeatCount; i++) textArea.userInput(ch);
      } finally {
        if (repeatCount != 1) buffer.endCompoundEdit();
      }
    }

    Macros.Recorder recorder = view.getMacroRecorder();

    if (recorder != null) {
      recorder.recordInput(repeatCount, ch, textArea.isOverwriteEnabled());
    }

    repeatCount = 1;
  } // }}}
Beispiel #4
0
  // {{{ invokeReadNextChar() method
  protected void invokeReadNextChar(char ch) {
    JEditBuffer buffer = view.getBuffer();

    /* if(buffer.insideCompoundEdit())
    buffer.endCompoundEdit(); */

    String charStr = StandardUtilities.charsToEscapes(String.valueOf(ch));

    // this might be a bit slow if __char__ occurs a lot
    int index;
    while ((index = readNextChar.indexOf("__char__")) != -1) {
      readNextChar =
          readNextChar.substring(0, index)
              + '\''
              + charStr
              + '\''
              + readNextChar.substring(index + 8);
    }

    Macros.Recorder recorder = view.getMacroRecorder();
    if (recorder != null) recorder.record(getRepeatCount(), readNextChar);

    view.getStatus().setMessage(null);

    if (getRepeatCount() != 1) {
      try {
        buffer.beginCompoundEdit();

        BeanShell.eval(
            view,
            BeanShell.getNameSpace(),
            "for(int i = 1; i < " + getRepeatCount() + "; i++)\n{\n" + readNextChar + "\n}");
      } finally {
        buffer.endCompoundEdit();
      }
    } else BeanShell.eval(view, BeanShell.getNameSpace(), readNextChar);

    readNextChar = null;
  } // }}}
  // {{{ print() method
  public static void print(final ViewSubstitute view, final JEditBuffer buffer, boolean selection) {
    job = getPrintJob(buffer.getPath());

    boolean header = jEdit.getBooleanProperty("print.header"); // $NON-NLS-1$
    boolean footer = jEdit.getBooleanProperty("print.footer"); // $NON-NLS-1$
    boolean lineNumbers = jEdit.getBooleanProperty("print.lineNumbers"); // $NON-NLS-1$
    boolean color = jEdit.getBooleanProperty("print.color"); // $NON-NLS-1$

    Font font = jEdit.getFontProperty("print.font"); // $NON-NLS-1$

    ModifiedBufferPrintable printable =
        new ModifiedBufferPrintable(
            job, format, view, buffer, font, header, footer, lineNumbers, color);
    job.setPrintable(printable);

    if (!job.printDialog(format)) {
      return;
    }

    savePrintSpec();

    printable.print();
  } // }}}
  /**
   * Replaces all occurrences of the search string with the replacement string.
   *
   * @param view The view
   * @param buffer The buffer
   * @param start The start offset
   * @param end The end offset
   * @param matcher The search matcher to use
   * @param smartCaseReplace See user's guide
   * @return The number of occurrences replaced
   */
  private static int _replace(
      View view,
      JEditBuffer buffer,
      SearchMatcher matcher,
      int start,
      int end,
      boolean smartCaseReplace)
      throws Exception {
    String noWordSep = buffer.getStringProperty("noWordSep");
    matcher.setNoWordSep(noWordSep);
    int occurCount = 0;

    boolean endOfLine = (buffer.getLineEndOffset(buffer.getLineOfOffset(end)) - 1 == end);

    int offset = start;
    loop:
    for (int counter = 0; ; counter++) {
      boolean startOfLine = (buffer.getLineStartOffset(buffer.getLineOfOffset(offset)) == offset);

      CharSequence text = buffer.getSegment(offset, end - offset);
      SearchMatcher.Match occur =
          matcher.nextMatch(text, startOfLine, endOfLine, counter == 0, false);
      if (occur == null) break loop;

      CharSequence found = text.subSequence(occur.start, occur.end);

      int length = replaceOne(view, buffer, occur, offset, found, smartCaseReplace);
      if (length == -1) offset += occur.end;
      else {
        offset += occur.start + length;
        end += (length - found.length());
        occurCount++;
      }
    }

    return occurCount;
  } // }}}
 public int calculateIndent(JEditBuffer buffer, int line, int oldIndent, int newIndent) {
   return newIndent - buffer.getIndentSize();
 }
 public int calculateIndent(JEditBuffer buffer, int line, int oldIndent, int newIndent) {
   int current =
       StandardUtilities.getLeadingWhiteSpaceWidth(
           buffer.getLineSegment(line), buffer.getTabSize());
   return (current < newIndent) ? current : newIndent;
 }
Beispiel #9
0
  /**
   * Invokes the specified action, repeating and recording it as necessary.
   *
   * @param action The action
   */
  @Override
  public void invokeAction(EditAction action) {
    JEditBuffer buffer = view.getBuffer();

    /* if(buffer.insideCompoundEdit())
    buffer.endCompoundEdit(); */

    // remember the last executed action
    if (!action.noRememberLast()) {
      HistoryModel.getModel("action").addItem(action.getName());
      if (lastAction == action) lastActionCount++;
      else {
        lastAction = action;
        lastActionCount = 1;
      }
    }

    // remember old values, in case action changes them
    int _repeatCount = repeatCount;

    // execute the action
    if (action.noRepeat() || _repeatCount == 1) action.invoke(view);
    else {
      // stop people doing dumb stuff like C+ENTER 100 C+n
      if (_repeatCount > REPEAT_COUNT_THRESHOLD) {
        String label = action.getLabel();
        if (label == null) label = action.getName();
        else label = GUIUtilities.prettifyMenuLabel(label);

        Object[] pp = {label, _repeatCount};

        if (GUIUtilities.confirm(
                view,
                "large-repeat-count",
                pp,
                JOptionPane.WARNING_MESSAGE,
                JOptionPane.YES_NO_OPTION)
            != JOptionPane.YES_OPTION) {
          repeatCount = 1;
          view.getStatus().setMessage(null);
          return;
        }
      }

      try {
        buffer.beginCompoundEdit();

        for (int i = 0; i < _repeatCount; i++) action.invoke(view);
      } finally {
        buffer.endCompoundEdit();
      }
    }

    Macros.Recorder recorder = view.getMacroRecorder();

    if (recorder != null && !action.noRecord()) recorder.record(_repeatCount, action.getCode());

    // If repeat was true originally, clear it
    // Otherwise it might have been set by the action, etc
    if (_repeatCount != 1) {
      // first of all, if this action set a
      // readNextChar, do not clear the repeat
      if (readNextChar != null) return;

      repeatCount = 1;
      view.getStatus().setMessage(null);
    }
  } // }}}