📄 macros.java
字号:
GUIUtilities.error(view,"already-recording",new String[0]); return; } Buffer buffer = jEdit.openFile(null,settings + File.separator + "macros","Temporary_Macro.bsh",true,null); if(buffer == null) return; buffer.remove(0,buffer.getLength()); buffer.insert(0,jEdit.getProperty("macro.temp.header")); recordMacro(view,buffer,true); } //}}} //{{{ recordMacro() method /** * Starts recording a macro. * @param view The view * @since jEdit 2.7pre2 */ public static void recordMacro(View view) { String settings = jEdit.getSettingsDirectory(); if(settings == null) { GUIUtilities.error(view,"no-settings",new String[0]); return; } if(view.getMacroRecorder() != null) { GUIUtilities.error(view,"already-recording",new String[0]); return; } String name = GUIUtilities.input(view,"record",null); if(name == null) return; name = name.replace(' ','_'); Buffer buffer = jEdit.openFile(null,null, MiscUtilities.constructPath(settings,"macros", name + ".bsh"),true,null); if(buffer == null) return; buffer.remove(0,buffer.getLength()); buffer.insert(0,jEdit.getProperty("macro.header")); recordMacro(view,buffer,false); } //}}} //{{{ stopRecording() method /** * Stops a recording currently in progress. * @param view The view * @since jEdit 2.7pre2 */ public static void stopRecording(View view) { Recorder recorder = view.getMacroRecorder(); if(recorder == null) GUIUtilities.error(view,"macro-not-recording",null); else { view.setMacroRecorder(null); if(!recorder.temporary) view.setBuffer(recorder.buffer); recorder.dispose(); } } //}}} //{{{ runTemporaryMacro() method /** * Runs the temporary macro. * @param view The view * @since jEdit 2.7pre2 */ public static void runTemporaryMacro(View view) { String settings = jEdit.getSettingsDirectory(); if(settings == null) { GUIUtilities.error(view,"no-settings",new String[0]); return; } String path = MiscUtilities.constructPath( jEdit.getSettingsDirectory(),"macros", "Temporary_Macro.bsh"); Handler handler = getHandler("beanshell"); Macro temp = handler.createMacro(path,path); Buffer buffer = view.getBuffer(); try { buffer.beginCompoundEdit(); temp.invoke(view); } finally { /* I already wrote a comment expaining this in * Macro.invoke(). */ if(buffer.insideCompoundEdit()) buffer.endCompoundEdit(); } } //}}} //{{{ runLastMacro() method /** * Runs the most recently run or recorded macro. * @param view The view * @since jEdit 2.7pre2 */ public static void runLastMacro(View view) { if(lastMacro == null) view.getToolkit().beep(); else lastMacro.invoke(view); } //}}} //{{{ Private members //{{{ Static variables private static String systemMacroPath; private static String userMacroPath; private static ArrayList macroHandlers; private static ActionSet macroActionSet; private static Vector macroHierarchy; private static Hashtable macroHash; private static Macro lastMacro; //}}} //{{{ Class initializer static { macroHandlers = new ArrayList(); registerHandler(new BeanShellHandler()); macroActionSet = new ActionSet(jEdit.getProperty("action-set.macros")); jEdit.addActionSet(macroActionSet); macroHierarchy = new Vector(); macroHash = new Hashtable(); } //}}} //{{{ loadMacros() method private static void loadMacros(Vector vector, String path, File directory) { File[] macroFiles = directory.listFiles(); if(macroFiles == null || macroFiles.length == 0) return; for(int i = 0; i < macroFiles.length; i++) { File file = macroFiles[i]; String fileName = file.getName(); if(file.isHidden()) { /* do nothing! */ continue; } else if(file.isDirectory()) { String submenuName = fileName.replace('_',' '); Vector submenu = null; //{{{ try to merge with an existing menu first for(int j = 0; j < vector.size(); j++) { Object obj = vector.get(j); if(obj instanceof Vector) { Vector vec = (Vector)obj; if(((String)vec.get(0)).equals(submenuName)) { submenu = vec; break; } } } //}}} if(submenu == null) { submenu = new Vector(); submenu.addElement(submenuName); vector.addElement(submenu); } loadMacros(submenu,path + fileName + '/',file); } else { Handler handler = getHandlerForPathName(file.getPath()); if(handler == null) continue; try { Macro newMacro = handler.createMacro( path + fileName, file.getPath()); vector.addElement(newMacro); macroActionSet.addAction(newMacro); macroHash.put(newMacro.getName(),newMacro); } catch (Exception e) { Log.log(Log.ERROR, Macros.class, e); macroHandlers.remove(handler); } } } } //}}} //{{{ recordMacro() method /** * Starts recording a macro. * @param view The view * @param buffer The buffer to record to * @param temporary True if this is a temporary macro * @since jEdit 3.0pre5 */ private static void recordMacro(View view, Buffer buffer, boolean temporary) { Handler handler = getHandler("beanshell"); String path = buffer.getPath(); lastMacro = handler.createMacro(path,path); view.setMacroRecorder(new Recorder(view,buffer,temporary)); // setting the message to 'null' causes the status bar to check // if a recording is in progress view.getStatus().setMessage(null); } //}}} //}}} //{{{ Recorder class /** * Handles macro recording. */ public static class Recorder implements EBComponent { View view; Buffer buffer; boolean temporary; boolean lastWasInput; //{{{ Recorder constructor public Recorder(View view, Buffer buffer, boolean temporary) { this.view = view; this.buffer = buffer; this.temporary = temporary; EditBus.addToBus(this); } //}}} //{{{ record() method public void record(String code) { if(lastWasInput) { lastWasInput = false; append("\");"); } append("\n"); append(code); } //}}} //{{{ record() method public void record(int repeat, String code) { if(repeat == 1) record(code); else { record("for(int i = 1; i <= " + repeat + "; i++)\n" + "{\n" + code + "\n" + "}"); } } //}}} //{{{ record() method public void record(int repeat, char ch) { // record \n and \t on lines specially so that auto indent // can take place if(ch == '\n') record(repeat,"textArea.userInput(\'\\n\');"); else if(ch == '\t') record(repeat,"textArea.userInput(\'\\t\');"); else { StringBuffer buf = new StringBuffer(); for(int i = 0; i < repeat; i++) buf.append(ch); String charStr = MiscUtilities.charsToEscapes(buf.toString()); if(lastWasInput) append(charStr); else { append("\ntextArea.setSelectedText(\"" + charStr); lastWasInput = true; } } } //}}} //{{{ handleMessage() method public void handleMessage(EBMessage msg) { if(msg instanceof BufferUpdate) { BufferUpdate bmsg = (BufferUpdate)msg; if(bmsg.getWhat() == BufferUpdate.CLOSED) { if(bmsg.getBuffer() == buffer) stopRecording(view); } } } //}}} //{{{ append() method private void append(String str) { buffer.insert(buffer.getLength(),str); } //}}} //{{{ dispose() method private void dispose() { if(lastWasInput) { lastWasInput = false; append("\");"); } for(int i = 0; i < buffer.getLineCount(); i++) { buffer.indentLine(i,true,true); } EditBus.removeFromBus(this); // setting the message to 'null' causes the status bar to // check if a recording is in progress view.getStatus().setMessage(null); } //}}} } //}}} //{{{ Handler class /** * Encapsulates creating and invoking macros in arbitrary scripting languages * @since jEdit 4.0pre6 */ public static abstract class Handler { //{{{ getName() method public String getName() { return name; } //}}} //{{{ getLabel() method public String getLabel() { return label; } //}}} //{{{ accept() method public boolean accept(String path) { return filter.isMatch(MiscUtilities.getFileName(path)); } //}}} //{{{ createMacro() method public abstract Macro createMacro(String macroName, String path); //}}} //{{{ runMacro() method /** * Runs the specified macro. * @param view The view - may be null. * @param macro The macro. */ public abstract void runMacro(View view, Macro macro); //}}} //{{{ runMacro() method /** * Runs the specified macro. This method is optional; it is * called if the specified macro is a startup script. The * default behavior is to simply call {@link #runMacro(View,Macros.Macro)}. * * @param view The view - may be null. * @param macro The macro. * @param ownNamespace A hint indicating whenever functions and * variables defined in the script are to be self-contained, or * made available to other scripts. The macro handler may ignore * this parameter. * @since jEdit 4.1pre3 */ public void runMacro(View view, Macro macro, boolean ownNamespace) { runMacro(view,macro); } //}}} //{{{ Handler constructor protected Handler(String name) { this.name = name; label = jEdit.getProperty("macro-handler." + name + ".label", name); try { filter = new RE(MiscUtilities.globToRE( jEdit.getProperty( "macro-handler." + name + ".glob"))); } catch (Exception e) { throw new InternalError("Missing or invalid glob for handler " + name); } } //}}} //{{{ Private members private String name; private String label; private RE filter; //}}} } //}}} //{{{ BeanShellHandler class static class BeanShellHandler extends Handler { //{{{ BeanShellHandler constructor BeanShellHandler() { super("beanshell"); } //}}} //{{{ createMacro() method public Macro createMacro(String macroName, String path) { // Remove '.bsh' macroName = macroName.substring(0, macroName.length() - 4); return new Macro(this, macroName, Macro.macroNameToLabel(macroName), path); } //}}} //{{{ runMacro() method public void runMacro(View view, Macro macro) { BeanShell.runScript(view,macro.getPath(),null,true); } //}}} //{{{ runMacro() method public void runMacro(View view, Macro macro, boolean ownNamespace) { BeanShell.runScript(view,macro.getPath(),null,ownNamespace); } //}}} } //}}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -