@@ -160,20 +160,20 @@ void line_extend_text(line_t *line, int extend_len) {
160160}
161161
162162void line_insert_text (line_t * line , int col , char * text ) {
163- printf (">>>>>>>>>>>>>>>>line_insert_text\n" );
163+ // printf(">>>>>>>>>>>>>>>>line_insert_text\n");
164164 int start = line_get_col_index (line , col );
165165 int len = utf8_strlen (text );
166166 int len1 = strlen (text );
167- printf ("line_insert_text at col=%d start=%d len=%d len1=%d %s\n" , col , start ,
168- len , len1 , text );
167+ // printf("line_insert_text at col=%d start=%d len=%d len1=%d %s\n", col, start,
168+ // len, len1, text);
169169 if ((line -> actual + len1 ) > line -> available ) {
170170 printf ("line no available line->available=%d %d\n" , line -> available ,
171171 (line -> actual + len1 ));
172172 line_extend_text (line , line -> available * 2 );
173173 }
174- printf ("line current %s\n" , line -> texts );
174+ // printf("line current %s\n", line->texts);
175175 insert_string (line -> texts , line -> actual , start , text , len1 );
176- printf ("line after %s$$$\n" , line -> texts );
176+ // printf("line after %s$$$\n", line->texts);
177177 line -> count += len ;
178178 line -> actual += len1 ;
179179}
@@ -532,7 +532,7 @@ float gl_edit_get_cursor_x(edit_t *self) {
532532}
533533
534534float gl_edit_get_cursor_y (edit_t * self ) {
535- return self -> buffer -> cursor_y / self -> scale + self -> scroll_y ;
535+ return self -> buffer -> cursor_y / self -> scale + self -> scroll_y ;
536536}
537537
538538int gl_edit_get_line_count (edit_t * self ) { return self -> buffer -> line_count ; }
@@ -619,8 +619,8 @@ void gl_edit_mouse_event(edit_t *self, int action, float x, float y) {
619619 pos_to_cursor (self -> buffer , x - self -> scroll_x , y - self -> scroll_y );
620620 self -> select_end [0 ] = self -> buffer -> cursor_row ;
621621 self -> select_end [1 ] = self -> buffer -> cursor_col ;
622- printf ("start %d,%d end %d,%d\n" , self -> select_start [0 ],
623- self -> select_start [1 ], self -> select_end [0 ], self -> select_end [1 ]);
622+ // printf("start %d,%d end %d,%d\n", self->select_start[0],
623+ // self->select_start[1], self->select_end[0], self->select_end[1]);
624624 // printf("gl_get_selection %s\n", gl_get_selection(self));
625625 }
626626 self -> select_press = action ;
@@ -745,11 +745,9 @@ int gl_edit_get_selection_length(edit_t *self) {
745745 return total ;
746746}
747747
748- char * gl_get_selection (edit_t * self ) {
748+ void auto_set_text (edit_t * self ) {
749749 int total = gl_edit_get_selection_length (self );
750- int crlf_len = strlen (self -> crlf );
751750 buffer_t * buffer = self -> buffer ;
752-
753751 if (self -> select_text == NULL ) {
754752 self -> select_text = malloc (total * 2 );
755753 self -> select_text_avail = total * 2 ;
@@ -765,36 +763,40 @@ char *gl_get_selection(edit_t *self) {
765763 self -> select_text_avail = avail ;
766764 free (old_texts );
767765 }
768- int row_start = self -> select_start [0 ];
769- int row_end = self -> select_end [0 ];
770- if (row_start > row_end ) { // selection from end to start
771- row_start = self -> select_end [0 ];
772- row_end = self -> select_start [0 ];
766+ }
767+
768+ char * gl_edit_get_text_range (edit_t * self , int row_start , int col_start , int row_end ,
769+ int col_end ) {
770+ int crlf_len = strlen (self -> crlf );
771+ buffer_t * buffer = self -> buffer ;
772+ auto_set_text (self );
773+ int rstart = row_start ;
774+ int rend = row_end ;
775+ if (row_start > row_end ) {
776+ rstart = row_end ;
777+ rend = row_start ;
773778 }
774779 int pos = 0 ;
775- for (int i = row_start ; i <= row_end ; i ++ ) {
780+ for (int i = rstart ; i <= rend ; i ++ ) {
776781 line_t * line = buffer -> lines [i ];
777782 int start = 0 ;
778783 int end = line -> actual ;
779- // printf("self->select_start[0]=%d self->select_end[0]=%d\n",
780- // self->select_start[0], self->select_end[0]);
781-
782- if (self -> select_start [0 ] > self -> select_end [0 ]) {
783- if (i == row_start ) {
784- start = line_get_col_index (line , self -> select_end [1 ]);
784+ if (row_start > row_end ) {
785+ if (i == rstart ) {
786+ start = line_get_col_index (line , col_end );
785787 }
786- if (i == row_end ) {
787- end = line_get_col_index (line , self -> select_start [ 1 ] );
788+ if (i == rend ) {
789+ end = line_get_col_index (line , col_start );
788790 }
789791 } else {
790- if (i == row_start ) {
791- start = line_get_col_index (line , self -> select_start [ 1 ] );
792+ if (i == rstart ) {
793+ start = line_get_col_index (line , col_start );
792794 }
793- if (i == row_end ) {
794- end = line_get_col_index (line , self -> select_end [ 1 ] );
795+ if (i == rend ) {
796+ end = line_get_col_index (line , col_end );
795797 }
796798 }
797- if (self -> select_start [ 0 ] == self -> select_end [ 0 ] ) {
799+ if (row_start == row_end ) {
798800 if (start > end ) {
799801 int tmp = start ;
800802 start = end ;
@@ -807,17 +809,36 @@ char *gl_get_selection(edit_t *self) {
807809 pos += crlf_len ;
808810 }
809811 self -> select_text [pos - 1 ] = 0 ;
810- // printf("gl_get_selection %s\n",self->select_text);
811812 return self -> select_text ;
812813}
814+ int gl_edit_get_select_row_start (edit_t * self ){
815+ return self -> select_start [0 ];
816+ }
817+
818+ int gl_edit_get_select_row_end (edit_t * self ){
819+ return self -> select_end [0 ];
820+ }
821+
822+ int gl_edit_get_select_col_start (edit_t * self ){
823+ return self -> select_start [1 ];
824+ }
825+
826+ int gl_edit_get_select_col_end (edit_t * self ){
827+ return self -> select_end [1 ];
828+ }
829+
830+ char * gl_edit_get_selection (edit_t * self ) {
831+ auto_set_text (self );
832+ return gl_edit_get_text_range (self ,self -> select_start [0 ] ,self -> select_start [1 ],self -> select_end [0 ],self -> select_end [1 ] );
833+ }
813834
814835// events
815836void gl_edit_char_event (edit_t * self , int ch , int mods ) {
816837 if (self != NULL ) {
817838 // int len = utf8_surrogate_len(input);
818839 char buf [10 ] = {0 };
819840 utf8_encode (buf , ch );
820- printf ("gl_edit_char_event %s\n" , buf );
841+ // printf("gl_edit_char_event %s\n", buf);
821842 buffer_t * buffer = self -> buffer ;
822843 line_insert_text (buffer -> lines [buffer -> cursor_row ], buffer -> cursor_col ,
823844 buf );
@@ -828,7 +849,7 @@ void gl_edit_char_event(edit_t *self, int ch, int mods) {
828849
829850void gl_edit_key_event (edit_t * self , int key , int scancode , int action ,
830851 int mods ) {
831- printf ("gl_edit_key_event %d %d\n" , key , action );
852+ // printf("gl_edit_key_event %d %d\n", key, action);
832853 if (self != NULL && (action == 1 || action == 2 )) {
833854 buffer_t * buffer = self -> buffer ;
834855 if (key == 263 ) { // left
@@ -870,15 +891,15 @@ void gl_edit_key_event(edit_t *self, int key, int scancode, int action,
870891 }
871892 update_cursor_pos (buffer );
872893 }
873- if (buffer -> cursor_row < buffer -> line_count ) {
894+ /* if (buffer->cursor_row < buffer->line_count) {
874895 printf("row=%d col=%d line_cols=%d count=%d actual=%d text=",
875896 buffer->cursor_row, buffer->cursor_col,
876897 buffer->lines[buffer->cursor_row]->count, buffer->line_count,
877898 buffer->lines[buffer->cursor_row]->actual);
878899 // print_string(buffer->lines[buffer->cursor_row]->texts,
879900 // buffer->lines[buffer->cursor_row]->actual);
880901 // printf("\n");
881- }
902+ }*/
882903 }
883904}
884905
@@ -1023,9 +1044,7 @@ edit_t *gl_new_edit(int shader, float w, float h, float width, float height) {
10231044 return self ;
10241045}
10251046
1026- font_t * gl_edit_get_font (edit_t * self ){
1027- return self -> font ;
1028- }
1047+ font_t * gl_edit_get_font (edit_t * self ) { return self -> font ; }
10291048void gl_resize_edit_window (edit_t * self , float width , float height ) {
10301049 glUseProgram (self -> mvp .shader );
10311050 mat4_set_orthographic (& self -> mvp .projection , 0 , width * self -> scale ,
@@ -1095,8 +1114,8 @@ char *gl_get_edit_text(edit_t *self) {
10951114 return self -> texts ;
10961115}
10971116
1098- float gl_edit_measure_text (edit_t * self ){
1099- if (self -> texts == NULL ){
1117+ float gl_edit_measure_text (edit_t * self ) {
1118+ if (self -> texts == NULL ) {
11001119 gl_get_edit_text (self );
11011120 }
11021121 return measure_text (self -> buffer -> font , self -> texts , strlen (self -> texts ));
0 commit comments