source: mainline/uspace/app/edit/sheet.c@ c6a7b3a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c6a7b3a was 7feb86e6, checked in by Martin Sucha <sucha14@…>, 13 years ago

Improve edit.

  • Fix Home moving caret to the end of previous line
  • Add next/prev char movement to spt
  • Refactor caret movement code
  • Implement search function
  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * Copyright (c) 2009 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup edit
30 * @{
31 */
32/**
33 * @file
34 * @brief Prototype implementation of Sheet data structure.
35 *
36 * The sheet is an abstract data structure representing a piece of text.
37 * On top of this data structure we can implement a text editor. It is
38 * possible to implement the sheet such that the editor can make small
39 * changes to large files or files containing long lines efficiently.
40 *
41 * The sheet structure allows basic operations of text insertion, deletion,
42 * retrieval and mapping of coordinates to position in the file and vice
43 * versa. The text that is inserted or deleted can contain tabs and newlines
44 * which are interpreted and properly acted upon.
45 *
46 * This is a trivial implementation with poor efficiency with O(N+n)
47 * insertion and deletion and O(N) mapping (in both directions), where
48 * N is the size of the file and n is the size of the inserted/deleted text.
49 */
50
51#include <stdlib.h>
52#include <str.h>
53#include <errno.h>
54#include <adt/list.h>
55#include <align.h>
56#include <macros.h>
57
58#include "sheet.h"
59#include "sheet_impl.h"
60
61enum {
62 TAB_WIDTH = 8,
63
64 /** Initial of dat buffer in bytes */
65 INITIAL_SIZE = 32
66};
67
68/** Initialize an empty sheet. */
69int sheet_create(sheet_t **rsh)
70{
71 sheet_t *sh;
72
73 sh = calloc(1, sizeof(sheet_t));
74 if (sh == NULL)
75 return ENOMEM;
76
77 sh->dbuf_size = INITIAL_SIZE;
78 sh->text_size = 0;
79
80 sh->data = malloc(sh->dbuf_size);
81 if (sh->data == NULL)
82 return ENOMEM;
83
84 list_initialize(&sh->tags);
85
86 *rsh = sh;
87 return EOK;
88}
89
90/** Insert text into sheet.
91 *
92 * @param sh Sheet to insert to.
93 * @param pos Point where to insert.
94 * @param dir Whether to insert before or after the point (affects tags).
95 * @param str The text to insert (printable characters, tabs, newlines).
96 *
97 * @return EOK on success or negative error code.
98 *
99 * @note @a dir affects which way tags that were placed on @a pos will
100 * move. If @a dir is @c dir_before, the tags will move forward
101 * and vice versa.
102 */
103int sheet_insert(sheet_t *sh, spt_t *pos, enum dir_spec dir, char *str)
104{
105 char *ipp;
106 size_t sz;
107 tag_t *tag;
108 char *newp;
109
110 sz = str_size(str);
111 if (sh->text_size + sz > sh->dbuf_size) {
112 /* Enlarge data buffer. */
113 newp = realloc(sh->data, sh->dbuf_size * 2);
114 if (newp == NULL)
115 return ELIMIT;
116
117 sh->data = newp;
118 sh->dbuf_size = sh->dbuf_size * 2;
119 }
120
121 ipp = sh->data + pos->b_off;
122
123 /* Copy data. */
124 memmove(ipp + sz, ipp, sh->text_size - pos->b_off);
125 memcpy(ipp, str, sz);
126 sh->text_size += sz;
127
128 /* Adjust tags. */
129
130 list_foreach(sh->tags, link) {
131 tag = list_get_instance(link, tag_t, link);
132
133 if (tag->b_off > pos->b_off)
134 tag->b_off += sz;
135 else if (tag->b_off == pos->b_off && dir == dir_before)
136 tag->b_off += sz;
137 }
138
139 return EOK;
140}
141
142/** Delete text from sheet.
143 *
144 * Deletes the range of text between two points from the sheet.
145 *
146 * @param sh Sheet to delete from.
147 * @param spos Starting point.
148 * @param epos Ending point.
149 *
150 * @return EOK on success or negative error code.
151 **/
152int sheet_delete(sheet_t *sh, spt_t *spos, spt_t *epos)
153{
154 char *spp;
155 size_t sz;
156 tag_t *tag;
157 char *newp;
158 size_t shrink_size;
159
160 spp = sh->data + spos->b_off;
161 sz = epos->b_off - spos->b_off;
162
163 memmove(spp, spp + sz, sh->text_size - (spos->b_off + sz));
164 sh->text_size -= sz;
165
166 /* Adjust tags. */
167 list_foreach(sh->tags, link) {
168 tag = list_get_instance(link, tag_t, link);
169
170 if (tag->b_off >= epos->b_off)
171 tag->b_off -= sz;
172 else if (tag->b_off >= spos->b_off)
173 tag->b_off = spos->b_off;
174 }
175
176 /* See if we should free up some memory. */
177 shrink_size = max(sh->dbuf_size / 4, INITIAL_SIZE);
178 if (sh->text_size <= shrink_size && sh->dbuf_size > INITIAL_SIZE) {
179 /* Shrink data buffer. */
180 newp = realloc(sh->data, shrink_size);
181 if (newp == NULL) {
182 /* Failed to shrink buffer... no matter. */
183 return EOK;
184 }
185
186 sh->data = newp;
187 sh->dbuf_size = shrink_size;
188 }
189
190 return EOK;
191}
192
193/** Read text from sheet. */
194void sheet_copy_out(sheet_t *sh, spt_t const *spos, spt_t const *epos,
195 char *buf, size_t bufsize, spt_t *fpos)
196{
197 char *spp;
198 size_t range_sz;
199 size_t copy_sz;
200 size_t off, prev;
201 wchar_t c;
202
203 spp = sh->data + spos->b_off;
204 range_sz = epos->b_off - spos->b_off;
205 copy_sz = (range_sz < bufsize - 1) ? range_sz : bufsize - 1;
206
207 prev = off = 0;
208 do {
209 prev = off;
210 c = str_decode(spp, &off, copy_sz);
211 } while (c != '\0');
212
213 /* Crop copy_sz down to the last full character. */
214 copy_sz = prev;
215
216 memcpy(buf, spp, copy_sz);
217 buf[copy_sz] = '\0';
218
219 fpos->b_off = spos->b_off + copy_sz;
220 fpos->sh = sh;
221}
222
223/** Get point preceding or following character cell. */
224void sheet_get_cell_pt(sheet_t *sh, coord_t const *coord, enum dir_spec dir,
225 spt_t *pt)
226{
227 size_t cur_pos, prev_pos;
228 wchar_t c;
229 coord_t cc;
230
231 cc.row = cc.column = 1;
232 cur_pos = prev_pos = 0;
233 while (true) {
234 if (prev_pos >= sh->text_size) {
235 /* Cannot advance any further. */
236 break;
237 }
238
239 if ((cc.row >= coord->row && cc.column > coord->column) ||
240 cc.row > coord->row) {
241 /* We are past the requested coordinates. */
242 break;
243 }
244
245 prev_pos = cur_pos;
246
247 c = str_decode(sh->data, &cur_pos, sh->text_size);
248 if (c == '\n') {
249 ++cc.row;
250 cc.column = 1;
251 } else if (c == '\t') {
252 cc.column = 1 + ALIGN_UP(cc.column, TAB_WIDTH);
253 } else {
254 ++cc.column;
255 }
256 }
257
258 pt->sh = sh;
259 pt->b_off = (dir == dir_before) ? prev_pos : cur_pos;
260}
261
262/** Get the number of character cells a row occupies. */
263void sheet_get_row_width(sheet_t *sh, int row, int *length)
264{
265 coord_t coord;
266 spt_t pt;
267
268 /* Especially nasty hack */
269 coord.row = row;
270 coord.column = 65536;
271
272 sheet_get_cell_pt(sh, &coord, dir_before, &pt);
273 spt_get_coord(&pt, &coord);
274 *length = coord.column;
275}
276
277/** Get the number of rows in a sheet. */
278void sheet_get_num_rows(sheet_t *sh, int *rows)
279{
280 int cnt;
281 size_t bo;
282
283 cnt = 1;
284 for (bo = 0; bo < sh->dbuf_size; ++bo) {
285 if (sh->data[bo] == '\n')
286 cnt += 1;
287 }
288
289 *rows = cnt;
290}
291
292/** Get the coordinates of an s-point. */
293void spt_get_coord(spt_t const *pos, coord_t *coord)
294{
295 size_t off;
296 coord_t cc;
297 wchar_t c;
298 sheet_t *sh;
299
300 sh = pos->sh;
301 cc.row = cc.column = 1;
302
303 off = 0;
304 while (off < pos->b_off && off < sh->text_size) {
305 c = str_decode(sh->data, &off, sh->text_size);
306 if (c == '\n') {
307 ++cc.row;
308 cc.column = 1;
309 } else if (c == '\t') {
310 cc.column = 1 + ALIGN_UP(cc.column, TAB_WIDTH);
311 } else {
312 ++cc.column;
313 }
314 }
315
316 *coord = cc;
317}
318
319/** Test if two s-points are equal. */
320bool spt_equal(spt_t const *a, spt_t const *b)
321{
322 return a->b_off == b->b_off;
323}
324
325/** Get a character at spt and return next spt */
326wchar_t spt_next_char(spt_t spt, spt_t *next)
327{
328 wchar_t ch = str_decode(spt.sh->data, &spt.b_off, spt.sh->text_size);
329 if (next)
330 *next = spt;
331 return ch;
332}
333
334wchar_t spt_prev_char(spt_t spt, spt_t *prev)
335{
336 wchar_t ch = str_decode_reverse(spt.sh->data, &spt.b_off, spt.sh->text_size);
337 if (prev)
338 *prev = spt;
339 return ch;
340}
341
342/** Place a tag on the specified s-point. */
343void sheet_place_tag(sheet_t *sh, spt_t const *pt, tag_t *tag)
344{
345 tag->b_off = pt->b_off;
346 tag->sh = sh;
347 list_append(&tag->link, &sh->tags);
348}
349
350/** Remove a tag from the sheet. */
351void sheet_remove_tag(sheet_t *sh, tag_t *tag)
352{
353 list_remove(&tag->link);
354}
355
356/** Get s-point on which the tag is located right now. */
357void tag_get_pt(tag_t const *tag, spt_t *pt)
358{
359 pt->b_off = tag->b_off;
360 pt->sh = tag->sh;
361}
362
363/** @}
364 */
Note: See TracBrowser for help on using the repository browser.