📄 de.c
字号:
while (dis_line > line) dis_line -= dis_granularity; while (dis_col > col) dis_col -= dis_granularity; while (line >= dis_line + LINES) dis_line += dis_granularity; while (col >= dis_col + COLS) dis_col += dis_granularity; if (old_line != dis_line || old_col != dis_col) { need_redisplay = ALL; }}# if defined(WIN32)# elif defined(MACINTOSH)# define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)# else# define move_cursor(x,y) move(y,x)# endif/* Adjust display so that cursor is visible; move cursor into position *//* Update screen if necessary. */void fix_cursor(void){ normalize_display(); if (need_redisplay != NONE) redisplay(); move_cursor(col - dis_col, line - dis_line); refresh();# ifndef WIN32 fflush(stdout);# endif}/* Make sure line, col, and dis_pos are somewhere inside file. *//* Recompute file_pos. Assumes dis_pos is accurate or past eof */void fix_pos(){ int my_col = col; if ((size_t)line > current_len) line = current_len; file_pos = line_pos(line, &my_col); if (file_pos == CORD_NOT_FOUND) { for (line = current_map -> line, file_pos = current_map -> pos; file_pos < current_len; line++, file_pos = CORD_chr(current, file_pos, '\n') + 1); line--; file_pos = line_pos(line, &col); } else { col = my_col; }}#if defined(WIN32)# define beep() Beep(1000 /* Hz */, 300 /* msecs */) #elif defined(MACINTOSH)# define beep() SysBeep(1)#else/* * beep() is part of some curses packages and not others. * We try to match the type of the builtin one, if any. */#ifdef __STDC__ int beep(void)#else int beep()#endif{ putc('\007', stderr); return(0);}#endif# define NO_PREFIX -1# define BARE_PREFIX -2int repeat_count = NO_PREFIX; /* Current command prefix. */int locate_mode = 0; /* Currently between 2 ^Ls */CORD locate_string = CORD_EMPTY; /* Current search string. */char * arg_file_name;#ifdef WIN32/* Change the current position to whatever is currently displayed at *//* the given SCREEN coordinates. */void set_position(int c, int l){ line = l + dis_line; col = c + dis_col; fix_pos(); move_cursor(col - dis_col, line - dis_line);}#endif /* WIN32 *//* Perform the command associated with character c. C may be an *//* integer > 256 denoting a windows command, one of the above control *//* characters, or another ASCII character to be used as either a *//* character to be inserted, a repeat count, or a search string, *//* depending on the current state. */void do_command(int c){ int i; int need_fix_pos; FILE * out; if ( c == '\r') c = '\n'; if (locate_mode) { size_t new_pos; if (c == LOCATE) { locate_mode = 0; locate_string = CORD_EMPTY; return; } locate_string = CORD_cat_char(locate_string, (char)c); new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1, locate_string); if (new_pos != CORD_NOT_FOUND) { need_redisplay = ALL; new_pos += CORD_len(locate_string); for (;;) { file_pos = line_pos(line + 1, 0); if (file_pos > new_pos) break; line++; } col = new_pos - line_pos(line, 0); file_pos = new_pos; fix_cursor(); } else { locate_string = CORD_substr(locate_string, 0, CORD_len(locate_string) - 1); beep(); } return; } if (c == REPEAT) { repeat_count = BARE_PREFIX; return; } else if (c < 0x100 && isdigit(c)){ if (repeat_count == BARE_PREFIX) { repeat_count = c - '0'; return; } else if (repeat_count != NO_PREFIX) { repeat_count = 10 * repeat_count + c - '0'; return; } } if (repeat_count == NO_PREFIX) repeat_count = 1; if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) { repeat_count = LINES - dis_granularity; } if (repeat_count == BARE_PREFIX) repeat_count = 8; need_fix_pos = 0; for (i = 0; i < repeat_count; i++) { switch(c) { case LOCATE: locate_mode = 1; break; case TOP: line = col = file_pos = 0; break; case UP: if (line != 0) { line--; need_fix_pos = 1; } break; case DOWN: line++; need_fix_pos = 1; break; case LEFT: if (col != 0) { col--; file_pos--; } break; case RIGHT: if (CORD_fetch(current, file_pos) == '\n') break; col++; file_pos++; break; case UNDO: del_hist(); need_redisplay = ALL; need_fix_pos = 1; break; case BS: if (col == 0) { beep(); break; } col--; file_pos--; /* fall through: */ case DEL: if (file_pos == current_len-1) break; /* Can't delete trailing newline */ if (CORD_fetch(current, file_pos) == '\n') { need_redisplay = ALL; need_fix_pos = 1; } else { need_redisplay = line - dis_line; } add_hist(CORD_cat( CORD_substr(current, 0, file_pos), CORD_substr(current, file_pos+1, current_len))); invalidate_map(line); break; case WRITE: { CORD name = CORD_cat(CORD_from_char_star(arg_file_name), ".new"); if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL || CORD_put(current, out) == EOF) { de_error("Write failed\n"); need_redisplay = ALL; } else { fclose(out); } } break; default: { CORD left_part = CORD_substr(current, 0, file_pos); CORD right_part = CORD_substr(current, file_pos, current_len); add_hist(CORD_cat(CORD_cat_char(left_part, (char)c), right_part)); invalidate_map(line); if (c == '\n') { col = 0; line++; file_pos++; need_redisplay = ALL; } else { col++; file_pos++; need_redisplay = line - dis_line; } break; } } } if (need_fix_pos) fix_pos(); fix_cursor(); repeat_count = NO_PREFIX;}/* OS independent initialization */void generic_init(void){ FILE * f; CORD initial; if ((f = fopen(arg_file_name, "rb")) == NULL) { initial = "\n"; } else { initial = CORD_from_file(f); if (initial == CORD_EMPTY || CORD_fetch(initial, CORD_len(initial)-1) != '\n') { initial = CORD_cat(initial, "\n"); } } add_map(0,0); add_hist(initial); now -> map = current_map; now -> previous = now; /* Can't back up further: beginning of the world */ need_redisplay = ALL; fix_cursor();}#ifndef WIN32main(argc, argv)int argc;char ** argv;{ int c;#if defined(MACINTOSH) console_options.title = "\pDumb Editor"; cshow(stdout); argc = ccommand(&argv);#endif GC_INIT(); if (argc != 2) goto usage; arg_file_name = argv[1]; setvbuf(stdout, GC_MALLOC_ATOMIC(8192), _IOFBF, 8192); initscr(); noecho(); nonl(); cbreak(); generic_init(); while ((c = getchar()) != QUIT) { if (c == EOF) break; do_command(c); }done: move(LINES-1, 0); clrtoeol(); refresh(); nl(); echo(); endwin(); exit(0);usage: fprintf(stderr, "Usage: %s file\n", argv[0]); fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n"); fprintf(stderr, "Undo: ^U Write to <file>.new: ^W"); fprintf(stderr, "Quit:^D Repeat count: ^R[n]\n"); fprintf(stderr, "Top: ^T Locate (search, find): ^L text ^L\n"); exit(1);}#endif /* !WIN32 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -