KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > buffer > Marker


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   /** Is this the special point marker? */
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 JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc string, Object JavaDoc 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   /** Insert count copies of ch at the current position. */
124   public void insert (char ch, int count, Object JavaDoc style)
125   {
126     if (count < 0)
127       return;
128     int n = count > 500 ? 500 : count;
129     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(n);
130     for (int i = n; --i >= 0; )
131       sbuf.append(ch);
132     String JavaDoc 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   // force is currently ignored FIXME
179
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             // FIXME
195
}
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             // FIXME
206
}
207         break;
208           }
209       }
210     setDot(offset);
211     return resultColumn;
212       }
213     catch (java.io.IOException JavaDoc 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   /** Move to start of frame line COUNTs down.
227    * Assume window width is WIDTH.
228    * If LINES is negative, this is moving up. */

229
230   /*
231   public int verticalMotion(int count, int width)
232   {
233     if (count == 0)
234       {
235     moveToColumn ((currentColumn() / width) * width, false);
236     return 0;
237       }
238     if (count > 0)
239       {
240     int todo = count + currentColumn() / width;
241     endOfLine();
242     // The loop iterates over buffer lines;
243     // H is the number of screen lines in the current line, i.e.
244     // the ceiling of dividing the buffer line width by width.
245     for (;;)
246       {
247         int h = (currentColumn() + width - 1) / width;
248         if (h <= 0) h = 1;
249         if (h > todo)
250           break;
251         if (eobp())
252           break;
253         todo -= h;
254         forwardChar(1); // move past '\n'.
255         endOfLine(); // and on to the end of the next line.
256       }
257     if (todo >= h && todo > 0)
258       return count - todo + h - 1; // Hit end of buffer.
259       }
260     else // count > 0 -- Similar algorithm, but for upward motion.
261       {
262     int todo = - count;
263     for (;;)
264       {
265         int h = (currentColumn() + width - 1) / width;
266         if (h <= 0) h = 1;
267         if (h > todo)
268           break;
269         beginningOfLine();
270         if (bobp())
271           break;
272         todo -= h;
273         backwardChar(1); // Move to end of previous line
274       }
275     if (todo >= h && todo > 0)
276       return count + todo - 1 + h; // Hit beginning of buffer.
277     todo = h - todo - 1;
278       }
279     moveToColumn(todo * width, false);
280     return count;
281   }
282   */

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 JavaDoc 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 JavaDoc toString()
312   {
313     if (buffer == null)
314       return "#<marker in no buffer>";
315     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(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