KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > swing > BufferContent


1 package gnu.jemacs.swing;
2 import gnu.jemacs.buffer.*;
3 import javax.swing.text.*;
4 import javax.swing.undo.*;
5 import gnu.lists.*;
6
7 /** A Content class that supports Emacs-style Markers.
8  * The standard GapContent is close, but unfortunately it only
9  * supports inserting *before* marks, which is not the Emacs default.
10  * This provides a superset of the Position functionality (except for undo).
11  */

12
13 public class BufferContent extends gnu.kawa.swingviews.SwingContent
14 {
15   public BufferContent()
16   {
17     this(100);
18   }
19
20   public BufferContent(int initialSize)
21   {
22     super(initialSize);
23   }
24
25   public static int indexOf(char[] buffer, int start, int limit, char ch)
26   {
27     for (int i = start; i < limit; i++)
28       {
29         if (buffer[i] == ch)
30           return i;
31       }
32     return -1;
33   }
34
35   /** Search for the last occurrence of a character
36    * in buffer[limit..start]. */

37   public static int lastIndexOf(char[] buffer, int start, int limit, char ch)
38   {
39     for (int i = start; i >= limit; i--)
40       {
41         if (buffer[i] == ch)
42           return i;
43       }
44     return -1;
45   }
46
47   /** Search in BUF for COUNT instances of the character TARGET between START and END.
48    * If COUNT is positive, search forwards; END must be >= START.
49    * If COUNT is negative, search backwards for the -COUNTth instance;
50    * END must be <= START.
51    * If COUNT is zero, do anything you please; run rogue, for all I care.
52    *
53    * If we find COUNT instances, SHORTAGE is zero, and return the
54    * position after the COUNTth match. Note that for reverse motion
55    * this is not the same as the usual convention for Emacs motion commands.
56
57    * If we don't find COUNT instances before reaching END, set SHORTAGE
58    * to the number of TARGETs left unfound, and return (shortage<<32|END).
59    * @return (SHORTAGE<<32|POS)
60   */

61
62   public final long scan(char target, int start, int end,
63                          int count, boolean allowQuit)
64   {
65     CharBuffer b = buffer;
66     int limit = end > b.gapStart ? end + b.gapEnd - b.gapStart : end;
67     if (start > b.gapStart)
68       start += b.gapEnd - b.gapStart;
69     if (count > 0)
70       {
71         while (start < limit && count > 0)
72           {
73             int ceil;
74             if (start == b.gapStart)
75               start = b.gapEnd;
76             if (start < b.gapStart && limit > b.gapStart)
77               ceil = b.gapStart;
78             else
79               {
80                 ceil = limit;
81               }
82             if (allowQuit)
83               {
84                 if (ceil - start > 5000)
85                   ceil = start + 5000;
86                 Signal.checkQuit();
87               }
88             int i = indexOf(b.getArray(), start, ceil, target);
89             if (i >= 0)
90               {
91                 count--;
92                 start = i + 1;
93               }
94             else
95               start = ceil;
96           }
97         if (start > b.gapEnd)
98           start -= b.gapEnd - b.gapStart;
99         return ((long) count << 32) | start;
100       }
101     else
102       {
103         while (start > limit && count < 0)
104           {
105             if (start == b.gapEnd)
106               start = b.gapStart;
107             int floor;
108             if (start <= b.gapStart || limit >= b.gapEnd)
109               floor = limit;
110             else
111               floor = b.gapEnd;
112             if (allowQuit)
113               {
114                 if (start - floor > 5000)
115                   floor = start - 5000;
116                 Signal.checkQuit();
117               }
118             int i = lastIndexOf(b.getArray(), start - 1, floor, target);
119             if (i >= 0)
120               {
121                 count++;
122                 start = i;
123               }
124             else
125               start = floor;
126           }
127   
128         if (start >= b.gapEnd)
129           start -= b.gapEnd - b.gapStart;
130         if (count != 0)
131           return ((long) (- count) << 32) | start;
132         else
133           {
134             // We found the character we were looking for; we have to return
135
// the position *after* it due to the strange way that the return
136
// value is defined.
137
return start + 1;
138           }
139       }
140   }
141 }
142
Popular Tags