1 21 22 package org.armedbear.j; 23 24 import java.util.ArrayList ; 25 import javax.swing.undo.CompoundEdit ; 26 import org.armedbear.lisp.Lisp; 27 import org.armedbear.lisp.LispObject; 28 import org.armedbear.lisp.LispThread; 29 30 public final class Macro implements Constants 31 { 32 private static Macro macro; 33 34 public static synchronized void recordMacro() 35 { 36 if (Editor.isRecordingMacro()) 37 Editor.setRecordingMacro(false); 38 else { 39 final Editor editor = Editor.currentEditor(); 40 if (macro != null && !macro.isEmpty()) { 41 if (!editor.confirm("Record Macro", 42 "Overwrite existing keyboard macro?")) 43 return; 44 } 45 macro = new Macro(editor); 46 Editor.setRecordingMacro(true); 47 } 48 } 49 50 public static synchronized void playbackMacro() 51 { 52 final Editor editor = Editor.currentEditor(); 53 if (Editor.isRecordingMacro()) { 54 MessageDialog.showMessageDialog(editor, 55 "Command ignored (playbackMacro is not allowed while recording a macro)", 56 "Record Macro"); 57 return; 58 } 59 if (macro == null || macro.isEmpty()){ 60 editor.status("No keyboard macro defined"); 61 return; 62 } 63 macro.playback(); 64 } 65 66 private final Editor editor; 67 private ArrayList list = new ArrayList (); 68 69 private Macro(Editor editor) 70 { 71 this.editor = editor; 72 } 73 74 public Editor getEditor() 75 { 76 return editor; 77 } 78 79 private synchronized boolean isEmpty() 80 { 81 return list.isEmpty(); 82 } 83 84 public static synchronized void record(Editor editor, Object command) 85 { 86 if (macro != null && macro.getEditor() == editor) 87 macro.record(command); 88 } 89 90 public static synchronized void record(Editor editor, char c) 91 { 92 if (macro != null && macro.getEditor() == editor) 93 macro.record(c); 94 } 95 96 private synchronized void record(Object command) 97 { 98 list.add(command); 99 } 100 101 private synchronized void record(char c) 102 { 103 list.add(new Character (c)); 104 } 105 106 private synchronized void playback() 107 { 108 final Editor editor = Editor.currentEditor(); 109 final Buffer buffer = editor.getBuffer(); 110 try { 111 buffer.lockWrite(); 112 } 113 catch (InterruptedException e) { 114 Log.debug(e); 115 return; 116 } 117 try { 118 CompoundEdit compoundEdit = buffer.beginCompoundEdit(); 119 final int size = list.size(); 120 for (int i = 0; i < size; i++) { 121 editor.setCurrentCommand(COMMAND_NOTHING); 122 Object object = list.get(i); 123 if (object instanceof String ) { 124 editor.executeCommand((String )object); 125 } else if (object instanceof LispObject) { 126 try { 127 Lisp.funcall0((LispObject)object, 128 LispThread.currentThread()); 129 } 130 catch (Throwable t) { 131 Log.error(t); 132 } 133 } else if (object instanceof Character ) { 134 editor.insertNormalChar(((Character )object).charValue()); 135 } 136 editor.setLastCommand(editor.getCurrentCommand()); 137 } 138 buffer.endCompoundEdit(compoundEdit); 139 } 140 finally { 141 buffer.unlockWrite(); 142 } 143 } 144 } 145 | Popular Tags |