📄 textsw_file.c
字号:
}extern inttextsw_append_file_name(abstract, name) Textsw abstract; char *name;/* Returns 0 iff editing a file and name could be successfully acquired. */{ Textsw_folio textsw = FOLIO_FOR_VIEW(VIEW_ABS_TO_REP(abstract)); char *internal_name; int result; result = textsw_file_name(textsw, &internal_name); if (result == 0) (void) strcat(name, internal_name); return(result);}/* =================================================================== * * Misc. file system manipulation utilities * * =================================================================== *//* Returns 0 iff change directory succeeded. */static inttextsw_change_directory(textsw, filename, might_not_be_dir, locx, locy) Textsw_folio textsw; char *filename; int might_not_be_dir; int locx, locy;{ char *sys_msg; char *full_pathname; char alert_msg[MAXNAMLEN+100]; struct stat stat_buf; int result = 0; Event event; errno = 0; if (stat(filename, &stat_buf) < 0) { result = -1; goto Error; } if ((stat_buf.st_mode&S_IFMT) != S_IFDIR) { if (might_not_be_dir) return(-2); } if (chdir(filename) < 0) { result = errno; goto Error; } textsw_notify((Textsw_view)textsw, /* Cast is for lint */ TEXTSW_ACTION_CHANGED_DIRECTORY, filename, 0); return(result);Error: full_pathname = textsw_full_pathname(filename); (void) sprintf(alert_msg, "Unable to %s:", (might_not_be_dir ? "access file" : "cd to directory")); sys_msg = (errno > 0 && errno < sys_nerr) ? sys_errlist[errno] : NULL; (void) alert_prompt( (Frame)window_get(VIEW_FROM_FOLIO_OR_VIEW(textsw), WIN_OWNER), &event, ALERT_BUTTON_YES, "Continue", ALERT_TRIGGER, ACTION_STOP, ALERT_MESSAGE_STRINGS, alert_msg, full_pathname, sys_msg, 0, 0); free(full_pathname); return(result);}/* Returns 0 iff change directory succeeded. */pkg_private inttextsw_change_directory_quietly(textsw, filename, err_msgs, might_not_be_dir, locx, locy) Textsw_folio textsw; char *filename, *err_msgs; int might_not_be_dir; int locx, locy;{ char *sys_msg; char *full_pathname; struct stat stat_buf; int result = 0; errno = 0; if (stat(filename, &stat_buf) < 0) { result = -1; goto Error; } if ((stat_buf.st_mode&S_IFMT) != S_IFDIR) { if (might_not_be_dir) return(-2); } if (chdir(filename) < 0) { result = errno; goto Error; } textsw_notify((Textsw_view)textsw, /* Cast is for lint */ TEXTSW_ACTION_CHANGED_DIRECTORY, filename, 0); return(result);Error: full_pathname = textsw_full_pathname(filename); (void) sprintf(err_msgs, "Cannot %s '%s': ", (might_not_be_dir ? "access file" : "cd to directory"), full_pathname); (void) sprintf(err_msgs, "Cannot %s", (might_not_be_dir ? "access file: " : "cd to directory: ")); free(full_pathname); sys_msg = (errno > 0 && errno < sys_nerr) ? sys_errlist[errno] : NULL; if (sys_msg) strcat(err_msgs, sys_msg); return(result);}pkg_private Es_statustextsw_checkpoint_internal(folio) Textsw_folio folio;{ Es_handle cp_file; Es_status result; if (!folio->checkpoint_name) { char *name; if (textsw_file_name(folio, &name) != 0) return(ES_CANNOT_GET_NAME); if ((folio->checkpoint_name = (char *)malloc(MAXNAMLEN)) == 0) return(ES_CANNOT_GET_NAME); (void) sprintf(folio->checkpoint_name, "%s%%%%", name); } cp_file = es_file_create(folio->checkpoint_name, ES_OPT_APPEND, &result); if (!cp_file) { /* BUG ALERT! For now, don't look at result. */ return(ES_CANNOT_OPEN_OUTPUT); } result = es_copy(folio->views->esh, cp_file, TRUE); es_destroy(cp_file); return(result);}/* * If the number of edits since the last checkpoint has exceeded the * checkpoint frequency, take a checkpoint. * Return ES_SUCCESS if we do the checkpoint. */pkg_private Es_statustextsw_checkpoint(folio) Textsw_folio folio;{ int edit_number = (int)LINT_CAST( ev_get((Ev_handle)folio->views, EV_CHAIN_EDIT_NUMBER)); Es_status result = ES_DID_NOT_CHECKPOINT; if (folio->state & TXTSW_IN_NOTIFY_PROC || folio->checkpoint_frequency <= 0) return(result); if ((edit_number / folio->checkpoint_frequency) > folio->checkpoint_number) { /* do the checkpoint */ result = textsw_checkpoint_internal(folio); if (result == ES_SUCCESS) { folio->checkpoint_number++; } } return(result);}pkg_private voidtextsw_empty_document(abstract, ie) Textsw abstract; Event *ie;{ register Textsw_view view = VIEW_ABS_TO_REP(abstract); register Textsw_folio textsw = FOLIO_FOR_VIEW(view); int has_been_modified = textsw_has_been_modified(abstract); int number_of_resets = 0; int is_cmdtool = (textsw->state & TXTSW_NO_RESET_TO_SCRATCH); int result; Event event; if (!textsw_menu_style_already_gotten) { textsw_menu_style = textsw_get_menu_style_internal(textsw); textsw_menu_style_already_gotten = 1; } if (textsw_menu_style == TEXTSW_STYLE_ORGANIZED) { if (has_been_modified) { result = alert_prompt( (Frame)window_get(WINDOW_FROM_VIEW(view), WIN_OWNER), &event, ALERT_MESSAGE_STRINGS, "The text has been edited.", "Empty Document will discard these edits. Please confirm.", 0, ALERT_BUTTON_YES, "Confirm, discard edits", ALERT_BUTTON_NO, "Cancel", 0); if (result != ALERT_YES) return; textsw_reset(abstract, ie->ie_locx, ie->ie_locy); number_of_resets++; } } else { if (textsw_has_been_modified(abstract)) { result = alert_prompt( (Frame)window_get( WINDOW_FROM_VIEW(view), WIN_OWNER), &event, ALERT_MESSAGE_STRINGS, "The text has been edited.", "Reset will discard these edits. Please confirm.", 0, ALERT_BUTTON_YES, "Confirm, discard edits", ALERT_BUTTON_NO, "Cancel", 0); if (result == ALERT_FAILED) return; if (result == ALERT_NO) return; } } if (is_cmdtool) { if ((has_been_modified) && (number_of_resets == 0)) textsw_reset(abstract, ie->ie_locx, ie->ie_locy); } else { textsw_reset(abstract, ie->ie_locx, ie->ie_locy); }}pkg_private voidtextsw_post_need_selection(abstract, ie) Textsw abstract; Event *ie;{ register Textsw_view view = VIEW_ABS_TO_REP(abstract); Event event; int result; result = alert_prompt( (Frame)window_get(WINDOW_FROM_VIEW(view), WIN_OWNER), &event, ALERT_MESSAGE_STRINGS, "Please select a file name and choose this option again.", 0, ALERT_BUTTON_YES, "Continue", ALERT_TRIGGER, ACTION_STOP, 0); if (result == ALERT_FAILED) return; if (result == ALERT_NO) return;}pkg_private voidtextsw_load_file_as_menu(abstract, ie) Textsw abstract; Event *ie;{ register Textsw_view view = VIEW_ABS_TO_REP(abstract); register Textsw_folio textsw = FOLIO_FOR_VIEW(view); Event event; int result, no_cd; int is_cmdtool = (textsw->state & TXTSW_NO_RESET_TO_SCRATCH); char msg[MAXNAMLEN+100]; if (textsw->state& TXTSW_NO_LOAD) { (void) alert_prompt( (Frame)window_get (VIEW_FROM_FOLIO_OR_VIEW(textsw), WIN_OWNER), &event, ALERT_BUTTON_YES, "Continue", ALERT_TRIGGER, ACTION_STOP, ALERT_MESSAGE_STRINGS, "Load File Operation Not Allowed", 0, 0); return; } if (textsw_has_been_modified(abstract)) { result = alert_prompt( (Frame)window_get(WINDOW_FROM_VIEW(view), WIN_OWNER), &event, ALERT_MESSAGE_STRINGS, "The text has been edited.", "Load File will discard these edits. Please confirm.", 0, ALERT_BUTTON_YES, "Confirm, discard edits", ALERT_BUTTON_NO, "Cancel", 0); if (result == ALERT_FAILED) return; if (result == ALERT_NO) return; } no_cd = ((textsw->state & TXTSW_NO_CD) || ((textsw->state & TXTSW_LOAD_CAN_CD) == 0)); if (textsw_load_selection(textsw, ie->ie_locx, ie->ie_locy, no_cd) == 0) { }}/* return 0 if textsw was readonly, 1 if should allow insert, accelerator not translated 2 shouldn't insert, accelerator translated*/pkg_private inttextsw_handle_esc_accelerators(abstract, ie) Textsw abstract; Event *ie;{ register Textsw_view view = VIEW_ABS_TO_REP(abstract); register Textsw_folio textsw = FOLIO_FOR_VIEW(view); char dummy[1024]; int length = es_get_length(textsw->views->esh); if (event_shift_is_down(ie)) { /* select & include current line */ unsigned span_to_beginning_flags = (EI_SPAN_LINE | EI_SPAN_LEFT_ONLY); unsigned span_line_flags = (EI_SPAN_LINE | EI_SPAN_RIGHT_ONLY); Ev_chain chain = textsw->views; Es_index first = 0; Es_index last_plus_one, insert_pos; textsw_flush_caches(view, TFC_STD); insert_pos = ev_get_insert(chain); if (insert_pos != 0) { /* first go left from insertion pt */ (void)ev_span(chain, insert_pos, &first, &last_plus_one, span_to_beginning_flags); } /* then go right to end of line from line beginning */ (void)ev_span(chain, first, &first, &last_plus_one, span_line_flags); /* check for no selection because at beginning of new line */ if ((first == last_plus_one) && (insert_pos != 0)) { insert_pos--; first = 0; last_plus_one = 0; if (insert_pos != 0) { /* first go left from insertion pt */ (void)ev_span(chain, insert_pos, &first, &last_plus_one, span_to_beginning_flags); } /* then go right to end of line from line beginning */ (void)ev_span(chain, first, &first, &last_plus_one, span_line_flags); } textsw_set_insert(textsw, last_plus_one); textsw_set_selection(abstract, first, last_plus_one, (EV_SEL_PRIMARY|EV_SEL_PD_PRIMARY)); (void)textsw_file_stuff(view, first, last_plus_one); } else if (0 == textsw_file_name(textsw, dummy)) { if (TXTSW_IS_READ_ONLY(textsw)) return (0); return (1); } else { /* select and load-file current line */ unsigned span_to_beginning_flags = (EI_SPAN_LINE | EI_SPAN_LEFT_ONLY); unsigned span_line_flags = (EI_SPAN_LINE | EI_SPAN_RIGHT_ONLY); unsigned span_document_flag = EI_SPAN_DOCUMENT; Ev_chain chain = textsw->views; Es_index first = 0, first_in_doc = 0; Es_index last_in_doc_plus_one, last_plus_one, insert_pos; Event event; int result; char filename[MAXNAMLEN]; textsw_flush_caches(view, TFC_STD); insert_pos = ev_get_insert(chain); if (insert_pos != 0) { /* first go left from insertion pt */ (void)ev_span(chain, insert_pos, &first, &last_plus_one, span_to_beginning_flags); } /* then go right to end of line from line beginning */ (void)ev_span(chain, first, &first, &last_plus_one, span_line_flags); if ((first == last_plus_one) && (insert_pos != 0)) { insert_pos--; first = 0; last_plus_one = 0; if (insert_pos != 0) { /* first go left from insertion pt */ (void)ev_span(chain, insert_pos, &first, &last_plus_one, span_to_beginning_flags); } /* then go right to end of line from line beginning */ (void)ev_span(chain, first, &first, &last_plus_one, span_line_flags); } textsw_set_insert(textsw, last_plus_one); textsw_set_selection(abstract, first, last_plus_one, EV_SEL_PRIMARY); /* span document */ (void)ev_span(chain, first, &first_in_doc, &last_in_doc_plus_one, span_document_flag); /* if document size is not equal to selection */ /* put up alert...may have inadvertently hit esc */ /* with potential to lose edits */ if ((last_plus_one - first) != last_in_doc_plus_one) { /* if name could be a directory */ if (!(int)(textsw->state & TXTSW_NO_CD)) { result = textsw_get_selection_as_filename(textsw, filename, sizeof(filename), ie->ie_locx, ie->ie_locy); /* if cancel -- mistakenly hit ESCAPE accelerator */ if (result != 0) return 2; /* if directory, change to it */ result = textsw_change_directory(textsw, filename, TRUE, ie->ie_locx, ie->ie_locy); /* if file or directory does not exist */ if (result == -1) return 2; } /* if file exists and not a directory */ if (result == -2) { result = alert_prompt( (Frame)window_get(WINDOW_FROM_VIEW(view), WIN_OWNER), &event, ALERT_MESSAGE_STRINGS, "The text has been edited.", "Load File will discard these edits. Please confirm.", 0, ALERT_BUTTON_YES, "Confirm, discard edits", ALERT_BUTTON_NO, "Cancel", 0); if (result == ALERT_FAILED) return 2; if (result == ALERT_NO) return 2; } } if (textsw_load_selection(textsw, ie->ie_locx, ie->ie_locy, (int)(textsw->state & TXTSW_NO_CD)) == 0) { textsw->state &= ~TXTSW_READ_ONLY_ESH; } } return (2);}#if defined(DEBUG) && !defined(lint)static char *header = "fd dev: #, type inode\n";static voiddebug_dump_fds(stream) FILE *stream;{ register int fd; struct stat s; if (stream == 0) stream = stderr; fprintf(stream, header); for (fd = 0; fd < 32; fd++) { if (fstat(fd, &s) != -1) { fprintf(stream, "%2d\t%6d\t%4d\t%5d\n", fd, s.st_dev, s.st_rdev, s.st_ino); } }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -