diff options
| author | Aurélien Aptel <aurelien.aptel@gmail.com> | 2010-11-18 12:43:33 +0100 |
|---|---|---|
| committer | Aurélien Aptel <aurelien.aptel@gmail.com> | 2010-11-18 12:43:33 +0100 |
| commit | 70464e208060746ad9184dfc298005fff3035d72 (patch) | |
| tree | a75349e048c4be09322428a28b22e64a2cf293d2 /st.c | |
| parent | utf8 support! print text in delicious unicode greatness! all hail to the glor... (diff) | |
| download | st-70464e208060746ad9184dfc298005fff3035d72.tar.gz st-70464e208060746ad9184dfc298005fff3035d72.zip | |
some code cleaning.
Diffstat (limited to 'st.c')
| -rw-r--r-- | st.c | 88 |
1 files changed, 42 insertions, 46 deletions
| @@ -201,6 +201,7 @@ static void xdrawcursor(void); | |||
| 201 | static void xinit(void); | 201 | static void xinit(void); |
| 202 | static void xloadcols(void); | 202 | static void xloadcols(void); |
| 203 | static void xseturgency(int); | 203 | static void xseturgency(int); |
| 204 | static void xsetsel(char*); | ||
| 204 | static void xresize(int, int); | 205 | static void xresize(int, int); |
| 205 | 206 | ||
| 206 | static void expose(XEvent *); | 207 | static void expose(XEvent *); |
| @@ -213,8 +214,13 @@ static void focus(XEvent *); | |||
| 213 | static void brelease(XEvent *); | 214 | static void brelease(XEvent *); |
| 214 | static void bpress(XEvent *); | 215 | static void bpress(XEvent *); |
| 215 | static void bmotion(XEvent *); | 216 | static void bmotion(XEvent *); |
| 216 | static void selection_notify(XEvent *); | 217 | static void selnotify(XEvent *); |
| 217 | static void selection_request(XEvent *); | 218 | static void selrequest(XEvent *); |
| 219 | |||
| 220 | static void selinit(void); | ||
| 221 | static inline int selected(int, int); | ||
| 222 | static void selcopy(void); | ||
| 223 | static void selpaste(void); | ||
| 218 | 224 | ||
| 219 | static int stou(char *, long *); | 225 | static int stou(char *, long *); |
| 220 | static int utos(long *, char *); | 226 | static 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; | |||
| 248 | static char *opt_title = NULL; | 254 | static char *opt_title = NULL; |
| 249 | 255 | ||
| 250 | /* UTF-8 decode */ | 256 | /* UTF-8 decode */ |
| 251 | static int | 257 | static int stou(char *s, long *u) { |
| 252 | stou(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 */ |
| 292 | static int | 296 | static int utos(long *u, char *s) { |
| 293 | utos(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 | 331 | static int canstou(char *s, int b) { |
| 330 | */ | 332 | unsigned char c = *s; |
| 331 | static int | ||
| 332 | canstou(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 | ||
| 361 | static int | 358 | static int slen(char *s) { |
| 362 | slen(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 | ||
| 377 | void | 371 | static void selinit(void) { |
| 378 | selinit(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 | ||
| 412 | static char *getseltext() { | 405 | static 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 | ||
| 433 | static void selection_notify(XEvent *e) { | 429 | static 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 | ||
| 459 | static void selection_request(XEvent *e) | 455 | static 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 | ||
| 494 | static void selcopy(char *str) { | 490 | static 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) { | |||
| 525 | static void bmotion(XEvent *e) { | 521 | static 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 | ||
