// {{{ 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; } // }}}
/** * invokes an Action. * * @param view The view to run the script in. * @param name The name of the node item * @param content The node content to be invoked as an action. */ public static void invokeAction(View view, String name, String content) { // borrowed from jedit/gui/ActionBar.java Log.log(Log.DEBUG, XScripter.class, "Invoking action for item named = " + name); final int repeatCount = 1; // is it worthwhile to make this configurable? final View v = view; final EditAction action = (content == null ? null : jEdit.getAction(content)); if (action == null) { if (content != null) view.getStatus().setMessageAndClear(jEdit.getProperty("view.action.no-completions")); } SwingUtilities.invokeLater( new Runnable() { public void run() { v.getInputHandler().setRepeatCount(repeatCount); v.getInputHandler().invokeAction(action); } }); } // }}}
// {{{ 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; } // }}}
/** * Replaces all occurrences of the search string with the replacement string. * * @param view The view * @param dontOpenChangedFiles Whether to open changed files or to autosave them quietly * @return the number of modified files */ public static boolean replaceAll(View view, boolean dontOpenChangedFiles) { // component that will parent any dialog boxes Component comp = SearchDialog.getSearchDialog(view); if (comp == null) comp = view; if (fileset.getFileCount(view) == 0) { GUIUtilities.error(comp, "empty-fileset", null); return false; } record(view, "replaceAll(view)", true, true); view.showWaitCursor(); boolean smartCaseReplace = getSmartCaseReplace(); int fileCount = 0; int occurCount = 0; try { SearchMatcher matcher = getSearchMatcher(); if (matcher == null) return false; initReplace(); String path = fileset.getFirstFile(view); loop: while (path != null) { Buffer buffer = jEdit.openTemporary(view, null, path, false); /* this is stupid and misleading. * but 'path' is not used anywhere except * the above line, and if this is done * after the 'continue', then we will * either hang, or be forced to duplicate * it inside the buffer == null, or add * a 'finally' clause. you decide which one's * worse. */ path = fileset.getNextFile(view, path); if (buffer == null) continue loop; // Wait for buffer to finish loading if (buffer.isPerformingIO()) VFSManager.waitForRequests(); if (!buffer.isEditable()) continue loop; // Leave buffer in a consistent state if // an error occurs int retVal = 0; try { buffer.beginCompoundEdit(); retVal = _replace(view, buffer, matcher, 0, buffer.getLength(), smartCaseReplace); } finally { buffer.endCompoundEdit(); } if (retVal != 0) { fileCount++; occurCount += retVal; if (dontOpenChangedFiles) { buffer.save(null, null); } else { jEdit.commitTemporary(buffer); jEdit.getBufferSetManager().addBuffer(view, buffer); } } } } catch (Exception e) { handleError(comp, e); } finally { view.hideWaitCursor(); } /* Don't do this when playing a macro, cos it's annoying */ if (!BeanShell.isScriptRunning()) { Object[] args = {Integer.valueOf(occurCount), Integer.valueOf(fileCount)}; view.getStatus().setMessageAndClear(jEdit.getProperty("view.status.replace-all", args)); if (occurCount == 0) view.getToolkit().beep(); } return (fileCount != 0); } // }}}
/** * Replaces the current selection with the replacement string. * * @param view The view * @return True if the operation was successful, false otherwise */ public static boolean replace(View view) { // component that will parent any dialog boxes Component comp = SearchDialog.getSearchDialog(view); if (comp == null) comp = view; JEditTextArea textArea = view.getTextArea(); Buffer buffer = view.getBuffer(); if (!buffer.isEditable()) return false; boolean smartCaseReplace = getSmartCaseReplace(); Selection[] selection = textArea.getSelection(); if (selection.length == 0) { view.getToolkit().beep(); return false; } record(view, "replace(view)", true, false); // a little hack for reverse replace and find int caret = textArea.getCaretPosition(); Selection s = textArea.getSelectionAtOffset(caret); if (s != null) caret = s.getStart(); try { buffer.beginCompoundEdit(); SearchMatcher matcher = getSearchMatcher(); if (matcher == null) return false; initReplace(); int retVal = 0; for (int i = 0; i < selection.length; i++) { s = selection[i]; retVal += replaceInSelection(view, textArea, buffer, matcher, smartCaseReplace, s); } if (reverse) { // so that Replace and Find continues from // the right location textArea.moveCaretPosition(caret); } else { s = textArea.getSelectionAtOffset(textArea.getCaretPosition()); if (s != null) textArea.moveCaretPosition(s.getEnd()); } if (!BeanShell.isScriptRunning()) { Object[] args = {Integer.valueOf(retVal), Integer.valueOf(1)}; view.getStatus().setMessageAndClear(jEdit.getProperty("view.status.replace-all", args)); } if (retVal == 0) { view.getToolkit().beep(); return false; } return true; } catch (Exception e) { handleError(comp, e); } finally { buffer.endCompoundEdit(); } return false; } // }}}
/** * Finds the next occurrence of the search string. * * @param view The view * @return True if the operation was successful, false otherwise */ public static boolean find(View view) { // component that will parent any dialog boxes Component comp = SearchDialog.getSearchDialog(view); if (comp == null || !comp.isShowing()) comp = view; String path = fileset.getNextFile(view, null); if (path == null) { GUIUtilities.error(comp, "empty-fileset", null); return false; } try { view.showWaitCursor(); SearchMatcher matcher = getSearchMatcher(); if (matcher == null) { view.getToolkit().beep(); return false; } record(view, "find(view)", false, true); boolean repeat = false; loop: for (; ; ) { while (path != null) { Buffer buffer = jEdit.openTemporary(view, null, path, false); /* this is stupid and misleading. * but 'path' is not used anywhere except * the above line, and if this is done * after the 'continue', then we will * either hang, or be forced to duplicate * it inside the buffer == null, or add * a 'finally' clause. you decide which one's * worse. */ if (reverse) { path = fileset.getPrevFile(view, path); } else { path = fileset.getNextFile(view, path); } if (buffer == null) continue loop; // Wait for the buffer to load if (!buffer.isLoaded()) VFSManager.waitForRequests(); int start; if (view.getBuffer() == buffer && !repeat) { JEditTextArea textArea = view.getTextArea(); Selection s = textArea.getSelectionAtOffset(textArea.getCaretPosition()); if (s == null) start = textArea.getCaretPosition(); else if (reverse) start = s.getStart(); else start = s.getEnd(); } else if (reverse) start = buffer.getLength(); else start = 0; if (find(view, buffer, start, repeat, reverse)) return true; } if (repeat) { if (!BeanShell.isScriptRunning()) { view.getStatus().setMessageAndClear(jEdit.getProperty("view.status.search-not-found")); view.getToolkit().beep(); } return false; } boolean restart; // if auto wrap is on, always restart search. // if auto wrap is off, and we're called from // a macro, stop search. If we're called // interactively, ask the user what to do. if (wrap) { if (!BeanShell.isScriptRunning()) { view.getStatus().setMessageAndClear(jEdit.getProperty("view.status.auto-wrap")); // beep if beep property set if (jEdit.getBooleanProperty("search.beepOnSearchAutoWrap")) { view.getToolkit().beep(); } } restart = true; } else if (BeanShell.isScriptRunning()) { restart = false; } else { Integer[] args = {Integer.valueOf(reverse ? 1 : 0)}; int result = GUIUtilities.confirm( comp, "keepsearching", args, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); restart = (result == JOptionPane.YES_OPTION); } if (restart) { // start search from beginning path = fileset.getFirstFile(view); repeat = true; } else break loop; } } catch (Exception e) { handleError(comp, e); } finally { view.hideWaitCursor(); } return false; } // }}}
public static void setStatusMessage(String msg) { View view = jEdit.getActiveView(); if (view != null) view.getStatus().setMessageAndClear(msg); else longToast(msg); }