File: wily.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 (184 lines) | stat: -rw-r--r-- 3,744 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
#include "wily.h"
#include "tile.h"
 
static int	ncolumns = 2;
int	tagheight;
Tile	*wily=0;			/* encloses the whole application */

char *wilytools = 	"Kill | Newcol Quit Putall wily-0.13.33 Dotfiles Font ";
char *filetools = 	"Del Look .";
char *dirtools = 	"Del Get Look ..";
char *columntools = "Delcol New Cut Paste Snarf Anchor Split | ";

/* Add  'cmd' to the visible list of running processes. */
void
addrunning(char *cmd){
	tag_addrunning(view_text(wily->tag), cmd);
}

/* Remove 'cmd' from the visible list of running processes.*/
void
rmrunning(char *cmd){
	tag_rmtool(view_text(wily->tag), cmd);
}

/* Initialize info about what bits of text get added to various tags.
 * Reads $WILYTOOLS or $HOME/.wilytools for regexp/tool pairs,
 * uses $WMAINTAG, $WCOLTAG $WFILETAG, $WDIRTAG 
 * as the tools for the wily tag, column tag, file tags and directory tags
 * respectively.
 */
static void
tools_init(void){
	char*s, t[200];
	
	if ((s = getenv("WILYTOOLS")))
		tag_init(s);
	else {
		Path p;
		sprintf(p, "%s/%s", getenv("HOME"), ".wilytools");
		tag_init(p);
	}
	
	if ((s=getenv("WCOLTAG")))
		columntools = strdup(s);
	if ((s=getenv("WMAINTAG"))) {
		strcpy(t, wilytools);
		strcat(t, s);
		wilytools = strdup(t);
	}
	if ((s=getenv("WFILETAG")))
		filetools = strdup(s);
	if ((s=getenv("WDIRTAG")))
		dirtools = strdup(s);
}

static void
usage(void) {
	fprintf(stderr,"wily [-c ncolumns] [-a tabsize] [-e command] [file1 ...]\n");
	exit(1);
}

static void
args(int argc,char **argv, char **envp)
{
	extern char *optarg;
	extern int optind;
	int	c;
	char *cmd = 0;

	tabsize = 4;	/* default */

	/* init libXg */
	xtbinit((Errfunc)error, "Wily", &argc, argv, 0);
	tagheight = font->height + 2*INSET;

	while ((c = getopt(argc, argv, "c:a:e:")) != EOF) {
		switch (c) {
		case 'c':	ncolumns = atoi(optarg); break;
		case 'a':	tabsize = atoi(optarg); break;
		case 'e':   cmd = optarg; break;
		default:	usage(); break;
		}
	}
	scroll_init();
	einit(Ekeyboard | Emouse);
	cursorswitch(cursor);
	wily_init();

	ex_init();

	if (optind<argc) {
		for ( ; optind < argc; optind++) {
			Path	label;
			
			data_addcontext(0, label, argv[optind]);
			data_open(label, true);
		}
	} else if (cmd == 0) {
		data_open(wilydir, false);
	}
	
	if (cmd != 0)
		run(wily->tag, cmd, 0);
}

/* Initialise the base tile, with some columns */
void
wily_init(void)
{
	Text	*t;
	int	j;
	
	t = text_alloc(0, false);
	text_replaceutf(t, nr, wilytools);
	wily = tile_new(V, 0, screen.r.max.y, 0, 0, t, 0);
	tile_reshaped(wily);
	last_selection = 0;
	view_setlastselection( wily->tag);
	for(j=0; j< ncolumns; j++)
		col_new(wily->tag, 0);
}

/* Reshape the base tile */
void
ereshaped(Rectangle r)
{
	if(!wily)
		return;	/* not initialised yet */
	wily->cmax = r.max.x;
	wily->max = r.max.y;
	tile_reshaped(wily);
}

/* Main event loop */
static void
mainloop(void)
{
	ulong	type;
	Event	e;
	Bool	mouseaction=true;
	Point	lastp={0,0};
	View	*v=0;

	while ((type = eread(~0, &e))) {
		switch(type){
		case Ekeyboard:
			if(mouseaction) {
				/* 'v' is a cache of the view we are pointing at,
				 * (we use point to type).  We update this only
				 * when we start typing after moving the pointer.
				 */
				v = point2view(lastp);
				mouseaction=false;
			}
			if(v)
				dokeyboard(v, e.kbdc);
			break;

		case Emouse:
			lastp = e.mouse.xy;
			mouseaction=true;
			if(e.mouse.buttons && (v = point2view(e.mouse.xy)))
				domouse(v, &e.mouse);	
			break;

		default:
			dofd(type, e.n, (char*)e.data);
			break;
		}
	}
}

int
main(int argc,char **argv, char **envp)
{
	if (!getcwd(wilydir,MAXPATH))
		fatal("couldn't find out what directory this is");
	add_slash(wilydir);
	env_init(envp);
	tools_init();
	args(argc,argv, envp);
	mainloop();
	return 0;
}