1 package gnu.jemacs.buffer; 2 import gnu.lists.*; 3 import gnu.mapping.*; 4 5 public final class Marker extends SeqPosition 6 { 7 Buffer buffer; 8 9 10 public final boolean isPoint() { return buffer != null && sequence == null; } 11 12 public Marker() 13 { 14 } 15 16 public Marker(Marker marker) 17 { 18 buffer = marker.buffer; 19 if (buffer != null) 20 { 21 if (marker.isPoint()) 22 set(buffer, buffer.getDot(), true); 23 else 24 set(marker); 25 } 26 } 27 28 public Marker (Buffer buffer, int offset, boolean isAfter) 29 { 30 super(buffer, offset, isAfter); 31 this.buffer = buffer; 32 } 33 34 public int getOffset() 35 { 36 if (buffer == null) 37 return -1; 38 else if (isPoint()) 39 return buffer.getDot(); 40 return nextIndex(); 41 } 42 43 public int getPoint() 44 { 45 return 1 + getOffset(); 46 } 47 48 public Buffer getBuffer() 49 { 50 return buffer; 51 } 52 53 public void setDot(int newPosition) 54 { 55 set(buffer, newPosition); 56 } 57 58 public void set(Buffer newBuffer, int newPosition) 59 { 60 if (isPoint()) 61 { 62 if (newBuffer != buffer) 63 { 64 String msg; 65 if (newBuffer == null) 66 msg = "Can't make point-marker point nowhere: "; 67 else 68 msg = "Can't change buffer of point-marker: "; 69 throw new Error (msg+this); 70 } 71 buffer.setDot(newPosition); 72 } 73 else 74 { 75 if (sequence != null) 76 release(); 77 sequence = null; 78 if (newBuffer == null) 79 { 80 buffer = null; 81 return; 82 } 83 84 if (newPosition < 0) 85 newPosition = 0; 86 else 87 { 88 int newLength = newBuffer.length(); 89 if (newPosition > newLength) 90 newPosition = newLength; 91 } 92 set(newBuffer, newPosition, false); 93 } 94 } 95 96 public void removeChar(int count) 97 { 98 if (isPoint()) 99 buffer.removeChar(count); 100 else 101 buffer.removePos(ipos, count); 102 } 103 104 public void insert (char[] data, int off, int len, Object style) 105 { 106 int point = getOffset(); 107 buffer.insert(data, off, len, style, ipos); 108 point += len; 109 setDot(point); 110 } 111 112 public void insert (String string, Object style) 113 { 114 int point = getOffset(); 115 if (isPoint()) 116 buffer.insert(string, style); 117 else 118 buffer.insert(string, style, ipos); 119 point += string.length(); 120 setDot(point); 121 } 122 123 124 public void insert (char ch, int count, Object style) 125 { 126 if (count < 0) 127 return; 128 int n = count > 500 ? 500 : count; 129 StringBuffer sbuf = new StringBuffer (n); 130 for (int i = n; --i >= 0; ) 131 sbuf.append(ch); 132 String str = sbuf.toString(); 133 for (;;) 134 { 135 insert(str, style); 136 count -= n; 137 if (count == 0) 138 break; 139 if (count < 500) 140 { 141 n = count; 142 sbuf.setLength(n); 143 str = sbuf.toString(); 144 } 145 } 146 } 147 148 public void forwardChar(int i) 149 { 150 int point = getOffset(); 151 int max = buffer.maxDot(); 152 if (point + i > max) 153 { 154 point = max; 155 Signal.signal("End of buffer"); 156 } 157 point += i; 158 setDot(point); 159 } 160 161 public void backwardChar(int i) 162 { 163 int point = getOffset(); 164 if (point < i) 165 { 166 point = 0; 167 Signal.signal("Beginning of buffer"); 168 } 169 point -= i; 170 setDot(point); 171 } 172 173 public int currentColumn() 174 { 175 return buffer.currentColumn(getOffset()); 176 } 177 178 public int moveToColumn(int column, boolean force) 180 { 181 int lineStart = buffer.lineStartOffset(getOffset()); 182 InPort port = buffer.openReader(lineStart, buffer.maxDot() - lineStart); 183 int resultColumn = 0; 184 try 185 { 186 int offset = lineStart; 187 for (;;) 188 { 189 int ch = port.read(); 190 if (ch < 0 || ch == '\n') 191 { 192 if (force) 193 { 194 } 196 break; 197 } 198 int width = buffer.charWidth((char) ch, resultColumn); 199 offset++; 200 resultColumn += width; 201 if (resultColumn >= column) 202 { 203 if (resultColumn > column && force) 204 { 205 } 207 break; 208 } 209 } 210 setDot(offset); 211 return resultColumn; 212 } 213 catch (java.io.IOException ex) 214 { 215 throw new WrappedException(ex); 216 } 217 } 218 219 public int forwardLine(int lines) 220 { 221 long value = buffer.forwardLine(lines, getOffset()); 222 setDot((int) value); 223 return (int) (value >> 32); 224 } 225 226 229 230 283 284 public boolean isBeginningOfLine() 285 { 286 int offset = getOffset(); 287 return offset == 0 || buffer.charAt(offset - 1) == '\n'; 288 } 289 290 public boolean isEndOfLine() 291 { 292 int offset = getOffset(); 293 return offset == buffer.length() || buffer.charAt(offset) == '\n'; 294 } 295 296 public int hashCode() 297 { 298 if (buffer == null) 299 return 0; 300 return buffer.hashCode() ^ getOffset(); 301 } 302 303 public boolean equals (Object other) 304 { 305 if (! (other instanceof Marker)) 306 return false; 307 Marker m2 = (Marker) other; 308 return buffer == m2.buffer && getOffset() == m2.getOffset(); 309 } 310 311 public String toString() 312 { 313 if (buffer == null) 314 return "#<marker in no buffer>"; 315 StringBuffer sbuf = new StringBuffer (80); 316 sbuf.append("#<marker at "); 317 sbuf.append(getPoint()); 318 sbuf.append(" in "); 319 sbuf.append(buffer.getName()); 320 sbuf.append('>'); 321 return sbuf.toString(); 322 } 323 } 324 | Popular Tags |