aboutsummaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorAurélien Aptel <aurelien.aptel@gmail.com>2010-08-31 15:36:13 +0200
committerAurélien Aptel <aurelien.aptel@gmail.com>2010-08-31 15:36:13 +0200
commitee858b4805aec3e12bff48cf27433ebcd3f6ac34 (patch)
treefce67e32c7925d17550a834045b87df295040758 /st.c
parent80f70f1c224ec6fb10f04c29ea2205f47ae553ff (diff)
downloadst-ee858b4805aec3e12bff48cf27433ebcd3f6ac34.tar.gz
st-ee858b4805aec3e12bff48cf27433ebcd3f6ac34.zip
use one global struct instead of many vars for selection. Cleanup.
Diffstat (limited to 'st.c')
-rw-r--r--st.c74
1 files changed, 38 insertions, 36 deletions
diff --git a/st.c b/st.c
index 665dc1a..4a89876 100644
--- a/st.c
+++ b/st.c
@@ -125,6 +125,14 @@ typedef struct {
125 GC gc; 125 GC gc;
126} DC; 126} DC;
127 127
128typedef struct {
129 int mode;
130 int bx, by;
131 int ex, ey;
132 int b[2], e[2];
133 char *clip;
134} Selection;
135
128#include "config.h" 136#include "config.h"
129 137
130static void die(const char *errstr, ...); 138static void die(const char *errstr, ...);
@@ -200,23 +208,18 @@ static Term term;
200static CSIEscape escseq; 208static CSIEscape escseq;
201static int cmdfd; 209static int cmdfd;
202static pid_t pid; 210static pid_t pid;
211static Selection sel;
203 212
204/* selection */ 213/* TODO: use X11 clipboard */
205// TODO: use X11 clipboard
206static int selmode = 0;
207static int selbx = -1, selby;
208static int selex, seley;
209int sb[2], se[2];
210static const char *clipboard = NULL;
211 214
212static inline int selected(int x, int y) { 215static inline int selected(int x, int y) {
213 if ((seley==y && selby==y)) { 216 if ((sel.ey==y && sel.by==y)) {
214 int bx = MIN(selbx, selex); 217 int bx = MIN(sel.bx, sel.ex);
215 int ex = MAX(selbx, selex); 218 int ex = MAX(sel.bx, sel.ex);
216 return (x>=bx && x<=ex); 219 return (x>=bx && x<=ex);
217 } 220 }
218 return (((y>sb[1] && y<se[1]) || (y==se[1] && x<=se[0])) || \ 221 return (((y>sel.b[1] && y<sel.e[1]) || (y==sel.e[1] && x<=sel.e[0])) || \
219 (y==sb[1] && x>=sb[0] && (x<=se[0] || sb[1]!=se[1]))); 222 (y==sel.b[1] && x>=sel.b[0] && (x<=sel.e[0] || sel.b[1]!=sel.e[1])));
220} 223}
221 224
222static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) { 225static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
@@ -224,24 +227,24 @@ static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
224 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1; 227 *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
225 *x = e->xbutton.x/xw.cw; 228 *x = e->xbutton.x/xw.cw;
226 *y = e->xbutton.y/xw.ch; 229 *y = e->xbutton.y/xw.ch;
227 sb[0] = selby<seley?selbx:selex; 230 sel.b[0] = sel.by<sel.ey?sel.bx:sel.ex;
228 sb[1] = MIN(selby, seley); 231 sel.b[1] = MIN(sel.by, sel.ey);
229 se[0] = selby<seley?selex:selbx; 232 sel.e[0] = sel.by<sel.ey?sel.ex:sel.bx;
230 se[1] = MAX(selby, seley); 233 sel.e[1] = MAX(sel.by, sel.ey);
231} 234}
232 235
233static void bpress(XEvent *e) { 236static void bpress(XEvent *e) {
234 selmode = 1; 237 sel.mode = 1;
235 selex = selbx = e->xbutton.x/xw.cw; 238 sel.ex = sel.bx = e->xbutton.x/xw.cw;
236 seley = selby = e->xbutton.y/xw.ch; 239 sel.ey = sel.by = e->xbutton.y/xw.ch;
237} 240}
238 241
239static char *getseltext() { 242static char *getseltext() {
240 char *str, *ptr; 243 char *str, *ptr;
241 int ls, x, y, sz; 244 int ls, x, y, sz;
242 if(selbx==-1) 245 if(sel.bx==-1)
243 return NULL; 246 return NULL;
244 sz = ((xw.w/xw.ch) * (se[1]-sb[1]+2)); 247 sz = ((xw.w/xw.ch) * (sel.e[1]-sel.b[1]+2));
245 ptr = str = malloc (sz); 248 ptr = str = malloc (sz);
246 for(y = 0; y < term.row; y++) { 249 for(y = 0; y < term.row; y++) {
247 for(x = 0; x < term.col; x++) { 250 for(x = 0; x < term.col; x++) {
@@ -259,29 +262,29 @@ static char *getseltext() {
259 return str; 262 return str;
260} 263}
261 264
262static void clipboard_copy(const char *str) { 265static void clipboard_copy(char *str) {
263 free((void *)clipboard); 266 free(sel.clip);
264 clipboard = str; 267 sel.clip = str;
265} 268}
266 269
267static void clipboard_paste() { 270static void clipboard_paste() {
268 if(clipboard) 271 if(sel.clip)
269 ttywrite(clipboard, strlen(clipboard)); 272 ttywrite(sel.clip, strlen(sel.clip));
270} 273}
271 274
272// TODO: doubleclick to select word 275// TODO: doubleclick to select word
273static void brelease(XEvent *e) { 276static void brelease(XEvent *e) {
274 int b; 277 int b;
275 selmode = 0; 278 sel.mode = 0;
276 getbuttoninfo(e, &b, &selex, &seley); 279 getbuttoninfo(e, &b, &sel.ex, &sel.ey);
277 if(b==4) 280 if(b==4)
278 tscrollup(1); 281 tscrollup(1);
279 else 282 else
280 if(b==5) 283 if(b==5)
281 tscrolldown(1); 284 tscrolldown(1);
282 else 285 else
283 if(selbx==selex && selby==seley) { 286 if(sel.bx==sel.ex && sel.by==sel.ey) {
284 selbx = -1; 287 sel.bx = -1;
285 if(b==2) 288 if(b==2)
286 clipboard_paste(); 289 clipboard_paste();
287 } else { 290 } else {
@@ -292,8 +295,8 @@ static void brelease(XEvent *e) {
292} 295}
293 296
294static void bmotion(XEvent *e) { 297static void bmotion(XEvent *e) {
295 if (selmode) { 298 if (sel.mode) {
296 getbuttoninfo(e, NULL, &selex, &seley); 299 getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
297 draw(1); 300 draw(1);
298 } 301 }
299} 302}
@@ -784,7 +787,6 @@ csihandle(void) {
784 case 2: /* all */ 787 case 2: /* all */
785 tclearregion(0, 0, term.col-1, term.row-1); 788 tclearregion(0, 0, term.col-1, term.row-1);
786 break; 789 break;
787 case 3: /* XXX: erase saved lines (xterm) */
788 default: 790 default:
789 goto unknown; 791 goto unknown;
790 } 792 }
@@ -1025,7 +1027,7 @@ tputc(char c) {
1025 term.mode &= ~MODE_APPKEYPAD; 1027 term.mode &= ~MODE_APPKEYPAD;
1026 term.esc = 0; 1028 term.esc = 0;
1027 break; 1029 break;
1028 case '7': /* DECSC -- Save Cursor*/ 1030 case '7': /* DECSC -- Save Cursor */
1029 tcursor(CURSOR_SAVE); 1031 tcursor(CURSOR_SAVE);
1030 term.esc = 0; 1032 term.esc = 0;
1031 break; 1033 break;
@@ -1096,7 +1098,7 @@ tresize(int col, int row) {
1096 1098
1097 /* resize to new height */ 1099 /* resize to new height */
1098 term.line = realloc(term.line, row * sizeof(Line)); 1100 term.line = realloc(term.line, row * sizeof(Line));
1099 term.alt = realloc(term.alt, row * sizeof(Line)); 1101 term.alt = realloc(term.alt, row * sizeof(Line));
1100 1102
1101 /* resize each row to new width, zero-pad if needed */ 1103 /* resize each row to new width, zero-pad if needed */
1102 for(i = 0; i < minrow; i++) { 1104 for(i = 0; i < minrow; i++) {
@@ -1312,7 +1314,7 @@ draw(int redraw_all) {
1312 i = ox = 0; 1314 i = ox = 0;
1313 for(x = 0; x < term.col; x++) { 1315 for(x = 0; x < term.col; x++) {
1314 new = term.line[y][x]; 1316 new = term.line[y][x];
1315 if(selbx!=-1 && new.c && selected(x, y)) 1317 if(sel.bx!=-1 && new.c && selected(x, y))
1316 new.mode ^= ATTR_REVERSE; 1318 new.mode ^= ATTR_REVERSE;
1317 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) || 1319 if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
1318 i >= DRAW_BUF_SIZ)) { 1320 i >= DRAW_BUF_SIZ)) {