aboutsummaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorAurélien Aptel <aurelien.aptel@gmail.com>2010-11-18 12:43:33 +0100
committerAurélien Aptel <aurelien.aptel@gmail.com>2010-11-18 12:43:33 +0100
commit70464e208060746ad9184dfc298005fff3035d72 (patch)
treea75349e048c4be09322428a28b22e64a2cf293d2 /st.c
parent9b74fcadc46ee78b38b20e9663c6924cd9df7e84 (diff)
downloadst-70464e208060746ad9184dfc298005fff3035d72.tar.gz
st-70464e208060746ad9184dfc298005fff3035d72.zip
some code cleaning.
Diffstat (limited to 'st.c')
-rw-r--r--st.c88
1 files changed, 42 insertions, 46 deletions
diff --git a/st.c b/st.c
index da72497..b25fe89 100644
--- a/st.c
+++ b/st.c
@@ -201,6 +201,7 @@ static void xdrawcursor(void);
201static void xinit(void); 201static void xinit(void);
202static void xloadcols(void); 202static void xloadcols(void);
203static void xseturgency(int); 203static void xseturgency(int);
204static void xsetsel(char*);
204static void xresize(int, int); 205static void xresize(int, int);
205 206
206static void expose(XEvent *); 207static void expose(XEvent *);
@@ -213,8 +214,13 @@ static void focus(XEvent *);
213static void brelease(XEvent *); 214static void brelease(XEvent *);
214static void bpress(XEvent *); 215static void bpress(XEvent *);
215static void bmotion(XEvent *); 216static void bmotion(XEvent *);
216static void selection_notify(XEvent *); 217static void selnotify(XEvent *);
217static void selection_request(XEvent *); 218static void selrequest(XEvent *);
219
220static void selinit(void);
221static inline int selected(int, int);
222static void selcopy(void);
223static void selpaste(void);
218 224
219static int stou(char *, long *); 225static int stou(char *, long *);
220static int utos(long *, char *); 226static int utos(long *, char *);
@@ -232,8 +238,8 @@ static void (*handler[LASTEvent])(XEvent *) = {
232 [MotionNotify] = bmotion, 238 [MotionNotify] = bmotion,
233 [ButtonPress] = bpress, 239 [ButtonPress] = bpress,
234 [ButtonRelease] = brelease, 240 [ButtonRelease] = brelease,
235 [SelectionNotify] = selection_notify, 241 [SelectionNotify] = selnotify,
236 [SelectionRequest] = selection_request, 242 [SelectionRequest] = selrequest,
237}; 243};
238 244
239/* Globals */ 245/* Globals */
@@ -248,9 +254,7 @@ static char *opt_cmd = NULL;
248static char *opt_title = NULL; 254static char *opt_title = NULL;
249 255
250/* UTF-8 decode */ 256/* UTF-8 decode */
251static int 257static int stou(char *s, long *u) {
252stou(char *s, long *u)
253{
254 unsigned char c; 258 unsigned char c;
255 int i, n, rtn; 259 int i, n, rtn;
256 260
@@ -289,9 +293,7 @@ invalid:
289} 293}
290 294
291/* UTF-8 encode */ 295/* UTF-8 encode */
292static int 296static int utos(long *u, char *s) {
293utos(long *u, char *s)
294{
295 unsigned char *sp; 297 unsigned char *sp;
296 unsigned long uc; 298 unsigned long uc;
297 int i, n; 299 int i, n;
@@ -324,17 +326,12 @@ invalid:
324 return 3; 326 return 3;
325} 327}
326 328
327/* 329/* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
328 * use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode UTF-8 330 UTF-8 otherwise return 0 */
329 * otherwise return 0 331static int canstou(char *s, int b) {
330 */ 332 unsigned char c = *s;
331static int
332canstou(char *s, int b)
333{
334 unsigned char c;
335 int n; 333 int n;
336 334
337 c = *s;
338 if (b < 1) 335 if (b < 1)
339 return 0; 336 return 0;
340 else if (~c&B7) 337 else if (~c&B7)
@@ -358,12 +355,9 @@ canstou(char *s, int b)
358 return 1; 355 return 1;
359} 356}
360 357
361static int 358static int slen(char *s) {
362slen(char *s) 359 unsigned char c = *s;
363{
364 unsigned char c;
365 360
366 c = *s;
367 if (~c&B7) 361 if (~c&B7)
368 return 1; 362 return 1;
369 else if ((c&(B7|B6|B5)) == (B7|B6)) 363 else if ((c&(B7|B6|B5)) == (B7|B6))
@@ -374,8 +368,7 @@ slen(char *s)
374 return 4; 368 return 4;
375} 369}
376 370
377void 371static void selinit(void) {
378selinit(void) {
379 sel.mode = 0; 372 sel.mode = 0;
380 sel.bx = -1; 373 sel.bx = -1;
381 sel.clip = NULL; 374 sel.clip = NULL;
@@ -409,28 +402,31 @@ static void bpress(XEvent *e) {
409 sel.ey = sel.by = e->xbutton.y/xw.ch; 402 sel.ey = sel.by = e->xbutton.y/xw.ch;
410} 403}
411 404
412static char *getseltext() { 405static void selcopy() {
413 char *str, *ptr; 406 char *str, *ptr;
414 int ls, x, y, sz, sl; 407 int ls, x, y, sz, sl;
408
415 if(sel.bx == -1) 409 if(sel.bx == -1)
416 return NULL; 410 str = NULL;
417 sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ; 411 else {
418 ptr = str = malloc(sz); 412 sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
419 for(y = 0; y < term.row; y++) { 413 ptr = str = malloc(sz);
420 for(x = 0; x < term.col; x++) 414 for(y = 0; y < term.row; y++) {
421 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) { 415 for(x = 0; x < term.col; x++)
422 sl = slen(term.line[y][x].c); 416 if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
423 memcpy(ptr, term.line[y][x].c, sl); 417 sl = slen(term.line[y][x].c);
424 ptr += sl; 418 memcpy(ptr, term.line[y][x].c, sl);
425 } 419 ptr += sl;
426 if(ls) 420 }
427 *ptr = '\n', ptr++; 421 if(ls)
422 *ptr = '\n', ptr++;
423 }
424 *ptr = 0;
428 } 425 }
429 *ptr = 0; 426 xsetsel(str);
430 return str;
431} 427}
432 428
433static void selection_notify(XEvent *e) { 429static void selnotify(XEvent *e) {
434 unsigned long nitems; 430 unsigned long nitems;
435 unsigned long ofs, rem; 431 unsigned long ofs, rem;
436 int format; 432 int format;
@@ -456,7 +452,7 @@ static void selpaste() {
456 XConvertSelection(xw.dis, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime); 452 XConvertSelection(xw.dis, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime);
457} 453}
458 454
459static void selection_request(XEvent *e) 455static void selrequest(XEvent *e)
460{ 456{
461 XSelectionRequestEvent *xsre; 457 XSelectionRequestEvent *xsre;
462 XSelectionEvent xev; 458 XSelectionEvent xev;
@@ -491,7 +487,7 @@ static void selection_request(XEvent *e)
491 fprintf(stderr, "Error sending SelectionNotify event\n"); 487 fprintf(stderr, "Error sending SelectionNotify event\n");
492} 488}
493 489
494static void selcopy(char *str) { 490static void xsetsel(char *str) {
495 /* register the selection for both the clipboard and the primary */ 491 /* register the selection for both the clipboard and the primary */
496 Atom clipboard; 492 Atom clipboard;
497 493
@@ -517,7 +513,7 @@ static void brelease(XEvent *e) {
517 selpaste(); 513 selpaste();
518 } else { 514 } else {
519 if(b==1) 515 if(b==1)
520 selcopy(getseltext()); 516 selcopy();
521 } 517 }
522 draw(1); 518 draw(1);
523} 519}
@@ -525,7 +521,7 @@ static void brelease(XEvent *e) {
525static void bmotion(XEvent *e) { 521static void bmotion(XEvent *e) {
526 if (sel.mode) { 522 if (sel.mode) {
527 getbuttoninfo(e, NULL, &sel.ex, &sel.ey); 523 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
528 draw(1); 524 // draw(1);
529 } 525 }
530} 526}
531 527