1 3 package gnu.jemacs.swt; 4 5 import gnu.jemacs.buffer.Buffer; 6 import gnu.jemacs.buffer.Marker; 7 import gnu.lists.CharSeq; 8 import gnu.lists.FString; 9 import gnu.mapping.InPort; 10 11 import java.io.Reader ; 12 import java.io.Writer ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.swt.custom.StyledTextContent; 18 19 23 public class SwtBuffer extends Buffer 24 { 25 26 private BufferContent bufferContent = null; 27 28 public SwtBuffer(String name) { 29 this(name, new BufferContent()); 30 } 31 32 public SwtBuffer(String name, BufferContent content) 33 { 34 super(name); 35 this.bufferContent = content; 36 37 pointMarker = new Marker(this, 0, true); 38 markMarker = new Marker(); 39 redrawModeline(); 40 } 41 42 45 public void redrawModeline() 46 { 47 modeline = new FString("-----JEmacs: " + getName() + " ---"); 48 } 49 50 private FString modeline = new FString(""); 51 52 public FString getModelineFormat() { 53 return modeline; 54 } 55 56 59 public int getLength() 60 { 61 return bufferContent.getCharCount(); 62 } 63 64 65 private int dot = 0; 67 68 71 public int getDot() 72 { 73 return dot; 74 } 75 76 79 public void setDot(int dot) 80 { 81 this.dot = dot; 82 } 83 84 87 public int maxDot() 88 { 89 return length(); 90 } 91 92 94 97 public void insert(String string, Object style, int ipos) 98 { 99 } 101 102 105 public void insert(char ch, int count, Object style) 106 { 107 char[] charr = new char[count]; 109 110 for (int i = 0; i < charr.length; i++) 111 { 112 charr[i] = ch; 113 } 114 115 bufferContent.replaceTextRange(getDot(), 0, new String (charr)); 116 setDot(getDot() + 1); 117 } 118 119 122 public void insert(char ch, int count) 123 { 124 insert(ch, count, null); 125 } 126 129 public void removeAll() 130 { 131 bufferContent.setText(""); 132 } 133 134 137 public void insertFile(Reader in) throws Exception 138 { 139 bufferContent.insertFile(in, getDot()); 140 } 141 142 145 public void save(Writer out) throws Exception 146 { 147 bufferContent.save(out); 148 } 149 150 151 152 155 public void removeChar(int count) 156 { 157 int start = Math.min(getDot(), getDot() + count); 158 int end = Math.max(getDot(), getDot() + count); 159 160 start = Math.max(0, Math.min(start, size())); 162 end = Math.max(0, Math.min(end, size())); 163 164 if (start != end) 165 { 166 count = Math.abs(end - start); 167 start = Math.min(start, end); 168 bufferContent.replaceTextRange(start, count, null); 169 setDot(start); 170 } 171 } 172 173 176 public int lineStartOffset(int offset) 177 { 178 return bufferContent.lineStartPos(offset); 179 } 180 181 184 public long scan(char target, int start, int end, int count, boolean allowQuit) 185 { 186 return 0; 188 } 189 190 public CharSeq getStringContent () 191 { 192 return bufferContent; 193 } 194 195 198 public InPort openReader(int start, int count) 199 { 200 return new InPort(new BufferContentReader(bufferContent, start, count)); 201 } 202 203 206 public long savePointMark() 207 { 208 return 0; 210 } 211 212 215 public void restorePointMark(long pointMark) 216 { 217 219 } 220 221 224 public void invoke(Runnable doRun) 225 { 226 228 } 229 230 233 public int size() 234 { 235 return bufferContent.getCharCount(); 236 } 237 238 241 public Object get(int index) 242 { 243 throw new UnsupportedOperationException (); 244 } 245 246 249 public int createPos(int index, boolean isAfter) 250 { 251 return 0; 253 } 254 255 public StyledTextContent getBufferContent() 256 { 257 return bufferContent; 258 } 259 260 263 public int forwardLine(int lines) 264 { 265 int currentLine = bufferContent.getLineAtOffset(getDot()); 266 int newLine = currentLine + lines; 267 if (newLine < 0) 268 { 269 setDot(bufferContent.getOffsetAtLine(0)); 270 return newLine; 271 } 272 else if (newLine > bufferContent.getLineCount()) 273 { 274 setDot(bufferContent.getOffsetAtLine(bufferContent.getLineCount())); 275 return newLine - bufferContent.getLineCount(); 276 } 277 else 278 { 279 setDot(bufferContent.getOffsetAtLine(newLine)); 280 return 0; 281 } 282 } 283 } 284 | Popular Tags |