File: tag.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 (192 lines) | stat: -rw-r--r-- 3,655 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
#include "wily.h"
#include "view.h"
#include "text.h"
#include <string.h>
#include <ctype.h>

/* The tag for a window contains: a label then whitespace, then some 
 * system-maintained tools, then a pipe symbol, then user stuff.
 */

static char * whitespace_regexp = "[ \t\n]+";
static Range	tag_findtool(Text *, char*);

static Bool		wily_modifying_tag = false;

/************************************************
	Gettools and Settools only used by the messaging interface.
	It's still up in the air what exactly they should do.
	TODO - ensure space before/after "|" separator.
************************************************/
char*
tag_gettools(Text*t){
	Range r;
	
	r = nr;
	if(text_utfregexp(t, "\\|.*", &r, true)) {
		r.p0++;
		return text_duputf(t, r);
	} else {
		return "";
	}
}

void
tag_settools(Text*t, char*s) {
	Range r = nr;
	ulong	len;
	
	if(text_utfregexp(t, "\\|.*", &r, true)) {
		r.p0++;
	} else {
		len = text_length(t);
		r = range(len,len);
	}
	text_replaceutf(t,r,s);
}

void
tag_set(Text*t, char*s) {
	assert(TEXT_ISTAG(t));

	wily_modifying_tag = true;
	text_replaceutf(t, text_all(t), s);
	wily_modifying_tag = false;
}

void
tag_setlabel(Text *t, char *s)
{
	Range	r;
	Path		buf;
	ulong	l;
	
	wily_modifying_tag = true;

	/* find first whitespace_regexp */
	r = range(0,0);
	if(! text_utfregexp(t, whitespace_regexp, &r, true)) {
		l = text_length(t);
		r = range(l,l);
	}

	r.p0 = 0;
	sprintf(buf, "%s ", s);
	text_replaceutf(t, r, buf);
	wily_modifying_tag = false;
}

/* Remove 's' from the tool section of tag 't', if it exists there.
 */
void
tag_rmtool(Text *t, char *s)
{
	Range	r;
	
	wily_modifying_tag = true;

	r = tag_findtool(t, s);
	if(RLEN(r))
		text_replace(t, r, rstring(0,0));
	if(STRSAME(s,"Put"))
		text_fillbutton(t, Zero);
	wily_modifying_tag = false;
}

/* Replace 'r' in 't' with 's' and a trailing space */
static void
place_tool(Text*t, Range r, char*s) {
	Path	tmp;
	
	sprintf(tmp, "%s ", s);
	text_replaceutf(t,r,tmp);
}

/* 
 * Add a string to represent a running command to 't'.
 *
 * Almost the same as tag_addtool, the difference being
 * that tag_addrunning will create duplicates, e.g. the
 * tag could read "xterm xterm "
 */
void
tag_addrunning(Text *t, char *cmd) {
	Range	r;
	
	r = tag_findtool(t,cmd);
	r.p0  = r.p1;	/* don't replace */
	place_tool(t,r,cmd);
}

/* Add 's' to the tool section of tag 't', unless it's there already.
*/
void
tag_addtool(Text *t, char *s)
{
	Range	r;

	wily_modifying_tag = true;
	
	r = tag_findtool(t,s);
	if(!RLEN(r))
		place_tool(t,r,s);
	if(STRSAME(s,"Put"))
		text_fillbutton(t, F);
	wily_modifying_tag = false;
}

/* If 's' is in the system tools section of t, return the range
 * containing 's' and all the immediately following whitespace.
 * Otherwise return a 0-length range where the next tool
 * should be inserted.
 */
static Range
tag_findtool(Text*t, char*s)
{
	Range	pos, endpos;
	ulong	l;

	endpos = pos = nr;
	if(text_findliteralutf(t, &endpos, "|")) {
		endpos.p1 = endpos.p0;
	} else {
		l = text_length(t);
		endpos = range(l,l);
	}

	if(text_findwordutf(t, &pos, s) && pos.p0 < endpos.p0)
		return pos;

	return endpos;
}

/*
 * 't' has been modified in some fashion. 'p' is the index of the
 * most recently modified character.
 */
void
tag_modified(Text*t, ulong p)
{
	Range	r;
	Path		buf;
	int		n;
	
	assert(t->data && !t->isbody);
	if( wily_modifying_tag)
		return;
	
	/* find first whitespace_regexp */
	r = nr;
	if(text_utfregexp(t, whitespace_regexp, &r, true)) {
		if(p>r.p0)
			return;
		r.p1 = r.p0;
		r.p0 = 0;
	} else {
		r = text_all(t);
	}

	n = text_copyutf(t, r, buf);
	buf[n] = 0;
	data_setlabel(text_data(t), buf);
}