File: text2.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (359 lines) | stat: -rw-r--r-- 7,164 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "wily.h"
#include "text.h"
#include "view.h"
#include <ctype.h>

/****************************************************
	Random garbage
****************************************************/
void
text_allread (Text*t) {
	undo_reset(t);
	undo_start(t);
	viewlist_refresh(t->v);
}

/****************************************************
	Simple data access
****************************************************/
Data*
text_data(Text*t) {
	return t? t->data:0;
}

View*
text_view(Text*t) {
	return t->v;
}

ulong
text_length(Text*t) {
	return t->length;
}

Bool
text_needsbackup(Text*t) {
	return t->needsbackup;
}

/****************************************************
	Cooked data access
****************************************************/
/* Return body view associated with 't'. */
View*
text_body(Text*t) {
	if(t->data) {
		if(t->isbody)
			return t->v;
		else {
			t = data_body(t->data);
			return t->v;
		}
	} else {
		return 0;
	}
}

/****************************************************
	Simple data manipulations
****************************************************/

void
text_setneedsbackup(Text*t, Bool b) {
	t->needsbackup = b;
}

Bool
text_badrange(Text *t, Range r) {
	return r.p1 > t->length || r.p0 > r.p1;
}

void
text_addview(Text*t, View*v) {
	v->next = t->v;
	t->v = v;
}

/*
 * Remove 'v' from list of views displaying 't'.  If 'v' was the last
 * one, remove 't'.  If 't' represents some Data, make sure we've backed
 * it up if necessary.  Return 0 for success.
 */
int
text_rmview(Text*t, View *v) {
	View**ptr;

	if (t->v ==v && v->next == 0) {
		/* this is the last view */
		if (t->isbody && data_del(t->data))
			return -1;
	}
	/* update the list of views of the same body */
	for (ptr = &(t->v); *ptr != v; ptr = &( (*ptr)->next))
		;
	*ptr = v->next;

	if(!t->v) { /* we've deleted the last view */
		text_free(t);
		free(t);     
	}
	return 0;
}

/****************************************************
	Write to file
****************************************************/

/* Write the contents of 't' to 'fname'.  Return 0 for success. */
int
text_write(Text *t, char *fname) {
	int		fd;
	int		retval;

	/* open 0666 and rely on the umask */
	if((fd = open(fname,O_RDWR|O_CREAT|O_TRUNC, 0666))<0){
		diag(fname, "couldn't open %s for write",fname);
		return 1;
	}
	retval = text_write_range(t, range(0, t->length), fd);
	if(retval)
		diag(fname, "couldn't write %s",fname);
	close(fd);
	return retval;
}

/*Return a file descriptor open for reading from a temporary
 * file which has a copy of the current selection.
 * 
 * The input for the child comes from the current selection.
 * Put the selection into a text file and attach that text
 * file to the command's fdin.
 */
int
text_fd(Text *t, Range sel)
{
	char	*file = tmpnam(0);
	int	fd;
	int	input;

	if ((fd = open(file, O_WRONLY|O_CREAT, 0600)) < 0) {
		perror("open temp file");
		(void) unlink(file);
		return(-1);
	}

	/* Now for the child's end.  Do it quick so we can unlink. */
	if ((input = open(file, O_RDONLY)) < 0) {
		perror("open temp file");
		(void) unlink(file);
		return(-1);
	}

	if (unlink(file) < 0)
		perror("unlink temp file");
		/* no need to *do* anything about it */

	/* Our buffer is the most recent selection, or if the
	 * most recent selection is in a tag, the selection in the body
	 * of that win.
	 */
	if (text_write_range(t, sel, fd)) {
		perror("write temp file");
		return(-1);
	}
	if (close(fd) <  0)
		perror("close temp file");

	return input;
}

/****************************************************
	Auto indent
****************************************************/
enum {MAXAI=128};

/*
 * Return an Rstring to be inserted after position 'p',
 * given that autoindent is on.
 * The Rstring is to a *static* Rune buffer.
 */
/** NB *not* reentrant */
Rstring
text_autoindent(Text *t, ulong p)
{
	static Rune buf[MAXAI];
	Rstring s;
	Range	r;
	int	i;

	buf[0] = '\n';
	s.r0 = buf;

	r = range(text_nl(t, p, 0), p);
	if (RLEN(r) > MAXAI)
		r.p1 = r.p0 + MAXAI;
	text_copy(t, r, buf +1);

	i = 1;
	while (i <= RLEN(r) && isspace(buf[i]))
		i++;
	s.r1 = buf + i;

	return s;
}

/****************************************************
	Convenience functions
****************************************************/

/*
 * Return the character to start displaying from,
 * such that the character at 'p' will be offset by
 * 'height', when displayed
 * in a window of width 'w', with Font 'f'
 */ 
int
back_height(Text *t, ulong p, Font *f, int width, int height) {
	int	c, hpos;
	
	if (p > 0) --p;
	for( hpos = 0; p>0 && height>0; p--) {
		c = TCHAR(t,p);
		switch(c) {
		case '\t': hpos += tabsize; break;
		case '\n': hpos = 0; height -= f->height; break;
		default: hpos += charwidth(f,c); break;
		}
		if (hpos >= width) {
			hpos =0;
			height -= f->height;
		}
	}
	return p? p+2 : p;
}

Range
text_all(Text*t){
	Range r;
	
	r.p0 = 0;
	r.p1 = t->length;
	return r;
}

void
text_fillbutton(Text*t, Fcode f) {
	View*v;
	for(v = t->v; v; v= v->next)
		view_fillbutton(v,f);
}

/* Try to copy 'n' Runes at 'p' from 't' into 'buf'.
 * Return the number of runes copied.
 */
ulong
text_ncopy(Text *t, Rune*buf, ulong p, ulong n)
{
	Range	r;
	if (p >= t->length)
		return 0;
	r = range(p, MIN(p+n, t->length));
	text_copy(t, r, buf);
	return RLEN(r);
}

/* Replace 'r' in 't' with 'utf', return the range of newly placed runes */
Range
text_replaceutf(Text*t, Range r, char*utf) {
	Rstring	s;

	s = utf2rstring(utf);
	r =  text_replace(t, r, s);
	if(s.r0)
		free(s.r0);
	return r;
}

/* Return a newly allocated string containing the utf
 * representation of the given range inside 't'
 */
char *
text_duputf(Text *t, Range r) {
	ulong	len = RLEN(r);
	ulong	n;
	char	*buf;

	if(!len)
		return strdup("");

	buf = salloc(len * UTFmax);
	n = text_copyutf(t, r, buf);
	buf[n] = '\0';
	return buf;
}

/* Copy Range 'r' from 't', convert it to utf, store it
 * in 'buf', (which must have enough space allocated).
 * Returns the number of utf characters copied.
 */
int
text_copyutf(Text*t, Range r, char *buf)
{
	Rune	*rbuf;
	int		rlen;
	int		n;

	rlen = RLEN(r);

	rbuf = salloc( rlen * sizeof(Rune));
	text_copy(t, r, rbuf);
	n =  texttoutf(buf, rbuf, rbuf + rlen);
	free(rbuf);
	return n;
}

/****************************************************
	Formatting text representing a directory
****************************************************/
static void
text_getdir(Text *t, char**names) {
	Frame	*f;
	char	*s;
	
	f = &t->v->f;
	s = columnate(Dx(f->r), f->maxtab, f->font, names);
	
	undo_reset(t);
	text_replaceutf(t, range(0, t->length), s);
	undo_start(t);
	free(s);
}

/*
* If 't' represents a directory, and doesn't have any modified text,
* reformat the directory and return true.  Otherwise return false.
*/
Bool
text_refreshdir(Text*t)
{
	char**names;

	if ( t->isbody && (names = data_names(t->data)) && undo_atmark(t) && t->v) {
		text_getdir(t, names);
		return true;
	} else {
		return false;
	}
}

/* Reformat 'd' (which must hold a directory) to fit nicely
 * in the smallest view displaying it.
 */
void
text_formatdir(Text *t, char**names) {
	if(!(t->v && names))
		return;

	text_getdir(t,names);
	viewlist_refresh(t->v);
}