Changeset 2568c94 in mainline for uspace/app/edit/edit.c
- Timestamp:
- 2012-08-17T17:15:19Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1789023
- Parents:
- 4f351432 (diff), 01e397ac (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/edit/edit.c
r4f351432 r2568c94 1 1 /* 2 2 * Copyright (c) 2009 Jiri Svoboda 3 * Copyright (c) 2012 Martin Sucha 3 4 * All rights reserved. 4 5 * … … 49 50 50 51 #include "sheet.h" 52 #include "search.h" 51 53 52 54 enum redraw_flags { … … 83 85 */ 84 86 int ideal_column; 87 88 char *previous_search; 89 bool previous_search_reverse; 85 90 } pane_t; 86 91 … … 140 145 static void delete_char_after(void); 141 146 static void caret_update(void); 142 static void caret_move(int drow, int dcolumn, enum dir_spec align_dir); 143 static void caret_move_word_left(void); 144 static void caret_move_word_right(void); 145 static void caret_move_to_line(int row); 147 static void caret_move_relative(int drow, int dcolumn, enum dir_spec align_dir, bool select); 148 static void caret_move_absolute(int row, int column, enum dir_spec align_dir, bool select); 149 static void caret_move(spt_t spt, bool select, bool update_ideal_column); 150 static void caret_move_word_left(bool select); 151 static void caret_move_word_right(bool select); 146 152 static void caret_go_to_line_ask(void); 147 153 … … 149 155 static void selection_sel_all(void); 150 156 static void selection_sel_range(spt_t pa, spt_t pb); 151 static void selection_sel_prev_word(void);152 static void selection_sel_next_word(void);153 157 static void selection_get_points(spt_t *pa, spt_t *pb); 154 158 static void selection_delete(void); 155 159 static void selection_copy(void); 156 160 static void insert_clipboard_data(void); 161 162 static void search(char *pattern, bool reverse); 163 static void search_prompt(bool reverse); 164 static void search_repeat(void); 157 165 158 166 static void pt_get_sof(spt_t *pt); … … 163 171 static bool pt_is_delimiter(spt_t *pt); 164 172 static bool pt_is_punctuation(spt_t *pt); 173 static spt_t pt_find_word_left(spt_t spt); 174 static spt_t pt_find_word_left(spt_t spt); 175 165 176 static int tag_cmp(tag_t const *a, tag_t const *b); 166 177 static int spt_cmp(spt_t const *a, spt_t const *b); … … 173 184 { 174 185 kbd_event_t ev; 175 coord_t coord;176 186 bool new_file; 177 187 int rc; 178 179 spt_t pt;180 188 181 189 con = console_init(stdin, stdout); … … 197 205 198 206 /* Place caret at the beginning of file. */ 199 coord.row = coord.column = 1;200 sheet_get_cell_pt(doc.sh, &coord, dir_before, &pt);201 sheet_place_tag(doc.sh, & pt, &pane.caret_pos);202 pane.ideal_column = coord.column;207 spt_t sof; 208 pt_get_sof(&sof); 209 sheet_place_tag(doc.sh, &sof, &pane.caret_pos); 210 pane.ideal_column = 1; 203 211 204 212 if (argc == 2) { … … 216 224 new_file = true; 217 225 226 /* Place selection start tag. */ 227 sheet_place_tag(doc.sh, &sof, &pane.sel_start); 228 218 229 /* Move to beginning of file. */ 219 caret_move(-ED_INFTY, -ED_INFTY, dir_before); 220 221 /* Place selection start tag. */ 222 tag_get_pt(&pane.caret_pos, &pt); 223 sheet_place_tag(doc.sh, &pt, &pane.sel_start); 230 pt_get_sof(&sof); 231 caret_move(sof, true, true); 224 232 225 233 /* Initial display */ … … 369 377 static void key_handle_ctrl(kbd_event_t const *ev) 370 378 { 379 spt_t pt; 371 380 switch (ev->key) { 372 381 case KC_Q: … … 400 409 selection_sel_all(); 401 410 break; 402 case KC_W:403 if (selection_active())404 break;405 selection_sel_prev_word();406 selection_delete();407 break;408 411 case KC_RIGHT: 409 caret_move_word_right( );412 caret_move_word_right(false); 410 413 break; 411 414 case KC_LEFT: 412 caret_move_word_left( );415 caret_move_word_left(false); 413 416 break; 414 417 case KC_L: 415 418 caret_go_to_line_ask(); 416 419 break; 420 case KC_F: 421 search_prompt(false); 422 break; 423 case KC_N: 424 search_repeat(); 425 break; 426 case KC_HOME: 427 pt_get_sof(&pt); 428 caret_move(pt, false, true); 429 break; 430 case KC_END: 431 pt_get_eof(&pt); 432 caret_move(pt, false, true); 433 break; 417 434 default: 418 435 break; … … 422 439 static void key_handle_shift_ctrl(kbd_event_t const *ev) 423 440 { 441 spt_t pt; 424 442 switch(ev->key) { 425 443 case KC_LEFT: 426 selection_sel_prev_word();444 caret_move_word_left(true); 427 445 break; 428 446 case KC_RIGHT: 429 selection_sel_next_word(); 447 caret_move_word_right(true); 448 break; 449 case KC_F: 450 search_prompt(true); 451 break; 452 case KC_HOME: 453 pt_get_sof(&pt); 454 caret_move(pt, true, true); 455 break; 456 case KC_END: 457 pt_get_eof(&pt); 458 caret_move(pt, true, true); 430 459 break; 431 460 default: … … 435 464 436 465 /** Move caret while preserving or resetting selection. */ 437 static void caret_movement(int drow, int dcolumn, enum dir_spec align_dir, 438 bool select) 439 { 440 spt_t pt; 441 spt_t caret_pt; 466 static void caret_move(spt_t new_caret_pt, bool select, bool update_ideal_column) 467 { 468 spt_t old_caret_pt, old_sel_pt; 442 469 coord_t c_old, c_new; 443 470 bool had_sel; 444 471 445 472 /* Check if we had selection before. */ 446 tag_get_pt(&pane.caret_pos, &caret_pt); 447 tag_get_pt(&pane.sel_start, &pt); 448 had_sel = !spt_equal(&caret_pt, &pt); 449 450 caret_move(drow, dcolumn, align_dir); 473 tag_get_pt(&pane.caret_pos, &old_caret_pt); 474 tag_get_pt(&pane.sel_start, &old_sel_pt); 475 had_sel = !spt_equal(&old_caret_pt, &old_sel_pt); 476 477 /* Place tag of the caret */ 478 sheet_remove_tag(doc.sh, &pane.caret_pos); 479 sheet_place_tag(doc.sh, &new_caret_pt, &pane.caret_pos); 451 480 452 481 if (select == false) { 453 482 /* Move sel_start to the same point as caret. */ 454 483 sheet_remove_tag(doc.sh, &pane.sel_start); 455 tag_get_pt(&pane.caret_pos, &pt);456 sheet_place_tag(doc.sh, &pt, &pane.sel_start);457 } 458 484 sheet_place_tag(doc.sh, &new_caret_pt, &pane.sel_start); 485 } 486 487 spt_get_coord(&new_caret_pt, &c_new); 459 488 if (select) { 460 tag_get_pt(&pane.caret_pos, &pt); 461 spt_get_coord(&caret_pt, &c_old); 462 spt_get_coord(&pt, &c_new); 489 spt_get_coord(&old_caret_pt, &c_old); 463 490 464 491 if (c_old.row == c_new.row) … … 471 498 pane.rflags |= REDRAW_TEXT; 472 499 } 500 501 if (update_ideal_column) 502 pane.ideal_column = c_new.column; 503 504 caret_update(); 473 505 } 474 506 … … 477 509 switch (key) { 478 510 case KC_LEFT: 479 caret_move ment(0, -1, dir_before, select);511 caret_move_relative(0, -1, dir_before, select); 480 512 break; 481 513 case KC_RIGHT: 482 caret_move ment(0, 0, dir_after, select);514 caret_move_relative(0, 0, dir_after, select); 483 515 break; 484 516 case KC_UP: 485 caret_move ment(-1, 0, dir_before, select);517 caret_move_relative(-1, 0, dir_before, select); 486 518 break; 487 519 case KC_DOWN: 488 caret_move ment(+1, 0, dir_before, select);520 caret_move_relative(+1, 0, dir_before, select); 489 521 break; 490 522 case KC_HOME: 491 caret_move ment(0, -ED_INFTY, dir_before, select);523 caret_move_relative(0, -ED_INFTY, dir_after, select); 492 524 break; 493 525 case KC_END: 494 caret_move ment(0, +ED_INFTY, dir_before, select);526 caret_move_relative(0, +ED_INFTY, dir_before, select); 495 527 break; 496 528 case KC_PAGE_UP: 497 caret_move ment(-pane.rows, 0, dir_before, select);529 caret_move_relative(-pane.rows, 0, dir_before, select); 498 530 break; 499 531 case KC_PAGE_DOWN: 500 caret_move ment(+pane.rows, 0, dir_before, select);532 caret_move_relative(+pane.rows, 0, dir_before, select); 501 533 break; 502 534 default: … … 1012 1044 } 1013 1045 1014 /** Change the caret position.1046 /** Relatively move caret position. 1015 1047 * 1016 1048 * Moves caret relatively to the current position. Looking at the first … … 1018 1050 * to a new character cell, and thus a new character. Then we either go to the 1019 1051 * point before the the character or after it, depending on @a align_dir. 1052 * 1053 * @param select true if the selection tag should stay where it is 1020 1054 */ 1021 static void caret_move(int drow, int dcolumn, enum dir_spec align_dir) 1055 static void caret_move_relative(int drow, int dcolumn, enum dir_spec align_dir, 1056 bool select) 1022 1057 { 1023 1058 spt_t pt; … … 1055 1090 */ 1056 1091 sheet_get_cell_pt(doc.sh, &coord, align_dir, &pt); 1057 sheet_remove_tag(doc.sh, &pane.caret_pos);1058 sheet_place_tag(doc.sh, &pt, &pane.caret_pos);1059 1092 1060 1093 /* For non-vertical movement set the new value for @c ideal_column. */ 1061 if (!pure_vertical) { 1062 spt_get_coord(&pt, &coord); 1063 pane.ideal_column = coord.column; 1064 } 1065 1066 caret_update(); 1067 } 1068 1069 static void caret_move_word_left(void) 1070 { 1094 caret_move(pt, select, !pure_vertical); 1095 } 1096 1097 /** Absolutely move caret position. 1098 * 1099 * Moves caret to a specified position. We get to a new character cell, and 1100 * thus a new character. Then we either go to the point before the the character 1101 * or after it, depending on @a align_dir. 1102 * 1103 * @param select true if the selection tag should stay where it is 1104 */ 1105 static void caret_move_absolute(int row, int column, enum dir_spec align_dir, 1106 bool select) 1107 { 1108 coord_t coord; 1109 coord.row = row; 1110 coord.column = column; 1111 1071 1112 spt_t pt; 1072 1113 sheet_get_cell_pt(doc.sh, &coord, align_dir, &pt); 1114 1115 caret_move(pt, select, true); 1116 } 1117 1118 /** Find beginning of a word to the left of spt */ 1119 static spt_t pt_find_word_left(spt_t spt) 1120 { 1073 1121 do { 1074 caret_move(0, -1, dir_before); 1075 1076 tag_get_pt(&pane.caret_pos, &pt); 1077 1078 sheet_remove_tag(doc.sh, &pane.sel_start); 1079 sheet_place_tag(doc.sh, &pt, &pane.sel_start); 1080 } while (!pt_is_word_beginning(&pt)); 1081 1082 pane.rflags |= REDRAW_TEXT; 1083 } 1084 1085 static void caret_move_word_right(void) 1122 spt_prev_char(spt, &spt); 1123 } while (!pt_is_word_beginning(&spt)); 1124 return spt; 1125 } 1126 1127 /** Find beginning of a word to the right of spt */ 1128 static spt_t pt_find_word_right(spt_t spt) 1129 { 1130 do { 1131 spt_next_char(spt, &spt); 1132 } while (!pt_is_word_beginning(&spt)); 1133 return spt; 1134 } 1135 1136 static void caret_move_word_left(bool select) 1086 1137 { 1087 1138 spt_t pt; 1088 1089 do { 1090 caret_move(0, 0, dir_after); 1091 1092 tag_get_pt(&pane.caret_pos, &pt); 1093 1094 sheet_remove_tag(doc.sh, &pane.sel_start); 1095 sheet_place_tag(doc.sh, &pt, &pane.sel_start); 1096 } while (!pt_is_word_beginning(&pt)); 1097 1098 pane.rflags |= REDRAW_TEXT; 1099 } 1100 1101 /** Change the caret position to a beginning of a given line 1102 */ 1103 static void caret_move_to_line(int row) 1139 tag_get_pt(&pane.caret_pos, &pt); 1140 spt_t word_left = pt_find_word_left(pt); 1141 caret_move(word_left, select, true); 1142 } 1143 1144 static void caret_move_word_right(bool select) 1104 1145 { 1105 1146 spt_t pt; 1106 coord_t coord;1107 1108 1147 tag_get_pt(&pane.caret_pos, &pt); 1109 spt_get_coord(&pt, &coord); 1110 1111 caret_movement(row - coord.row, 0, dir_before, false); 1148 spt_t word_right = pt_find_word_right(pt); 1149 caret_move(word_right, select, true); 1112 1150 } 1113 1151 … … 1126 1164 int line = strtol(sline, &endptr, 10); 1127 1165 if (*endptr != '\0') { 1166 free(sline); 1128 1167 status_display("Invalid number entered."); 1129 1168 return; 1130 1169 } 1131 1132 caret_move_to_line(line); 1133 } 1134 1170 free(sline); 1171 1172 caret_move_absolute(line, pane.ideal_column, dir_before, false); 1173 } 1174 1175 /* Search operations */ 1176 static int search_spt_producer(void *data, wchar_t *ret) 1177 { 1178 assert(data != NULL); 1179 assert(ret != NULL); 1180 spt_t *spt = data; 1181 *ret = spt_next_char(*spt, spt); 1182 return EOK; 1183 } 1184 1185 static int search_spt_reverse_producer(void *data, wchar_t *ret) 1186 { 1187 assert(data != NULL); 1188 assert(ret != NULL); 1189 spt_t *spt = data; 1190 *ret = spt_prev_char(*spt, spt); 1191 return EOK; 1192 } 1193 1194 static int search_spt_mark(void *data, void **mark) 1195 { 1196 assert(data != NULL); 1197 assert(mark != NULL); 1198 spt_t *spt = data; 1199 spt_t *new = calloc(1, sizeof(spt_t)); 1200 *mark = new; 1201 if (new == NULL) 1202 return ENOMEM; 1203 *new = *spt; 1204 return EOK; 1205 } 1206 1207 static void search_spt_mark_free(void *data) 1208 { 1209 free(data); 1210 } 1211 1212 static search_ops_t search_spt_ops = { 1213 .equals = char_exact_equals, 1214 .producer = search_spt_producer, 1215 .mark = search_spt_mark, 1216 .mark_free = search_spt_mark_free, 1217 }; 1218 1219 static search_ops_t search_spt_reverse_ops = { 1220 .equals = char_exact_equals, 1221 .producer = search_spt_reverse_producer, 1222 .mark = search_spt_mark, 1223 .mark_free = search_spt_mark_free, 1224 }; 1225 1226 /** Ask for line and go to it. */ 1227 static void search_prompt(bool reverse) 1228 { 1229 char *pattern; 1230 1231 const char *prompt_text = "Find next"; 1232 if (reverse) 1233 prompt_text = "Find previous"; 1234 1235 const char *default_value = ""; 1236 if (pane.previous_search) 1237 default_value = pane.previous_search; 1238 1239 pattern = prompt(prompt_text, default_value); 1240 if (pattern == NULL) { 1241 status_display("Search cancelled."); 1242 return; 1243 } 1244 1245 if (pane.previous_search) 1246 free(pane.previous_search); 1247 pane.previous_search = pattern; 1248 pane.previous_search_reverse = reverse; 1249 1250 search(pattern, reverse); 1251 } 1252 1253 static void search_repeat(void) 1254 { 1255 if (pane.previous_search == NULL) { 1256 status_display("No previous search to repeat."); 1257 return; 1258 } 1259 1260 search(pane.previous_search, pane.previous_search_reverse); 1261 } 1262 1263 static void search(char *pattern, bool reverse) 1264 { 1265 status_display("Searching..."); 1266 1267 spt_t sp, producer_pos; 1268 tag_get_pt(&pane.caret_pos, &sp); 1269 1270 /* Start searching on the position before/after caret */ 1271 if (!reverse) { 1272 spt_next_char(sp, &sp); 1273 } 1274 else { 1275 spt_prev_char(sp, &sp); 1276 } 1277 producer_pos = sp; 1278 1279 search_ops_t ops = search_spt_ops; 1280 if (reverse) 1281 ops = search_spt_reverse_ops; 1282 1283 search_t *search = search_init(pattern, &producer_pos, ops, reverse); 1284 if (search == NULL) { 1285 status_display("Failed initializing search."); 1286 return; 1287 } 1288 1289 match_t match; 1290 int rc = search_next_match(search, &match); 1291 if (rc != EOK) { 1292 status_display("Failed searching."); 1293 search_fini(search); 1294 } 1295 1296 if (match.end) { 1297 status_display("Match found."); 1298 assert(match.end != NULL); 1299 spt_t *end = match.end; 1300 caret_move(*end, false, true); 1301 while (match.length > 0) { 1302 match.length--; 1303 if (reverse) { 1304 spt_next_char(*end, end); 1305 } 1306 else { 1307 spt_prev_char(*end, end); 1308 } 1309 } 1310 caret_move(*end, true, true); 1311 free(end); 1312 } 1313 else { 1314 status_display("Not found."); 1315 } 1316 1317 search_fini(search); 1318 } 1135 1319 1136 1320 /** Check for non-empty selection. */ … … 1202 1386 pane.rflags |= REDRAW_TEXT; 1203 1387 caret_update(); 1204 }1205 1206 /** Add the previous word to the selection */1207 static void selection_sel_prev_word(void)1208 {1209 spt_t cpt, wpt, spt, ept;1210 1211 selection_get_points(&spt, &ept);1212 1213 tag_get_pt(&pane.caret_pos, &cpt);1214 caret_move_word_left();1215 tag_get_pt(&pane.caret_pos, &wpt);1216 1217 if (spt_cmp(&spt, &cpt) == 0)1218 selection_sel_range(ept, wpt);1219 else1220 selection_sel_range(spt, wpt);1221 }1222 1223 /** Add the next word to the selection */1224 static void selection_sel_next_word(void)1225 {1226 spt_t cpt, wpt, spt, ept;1227 1228 selection_get_points(&spt, &ept);1229 1230 tag_get_pt(&pane.caret_pos, &cpt);1231 caret_move_word_right();1232 tag_get_pt(&pane.caret_pos, &wpt);1233 1234 if (spt_cmp(&ept, &cpt) == 0)1235 selection_sel_range(spt, wpt);1236 else1237 selection_sel_range(ept, wpt);1238 1388 } 1239 1389
Note:
See TracChangeset
for help on using the changeset viewer.