Changeset 850fd32 in mainline for uspace/dist/src/c/demos/edit/edit.c
- Timestamp:
- 2018-03-11T01:21:19Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 84239b1, f0e825d
- Parents:
- 338d54a7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/dist/src/c/demos/edit/edit.c
r338d54a7 r850fd32 48 48 #include <macros.h> 49 49 #include <clipboard.h> 50 #include <types/common.h> 50 51 51 52 #include "sheet.h" … … 131 132 static void pos_handle(pos_event_t *ev); 132 133 133 static int file_save(char const *fname);134 static errno_t file_save(char const *fname); 134 135 static void file_save_as(void); 135 static int file_insert(char *fname);136 static int file_save_range(char const *fname, spt_t const *spos,136 static errno_t file_insert(char *fname); 137 static errno_t file_save_range(char const *fname, spt_t const *spos, 137 138 spt_t const *epos); 138 139 static char *range_get_str(spt_t const *spos, spt_t const *epos); … … 577 578 578 579 /** Save the document. */ 579 static int file_save(char const *fname)580 static errno_t file_save(char const *fname) 580 581 { 581 582 spt_t sp, ep; … … 654 655 655 656 /* Handle key press. */ 656 if (((kev->mods & KM_ALT) == 0) && 657 (kev->mods & KM_CTRL) != 0) { 658 ; 659 } else if ((kev->mods & (KM_CTRL | KM_ALT)) == 0) { 657 if ((kev->mods & (KM_CTRL | KM_ALT)) == 0) { 660 658 switch (kev->key) { 661 659 case KC_ESCAPE: … … 696 694 * of the caret. 697 695 */ 698 static int file_insert(char *fname)696 static errno_t file_insert(char *fname) 699 697 { 700 698 FILE *f; … … 734 732 735 733 /** Save a range of text into a file. */ 736 static int file_save_range(char const *fname, spt_t const *spos,734 static errno_t file_save_range(char const *fname, spt_t const *spos, 737 735 spt_t const *epos) 738 736 { … … 760 758 } while (!spt_equal(&bep, epos)); 761 759 762 if (fclose(f) != EOK)760 if (fclose(f) < 0) 763 761 return EIO; 764 762 … … 954 952 coord_t coord; 955 953 int last_row; 954 char *fname; 955 char *p; 956 char *text; 957 size_t n; 958 int pos; 959 size_t nextra; 960 size_t fnw; 956 961 957 962 tag_get_pt(&pane.caret_pos, &caret_pt); … … 960 965 sheet_get_num_rows(doc.sh, &last_row); 961 966 962 const char *fname = (doc.file_name != NULL) ? doc.file_name : "<unnamed>"; 967 if (doc.file_name != NULL) { 968 /* Remove directory component */ 969 p = str_rchr(doc.file_name, '/'); 970 if (p != NULL) 971 fname = str_dup(p + 1); 972 else 973 fname = str_dup(doc.file_name); 974 } else { 975 fname = str_dup("<unnamed>"); 976 } 977 978 if (fname == NULL) 979 return; 963 980 964 981 console_set_pos(con, 0, scr_rows - 1); 965 982 console_set_style(con, STYLE_INVERTED); 966 int n = printf(" %d, %d (%d): File '%s'. Ctrl-Q Quit Ctrl-S Save " 967 "Ctrl-E Save As", coord.row, coord.column, last_row, fname); 968 969 int pos = scr_columns - 1 - n; 983 984 /* 985 * Make sure the status fits on the screen. This loop should 986 * be executed at most twice. 987 */ 988 while (true) { 989 int rc = asprintf(&text, " %d, %d (%d): File '%s'. Ctrl-Q Quit Ctrl-S Save " 990 "Ctrl-E Save As", coord.row, coord.column, last_row, fname); 991 if (rc < 0) { 992 n = 0; 993 goto finish; 994 } 995 996 /* If it already fits, we're done */ 997 n = str_width(text); 998 if (n <= scr_columns - 2) 999 break; 1000 1001 /* Compute number of excess characters */ 1002 nextra = n - (scr_columns - 2); 1003 /** With of the file name part */ 1004 fnw = str_width(fname); 1005 1006 /* 1007 * If reducing file name to two characters '..' won't help, 1008 * just give up and print a blank status. 1009 */ 1010 if (nextra > fnw - 2) 1011 goto finish; 1012 1013 /* Compute position where we overwrite with '..\0' */ 1014 if (fnw >= nextra + 2) { 1015 p = fname + str_lsize(fname, fnw - nextra - 2); 1016 } else { 1017 p = fname; 1018 } 1019 1020 /* Shorten the string */ 1021 p[0] = p[1] = '.'; 1022 p[2] = '\0'; 1023 1024 /* Need to format the string once more. */ 1025 free(text); 1026 } 1027 1028 printf("%s", text); 1029 free(text); 1030 free(fname); 1031 finish: 1032 /* Fill the rest of the line */ 1033 pos = scr_columns - 1 - n; 970 1034 printf("%*s", pos, ""); 971 1035 console_flush(con); … … 1216 1280 1217 1281 /* Search operations */ 1218 static int search_spt_producer(void *data, wchar_t *ret)1282 static errno_t search_spt_producer(void *data, wchar_t *ret) 1219 1283 { 1220 1284 assert(data != NULL); … … 1225 1289 } 1226 1290 1227 static int search_spt_reverse_producer(void *data, wchar_t *ret)1291 static errno_t search_spt_reverse_producer(void *data, wchar_t *ret) 1228 1292 { 1229 1293 assert(data != NULL); … … 1234 1298 } 1235 1299 1236 static int search_spt_mark(void *data, void **mark)1300 static errno_t search_spt_mark(void *data, void **mark) 1237 1301 { 1238 1302 assert(data != NULL);
Note:
See TracChangeset
for help on using the changeset viewer.