some code cleaning.

master
Aurélien Aptel 2010-11-18 12:43:33 +01:00
parent 9b74fcadc4
commit 70464e2080
1 changed files with 42 additions and 46 deletions

88
st.c
View File

@ -201,6 +201,7 @@ static void xdrawcursor(void);
static void xinit(void); static void xinit(void);
static void xloadcols(void); static void xloadcols(void);
static void xseturgency(int); static void xseturgency(int);
static void xsetsel(char*);
static void xresize(int, int); static void xresize(int, int);
static void expose(XEvent *); static void expose(XEvent *);
@ -213,8 +214,13 @@ static void focus(XEvent *);
static void brelease(XEvent *); static void brelease(XEvent *);
static void bpress(XEvent *); static void bpress(XEvent *);
static void bmotion(XEvent *); static void bmotion(XEvent *);
static void selection_notify(XEvent *); static void selnotify(XEvent *);
static void selection_request(XEvent *); static void selrequest(XEvent *);
static void selinit(void);
static inline int selected(int, int);
static void selcopy(void);
static void selpaste(void);
static int stou(char *, long *); static int stou(char *, long *);
static int utos(long *, char *); static int utos(long *, char *);
@ -232,8 +238,8 @@ static void (*handler[LASTEvent])(XEvent *) = {
[MotionNotify] = bmotion, [MotionNotify] = bmotion,
[ButtonPress] = bpress, [ButtonPress] = bpress,
[ButtonRelease] = brelease, [ButtonRelease] = brelease,
[SelectionNotify] = selection_notify, [SelectionNotify] = selnotify,
[SelectionRequest] = selection_request, [SelectionRequest] = selrequest,
}; };
/* Globals */ /* Globals */
@ -248,9 +254,7 @@ static char *opt_cmd = NULL;
static char *opt_title = NULL; static char *opt_title = NULL;
/* UTF-8 decode */ /* UTF-8 decode */
static int static int stou(char *s, long *u) {
stou(char *s, long *u)
{
unsigned char c; unsigned char c;
int i, n, rtn; int i, n, rtn;
@ -289,9 +293,7 @@ invalid:
} }
/* UTF-8 encode */ /* UTF-8 encode */
static int static int utos(long *u, char *s) {
utos(long *u, char *s)
{
unsigned char *sp; unsigned char *sp;
unsigned long uc; unsigned long uc;
int i, n; int i, n;
@ -324,17 +326,12 @@ invalid:
return 3; return 3;
} }
/* /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode UTF-8 UTF-8 otherwise return 0 */
* otherwise return 0 static int canstou(char *s, int b) {
*/ unsigned char c = *s;
static int
canstou(char *s, int b)
{
unsigned char c;
int n; int n;
c = *s;
if (b < 1) if (b < 1)
return 0; return 0;
else if (~c&B7) else if (~c&B7)
@ -358,12 +355,9 @@ canstou(char *s, int b)
return 1; return 1;
} }
static int static int slen(char *s) {
slen(char *s) unsigned char c = *s;
{
unsigned char c;
c = *s;
if (~c&B7) if (~c&B7)
return 1; return 1;
else if ((c&(B7|B6|B5)) == (B7|B6)) else if ((c&(B7|B6|B5)) == (B7|B6))
@ -374,8 +368,7 @@ slen(char *s)
return 4; return 4;
} }
void static void selinit(void) {
selinit(void) {
sel.mode = 0; sel.mode = 0;
sel.bx = -1; sel.bx = -1;
sel.clip = NULL; sel.clip = NULL;
@ -409,28 +402,31 @@ static void bpress(XEvent *e) {
sel.ey = sel.by = e->xbutton.y/xw.ch; sel.ey = sel.by = e->xbutton.y/xw.ch;
} }
static char *getseltext() { static void selcopy() {
char *str, *ptr; char *str, *ptr;
int ls, x, y, sz, sl; int ls, x, y, sz, sl;
if(sel.bx == -1) if(sel.bx == -1)
return NULL; str = NULL;
sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ; else {
ptr = str = malloc(sz); sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
for(y = 0; y < term.row; y++) { ptr = str = malloc(sz);
for(x = 0; x < term.col; x++) for(y = 0; y < term.row; y++) {
if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) { for(x = 0; x < term.col; x++)
sl = slen(term.line[y][x].c); if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
memcpy(ptr, term.line[y][x].c, sl); sl = slen(term.line[y][x].c);
ptr += sl; memcpy(ptr, term.line[y][x].c, sl);
} ptr += sl;
if(ls) }
*ptr = '\n', ptr++; if(ls)
*ptr = '\n', ptr++;
}
*ptr = 0;
} }
*ptr = 0; xsetsel(str);
return str;
} }
static void selection_notify(XEvent *e) { static void selnotify(XEvent *e) {
unsigned long nitems; unsigned long nitems;
unsigned long ofs, rem; unsigned long ofs, rem;
int format; int format;
@ -456,7 +452,7 @@ static void selpaste() {
XConvertSelection(xw.dis, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime); XConvertSelection(xw.dis, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime);
} }
static void selection_request(XEvent *e) static void selrequest(XEvent *e)
{ {
XSelectionRequestEvent *xsre; XSelectionRequestEvent *xsre;
XSelectionEvent xev; XSelectionEvent xev;
@ -491,7 +487,7 @@ static void selection_request(XEvent *e)
fprintf(stderr, "Error sending SelectionNotify event\n"); fprintf(stderr, "Error sending SelectionNotify event\n");
} }
static void selcopy(char *str) { static void xsetsel(char *str) {
/* register the selection for both the clipboard and the primary */ /* register the selection for both the clipboard and the primary */
Atom clipboard; Atom clipboard;
@ -517,7 +513,7 @@ static void brelease(XEvent *e) {
selpaste(); selpaste();
} else { } else {
if(b==1) if(b==1)
selcopy(getseltext()); selcopy();
} }
draw(1); draw(1);
} }
@ -525,7 +521,7 @@ static void brelease(XEvent *e) {
static void bmotion(XEvent *e) { static void bmotion(XEvent *e) {
if (sel.mode) { if (sel.mode) {
getbuttoninfo(e, NULL, &sel.ex, &sel.ey); getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
draw(1); // draw(1);
} }
} }